template/wordpress/code/cpt.php in edge_framework-2.1.0 vs template/wordpress/code/cpt.php in edge_framework-2.1.1

- old
+ new

@@ -1,84 +1,214 @@ <?php -/* - Create Custom Post Type -*/ -function add_post_type($name, $icon = "admin-post", $tax_name = null) { - $plural = Inflector::pluralize($name); - $singular = $name; +function add_post_type($name, $args) { + $cpt = new CPT($name, $args); + $cpt->init(); +} - $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 . ":", - ); +// CUSTOM Post Type +class CPT { + private $name; + private $args; - $post_args = array( - "public" => true, - "menu_icon" => "dashicons-".$icon, - "labels" => $labels, - "capability_type" => "post", - "supports" => array( - "title", - "editor", - "custom-fields", - "revisions", - "thumbnail", - ), - ); - - // If taxonomy is given - if($tax_name) { - add_taxonomy($tax_name, $name); + public function __construct($name, $args) { + $this->name = $name; + $this->args = $args; } - register_post_type(strtolower($name), $post_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); -/* - Create Custom Taxonomy -*/ + // If taxonomy is given + if($this->args["taxonomy"]) { + $this->add_taxonomy($this->args["taxonomy"], $this->name); + } -function add_taxonomy($name, $post_type) { - $plural = Inflector::pluralize($name); - $singular = $name; + register_post_type(strtolower($this->name), $post_args); - $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" - ); + // if column ordering is given + if($this->args["columns"]) { + $this->add_column($this->name, $this->args["columns"]); + } + } - $tax_args = array( - "labels" => $labels, - "show_ui" => true, - "query_var" => true, - "show_admin_column" => false, - "hierarchical" => true, - ); - register_taxonomy(strtolower($name), strtolower($post_type), $tax_args); + // Build the necessary arguments for CPT + private function buildArgs($name, $args) { + $plural = Inflector::pluralize($name); + $singular = $name; - new CPT_Filter(array(strtolower($post_type) => array(strtolower($singular) ) ) ); + $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("<a href='%s'>%s</a>", + 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 <admin@bainternet.info> @@ -135,6 +265,6 @@ // output each select option line, check against the last $_GET to show the current option selected echo "<option value=". $term->slug, $selected == $term->slug ? " selected='selected'" : "", ">" . $tab . $term->name ." (" . $term->count .")</option>"; $this->generate_taxonomy_options($tax_slug, $term->term_id, $level + 1, $selected); } } -}//end class +} \ No newline at end of file