/**
  * Filter the post.
  *
  * Register all properties with `register_meta` and prepare response filter.
  *
  * @param  WP_Post $post
  *
  * @return WP_Post
  */
 public function get_post(WP_Post $post)
 {
     if (!($page_type = papi_get_entry_type_by_meta_id($post->ID))) {
         return $post;
     }
     // Register all properties fields with register meta.
     foreach ($page_type->get_properties() as $property) {
         $property->register();
     }
     // Get the post store for a page type.
     $this->store = papi_get_meta_store($post->ID);
     // Add filter to prepare the response for a post type.
     add_filter('rest_prepare_' . $post->post_type, [$this, 'prepare_response']);
     return $post;
 }
 /**
  * Get the store that the property will get data from.
  *
  * @return Papi_Core_Meta_Store|null
  */
 public function get_store()
 {
     if ($this->store instanceof Papi_Core_Meta_Store) {
         return $this->store;
     }
     return papi_get_meta_store($this->get_post_id());
 }
Example #3
0
 /**
  * Export data from Papi. With or without all property options.
  *
  * @param  mixed $post_id
  * @param  bool  $only_values
  *
  * @return array
  */
 public function export($post_id, $only_values = false)
 {
     $post_id = papi_get_post_id($post_id);
     if (empty($post_id)) {
         return [];
     }
     $slugs = papi_get_slugs($post_id);
     foreach ($slugs as $key => $box) {
         foreach ($box as $index => $slug) {
             unset($slugs[$key][$index]);
             $value = papi_get_field($post_id, $slug, null);
             if ($only_values === true) {
                 $slugs[$key][$slug] = $value;
             } else {
                 $store = papi_get_meta_store($post_id);
                 // @codeCoverageIgnoreStart
                 if (is_null($store)) {
                     continue;
                 }
                 // @codeCoverageIgnoreEnd
                 $property = $store->get_property($slug);
                 // @codeCoverageIgnoreStart
                 if (!papi_is_property($property)) {
                     continue;
                 }
                 // @codeCoverageIgnoreEnd
                 $options = clone $property->get_options();
                 $options->value = $value;
                 $slugs[$key][$slug] = $options;
             }
         }
     }
     return $slugs;
 }
Example #4
0
/**
 * Update field with new value. The old value will be deleted.
 *
 * @param  int    $id
 * @param  string $slug
 * @param  mixed  $value
 * @param  string $type
 *
 * @return bool
 */
function papi_update_field($id = null, $slug = null, $value = null, $type = 'post')
{
    if (!is_numeric($id) && is_string($id)) {
        $type = empty($value) ? $value : $type;
        $value = $slug;
        $slug = $id;
        $id = null;
    }
    if (!is_string($slug) || empty($slug)) {
        return false;
    }
    if (papi_is_empty($value)) {
        return papi_delete_field($id, $slug, $type);
    }
    $id = papi_get_meta_id($type, $id);
    $store = papi_get_meta_store($id, $type);
    if (is_null($store)) {
        return false;
    }
    $property = $store->get_property($slug);
    if (!papi_is_property($property)) {
        return false;
    }
    papi_delete_field($id, $slug, $type);
    $value = $property->update_value($value, $slug, $id);
    $value = papi_filter_update_value($property->get_option('type'), $value, $slug, $id, $type);
    return papi_update_property_meta_value(['type' => $type, 'id' => $id, 'slug' => $slug, 'value' => $value]);
}
Example #5
0
/**
 * Get the data page.
 *
 * @param  int    $id
 * @param  string $type
 *
 * @return Papi_Core_Meta_Store
 */
function papi_get_page($id = 0, $type = 'post')
{
    return papi_get_meta_store($id, $type);
}