WP_CLI::read_value PHP Method

read_value() public static method

Read a value, from various formats.
public static read_value ( $raw_value, array $assoc_args = [] )
$assoc_args array
    public static function read_value($raw_value, $assoc_args = array())
    {
        if (\WP_CLI\Utils\get_flag_value($assoc_args, 'format') === 'json') {
            $value = json_decode($raw_value, true);
            if (null === $value) {
                WP_CLI::error(sprintf('Invalid JSON: %s', $raw_value));
            }
        } else {
            $value = $raw_value;
        }
        return $value;
    }

Usage Example

 /**
  * 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;
     }
 }
All Usage Examples Of WP_CLI::read_value