init();
}
// CUSTOM Post Type
class CPT {
private $name;
private $args;
public function __construct($name, $args) {
$this->name = $name;
$this->args = $args;
}
// Add the post type and all of it's perk based on the args
public function init() {
$post_args = $this->buildArgs($this->name, $this->args);
// If taxonomy is given
if($this->args["taxonomy"]) {
$this->add_taxonomy($this->args["taxonomy"], $this->name);
}
register_post_type(strtolower($this->name), $post_args);
// if column ordering is given
if($this->args["columns"]) {
$this->add_column($this->name, $this->args["columns"]);
}
}
// Build the necessary arguments for CPT
private function buildArgs($name, $args) {
$plural = Inflector::pluralize($name);
$singular = $name;
$labels = array(
"name" => $plural,
"singular_name" => $singular,
"all_items" => "All " . $plural,
"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 " . strtolower($plural) . " found",
"not_found_in_trash" => "No " . strtolower($plural) . " found in Trash",
"parent_item_colon" => "Parent " . $singular . ":",
);
// Build the post arguments
$post_args = array(
"public" => true,
"labels" => $labels,
"capability_type" => "post",
"supports" => array(
"title",
"editor",
"custom-fields",
"revisions",
"thumbnail",
),
"rewrite" => array(
"with_front" => false
),
);
if($args["icon"]) {
$post_args["menu_icon"] = "dashicons-".$args["icon"];
}
return $post_args;
}
// Create taxonomy and it's filter
private function add_taxonomy($tax_name, $post_type) {
$plural = Inflector::pluralize($tax_name);
$singular = $tax_name;
$labels = array(
"name" => $plural,
"singular_name" => $singular,
"all_items" => "All " . $plural,
"edit_item" => "Edit " . $singular,
"view_item" => "View " . $singular,
"update_item" => "Update " . $singular,
"add_new_item" => "Add New " . $singular,
"parent_item" => "Parent " . $singular,
"search_items" => "Search " . $plural,
"popular_items" => "Popular " . $plural,
"add_or_remove_items" => "Add or remove " . strtolower($plural),
"choose_from_most_used" => "Choose from the most used " . strtolower($plural),
"not_found" => "No " . strtolower($plural) . " found"
);
$tax_args = array(
"labels" => $labels,
"show_ui" => true,
"query_var" => true,
"show_admin_column" => false,
"hierarchical" => true,
);
register_taxonomy(strtolower($tax_name), strtolower($post_type), $tax_args);
new CPT_Filter(array(strtolower($post_type) => array(strtolower($singular) ) ) );
}
// Add visible column in admin panel
private function add_column($name, $raw_columns) {
// create the WP filter name
$name_slug = strtolower($name);
$name_create = "manage_".$name_slug."_posts_columns"; // create column
$name_fill = "manage_".$name_slug."_posts_custom_column"; // fill column
$name_sortable = "manage_edit-".$name_slug."_sortable_columns"; // enable sorting
// cleanup and build the dataset
$columns = $this->clean_columns($raw_columns);
$sortable_columns = $this->get_sortable_columns($raw_columns);
// create the filter function
$filter_create = function($defaults) use ($columns) {
$cols = array();
foreach($columns as $c) {
$cols[$c] = ucwords( str_replace("_", " ", $c) );
}
$cols = array("cb" => $defaults["cb"]) + $cols;
return $cols;
};
$filter_fill = function($column_name, $post_id) {
switch($column_name) {
case "cb":
case "title":
case "author":
case "date":
// do nothing, those are automatically filled
break;
case "thumbnail":
$thumb = get_the_post_thumbnail($post_id, array(75, 75) );
echo $thumb;
// if custom field
default:
global $post;
$meta = get_post_meta($post_id, $column_name, true);
$terms = get_the_terms($post_id, $column_name);
// if the column is a custom field
if($meta) {
echo $meta;
}
// if the column is a term
elseif (!empty($terms) ) {
$out = array();
// loop through each term, linking to the 'edit posts' page for the specific term
foreach ($terms as $term) {
$out[] = sprintf("%s",
esc_url( add_query_arg(
array("post_type" => $post->post_type, "type" => $term->slug), "edit.php")
),
esc_html( sanitize_term_field(
"name", $term->name, $term->term_id, "type", "display")
)
);
}
// join the terms, separating with comma
echo join(", ", $out);
}
break;
}
};
$filter_sortable = function($defaults) use ($sortable_columns) {
foreach($sortable_columns as $sc) {
$defaults[$sc] = $sc;
}
return $defaults;
};
add_filter($name_create, $filter_create);
add_action($name_fill, $filter_fill, 10, 2);
add_filter($name_sortable, $filter_sortable);
}
// Cleanup the column args from annotation
private function clean_columns($raw) {
$columns = array_map(function($c) {
return trim($c, "^");
}, $raw);
return $columns;
}
// Look for sortable column, annotated with ^
private function get_sortable_columns($raw) {
$columns = array_map(function($c) {
if(strpos($c, "^") ) {
return trim($c, "^");
}
}, $raw);
return array_filter($columns);
}
}
/*
Add Taxonomy filter to a CPT
@author Ohad Raz
*/
class CPT_Filter {
function __construct($cpt = array()) {
$this->cpt = $cpt;
add_action("restrict_manage_posts", array($this, "my_restrict_manage_posts") );
}
/*
Add select dropdown per taxonomy
*/
public function my_restrict_manage_posts() {
// only display these taxonomy filters on desired custom post_type listings
global $typenow;
$types = array_keys($this->cpt);
if (in_array($typenow, $types) ) {
// create an array of taxonomy slugs you want to filter by - if you want to retrieve all taxonomies, could use get_taxonomies() to build the list
$filters = $this->cpt[$typenow];
foreach ($filters as $tax_slug) {
// retrieve the taxonomy object
$tax_obj = get_taxonomy($tax_slug);
$tax_name = $tax_obj->labels->name;
// output html for taxonomy dropdown filter
echo "";
}
}
}
/*
Generate_taxonomy_options generate dropdown
*/
public function generate_taxonomy_options($tax_slug, $parent = "", $level = 0, $selected = null) {
$args = array("show_empty" => 1);
if(!is_null($parent) ) {
$args = array("parent" => $parent);
}
$terms = get_terms($tax_slug, $args);
$tab = "";
for($i = 0; $i < $level; $i++) {
$tab .= "--";
}
foreach ($terms as $term) {
// output each select option line, check against the last $_GET to show the current option selected
echo "";
$this->generate_taxonomy_options($tax_slug, $term->term_id, $level + 1, $selected);
}
}
}