コード例 #1
0
ファイル: page.php プロジェクト: ekandreas/papi
/**
 * Get value for a property on a page.
 *
 * @param  int    $post_id
 * @param  string $slug
 * @param  mixed  $default
 * @param  string $type
 *
 * @return mixed
 */
function papi_get_field($post_id = null, $slug = null, $default = null, $type = 'page')
{
    if (!is_numeric($post_id) && is_string($post_id)) {
        $default = $slug;
        $slug = $post_id;
        $post_id = null;
    }
    if (!is_string($slug) || empty($slug)) {
        return $default;
    }
    $post_id = papi_get_post_id($post_id);
    if ($post_id === 0 && $type === Papi_Post_Page::TYPE) {
        return $default;
    }
    $value = papi_cache_get($slug, $post_id);
    if ($value === null || $value === false) {
        // Check for "dot" notation.
        $slugs = explode('.', $slug);
        $slug = $slugs[0];
        $slugs = array_slice($slugs, 1);
        // Get the right page for right content type.
        $page = papi_get_page($post_id, $type);
        // Return the default value if we don't have a valid page.
        if (is_null($page)) {
            return $default;
        }
        $value = papi_field_value($slugs, $page->get_value($slug), $default);
        if (papi_is_empty($value)) {
            return $default;
        }
        papi_cache_set($slug, $post_id, $value);
    }
    return $value;
}
コード例 #2
0
 /**
  * Get deep value.
  *
  * @param  string $slug
  * @param  mixed $value
  *
  * @return mixed
  */
 private function get_deep_value($slug, $value)
 {
     $slugs = explode('.', $slug);
     array_shift($slugs);
     return papi_field_value($slugs, $value, $value);
 }
コード例 #3
0
ファイル: page.php プロジェクト: nlemoine/papi
/**
 * Get value for a property on a page.
 *
 * @param  int    $id
 * @param  string $slug
 * @param  mixed  $default
 * @param  string $type
 *
 * @return mixed
 */
function papi_get_field($id = null, $slug = null, $default = null, $type = 'post')
{
    if (!is_numeric($id) && is_string($id)) {
        $type = empty($default) ? $type : $default;
        $default = $slug;
        $slug = $id;
        $id = null;
    }
    if (!is_string($slug) || empty($slug)) {
        return $default;
    }
    // Check for "dot" notation.
    $slugs = explode('.', $slug);
    $slug = $slugs[0];
    $slugs = array_slice($slugs, 1);
    // Get right id for right meta type.
    $id = papi_get_meta_id($type, $id);
    // Get the right store for right entry type.
    $store = papi_get_meta_store($id, $type);
    // Return the default value if we don't have a valid store.
    if (is_null($store)) {
        return $default;
    }
    // Determine if we should use the cache or not.
    $cache = $store->get_property_option($slug, 'cache', true);
    // Get the raw value from the cache.
    $raw_value = $cache ? papi_cache_get($slug, $id, $type) : false;
    // Load raw value if not cached.
    if ($raw_value === null || $raw_value === false) {
        $raw_value = $store->load_value($slug);
        if (papi_is_empty($raw_value)) {
            return $default;
        }
        if ($cache) {
            papi_cache_set($slug, $id, $raw_value, $type);
        } else {
            papi_cache_delete($slug, $id, $type);
        }
    }
    if (papi_is_empty($raw_value)) {
        return $default;
    }
    // Format raw value.
    $value = $store->format_value($slug, $raw_value);
    // Get value by dot keys if any.
    $value = papi_field_value($slugs, $value, $default);
    return papi_is_empty($value) ? $default : $value;
}
コード例 #4
0
ファイル: page.php プロジェクト: wp-papi/papi
/**
 * Get value for a property on a page.
 *
 * @param  int    $id
 * @param  string $slug
 * @param  mixed  $default
 * @param  string $type
 *
 * @return mixed
 */
function papi_get_field($id = null, $slug = null, $default = null, $type = 'post')
{
    if (!is_numeric($id) && is_string($id)) {
        $type = empty($default) ? $type : $default;
        $default = $slug;
        $slug = $id;
        $id = null;
    }
    if (!is_string($slug) || empty($slug)) {
        return $default;
    }
    // Check for "dot" notation.
    $slugs = explode('.', $slug);
    $slug = $slugs[0];
    $slugs = array_slice($slugs, 1);
    // Get right id for right meta type.
    $id = papi_get_meta_id($type, $id);
    // Get the right store for right entry type.
    $store = papi_get_meta_store($id, $type);
    // Return the default value if we don't have a valid store.
    if (is_null($store)) {
        return $default;
    }
    // Get value from store.
    $value = $store->get_value($id, $slug, $default, $type);
    // Get value by dot keys if any.
    return papi_field_value($slugs, $value, $default);
}