Ejemplo n.º 1
0
 /**
  * Load post new action
  * Redirect to right url if no page type is set.
  */
 public function load_post_new()
 {
     $request_uri = $_SERVER['REQUEST_URI'];
     $post_types = papi_get_post_types();
     if (in_array($this->post_type, $post_types, true) && strpos($request_uri, 'page_type=') === false) {
         $parsed_url = parse_url($request_uri);
         $only_page_type = papi_filter_settings_only_page_type($this->post_type);
         $page_types = papi_get_all_page_types($this->post_type);
         $show_standard = false;
         if (count($page_types) === 1 && empty($only_page_type)) {
             $show_standard = $page_types[0]->standard_type;
             $show_standard = $show_standard ? papi_filter_settings_show_standard_page_type($this->post_type) : $show_standard;
             $only_page_type = $show_standard ? '' : $page_types[0]->get_id();
         }
         // Check if we should show one post type or not and
         // create the right url for that.
         if (!empty($only_page_type) && !$show_standard) {
             $url = papi_get_page_new_url($only_page_type, false);
         } else {
             $page = 'page=papi-add-new-page,' . $this->post_type;
             if ($this->post_type !== 'post') {
                 $page = '&' . $page;
             }
             $url = 'edit.php?' . $parsed_url['query'] . $page;
         }
         wp_safe_redirect($url);
         is_admin() && exit;
     }
 }
Ejemplo n.º 2
0
/**
 * 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;
}
Ejemplo n.º 3
0
 /**
  * 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;
 }
Ejemplo n.º 4
0
 /**
  * Setup menu items for real post types.
  */
 public function post_types_menu()
 {
     global $submenu;
     $post_types = papi_get_post_types();
     foreach ($post_types as $post_type) {
         if (!post_type_exists($post_type)) {
             continue;
         }
         if ($post_type === 'post') {
             $edit_url = 'edit.php';
         } else {
             $edit_url = 'edit.php?post_type=' . $post_type;
         }
         if (!isset($submenu[$edit_url]) || !isset($submenu[$edit_url][10]) || !isset($submenu[$edit_url][10][2])) {
             $post_type_object = get_post_type_object($post_type);
             if (!$post_type_object->show_in_menu) {
                 $submenu[$edit_url] = [10 => [__('Add New', 'papi'), 'edit_posts', 'post-new.php']];
             } else {
                 continue;
             }
         }
         $only_page_type = papi_filter_settings_only_page_type($post_type);
         $page_types = papi_get_all_page_types(false, $post_type);
         $show_standard = false;
         // Don't change menu item when no page types is found.
         // @codeCoverageIgnoreStart
         if (empty($page_types)) {
             continue;
         }
         // @codeCoverageIgnoreEnd
         if (count($page_types) === 1 && empty($only_page_type)) {
             $show_standard = $page_types[0]->standard_type;
             $show_standard = $show_standard ? papi_filter_settings_show_standard_page_type($post_type) : $show_standard;
             $only_page_type = $show_standard ? '' : $page_types[0]->get_id();
         }
         if (!empty($only_page_type) && !$show_standard) {
             $submenu[$edit_url][10][2] = papi_get_page_new_url($only_page_type, false, $post_type, ['action', 'message', 'page_type', 'post', 'post_type']);
         } else {
             $page = 'papi-add-new-page,' . $post_type;
             $start = strpos($edit_url, 'post_type') === false ? '?' : '&';
             $submenu[$edit_url][10][2] = sprintf('%s%spage=%s', $edit_url, $start, $page);
             // Add menu item.
             add_menu_page(__('Add New', 'papi'), __('Add New', 'papi'), 'read', $page, [$this, 'render_view']);
             // Remove the menu item so it's hidden.
             remove_menu_page($page);
         }
     }
 }
Ejemplo n.º 5
0
 /**
  * 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;
 }
Ejemplo n.º 6
0
/**
 * 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;
}