/** * Get WordPress post type in various ways. * * @param int $post_id * * @return string */ function papi_get_post_type($post_id = null) { if ($post_type = papi_get_or_post('post_type')) { return $post_type; } $post_id = papi_get_post_id($post_id); if ($post_id !== 0) { return strtolower(get_post_type($post_id)); } $page = papi_get_qs('page'); if (is_string($page) && strpos(strtolower($page), 'papi-add-new-page,') !== false) { $exploded = explode(',', $page); if (empty($exploded[1])) { return ''; } return $exploded[1]; } // If only `post-new.php` without any querystrings // it would be the post post type. $req_uri = $_SERVER['REQUEST_URI']; $exploded = explode('/', $req_uri); $last = end($exploded); if ($last === 'post-new.php') { return 'post'; } return ''; }
/** * Render tools page view. */ public function render_view() { $page_view = (string) papi_get_qs('view'); if (empty($page_view)) { $page_view = 'management-start'; } $this->view->render($page_view); }
/** * Prepare taxonomy types, add standard taxonomy if it should be added. * * @param array $taxonomy_types * * @return array */ protected function prepare_taxonomy_types(array $taxonomy_types) { $taxonomy = papi_get_qs('taxonomy'); if (papi_filter_settings_show_standard_taxonomy_type($taxonomy)) { $id = sprintf('papi-standard-%s-type', $taxonomy); $taxonomy_type = new Papi_Taxonomy_Type($id); $taxonomy_type->id = $id; $taxonomy_type->name = papi_filter_settings_standard_taxonomy_type_name($taxonomy); $taxonomy_type->taxonomy = [$taxonomy]; $taxonomy_types[] = $taxonomy_type; } usort($taxonomy_types, function ($a, $b) { return strcmp($a->name, $b->name); }); return papi_sort_order(array_reverse($taxonomy_types)); }
/** * Load property from page type. * * @param string $slug * @param string $child_slug * * @return object */ public function get_property($slug, $child_slug = '') { $page_type_id = str_replace('papi/', '', papi_get_qs('page')); if (empty($page_type_id)) { $page_types = papi_get_all_page_types(false, null, true); $property = null; foreach ($page_types as $index => $page_type) { if ($property = $page_type->get_property($slug, $child_slug)) { break; } } if (is_null($property)) { return; } return $property; } $page_type = papi_get_page_type_by_id($page_type_id); if ($page_type instanceof Papi_Option_Type === false) { return; } return $page_type->get_property($slug, $child_slug); }
/** * Render the property. */ public function render() { // Check so the property has a type and capabilities on the property. if (!papi_current_user_is_allowed($this->get_option('capabilities'))) { return; } // A disabled property should not be rendered. if ($this->disabled()) { return; } // Only render if it's the right language if the definition exist. if ($this->get_option('lang') === strtolower(papi_get_qs('lang'))) { $render = true; } else { $render = $this->get_option('lang') === false && papi_is_empty(papi_get_qs('lang')); } if ($this->display()) { $this->display = $this->render_is_allowed_by_rules(); } $this->render_row_html(); $this->render_hidden_html(); $this->render_rules_json(); }
/** * Render the property. */ public function render() { // Check if current user can view the property. if (!$this->current_user_can()) { return; } // A disabled property should not be rendered. if ($this->disabled()) { return; } // Only render if it's the right language if the definition exist. if ($this->get_option('lang') === strtolower(papi_get_qs('lang'))) { $this->display = true; } else { $this->display = $this->get_option('lang') === false && papi_is_empty(papi_get_qs('lang')); } // Check rules. if ($this->display()) { $this->display = $this->render_is_allowed_by_rules(); } $this->render_row_html(); $this->render_hidden_html(); $this->render_rules_json(); }
/** * 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); }
/** * Load property from page type. * * @param string $slug * @param string $child_slug * * @return null|object */ public function get_property($slug, $child_slug = '') { $entry_type_id = papi_get_qs('page'); if (empty($entry_type_id)) { $property = null; $entry_types = papi_get_all_entry_types(['types' => 'option']); foreach ($entry_types as $entry_type) { if ($property = $entry_type->get_property($slug, $child_slug)) { break; } } if (is_null($property)) { return; } return $property; } $entry_type = papi_get_entry_type_by_id($entry_type_id); if ($entry_type instanceof Papi_Option_Type === false) { return; } if ($property = $entry_type->get_property($slug, $child_slug)) { return $this->prepare_property($property); } }
/** * Menu callback that loads right view depending on what the `page` query string says. */ public function render_view() { if (strpos(papi_get_qs('page'), 'papi') !== false) { $page = str_replace('papi-', '', papi_get_qs('page')); $res = preg_replace('/\\,.*/', '', $page); if (is_string($res)) { $page_view = $res; } } if (!isset($page_view)) { $page_view = null; } if (!is_null($page_view)) { $view = new Papi_Admin_View(); $view->render($page_view); } else { echo '<h2>Papi - 404</h2>'; } }
/** * Get property html via GET. * * GET /papi-ajax/?action=get_property */ public function get_property() { $default_options = Papi_Core_Property::create()->get_options(); $keys = array_keys(get_object_vars($default_options)); $options = papi_get_qs($keys, true); if ($property = papi_property($options)) { ob_start(); $property->render_ajax_request(); $html = ob_get_clean(); wp_send_json(['html' => utf8_encode($html)]); } else { $this->render_error('No property found'); } }
/** * Render AJAX request. */ public function render_ajax_request() { $items = $this->get_settings_properties(); if (papi_doing_ajax()) { $counter = papi_get_qs('counter'); $this->counter = intval($counter); } $this->render_properties($items, false); }
/** * Get content type id. * * @return string */ function papi_get_content_type_id() { $content_type_id = papi_get_qs('content_type'); /** * Change content type id. * * @param string $content_type_id */ return apply_filters('papi/content_type_id', $content_type_id); }
/** * Get terms via GET. * * GET /papi-ajax/?action=get_terms */ public function get_terms() { $query = papi_get_qs('query') ?: []; $query = is_array($query) ? $query : []; $taxonomy = papi_get_qs('taxonomy') ?: ''; $args = array_merge($query, ['fields' => 'id=>name']); $terms = []; if (taxonomy_exists($taxonomy)) { $terms = get_terms($taxonomy, $args); } wp_send_json($terms); }
/** * Get Entry Type instance. * * @return Papi_Entry_Type|false */ private function get_entry_type() { if ($this->entry_type instanceof Papi_Entry_Type) { return $this->entry_type; } $entry_type_id = papi_get_entry_type_id(); // If the entry type id is empty try to load // the entry type id from `page` query string. // // Example: // /wp-admin/options-general.php?page=papi/option/site-option-type if (empty($entry_type_id)) { $entry_type_id = preg_replace('/^papi\\/\\w+\\//', '', papi_get_qs('page')); } // Use the default entry type id if empty. if (empty($entry_type_id)) { $entry_type_id = papi_get_entry_type_id(); } // If no entry type id exists Papi can't setup a entry type. if (empty($entry_type_id)) { return false; } return $this->entry_type = papi_get_entry_type_by_id($entry_type_id); }
/** * Load the content type id on a post types. * * @param string $content_type_id * * @return string */ function papi_load_page_type_id($content_type_id = '') { $key = papi_get_page_type_key(); $post_id = papi_get_post_id(); $post_type = papi_get_post_type($post_id); // If we have a post id we can load the content type id // from the post. if ($post_id > 0) { $meta_value = get_post_meta($post_id, $key, true); $content_type_id = empty($meta_value) ? '' : $meta_value; } // Try to fetch the content type id from `page_type` // query string. if (empty($content_type_id)) { $content_type_id = papi_get_qs('page_type'); } // When using `only_page_type` filter we need to fetch the value since it // maybe not always saved in the database. if (empty($content_type_id)) { $content_type_id = papi_filter_settings_only_page_type($post_type); } // Load right content type from the parent post id. if (empty($content_type_id)) { $meta_value = get_post_meta(papi_get_parent_post_id(), $key, true); $content_type_id = empty($meta_value) ? '' : $meta_value; } // Load content type id from the container if it exists. if (empty($content_type_id)) { $key = sprintf('content_type_id.%s', $post_type); if (papi()->exists($key)) { return papi()->make($key); } } return $content_type_id; }
/** * Get entry type id. * * @param int $id * @param string $type * * @return string */ function papi_get_entry_type_id($id = 0, $type = null) { $type = papi_get_meta_type($type); $id = papi_get_meta_id($type, $id); if ($id > 0) { if ($meta_value = get_metadata($type, $id, papi_get_page_type_key(), true)) { return $meta_value; } } $entry_type_id = papi_get_qs('entry_type'); /** * Change entry type id. * * @param string $entry_type_id * @param string $type * * @return string */ return apply_filters('papi/entry_type_id', $entry_type_id, $type); }
/** * Get right meta type value. It will treat option * as a meta type even if isn't a real meta type. * * @param string $type * * @return string|null */ function papi_get_meta_type($type = null) { if ($meta_type = papi_get_qs('meta_type')) { return $meta_type; } switch ($type) { case 'option': return 'option'; case 'post': case 'page': return 'post'; case 'taxonomy': case 'term': return 'term'; default: break; } $request_uri = $_SERVER['REQUEST_URI']; $parsed_url = parse_url($request_uri); if (!empty($parsed_url['query'])) { // Taxonomy page in admin. if (is_admin() && preg_match('/taxonomy=/', $parsed_url['query'])) { return 'term'; } // Option page in admin. if (is_admin() && preg_match('/page\\=papi(\\%2F|\\/)option/', $parsed_url['query'])) { return 'option'; } } // When doing ajax we need to check if it's a taxonomy ajax or a post type ajax. if (is_admin() && defined('DOING_AJAX') && DOING_AJAX) { if (isset($_POST['taxonomy'])) { return 'term'; } if (isset($_POST['post_type'])) { return 'post'; } } // On the frontend term should be returned if on a category or tag page. if (!is_admin() && (is_category() || is_tag())) { return 'term'; } // Check queried object for right meta type. if ($obj = get_queried_object()) { if ($obj instanceof WP_Term || isset($obj->term_id)) { return 'term'; } if ($obj instanceof WP_Post || isset($obj->post_id)) { return 'post'; } } // Default was has to be set here since we trying to figure out // which url conform which meta type. if (is_null($type)) { $type = 'post'; } // If meta type exists as a filter we can return it. if (function_exists("get_{$type}_meta")) { return $type; } }
/** * Get query string if it exists and is not empty. * * @param array|string $qs * @param bool $keep_keys * * @return array|string */ function papi_get_qs($qs, $keep_keys = false) { if (!is_string($qs) && !is_array($qs)) { return; } if (is_array($qs)) { if ($keep_keys) { $results = []; foreach ($qs as $key) { $value = papi_get_qs($key); if (!papi_is_empty($value)) { $results[$key] = $value; } } return $results; } else { return array_map('papi_get_qs', $qs); } } if (isset($_GET[$qs]) && !empty($_GET[$qs])) { $value = $_GET[$qs]; if (is_string($value)) { $value = sanitize_text_field($value); } if ($value === 'false') { $value = false; } if ($value === 'true') { $value = true; } return $value; } }
/** * Get page type id. * * @param int $post_id * * @return string */ function papi_get_page_type_id($post_id = 0) { $post_id = papi_get_post_id($post_id); $key = papi_get_page_type_key(); $page_type = ''; if ($post_id !== 0) { $meta_value = get_post_meta($post_id, $key, true); $page_type = empty($meta_value) ? '' : $meta_value; } if (empty($page_type)) { $page_type = str_replace('papi/', '', papi_get_qs('page_type')); } if (empty($page_type)) { $page_type = papi_get_sanitized_post(papi_get_page_type_key()); } // Load right page type from a post query string if (empty($page_type)) { $meta_value = get_post_meta(papi_get_parent_post_id(), $key, true); $page_type = empty($meta_value) ? '' : $meta_value; } // Load page type id from the container if it exists or // load it from `papi_get_all_page_types`. if (empty($page_type)) { $post_type = papi_get_post_type(); $load_once = papi_filter_core_load_one_type_on(); $collection_key = 'core.page_type.' . $post_type; if (in_array($post_type, $load_once)) { if (papi()->exists($collection_key)) { return papi()->make($collection_key); } if ($page_types = papi_get_all_page_types(false, $post_type)) { return $page_types[0]->get_id(); } } } return $page_type; }
/** * Filter page types in post type list. */ public function restrict_page_types() { $post_types = papi_get_post_types(); if (in_array($this->post_type, $post_types, true)) { $page_types = papi_get_all_page_types($this->post_type); $page_types = array_map(function ($page_type) { return ['name' => $page_type->name, 'value' => $page_type->get_id()]; }, $page_types); // Add the standard page that isn't a real page type. if (papi_filter_settings_show_standard_page_type_in_filter($this->post_type)) { $page_types[] = ['name' => papi_filter_settings_standard_page_type_name($this->post_type), 'value' => 'papi-standard-page']; } usort($page_types, function ($a, $b) { return strcmp(strtolower($a['name']), strtolower($b['name'])); }); ?> <select name="page_type" class="postform"> <option value="0" selected><?php esc_html_e('All types', 'papi'); ?> </option> <?php foreach ($page_types as $page_type) { printf('<option value="%s" %s>%s</option>', esc_attr($page_type['value']), papi_get_qs('page_type') === $page_type['value'] ? ' selected' : '', esc_html($page_type['name'])); } ?> </select> <?php } }
/** * Load right Papi file if it exists. * * @return bool */ public function setup_papi() { // If the post type isn't in the post types array we can't proceed. if (in_array($this->post_type, ['revision', 'nav_menu_item'])) { return false; } if (empty($this->page_type_id)) { // If only page type is used, override the page type value. $this->page_type_id = papi_filter_settings_only_page_type($this->post_type); if (empty($this->page_type_id)) { // Load page types that don't have any real post type. $this->page_type_id = str_replace('papi/', '', papi_get_qs('page')); } if (empty($this->page_type_id)) { $this->page_type_id = papi_get_page_type_id(); } } if (empty($this->page_type_id)) { return false; } $this->page_type = papi_get_page_type_by_id($this->page_type_id); // Do a last check so we can be sure that we have a page type instance. return $this->page_type instanceof Papi_Page_Type; }
/** * Render AJAX request. */ public function render_ajax_request() { $items = null; $layouts = $this->get_settings_layouts(); if (defined('DOING_PAPI_AJAX') && DOING_PAPI_AJAX) { $counter = papi_get_qs('counter'); $this->counter = intval($counter); $flexible_layout = papi_get_qs('flexible_layout'); foreach ($layouts as $layout) { if ($layout['slug'] === $flexible_layout) { $items = $layout; break; } } } if (!empty($items)) { $this->render_properties($items, false); } }
/** * Load right Papi file if it exists. * * @return bool */ public function setup_papi() { // If the post type isn't in the post types array we can't proceed. if (in_array($this->post_type, ['revision', 'nav_menu_item'])) { return false; } $content_type_id = papi_get_content_type_id(); // If a post type exists, try to load the content type id // from only page type filter. if ($this->post_type) { $content_type_id = papi_filter_settings_only_page_type($this->post_type); } // If the content type id is empty try to load // the content type id from `page` query string. // // Example: // /wp-admin/options-general.php?page=papi/option/site-option-type if (empty($content_type_id)) { $content_type_id = preg_replace('/^papi\\/\\w+\\//', '', papi_get_qs('page')); } // Use the default content type id if empty. if (empty($content_type_id)) { $content_type_id = papi_get_content_type_id(); } // If no content type id exists Papi can't setup a content type. if (empty($content_type_id)) { return false; } // Do a last check so we can be sure that we have a page type instance. return ($this->page_type = papi_get_content_type_by_id($content_type_id)) instanceof Papi_Content_Type; }
</table> <?php } } ?> <div class="wrap"> <div class="papi-options-logo"></div> <h1><?php echo papi()->name; ?> </h1> <br/> <?php $page_type = papi_get_qs('page_type'); $page_type = papi_get_page_type_by_id($page_type); if (empty($page_type)) { ?> <h3><?php _e('Cannot find the page type', 'papi'); ?> </h3> <p> <a href="tools.php?page=papi"><?php echo esc_html('Back to list', 'papi'); ?> </a> </p> <?php } else {
/** * Load the entry type id on a post types. * * @param string $entry_type_id * * @return string */ function papi_load_page_type_id($entry_type_id = '') { $key = papi_get_page_type_key(); $post_id = papi_get_post_id(); $post_type = papi_get_post_type($post_id); // Try to load the entry type id from only page type filter. if (empty($entry_type_id)) { $entry_type_id = papi_filter_settings_only_page_type($post_type); } // If we have a post id we can load the entry type id from the post. if (empty($entry_type_id) && $post_id > 0) { $meta_value = get_post_meta($post_id, $key, true); $entry_type_id = empty($meta_value) ? '' : $meta_value; } // Try to fetch the entry type id from `page_type` query string. if (empty($entry_type_id)) { $entry_type_id = papi_get_qs('page_type'); } // Load right entry type from the parent post id. if (empty($entry_type_id)) { $meta_value = get_post_meta(papi_get_parent_post_id(), $key, true); $entry_type_id = empty($meta_value) ? '' : $meta_value; } // Try to load the entry type from all page types and check // if only one exists of that post type. // // The same as only page type filter but without the filter. if (empty($entry_type_id)) { $key = sprintf('entry_type_id.post_type.%s', $post_type); if (papi()->exists($key)) { return papi()->make($key); } $entries = papi_get_all_page_types($post_type); if (count($entries) === 1) { $entry_type_id = $entries[0]->get_id(); papi()->bind($key, $entry_type_id); } } return $entry_type_id; }