Skip to content

Instantly share code, notes, and snippets.

@alettieri
Created February 12, 2013 02:15
Show Gist options
  • Save alettieri/4759587 to your computer and use it in GitHub Desktop.
Save alettieri/4759587 to your computer and use it in GitHub Desktop.
Custom Post Type helper Object.
<?php
session_start();
/**
* JW Post Types
* @author Jeffrey Way
* @link http://jeffrey-way.com
*/
class JW_Post_Type
{
/**
* The name of the post type.
* @var string
*/
public $post_type_name;
/**
* A list of user-specific options for the post type.
* @var array
*/
public $post_type_args;
/**
* Sets default values, registers the passed post type, and
* listens for when the post is saved.
*
* @param string $name The name of the desired post type.
* @param array @post_type_args Override the options.
*/
function __construct($name, $post_type_args = array())
{
if (!isset($_SESSION["taxonomy_data"])) {
$_SESSION['taxonomy_data'] = array();
}
$this->post_type_name = strtolower($name);
$this->post_type_args = (array)$post_type_args;
// First step, register that new post type
$this->init(array(&$this, "register_post_type"));
}
/**
* Helper method, that attaches a passed function to the 'init' WP action
* @param function $cb Passed callback function.
*/
function init($cb)
{
add_action("init", $cb);
}
/**
* Registers a new post type in the WP db.
*/
function register_post_type()
{
// Get the post type name
$type = $this->post_type_name;
// Upercase the name
$n = ucwords($type);
// Pluralize
$plural = $n . 's';
$args = array(
"labels" => array(
'name' => _x( $n, $type ),
'singular_name' => _x( $n, $type ),
'menu_name' => _x( $plural, $type ),
'all_items' => _x( "All $plural", $type ),
'add_new' => _x( "Add New", $type ),
'add_new_item' => _x( "Add new $n", $type ),
'edit_item' => _x( "Edit $n", $type ),
'new_item' => _x( "New $n", $type ),
'view_item' => _x( "View $n", $type ),
'items_archive' => _x( "$n Archive", $type ),
'search_items' => _x( "Search $n", $type ),
'not_found' => _x( "$plural Not Found", $type ),
'not_found_in_trash' => _x( "No $plural found in Trash", $type ),
'parent_item_colon' => _x( "Parent $n", $type )
),
"public" => true,
"publicly_queryable" => true,
"query_var" => true,
"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_post_type($this->post_type_name, $args);
} // register_post_type
/**
* Registers a new taxonomy, associated with the instantiated post type.
*
* @param string $taxonomy_name The name of the desired taxonomy
* @param string $plural The plural form of the taxonomy name. (Optional)
* @param array $options A list of overrides
*/
function add_taxonomy($taxonomy_name, $plural = '', $options = array())
{
// Create local reference so we can pass it to the init cb.
$post_type_name = $this->post_type_name;
// If no plural form of the taxonomy was provided, do a crappy fix. :)
if (empty($plural)) {
$plural = $taxonomy_name . 's';
}
// Taxonomies need to be lowercase, but displaying them will look better this way...
$taxonomy_name = ucwords($taxonomy_name);
// At WordPress' init, register the taxonomy
$this->init(
function() use($taxonomy_name, $plural, $post_type_name, $options)
{
// Override defaults with user provided options
$options = array_merge(
array(
"hierarchical" => false,
"label" => $taxonomy_name,
"singular_label" => $plural,
"show_ui" => true,
"query_var" => true,
"rewrite" => array("slug" => strtolower($taxonomy_name))
),
$options
);
// name of taxonomy, associated post type, options
register_taxonomy(strtolower($taxonomy_name), $post_type_name, $options);
});
} // add_taxonomy
} // end class JW_Post_Type
/*********/
/* USAGE */
/*********/
// $product = new PostType("movie");
// $product->add_taxonomy('Actor');
// $product->add_taxonomy('Director');
// ));
<?php
if( !class_exists( "WB_PostType" ) ) {
require_once( "post-type.php" );
}
/**
* Publication Post Type
* has_many => services
*
* @inherits WB_PostType
*
* Fields
* - Title
* - excerpt
*/
class Publication extends WB_PostType{
private $file;
private $year;
private $authors;
private $journal;
public $media;
function __construct() {
parent::__construct( "publication", array( "supports" => array( "title", "excerpt" ) ) );
$this->_register_taxonomies();
$this->_register_fields();
add_filter('query_vars', array( $this, 'publication_query_vars' ) );
}
/**
* Filter for authors save event.
*
* Trims the author names. For some reason empty strings are appended.
*/
public function trim_author( $meta, $post_id ) {
$authors = $meta[ "authors" ];
$new_authors = array();
foreach( $authors as $author ) {
$the_name = $author[ "name" ];
$author[ "name" ] = trim( $the_name );
$new_authors[] = $author;
}
$meta[ "authors" ] = $new_authors;
return $meta;
}
public function trim_string( $meta, $post_id ) {
foreach( $meta as $key => $val ) {
$meta[ $key ] = trim($val);
}
return $meta;
}
/**
* Add custom query variables to the wp_query list.
*/
public function publication_query_vars( $qvars )
{
$qvars[] = 'condition';
$qvars[] = 'specialty';
$qvars[] = 'method';
$qvars[] = 'list-type';
return $qvars;
}
/**
* Register publication taxonomies
*/
private function _register_taxonomies() {
/**
* Taxonomies
* - Disease/Condition
* - Speciality
* - Study Type/Method
*/
$this->add_taxonomy( "Condition", "Condition" );
$this->add_taxonomy( "Specialty", "Specialties" );
$this->add_taxonomy( "Method", "Methods" );
}
private function _register_fields() {
// Our media upload object.
// Called in publication-file template using global
$this->media = $this->add_media();
/**--- Publication File */
$this->file = $this->add_field( array
(
"id" => "_file",
"title" => "Publication File",
"template" => "publication-file.php",
)
);
/**--- Publication year */
$this->year = $this->add_field( array
(
"id" => "_year",
"title" => "Year Published",
"template" => "publication-year.php",
"context" => "side"
)
);
/**--- Publication Authors */
$this->authors = $this->add_field( array
(
"id" => "_authors",
"title" => "Authors",
"template" => "publication-authors.php",
"save_filter" => array( $this, "trim_author" )
)
);
/**--- Publication journal */
$this->journal = $this->add_field( array
(
"id" => "_journal",
"title" => "Journal",
"template" => "publication-journal.php",
"save_filter" => array( $this, "trim_string" )
)
);
}
}
<?php
/**
* Generic PostType
* @extends JW_POST_TYPE
*/
class WB_PostType extends JW_Post_Type{
function __construct( $name, $supports = array() ) {
parent::__construct( $name, $supports );
}
/**
* Prepares AlchemyMediaAccess library
* @return AlchemyMediaAccess
*/
public function add_media() {
return new WPAlchemy_MediaAccess();
}
/**
* Adds AlchemyMeta box
* @param array $args Alchemy Arguments
* @return AlchemyMetaBox
*/
public function add_field( $args = array() )
{
$args = (array)$args;
$default_args = array(
"id" => "",
"title" => "",
"types" => array( $this->post_type_name ),
"template" => "",
"priority" => "low",
"context" => "normal",
"mode" => WPALCHEMY_MODE_EXTRACT
);
$args = array_merge( $default_args, $args );
$args[ "template" ] = POST_ADMIN_TEMPLATES . $args[ "template" ];
return new WPAlchemy_MetaBox( $args );
}
}
@menslow
Copy link

menslow commented Feb 28, 2013

I think Line 4 of PublicationPostType.php should be: require_once( "WB_PostType.php" );

@menslow
Copy link

menslow commented Feb 28, 2013

Do you need a require statement at the top of WB_PostType.php or are you including the JW_PostType.php file somewhere else?:

if( !class_exists( "WB_PostType" ) ) {
require_once( "JW_PostType.php" );
}

@menslow
Copy link

menslow commented Mar 1, 2013

Where do you recommend defining the POST_ADMIN_TEMPLATES constant that is used in WB_PostType?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment