/** * 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; }
/** * 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; }
/** * Change value after it's loaded from the database * and populate every property in the flexible with the right property type. * * @param mixed $value * @param string $repeater_slug * @param int $post_id * * @return array */ public function load_value($value, $repeater_slug, $post_id) { if (is_array($value)) { return $value; } list($results, $trash) = $this->get_results($value, $repeater_slug, $post_id); // Will not need this array. unset($trash); $page = $this->get_page(); $results = papi_from_property_array_slugs($results, papi_remove_papi($repeater_slug)); if (is_null($page)) { return $this->default_value; } foreach ($results as $index => $row) { foreach ($row as $slug => $value) { if (papi_is_property_type_key($slug)) { continue; } if ($property = $page->get_property($repeater_slug, $slug)) { $type_key = papi_get_property_type_key_f($slug); $results[$index][$type_key] = $property; } } } return $results; }