] * : table or json. * --- * default: table * options: * - table * - json * --- * * ## EXAMPLES * * wp gorilla-cache status * * @param array $args Positional arguments. * @param array $assoc_args Options. */ public function status( $args, $assoc_args ) { GorillaCache_Installer::heal_schedules(); $state = GorillaCache_Installer::state(); $stats = GorillaCache_Purge::stats(); $preload = GorillaCache_Preload::state(); $rows = array( 'caching' => GorillaCache_Installer::is_caching( $state ) ? 'on' : ( 'on' === $state['verdict'] ? 'not working' : $state['verdict'] ), 'reason' => GorillaCache_Environment::message( $state ), 'note' => GorillaCache_Environment::note_message( (string) $state['note'] ), 'pages' => $stats['pages'] . ( $stats['complete'] ? '' : '+' ), 'disk' => size_format( $stats['bytes'] ) ? size_format( $stats['bytes'] ) : '0 B', 'lifespan' => (int) round( $state['lifespan'] / HOUR_IN_SECONDS ) . 'h', 'warming' => 'warming' === GorillaCache_Preload::summary() ? $preload['done'] . '/' . $preload['total'] : ( 'paused' === GorillaCache_Preload::summary() ? 'paused: ' . GorillaCache_Preload::blocked_message( $preload['blocked'] ) : GorillaCache_Preload::summary() ), 'not_stored' => self::not_stored(), 'wp_cron' => GorillaCache_Installer::cron_stalled_hours() ? 'not run for ' . GorillaCache_Installer::cron_stalled_hours() . ' hours' : 'ok', 'last_clear' => empty( $state['last_clear']['time'] ) ? '' : human_time_diff( (int) $state['last_clear']['time'] ) . ' ago (' . GorillaCache_Purge::cause_label( (string) $state['last_clear']['cause'] ) . ')', 'stores' => implode( ', ', GorillaCache_Registry::detected_names() ), ); if ( isset( $assoc_args['format'] ) && 'json' === $assoc_args['format'] ) { WP_CLI::line( (string) wp_json_encode( $rows ) ); return; } $table = array(); foreach ( $rows as $field => $value ) { $table[] = array( 'Field' => $field, 'Value' => $value, ); } WP_CLI\Utils\format_items( 'table', $table, array( 'Field', 'Value' ) ); } /** * Clears the whole cache, or one address. * * ## OPTIONS * * [
] * : Clear only this address on the site. (WP-CLI keeps --url for itself, so the address is given plainly.) * * ## EXAMPLES * * wp gorilla-cache clear * wp gorilla-cache clear https://example.com/shop/ * * @param array $args Positional arguments. * @param array $assoc_args Options. */ public function clear( $args, $assoc_args ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter -- WP-CLI passes both to every command. self::warn_other_user(); if ( ! GorillaCache_Installer::is_caching() ) { WP_CLI::warning( 'Caching is off on this site; there is nothing to clear.' ); return; } if ( ! empty( $args[0] ) ) { $url = sanitize_url( (string) $args[0] ); if ( wp_parse_url( $url, PHP_URL_HOST ) !== wp_parse_url( home_url(), PHP_URL_HOST ) ) { WP_CLI::error( 'That address is not on this site.' ); } GorillaCache_Purge::url( $url ); GorillaCache_Purge::flush(); WP_CLI::success( 'Cleared ' . $url ); return; } GorillaCache_Purge::all( 'cleared from WP-CLI' ); GorillaCache_Purge::flush(); $last = (array) GorillaCache_Installer::state()['last_clear']; $cf = isset( $last['cloudflare'] ) ? (string) $last['cloudflare'] : ''; WP_CLI::success( 'Cache cleared.' . ( 'requested' === $cf ? ' Cloudflare purge requested.' : '' ) . ( 'not_on_copy' === $cf ? ' Cloudflare is not purged from this copy: it shares the live site\'s zone.' : '' ) . ' It fills again as visitors arrive and as the cache warms.' ); } /** * Warms the cache: in the background, or now with --now. * * ## OPTIONS * * [--now] * : Visit every address now, one at a time, instead of in the background. * * ## EXAMPLES * * wp gorilla-cache preload * wp gorilla-cache preload --now * * @param array $args Positional arguments. * @param array $assoc_args Options. */ public function preload( $args, $assoc_args ) { self::warn_other_user(); if ( ! GorillaCache_Preload::enabled() ) { WP_CLI::error( 'Warming is off: caching is off, or warming is switched off in the settings or wp-config.php.' ); } GorillaCache_Preload::start_full(); if ( empty( $assoc_args['now'] ) ) { WP_CLI::success( 'Warming started in the background.' ); return; } $runs = 0; do { GorillaCache_Preload::run(); $state = GorillaCache_Preload::state(); if ( $state['blocked'] ) { WP_CLI::error( 'Warming is paused: ' . GorillaCache_Preload::blocked_message( $state['blocked'] ) ); } WP_CLI::log( sprintf( 'Warmed %d of %d.', $state['done'], $state['total'] ) ); ++$runs; } while ( array() !== $state['queue'] && $runs < 500 ); if ( array() !== $state['queue'] ) { // Stopped by the safety limit: the background warm-up stays scheduled and finishes the rest. WP_CLI::warning( sprintf( '%d addresses are left; the background warm-up will finish them.', count( $state['queue'] ) ) ); return; } wp_clear_scheduled_hook( GorillaCache_Preload::HOOK ); WP_CLI::success( 'Warming done.' ); } /** * Explains whether an address is cached, and why. * * ## OPTIONS * *