get_value_from_arg_or_stdin() public static method

Read value from a positional argument or from STDIN.
public static get_value_from_arg_or_stdin ( array $args, integer $index ) : string
$args array The list of positional arguments.
$index integer At which position to check for the value.
return string
 /**
  * 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);
     $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.");
     }
 }
Example #2
0
 /**
  * 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 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);
     $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.");
     }
 }
Example #4
0
 /**
  * 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}'.");
         }
     }
 }