/** * Perform an option action on a remote site */ private function perform_option_action($action, $args, $assoc_args) { $site_id = $assoc_args['site-id']; unset($assoc_args['site-id']); list($option_name) = $args; $this->set_account(); $method = strtoupper($action); if ('update' == $action) { $method = 'POST'; $api_args = array('option_value' => WP_CLI::read_value($args[1], $assoc_args)); } else { $api_args = array(); } $args = array('endpoint' => 'site/' . (int) $site_id . '/option/' . $option_name, 'method' => $method, 'body' => $api_args); $response = $this->api_request($args); if (is_wp_error($response)) { WP_CLI::error($response->get_error_message()); } switch ($action) { case 'get': if (empty($response)) { die(1); } WP_CLI::print_value($response, $assoc_args); break; case 'update': WP_CLI::success("Updated '{$option_name}' option."); break; case 'delete': WP_CLI::success("Deleted '{$option_name}' option."); break; } }
/** * Update an option * * @alias set * @synopsis <key> <value> [--json] */ public function update($args, $assoc_args) { $key = $args[0]; $value = WP_CLI::read_value($args[1], $assoc_args); if ($value === get_option($key)) { return; } if (!update_option($key, $value)) { WP_CLI::error("Could not update option '{$key}'."); } }
/** * Update a meta field. * * @alias set * @synopsis <id> <key> <value> [--format=<format>] */ public function update($args, $assoc_args) { list($object_id, $meta_key) = $args; $meta_value = \WP_CLI::read_value($args[2], $assoc_args); $success = \update_metadata($this->meta_type, $object_id, $meta_key, $meta_value); if ($success) { \WP_CLI::success("Updated custom field."); } else { \WP_CLI::error("Failed to update custom field."); } }
/** * Update an option. * * @alias set * @synopsis <key> <value> [--format=<format>] */ public function update($args, $assoc_args) { $key = $args[0]; $value = WP_CLI::read_value($args[1], $assoc_args); $result = update_option($key, $value); // update_option() returns false if the value is the same if (!$result && $value != get_option($key)) { WP_CLI::error("Could not update option '{$key}'."); } else { WP_CLI::success("Updated '{$key}' option."); } }
/** * Update an option * * @param array $args **/ public function update($args, $assoc_args) { if (count($args) < 2) { WP_CLI::line("usage: wp option update <option-name> <option-value>"); exit; } $key = $args[0]; $value = WP_CLI::read_value($args[1], $assoc_args); if ($value === get_option($key)) { return; } if (!update_option($key, $value)) { WP_CLI::error("Could not update option '{$key}'."); } }
/** * Update an option. * * ## OPTIONS * * <key> * : The name of the option to add. * * [<value>] * : The new value. If ommited, the value is read from STDIN. * * [--autoload=<autoload>] * : Requires WP 4.2. Should this option be automatically loaded. Accepted values: yes, no. Default: yes * * [--format=<format>] * : The serialization format for the value. Default is plaintext. * * ## EXAMPLES * * # Update an option by reading from a file * wp option update my_option < value.txt * * # Update one option on multiple sites using xargs * wp site list --field=url | xargs -n1 -I {} sh -c 'wp --url={} option update <key> <value>' * * @alias set */ public function update($args, $assoc_args) { $key = $args[0]; $value = WP_CLI::get_value_from_arg_or_stdin($args, 1); $value = WP_CLI::read_value($value, $assoc_args); $autoload = \WP_CLI\Utils\get_flag_value($assoc_args, 'autoload'); if (!in_array($autoload, array('yes', 'no'))) { $autoload = null; } $value = sanitize_option($key, $value); $old_value = sanitize_option($key, get_option($key)); if ($value === $old_value && is_null($autoload)) { WP_CLI::success("Value passed for '{$key}' option is unchanged."); } else { if (update_option($key, $value, $autoload)) { WP_CLI::success("Updated '{$key}' option."); } else { WP_CLI::error("Could not update option '{$key}'."); } } }
/** * Update an option. * * ## OPTIONS * * <key> * : The name of the option to add. * * [<value>] * : The new value. If ommited, the value is read from STDIN. * * [--format=<format>] * : The serialization format for the value. Default is plaintext. * * ## EXAMPLES * * # Update an option by reading from a file * wp option update my_option < value.txt * * # Update one option on multiple sites using xargs * wp site list --field=url | xargs -n1 -I {} sh -c 'wp --url={} option update <key> <value>' * * @alias set */ public function update($args, $assoc_args) { $key = $args[0]; $value = WP_CLI::get_value_from_arg_or_stdin($args, 1); $value = WP_CLI::read_value($value, $assoc_args); $value = sanitize_option($key, $value); $old_value = sanitize_option($key, get_option($key)); if ($value === $old_value) { WP_CLI::success("Value passed for '{$key}' option is unchanged."); } else { if (update_option($key, $value)) { WP_CLI::success("Updated '{$key}' option."); } else { WP_CLI::error("Could not update option '{$key}'."); } } }
/** * Update a meta field. * * ## OPTIONS * * <id> * : The ID of the object. * * <key> * : The name of the meta field to update. * * [<value>] * : The new value. If ommited, the value is read from STDIN. * * [--format=<format>] * : The serialization format for the value. Default is plaintext. * * @alias set */ public function update($args, $assoc_args) { list($object_id, $meta_key) = $args; $meta_value = \WP_CLI::get_value_from_arg_or_stdin($args, 2); $meta_value = \WP_CLI::read_value($meta_value, $assoc_args); $object_id = $this->check_object_id($object_id); $meta_value = sanitize_meta($meta_key, $meta_value, $this->meta_type); $old_value = sanitize_meta($meta_key, get_metadata($this->meta_type, $object_id, $meta_key, true), $this->meta_type); if ($meta_value === $old_value) { \WP_CLI::success("Value passed for custom field '{$meta_key}' is unchanged."); } else { $success = \update_metadata($this->meta_type, $object_id, $meta_key, $meta_value); if ($success) { \WP_CLI::success("Updated custom field '{$meta_key}'."); } else { \WP_CLI::error("Failed to update custom field '{$meta_key}'."); } } }