Skip to content

Instantly share code, notes, and snippets.

@zenril
Last active August 29, 2015 14:23
Show Gist options
  • Save zenril/80a675f0fcb8ad3b8d8d to your computer and use it in GitHub Desktop.
Save zenril/80a675f0fcb8ad3b8d8d to your computer and use it in GitHub Desktop.
class PostType {
public $post_type;
private $name;
private $plural;
private $altArgs;
function PostType( $post_type, $single_name, $plural_name = '', $altArgs = array() ) {
if(isset($post_type) && isset($single_name)){
$this->post_type = $post_type;
$this->name = $single_name;
$this->plural = empty($plural_name)?$single_name.'s':$plural_name;
$this->altArgs = $altArgs;
add_filter( 'init' , array( &$this , 'register_post_type_helper' ) );
}
}
function register_post_type_helper() {
$labels = array(
'name' => $this->name,
'singular_name' => $this->name,
'menu_name' => $this->plural,
'name_admin_bar' => $this->name,
'add_new' => 'Add New',
'add_new_item' => 'Add New ' . $this->name,
'new_item' => 'New ' . $this->name,
'edit_item' => 'Edit ' . $this->name,
'view_item' => 'View ' . $this->name,
'all_items' => 'All ' . $this->plural,
'search_items' => 'Search ' . $this->plural ,
'parent_item_colon' => 'Parent ' . $this->plural,
'not_found' => 'No ' . $this->plural . ' found',
'not_found_in_trash' => 'No ' . $this->plural . ' found in Trash',
);
$args = array(
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => preg_replace("/[^A-Za-z]/", '-', $this->name) ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
);
$args = array_merge($args, $this->altArgs);
$args['labels'] = $labels;
register_post_type( $this->post_type, $args );
}
public function getQuery( $arguments = array() ){
$args = array_merge(array( 'post_type' => $this->post_type ), $arguments);
// The Query
$query = new WP_Query( $args );
return $query;
}
public function archiveLink($text = null){
if($text == null){
return get_post_type_archive_link( $this->post_type );
}
return "".$text."";
}
public function doQuery($fn, $numargs = array()){
if($fn === null){
return false;
}
$query = $this->getQuery($numargs);
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$fn($post);
}
wp_reset_postdata();
return 'true';
} else {
$fn(null);
wp_reset_postdata();
return 'false';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment