<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
* @package automattic/jetpack
use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
use Automattic\Jetpack\Connection\Tokens;
use Automattic\Jetpack\Identity_Crisis;
use Automattic\Jetpack\IP\Utils as IP_Utils;
use Automattic\Jetpack\Publicize\Connections;
use Automattic\Jetpack\Publicize\Publicize;
use Automattic\Jetpack\Status;
use Automattic\Jetpack\Sync\Actions;
use Automattic\Jetpack\Sync\Listener;
use Automattic\Jetpack\Sync\Modules;
use Automattic\Jetpack\Sync\Queue;
use Automattic\Jetpack\Sync\Settings;
use Automattic\Jetpack\Waf\Brute_Force_Protection\Brute_Force_Protection_Shared_Functions;
if ( ! class_exists( 'WP_CLI_Command' ) ) {
// @phan-suppress-next-line PhanUndeclaredFunctionInCallable -- https://github.com/phan/phan/issues/4763
WP_CLI::add_command( 'jetpack', 'Jetpack_CLI' );
* Control your local Jetpack installation.
class Jetpack_CLI extends WP_CLI_Command {
* Console escape code for green.
public $green_open = "\033[32m";
* Console escape code for red.
public $red_open = "\033[31m";
* Console escape code for yellow.
public $yellow_open = "\033[33m";
* Console escape code to reset coloring.
public $color_close = "\033[0m";
* empty: Leave it empty for basic stats
* full: View full stats. It's the data from the heartbeat
* @param array $args Positional args.
public function status( $args ) {
require_once JETPACK__PLUGIN_DIR . '_inc/lib/debugger.php';
/* translators: %s is the site URL */
WP_CLI::line( sprintf( __( 'Checking status for %s', 'jetpack' ), esc_url( get_home_url() ) ) );
if ( isset( $args[0] ) && 'full' !== $args[0] ) {
/* translators: %s is a command like "prompt" */
WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $args[0] ) );
$master_user_email = Jetpack::get_master_user_email();
$cxntests = new Jetpack_Cxn_Tests();
if ( $cxntests->pass() ) {
$cxntests->output_results_for_cli();
WP_CLI::success( __( 'Jetpack is currently connected to WordPress.com', 'jetpack' ) );
foreach ( $cxntests->list_fails() as $fail ) {
$error[] = $fail['name'] . ( empty( $fail['message'] ) ? '' : ': ' . $fail['message'] );
WP_CLI::error_multi_line( $error );
$cxntests->output_results_for_cli();
WP_CLI::error( __( 'One or more tests did not pass. Please investigate!', 'jetpack' ) ); // Exit CLI.
/* translators: %s is current version of Jetpack, for example 7.3 */
WP_CLI::line( sprintf( __( 'The Jetpack Version is %s', 'jetpack' ), JETPACK__VERSION ) );
/* translators: %d is WP.com ID of this blog */
WP_CLI::line( sprintf( __( 'The WordPress.com blog_id is %d', 'jetpack' ), Jetpack_Options::get_option( 'id' ) ) );
/* translators: %s is the email address of the connection owner */
WP_CLI::line( sprintf( __( 'The WordPress.com account for the primary connection is %s', 'jetpack' ), $master_user_email ) );
* Are they asking for all data?
* Loop through heartbeat data and organize by priority.
$all_data = ( isset( $args[0] ) && 'full' === $args[0] ) ? 'full' : false;
WP_CLI::line( "\n" . __( 'Additional data: ', 'jetpack' ) );
// Get the filtered heartbeat data.
// Filtered so we can color/list by severity.
$stats = Jetpack::jetpack_check_heartbeat_data();
// Display red flags first.
foreach ( $stats['bad'] as $stat => $value ) {
WP_CLI::line( sprintf( "$this->red_open%-'.16s %s $this->color_close", $stat, $value ) );
// Display caution warnings next.
foreach ( $stats['caution'] as $stat => $value ) {
WP_CLI::line( sprintf( "$this->yellow_open%-'.16s %s $this->color_close", $stat, $value ) );
// The rest of the results are good!
foreach ( $stats['good'] as $stat => $value ) {
// Modules should get special spacing for aestetics.
if ( strpos( $stat, 'odule-' ) ) {
WP_CLI::line( sprintf( "%-'.30s %s", $stat, $value ) );
usleep( 4000 ); // For dramatic effect lolz.
WP_CLI::line( sprintf( "%-'.16s %s", $stat, $value ) );
usleep( 4000 ); // For dramatic effect lolz.
WP_CLI::line( "\n" . _x( "View full status with 'wp jetpack status full'", '"wp jetpack status full" is a command - do not translate', 'jetpack' ) );
* Tests the active connection
* Does a two-way test to verify that the local site can communicate with remote Jetpack/WP.com servers and that Jetpack/WP.com servers can talk to the local site.
* wp jetpack test-connection
* @subcommand test-connection
public function test_connection() {
/* translators: %s is the site URL */
WP_CLI::line( sprintf( __( 'Testing connection for %s', 'jetpack' ), esc_url( get_site_url() ) ) );
if ( ! Jetpack::is_connection_ready() ) {
WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
$response = Client::wpcom_json_api_request_as_blog(
sprintf( '/jetpack-blogs/%d/test-connection', Jetpack_Options::get_option( 'id' ) ),
Client::WPCOM_JSON_API_VERSION
if ( is_wp_error( $response ) ) {
/* translators: %1$s is the error code, %2$s is the error message */
WP_CLI::error( sprintf( __( 'Failed to test connection (#%1$s: %2$s)', 'jetpack' ), $response->get_error_code(), $response->get_error_message() ) );
$body = wp_remote_retrieve_body( $response );
WP_CLI::error( __( 'Failed to test connection (empty response body)', 'jetpack' ) );
$result = json_decode( $body );
$is_connected = (bool) $result->connected;
$message = $result->message;
WP_CLI::success( $message );
WP_CLI::error( $message );
* Disconnect Jetpack Blogs or Users
* blog: Disconnect the entire blog.
* user <user_identifier>: Disconnect a specific user from WordPress.com.
* If the user ID provided is the connection owner, it will only be disconnected if --force is passed
* wp jetpack disconnect blog
* wp jetpack disconnect user 13
* wp jetpack disconnect user 1 --force
* wp jetpack disconnect user username
* wp jetpack disconnect user email@domain.com
* @synopsis <blog|user> [<user_identifier>] [--force]
* @param array $args Positional args.
* @param array $assoc_args Named args.
public function disconnect( $args, $assoc_args ) {
if ( ! Jetpack::is_connection_ready() ) {
WP_CLI::success( __( 'The site is not currently connected, so nothing to do!', 'jetpack' ) );
$action = isset( $args[0] ) ? $args[0] : 'prompt';
if ( ! in_array( $action, array( 'blog', 'user', 'prompt' ), true ) ) {
/* translators: %s is a command like "prompt" */
WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
if ( in_array( $action, array( 'user' ), true ) ) {
if ( isset( $args[1] ) ) {
if ( ctype_digit( $user_id ) ) {
$user_id = (int) $user_id;
} elseif ( is_email( $user_id ) ) {
$user_id = sanitize_user( $user_id, true );
$user_id = sanitize_user( $user_id, true );
$user = get_user_by( $field, $user_id );
WP_CLI::error( __( 'Please specify a valid user.', 'jetpack' ) );
WP_CLI::error( __( 'Please specify a user by either ID, username, or email.', 'jetpack' ) );
$force_user_disconnect = ! empty( $assoc_args['force'] );
Jetpack::log( 'disconnect' );
( new Connection_Manager( 'jetpack' ) )->disconnect_site();
/* translators: %s is the site URL */
__( 'Jetpack has been successfully disconnected for %s.', 'jetpack' ),
esc_url( get_site_url() )
$connection_manager = new Connection_Manager( 'jetpack' );
$disconnected = $connection_manager->disconnect_user( $user->ID, $force_user_disconnect );
Jetpack::log( 'unlink', $user->ID );
WP_CLI::success( __( 'User has been successfully disconnected.', 'jetpack' ) );
if ( ! $connection_manager->is_user_connected( $user->ID ) ) {
/* translators: %s is a username */
$error_message = sprintf( __( 'User %s could not be disconnected because it is not connected!', 'jetpack' ), "{$user->data->user_login} <{$user->data->user_email}>" );
} elseif ( ! $force_user_disconnect && $connection_manager->is_connection_owner( $user->ID ) ) {
/* translators: %s is a username */
$error_message = sprintf( __( 'User %s could not be disconnected because it is the connection owner! If you want to disconnect in anyway, use the --force parameter.', 'jetpack' ), "{$user->data->user_login} <{$user->data->user_email}>" );
/* translators: %s is a username */
$error_message = sprintf( __( 'User %s could not be disconnected.', 'jetpack' ), "{$user->data->user_login} <{$user->data->user_email}>" );
WP_CLI::error( $error_message );
WP_CLI::error( __( 'Please specify if you would like to disconnect a blog or user.', 'jetpack' ) );
* Reset Jetpack options and settings to default
* modules: Resets modules to default state ( get_default_modules() )
* options: Resets all Jetpack options except:
* - All private options (Blog token, user token, etc...)
* - id (The Client ID/WP.com Blog ID of this site)
* wp jetpack reset options
* wp jetpack reset modules
* wp jetpack reset sync-checksum --dry-run --offset=0
* @synopsis <modules|options|sync-checksum> [--dry-run] [--offset=<offset>]
* @param array $args Positional args.
* @param array $assoc_args Named args.
public function reset( $args, $assoc_args ) {
$action = isset( $args[0] ) ? $args[0] : 'prompt';
if ( ! in_array( $action, array( 'options', 'modules', 'sync-checksum' ), true ) ) {
/* translators: %s is a command like "prompt" */
WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
$is_dry_run = ! empty( $assoc_args['dry-run'] );
__( "\nThis is a dry run.\n", 'jetpack' ) .
__( "No actions will be taken.\n", 'jetpack' ) .
__( "The following messages will give you preview of what will happen when you run this command.\n\n", 'jetpack' )
// We only need to confirm "Are you sure?" when we are not doing a dry run.
jetpack_cli_are_you_sure();
$options_to_reset = Jetpack_Options::get_options_for_reset();
// Reset the Jetpack options.
/* translators: %s is the site URL */
__( "Resetting Jetpack Options for %s...\n", 'jetpack' ),
esc_url( get_site_url() )
sleep( 1 ); // Take a breath.
foreach ( $options_to_reset['jp_options'] as $option_to_reset ) {
Jetpack_Options::delete_option( $option_to_reset );
/* translators: This is the result of an action. The option named %s was reset */
WP_CLI::success( sprintf( __( '%s option reset', 'jetpack' ), $option_to_reset ) );
WP_CLI::line( __( "Resetting the jetpack options stored in wp_options...\n", 'jetpack' ) );
usleep( 500000 ); // Take a breath.
foreach ( $options_to_reset['wp_options'] as $option_to_reset ) {
delete_option( $option_to_reset );
/* translators: This is the result of an action. The option named %s was reset */
WP_CLI::success( sprintf( __( '%s option reset', 'jetpack' ), $option_to_reset ) );
// Reset to default modules.
WP_CLI::line( __( "Resetting default modules...\n", 'jetpack' ) );
usleep( 500000 ); // Take a breath.
$default_modules = Jetpack::get_default_modules();
Jetpack::update_active_modules( $default_modules );
WP_CLI::success( __( 'Modules reset to default.', 'jetpack' ) );
$default_modules = Jetpack::get_default_modules();
Jetpack::update_active_modules( $default_modules );
WP_CLI::success( __( 'Modules reset to default.', 'jetpack' ) );
WP_CLI::error( __( 'Please specify if you would like to reset your options, modules or sync-checksum', 'jetpack' ) );
$option = 'jetpack_callables_sync_checksum';
$offset = isset( $assoc_args['offset'] ) ? (int) $assoc_args['offset'] : 0;
* 1000 is a good limit since we don't expect the number of sites to be more than 1000
* Offset can be used to paginate and try to clean up more sites.
foreach ( $sites as $site ) {
switch_to_blog( $site->blog_id );
$count = self::count_option( $option );
delete_option( $option );
/* translators: %1$d is a number, %2$s is the name of an option, %2$s is the site URL. */
__( 'Deleted %1$d %2$s options from %3$s', 'jetpack' ),
"{$site->domain}{$site->path}"
* We could be deleting a lot of options rows at the same time.
* Allow some time for replication to catch up.
/* translators: %1$s is the name of an option, %2$d is a number of sites. */
__( 'Successfully reset %1$s on %2$d sites.', 'jetpack' ),
WP_CLI::success( __( 'No options were deleted.', 'jetpack' ) );
$count = self::count_option( $option );
delete_option( $option );
/* translators: %1$d is a number, %2$s is the name of an option. */
__( 'Deleted %1$d %2$s options', 'jetpack' ),
WP_CLI::success( __( 'No options were deleted.', 'jetpack' ) );
* Return the number of times an option appears
* Normally an option would only appear 1 since the option key is supposed to be unique
* but if a site hasn't updated the DB schema then that would not be the case.
* @param string $option Option name.
private static function count_option( $option ) {
return (int) $wpdb->get_var(
"SELECT COUNT(*) FROM $wpdb->options WHERE option_name = %s",