diff --git a/CHANGELOG.md b/CHANGELOG.md index f7acbd3..8f8029b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +##### v1.3.0 +* fix translation issues +* new method to set custom textdomain with `set_textdomain()` + ##### v1.2.4 * add check if `$filter` array is empty @@ -28,4 +32,4 @@ ##### v1.0.1 * fixed issue with registering taxonomies * fixed issue with options merge, now accepts boolean -* register_taxonomy method can now register exisiting taxonomies to post type \ No newline at end of file +* register_taxonomy method can now register exisiting taxonomies to post type diff --git a/README.md b/README.md index 9a7de9a..c5f260b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# WP Custom Post Type Class v1.2.4 +# WP Custom Post Type Class v1.3 A single class to help you build more advanced custom post types quickly. @@ -217,10 +217,17 @@ $books->menu_icon("dashicons-book-alt"); For a full list of icons and the class names to use visit [http://melchoyce.github.io/dashicons/](http://melchoyce.github.io/dashicons/) +### Translation + +The class is setup for translation, but if you need to set your own textdomain to work with your theme or plugin use the `set_textdomain()` method: + +```php +$books->set_textdomain('your-textdomain'); +``` ## Notes -* The class has no methods for making custom fields for post types, use [Advanced Custom Fields (ACF)](http://advancedcustomfields.com) +* The class has no methods for making custom fields for post types, use [Advanced Custom Fields](http://advancedcustomfields.com) * The books example used in the README.md can be found in the [books-post-type.php](examples/books-post-type.php) * Licensed under the [MIT License](https://github.com/jjgrainger/wp-custom-post-type-class/blob/master/LICENSE) * Maintained under the [Semantic Versioning Guide](http://semver.org) diff --git a/src/CPT.php b/src/CPT.php index 58c0fec..72b0ea6 100644 --- a/src/CPT.php +++ b/src/CPT.php @@ -1,1114 +1,988 @@ post_type_name = $post_type_names['post_type_name']; - - // cycle through possible names - foreach($names as $name) { - - // if the name has been set by user - if(isset($post_type_names[$name])) { - - // use the user setting - $this->$name = $post_type_names[$name]; - - // else generate the name + /** + * Holds the plural name of the post type. This is a human friendly + * name, capitalized with spaces assigned on __construct(). + * + * @var string $plural Singular post type name. + */ + public $plural; + + /** + * Post type slug. This is a robot friendly name, all lowercase and uses + * hyphens assigned on __construct(). + * + * @var string $slug Holds the post type slug name. + */ + public $slug; + + /** + * User submitted options assigned on __construct(). + * + * @var array $options Holds the user submitted post type options. + */ + public $options; + + /** + * Taxonomies + * + * @var array $taxonomies Holds an array of taxonomies associated with the post type. + */ + public $taxonomies; + + /** + * Taxonomy settings, an array of the taxonomies associated with the post + * type and their options used when registering the taxonomies. + * + * @var array $taxonomy_settings Holds the taxonomy settings. + */ + public $taxonomy_settings; + + /** + * Taxonomy filters. Defines which filters are to appear on admin edit + * screen used in add_taxonmy_filters(). + * + * @var array $filters Taxonomy filters. + */ + public $filters; + + /** + * Defines which columns are to appear on the admin edit screen used + * in add_admin_columns(). + * + * @var array $columns Columns visible in admin edit screen. + */ + public $columns; + + /** + * User defined functions to populate admin columns. + * + * @var array $custom_populate_columns User functions to populate columns. + */ + public $custom_populate_columns; + + /** + * Sortable columns. + * + * @var array $sortable Define which columns are sortable on the admin edit screen. + */ + public $sortable; + + /** + * Textdomain used for translation. Use the set_textdomain() method to set a custom textdomain. + * + * @var string $textdomain Used for internationalising. Defaults to "cpt" without quotes. + */ + public $textdomain = 'cpt'; + + /** + * Constructor + * + * Register a custom post type. + * + * @param mixed $post_type_names The name(s) of the post type, accepts (post type name, slug, plural, singular). + * @param array $options User submitted options. + */ + function __construct( $post_type_names, $options = array() ) { + + // Check if post type names is a string or an array. + if ( is_array( $post_type_names ) ) { + + // Add names to object. + $names = array( + 'singular', + 'plural', + 'slug' + ); + + // Set the post type name. + $this->post_type_name = $post_type_names['post_type_name']; + + // Cycle through possible names. + foreach ( $names as $name ) { + + // If the name has been set by user. + if ( isset( $post_type_names[ $name ] ) ) { + + // Use the user setting + $this->$name = $post_type_names[ $name ]; + + // Else generate the name. } else { - // define the method to be used - $method = 'get_'.$name; - - // generate the name - $this->$name = $this->$method(); - - } - - } - - // else the post type name is only supplied - } else { - - // apply to post type name - $this->post_type_name = $post_type_names; - - // set the slug name - $this->slug = $this->get_slug(); - - // set the plural name label - $this->plural = $this->get_plural(); - - // set the singular name label - $this->singular = $this->get_singular(); - - } - - // set the user submitted options to the object - $this->options = $options; - - // register the post type - $this->add_action('init', array(&$this, 'register_post_type')); - - // register taxonomies - $this->add_action('init', array(&$this, 'register_taxonomies')); - - // add taxonomy to admin edit columns - $this->add_filter('manage_edit-' . $this->post_type_name . '_columns', array(&$this, 'add_admin_columns')); - - // populate the taxonomy columns with the posts terms - $this->add_action('manage_' . $this->post_type_name . '_posts_custom_column', array(&$this, 'populate_admin_columns'), 10, 2); - - // add filter select option to admin edit - $this->add_action('restrict_manage_posts', array(&$this, 'add_taxonomy_filters')); - - } - - - - /* - helper function get - used to get an object variable - - @param string $var the variable you would like to retrieve - @return mixed returns the value on sucess, bool (false) when fails + // define the method to be used + $method = 'get_' . $name; - */ + // Generate the name + $this->$name = $this->$method(); + } + } - function get($var) { + // Else the post type name is only supplied. + } else { - // if the variable exisits - if($this->$var) { + // Apply to post type name. + $this->post_type_name = $post_type_names; - // on success return the value - return $this->$var; + // Set the slug name. + $this->slug = $this->get_slug(); - } else { - - // on fail return false - return false; - - } - } - - - - /* - helper function set - used to set an object variable - can overwrite exisiting variables and create new ones - cannot overwrite reserved variables - - @param mixed $var the variable you would like to create/overwrite - @param mixed $value the value you would like to set to the variable - - */ - - function set($var, $value) { - - // an array of reserved variables that cannot be overwritten - $reserved = array( - 'config', - 'post_type_name', - 'singular', - 'plural', - 'slug', - 'options', - 'taxonomies' - ); - - // if the variable is not a reserved variable - if(!in_array($var, $reserved)) { - - // write variable and value - $this->$var = $value; - - } - - } - - - - /* - helper function add_action - used to create add_action wordpress filter - - see Wordpress Codex - http://codex.wordpress.org/Function_Reference/add_action - - @param string $action name of the action to hook to, e.g 'init' - @param string $function function to hook that will run on @action - @param int $priority order in which to execute the function, relation to other function hooked to this action - @param int $accepted_args the number of arguements the function accepts - */ - - function add_action($action, $function, $priority = 10, $accepted_args = 1) { - - // pass variables into Wordpress add_action function - add_action($action, $function, $priority, $accepted_args); - - } - - - - /* - helper function add_filter - used to create add_filter wordpress filter - - see Wordpress Codex - http://codex.wordpress.org/Function_Reference/add_filter - - @param string $action name of the action to hook to, e.g 'init' - @param string $function function to hook that will run on @action - @param int $priority order in which to execute the function, relation to other function hooked to this action - @param int $accepted_args the number of arguements the function accepts - */ - - function add_filter($action, $function, $priority = 10, $accepted_args = 1) { - - // pass variables into Wordpress add_action function - add_filter($action, $function, $priority, $accepted_args); - - } - - - - /* - helper function get slug - creates url friendly slug - - @param string $name name to slugify - @return string $name returns the slug - */ - - function get_slug($name = null) { - - // if no name set use the post type name - if(!isset($name)) { - - $name = $this->post_type_name; + // Set the plural name label. + $this->plural = $this->get_plural(); + // Set the singular name label. + $this->singular = $this->get_singular(); } - // name to lower case - $name = strtolower($name); - - // replace spaces with hyphen - $name = str_replace(" ", "-", $name); - - // replace underscore with hyphen - $name = str_replace("_", "-", $name); - - return $name; - - } - - + // Set the user submitted options to the object. + $this->options = $options; - /* - helper function get_plural - returns the friendly plural name + // Register the post type. + $this->add_action( 'init', array( &$this, 'register_post_type' ) ); - ucwords capitalize words - strtolower makes string lowercase before capitalizing - str_replace replace all instances of _ to space + // Register taxonomies. + $this->add_action( 'init', array( &$this, 'register_taxonomies' ) ); - @param string $name the slug name you want to pluralize - @return string the friendly pluralized name - */ + // Add taxonomy to admin edit columns. + $this->add_filter( 'manage_edit-' . $this->post_type_name . '_columns', array( &$this, 'add_admin_columns' ) ); - function get_plural($name = null) { + // Populate the taxonomy columns with the posts terms. + $this->add_action( 'manage_' . $this->post_type_name . '_posts_custom_column', array( &$this, 'populate_admin_columns' ), 10, 2 ); - // if no name is passed the post_type_name is used - if(!isset($name)) { - - $name = $this->post_type_name; - - } - - // return the plural name - // add 's' to the end - return $this->get_human_friendly($name) . 's'; + // Add filter select option to admin edit. + $this->add_action( 'restrict_manage_posts', array( &$this, 'add_taxonomy_filters' ) ); } + /** + * Get + * + * Helper function to get an object variable. + * + * @param string $var The variable you would like to retrieve. + * @return mixed Returns the value on success, boolean false whe it fails. + */ + function get( $var ) { + // If the variable exists. + if ( $this->$var ) { - /* - helper function get_singular - returns the friendly singular name - - ucwords capitalize words - strtolower makes string lowercase before capitalizing - str_replace replace all instances of _ to space + // On success return the value. + return $this->$var; - @param string $name the slug name you want to unpluralize - @return string the friendly singular name - */ - - function get_singular($name = null) { - - // if no name is passed the post_type_name is used - if(!isset($name)) { - - $name = $this->post_type_name; + } else { + // on fail return false + return false; } - - // return the string - return $this->get_human_friendly($name); - } - - - /* - helper function get_human_friendly - returns the human friendly name - - ucwords capitalize words - strtolower makes string lowercase before capitalizing - str_replace replace all instances of hyphens and underscores to spaces - - @param string $name the name you want to make friendly - @return string the human friendly name - */ - - function get_human_friendly($name = null) { - - // if no name is passed the post_type_name is used - if(!isset($name)) { - - $name = $this->post_type_name; - - } - - // return human friendly name - return ucwords(strtolower(str_replace("-", " ", str_replace("_", " ", $name)))); - + /** + * Set + * + * Helper function used to set an object variable. Can overwrite existsing + * variables or create new ones. Cannot overwrite reserved variables. + * + * @param mixed $var The variable you would like to create/overwrite. + * @param mixed $value The value you would like to set to the variable. + */ + function set( $var, $value ) { + + // An array of reserved variables that cannot be overwritten. + $reserved = array( + 'config', + 'post_type_name', + 'singular', + 'plural', + 'slug', + 'options', + 'taxonomies' + ); + + // If the variable is not a reserved variable + if ( ! in_array( $var, $reserved ) ) { + + // Write variable and value + $this->$var = $value; + } + } + + /** + * Add Action + * + * Helper function to add add_action WordPress filters. + * + * @param string $action Name of the action. + * @param string $function Function to hook that will run on action. + * @param integet $priority Order in which to execute the function, relation to other functions hooked to this action. + * @param integer $accepted_args The number of arguments the function accepts. + */ + function add_action( $action, $function, $priority = 10, $accepted_args = 1 ) { + + // Pass variables into WordPress add_action function + add_action( $action, $function, $priority, $accepted_args ); } - - - /* - register_post_type function - object function to register the post type - - see Wordpress Codex - http://codex.wordpress.org/Function_Reference/register_post_type - */ - - function register_post_type() { - - // friendly post type names - $plural = $this->plural; - $singular = $this->singular; - $slug = $this->slug; - - // default labels - $labels = array( - 'name' => __($plural), - 'singular_name' => __($singular), - 'menu_name' => __($plural), - 'all_items' => __($plural), - 'add_new' => _('Add New'), - 'add_new_item' => __('Add New ' . $singular), - 'edit_item' => __('Edit ' . $singular), - 'new_item' => __('New ' . $singular), - 'view_item' => __('View ' . $singular), - 'search_items' => __('Search ' . $plural), - 'not_found' => __('No ' . $plural . ' found'), - 'not_found_in_trash' => __('No ' . $plural . ' found in Trash'), - 'parent_item_colon' => __('Parent ' . $singular . ':') - ); - - // default options - $defaults = array( - 'labels' => $labels, - 'public' => true, - 'rewrite' => array( - 'slug' => $slug, - ) - ); - - // merge user submitted options with defaults - $options = array_replace_recursive($defaults, $this->options); - - // set the object options as full options passed - $this->options = $options; - - // check that the post type doesn't already exist - if(!post_type_exists($this->post_type_name)) { - - // register the post type - register_post_type($this->post_type_name, $options); - - } - + /** + * Add Filter + * + * Create add_filter WordPress filter. + * + * @see http://codex.wordpress.org/Function_Reference/add_filter + * + * @param string $action Name of the action to hook to, e.g 'init'. + * @param string $function Function to hook that will run on @action. + * @param int $priority Order in which to execute the function, relation to other function hooked to this action. + * @param int $accepted_args The number of arguements the function accepts. + */ + function add_filter( $action, $function, $priority = 10, $accepted_args = 1 ) { + + // Pass variables into Wordpress add_action function + add_filter( $action, $function, $priority, $accepted_args ); } - - - /* - function register_taxonomy - register a taxonomy to a post type - - @param string $taxonomy_name the slug for the taxonomy - @param array $options taxonomy options - - see Wordpress codex - http://codex.wordpress.org/Function_Reference/register_taxonomy - */ - - function register_taxonomy($taxonomy_names, $options = array()) { - - // post type defaults to $this post type if unspecified - $post_type = $this->post_type_name; - - // an array of the names required excluding taxonomy_name - $names = array( - 'singular', - 'plural', - 'slug' - ); - - // if an array of names are passed - if(is_array($taxonomy_names)) { - - // set the taxonomy name - $taxonomy_name = $taxonomy_names['taxonomy_name']; - - // cycle through possible names - foreach($names as $name) { - - // if the user has set the name - if(isset($taxonomy_names[$name])) { - - // use user submitted name - $$name = $taxonomy_names[$name]; - - // else generate the name - } else { - - // define the fnction to be used - $method = 'get_'.$name; - - // generate the name - $$name = $this->$method($taxonomy_name); - - } - - } - - // else if only the taxonomy_name has been supplied - } else { - - // create user friendly names - $taxonomy_name = $taxonomy_names; - $singular = $this->get_singular($taxonomy_name); - $plural = $this->get_plural($taxonomy_name); - $slug = $this->get_slug($taxonomy_name); - - } - - // default labels - $labels = array( - 'name' => _($plural), - 'singular_name' => _($singular), - 'menu_name' => __($plural), - 'all_items' => __('All ' . $plural), - 'edit_item' => __('Edit ' . $singular), - 'view_item' => __('View ' . $singular), - 'update_item' => __('Update ' . $singular), - 'add_new_item' => __('Add New ' . $singular), - 'new_item_name' => __('New ' . $singular . ' Name'), - 'parent_item' => __('Parent ' . $plural), - 'parent_item_colon' => __('Parent ' . $plural .':'), - 'search_items' => __('Search ' . $plural), - 'popular_items' => __('Popular ' . $plural), - 'separate_items_with_commas' => __('Seperate ' . $plural . ' with commas'), - 'add_or_remove_items' => __('Add or remove ' . $plural), - 'choose_from_most_used' => __('Choose from most used ' . $plural), - 'not_found' => __('No ' . $plural . ' found'), - ); - - // default options - $defaults = array( - 'labels' => $labels, - 'hierarchical' => true, - 'rewrite' => array( - 'slug' => $slug - ) - ); - - // merge default options with user submitted options - $options = array_replace_recursive($defaults, $options); - - // add the taxonomy to the object array - // this is used to add columns and filters to admin pannel - $this->taxonomies[] = $taxonomy_name; - - // create array used when registering taxonomies - $this->taxonomy_settings[$taxonomy_name] = $options; - + /** + * Get slug + * + * Creates an url friendly slug. + * + * @param string $name Name to slugify. + * @return string $name Returns the slug. + */ + function get_slug( $name = null ) { + + // If no name set use the post type name. + if ( ! isset( $name ) ) { + + $name = $this->post_type_name; + } + + // Name to lower case. + $name = strtolower( $name ); + + // Replace spaces with hyphen. + $name = str_replace( " ", "-", $name ); + + // Replace underscore with hyphen. + $name = str_replace( "_", "-", $name ); + + return $name; + } + + /** + * Get plural + * + * Returns the friendly plural name. + * + * ucwords capitalize words + * strtolower makes string lowercase before capitalizing + * str_replace replace all instances of _ to space + * + * @param string $name The slug name you want to pluralize. + * @return string the friendly pluralized name. + */ + function get_plural( $name = null ) { + + // If no name is passed the post_type_name is used. + if ( ! isset( $name ) ) { + + $name = $this->post_type_name; + } + + // Return the plural name. Add 's' to the end. + return $this->get_human_friendly( $name ) . 's'; } - - - /* - function register_taxonomies - cycles through taxonomies added with the class and registers them - - function is used with add_action - */ - function register_taxonomies() { - - if(is_array($this->taxonomy_settings)) { - // foreach taxonomy registered with the post type - foreach($this->taxonomy_settings as $taxonomy_name => $options) { - - // register the taxonomy if it doesn't exist - if(!taxonomy_exists($taxonomy_name)) { - - // register the taxonomy with Wordpress - register_taxonomy($taxonomy_name, $this->post_type_name, $options); - - - } else { - - // if taxonomy exists, attach exisiting taxonomy to post type - register_taxonomy_for_object_type($taxonomy_name, $this->post_type_name); - - } - } - } - + /** + * Get singular + * + * Returns the friendly singular name. + * + * ucwords capitalize words + * strtolower makes string lowercase before capitalizing + * str_replace replace all instances of _ to space + * + * @param string $name The slug name you want to unpluralize. + * @return string The friendly singular name. + */ + function get_singular( $name = null ) { + + // If no name is passed the post_type_name is used. + if ( ! isset( $name ) ) { + + $name = $this->post_type_name; + + } + + // Return the string. + return $this->get_human_friendly( $name ); } + /** + * Get human friendly + * + * Returns the human friendly name. + * + * ucwords capitalize words + * strtolower makes string lowercase before capitalizing + * str_replace replace all instances of hyphens and underscores to spaces + * + * @param string $name The name you want to make friendly. + * @return string The human friendly name. + */ + function get_human_friendly( $name = null ) { + + // If no name is passed the post_type_name is used. + if ( ! isset( $name ) ) { + + $name = $this->post_type_name; + } + + // Return human friendly name. + return ucwords( strtolower( str_replace( "-", " ", str_replace( "_", " ", $name ) ) ) ); + } + + /** + * Register Post Type + * + * @see http://codex.wordpress.org/Function_Reference/register_post_type + */ + function register_post_type() { + + // Friendly post type names. + $plural = $this->plural; + $singular = $this->singular; + $slug = $this->slug; + + // Default labels. + $labels = array( + 'name' => sprintf( __( '%s', $this->textdomain ), $plural ), + 'singular_name' => sprintf( __( '%s', $this->textdomain ), $singular ), + 'menu_name' => sprintf( __( '%s', $this->textdomain ), $plural ), + 'all_items' => sprintf( __( '%s', $this->textdomain ), $plural ), + 'add_new' => __( 'Add New', $this->textdomain ), + 'add_new_item' => sprintf( __( 'Add New %s', $this->textdomain ), $singular ), + 'edit_item' => sprintf( __( 'Edit %s', $this->textdomain ), $singular ), + 'new_item' => sprintf( __( 'New %s', $this->textdomain ), $singular ), + 'view_item' => sprintf( __( 'View %s', $this->textdomain ), $singular ), + 'search_items' => sprintf( __( 'Search %s', $this->textdomain ), $plural ), + 'not_found' => sprintf( __( 'No %s found', $this->textdomain ), $plural ), + 'not_found_in_trash' => sprintf( __( 'No %s found in Trash', $this->textdomain ), $plural ), + 'parent_item_colon' => sprintf( __( 'Parent %s:', $this->textdomain ), $singular ) + ); + + // Default options. + $defaults = array( + 'labels' => $labels, + 'public' => true, + 'rewrite' => array( + 'slug' => $slug, + ) + ); + + // Merge user submitted options with defaults. + $options = array_replace_recursive( $defaults, $this->options ); + + // Set the object options as full options passed. + $this->options = $options; + + // Check that the post type doesn't already exist. + if ( ! post_type_exists( $this->post_type_name ) ) { + + // Register the post type. + register_post_type( $this->post_type_name, $options ); + } + } + + /** + * Register taxonomy + * + * @see http://codex.wordpress.org/Function_Reference/register_taxonomy + * + * @param string $taxonomy_name The slug for the taxonomy. + * @param array $options Taxonomy options. + */ + function register_taxonomy($taxonomy_names, $options = array()) { + + // Post type defaults to $this post type if unspecified. + $post_type = $this->post_type_name; + + // An array of the names required excluding taxonomy_name. + $names = array( + 'singular', + 'plural', + 'slug' + ); + + // if an array of names are passed + if ( is_array( $taxonomy_names ) ) { + + // Set the taxonomy name + $taxonomy_name = $taxonomy_names['taxonomy_name']; + + // Cycle through possible names. + foreach ( $names as $name ) { + + // If the user has set the name. + if ( isset( $taxonomy_names[ $name ] ) ) { + + // Use user submitted name. + $$name = $taxonomy_names[ $name ]; + + // Else generate the name. + } else { + + // Define the function to be used. + $method = 'get_' . $name; + + // Generate the name + $$name = $this->$method( $taxonomy_name ); + + } + } + + // Else if only the taxonomy_name has been supplied. + } else { + + // Create user friendly names. + $taxonomy_name = $taxonomy_names; + $singular = $this->get_singular( $taxonomy_name ); + $plural = $this->get_plural( $taxonomy_name ); + $slug = $this->get_slug( $taxonomy_name ); + + } + + // Default labels. + $labels = array( + 'name' => sprintf( __( '%s', $this->textdomain ), $plural ), + 'singular_name' => sprintf( __( '%s', $this->textdomain ), $singular ), + 'menu_name' => sprintf( __( '%s', $this->textdomain ), $plural ), + 'all_items' => sprintf( __( 'All %s', $this->textdomain ), $plural ), + 'edit_item' => sprintf( __( 'Edit %s', $this->textdomain ), $singular ), + 'view_item' => sprintf( __( 'View %s', $this->textdomain ), $singular ), + 'update_item' => sprintf( __( 'Update %s', $this->textdomain ), $singular ), + 'add_new_item' => sprintf( __( 'Add New %s', $this->textdomain ), $singular ), + 'new_item_name' => sprintf( __( 'New %s Name', $this->textdomain ), $singular ), + 'parent_item' => sprintf( __( 'Parent %s', $this->textdomain ), $plural ), + 'parent_item_colon' => sprintf( __( 'Parent %s:', $this->textdomain ), $plural ), + 'search_items' => sprintf( __( 'Search %s', $this->textdomain ), $plural ), + 'popular_items' => sprintf( __( 'Popular %s', $this->textdomain ), $plural ), + 'separate_items_with_commas' => sprintf( __( 'Seperate %s with commas', $this->textdomain ), $plural ), + 'add_or_remove_items' => sprintf( __( 'Add or remove %s', $this->textdomain ), $plural ), + 'choose_from_most_used' => sprintf( __( 'Choose from most used %s', $this->textdomain ), $plural ), + 'not_found' => sprintf( __( 'No %s found', $this->textdomain ), $plural ), + ); + + // Default options. + $defaults = array( + 'labels' => $labels, + 'hierarchical' => true, + 'rewrite' => array( + 'slug' => $slug + ) + ); + + // Merge default options with user submitted options. + $options = array_replace_recursive( $defaults, $options ); + + // Add the taxonomy to the object array, this is used to add columns and filters to admin panel. + $this->taxonomies[] = $taxonomy_name; + + // Create array used when registering taxonomies. + $this->taxonomy_settings[ $taxonomy_name ] = $options; + + } + - /* - function add_admin_columns - adds columns to the admin edit screen - - function is used with add_action - */ - - function add_admin_columns($columns) { - + /** + * Register taxonomies + * + * Cycles through taxonomies added with the class and registers them. + */ + function register_taxonomies() { + + if ( is_array( $this->taxonomy_settings ) ) { - // if no user columns have been specified use following defaults - if(!isset($this->columns)) { + // Foreach taxonomy registered with the post type. + foreach ( $this->taxonomy_settings as $taxonomy_name => $options ) { - // default columns - $columns = array( - 'cb' => '', - 'title' => __('Title') - ); + // Register the taxonomy if it doesn't exist. + if ( ! taxonomy_exists( $taxonomy_name ) ) { - // if there are taxonomies registered to the post type - if(is_array($this->taxonomies)) { + // Register the taxonomy with Wordpress + register_taxonomy( $taxonomy_name, $this->post_type_name, $options ); - // create a column for each taxonomy - foreach($this->taxonomies as $tax) { + } else { - // get the taxonomy object for labels - $taxonomy_object = get_taxonomy($tax); + // If taxonomy exists, attach exisiting taxonomy to post type. + register_taxonomy_for_object_type( $taxonomy_name, $this->post_type_name ); + } + } + } + } - // column key is the slug, value is friendly name - $columns[$tax] = __($taxonomy_object->labels->name); - } - } - - // if post type supports comments - if(post_type_supports($this->post_type_name, 'comments')) { - - $columns['comments'] = 'Comments'; - - } + /** + * Add admin columns + * + * Adds columns to the admin edit screen. Function is used with add_action + * + * @param array $columns Columns to be added to the admin edit screen. + * @return array + */ + function add_admin_columns( $columns ) { - // add date of post to end of columns - $columns['date'] = __('Date'); + // If no user columns have been specified use following defaults. + if ( ! isset( $this->columns ) ) { - } else { + // Default columns + $columns = array( + 'cb' => '', + 'title' => __( 'Title', $this->textdomain ) + ); - // use user submitted columns - // these are defined using the object columns() method - $columns = $this->columns; + // If there are taxonomies registered to the post type. + if ( is_array( $this->taxonomies ) ) { - } + // Create a column for each taxonomy. + foreach( $this->taxonomies as $tax ) { - return $columns; + // Get the taxonomy object for labels. + $taxonomy_object = get_taxonomy( $tax ); - } + // Column key is the slug, value is friendly name. + $columns[ $tax ] = sprintf( __( '%s', $this->textdomain ), $taxonomy_object->labels->name ); + } + } + // If post type supports comments. + if ( post_type_supports( $this->post_type_name, 'comments' ) ) { + $columns['comments'] = 'Comments'; + } - /* - function populate_admin_columns - populates custom columns on the admin edit screen + // Add date of post to end of columns. + $columns['date'] = __( 'Date', $this->textdomain ); - function is used with add_action - */ + } else { - function populate_admin_columns($column, $post_id) { + // Use user submitted columns, these are defined using the object columns() method. + $columns = $this->columns; + } - // get wordpress $post object - global $post; + return $columns; + } - // determine the column - switch($column) { + /** + * Populate admin columns + * + * Populate custom columns on the admin edit screen. + * + * @param string $column The name of the column. + * @param integer $post_id The post ID. + */ + function populate_admin_columns( $column, $post_id ) { - // if column is a taxonomy associated with the post type - case (taxonomy_exists($column)) : + // Get wordpress $post object. + global $post; - // Get the taxonomy for the post - $terms = get_the_terms($post_id, $column); + // determine the column + switch( $column ) { - // if we have terms - if (!empty($terms)) { + // If column is a taxonomy associated with the post type. + case ( taxonomy_exists( $column ) ) : - $output = array(); + // Get the taxonomy for the post + $terms = get_the_terms( $post_id, $column ); - // Loop through each term, linking to the 'edit posts' page for the specific term. - foreach($terms as $term) { + // If we have terms. + if ( ! empty( $terms ) ) { - // output is an array of terms associated with the post - $output[] = sprintf( + $output = array(); - // define link - '%s', + // Loop through each term, linking to the 'edit posts' page for the specific term. + foreach( $terms as $term ) { - // create filter url - esc_url(add_query_arg(array('post_type' => $post->post_type, $column => $term->slug), 'edit.php')), + // Output is an array of terms associated with the post. + $output[] = sprintf( - // create friendly term name - esc_html(sanitize_term_field('name', $term->name, $term->term_id, $column, 'display')) + // Define link. + '%s', - ); + // Create filter url. + esc_url( add_query_arg( array( 'post_type' => $post->post_type, $column => $term->slug ), 'edit.php' ) ), - } + // Create friendly term name. + esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, $column, 'display' ) ) + ); - // Join the terms, separating them with a comma - echo join(', ', $output); + } - // if no terms found - } else { + // Join the terms, separating them with a comma. + echo join( ', ', $output ); - // get the taxonomy object for labels - $taxonomy_object = get_taxonomy($column); + // If no terms found. + } else { - // echo no terms - _e('No ' . $taxonomy_object->labels->name); + // Get the taxonomy object for labels + $taxonomy_object = get_taxonomy( $column ); - } + // Echo no terms. + printf( __( 'No %s', $this->textdomain ), $taxonomy_object->labels->name ); + } + break; - break; + // If column is for the post ID. + case 'post_id' : - // if column is for the post ID - case 'post_id' : + echo $post->ID; - echo $post->ID; + break; - break; + // if the column is prepended with 'meta_', this will automagically retrieve the meta values and display them. + case ( preg_match( '/^meta_/', $column ) ? true : false ) : - // if the column is prepended with 'meta_' - // this will automagically retrieve the meta values and display them - case (preg_match('/^meta_/', $column) ? true : false) : + // meta_book_author (meta key = book_author) + $x = substr( $column, 5 ); - // meta_book_author (meta key = book_author) - $x = substr($column, 5); + $meta = get_post_meta( $post->ID, $x ); - $meta = get_post_meta($post->ID, $x); + echo join( ", ", $meta ); - echo join(", ", $meta); + break; - break; + // If the column is post thumbnail. + case 'icon' : - // if the column is post thumbnail - case 'icon' : + // Create the edit link. + $link = esc_url( add_query_arg( array( 'post' => $post->ID, 'action' => 'edit' ), 'post.php' ) ); - // create the edit link - $link = esc_url(add_query_arg(array('post' => $post->ID, 'action' => 'edit'), 'post.php')); + // If it post has a featured image. + if ( has_post_thumbnail() ) { - // if it post has a featured image - if(has_post_thumbnail()) { - - // display post featured image with edit link - echo ''; - the_post_thumbnail(array(60, 60)); + // Display post featured image with edit link. + echo ''; + the_post_thumbnail( array(60, 60) ); echo ''; - } else { - - // display default media image with link - echo ''. $post->post_title .''; - - } - - break; - - // default case checks if the column has a user function - // this is most commonly used for custom fields - default : - - // if there are user custom columns to populate - if(isset($this->custom_populate_columns) && is_array($this->custom_populate_columns)) { - - // if this column has a user submitted function to run - if(isset($this->custom_populate_columns[$column]) && is_callable($this->custom_populate_columns[$column])) { - - // run the function - $this->custom_populate_columns[$column]($column, $post); - - } - - } - - break; - - } // end switch($column) - - } - - - - /* - function filters - user function to define which taxonomy filters to display on the admin page - - @param array $filters an array of taxonomy filters to display - - */ - - function filters($filters = array()) { - - $this->filters = $filters; - - } - - - - - /* - function add_taxtonomy_filters - creates select fields for filtering posts by taxonomies on admin edit screen - - */ - - function add_taxonomy_filters() { - - global $typenow; - global $wp_query; + } else { - // must set this to the post type you want the filter(s) displayed on - if($typenow == $this->post_type_name){ + // Display default media image with link. + echo '' . $post->post_title . ''; - // if custom filters are defined use those - if(is_array($this->filters)) { + } - $filters = $this->filters; + break; - // else default to use all taxonomies associated with the post - } else { + // Default case checks if the column has a user function, this is most commonly used for custom fields. + default : - $filters = $this->taxonomies; + // If there are user custom columns to populate. + if ( isset( $this->custom_populate_columns ) && is_array( $this->custom_populate_columns ) ) { - } - - if(!empty($filters)) { - - // foreach of the taxonomies we want to create filters for - foreach($filters as $tax_slug) { - - // object for taxonomy, doesn't contain the terms - $tax = get_taxonomy($tax_slug); - - // get taxonomy terms and order by name - $args = array( - 'orderby' => 'name', - 'hide_empty' => false - ); - - // get taxonomy terms - $terms = get_terms($tax_slug, $args); - - // if we have terms - if($terms) { - - // set up select box - printf('   '); - - } - - } - } - } + /** + * Filters + * + * User function to define which taxonomy filters to display on the admin page. + * + * @param array $filters An array of taxonomy filters to display. + */ + function filters( $filters = array() ) { + $this->filters = $filters; } + /** + * Add taxtonomy filters + * + * Creates select fields for filtering posts by taxonomies on admin edit screen. + */ + function add_taxonomy_filters() { + global $typenow; + global $wp_query; - /* - function columns - user function to choose columns to be displayed on the admin edit screen + // Must set this to the post type you want the filter(s) displayed on. + if ( $typenow == $this->post_type_name ) { - @param array $columns an array of columns to be displayed + // if custom filters are defined use those + if ( is_array( $this->filters ) ) { - */ + $filters = $this->filters; - function columns($columns) { + // else default to use all taxonomies associated with the post + } else { - // if columns is set - if(isset($columns)) { + $filters = $this->taxonomies; + } - // assign user submitted columns to object - $this->columns = $columns; + if ( ! empty( $filters ) ) { - } + // Foreach of the taxonomies we want to create filters for... + foreach ( $filters as $tax_slug ) { - } + // ...object for taxonomy, doesn't contain the terms. + $tax = get_taxonomy( $tax_slug ); + // Get taxonomy terms and order by name. + $args = array( + 'orderby' => 'name', + 'hide_empty' => false + ); + // Get taxonomy terms. + $terms = get_terms( $tax_slug, $args ); - /* - function populate_column - user function to define what and how to populate a specific admin column + // If we have terms. + if ( $terms ) { - @param string $column_name the name of the column to populate - @param func $function an anonymous function to run when populating the column - */ + // Set up select box. + printf( '   ' ); + } + } + } + } + } - @param array $columns an array of the columns that are sortable - */ + /** + * Columns + * + * Choose columns to be displayed on the admin edit screen. + * + * @param array $columns An array of columns to be displayed. + */ + function columns( $columns ) { - function sortable($columns = array()) { + // If columns is set. + if( isset( $columns ) ) { - // assign user defined sortable columns to object variable - $this->sortable = $columns; - - // run filter to make columns sortable - $this->add_filter('manage_edit-' . $this->post_type_name . '_sortable_columns', array(&$this, 'make_columns_sortable')); - - // run action that sorts columns on request - $this->add_action('load-edit.php', array(&$this, 'load_edit')); - - } - - - - /* - function make_columns_sortable - internal function that adds any user defined sortable columns to wordpress default columns - - */ - - function make_columns_sortable($columns) { - - // for each sortable column - foreach($this->sortable as $column => $values) { - - // make an array to merege into wordpress sortable columns - $sortable_columns[$column] = $values[0]; + // Assign user submitted columns to object. + $this->columns = $columns; } - - - // merge sortable columns array into wordpress sortable columns - $columns = array_merge($sortable_columns, $columns); - - return $columns; - - } - - - - /* - function load_edit - only sort columns on the edit.php page when requested - - */ - + } + + /** + * Populate columns + * + * Define what and how to populate a speicific admin column. + * + * @param string $column_name The name of the column to populate. + * @param function $function An anonyous function to run when populating the column. + */ + function populate_column( $column_name, $function ) { + + $this->custom_populate_columns[ $column_name ] = $function; + + } + + /** + * Sortable + * + * Define what columns are sortable in the admin edit screen. + * + * @param array $columns An array of columns that are sortable. + */ + function sortable( $columns = array() ) { + + // Assign user defined sortable columns to object variable. + $this->sortable = $columns; + + // Run filter to make columns sortable. + $this->add_filter( 'manage_edit-' . $this->post_type_name . '_sortable_columns', array( &$this, 'make_columns_sortable' ) ); + + // Run action that sorts columns on request. + $this->add_action( 'load-edit.php', array( &$this, 'load_edit' ) ); + } + + /** + * Make columns sortable + * + * Internal function that adds user defined sortable columns to WordPress default columns. + * + * @param array $columns Columns to be sortable. + * + */ + function make_columns_sortable( $columns ) { + + // For each sortable column. + foreach ( $this->sortable as $column => $values ) { + + // Make an array to merge into wordpress sortable columns. + $sortable_columns[ $column ] = $values[0]; + } + + // Merge sortable columns array into wordpress sortable columns. + $columns = array_merge( $sortable_columns, $columns ); + + return $columns; + } + + /** + * Load edit + * + * Sort columns only on the edit.php page when requested. + * + * @see http://codex.wordpress.org/Plugin_API/Filter_Reference/request + */ function load_edit() { - // run filter to sort columns when requested - $this->add_filter( 'request', array(&$this, 'sort_columns') ); + // Run filter to sort columns when requested + $this->add_filter( 'request', array( &$this, 'sort_columns' ) ); } - - - /* - function sort columns - internal function that sorts columns on request - - run by load_edit() filter - - @param array $vars the query vars submitted by user - - */ - - function sort_columns($vars) { - - // cycle through all sortable columns submitted by the user - foreach($this->sortable as $column => $values) { - - // retrieve the meta key from the user submitted array of sortable columns + /** + * Sort columns + * + * Internal function that sorts columns on request. + * + * @see load_edit() + * + * @param array $vars The query vars submitted by user. + * @return array A sorted array. + */ + function sort_columns( $vars ) { + + // Cycle through all sortable columns submitted by the user + foreach ( $this->sortable as $column => $values ) { + + // Retrieve the meta key from the user submitted array of sortable columns $meta_key = $values[0]; - // if the meta_key is a taxonomy - if(taxonomy_exists($meta_key)) { + // If the meta_key is a taxonomy + if( taxonomy_exists( $meta_key ) ) { - // sort by taxonomy - $key = "taxonomy"; + // Sort by taxonomy. + $key = "taxonomy"; - } else { - - // else by meta key - $key = "meta_key"; - - } - - // if the optional parameter is set and is set to true - if(isset($values[1]) && true === $values[1]) { - - // vaules needed to be ordered by integer value - $orderby = 'meta_value_num'; - - } else { - - // values are to be order by string value - $orderby = 'meta_value'; + } else { + // else by meta key. + $key = "meta_key"; } - // Check if we're viewing this post type - if (isset($vars['post_type']) && $this->post_type_name == $vars['post_type']) { - - // find the meta key we want to order posts by - if (isset($vars['orderby']) && $meta_key == $vars['orderby']) { - - // merge the query vars with our custom variables - $vars = array_merge($vars, - array( - 'meta_key' => $meta_key, - 'orderby' => $orderby - ) - ); - } - - } - - } - - return $vars; - } - - - - /* - function menu icon - used to change the menu icon in the admin dashboard - pass name of dashicon, list found here http://melchoyce.github.io/dashicons/ - - @param mixed $icon a string of the name of the icon to use - */ - - function menu_icon($icon = "dashicons-admin-page") { - - // WP 3.8 changed the icon system to use an icon font. - // http://melchoyce.github.io/dashicons/ - - if(is_string($icon) && stripos($icon, "dashicons") !== FALSE) { - - $this->options["menu_icon"] = $icon; - - } else { - // set a default - $this->options["menu_icon"] = "dashicons-admin-page"; - - } - - } - -} + // If the optional parameter is set and is set to true + if ( isset( $values[1] ) && true === $values[1] ) { + + // Vaules needed to be ordered by integer value + $orderby = 'meta_value_num'; + + } else { + + // Values are to be order by string value + $orderby = 'meta_value'; + } + + // Check if we're viewing this post type + if ( isset( $vars['post_type'] ) && $this->post_type_name == $vars['post_type'] ) { + + // find the meta key we want to order posts by + if ( isset( $vars['orderby'] ) && $meta_key == $vars['orderby'] ) { + + // Merge the query vars with our custom variables + $vars = array_merge( + $vars, + array( + 'meta_key' => $meta_key, + 'orderby' => $orderby + ) + ); + } + } + } + return $vars; + } + + /** + * Set menu icon + * + * Use this function to set the menu icon in the admin dashboard. Since WordPress v3.8 + * dashicons are used. For more information see @link http://melchoyce.github.io/dashicons/ + * + * @param string $icon dashicon name + */ + function menu_icon( $icon = "dashicons-admin-page" ) { + + if ( is_string( $icon ) && stripos( $icon, "dashicons" ) !== false ) { + + $this->options["menu_icon"] = $icon; + + } else { + + // Set a default menu icon + $this->options["menu_icon"] = "dashicons-admin-page"; + } + } + + /** + * Set textdomain + * + * @param string $textdomain Textdomain used for translation. + */ + function set_textdomain( $textdomain ) { + $this->textdomain = $textdomain; + } +} \ No newline at end of file