示例#1
0
文件: page.php 项目: KristoferN/papi
/**
 * Get all page types that exists.
 *
 * @param  bool   $all
 * @param  string $post_type
 * @param  bool   $fake_post_types
 *
 * @return array
 */
function papi_get_all_page_types($all = false, $post_type = null, $fake_post_types = false)
{
    if (empty($post_type)) {
        $post_type = papi_get_post_type();
    }
    $cache_key = papi_cache_key(sprintf('%s_%s', $all, $post_type), $fake_post_types);
    $page_types = wp_cache_get($cache_key);
    $load_once = papi_filter_core_load_one_type_on();
    if (empty($page_types)) {
        $files = papi_get_all_page_type_files();
        foreach ($files as $file) {
            $page_type = papi_get_page_type($file);
            if (is_null($page_type)) {
                continue;
            }
            if ($page_type instanceof Papi_Page_Type === false) {
                continue;
            }
            if (papi()->exists('core.page_type.' . $page_type->post_type[0])) {
                if (!empty($page_types)) {
                    continue;
                }
            } else {
                if (in_array($page_type->post_type[0], $load_once)) {
                    papi()->singleton('core.page_type.' . $page_type->post_type[0], $page_type->get_id());
                }
            }
            if ($fake_post_types) {
                if (isset($page_type->post_type[0]) && !post_type_exists($page_type->post_type[0])) {
                    // Boot page type.
                    $page_type->boot();
                    // Add it to the page types array.
                    $page_types[] = $page_type;
                }
                continue;
            } else {
                if ($page_type instanceof Papi_Option_Type) {
                    continue;
                }
            }
            // Add the page type if the post types is allowed.
            if (!is_null($page_type) && papi_current_user_is_allowed($page_type->capabilities) && ($all || in_array($post_type, $page_type->post_type))) {
                // Boot page type.
                $page_type->boot();
                // Add it to the page types array.
                $page_types[] = $page_type;
            }
        }
        if (is_array($page_types)) {
            usort($page_types, function ($a, $b) {
                return strcmp($a->name, $b->name);
            });
            wp_cache_set($cache_key, $page_types);
        }
    }
    if (!is_array($page_types)) {
        return [];
    }
    return papi_sort_order(array_reverse($page_types));
}
示例#2
0
文件: cache.php 项目: wp-papi/papi
/**
 * Set value in cache.
 *
 * @param  string $key
 * @param  mixed  $suffix
 * @param  mixed  $value
 * @param  string $type
 *
 * @return bool
 */
function papi_cache_set($key, $suffix, $value, $type = 'post')
{
    $key = papi_cache_key($key, $suffix, $type);
    if (is_admin()) {
        $key = 'admin_' . $key;
    }
    return wp_cache_set($key, $value);
}
示例#3
0
文件: entry.php 项目: nlemoine/papi
/**
 * Get all entry types that exists.
 *
 * @param  array $args {
 *   @type bool         $all
 *   @type mixed        $args
 *   @type bool         $cache
 *   @type array|string $id
 *   @type string       $mode
 *   @type array|string $types
 * }
 *
 * @return array
 */
function papi_get_all_entry_types(array $args = [])
{
    $default_args = ['all' => true, 'args' => [], 'cache' => true, 'mode' => 'include', 'types' => []];
    $args = array_merge($default_args, $args);
    if (!is_array($args['types'])) {
        $args['types'] = [$args['types']];
    }
    if (!is_array($args['args'])) {
        $args['args'] = [$args['args']];
    }
    $args['args'] = array_filter($args['args']);
    if (!empty($args['types'])) {
        $args['all'] = false;
    }
    $cache_key = papi_cache_key(__FUNCTION__, md5(serialize($args)));
    if (!$args['cache']) {
        papi()->remove('papi_get_all_core_type_files');
        papi()->remove($cache_key);
    }
    if (papi()->exists($cache_key)) {
        return papi()->make($cache_key);
    }
    $singletons = [];
    $entry_types = [];
    $files = papi_get_all_core_type_files();
    foreach ($files as $file) {
        $entry_type = papi_get_entry_type($file);
        if (is_null($entry_type)) {
            continue;
        }
        // Only entry types can be loaded.
        // @codeCoverageIgnoreStart
        if ($entry_type instanceof Papi_Entry_Type === false) {
            continue;
        }
        // @codeCoverageIgnoreEnd
        if ($entry_type->singleton()) {
            if (isset($singletons[$entry_type->type])) {
                continue;
            } else {
                $singletons[$entry_type->type] = true;
            }
        }
        $id = $entry_type->get_id();
        $valid_type = in_array($entry_type->type, $args['types'], true);
        $valid_type = $args['mode'] === 'include' ? $valid_type : !$valid_type;
        if ($args['all'] || $valid_type && call_user_func_array([$entry_type, 'allowed'], $args['args'])) {
            $entry_type->boot();
            $entry_types[$id] = $entry_type;
        }
    }
    $entry_types = array_values($entry_types);
    if (is_array($entry_types)) {
        usort($entry_types, function ($a, $b) {
            return strcmp($a->name, $b->name);
        });
    }
    $entry_types = papi_sort_order(array_reverse($entry_types));
    if (!papi()->exists($cache_key)) {
        papi()->singleton($cache_key, $entry_types);
    }
    return $entry_types;
}
示例#4
0
文件: cache.php 项目: KristoferN/papi
/**
 * Set value in cache.
 *
 * @param  string $key
 * @param  mixed  $suffix
 * @param  mixed  $value
 *
 * @return bool
 */
function papi_cache_set($key, $suffix, $value)
{
    return wp_cache_set(papi_cache_key($key, $suffix), $value);
}