Last active
August 29, 2015 14:24
-
-
Save fjarrett/7c30eef588233fec1817 to your computer and use it in GitHub Desktop.
Register post type helper class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/////////////////////////////////////////////////////////////////////////////////// | |
// Please use within a namespace, or change this helper class name. // | |
// Don't forget to properly prefix the apply_filters() calls made throughout. // | |
// // | |
// Example usage: // | |
// // | |
// new Post_Type( 'book', 'Book', 'Books' ); // | |
// new Post_Type( 'video', 'Video', 'Videos', array( 'has_archive' => false ) ); // | |
/////////////////////////////////////////////////////////////////////////////////// | |
class Post_Type { | |
/** | |
* Post type name/slug | |
* | |
* @var string | |
*/ | |
protected $name; | |
/** | |
* Post type singular label | |
* | |
* @var string | |
*/ | |
protected $singular; | |
/** | |
* Post type plural label | |
* | |
* @var string | |
*/ | |
protected $plural; | |
/** | |
* Post type args | |
* | |
* @var array | |
*/ | |
protected $args = array(); | |
/** | |
* Class constructor | |
* | |
* @param string $name | |
* @param string $singular (optional) Defaults to $name | |
* @param string $plural (optional) Defaults to $name | |
* @param array $args (optional) | |
*/ | |
public function __construct( $name, $singular = null, $plural = null, $args = array() ) { | |
$this->name = sanitize_key( $name ); | |
$this->singular = ! empty( $singular ) ? $singular : $name; | |
$this->plural = ! empty( $plural ) ? $plural : $name; | |
$this->args = $this->get_args( (array) $args ); | |
add_action( 'init', function() { | |
register_post_type( $this->name, $this->args ); | |
} ); | |
} | |
/** | |
* Get post type args | |
* | |
* @param array $args | |
* | |
* @return array | |
*/ | |
private function get_args( $args ) { | |
/** | |
* Filter the default args used for post types | |
* | |
* @param array $default_args | |
*/ | |
$defaults = (array) apply_filters( 'myprefix_post_type_default_args', | |
array( | |
'public' => true, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'query_var' => true, | |
'rewrite' => array( 'slug' => sanitize_key( $this->plural ) ), | |
'capability_type' => 'post', | |
'has_archive' => true, | |
'hierarchical' => false, | |
'menu_position' => null, | |
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ), | |
'labels' => array( | |
'name' => $this->plural, | |
'singular_name' => $this->singular, | |
'menu_name' => $this->plural, | |
'name_admin_bar' => $this->singular, | |
'add_new' => _x( 'Add New', $this->name, 'text-domain' ), | |
'add_new_item' => sprintf( __( 'Add New %s', 'text-domain' ), $this->singular ), | |
'new_item' => sprintf( __( 'New %s', 'text-domain' ), $this->singular ), | |
'edit_item' => sprintf( __( 'Edit %s', 'text-domain' ), $this->singular ), | |
'view_item' => sprintf( __( 'View %s', 'text-domain' ), $this->singular ), | |
'all_items' => sprintf( __( 'All %s', 'text-domain' ), $this->plural ), | |
'search_items' => sprintf( __( 'Search %s', 'text-domain' ), $this->plural ), | |
'parent_item_colon' => sprintf( __( 'Parent %s:', 'text-domain' ), $this->plural ), | |
'not_found' => sprintf( __( 'No %s found', 'text-domain' ), mb_strtolower( $this->plural ) ), | |
'not_found_in_trash' => sprintf( __( 'No %s found in Trash', 'text-domain' ), mb_strtolower( $this->plural ) ), | |
), | |
) | |
); | |
/** | |
* Filter the args for a given post type | |
* | |
* @param array $args | |
*/ | |
return (array) apply_filters( "myprefix_{$this->name}_post_type_args", self::assoc_array_merge_recursive( $defaults, $args ) ); | |
} | |
/** | |
* Recursively merge two or more associative arrays and preserve all keys | |
* | |
* @param array $array1 | |
* @param array $array2 | |
* @param array $... | |
* | |
* @return array | |
*/ | |
public static function assoc_array_merge_recursive( array $array1, array $array2 ) { | |
$result = array(); | |
for ( $i = 0, $total = func_num_args(); $i < $total; $i++ ) { | |
$_array = func_get_arg( $i ); | |
$is_assoc = self::is_accoc_array( $_array ); | |
foreach ( $_array as $key => &$value ) { | |
if ( is_array( $value ) && isset( $result[ $key ] ) && is_array( $result[ $key ] ) ) { | |
$result[ $key ] = self::assoc_array_merge_recursive( $result[ $key ], $value ); | |
continue; | |
} | |
if ( ! $is_assoc && ! in_array( $value, $merged, true ) ) { | |
$result[] = $value; | |
continue; | |
} | |
$result[ $key ] = $value; | |
} | |
} | |
return (array) $result; | |
} | |
/** | |
* Determine if an array is associative | |
* | |
* @param array $array | |
* | |
* @return bool | |
*/ | |
public static function is_accoc_array( array $array ) { | |
return ( array_keys( $array ) !== range( 0, count( $array ) - 1 ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment