WP_CLI::error PHP Method

error() public static method

Error message is written to STDERR. Defaults to halting script execution with return code 1. Use WP_CLI::warning() instead when script execution should be permitted to continue. # wp cache flush considers flush failure to be a fatal error. if ( false === wp_cache_flush() ) { WP_CLI::error( 'The object cache could not be flushed.' ); }
public static error ( string | WP_Error $message, boolean | integer $exit = true ) : null
$message string | WP_Error Message to write to STDERR.
$exit boolean | integer True defaults to exit(1).
return null
    public static function error($message, $exit = true)
    {
        if (!isset(self::get_runner()->assoc_args['completions'])) {
            self::$logger->error(self::error_to_string($message));
        }
        $return_code = false;
        if (true === $exit) {
            $return_code = 1;
        } elseif (is_int($exit) && $exit >= 1) {
            $return_code = $exit;
        }
        if ($return_code) {
            if (self::$capture_exit) {
                throw new ExitException(null, $return_code);
            }
            exit($return_code);
        }
    }

Usage Example

Esempio n. 1
0
 /**
  * Forces a full Varnish Purge of the entire site (provided
  * regex is supported).
  * 
  * ## EXAMPLES
  * 
  *		wp varnish purge
  *
  *		wp varnish purge http://example.com/wp-content/themes/twentyeleventy/style.css
  *
  *		wp vanrish purge "/wp-content/themes/twentysixty/style.css"
  *
  *		wp varnish purge http://example.com/wp-content/themes/ --wildcard
  *
  *		wp varnish purge "/wp-content/themes/" --wildcard
  *
  */
 function purge($args, $assoc_args)
 {
     $wp_version = get_bloginfo('version');
     $cli_version = WP_CLI_VERSION;
     // Set the URL/path
     list($url) = $args;
     // If wildcard is set, or the URL argument is empty
     // then treat this as a full purge
     if (isset($assoc_args['wildcard']) || empty($url)) {
         $pregex = '/?vhp-regex';
         $wild = ".*";
     } else {
         $pregex = $wild = '';
     }
     wp_create_nonce('vhp-flush-cli');
     // Make sure the URL is a URL:
     if (!empty($url)) {
         // If the URL isn't a URL, make it a URL
         if (empty(esc_url($url))) {
             $url = $this->varnish_purge->the_home_url() . $url;
         }
     } else {
         $url = $this->varnish_purge->the_home_url();
     }
     if (version_compare($wp_version, '4.6', '>=') && (version_compare($cli_version, '0.25.0', '<') || version_compare($cli_version, '0.25.0-alpha', 'eq'))) {
         WP_CLI::log(sprintf('This plugin does not work on WP 4.6 and up, unless WP-CLI is version 0.25.0 or greater. You\'re using WP-CLI %s and WordPress %s.', $cli_version, $wp_version));
         WP_CLI::log('To flush your cache, please run the following command:');
         WP_CLI::log(sprintf('$ curl -X PURGE "%s"', $url . $wild));
         WP_CLI::error('Varnish Cache must be purged manually.');
     }
     $this->varnish_purge->purgeUrl($url . $pregex);
     WP_CLI::success('The Varnish cache was purged.');
 }
All Usage Examples Of WP_CLI::error