/** * Delete value in the database. * * @param int $post_id * @param string $slug * @param string $type * * @return bool */ function papi_delete_field($post_id = null, $slug = null, $type = 'page') { if (!is_numeric($post_id) && is_string($post_id)) { $slug = $post_id; $post_id = null; } if (!is_string($slug) || empty($slug)) { return false; } $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_cache_delete($slug, $post_id); papi_action_delete_value($type, $slug, $post_id); return $property->delete_value($slug, $post_id, $type); }
/** * Overwrite post data in `posts` table. * * @param int $post_id */ protected function overwrite_post_data($post_id) { global $wpdb; if (empty($post_id) || empty($this->overwrite)) { return; } $wpdb->update($wpdb->posts, $this->overwrite, ['ID' => $post_id]); // Delete cache since it can be cached even if not saved in the database. foreach (array_keys($this->overwrite) as $key) { papi_cache_delete($key, $post_id); } }
/** * Delete value in the database. * * @param int $id * @param string $slug * @param string $type * * @return bool */ function papi_delete_field($id = null, $slug = null, $type = 'post') { if (!is_numeric($id) && is_string($id)) { $type = empty($slug) ? $type : $slug; $slug = $id; $id = null; } if (!is_string($slug) || empty($slug)) { return false; } $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_cache_delete($slug, $id, $type); papi_action_delete_value($type, $slug, $id); return $property->delete_value($slug, $id, $type); }
/** * Delete cache values on update property meta. * * @param object $meta * @param array $value */ function papi_update_property_meta_value_cache_delete($meta, $value) { $value = is_array($value) ? $value : []; foreach ($value as $child_key => $child_value) { papi_cache_delete($child_key, $meta->id, $meta->type); if (is_array($child_value)) { papi_update_property_meta_value_cache_delete($meta, $child_value); } } }
/** * 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 = (object) $meta; $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 (!isset($meta->post_id)) { $meta->post_id = 0; } if (!$save_value && is_array($meta->value)) { $meta->value = [$meta->value]; } if (papi_is_empty($meta->value)) { papi_cache_delete($meta->slug, $meta->post_id); if ($option) { return delete_option(papi_remove_papi($meta->slug)); } else { return delete_post_meta($meta->post_id, papi_remove_papi($meta->slug)); } } $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(papi_remove_papi($meta->slug), $value); $result = $out ? $result : $out; } else { $out = update_post_meta($meta->post_id, papi_remove_papi($meta->slug), $value); $result = $out ? $result : $out; } continue; } foreach ($value as $child_key => $child_value) { if (papi_is_empty($child_value)) { if ($option) { delete_option(papi_remove_papi($child_key)); } else { delete_post_meta($meta->post_id, papi_remove_papi($child_key)); } } else { if ($option) { update_option(papi_remove_papi($child_key), $child_value); } else { update_post_meta($meta->post_id, papi_remove_papi($child_key), $child_value); } } } } return $result; }
/** * 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; }
/** * 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; }
/** * 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; }