/** ************************************************************************ * REQUIRED. Set up a constructor that references the parent constructor. We * use the parent reference to set some default configs. ***************************************************************************/ function __construct() { global $status, $page; //Set parent defaults parent::__construct(array('singular' => 'Taxonomy', 'plural' => 'Taxonomies', 'ajax' => true)); $this->custom_taxonomies = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array()); $this->builtin_taxonomies = wpcf_get_builtin_in_taxonomies(); }
/** * Check is a build-in taxonomy * * Check is that build-in taxonomy? * * @since 1.9.0 * * @parem string taxonomy slug * @return boolean is this build-in taxonomy */ function wpcf_is_builtin_taxonomy($taxonomy) { switch ($taxonomy) { case 'post_tag': case 'category': return true; } return in_array($taxonomy, wpcf_get_builtin_in_taxonomies()); }
/** ************************************************************************ * REQUIRED! This is where you prepare your data for display. This method will * usually be used to query the database, sort and filter the data, and generally * get it ready to be displayed. At a minimum, we should set $this->items and * $this->set_pagination_args(), although the following properties and methods * are frequently interacted with here... * * @uses $this->_column_headers * @uses $this->items * @uses $this->get_columns() * @uses $this->get_sortable_columns() * @uses $this->get_pagenum() * @uses $this->set_pagination_args() **************************************************************************/ function prepare_items() { /** * First, lets decide how many records per page to show */ $per_page = $this->get_items_per_page('wpcf_cpt_per_page', 10); /** * REQUIRED. Now we need to define our column headers. This includes a complete * array of columns to be displayed (slugs & titles), a list of columns * to keep hidden, and a list of columns that are sortable. Each of these * can be defined in another method (as we've done here) before being * used to build the value for our _column_headers property. */ $columns = $this->get_columns(); $hidden = array(); $sortable = $this->get_sortable_columns(); /** * REQUIRED. Finally, we build an array to be used by the class for column * headers. The $this->_column_headers property takes an array which contains * 3 other arrays. One for all columns, one for hidden columns, and one * for sortable columns. */ $this->_column_headers = array($columns, $hidden, $sortable); /** * Optional. You can handle your bulk actions however you see fit. In this * case, we'll handle them within our package just to keep things clean. */ $this->process_bulk_action(); /** * map taxonomies to Post Types */ $taxonomies = wpcf_get_builtin_in_taxonomies('objects'); $map_taxonomies_by_post_type = array(); foreach ($taxonomies as $slug => $data) { if (isset($data->object_type)) { foreach ($data->object_type as $post_type) { if (!isset($map_taxonomies_by_post_type[$post_type])) { $map_taxonomies_by_post_type[$post_type] = array(); } $map_taxonomies_by_post_type[$post_type][$slug] = $data->labels->name; } } } $custom_taxonomies = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array()); foreach ($custom_taxonomies as $slug => $data) { if (true && isset($data['labels']) && isset($data['labels']['name']) && isset($data['supports']) && !empty($data['supports'])) { foreach (array_keys($data['supports']) as $post_type) { $map_taxonomies_by_post_type[$post_type][$slug] = $data['labels']['name']; } } } /** * Instead of querying a database, we're going to fetch the example data * property we created for use in this plugin. This makes this example * package slightly different than one you might build on your own. In * this example, we'll be using array manipulation to sort and paginate * our data. In a real-world implementation, you will probably want to * use sort and pagination data to build a custom query instead, as you'll * be able to use your precisely-queried data immediately. */ $s = isset($_POST['s']) ? mb_strtolower(trim($_POST['s'])) : false; $data = array(); if (!empty($this->custom_types)) { foreach (array_values($this->custom_types) as $type) { if (empty($type) || empty($type['slug'])) { continue; } $one = array('description' => isset($type['description']) ? $type['description'] : '', 'taxonomies' => isset($map_taxonomies_by_post_type[$type['slug']]) ? $map_taxonomies_by_post_type[$type['slug']] : array(), 'slug' => $type['slug'], 'status' => isset($type['disabled']) ? 'inactive' : 'active', 'title' => stripslashes($type['labels']['name']), 'type' => 'cpt', '_builtin' => false, WPCF_AUTHOR => isset($type[WPCF_AUTHOR]) ? intval($type[WPCF_AUTHOR]) : 0); $add_one = true; if ($s) { $add_one = false; foreach (array('description', 'slug', 'title') as $key) { if ($add_one || empty($one[$key])) { continue; } if (is_numeric(strpos(mb_strtolower($one[$key]), $s))) { $add_one = true; } } } if ($add_one) { $data[$one['slug']] = $one; } } } /** * built-in post types */ foreach ($this->buildin_post_types as $type) { $pt = get_post_type_object($type); $one = array('description' => __('This is built-in WordPress Post Type.', 'wpcf'), 'taxonomies' => isset($map_taxonomies_by_post_type[$type]) ? $map_taxonomies_by_post_type[$type] : array(), 'slug' => $type, 'status' => 'active', 'title' => $pt->labels->name, '_builtin' => true, 'type' => '_builtin'); $add_one = true; if ($s) { $add_one = false; foreach (array('description', 'slug', 'title') as $key) { if ($add_one || empty($one[$key])) { continue; } if (is_numeric(strpos(mb_strtolower($one[$key]), $s))) { $add_one = true; } } } if ($add_one) { $data[$one['slug']] = $one; } } /** * This checks for sorting input and sorts the data in our array accordingly. */ usort($data, 'wpcf_usort_reorder'); /** * REQUIRED for pagination. Let's figure out what page the user is currently * looking at. We'll need this later, so you should always include it in * your own package classes. */ $current_page = $this->get_pagenum(); /** * REQUIRED for pagination. Let's check how many items are in our data array. * In real-world use, this would be the total number of items in your database, * without filtering. We'll need this later, so you should always include it * in your own package classes. */ $total_items = count($data); /** * The WP_List_Table class does not handle pagination for us, so we need * to ensure that the data is trimmed to only the current page. We can use * array_slice() to */ $data = array_slice($data, ($current_page - 1) * $per_page, $per_page); /** * REQUIRED. Now we can add our *sorted* data to the items property, where * it can be used by the rest of the class. */ $this->items = $data; /** * REQUIRED. We also have to register our pagination options & calculations. */ $this->set_pagination_args(array('total_items' => $total_items, 'per_page' => $per_page, 'total_pages' => ceil($total_items / $per_page))); }
/** * Summary. * * Description. * * @since x.x.x * @access (for functions: only use if private) * * @see Function/method/class relied on * @link URL * @global type $varname Description. * @global type $varname Description. * * @param type $var Description. * @param type $var Optional. Description. * @return type Description. */ public function get() { if (!empty($this->taxonomies_array)) { return $this->taxonomies_array; } $taxonomies = array(); /** * get custom taxonomies */ $custom_taxonomies = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array()); if (is_array($custom_taxonomies)) { foreach ($custom_taxonomies as $slug => $data) { /* * commented out next line as it's a db saved value and deactivated * build-in taxonomies also saved as custom */ // $data['_builtin'] = false; $taxonomies[$slug] = $data; } } /** * get built-in taxonomies */ $buildin_taxonomies = $this->object_to_array(wpcf_get_builtin_in_taxonomies('objects')); foreach ($buildin_taxonomies as $slug => $data) { // check if built-in taxonomies are already saved as custom taxonomies if (isset($taxonomies[$slug])) { continue; } if (!isset($data['slug'])) { $data['slug'] = $slug; } $taxonomies[$slug] = $data; } return $taxonomies; }