Ejemplo n.º 1
0
 /**
  * Delete value from the database.
  *
  * @param  string $slug
  * @param  int    $post_id
  * @param  string $type
  *
  * @return bool
  */
 public function delete_value($slug, $post_id, $type)
 {
     $rows = intval(papi_get_property_meta_value($post_id, $slug));
     $value = $this->load_value($rows, $slug, $post_id);
     $value = papi_to_property_array_slugs($value, $slug);
     $result = true;
     foreach (array_keys($value) as $key) {
         $out = papi_delete_property_meta_value($post_id, $key, $type);
         $result = $out ? $result : $out;
     }
     return $result;
 }
Ejemplo n.º 2
0
 /**
  * Delete value from the database.
  *
  * @param  string $slug
  * @param  int    $post_id
  * @param  string $type
  *
  * @return bool
  */
 public function delete_value($slug, $post_id, $type)
 {
     $values = $this->load_value(null, $slug, $post_id);
     $result = true;
     foreach ($values as $key => $val) {
         $out = papi_delete_property_meta_value($post_id, $slug . '_' . $key);
         $result = $out ? $result : $out;
     }
     if ($result) {
         $result = papi_delete_property_meta_value($post_id, $slug);
     }
     return $result;
 }
Ejemplo n.º 3
0
 /**
  * Delete value from the database.
  *
  * @param  string $slug
  * @param  int    $post_id
  * @param  string $type
  *
  * @return bool
  */
 public function delete_value($slug, $post_id, $type)
 {
     $values = $this->load_value(null, $slug, $post_id);
     $values = is_object($values) ? (array) $values : $values;
     $result = true;
     foreach (array_keys($values) as $key) {
         $out = papi_delete_property_meta_value($post_id, $slug . '_' . $key);
         $result = $out ? $result : $out;
     }
     if ($result) {
         $result = papi_delete_property_meta_value($post_id, $slug);
     }
     return $result;
 }
Ejemplo n.º 4
0
 /**
  * Delete value from the database.
  *
  * @param  string $slug
  * @param  int    $post_id
  * @param  string $type
  *
  * @return bool
  */
 public function delete_value($slug, $post_id, $type)
 {
     return papi_delete_property_meta_value($post_id, $slug, $type);
 }
Ejemplo n.º 5
0
/**
 * Update property values on the post with the given post id
 * or update property values on the option page.
 *
 * @param  array $meta
 *
 * @return bool
 */
function papi_update_property_meta_value(array $meta = [])
{
    $meta = array_merge(['id' => 0, 'slug' => '', 'type' => 'post', 'value' => ''], $meta);
    $meta = (object) $meta;
    $meta->type = papi_get_meta_type($meta->type);
    $save_value = true;
    // Set the right update value function for the type.
    $update_value_fn = $meta->type === 'option' ? 'update_option' : 'update_metadata';
    /**
     * Change update function.
     *
     * @param string $update_value_fn
     */
    $update_value_fn = apply_filters('papi/core/update_value_fn', $update_value_fn);
    // Check so the function is callable before using it.
    if (!is_callable($update_value_fn)) {
        return;
    }
    // Check for string keys in the array if any.
    foreach (papi_to_array($meta->value) as $key => $value) {
        if (is_string($key)) {
            $save_value = false;
            break;
        }
    }
    // If main value shouldn't be saved it should be array.
    if (!$save_value && is_array($meta->value)) {
        $meta->value = [$meta->value];
    }
    // Delete saved value if empty.
    if (papi_is_empty($meta->value)) {
        return papi_delete_property_meta_value($meta->id, $meta->slug, $meta->type);
    }
    $result = true;
    foreach (papi_to_array($meta->value) as $key => $value) {
        // Delete saved value if value is empty.
        if (papi_is_empty($value) || $value === '[]' || $value === '{}') {
            return papi_delete_property_meta_value($meta->id, $meta->slug, $meta->type);
        }
        // Delete main value cache.
        papi_cache_delete($meta->slug, $meta->id, $meta->type);
        // If not a array we can save the value.
        if (!is_array($value)) {
            if ($save_value) {
                $value = $meta->value;
            }
            if (papi_get_meta_type($meta->type) === 'option') {
                $out = call_user_func_array($update_value_fn, [unpapify($meta->slug), $value]);
                $result = $out ? $result : $out;
            } else {
                $out = call_user_func_array($update_value_fn, [$meta->type, $meta->id, unpapify($meta->slug), $value]);
                $result = $out ? $result : $out;
            }
            continue;
        }
        // Delete all child value caches.
        papi_update_property_meta_value_cache_delete($meta, $value);
        // Update metadata or option value for all child values.
        foreach ($value as $child_key => $child_value) {
            if (papi_is_empty($child_value)) {
                papi_delete_property_meta_value($meta->id, $child_key, $meta->type);
            } else {
                if (papi_get_meta_type($meta->type) === 'option') {
                    call_user_func_array($update_value_fn, [unpapify($child_key), $child_value]);
                } else {
                    call_user_func_array($update_value_fn, [$meta->type, $meta->id, unpapify($child_key), $child_value]);
                }
            }
        }
    }
    return $result;
}
Ejemplo n.º 6
0
 /**
  * Update value before it's saved to the database.
  *
  * @param mixed  $values
  * @param string $repeater_slug
  * @param int    $post_id
  *
  * @return array
  */
 public function update_value($values, $repeater_slug, $post_id)
 {
     $rows = intval(papi_get_property_meta_value($post_id, $repeater_slug));
     if (!is_array($values)) {
         $values = [];
     }
     list($results, $trash) = $this->get_results($rows, $repeater_slug, $post_id);
     // Delete trash values.
     foreach ($trash as $meta) {
         papi_delete_property_meta_value($post_id, $meta->meta_key);
     }
     $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 real property slug
         $property_slug = $this->get_child_slug($repeater_slug, $slug);
         // Get property type
         $property_type_value = $values[$property_type_slug]->type;
         $property_type = papi_get_property_type($property_type_value);
         // Unserialize if needed.
         $value = papi_maybe_json_decode(maybe_unserialize($value));
         // Run update value on each property type class.
         $value = $property_type->update_value($value, $property_slug, $post_id);
         // Run update value on each property type filter.
         $values[$slug] = papi_filter_update_value($property_type_value, $value, $property_slug, $post_id, papi_get_meta_type());
         if (is_array($values[$slug])) {
             foreach ($values[$slug] as $key => $val) {
                 if (!is_string($key)) {
                     continue;
                 }
                 unset($values[$slug][$key]);
                 $key = preg_replace('/^\\_/', '', $key);
                 $values[$slug][$key] = $val;
             }
         }
     }
     // Find out which keys that should be deleted.
     $trash = array_diff(array_keys(papi_to_array($results)), array_keys(papi_to_array($values)));
     // Delete unwanted (trash) values.
     foreach (array_keys($trash) as $trash_key) {
         papi_delete_property_meta_value($post_id, $trash_key);
     }
     // It's safe to remove all rows in the database here.
     $this->remove_repeater_rows($post_id, $repeater_slug);
     // Remove unnecessary property type keys if any is left.
     foreach (array_keys($values) as $slug) {
         if (papi_is_property_type_key($slug)) {
             unset($values[$slug]);
         }
     }
     return $values;
 }
Ejemplo n.º 7
0
/**
 * Update property values on the post with the given post id
 * or update property values on the option page.
 *
 * @param  array $meta
 *
 * @return bool
 */
function papi_update_property_meta_value(array $meta = [])
{
    $meta = array_merge(['post_id' => 0, 'slug' => '', 'type' => Papi_Post_Page::TYPE, 'value' => ''], $meta);
    $meta = (object) $meta;
    $option = $meta->type === 'option' || papi_is_option_page();
    $save_value = true;
    foreach (papi_to_array($meta->value) as $key => $value) {
        if (is_string($key)) {
            $save_value = false;
            break;
        }
    }
    if (!$save_value && is_array($meta->value)) {
        $meta->value = [$meta->value];
    }
    if (papi_is_empty($meta->value)) {
        return papi_delete_property_meta_value($meta->post_id, $meta->slug, $meta->type);
    }
    $result = true;
    foreach (papi_to_array($meta->value) as $key => $value) {
        papi_cache_delete($meta->slug, $meta->post_id);
        if (!is_array($value)) {
            if ($save_value) {
                $value = $meta->value;
            }
            if ($option) {
                $out = update_option(unpapify($meta->slug), $value);
                $result = $out ? $result : $out;
            } else {
                $out = update_post_meta($meta->post_id, unpapify($meta->slug), $value);
                $result = $out ? $result : $out;
            }
            continue;
        }
        foreach ($value as $child_key => $child_value) {
            if (papi_is_empty($child_value)) {
                papi_delete_property_meta_value($meta->post_id, $child_key, $meta->type);
            } else {
                if ($option) {
                    update_option(unpapify($child_key), $child_value);
                } else {
                    update_post_meta($meta->post_id, unpapify($child_key), $child_value);
                }
            }
        }
    }
    return $result;
}