Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack
File: class.jetpack-cli.php
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
[0] Fix | Delete
/**
[1] Fix | Delete
* WP-CLI command class.
[2] Fix | Delete
*
[3] Fix | Delete
* @package automattic/jetpack
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
use Automattic\Jetpack\Connection\Client;
[7] Fix | Delete
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
[8] Fix | Delete
use Automattic\Jetpack\Connection\Tokens;
[9] Fix | Delete
use Automattic\Jetpack\Identity_Crisis;
[10] Fix | Delete
use Automattic\Jetpack\IP\Utils as IP_Utils;
[11] Fix | Delete
use Automattic\Jetpack\Publicize\Connections;
[12] Fix | Delete
use Automattic\Jetpack\Publicize\Publicize;
[13] Fix | Delete
use Automattic\Jetpack\Status;
[14] Fix | Delete
use Automattic\Jetpack\Sync\Actions;
[15] Fix | Delete
use Automattic\Jetpack\Sync\Listener;
[16] Fix | Delete
use Automattic\Jetpack\Sync\Modules;
[17] Fix | Delete
use Automattic\Jetpack\Sync\Queue;
[18] Fix | Delete
use Automattic\Jetpack\Sync\Settings;
[19] Fix | Delete
use Automattic\Jetpack\Waf\Brute_Force_Protection\Brute_Force_Protection_Shared_Functions;
[20] Fix | Delete
[21] Fix | Delete
if ( ! class_exists( 'WP_CLI_Command' ) ) {
[22] Fix | Delete
return;
[23] Fix | Delete
}
[24] Fix | Delete
[25] Fix | Delete
// @phan-suppress-next-line PhanUndeclaredFunctionInCallable -- https://github.com/phan/phan/issues/4763
[26] Fix | Delete
WP_CLI::add_command( 'jetpack', 'Jetpack_CLI' );
[27] Fix | Delete
[28] Fix | Delete
/**
[29] Fix | Delete
* Control your local Jetpack installation.
[30] Fix | Delete
*/
[31] Fix | Delete
class Jetpack_CLI extends WP_CLI_Command {
[32] Fix | Delete
/**
[33] Fix | Delete
* Console escape code for green.
[34] Fix | Delete
*
[35] Fix | Delete
* @var string
[36] Fix | Delete
*/
[37] Fix | Delete
public $green_open = "\033[32m";
[38] Fix | Delete
[39] Fix | Delete
/**
[40] Fix | Delete
* Console escape code for red.
[41] Fix | Delete
*
[42] Fix | Delete
* @var string
[43] Fix | Delete
*/
[44] Fix | Delete
public $red_open = "\033[31m";
[45] Fix | Delete
[46] Fix | Delete
/**
[47] Fix | Delete
* Console escape code for yellow.
[48] Fix | Delete
*
[49] Fix | Delete
* @var string
[50] Fix | Delete
*/
[51] Fix | Delete
public $yellow_open = "\033[33m";
[52] Fix | Delete
[53] Fix | Delete
/**
[54] Fix | Delete
* Console escape code to reset coloring.
[55] Fix | Delete
*
[56] Fix | Delete
* @var string
[57] Fix | Delete
*/
[58] Fix | Delete
public $color_close = "\033[0m";
[59] Fix | Delete
[60] Fix | Delete
/**
[61] Fix | Delete
* Get Jetpack Details
[62] Fix | Delete
*
[63] Fix | Delete
* ## OPTIONS
[64] Fix | Delete
*
[65] Fix | Delete
* empty: Leave it empty for basic stats
[66] Fix | Delete
*
[67] Fix | Delete
* full: View full stats. It's the data from the heartbeat
[68] Fix | Delete
*
[69] Fix | Delete
* ## EXAMPLES
[70] Fix | Delete
*
[71] Fix | Delete
* wp jetpack status
[72] Fix | Delete
* wp jetpack status full
[73] Fix | Delete
*
[74] Fix | Delete
* @param array $args Positional args.
[75] Fix | Delete
*/
[76] Fix | Delete
public function status( $args ) {
[77] Fix | Delete
require_once JETPACK__PLUGIN_DIR . '_inc/lib/debugger.php';
[78] Fix | Delete
[79] Fix | Delete
/* translators: %s is the site URL */
[80] Fix | Delete
WP_CLI::line( sprintf( __( 'Checking status for %s', 'jetpack' ), esc_url( get_home_url() ) ) );
[81] Fix | Delete
[82] Fix | Delete
if ( isset( $args[0] ) && 'full' !== $args[0] ) {
[83] Fix | Delete
/* translators: %s is a command like "prompt" */
[84] Fix | Delete
WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $args[0] ) );
[85] Fix | Delete
}
[86] Fix | Delete
[87] Fix | Delete
$master_user_email = Jetpack::get_master_user_email();
[88] Fix | Delete
[89] Fix | Delete
$cxntests = new Jetpack_Cxn_Tests();
[90] Fix | Delete
[91] Fix | Delete
if ( $cxntests->pass() ) {
[92] Fix | Delete
$cxntests->output_results_for_cli();
[93] Fix | Delete
[94] Fix | Delete
WP_CLI::success( __( 'Jetpack is currently connected to WordPress.com', 'jetpack' ) );
[95] Fix | Delete
} else {
[96] Fix | Delete
$error = array();
[97] Fix | Delete
foreach ( $cxntests->list_fails() as $fail ) {
[98] Fix | Delete
$error[] = $fail['name'] . ( empty( $fail['message'] ) ? '' : ': ' . $fail['message'] );
[99] Fix | Delete
}
[100] Fix | Delete
WP_CLI::error_multi_line( $error );
[101] Fix | Delete
[102] Fix | Delete
$cxntests->output_results_for_cli();
[103] Fix | Delete
[104] Fix | Delete
WP_CLI::error( __( 'One or more tests did not pass. Please investigate!', 'jetpack' ) ); // Exit CLI.
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
/* translators: %s is current version of Jetpack, for example 7.3 */
[108] Fix | Delete
WP_CLI::line( sprintf( __( 'The Jetpack Version is %s', 'jetpack' ), JETPACK__VERSION ) );
[109] Fix | Delete
/* translators: %d is WP.com ID of this blog */
[110] Fix | Delete
WP_CLI::line( sprintf( __( 'The WordPress.com blog_id is %d', 'jetpack' ), Jetpack_Options::get_option( 'id' ) ) );
[111] Fix | Delete
/* translators: %s is the email address of the connection owner */
[112] Fix | Delete
WP_CLI::line( sprintf( __( 'The WordPress.com account for the primary connection is %s', 'jetpack' ), $master_user_email ) );
[113] Fix | Delete
[114] Fix | Delete
/*
[115] Fix | Delete
* Are they asking for all data?
[116] Fix | Delete
*
[117] Fix | Delete
* Loop through heartbeat data and organize by priority.
[118] Fix | Delete
*/
[119] Fix | Delete
$all_data = ( isset( $args[0] ) && 'full' === $args[0] ) ? 'full' : false;
[120] Fix | Delete
if ( $all_data ) {
[121] Fix | Delete
// Heartbeat data.
[122] Fix | Delete
WP_CLI::line( "\n" . __( 'Additional data: ', 'jetpack' ) );
[123] Fix | Delete
[124] Fix | Delete
// Get the filtered heartbeat data.
[125] Fix | Delete
// Filtered so we can color/list by severity.
[126] Fix | Delete
$stats = Jetpack::jetpack_check_heartbeat_data();
[127] Fix | Delete
[128] Fix | Delete
// Display red flags first.
[129] Fix | Delete
foreach ( $stats['bad'] as $stat => $value ) {
[130] Fix | Delete
WP_CLI::line( sprintf( "$this->red_open%-'.16s %s $this->color_close", $stat, $value ) );
[131] Fix | Delete
}
[132] Fix | Delete
[133] Fix | Delete
// Display caution warnings next.
[134] Fix | Delete
foreach ( $stats['caution'] as $stat => $value ) {
[135] Fix | Delete
WP_CLI::line( sprintf( "$this->yellow_open%-'.16s %s $this->color_close", $stat, $value ) );
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
// The rest of the results are good!
[139] Fix | Delete
foreach ( $stats['good'] as $stat => $value ) {
[140] Fix | Delete
[141] Fix | Delete
// Modules should get special spacing for aestetics.
[142] Fix | Delete
if ( strpos( $stat, 'odule-' ) ) {
[143] Fix | Delete
WP_CLI::line( sprintf( "%-'.30s %s", $stat, $value ) );
[144] Fix | Delete
usleep( 4000 ); // For dramatic effect lolz.
[145] Fix | Delete
continue;
[146] Fix | Delete
}
[147] Fix | Delete
WP_CLI::line( sprintf( "%-'.16s %s", $stat, $value ) );
[148] Fix | Delete
usleep( 4000 ); // For dramatic effect lolz.
[149] Fix | Delete
}
[150] Fix | Delete
} else {
[151] Fix | Delete
// Just the basics.
[152] Fix | Delete
WP_CLI::line( "\n" . _x( "View full status with 'wp jetpack status full'", '"wp jetpack status full" is a command - do not translate', 'jetpack' ) );
[153] Fix | Delete
}
[154] Fix | Delete
}
[155] Fix | Delete
[156] Fix | Delete
/**
[157] Fix | Delete
* Tests the active connection
[158] Fix | Delete
*
[159] Fix | Delete
* 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.
[160] Fix | Delete
*
[161] Fix | Delete
* ## EXAMPLES
[162] Fix | Delete
*
[163] Fix | Delete
* wp jetpack test-connection
[164] Fix | Delete
*
[165] Fix | Delete
* @subcommand test-connection
[166] Fix | Delete
*/
[167] Fix | Delete
public function test_connection() {
[168] Fix | Delete
[169] Fix | Delete
/* translators: %s is the site URL */
[170] Fix | Delete
WP_CLI::line( sprintf( __( 'Testing connection for %s', 'jetpack' ), esc_url( get_site_url() ) ) );
[171] Fix | Delete
[172] Fix | Delete
if ( ! Jetpack::is_connection_ready() ) {
[173] Fix | Delete
WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
[174] Fix | Delete
}
[175] Fix | Delete
[176] Fix | Delete
$response = Client::wpcom_json_api_request_as_blog(
[177] Fix | Delete
sprintf( '/jetpack-blogs/%d/test-connection', Jetpack_Options::get_option( 'id' ) ),
[178] Fix | Delete
Client::WPCOM_JSON_API_VERSION
[179] Fix | Delete
);
[180] Fix | Delete
[181] Fix | Delete
if ( is_wp_error( $response ) ) {
[182] Fix | Delete
/* translators: %1$s is the error code, %2$s is the error message */
[183] Fix | Delete
WP_CLI::error( sprintf( __( 'Failed to test connection (#%1$s: %2$s)', 'jetpack' ), $response->get_error_code(), $response->get_error_message() ) );
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
$body = wp_remote_retrieve_body( $response );
[187] Fix | Delete
if ( ! $body ) {
[188] Fix | Delete
WP_CLI::error( __( 'Failed to test connection (empty response body)', 'jetpack' ) );
[189] Fix | Delete
}
[190] Fix | Delete
[191] Fix | Delete
$result = json_decode( $body );
[192] Fix | Delete
$is_connected = (bool) $result->connected;
[193] Fix | Delete
$message = $result->message;
[194] Fix | Delete
[195] Fix | Delete
if ( $is_connected ) {
[196] Fix | Delete
WP_CLI::success( $message );
[197] Fix | Delete
} else {
[198] Fix | Delete
WP_CLI::error( $message );
[199] Fix | Delete
}
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
/**
[203] Fix | Delete
* Disconnect Jetpack Blogs or Users
[204] Fix | Delete
*
[205] Fix | Delete
* ## OPTIONS
[206] Fix | Delete
*
[207] Fix | Delete
* blog: Disconnect the entire blog.
[208] Fix | Delete
*
[209] Fix | Delete
* user <user_identifier>: Disconnect a specific user from WordPress.com.
[210] Fix | Delete
*
[211] Fix | Delete
* [--force]
[212] Fix | Delete
* If the user ID provided is the connection owner, it will only be disconnected if --force is passed
[213] Fix | Delete
*
[214] Fix | Delete
* ## EXAMPLES
[215] Fix | Delete
*
[216] Fix | Delete
* wp jetpack disconnect blog
[217] Fix | Delete
* wp jetpack disconnect user 13
[218] Fix | Delete
* wp jetpack disconnect user 1 --force
[219] Fix | Delete
* wp jetpack disconnect user username
[220] Fix | Delete
* wp jetpack disconnect user email@domain.com
[221] Fix | Delete
*
[222] Fix | Delete
* @synopsis <blog|user> [<user_identifier>] [--force]
[223] Fix | Delete
*
[224] Fix | Delete
* @param array $args Positional args.
[225] Fix | Delete
* @param array $assoc_args Named args.
[226] Fix | Delete
*/
[227] Fix | Delete
public function disconnect( $args, $assoc_args ) {
[228] Fix | Delete
$user = null;
[229] Fix | Delete
if ( ! Jetpack::is_connection_ready() ) {
[230] Fix | Delete
WP_CLI::success( __( 'The site is not currently connected, so nothing to do!', 'jetpack' ) );
[231] Fix | Delete
return;
[232] Fix | Delete
}
[233] Fix | Delete
[234] Fix | Delete
$action = isset( $args[0] ) ? $args[0] : 'prompt';
[235] Fix | Delete
if ( ! in_array( $action, array( 'blog', 'user', 'prompt' ), true ) ) {
[236] Fix | Delete
/* translators: %s is a command like "prompt" */
[237] Fix | Delete
WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
[238] Fix | Delete
}
[239] Fix | Delete
[240] Fix | Delete
if ( in_array( $action, array( 'user' ), true ) ) {
[241] Fix | Delete
if ( isset( $args[1] ) ) {
[242] Fix | Delete
$user_id = $args[1];
[243] Fix | Delete
if ( ctype_digit( $user_id ) ) {
[244] Fix | Delete
$field = 'id';
[245] Fix | Delete
$user_id = (int) $user_id;
[246] Fix | Delete
} elseif ( is_email( $user_id ) ) {
[247] Fix | Delete
$field = 'email';
[248] Fix | Delete
$user_id = sanitize_user( $user_id, true );
[249] Fix | Delete
} else {
[250] Fix | Delete
$field = 'login';
[251] Fix | Delete
$user_id = sanitize_user( $user_id, true );
[252] Fix | Delete
}
[253] Fix | Delete
$user = get_user_by( $field, $user_id );
[254] Fix | Delete
if ( ! $user ) {
[255] Fix | Delete
WP_CLI::error( __( 'Please specify a valid user.', 'jetpack' ) );
[256] Fix | Delete
}
[257] Fix | Delete
} else {
[258] Fix | Delete
WP_CLI::error( __( 'Please specify a user by either ID, username, or email.', 'jetpack' ) );
[259] Fix | Delete
}
[260] Fix | Delete
}
[261] Fix | Delete
[262] Fix | Delete
$force_user_disconnect = ! empty( $assoc_args['force'] );
[263] Fix | Delete
[264] Fix | Delete
switch ( $action ) {
[265] Fix | Delete
case 'blog':
[266] Fix | Delete
Jetpack::log( 'disconnect' );
[267] Fix | Delete
( new Connection_Manager( 'jetpack' ) )->disconnect_site();
[268] Fix | Delete
WP_CLI::success(
[269] Fix | Delete
sprintf(
[270] Fix | Delete
/* translators: %s is the site URL */
[271] Fix | Delete
__( 'Jetpack has been successfully disconnected for %s.', 'jetpack' ),
[272] Fix | Delete
esc_url( get_site_url() )
[273] Fix | Delete
)
[274] Fix | Delete
);
[275] Fix | Delete
break;
[276] Fix | Delete
case 'user':
[277] Fix | Delete
$connection_manager = new Connection_Manager( 'jetpack' );
[278] Fix | Delete
$disconnected = $connection_manager->disconnect_user( $user->ID, $force_user_disconnect );
[279] Fix | Delete
if ( $disconnected ) {
[280] Fix | Delete
Jetpack::log( 'unlink', $user->ID );
[281] Fix | Delete
WP_CLI::success( __( 'User has been successfully disconnected.', 'jetpack' ) );
[282] Fix | Delete
} else {
[283] Fix | Delete
if ( ! $connection_manager->is_user_connected( $user->ID ) ) {
[284] Fix | Delete
/* translators: %s is a username */
[285] Fix | Delete
$error_message = sprintf( __( 'User %s could not be disconnected because it is not connected!', 'jetpack' ), "{$user->data->user_login} <{$user->data->user_email}>" );
[286] Fix | Delete
} elseif ( ! $force_user_disconnect && $connection_manager->is_connection_owner( $user->ID ) ) {
[287] Fix | Delete
/* translators: %s is a username */
[288] Fix | Delete
$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}>" );
[289] Fix | Delete
} else {
[290] Fix | Delete
/* translators: %s is a username */
[291] Fix | Delete
$error_message = sprintf( __( 'User %s could not be disconnected.', 'jetpack' ), "{$user->data->user_login} <{$user->data->user_email}>" );
[292] Fix | Delete
}
[293] Fix | Delete
WP_CLI::error( $error_message );
[294] Fix | Delete
}
[295] Fix | Delete
break;
[296] Fix | Delete
case 'prompt':
[297] Fix | Delete
WP_CLI::error( __( 'Please specify if you would like to disconnect a blog or user.', 'jetpack' ) );
[298] Fix | Delete
break;
[299] Fix | Delete
}
[300] Fix | Delete
}
[301] Fix | Delete
[302] Fix | Delete
/**
[303] Fix | Delete
* Reset Jetpack options and settings to default
[304] Fix | Delete
*
[305] Fix | Delete
* ## OPTIONS
[306] Fix | Delete
*
[307] Fix | Delete
* modules: Resets modules to default state ( get_default_modules() )
[308] Fix | Delete
*
[309] Fix | Delete
* options: Resets all Jetpack options except:
[310] Fix | Delete
* - All private options (Blog token, user token, etc...)
[311] Fix | Delete
* - id (The Client ID/WP.com Blog ID of this site)
[312] Fix | Delete
* - master_user
[313] Fix | Delete
* - version
[314] Fix | Delete
* - activated
[315] Fix | Delete
*
[316] Fix | Delete
* ## EXAMPLES
[317] Fix | Delete
*
[318] Fix | Delete
* wp jetpack reset options
[319] Fix | Delete
* wp jetpack reset modules
[320] Fix | Delete
* wp jetpack reset sync-checksum --dry-run --offset=0
[321] Fix | Delete
*
[322] Fix | Delete
* @synopsis <modules|options|sync-checksum> [--dry-run] [--offset=<offset>]
[323] Fix | Delete
*
[324] Fix | Delete
* @param array $args Positional args.
[325] Fix | Delete
* @param array $assoc_args Named args.
[326] Fix | Delete
*/
[327] Fix | Delete
public function reset( $args, $assoc_args ) {
[328] Fix | Delete
$action = isset( $args[0] ) ? $args[0] : 'prompt';
[329] Fix | Delete
if ( ! in_array( $action, array( 'options', 'modules', 'sync-checksum' ), true ) ) {
[330] Fix | Delete
/* translators: %s is a command like "prompt" */
[331] Fix | Delete
WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
[332] Fix | Delete
}
[333] Fix | Delete
[334] Fix | Delete
$is_dry_run = ! empty( $assoc_args['dry-run'] );
[335] Fix | Delete
[336] Fix | Delete
if ( $is_dry_run ) {
[337] Fix | Delete
WP_CLI::warning(
[338] Fix | Delete
__( "\nThis is a dry run.\n", 'jetpack' ) .
[339] Fix | Delete
__( "No actions will be taken.\n", 'jetpack' ) .
[340] Fix | Delete
__( "The following messages will give you preview of what will happen when you run this command.\n\n", 'jetpack' )
[341] Fix | Delete
);
[342] Fix | Delete
} else {
[343] Fix | Delete
// We only need to confirm "Are you sure?" when we are not doing a dry run.
[344] Fix | Delete
jetpack_cli_are_you_sure();
[345] Fix | Delete
}
[346] Fix | Delete
[347] Fix | Delete
switch ( $action ) {
[348] Fix | Delete
case 'options':
[349] Fix | Delete
$options_to_reset = Jetpack_Options::get_options_for_reset();
[350] Fix | Delete
// Reset the Jetpack options.
[351] Fix | Delete
WP_CLI::line(
[352] Fix | Delete
sprintf(
[353] Fix | Delete
/* translators: %s is the site URL */
[354] Fix | Delete
__( "Resetting Jetpack Options for %s...\n", 'jetpack' ),
[355] Fix | Delete
esc_url( get_site_url() )
[356] Fix | Delete
)
[357] Fix | Delete
);
[358] Fix | Delete
sleep( 1 ); // Take a breath.
[359] Fix | Delete
foreach ( $options_to_reset['jp_options'] as $option_to_reset ) {
[360] Fix | Delete
if ( ! $is_dry_run ) {
[361] Fix | Delete
Jetpack_Options::delete_option( $option_to_reset );
[362] Fix | Delete
usleep( 100000 );
[363] Fix | Delete
}
[364] Fix | Delete
[365] Fix | Delete
/* translators: This is the result of an action. The option named %s was reset */
[366] Fix | Delete
WP_CLI::success( sprintf( __( '%s option reset', 'jetpack' ), $option_to_reset ) );
[367] Fix | Delete
}
[368] Fix | Delete
[369] Fix | Delete
// Reset the WP options.
[370] Fix | Delete
WP_CLI::line( __( "Resetting the jetpack options stored in wp_options...\n", 'jetpack' ) );
[371] Fix | Delete
usleep( 500000 ); // Take a breath.
[372] Fix | Delete
foreach ( $options_to_reset['wp_options'] as $option_to_reset ) {
[373] Fix | Delete
if ( ! $is_dry_run ) {
[374] Fix | Delete
delete_option( $option_to_reset );
[375] Fix | Delete
usleep( 100000 );
[376] Fix | Delete
}
[377] Fix | Delete
/* translators: This is the result of an action. The option named %s was reset */
[378] Fix | Delete
WP_CLI::success( sprintf( __( '%s option reset', 'jetpack' ), $option_to_reset ) );
[379] Fix | Delete
}
[380] Fix | Delete
[381] Fix | Delete
// Reset to default modules.
[382] Fix | Delete
WP_CLI::line( __( "Resetting default modules...\n", 'jetpack' ) );
[383] Fix | Delete
usleep( 500000 ); // Take a breath.
[384] Fix | Delete
$default_modules = Jetpack::get_default_modules();
[385] Fix | Delete
if ( ! $is_dry_run ) {
[386] Fix | Delete
Jetpack::update_active_modules( $default_modules );
[387] Fix | Delete
}
[388] Fix | Delete
WP_CLI::success( __( 'Modules reset to default.', 'jetpack' ) );
[389] Fix | Delete
break;
[390] Fix | Delete
case 'modules':
[391] Fix | Delete
if ( ! $is_dry_run ) {
[392] Fix | Delete
$default_modules = Jetpack::get_default_modules();
[393] Fix | Delete
Jetpack::update_active_modules( $default_modules );
[394] Fix | Delete
}
[395] Fix | Delete
[396] Fix | Delete
WP_CLI::success( __( 'Modules reset to default.', 'jetpack' ) );
[397] Fix | Delete
break;
[398] Fix | Delete
case 'prompt':
[399] Fix | Delete
WP_CLI::error( __( 'Please specify if you would like to reset your options, modules or sync-checksum', 'jetpack' ) );
[400] Fix | Delete
break;
[401] Fix | Delete
case 'sync-checksum':
[402] Fix | Delete
$option = 'jetpack_callables_sync_checksum';
[403] Fix | Delete
[404] Fix | Delete
if ( is_multisite() ) {
[405] Fix | Delete
$offset = isset( $assoc_args['offset'] ) ? (int) $assoc_args['offset'] : 0;
[406] Fix | Delete
[407] Fix | Delete
/*
[408] Fix | Delete
* 1000 is a good limit since we don't expect the number of sites to be more than 1000
[409] Fix | Delete
* Offset can be used to paginate and try to clean up more sites.
[410] Fix | Delete
*/
[411] Fix | Delete
$sites = get_sites(
[412] Fix | Delete
array(
[413] Fix | Delete
'number' => 1000,
[414] Fix | Delete
'offset' => $offset,
[415] Fix | Delete
)
[416] Fix | Delete
);
[417] Fix | Delete
$count_fixes = 0;
[418] Fix | Delete
foreach ( $sites as $site ) {
[419] Fix | Delete
switch_to_blog( $site->blog_id );
[420] Fix | Delete
$count = self::count_option( $option );
[421] Fix | Delete
if ( $count > 1 ) {
[422] Fix | Delete
if ( ! $is_dry_run ) {
[423] Fix | Delete
delete_option( $option );
[424] Fix | Delete
}
[425] Fix | Delete
WP_CLI::line(
[426] Fix | Delete
sprintf(
[427] Fix | Delete
/* translators: %1$d is a number, %2$s is the name of an option, %2$s is the site URL. */
[428] Fix | Delete
__( 'Deleted %1$d %2$s options from %3$s', 'jetpack' ),
[429] Fix | Delete
$count,
[430] Fix | Delete
$option,
[431] Fix | Delete
"{$site->domain}{$site->path}"
[432] Fix | Delete
)
[433] Fix | Delete
);
[434] Fix | Delete
++$count_fixes;
[435] Fix | Delete
if ( ! $is_dry_run ) {
[436] Fix | Delete
/*
[437] Fix | Delete
* We could be deleting a lot of options rows at the same time.
[438] Fix | Delete
* Allow some time for replication to catch up.
[439] Fix | Delete
*/
[440] Fix | Delete
sleep( 3 );
[441] Fix | Delete
}
[442] Fix | Delete
}
[443] Fix | Delete
[444] Fix | Delete
restore_current_blog();
[445] Fix | Delete
}
[446] Fix | Delete
if ( $count_fixes ) {
[447] Fix | Delete
WP_CLI::success(
[448] Fix | Delete
sprintf(
[449] Fix | Delete
/* translators: %1$s is the name of an option, %2$d is a number of sites. */
[450] Fix | Delete
__( 'Successfully reset %1$s on %2$d sites.', 'jetpack' ),
[451] Fix | Delete
$option,
[452] Fix | Delete
$count_fixes
[453] Fix | Delete
)
[454] Fix | Delete
);
[455] Fix | Delete
} else {
[456] Fix | Delete
WP_CLI::success( __( 'No options were deleted.', 'jetpack' ) );
[457] Fix | Delete
}
[458] Fix | Delete
return;
[459] Fix | Delete
}
[460] Fix | Delete
[461] Fix | Delete
$count = self::count_option( $option );
[462] Fix | Delete
if ( $count > 1 ) {
[463] Fix | Delete
if ( ! $is_dry_run ) {
[464] Fix | Delete
delete_option( $option );
[465] Fix | Delete
}
[466] Fix | Delete
WP_CLI::success(
[467] Fix | Delete
sprintf(
[468] Fix | Delete
/* translators: %1$d is a number, %2$s is the name of an option. */
[469] Fix | Delete
__( 'Deleted %1$d %2$s options', 'jetpack' ),
[470] Fix | Delete
$count,
[471] Fix | Delete
$option
[472] Fix | Delete
)
[473] Fix | Delete
);
[474] Fix | Delete
return;
[475] Fix | Delete
}
[476] Fix | Delete
[477] Fix | Delete
WP_CLI::success( __( 'No options were deleted.', 'jetpack' ) );
[478] Fix | Delete
break;
[479] Fix | Delete
[480] Fix | Delete
}
[481] Fix | Delete
}
[482] Fix | Delete
[483] Fix | Delete
/**
[484] Fix | Delete
* Return the number of times an option appears
[485] Fix | Delete
* Normally an option would only appear 1 since the option key is supposed to be unique
[486] Fix | Delete
* but if a site hasn't updated the DB schema then that would not be the case.
[487] Fix | Delete
*
[488] Fix | Delete
* @param string $option Option name.
[489] Fix | Delete
*
[490] Fix | Delete
* @return int
[491] Fix | Delete
*/
[492] Fix | Delete
private static function count_option( $option ) {
[493] Fix | Delete
global $wpdb;
[494] Fix | Delete
return (int) $wpdb->get_var(
[495] Fix | Delete
$wpdb->prepare(
[496] Fix | Delete
"SELECT COUNT(*) FROM $wpdb->options WHERE option_name = %s",
[497] Fix | Delete
$option
[498] Fix | Delete
)
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function