*/ defined( 'ABSPATH' ) || exit; if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { return; } /** * Manages Adaptive Speed from the command line. */ class CTPS_CLI { /** * Shows the plugin status. * * ## EXAMPLES * * wp ctps status */ public function status() { $last = get_option( CTPS_PSI::LAST, array() ); $health = get_option( CTPS_Safety::HEALTH ); $rum = CTPS_RUM::summary( 0 ); $profile = CTPS_Settings::current_profile(); list( $webp_done, $webp_total ) = CTPS_Webp::stats(); $rows = array( array( 'item' => __( 'Version', 'celero-adaptive-speed' ), 'value' => CTPS_VERSION ), /* translators: %s: reason code (constant, file or emergency) */ array( 'item' => __( 'Status', 'celero-adaptive-speed' ), 'value' => CTPS_Safety::is_disabled() ? sprintf( __( 'emergency mode (%s)', 'celero-adaptive-speed' ), CTPS_Safety::disabled_reason() ) : __( 'active', 'celero-adaptive-speed' ) ), array( 'item' => __( 'Profile', 'celero-adaptive-speed' ), 'value' => $profile ? CTPS_Settings::profiles()[ $profile ]['name'] : __( 'custom', 'celero-adaptive-speed' ) ), array( 'item' => __( 'Cached pages', 'celero-adaptive-speed' ), 'value' => CTPS_Page_Cache::count_files() ), array( 'item' => __( 'Preload queue', 'celero-adaptive-speed' ), 'value' => CTPS_Preload::remaining() ), /* translators: 1: converted images, 2: total images */ array( 'item' => 'WebP', 'value' => sprintf( __( '%1$s of %2$s', 'celero-adaptive-speed' ), $webp_done, $webp_total ) ), array( 'item' => __( 'Mobile performance', 'celero-adaptive-speed' ), 'value' => isset( $last['mobile']['scores']['performance']['score'] ) ? $last['mobile']['scores']['performance']['score'] : '–' ), array( 'item' => __( 'Measured visits (28 days)', 'celero-adaptive-speed' ), 'value' => $rum['samples'] ), array( 'item' => __( 'Last site check', 'celero-adaptive-speed' ), 'value' => is_array( $health ) ? $health['status'] . ' (' . wp_date( 'j M H:i', $health['time'] ) . ')' : '–' ), ); WP_CLI\Utils\format_items( 'table', $rows, array( 'item', 'value' ) ); } /** * Clears the cache. * * ## OPTIONS * * [--url=] * : Clear only this page. * * [--all] * : Also delete local fonts and generated CSS. * * ## EXAMPLES * * wp ctps purge * wp ctps purge --url=https://www.esempio.it/chi-siamo/ */ public function purge( $args, $assoc ) { if ( ! empty( $assoc['url'] ) ) { CTPS_Page_Cache::purge_url( $assoc['url'] ); do_action( 'ctps_urls_purged', array( $assoc['url'] ) ); /* translators: %s: page URL */ WP_CLI::success( sprintf( __( 'Cache cleared for %s', 'celero-adaptive-speed' ), $assoc['url'] ) ); return; } CTPS_Page_Cache::purge_all(); if ( ! empty( $assoc['all'] ) ) { CTPS_Assets::remove_all(); do_action( 'ctps_purge_everything' ); // Also clears the Redis cache. } WP_CLI::success( __( 'Cache cleared.', 'celero-adaptive-speed' ) ); } /** * Rebuilds the cache of the main pages. * * ## OPTIONS * * [--now] * : Visit all pages right away instead of using WP-Cron. */ public function preload( $args, $assoc ) { if ( empty( $assoc['now'] ) ) { CTPS_Preload::schedule(); $remaining = CTPS_Preload::remaining(); /* translators: %d: number of pages */ WP_CLI::success( sprintf( _n( 'Preload scheduled: %d page.', 'Preload scheduled: %d pages.', $remaining, 'celero-adaptive-speed' ), $remaining ) ); return; } $urls = CTPS_Preload::urls(); $progress = WP_CLI\Utils\make_progress_bar( 'Preload', count( $urls ) ); foreach ( $urls as $url ) { wp_remote_get( $url, array( 'timeout' => 20, 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), 'user-agent' => 'Adaptive Speed preload' ) ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- core WordPress filter. $progress->tick(); } $progress->finish(); /* translators: %d: number of pages */ WP_CLI::success( sprintf( _n( '%d page visited.', '%d pages visited.', count( $urls ), 'celero-adaptive-speed' ), count( $urls ) ) ); } /** * Runs a PageSpeed Insights test. * * ## OPTIONS * * [--url=] * : Page to test (default: home page). * * [--strategy=] * : mobile or desktop. * --- * default: mobile * options: * - mobile * - desktop * --- */ public function test( $args, $assoc ) { $url = ! empty( $assoc['url'] ) ? $assoc['url'] : home_url( '/' ); /* translators: %s: page URL */ WP_CLI::log( sprintf( __( 'Running test on %s…', 'celero-adaptive-speed' ), $url ) ); $r = CTPS_PSI::run( $url, $assoc['strategy'], true ); if ( is_wp_error( $r ) ) { WP_CLI::error( $r->get_error_message() ); } $rows = array(); foreach ( $r['scores'] as $s ) { $rows[] = array( 'item' => $s['title'], 'value' => $s['score'] ); } foreach ( $r['metrics'] as $m ) { $rows[] = array( 'item' => $m['short'], 'value' => $m['value'] ); } WP_CLI\Utils\format_items( 'table', $rows, array( 'item', 'value' ) ); foreach ( array_slice( $r['failed'], 0, 10 ) as $f ) { WP_CLI::log( '• ' . $f['title'] . ( $f['hint'] ? ' → ' . $f['hint'] : '' ) ); } } /** * Applies a profile. * * ## OPTIONS * * * : safe, balanced or max. */ public function profile( $args ) { if ( ! CTPS_Settings::apply_profile( $args[0] ) ) { WP_CLI::error( __( 'Unknown profile. Use: safe, balanced, max.', 'celero-adaptive-speed' ) ); } $health = get_option( CTPS_Safety::HEALTH ); if ( is_array( $health ) && ! empty( $health['rolled_back'] ) && $health['time'] >= time() - 60 ) { /* translators: %s: error description */ WP_CLI::warning( sprintf( __( 'The profile broke the site (%s): previous settings restored.', 'celero-adaptive-speed' ), $health['detail'] ) ); return; } WP_CLI::success( __( 'Profile applied.', 'celero-adaptive-speed' ) ); } /** * Turns emergency mode on or off. * * ## OPTIONS * * * : on or off. */ public function emergency( $args ) { if ( 'on' === $args[0] ) { CTPS_Safety::enter_emergency(); WP_CLI::success( __( 'Emergency mode on: no optimizations, .htaccess rules and cache removed.', 'celero-adaptive-speed' ) ); } elseif ( 'off' === $args[0] ) { CTPS_Safety::exit_emergency(); if ( CTPS_Safety::is_disabled() ) { WP_CLI::warning( __( 'The plugin stays disabled because of the wp-content/ctps-off file or the CTPS_DISABLE constant.', 'celero-adaptive-speed' ) ); return; } WP_CLI::success( __( 'Optimizations re-enabled.', 'celero-adaptive-speed' ) ); } else { WP_CLI::error( __( 'Usage: wp ctps emergency on|off', 'celero-adaptive-speed' ) ); } } /** * Converts library images to WebP (or removes the WebP copies). * * ## OPTIONS * * [--remove] * : Delete the WebP copies created by the plugin. */ public function webp( $args, $assoc ) { if ( ! empty( $assoc['remove'] ) ) { $total = 0; do { $r = CTPS_Webp::remove_batch(); $total += $r['removed']; } while ( $r['processed'] > 0 && $r['left'] > 0 ); CTPS_Page_Cache::purge_all(); /* translators: %d: number of files */ WP_CLI::success( sprintf( _n( '%d WebP file deleted.', '%d WebP files deleted.', $total, 'celero-adaptive-speed' ), $total ) ); return; } if ( ! CTPS_Webp::supported() ) { WP_CLI::error( __( 'The server cannot create WebP images (requires GD or Imagick with WebP support).', 'celero-adaptive-speed' ) ); } list( $done, $total ) = CTPS_Webp::stats(); $progress = WP_CLI\Utils\make_progress_bar( __( 'WebP conversion', 'celero-adaptive-speed' ), max( 0, $total - $done ) ); $saved = 0; do { $b = CTPS_Webp::convert_batch(); $saved += $b['saved']; for ( $i = 0; $i < $b['processed']; $i++ ) { $progress->tick(); } } while ( $b['processed'] > 0 ); $progress->finish(); CTPS_Page_Cache::purge_all(); /* translators: %s: saved size (e.g. 2 MB) */ WP_CLI::success( sprintf( __( 'Conversion complete, %s saved.', 'celero-adaptive-speed' ), size_format( $saved ) ) ); } /** * Lists or restores settings snapshots. * * ## OPTIONS * * [] * : ID of the snapshot to restore. Without an ID, shows the list. */ public function snapshots( $args ) { if ( ! empty( $args[0] ) ) { if ( ! CTPS_Safety::restore( $args[0] ) ) { WP_CLI::error( __( 'Snapshot not found.', 'celero-adaptive-speed' ) ); } WP_CLI::success( __( 'Settings restored.', 'celero-adaptive-speed' ) ); return; } $rows = array(); foreach ( array_reverse( CTPS_Safety::snapshots() ) as $s ) { $rows[] = array( 'id' => $s['id'], 'date' => wp_date( 'Y-m-d H:i', $s['time'] ), 'description' => $s['label'] ); } WP_CLI\Utils\format_items( 'table', $rows, array( 'id', 'date', 'description' ) ); } /** * Sends the report by email. * * ## OPTIONS * * [--to=] * : Recipient (default: the ones in the settings). */ public function report( $args, $assoc ) { $to = ! empty( $assoc['to'] ) ? array( $assoc['to'] ) : CTPS_Report::recipients(); if ( ! $to ) { WP_CLI::error( __( 'No recipients: use --to=email or fill in "Weekly email report" in the settings panel.', 'celero-adaptive-speed' ) ); } /* translators: %s: comma-separated email addresses */ CTPS_Report::send( $to ) ? WP_CLI::success( sprintf( __( 'Report sent to %s.', 'celero-adaptive-speed' ), implode( ', ', $to ) ) ) : WP_CLI::error( __( 'Sending failed.', 'celero-adaptive-speed' ) ); } } WP_CLI::add_command( 'ctps', 'CTPS_CLI' );