/** * Update field with new value. The old value will be deleted. * * @param int $post_id * @param string $slug * @param mixed $value * @param string $type * * @return bool */ function papi_update_field($post_id = null, $slug = null, $value = null, $type = 'page') { if (!is_numeric($post_id) && is_string($post_id)) { $value = $slug; $slug = $post_id; $post_id = null; } if (!is_string($slug) || empty($slug)) { return false; } if (papi_is_empty($value)) { return papi_delete_field($post_id, $slug, $type); } $post_id = papi_get_post_id($post_id); if ($post_id === 0 && $type === Papi_Post_Page::TYPE) { return false; } $page = papi_get_page($post_id, $type); if (is_null($page)) { return false; } $property = $page->get_property($slug); if (!papi_is_property($property)) { return false; } papi_delete_field($post_id, $slug, $type); $value = $property->update_value($value, $slug, $post_id); $value = papi_filter_update_value($property->get_option('type'), $value, $slug, $post_id); return papi_update_property_meta_value(['type' => $type, 'post_id' => $post_id, 'slug' => $slug, 'value' => $value]); }
/** * Prepare properties data for saving. * * @param array $data * @param int $post_id * * @return array */ protected function prepare_properties_data(array $data = [], $post_id = 0) { // Since we are storing witch property it is in the $data array // we need to remove that and set the property type to the property // and make a array of the property type and the value. foreach ($data as $key => $value) { $property_type_key = papi_get_property_type_key(); if (strpos($key, $property_type_key) === false) { continue; } $property_key = str_replace($property_type_key, '', $key); // Check if value exists. if (isset($data[$property_key])) { $data[$property_key] = ['type' => $value, 'value' => $data[$property_key]]; } unset($data[$key]); } foreach ($data as $key => $item) { $property = papi_get_property_type($item['type']); unset($data[$key]); if (papi_is_property($property)) { // Run `update_value` method on the property class. $data[$key] = $property->update_value($item['value'], papi_remove_papi($key), $post_id); // Apply `update_value` filter so this can be changed from the theme for specified property type. $data[$key] = papi_filter_update_value($item['type']->type, $data[$key], papi_remove_papi($key), $post_id); if ($item['type']->overwrite) { $slug = papi_remove_papi($key); $this->overwrite[$slug] = $data[$key]; } } } return $data; }
/** * Prepare properties data for saving. * * @param array $data * @param int $post_id * * @return array */ protected function prepare_properties_data(array $data = [], $post_id = 0) { // Since we are storing witch property it is in the `$data` array // we need to remove that and set the property type to the property // and make a array of the property type and the value. foreach ($data as $key => $value) { if (papi_is_property_type_key($key)) { continue; } $property_type_key = papify(papi_get_property_type_key($key)); // Check if value exists. if (!isset($data[$key]) && !isset($data[$property_type_key])) { continue; } // Pair property value with property type object. $data[$key] = ['type' => $data[$property_type_key], 'value' => $value]; // Remove property type object since it's not needed anymore. unset($data[$property_type_key]); } foreach ($data as $key => $item) { if (papi_is_property_type_key($key)) { continue; } $property = papi_get_property_type($item['type']); unset($data[$key]); if (papi_is_property($property)) { // Run `update_value` method on the property class. $data[$key] = $property->update_value($item['value'], unpapify($key), $post_id); // Apply `update_value` filter so this can be changed from // the theme for specified property type. $data[$key] = papi_filter_update_value($item['type']->type, $data[$key], unpapify($key), $post_id, papi_get_meta_type()); if ($item['type']->overwrite) { $slug = unpapify($key); $this->overwrite[$slug] = $data[$key]; unset($data[$key]); } } } return $data; }
/** * 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; }
/** * 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]); }