Debug message is written to STDERR, and includes script execution time.
Helpful for optionally showing greater detail when needed. Used throughout
WP-CLI bootstrap process for easier debugging and profiling.
# Called in WP_CLI\Runner::set_wp_root().
private static function set_wp_root( $path ) {
define( 'ABSPATH', rtrim( $path, '/' ) . '/' );
WP_CLI::debug( 'ABSPATH defined: ' . ABSPATH );
$_SERVER['DOCUMENT_ROOT'] = realpath( $path );
}
# Debug details only appear when --debug is used.
# $ wp --debug
# [...]
# Debug: ABSPATH defined: /srv/www/wordpress-develop.dev/src/ (0.225s)
protected static function tgz($args, $assoc_args) { @(list($repo, $tag) = $args); $url = sprintf("https://bitbucket.org/%s/get/%s.tar.gz", $repo, $tag ?: 'master'); if (array_intersect_key($assoc_args, array('key' => TRUE, 'secret' => TRUE))) { WP_CLI::debug("Fetching {$url} via OAuth"); $tgz = self::fetch_tarball_via_oauth($assoc_args['key'], $assoc_args['secret'], $url); } else { WP_CLI::debug("Fetching {$url} via cURL"); $tgz = self::fetch_tarball($url); } WP_CLI::debug("Fetched {$tgz}"); WP_CLI::debug("Converting {$tgz} to zip"); $zip = self::tgz_to_zip($repo, $tgz); WP_CLI::debug("Converted {$tgz} to {$zip}"); return array($url, $tgz, $zip); }
protected static function tgz($args, $assoc_args) { @(list($repo, $tag) = $args); // Get the tarball URL for the latest (or specified release) $url = sprintf("https://api.github.com/repos/%s/releases/%s", $repo, $tag ? "tags/{$tag}" : 'latest'); WP_CLI::debug("Querying for releases: {$url}"); $url = self::tarball_url($url, @$assoc_args['token']); // If no releases are available fail-back to a commitish if ($url) { WP_CLI::log("Found release: {$url}"); } else { $url = sprintf("https://api.github.com/repos/%s/tarball/%s", $repo, $tag ?: 'master'); } WP_CLI::debug("Fetching {$url}"); $tgz = self::fetch_tarball($url, @$assoc_args['token']); WP_CLI::debug("Fetched {$tgz}"); WP_CLI::debug("Converting {$tgz} to zip"); $zip = self::tgz_to_zip($repo, $tgz); WP_CLI::debug("Converted {$tgz} to {$zip}"); return array($url, $tgz, $zip); }
/** * Register's all endpoints as commands once WP and WC have all loaded. */ public static function after_wp_load() { global $wp_rest_server; $wp_rest_server = new WP_REST_Server(); do_action('rest_api_init', $wp_rest_server); $request = new WP_REST_Request('GET', '/'); $request->set_param('context', 'help'); $response = $wp_rest_server->dispatch($request); $response_data = $response->get_data(); if (empty($response_data)) { return; } // Loop through all of our endpoints and register any valid WC endpoints. foreach ($response_data['routes'] as $route => $route_data) { // Only register WC endpoints if (substr($route, 0, 4) !== '/wc/') { continue; } // Only register endpoints with schemas if (empty($route_data['schema']['title'])) { WP_CLI::debug(sprintf(__('No schema title found for %s, skipping REST command registration.', 'woocommerce'), $route), 'wc'); continue; } // Ignore batch endpoints if ('batch' === $route_data['schema']['title']) { continue; } // Disable specific endpoints $route_pieces = explode('/', $route); $endpoint_piece = str_replace('/wc/' . $route_pieces[2] . '/', '', $route); if (in_array($endpoint_piece, self::$disabled_endpoints)) { continue; } self::register_route_commands(new WC_CLI_REST_Command($route_data['schema']['title'], $route, $route_data['schema']), $route, $route_data); } }
protected static function apply($cmd, $args, $assoc_args) { $op = array_shift($args); list($url, $tgz, $zip) = static::tgz($args, $assoc_args); WP_CLI::debug("Installing from {$zip}"); WP_CLI::run_command(array($cmd, $op, $zip), array('force' => 1)); WP_CLI::debug("Removing {$tgz}, {$zip}"); unlink($tgz); unlink($zip); }
/** * Set the context in which WP-CLI should be run */ public static function set_url($url) { WP_CLI::debug('Set URL: ' . $url); $url_parts = Utils\parse_url($url); self::set_url_params($url_parts); }
/** * Runs through the entirety of the WP bootstrap process */ private function load_wordpress_with_template() { global $wp_query; WP_CLI::get_runner()->load_wordpress(); // Set up the main WordPress query. wp(); $interpreted = array(); foreach ($wp_query as $key => $value) { if (0 === stripos($key, 'is_') && $value) { $interpreted[] = $key; } } WP_CLI::debug('Main WP_Query: ' . implode(', ', $interpreted), 'redis-debug'); define('WP_USE_THEMES', true); add_filter('template_include', function ($template) { $display_template = str_replace(dirname(get_template_directory()) . '/', '', $template); WP_CLI::debug("Theme template: {$display_template}", 'redis-debug'); return $template; }, 999); // Template is normally loaded in global scope, so we need to replicate foreach ($GLOBALS as $key => $value) { global ${$key}; } // Load the theme template. ob_start(); require_once ABSPATH . WPINC . '/template-loader.php'; ob_get_clean(); }
/** * @param string $message */ public function debug($message) { \WP_CLI::debug($message); }
/** * Allows us to log if we are using WP_CLI to fire this cron task * * @see http://wp-cli.org/docs/internal-api/#output * * @param string $type What kind of log is this * @param string $message message displayed * * @return void */ public function log($type = 'colorize', $message = '') { // Log on our Structure Tribe__Main::instance()->log()->log_debug($message, 'aggregator'); // Only go further if we have WP_CLI if (!class_exists('WP_CLI')) { return false; } switch ($type) { case 'error': WP_CLI::error($message); break; case 'warning': WP_CLI::warning($message); break; case 'success': WP_CLI::success($message); break; case 'debug': WP_CLI::debug($message, 'aggregator'); break; case 'colorize': default: WP_CLI::log(WP_CLI::colorize($message)); break; } }
/** * Do a REST Request * * @param string $method * */ private function do_request($method, $route, $assoc_args) { if (!defined('REST_REQUEST')) { define('REST_REQUEST', true); } $request = new WP_REST_Request($method, $route); if (in_array($method, array('POST', 'PUT'))) { $request->set_body_params($assoc_args); } else { foreach ($assoc_args as $key => $value) { $request->set_param($key, $value); } } if (defined('SAVEQUERIES') && SAVEQUERIES) { $original_queries = is_array($GLOBALS['wpdb']->queries) ? array_keys($GLOBALS['wpdb']->queries) : array(); } $response = rest_do_request($request); if (defined('SAVEQUERIES') && SAVEQUERIES) { $performed_queries = array(); foreach ((array) $GLOBALS['wpdb']->queries as $key => $query) { if (in_array($key, $original_queries)) { continue; } $performed_queries[] = $query; } usort($performed_queries, function ($a, $b) { if ($a[1] === $b[1]) { return 0; } return $a[1] > $b[1] ? -1 : 1; }); $query_count = count($performed_queries); $query_total_time = 0; foreach ($performed_queries as $query) { $query_total_time += $query[1]; } $slow_query_message = ''; if ($performed_queries && 'wc' === WP_CLI::get_config('debug')) { $slow_query_message .= '. Ordered by slowness, the queries are:' . PHP_EOL; foreach ($performed_queries as $i => $query) { $i++; $bits = explode(', ', $query[2]); $backtrace = implode(', ', array_slice($bits, 13)); $seconds = round($query[1], 6); $slow_query_message .= <<<EOT {$i}: - {$seconds} seconds - {$backtrace} - {$query[0]} EOT; $slow_query_message .= PHP_EOL; } } elseif ('wc' !== WP_CLI::get_config('debug')) { $slow_query_message = '. Use --debug=wc to see all queries.'; } $query_total_time = round($query_total_time, 6); WP_CLI::debug("wc command executed {$query_count} queries in {$query_total_time} seconds{$slow_query_message}", 'wc'); } if ($error = $response->as_error()) { WP_CLI::error($error); } return array($response->get_status(), $response->get_data(), $response->get_headers()); }