public function setPostMeta($value, $key = null, $postId = null) { if ($key === null) { $key = $this->dataKey; } if ($postId === null) { $postId = $this->getPost()->ID; } /* * if it's a revision save it also to the revision */ if (wp_is_post_revision($postId)) { add_metadata('post', $postId, $key, $value); } return update_post_meta($postId, $key, $value); }
/** * Add meta data field to a term. * */ public static function add_meta($term_id, $meta_key, $meta_value, $unique = false) { if (current_theme_supports('extended-taxonomies')) { return add_post_meta(self::get_post_for_extended_term($term_id)->ID, $meta_key, $meta_value, $unique); } return add_metadata('taxonomy', $term_id, $meta_key, $meta_value, $unique); }
function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value = '') { if (!$meta_type || !$meta_key) { return false; } if (!($table = _get_meta_table($meta_type))) { return false; } global $wpdb; $column = esc_sql($meta_type . '_id'); $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; // expected_slashed ($meta_key) $meta_key = stripslashes($meta_key); if (!($meta_id = $wpdb->get_var($wpdb->prepare("SELECT {$id_column} FROM {$table} WHERE meta_key = %s AND {$column} = %d", $meta_key, $object_id)))) { return add_metadata($meta_type, $object_id, $meta_key, $meta_value); } $meta_value = maybe_serialize(stripslashes_deep($meta_value)); $data = compact('meta_value'); $where = array($column => $object_id, 'meta_key' => $meta_key); if (!empty($prev_value)) { $prev_value = maybe_serialize($prev_value); $where['meta_value'] = $prev_value; } do_action("update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $meta_value); $wpdb->update($table, $data, $where); wp_cache_delete($object_id, $meta_type . '_meta'); do_action("updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $meta_value); return true; }
function hocwp_term_add_meta($term_id, $meta_key, $meta_value, $unique = false) { $version = hocwp_get_wp_version(); if (version_compare($version, '4.4', '>=')) { return add_term_meta($term_id, $meta_key, $meta_value, $unique); } return add_metadata('term', $term_id, $meta_key, $meta_value, $unique); }
/** * @ticket 35795 */ public function test_slashed_key_for_existing_metadata() { global $wpdb; add_metadata('post', 123, wp_slash('foo\\foo'), 'bar'); update_metadata('post', 123, wp_slash('foo\\foo'), 'baz'); $found = get_metadata('post', 123, 'foo\\foo', true); $this->assertSame('baz', $found); }
public static function save_post($post_id, $post) { if ($parent_id = wp_is_post_revision($post_id) && !wp_is_post_autosave($post_id)) { $meta = piklist('post_custom', $parent_id); foreach ($meta as $key => $value) { add_metadata('post', $post_id, $key, maybe_serialize($value)); } } }
/** * @param $postId * @param $revisionId */ public function restoreRevision($postId, $revisionId) { $key = $this->getKey(); $revisionValues = get_post_meta($revisionId, $key, false); delete_metadata('post', $revisionId, $key); foreach ($revisionValues as $revisionValue) { add_metadata('post', $revisionId, $key, $revisionValue); } }
function create_post() { $post_data = array('post_content' => $_POST['content'], 'post_name' => $_POST['slug'], 'post_title' => $_POST['title'], 'post_status' => $_POST['post_status'], 'post_type' => $_POST['post_type']); if (array_key_exists('category', $_POST)) { $post_data['post_category'] = $_POST['category']; } $new_post_ID = wp_insert_post($post_data); add_metadata('post', $new_post_ID, 'discourse_order', $_POST['order']); wp_die(); }
/** * Add a meta field. * * @synopsis <id> <key> <value> */ public function add($args, $assoc_args) { list($object_id, $meta_key, $meta_value) = $args; $success = add_metadata($this->meta_type, $object_id, $meta_key, $meta_value); if ($success) { WP_CLI::success("Added custom field."); } else { WP_CLI::error("Failed to add custom field."); } }
function wpsc_meta_migrate($meta_object_type) { global $wpdb; $legacy_meta_table = $wpdb->prefix . 'wpsc_meta'; $sql = "SELECT meta_id, object_id, meta_key, meta_value FROM `{$legacy_meta_table}` WHERE object_type ='%s'"; $old_meta_rows = $wpdb->get_results($wpdb->prepare($sql, 'wpsc_' . $meta_object_type)); foreach ($old_meta_rows as $old_meta_row) { $meta_data = maybe_unserialize($old_meta_row->meta_value); add_metadata('wpsc_' . $meta_object_type, $old_meta_row->object_id, $old_meta_row->meta_key, $meta_data, false); } }
public function update_data($new_value, $single = true) { extract($this->data_args(array('new_value' => $new_value, 'single' => $single))); if ('options-page' === $type) { return rrze_Meta_Box::update_option($id, $field_id, $new_value, $single); } if (!$single) { return add_metadata($type, $id, $field_id, $new_value, false); } return update_metadata($type, $id, $field_id, $new_value); }
function easy_ads_update_section_meta($post_id, $field_name, $value = '') { if (empty($value) or !$value) { $doo = delete_metadata('easy_ads_taxnomy_sections_', $post_id, $field_name, $value); } elseif (!get_metadata('easy_ads_taxnomy_sections_', $post_id, $field_name)) { $doo = add_metadata('easy_ads_taxnomy_sections_', $post_id, $field_name, $value); } else { $doo = update_metadata('easy_ads_taxnomy_sections_', $post_id, $field_name, $value); } return $doo; }
/** * Store the Page Builder meta in the revision. * @param $post_id * @since 0.1.0 */ function pootlepb_revisions_save_post($post_id) { $parent_id = wp_is_post_revision($post_id); if ($parent_id) { // If the panels data meta exists, copy it into the revision. $panels_data = get_post_meta($parent_id, 'panels_data', true); if (!empty($panels_data)) { add_metadata('post', $post_id, 'panels_data', $panels_data); } } }
/** * Add a meta field. * * ## OPTIONS * * <id> * : The ID of the object. * * <key> * : The name of the meta field to create. * * [<value>] * : The value of the meta field. If ommited, the value is read from STDIN. * * [--format=<format>] * : The serialization format for the value. Default is plaintext. */ public function add($args, $assoc_args) { list($object_id, $meta_key) = $args; $meta_value = \Terminus::get_value_from_arg_or_stdin($args, 2); $meta_value = \Terminus::read_value($meta_value, $assoc_args); $success = \add_metadata($this->meta_type, $object_id, $meta_key, $meta_value); if ($success) { \Terminus::success("Added custom field."); } else { \Terminus::error("Failed to add custom field."); } }
/** * Update meta field for a user * * @param array $args * @param array $assoc_args **/ public function add($args, $assoc_args) { $object_id = WP_CLI::get_numeric_arg($args, 0, "{$this->meta_type} ID"); $meta_key = self::get_arg_or_error($args, 1, "meta_key"); $meta_value = self::get_arg_or_error($args, 2, "meta_value"); $success = add_metadata($this->meta_type, $object_id, $meta_key, $meta_value); if ($success) { WP_CLI::success("Added custom field."); } else { WP_CLI::error("Failed to add custom field."); } }
public function saveRevision($post_id, $post) { $parent_id = wp_is_post_revision($post_id); if ($parent_id) { $parent = get_post($parent_id); foreach ($this->post_meta as $name) { $meta = get_post_meta($parent->ID, $name, true); if (false !== $meta) { add_metadata('post', $post_id, $name, $meta); } } } }
/** * @param WP_Post $post * @param WP_Post $revision */ public function persist(WP_Post $post, WP_Post $revision) { $key = $this->getKey(); $values = $_POST[$key]; if (is_array($values)) { delete_metadata('post', $revision->ID, $key); foreach ($values as $value) { add_metadata('post', $revision->ID, $key, $value); } } else { if (empty($values)) { delete_post_meta($revision->ID, $key); } else { update_metadata('post', $revision->ID, $key, $values); } } }
/** * @group wp_restore_post_revision */ public function test_wp_restore_post_revision() { // 投稿のメタデータ add_post_meta($this->post_id, 'text', 'text'); add_post_meta($this->post_id, 'checkbox', 'check'); add_post_meta($this->post_id, 'text3', 'loop-text'); // リビジョンのメタデータ add_metadata('post', $this->revision_id, 'text', 'text-2'); add_metadata('post', $this->revision_id, SCF_Config::PREFIX . 'repeat-multiple-data', array('checkbox3' => array(1, 2))); add_metadata('post', $this->revision_id, 'checkbox3', 'loop-check-1'); add_metadata('post', $this->revision_id, 'checkbox3', 'loop-check-2'); add_metadata('post', $this->revision_id, 'checkbox3', 'loop-check-3'); $Revision = new Smart_Custom_Fields_Revisions(); $Revision->wp_restore_post_revision($this->post_id, $this->revision_id); $this->assertEquals('text-2', SCF::get('text', $this->post_id)); $this->assertSame(array(), SCF::get('checkbox', $this->post_id)); $this->assertEquals(array(array('loop-check-1'), array('loop-check-2', 'loop-check-3')), SCF::get('checkbox3', $this->post_id)); }
/** * Add meta data field to a user. * * Post meta data is called "Custom Fields" on the Administration Panels. * * @since 3.0.0 * @uses add_metadata() * @link http://codex.wordpress.org/Function_Reference/add_user_meta * * @param int $user_id Post ID. * @param string $key Metadata name. * @param mixed $value Metadata value. * @param bool $unique Optional, default is false. Whether the same key should not be added. * @return bool False for failure. True for success. */ function add_user_meta($user_id, $meta_key, $meta_value, $unique = false) { return add_metadata('user', $user_id, $meta_key, $meta_value, $unique); }
/** * Add a piece of group metadata. * * @since 2.0.0 * * @param int $group_id ID of the group. * @param string $meta_key Metadata key. * @param mixed $meta_value Metadata value. * @param bool $unique Optional. Whether to enforce a single metadata value * for the given key. If true, and the object already * has a value for the key, no change will be made. * Default: false. * * @return int|bool The meta ID on successful update, false on failure. */ function groups_add_groupmeta($group_id, $meta_key, $meta_value, $unique = false) { add_filter('query', 'bp_filter_metaid_column_name'); $retval = add_metadata('group', $group_id, $meta_key, $meta_value, $unique); remove_filter('query', 'bp_filter_metaid_column_name'); return $retval; }
/** * Save the field value(s) into the database. * * @param Field $field The field to save. */ public function save(Field $field) { if (!update_metadata($this->get_meta_type(), $this->get_id(), $this->get_field_name($field), $field->get_value())) { add_metadata($this->get_meta_type(), $this->get_id(), $this->get_field_name($field), $field->get_value(), true); } }
/** * WooCommerce Term Meta API * * WC tables for storing term meta are @deprecated from WordPress 4.4 since 4.4 has its own table. * This function serves as a wrapper, using the new table if present, or falling back to the WC table. * * @todo These functions should be deprecated with notices in a future WC version, allowing users a chance to upgrade WordPress. * @param mixed $term_id * @param mixed $meta_key * @param mixed $meta_value * @param bool $unique (default: false) * @return bool */ function add_woocommerce_term_meta($term_id, $meta_key, $meta_value, $unique = false) { return function_exists('add_term_meta') ? add_term_meta($term_id, $meta_key, $meta_value, $unique) : add_metadata('woocommerce_term', $term_id, $meta_key, $meta_value, $unique); }
/** * メタデータを追加 * * @param string $key メタキー * @param mixed $value 保存する値 * @param bool $unique キーをユニークにするかどうか * @return int|false Meta ID */ public function add($key, $value, $unique = false) { $return = false; do_action(SCF_Config::PREFIX . '-before-save-' . $this->meta_type, $this->id, $key, $value); $is_valid = apply_filters(SCF_Config::PREFIX . '-validate-save-' . $this->meta_type, $this->id, $key, $value); if ($is_valid) { if (_get_meta_table($this->meta_type)) { $return = add_metadata($this->meta_type, $this->id, $key, $value, $unique); } else { $option_name = $this->get_option_name(); $option = get_option($option_name); if (!$unique || !isset($option[$key])) { $option[$key][] = $value; $option = stripslashes_deep($option); $return = update_option($option_name, $option, false); } } } do_action(SCF_Config::PREFIX . '-after-save-' . $this->meta_type, $this->id, $key, $value); return $return; }
/** * Adds metadata to a term. * * @since 4.4.0 * * @param int $term_id Term ID. * @param string $meta_key Metadata name. * @param mixed $meta_value Metadata value. * @param bool $unique Optional. Whether to bail if an entry with the same key is found for the term. * Default false. * @return int|bool Meta ID on success, false on failure. */ function add_term_meta($term_id, $meta_key, $meta_value, $unique = false) { // Bail if term meta table is not installed. if (get_option('db_version') < 34370) { return false; } $added = add_metadata('term', $term_id, $meta_key, $meta_value, $unique); // Bust term query cache. if ($added) { wp_cache_set('last_changed', microtime(), 'terms'); } return $added; }
/** * Adds metadata to a term. * * @since 4.4.0 * * @param int $term_id Term ID. * @param string $meta_key Metadata name. * @param mixed $meta_value Metadata value. * @param bool $unique Optional. Whether to bail if an entry with the same key is found for the term. * Default false. * @return int|WP_Error|bool Meta ID on success. WP_Error when term_id is ambiguous between taxonomies. * False on failure. */ function add_term_meta($term_id, $meta_key, $meta_value, $unique = false) { // Bail if term meta table is not installed. if (get_option('db_version') < 34370) { return false; } if (wp_term_is_shared($term_id)) { return new WP_Error('ambiguous_term_id', __('Term meta cannot be added to terms that are shared between taxonomies.'), $term_id); } $added = add_metadata('term', $term_id, $meta_key, $meta_value, $unique); // Bust term query cache. if ($added) { wp_cache_set('last_changed', microtime(), 'terms'); } return $added; }
static function update_meta($fields, $data, $object_id, $meta_type = 'post') { foreach ($fields as $field_args) { $key = $field_args['name']; if ('checkbox' == $field_args['type']) { $new_values = isset($data[$key]) ? $data[$key] : array(); $old_values = get_metadata($meta_type, $object_id, $key); foreach (array_diff($new_values, $old_values) as $value) { add_metadata($meta_type, $object_id, $key, $value); } foreach (array_diff($old_values, $new_values) as $value) { delete_metadata($meta_type, $object_id, $key, $value); } } else { $value = isset($data[$key]) ? $data[$key] : ''; if ('' === $value) { delete_metadata($meta_type, $object_id, $key); } else { update_metadata($meta_type, $object_id, $key, $value); } } } }
/** * Add meta data field to a comment. * * @since 2.9.0 * @link https://codex.wordpress.org/Function_Reference/add_comment_meta * * @param int $comment_id Comment ID. * @param string $meta_key Metadata name. * @param mixed $meta_value Metadata value. * @param bool $unique Optional, default is false. Whether the same key should not be added. * @return int|bool Meta ID on success, false on failure. */ function add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false) { return add_metadata('comment', $comment_id, $meta_key, $meta_value, $unique); }
/** * Update metadata for the specified object. If no value already exists for the specified object * ID and metadata key, the metadata will be added. * * @since 2.9.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) * @param int $object_id ID of the object metadata is for * @param string $meta_key Metadata key * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. * @param mixed $prev_value Optional. If specified, only update existing metadata entries with * the specified value. Otherwise, update all entries. * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. */ function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value = '') { global $wpdb; if (!$meta_type || !$meta_key || !is_numeric($object_id)) { return false; } $object_id = absint($object_id); if (!$object_id) { return false; } $table = _get_meta_table($meta_type); if (!$table) { return false; } $column = sanitize_key($meta_type . '_id'); $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; // expected_slashed ($meta_key) $meta_key = wp_unslash($meta_key); $passed_value = $meta_value; $meta_value = wp_unslash($meta_value); $meta_value = sanitize_meta($meta_key, $meta_value, $meta_type); /** * Filter whether to update metadata of a specific type. * * The dynamic portion of the hook, `$meta_type`, refers to the meta * object type (comment, post, or user). Returning a non-null value * will effectively short-circuit the function. * * @since 3.1.0 * * @param null|bool $check Whether to allow updating metadata for the given type. * @param int $object_id Object ID. * @param string $meta_key Meta key. * @param mixed $meta_value Meta value. Must be serializable if non-scalar. * @param mixed $prev_value Optional. If specified, only update existing * metadata entries with the specified value. * Otherwise, update all entries. */ $check = apply_filters("update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value); if (null !== $check) { return (bool) $check; } // Compare existing value to new value if no prev value given and the key exists only once. if (empty($prev_value)) { $old_value = get_metadata($meta_type, $object_id, $meta_key); if (count($old_value) == 1) { if ($old_value[0] === $meta_value) { return false; } } } $meta_ids = $wpdb->get_col($wpdb->prepare("SELECT {$id_column} FROM {$table} WHERE meta_key = %s AND {$column} = %d", $meta_key, $object_id)); if (empty($meta_ids)) { return add_metadata($meta_type, $object_id, $meta_key, $passed_value); } $_meta_value = $meta_value; $meta_value = maybe_serialize($meta_value); $data = compact('meta_value'); $where = array($column => $object_id, 'meta_key' => $meta_key); if (!empty($prev_value)) { $prev_value = maybe_serialize($prev_value); $where['meta_value'] = $prev_value; } foreach ($meta_ids as $meta_id) { /** * Fires immediately before updating metadata of a specific type. * * The dynamic portion of the hook, `$meta_type`, refers to the meta * object type (comment, post, or user). * * @since 2.9.0 * * @param int $meta_id ID of the metadata entry to update. * @param int $object_id Object ID. * @param string $meta_key Meta key. * @param mixed $meta_value Meta value. */ do_action("update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value); } if ('post' == $meta_type) { foreach ($meta_ids as $meta_id) { /** * Fires immediately before updating a post's metadata. * * @since 2.9.0 * * @param int $meta_id ID of metadata entry to update. * @param int $object_id Object ID. * @param string $meta_key Meta key. * @param mixed $meta_value Meta value. */ do_action('update_postmeta', $meta_id, $object_id, $meta_key, $meta_value); } } $result = $wpdb->update($table, $data, $where); if (!$result) { return false; } wp_cache_delete($object_id, $meta_type . '_meta'); foreach ($meta_ids as $meta_id) { /** * Fires immediately after updating metadata of a specific type. * * The dynamic portion of the hook, `$meta_type`, refers to the meta * object type (comment, post, or user). * * @since 2.9.0 * * @param int $meta_id ID of updated metadata entry. * @param int $object_id Object ID. * @param string $meta_key Meta key. * @param mixed $meta_value Meta value. */ do_action("updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value); } if ('post' == $meta_type) { foreach ($meta_ids as $meta_id) { /** * Fires immediately after updating a post's metadata. * * @since 2.9.0 * * @param int $meta_id ID of updated metadata entry. * @param int $object_id Object ID. * @param string $meta_key Meta key. * @param mixed $meta_value Meta value. */ do_action('updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value); } } return true; }
/** * Add meta data field to a post. * * Post meta data is called "Custom Fields" on the Administration Panels. * * @since 1.5.0 * @uses $wpdb * @link http://codex.wordpress.org/Function_Reference/add_post_meta * * @param int $post_id Post ID. * @param string $meta_key Metadata name. * @param mixed $meta_value Metadata value. * @param bool $unique Optional, default is false. Whether the same key should not be added. * @return bool False for failure. True for success. */ function add_post_meta($post_id, $meta_key, $meta_value, $unique = false) { // make sure meta is added to the post, not a revision if ($the_post = wp_is_post_revision($post_id)) { $post_id = $the_post; } return add_metadata('post', $post_id, $meta_key, $meta_value, $unique); }
/** * Function to save fusion builder content revisions * * @since 2.0.0 * * @return NULL * * @Param Post ID */ public function save_fusion_revisions_with_post($post_id) { if (isset($_POST['fusion_builder_status']) && $_POST['fusion_builder_status']) { update_post_meta($post_id, 'fusion_builder_status', $_POST['fusion_builder_status']); } $parent_id = wp_is_post_revision($post_id); if ($parent_id) { $parent = get_post($parent_id); $FB_content = get_post_meta($parent->ID, 'fusion_builder_content', true); if (false !== $FB_content) { add_metadata('post', $post_id, 'FB_content', $FB_content); } } }