Get file permissions, owner and group with PHP

Here are a few functions to help you when interacting with files:

Find the owner of a file:
An array of information about the owner is returned.

function foo_get_file_ownership($file){
	$stat = stat($file);
	if($stat){
		$group = posix_getgrgid($stat[5]);
		$user = posix_getpwuid($stat[4]);
		return compact('user', 'group');
	}
	else
		return false;
}

Get the four digit file permissions number:
A permissions string is returned. Example: 0755

function foo_get_file_perms($file){
	return substr(sprintf('%o', fileperms($file)), -4);
}

Convert permissions number to Read Write eXecute format:
Directory Example: 0755 converts to drwxr-xr-x
File Example: 0664 coverts to -rw-rw-r–

function foo_convert_perms_to_rwx($perms, $file){
	$rwx = array(
		'---',
		'--x',
		'-w-',
		'-wx',
		'r--',
		'r-x',
		'rw-',
		'rwx'
	);
	$type = is_dir($file) ? 'd' : '-';
	$owner = $perms[1];
	$group = $perms[2];
	$public = $perms[3];
	return $type.$rwx[$owner].$rwx[$group].$rwx[$public];
}

Kint Debugger for WordPress

We just released a new UpThemes plugin called Kint Debugger for WordPress. It’s a simple wrapper for the awesome Kint debugger class. It allows you to view variable dumps in a styled and collapsible output. Kint also allows for easy back-tracing.

Examples

global $wp_query;
d($wp_query); //styled, collapsible output
s($wp_query); //un-styled output

I added a couple WordPress specific functions to aid in plugin and theme development.

dump_wp_query();
dump_wp();
dump_post();

View the entire documentation at the Kint site.

Check out the UpThemes plugin page.

Download Kint for WordPress

PHP – Passing data between classes

Static class properties are the most efficient way to pass information from one class to another. Typically, one would place a variable in the global scope for other functions and classes to use. However, this is not a good practice as setting GLOBALS equates to a downgrade in performance. Using static properties allows you to store/retrieve data without instantiating a class object.

class IntermediaryData{
    public static $global = null;

    public function set($data){	
        return self::$global = $data;
    }
	
    public function get(){
        return self::$global;
    }
}

Use this in your classes to set and retrieve data:

IntermediaryData::get();
IntermediaryData::set($data);

A good use case for this functionality is passing a rolling list of ids to exclude from queries from widget to widget within a dynamic WordPress sidebar.

Display hidden post types automatically in WP nav menus admin

One of the most annoying things about WP nav menu admin is when you create a custom post type, you first must go to Screen Options and check the custom post type checkbox to display it for use. Here’s a hack I created to get around this and force the custom post type to display for all users all the time.

function display_post_type_nav_box(){

    $hidden_nav_boxes = get_user_option( 'metaboxhidden_nav-menus' );

    $post_type = 'foobar'; //Can also be a taxonomy slug
    $post_type_nav_box = 'add-'.$post_type;

    if(in_array($post_type_nav_box, $hidden_nav_boxes)):
        foreach ($hidden_nav_boxes as $i => $nav_box):
            if($nav_box == $post_type_nav_box)
                unset($hidden_nav_boxes[$i]);
        endforeach;
        update_user_option(get_current_user_id(), 'metaboxhidden_nav-menus', $hidden_nav_boxes);
    endif;
}
add_action('admin_init', 'display_post_type_nav_box');