/** * Get all page types based on a post type. * * @param string $post_type * * @return array */ function papi_get_all_page_types($post_type = '') { $content_types = papi_get_all_content_types(['args' => $post_type, 'mode' => 'include', 'types' => ['attachment', 'page']]); $page_types = array_filter($content_types, 'papi_is_page_type'); if (is_array($page_types)) { usort($page_types, function ($a, $b) { return strcmp($a->name, $b->name); }); } return papi_sort_order(array_reverse($page_types)); }
/** * Admin init. * * Setup the page type. */ public function admin_init() { // Preload all page types. foreach (papi_get_post_types() as $post_type) { papi_get_all_content_types(['args' => $post_type]); } if (!$this->setup_papi()) { return; } $this->page_type->setup(); }
/** * Check if option type exists. * * @param string $id * * @return bool */ function papi_option_type_exists($id) { $exists = false; $option_types = papi_get_all_content_types(['types' => 'option']); foreach ($option_types as $option_type) { if ($option_type->match_id($id)) { $exists = true; break; } } return $exists; }
/** * Page items menu. * * This function will register all content types * that has a fake post type. Like option types. */ public function page_items_menu() { $content_types = papi_get_all_content_types(['mode' => 'exclude', 'types' => 'page']); foreach ($content_types as $content_type) { // @codeCoverageIgnoreStart if (empty($content_type->menu) || empty($content_type->name)) { continue; } // @codeCoverageIgnoreEnd $slug = sprintf('papi/%s/%s', $content_type->get_type(), $content_type->get_id()); add_submenu_page($content_type->menu, $content_type->name, $content_type->name, $content_type->capability, $slug, [$content_type, 'render']); } }
/** * List Papi types. * * ## Options * * [--<field>=<value>] * : Filter types based on type property. * * [--field=<field>] * : Prints the value of a single field for each type. * * [--fields=<fields>] * : Limit the output to specific type fields. * * [--format=<format>] * : Acceptec values: table, csv, json, count, ids. Default: table. * * ## AVAILABLE FIELDS * * These fields will be displayed by default for each type: * * * name * * id * * post_type * * template * * number_of_pages * * type * * Not all fields exists on a Papi type so some fields will have `n/a` * as value when no value can be displayed. * * ## EXAMPLES * * wp papi type list * * @subcommand list */ public function list_($_, $assoc_args) { // Get all page types. $types = papi_get_all_content_types(); // Create type item with the fields that // will be displayed. $types = array_map(function ($type) { $is_page_type = papi_is_page_type($type); return ['id' => $type->get_id(), 'name' => $type->name, 'post_type' => $is_page_type ? implode(', ', $type->post_type) : 'n/a', 'template' => $is_page_type ? $type->template : 'n/a', 'type' => $type->get_type(), 'number_of_pages' => $is_page_type ? papi_get_number_of_pages($type->get_id()) : 'n/a']; }, $types); // Render types as a table. $formatter = $this->get_formatter($assoc_args); $formatter->display_items($types); }
/** * Load property from page type. * * @param string $slug * @param string $child_slug * * @return object */ public function get_property($slug, $child_slug = '') { $content_type_id = papi_get_qs('page'); if (empty($content_type_id)) { $property = null; $content_types = papi_get_all_content_types(['types' => 'option']); foreach ($content_types as $index => $content_type) { if ($property = $content_type->get_property($slug, $child_slug)) { $this->option_type = $content_type; break; } } if (is_null($property)) { return; } return $property; } $content_type = papi_get_content_type_by_id($content_type_id); if (!papi_is_option_type($content_type)) { return; } $this->option_type = $content_type; return $content_type->get_property($slug, $child_slug); }
/** * Get content type by identifier. * * @param string $id * * @return Papi_content_type */ function papi_get_content_type_by_id($id) { if (!is_string($id) || empty($id)) { return; } $result = null; $content_types = papi_get_all_content_types(); foreach ($content_types as $content_type) { if ($content_type->match_id($id)) { $result = $content_type; break; } } if (is_null($result)) { $path = papi_get_file_path($id); $result = papi_get_content_type($path); } return $result; }