WP_CLI::error_to_string PHP Method

error_to_string() public static method

Convert a wp_error into a string
public static error_to_string ( mixed $errors ) : string
$errors mixed
return string
    public static function error_to_string($errors)
    {
        if (is_string($errors)) {
            return $errors;
        }
        if (is_object($errors) && is_a($errors, 'WP_Error')) {
            foreach ($errors->get_error_messages() as $message) {
                if ($errors->get_error_data()) {
                    return $message . ' ' . json_encode($errors->get_error_data());
                } else {
                    return $message;
                }
            }
        }
    }

Usage Example

Example #1
0
 /**
  * Update WordPress.
  *
  * ## OPTIONS
  *
  * [<zip>]
  * : Path to zip file to use, instead of downloading from wordpress.org.
  *
  * [--version=<version>]
  * : Update to this version, instead of to the latest version.
  *
  * [--force]
  * : Update even when installed WP version is greater than the requested version.
  *
  * [--locale=<locale>]
  * : Select which language you want to download.
  *
  * ## EXAMPLES
  *
  *     wp core update
  *
  *     wp core update --version=3.8 ../latest.zip
  *
  *     wp core update --version=3.1 --force
  *
  * @alias upgrade
  */
 function update($args, $assoc_args)
 {
     global $wp_version;
     $update = $from_api = null;
     $upgrader = 'WP_CLI\\CoreUpgrader';
     if (!empty($args[0])) {
         $upgrader = 'WP_CLI\\NonDestructiveCoreUpgrader';
         $version = !empty($assoc_args['version']) ? $assoc_args['version'] : null;
         $update = (object) array('response' => 'upgrade', 'current' => $version, 'download' => $args[0], 'packages' => (object) array('partial' => null, 'new_bundled' => null, 'no_content' => null, 'full' => $args[0]), 'version' => $version, 'locale' => null);
     } else {
         if (empty($assoc_args['version'])) {
             wp_version_check();
             $from_api = get_site_transient('update_core');
             if (empty($from_api->updates)) {
                 $update = false;
             } else {
                 list($update) = $from_api->updates;
             }
         } else {
             if (version_compare($wp_version, $assoc_args['version'], '<') || isset($assoc_args['force'])) {
                 $version = $assoc_args['version'];
                 $locale = isset($assoc_args['locale']) ? $assoc_args['locale'] : get_locale();
                 $new_package = $this->get_download_url($version, $locale);
                 $update = (object) array('response' => 'upgrade', 'current' => $assoc_args['version'], 'download' => $new_package, 'packages' => (object) array('partial' => null, 'new_bundled' => null, 'no_content' => null, 'full' => $new_package), 'version' => $version, 'locale' => $locale);
             } else {
                 WP_CLI::success('WordPress is up to date.');
                 return;
             }
         }
     }
     require_once ABSPATH . 'wp-admin/includes/upgrade.php';
     if ($update->version) {
         WP_CLI::log("Updating to version {$update->version} ({$update->locale})...");
     } else {
         WP_CLI::log("Starting update...");
     }
     $GLOBALS['wp_cli_update_obj'] = $update;
     $result = Utils\get_upgrader($upgrader)->upgrade($update);
     unset($GLOBALS['wp_cli_update_obj']);
     if (is_wp_error($result)) {
         $msg = WP_CLI::error_to_string($result);
         if ('up_to_date' != $result->get_error_code()) {
             WP_CLI::error($msg);
         } else {
             WP_CLI::success($msg);
         }
     } else {
         WP_CLI::success('WordPress updated successfully.');
     }
 }
All Usage Examples Of WP_CLI::error_to_string