Exemplo n.º 1
0
/**
 * 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;
}
 /**
  * Format the value of the property before it's returned
  * to WordPress admin or the site.
  *
  * @param  mixed  $values
  * @param  string $repeater_slug
  * @param  int    $post_id
  *
  * @return array
  */
 public function format_value($values, $repeater_slug, $post_id)
 {
     if (!is_array($values)) {
         return [];
     }
     foreach ($values as $index => $layout) {
         foreach ($layout as $slug => $value) {
             if (is_string($value) && preg_match($this->layout_value_regex, $value)) {
                 if (isset($values[$index][$this->layout_key])) {
                     unset($values[$index][$slug]);
                     continue;
                 }
                 $values[$index][$this->layout_key] = $value;
                 unset($values[$index][$slug]);
                 continue;
             }
             if (papi_is_property_type_key($slug)) {
                 continue;
             }
             $property_type_slug = papi_get_property_type_key_f($slug);
             if (!isset($values[$index][$property_type_slug])) {
                 continue;
             }
             $property_type_value = $values[$index][$property_type_slug];
             $property_type = papi_get_property_type($property_type_value);
             if (!is_object($property_type)) {
                 continue;
             }
             // Get property child slug.
             $child_slug = $this->get_child_slug($repeater_slug, $slug);
             // Create cache key.
             $cache_key = sprintf('%s_%d_%s', $repeater_slug, $index, $slug);
             // Get raw value from cache if enabled.
             if ($this->cache) {
                 $raw_value = papi_cache_get($cache_key, $post_id, $this->get_meta_type());
             } else {
                 $raw_value = false;
             }
             // Load the value.
             if ($raw_value === null || $raw_value === false) {
                 $values[$index][$slug] = $property_type->load_value($value, $child_slug, $post_id);
                 $values[$index][$slug] = papi_filter_load_value($property_type->type, $values[$index][$slug], $child_slug, $post_id, papi_get_meta_type());
                 if (!papi_is_empty($values[$index][$slug]) && $this->cache) {
                     papi_cache_set($cache_key, $post_id, $values[$index][$slug], $this->get_meta_type());
                 }
             } else {
                 $values[$index][$slug] = $raw_value;
             }
             if (strtolower($property_type->type) === 'repeater') {
                 $property_type->cache = false;
             }
             // Format the value from the property class.
             $values[$index][$slug] = $property_type->format_value($values[$index][$slug], $child_slug, $post_id);
             if (!is_admin()) {
                 $values[$index][$slug] = papi_filter_format_value($property_type->type, $values[$index][$slug], $child_slug, $post_id, papi_get_meta_type());
             }
             $values[$index][$property_type_slug] = $property_type_value;
         }
     }
     if (!is_admin()) {
         foreach ($values as $index => $row) {
             foreach ($row as $slug => $value) {
                 if (is_string($value) && preg_match($this->layout_value_regex, $value)) {
                     unset($values[$index][$slug]);
                     $values[$index]['_layout'] = preg_replace($this->layout_value_regex, '', $value);
                 }
                 if (papi_is_property_type_key($slug)) {
                     unset($values[$index][$slug]);
                 }
                 if (papi_is_empty($value)) {
                     unset($values[$index][$slug]);
                 }
             }
         }
     }
     return $values;
 }
Exemplo n.º 3
0
/**
 * Get number of how many pages uses the given page type.
 * This will also work with only page type.
 *
 * @param  string|object $page_type
 *
 * @return int
 */
function papi_get_number_of_pages($page_type)
{
    global $wpdb;
    if (empty($page_type) || !is_string($page_type) && !is_object($page_type)) {
        return 0;
    }
    if (is_object($page_type) && method_exists($page_type, 'get_id')) {
        $page_type = $page_type->get_id();
    }
    if (!is_string($page_type)) {
        return 0;
    }
    $value = papi_cache_get('page_type', $page_type);
    if ($value === false) {
        $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}postmeta WHERE `meta_key` = '%s' AND `meta_value` = '%s'";
        $sql = $wpdb->prepare($sql, papi_get_page_type_key(), $page_type);
        $value = intval($wpdb->get_var($sql));
        papi_cache_set('page_type', $page_type, $value);
    }
    return $value;
}
 /**
  * Format the value of the property before it's returned
  * to WordPress admin or the site.
  *
  * @param  mixed  $values
  * @param  string $repeater_slug
  * @param  int    $post_id
  *
  * @return array
  */
 public function format_value($values, $repeater_slug, $post_id)
 {
     if (!is_array($values)) {
         return [];
     }
     $values = papi_to_property_array_slugs($values, $repeater_slug);
     foreach ($values as $slug => $value) {
         if (papi_is_property_type_key($slug)) {
             continue;
         }
         $property_type_slug = papi_get_property_type_key_f($slug);
         if (!isset($values[$property_type_slug])) {
             continue;
         }
         // Get property type
         $property_type_value = $values[$property_type_slug];
         $property_type = papi_get_property_type($property_type_value);
         if (!is_object($property_type)) {
             continue;
         }
         // Get property child slug.
         $child_slug = $this->get_child_slug($repeater_slug, $slug);
         // Get raw value from cache if enabled.
         if ($this->cache) {
             $raw_value = papi_cache_get($slug, $post_id, $this->get_meta_type());
         } else {
             $raw_value = false;
         }
         // Load the value.
         if ($raw_value === null || $raw_value === false) {
             $values[$slug] = $property_type->load_value($value, $child_slug, $post_id);
             $values[$slug] = papi_filter_load_value($property_type->type, $values[$slug], $child_slug, $post_id, papi_get_meta_type());
             if (!papi_is_empty($values[$slug]) && $this->cache) {
                 papi_cache_set($slug, $post_id, $values[$slug], $this->get_meta_type());
             }
         } else {
             $values[$slug] = $raw_value;
         }
         // Format the value from the property class.
         $values[$slug] = $property_type->format_value($values[$slug], $child_slug, $post_id);
         if (!is_admin()) {
             $values[$slug] = papi_filter_format_value($property_type->type, $values[$slug], $child_slug, $post_id, papi_get_meta_type());
         }
         $values[$property_type_slug] = $property_type_value;
     }
     if (!is_admin()) {
         foreach ($values as $slug => $value) {
             if (papi_is_property_type_key($slug)) {
                 unset($values[$slug]);
             }
         }
     }
     return papi_from_property_array_slugs($values, $repeater_slug);
 }
Exemplo n.º 5
0
/**
 * 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;
}
Exemplo n.º 6
0
 /**
  * Get value.
  *
  * @param  int    $id
  * @param  string $slug
  * @param  mixed  $default
  * @param  string $type
  *
  * @return mixed
  */
 public function get_value($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;
     }
     $slug = strtolower($slug);
     // Determine if we should use the cache or not.
     $cache = $this->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 = $this->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 = $this->format_value($slug, $raw_value);
     return papi_is_empty($value) ? $default : $value;
 }