Created
August 20, 2013 03:22
-
-
Save joshrcook/6276765 to your computer and use it in GitHub Desktop.
This gist is a sample of code that I created for a website that I have been working on recently. I brought together two other classes, extended their functionality, made some functions more flexible, and created a cohesive class that simplifies code and saves lots of time.
This file contains hidden or 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 | |
// require the JW_Post_Type class | |
require_once('inc/Easy-WordPress-Custom-Post-Types/jw_custom_posts.php'); | |
// require CMB_Meta_Boxes | |
require_once('inc/Custom-Metaboxes-and-Fields-for-WordPress/init.php'); | |
/** | |
* This class brings together Easy Wordpress Custom Post Types, Custom Metaboxes | |
* and Fields for Wordpress, and some other functions to make custom post type | |
* creation insanely simple. This class saves hundreds of lines of code and | |
* simplifies post type creation, yet has the flexibility to allow customization | |
* for more complex integration. | |
* | |
* @version 1.0 | |
* @author Joshua Cook | |
*/ | |
class JRC_Post_Type extends JW_Post_Type | |
{ | |
protected $_columns; | |
protected $_column_functions; // the user functions to call to output column contents | |
protected $_column_filters_added; // whether the column filters have been added or not | |
/** | |
* Sets default values, registers the passed post type, and | |
* listens for when the post is saved. | |
* | |
* @param string $label The label for the post type. | |
* @param string $plural_label The plural label for the post type | |
* @param string $slug The "slug" for the post type. | |
* @param array @post_type_args Override the options. | |
*/ | |
function __construct($label, $plural_label, $slug, $post_type_args = array()) | |
{ | |
if (!isset($_SESSION["taxonomy_data"])) { | |
$_SESSION['taxonomy_data'] = array(); | |
} | |
$this->post_type_label = $label; | |
$this->post_type_label_plural = $plural_label; | |
$this->post_type_name = strtolower($slug); | |
$this->post_type_args = (array)$post_type_args; | |
// First step, register that new post type | |
$this->init(array(&$this, "register_post_type")); | |
$this->save_post(); | |
} | |
/** | |
* Registers a new post type in the WP db. | |
*/ | |
function register_post_type() | |
{ | |
$n = $this->post_type_label; | |
$n_plural = $this->post_type_label_plural; | |
$args = array( | |
"label" => $n_plural, | |
'singular_name' => $n, | |
'labels' => array( | |
'name' => $n, | |
'singular_name' => $n_plural, | |
'add_new' => "Add New $n", | |
'add_new_item' => "Add New $n", | |
'edit_item' => "Edit $n", | |
'new_item' => "New $n", | |
'view item' => "View $n", | |
'search_items' => "Search $n_plural", | |
'parent_item_colon' => "Parent" | |
), | |
"public" => true, | |
"publicly_queryable" => true, | |
"query_var" => true, | |
#"menu_icon" => get_stylesheet_directory_uri() . "/article16.png", | |
"rewrite" => true, | |
"capability_type" => "post", | |
"hierarchical" => false, | |
"menu_position" => null, | |
"supports" => array("title", "editor", "thumbnail"), | |
'has_archive' => true | |
); | |
// Take user provided options, and override the defaults. | |
$args = array_merge($args, $this->post_type_args); | |
// register the post type | |
register_post_type($this->post_type_name, $args); | |
} | |
/** | |
* Adds a new meta box to the post type. | |
* | |
* This function uses 'Custom Metaboxes and Fields for WordPress'. To | |
* find out how to input fields, check the documentation for this class | |
* at https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress | |
* | |
* @param string $title The title of the metabox | |
* @param array $fields The fields array, following the syntax of CMB | |
* @param array $options Override any default options | |
*/ | |
function add_meta_box($title, array $fields, array $options = array()) | |
{ | |
$defaults_array = array( | |
'id' => str_replace(' ', '_', strtolower($title)), | |
'title' => $title, | |
'pages' => array($this->post_type_name), | |
'context' => 'normal', | |
'priority' => 'high', | |
'show_names' => true, | |
#'show_on' => array( 'key' => 'id', 'value' => array( 2, ), ), // Specific post IDs to display this metabox, | |
'fields' => $fields | |
); | |
$this->meta_boxes[] = array_merge($defaults_array, $options); | |
add_filter( 'cmb_meta_boxes', array($this, 'cmb_meta_boxes')); | |
} | |
/** | |
* Adds the metaboxes to the filter, so they can be displayed | |
* | |
* @param array $metaboxes The metaboxes already added | |
* @return array $metaboxes The new metabox array | |
*/ | |
function cmb_meta_boxes(array $metaboxes) | |
{ | |
foreach($this->meta_boxes as $meta_box) { | |
$metaboxes[] = $meta_box; | |
} | |
return $metaboxes; | |
} | |
/** | |
* Adds a column to the post type | |
* | |
* This function takes a name and a content function as arguements and adds a column | |
* to the post type main admin screen. The content function can accept 0 or 1 arguements, | |
* and the arguement will be the id of the post for that particular column. | |
* | |
* @param string $col_title Title of the column | |
* @param string $content_function Name of function that outputs column content | |
*/ | |
function add_column($col_title, $content_function) | |
{ | |
$column_slug = str_replace(' ', '_', strtolower($this->post_type_name . '_' . $col_title)); | |
$this->_columns[$column_slug] = $col_title; | |
// add the function to a variable so it can be called | |
$this->_column_functions[$column_slug] = $content_function; | |
if(!$this->_column_filters_added) { | |
add_filter("manage_edit-{$this->post_type_name}_columns", array($this, 'post_type_edit_columns')); | |
add_action("manage_{$this->post_type_name}_posts_custom_column", array($this, 'post_type_column_info'), 10, 2); | |
$this->_column_filters_added = true; | |
} | |
} | |
/** | |
* Adds the columns between the title and date columns | |
* | |
* @param array $columns Added by WordPress. The array of the columns | |
* @return array Columns array | |
*/ | |
function post_type_edit_columns($columns) | |
{ | |
// add the column between the title and the date | |
$new_columns = array_merge(array_slice($columns, 0, 2, true), $this->_columns, array_slice($columns, 2, 3, true)); | |
// return the columns | |
return $new_columns; | |
} | |
/** | |
* Adds the info for each column, which is defined by the user passed function. This | |
* will run as long as the user function uses one parameter or less. The parameter passed | |
* is the post id. | |
* | |
* @param string $column Slug of the column. | |
* @param int $post_id The post id for that row. | |
*/ | |
function post_type_column_info($column, $post_id) | |
{ | |
// if the column is in the user defined columns list | |
if(array_key_exists($column, $this->_columns)) { | |
// if the function exists | |
if(is_callable($this->_column_functions["$column"])) { | |
// call the user function to output the column contents | |
call_user_func_array($this->_column_functions["$column"], array(&$post_id)); | |
} | |
} | |
} | |
} | |
// ****** USING THE CLASS ****** | |
// create the post type | |
$jrc_meet = new JRC_Post_Type('Meeting Minutes', 'Meeting Minutes', 'jrc_meet', array( | |
'singular_name' => 'Meeting Minutes' | |
)); | |
// set the prefix for the post meta fields | |
$prefix = '_jrc_meet_'; | |
// create the array for the meta boxes | |
$meet_details_fields = array( | |
array( | |
'name' => 'Meeting Date', | |
'id' => $prefix . 'date', | |
'type' => 'text_date_timestamp' | |
), | |
array( | |
'name' => 'Meeting Time', | |
'id' => $prefix . 'time', | |
'type' => 'text_time' | |
) | |
); | |
// add the meta box | |
$jrc_meet->add_meta_box('Meeting Details', $meet_details_fields); | |
// create a function for the column content callback | |
function meeting_date(&$post_id) | |
{ | |
$date = new DateTime(); | |
$date->setTimestamp(get_post_meta($post_id, '_jrc_meet_date', true)); | |
if($time = get_post_meta($post_id, '_jrc_meet_time', true)) { | |
$date->modify($time); | |
echo $date->format('m/d/Y @ h:ia'); | |
} else { | |
echo $date->format('m/d/Y'); | |
} | |
} | |
// add the column, using the function name as the second argument. | |
$jrc_meet->add_column('Meeting Date', 'meeting_date'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment