summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/jetpack/class.jetpack-cli.php')
-rw-r--r--plugins/jetpack/class.jetpack-cli.php581
1 files changed, 389 insertions, 192 deletions
diff --git a/plugins/jetpack/class.jetpack-cli.php b/plugins/jetpack/class.jetpack-cli.php
index 90b605b6..653d0f93 100644
--- a/plugins/jetpack/class.jetpack-cli.php
+++ b/plugins/jetpack/class.jetpack-cli.php
@@ -2,14 +2,20 @@
WP_CLI::add_command( 'jetpack', 'Jetpack_CLI' );
+use Automattic\Jetpack\Connection\Client;
+use Automattic\Jetpack\Connection\Manager as Connection_Manager;
+use Automattic\Jetpack\Connection\Utils as Connection_Utils;
+use Automattic\Jetpack\Status;
+use Automattic\Jetpack\Sync\Actions;
+use Automattic\Jetpack\Sync\Listener;
+use Automattic\Jetpack\Sync\Queue;
+use Automattic\Jetpack\Sync\Settings;
+
/**
* Control your local Jetpack installation.
- *
- * Minimum PHP requirement for WP-CLI is PHP 5.3, so ignore PHP 5.2 compatibility issues.
- * @phpcs:disable PHPCompatibility.PHP.NewLanguageConstructs.t_ns_separatorFound
*/
class Jetpack_CLI extends WP_CLI_Command {
- // Aesthetics
+ // Aesthetics.
public $green_open = "\033[32m";
public $red_open = "\033[31m";
public $yellow_open = "\033[33m";
@@ -28,11 +34,11 @@ class Jetpack_CLI extends WP_CLI_Command {
*
* wp jetpack status
* wp jetpack status full
- *
*/
public function status( $args, $assoc_args ) {
jetpack_require_lib( 'debugger' );
+ /* 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] ) {
@@ -57,11 +63,14 @@ class Jetpack_CLI extends WP_CLI_Command {
$cxntests->output_results_for_cli();
- WP_CLI::error( __('Jetpack connection is broken.', 'jetpack' ) ); // Exit CLI.
+ WP_CLI::error( __( 'Jetpack connection is broken.', '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 ) );
/*
@@ -119,15 +128,16 @@ class Jetpack_CLI extends WP_CLI_Command {
*/
public function test_connection( $args, $assoc_args ) {
+ /* translators: %s is the site URL */
WP_CLI::line( sprintf( __( 'Testing connection for %s', 'jetpack' ), esc_url( get_site_url() ) ) );
if ( ! Jetpack::is_active() ) {
WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
}
- $response = Jetpack_Client::wpcom_json_api_request_as_blog(
+ $response = Client::wpcom_json_api_request_as_blog(
sprintf( '/jetpack-blogs/%d/test-connection', Jetpack_Options::get_option( 'id' ) ),
- Jetpack_Client::WPCOM_JSON_API_VERSION
+ Client::WPCOM_JSON_API_VERSION
);
if ( is_wp_error( $response ) ) {
@@ -140,9 +150,9 @@ class Jetpack_CLI extends WP_CLI_Command {
WP_CLI::error( __( 'Failed to test connection (empty response body)', 'jetpack' ) );
}
- $result = json_decode( $body );
+ $result = json_decode( $body );
$is_connected = (bool) $result->connected;
- $message = $result->message;
+ $message = $result->message;
if ( $is_connected ) {
WP_CLI::success( $message );
@@ -175,7 +185,8 @@ class Jetpack_CLI extends WP_CLI_Command {
*/
public function disconnect( $args, $assoc_args ) {
if ( ! Jetpack::is_active() ) {
- WP_CLI::error( __( 'You cannot disconnect, without having first connected.', 'jetpack' ) );
+ WP_CLI::success( __( 'The site is not currently connected, so nothing to do!', 'jetpack' ) );
+ return;
}
$action = isset( $args[0] ) ? $args[0] : 'prompt';
@@ -188,13 +199,13 @@ class Jetpack_CLI extends WP_CLI_Command {
if ( isset( $args[1] ) ) {
$user_id = $args[1];
if ( ctype_digit( $user_id ) ) {
- $field = 'id';
+ $field = 'id';
$user_id = (int) $user_id;
} elseif ( is_email( $user_id ) ) {
- $field = 'email';
+ $field = 'email';
$user_id = sanitize_user( $user_id, true );
} else {
- $field = 'login';
+ $field = 'login';
$user_id = sanitize_user( $user_id, true );
}
if ( ! $user = get_user_by( $field, $user_id ) ) {
@@ -209,13 +220,16 @@ class Jetpack_CLI extends WP_CLI_Command {
case 'blog':
Jetpack::log( 'disconnect' );
Jetpack::disconnect();
- WP_CLI::success( sprintf(
- __( 'Jetpack has been successfully disconnected for %s.', 'jetpack' ),
- esc_url( get_site_url() )
- ) );
+ WP_CLI::success(
+ sprintf(
+ /* translators: %s is the site URL */
+ __( 'Jetpack has been successfully disconnected for %s.', 'jetpack' ),
+ esc_url( get_site_url() )
+ )
+ );
break;
case 'user':
- if ( Jetpack::unlink_user( $user->ID ) ) {
+ if ( Connection_Manager::disconnect_user( $user->ID ) ) {
Jetpack::log( 'unlink', $user->ID );
WP_CLI::success( __( 'User has been successfully disconnected.', 'jetpack' ) );
} else {
@@ -247,69 +261,187 @@ class Jetpack_CLI extends WP_CLI_Command {
*
* wp jetpack reset options
* wp jetpack reset modules
+ * wp jetpack reset sync-checksum --dry-run --offset=0
*
- * @synopsis <modules|options>
+ * @synopsis <modules|options|sync-checksum> [--dry-run] [--offset=<offset>]
*/
public function reset( $args, $assoc_args ) {
$action = isset( $args[0] ) ? $args[0] : 'prompt';
- if ( ! in_array( $action, array( 'options', 'modules' ) ) ) {
+ 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 ) );
}
- // Are you sure?
- jetpack_cli_are_you_sure();
+ $is_dry_run = ! empty( $assoc_args['dry-run'] );
+
+ if ( $is_dry_run ) {
+ WP_CLI::warning(
+ __( "\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' )
+ );
+ } else {
+ // We only need to confirm "Are you sure?" when we are not doing a dry run.
+ jetpack_cli_are_you_sure();
+ }
switch ( $action ) {
case 'options':
$options_to_reset = Jetpack_Options::get_options_for_reset();
-
// Reset the Jetpack options
- WP_CLI::line( sprintf(
- __( "Resetting Jetpack Options for %s...\n", "jetpack" ),
- esc_url( get_site_url() )
- ) );
- sleep(1); // Take a breath
+ WP_CLI::line(
+ sprintf(
+ /* 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 );
- usleep( 100000 );
+ if ( ! $is_dry_run ) {
+ Jetpack_Options::delete_option( $option_to_reset );
+ usleep( 100000 );
+ }
+
/* 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 the WP options
- WP_CLI::line( __( "Resetting the jetpack options stored in wp_options...\n", "jetpack" ) );
+ 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 );
- usleep( 100000 );
+ if ( ! $is_dry_run ) {
+ delete_option( $option_to_reset );
+ usleep( 100000 );
+ }
/* 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" ) );
+ 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 );
+ if ( ! $is_dry_run ) {
+ Jetpack::update_active_modules( $default_modules );
+ }
WP_CLI::success( __( 'Modules reset to default.', 'jetpack' ) );
-
- // Jumpstart option is special
- Jetpack_Options::update_option( 'jumpstart', 'new_connection' );
- WP_CLI::success( __( 'jumpstart option reset', 'jetpack' ) );
break;
case 'modules':
- $default_modules = Jetpack::get_default_modules();
- Jetpack::update_active_modules( $default_modules );
+ if ( ! $is_dry_run ) {
+ $default_modules = Jetpack::get_default_modules();
+ Jetpack::update_active_modules( $default_modules );
+ }
+
WP_CLI::success( __( 'Modules reset to default.', 'jetpack' ) );
break;
case 'prompt':
- WP_CLI::error( __( 'Please specify if you would like to reset your options, or modules', 'jetpack' ) );
+ WP_CLI::error( __( 'Please specify if you would like to reset your options, modules or sync-checksum', 'jetpack' ) );
+ break;
+ case 'sync-checksum':
+ $option = 'jetpack_callables_sync_checksum';
+
+ if ( is_multisite() ) {
+ $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.
+ */
+ $sites = get_sites(
+ array(
+ 'number' => 1000,
+ 'offset' => $offset,
+ )
+ );
+ $count_fixes = 0;
+ foreach ( $sites as $site ) {
+ switch_to_blog( $site->blog_id );
+ $count = self::count_option( $option );
+ if ( $count > 1 ) {
+ if ( ! $is_dry_run ) {
+ delete_option( $option );
+ }
+ WP_CLI::line(
+ sprintf(
+ /* 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' ),
+ $count,
+ $option,
+ "{$site->domain}{$site->path}"
+ )
+ );
+ $count_fixes++;
+ if ( ! $is_dry_run ) {
+ /*
+ * We could be deleting a lot of options rows at the same time.
+ * Allow some time for replication to catch up.
+ */
+ sleep( 3 );
+ }
+ }
+
+ restore_current_blog();
+ }
+ if ( $count_fixes ) {
+ WP_CLI::success(
+ sprintf(
+ /* 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' ),
+ $option,
+ $count_fixes
+ )
+ );
+ } else {
+ WP_CLI::success( __( 'No options were deleted.', 'jetpack' ) );
+ }
+ return;
+ }
+
+ $count = self::count_option( $option );
+ if ( $count > 1 ) {
+ if ( ! $is_dry_run ) {
+ delete_option( $option );
+ }
+ WP_CLI::success(
+ sprintf(
+ /* translators: %1$d is a number, %2$s is the name of an option. */
+ __( 'Deleted %1$d %2$s options', 'jetpack' ),
+ $count,
+ $option
+ )
+ );
+ return;
+ }
+
+ WP_CLI::success( __( 'No options were deleted.', 'jetpack' ) );
break;
+
}
}
/**
+ * 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.
+ *
+ * @return int
+ */
+ private static function count_option( $option ) {
+ global $wpdb;
+ return (int) $wpdb->get_var(
+ $wpdb->prepare(
+ "SELECT COUNT(*) FROM $wpdb->options WHERE option_name = %s",
+ $option
+ )
+ );
+
+ }
+
+ /**
* Manage Jetpack Modules
*
* ## OPTIONS
@@ -397,8 +529,10 @@ class Jetpack_CLI extends WP_CLI_Command {
$module = Jetpack::get_module( $module_slug );
Jetpack::log( 'activate', $module_slug );
if ( Jetpack::activate_module( $module_slug, false, false ) ) {
+ /* translators: %s is the name of a Jetpack module */
WP_CLI::success( sprintf( __( '%s has been activated.', 'jetpack' ), $module['name'] ) );
} else {
+ /* translators: %s is the name of a Jetpack module */
WP_CLI::error( sprintf( __( '%s could not be activated.', 'jetpack' ), $module['name'] ) );
}
break;
@@ -411,6 +545,7 @@ class Jetpack_CLI extends WP_CLI_Command {
$module = Jetpack::get_module( $module_slug );
Jetpack::log( 'deactivate', $module_slug );
Jetpack::deactivate_module( $module_slug );
+ /* translators: %s is the name of a Jetpack module */
WP_CLI::success( sprintf( __( '%s has been deactivated.', 'jetpack' ), $module['name'] ) );
break;
case 'deactivate_all':
@@ -447,7 +582,8 @@ class Jetpack_CLI extends WP_CLI_Command {
}
// Check if module is active
if ( ! Jetpack::is_module_active( __FUNCTION__ ) ) {
- WP_CLI::error( sprintf( _x( '%s is not active. You can activate it with "wp jetpack module activate %s"', '"wp jetpack module activate" is a command - do not translate', 'jetpack' ), __FUNCTION__, __FUNCTION__ ) );
+ /* translators: %s is a module name */
+ WP_CLI::error( sprintf( _x( '%1$s is not active. You can activate it with "wp jetpack module activate %2$s"', '"wp jetpack module activate" is a command - do not translate', 'jetpack' ), __FUNCTION__, __FUNCTION__ ) );
}
if ( in_array( $action, array( 'whitelist' ) ) ) {
if ( isset( $args[1] ) ) {
@@ -465,7 +601,7 @@ class Jetpack_CLI extends WP_CLI_Command {
// Build array of IPs that are already whitelisted.
// Re-build manually instead of using jetpack_protect_format_whitelist() so we can easily get
// low & high range params for jetpack_protect_ip_address_is_in_range();
- foreach( $current_whitelist as $whitelisted ) {
+ foreach ( $current_whitelist as $whitelisted ) {
// IP ranges
if ( $whitelisted->range ) {
@@ -476,7 +612,7 @@ class Jetpack_CLI extends WP_CLI_Command {
WP_CLI::error( sprintf( __( '%s has already been whitelisted', 'jetpack' ), $new_ip ) );
break;
}
- $whitelist[] = $whitelisted->range_low . " - " . $whitelisted->range_high;
+ $whitelist[] = $whitelisted->range_low . ' - ' . $whitelisted->range_high;
} else { // Individual IPs
@@ -499,10 +635,10 @@ class Jetpack_CLI extends WP_CLI_Command {
if ( ! empty( $whitelist ) ) {
WP_CLI::success( __( 'Here are your whitelisted IPs:', 'jetpack' ) );
foreach ( $whitelist as $ip ) {
- WP_CLI::line( "\t" . str_pad( $ip, 24 ) ) ;
+ WP_CLI::line( "\t" . str_pad( $ip, 24 ) );
}
} else {
- WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ;
+ WP_CLI::line( __( 'Whitelist is empty.', 'jetpack' ) );
}
break;
}
@@ -516,7 +652,7 @@ class Jetpack_CLI extends WP_CLI_Command {
jetpack_protect_save_whitelist( $whitelist );
WP_CLI::success( __( 'Cleared all whitelisted IPs', 'jetpack' ) );
} else {
- WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ;
+ WP_CLI::line( __( 'Whitelist is empty.', 'jetpack' ) );
}
break;
}
@@ -527,7 +663,7 @@ class Jetpack_CLI extends WP_CLI_Command {
// Save whitelist if there are no errors
$result = jetpack_protect_save_whitelist( $whitelist );
if ( is_wp_error( $result ) ) {
- WP_CLI::error( __( $result, 'jetpack' ) );
+ WP_CLI::error( $result );
}
/* translators: %s is an IP address */
@@ -566,12 +702,9 @@ class Jetpack_CLI extends WP_CLI_Command {
* @synopsis <list|get|delete|update> [<option_name>] [<option_value>]
*/
public function options( $args, $assoc_args ) {
- $action = isset( $args[0] ) ? $args[0] : 'list';
+ $action = isset( $args[0] ) ? $args[0] : 'list';
$safe_to_modify = Jetpack_Options::get_options_for_reset();
- // Jumpstart is special
- array_push( $safe_to_modify, 'jumpstart' );
-
// Is the option flagged as unsafe?
$flagged = ! in_array( $args[1], $safe_to_modify );
@@ -583,9 +716,9 @@ class Jetpack_CLI extends WP_CLI_Command {
if ( isset( $args[0] ) ) {
if ( 'get' == $args[0] && isset( $args[1] ) ) {
$action = 'get';
- } else if ( 'delete' == $args[0] && isset( $args[1] ) ) {
+ } elseif ( 'delete' == $args[0] && isset( $args[1] ) ) {
$action = 'delete';
- } else if ( 'update' == $args[0] && isset( $args[1] ) ) {
+ } elseif ( 'update' == $args[0] && isset( $args[1] ) ) {
$action = 'update';
} else {
$action = 'list';
@@ -610,6 +743,7 @@ class Jetpack_CLI extends WP_CLI_Command {
jetpack_cli_are_you_sure( $flagged );
Jetpack_Options::delete_option( $args[1] );
+ /* translators: %s is the option name */
WP_CLI::success( sprintf( __( 'Deleted option: %s', 'jetpack' ), $args[1] ) );
break;
case 'update':
@@ -622,7 +756,8 @@ class Jetpack_CLI extends WP_CLI_Command {
}
Jetpack_Options::update_option( $args[1], $args[2] );
- WP_CLI::success( sprintf( _x( 'Updated option: %s to "%s"', 'Updating an option from "this" to "that".', 'jetpack' ), $args[1], $args[2] ) );
+ /* translators: %1$s is the previous value, %2$s is the new value */
+ WP_CLI::success( sprintf( _x( 'Updated option: %1$s to "%2$s"', 'Updating an option from "this" to "that".', 'jetpack' ), $args[1], $args[2] ) );
break;
case 'list':
$options_compact = Jetpack_Options::get_option_names();
@@ -645,7 +780,7 @@ class Jetpack_CLI extends WP_CLI_Command {
if ( ! is_array( $value ) ) {
WP_CLI::line( "\t" . str_pad( $option, 30 ) . $value );
- } else if ( is_array( $value ) ) {
+ } elseif ( is_array( $value ) ) {
WP_CLI::line( "\t" . str_pad( $option, 30 ) . 'Array - Use "get <option>" to read option array.' );
}
}
@@ -654,7 +789,7 @@ class Jetpack_CLI extends WP_CLI_Command {
WP_CLI::success(
_x( "Above are your options. You may 'get', 'delete', and 'update' them.", "'get', 'delete', and 'update' are commands - do not translate.", 'jetpack' ) . "\n" .
- str_pad( 'wp jetpack options get', 26 ) . $option_text . "\n" .
+ str_pad( 'wp jetpack options get', 26 ) . $option_text . "\n" .
str_pad( 'wp jetpack options delete', 26 ) . $option_text . "\n" .
str_pad( 'wp jetpack options update', 26 ) . "$option_text $value_text" . "\n" .
_x( "Type 'wp jetpack options' for more info.", "'wp jetpack options' is a command - do not translate.", 'jetpack' ) . "\n"
@@ -693,12 +828,12 @@ class Jetpack_CLI extends WP_CLI_Command {
switch ( $action ) {
case 'status':
- $status = Jetpack_Sync_Actions::get_sync_status();
+ $status = Actions::get_sync_status();
$collection = array();
foreach ( $status as $key => $item ) {
- $collection[] = array(
+ $collection[] = array(
'option' => $key,
- 'value' => is_scalar( $item ) ? $item : json_encode( $item )
+ 'value' => is_scalar( $item ) ? $item : json_encode( $item ),
);
}
WP_CLI::log( __( 'Sync Status:', 'jetpack' ) );
@@ -706,33 +841,36 @@ class Jetpack_CLI extends WP_CLI_Command {
break;
case 'settings':
WP_CLI::log( __( 'Sync Settings:', 'jetpack' ) );
- foreach( Jetpack_Sync_Settings::get_settings() as $setting => $item ) {
- $settings[] = array(
+ foreach ( Settings::get_settings() as $setting => $item ) {
+ $settings[] = array(
'setting' => $setting,
- 'value' => is_scalar( $item ) ? $item : json_encode( $item )
+ 'value' => is_scalar( $item ) ? $item : json_encode( $item ),
);
}
WP_CLI\Utils\format_items( 'table', $settings, array( 'setting', 'value' ) );
case 'disable':
- // Don't set it via the Jetpack_Sync_Settings since that also resets the queues.
+ // Don't set it via the Settings since that also resets the queues.
update_option( 'jetpack_sync_settings_disable', 1 );
+ /* translators: %s is the site URL */
WP_CLI::log( sprintf( __( 'Sync Disabled on %s', 'jetpack' ), get_site_url() ) );
break;
case 'enable':
- Jetpack_Sync_Settings::update_settings( array( 'disable' => 0 ) );
+ Settings::update_settings( array( 'disable' => 0 ) );
+ /* translators: %s is the site URL */
WP_CLI::log( sprintf( __( 'Sync Enabled on %s', 'jetpack' ), get_site_url() ) );
break;
case 'reset':
- // Don't set it via the Jetpack_Sync_Settings since that also resets the queues.
+ // Don't set it via the Settings since that also resets the queues.
update_option( 'jetpack_sync_settings_disable', 1 );
+ /* translators: %s is the site URL */
WP_CLI::log( sprintf( __( 'Sync Disabled on %s. Use `wp jetpack sync enable` to enable syncing again.', 'jetpack' ), get_site_url() ) );
- require_once dirname( __FILE__ ) . '/sync/class.jetpack-sync-listener.php';
- $listener = Jetpack_Sync_Listener::get_instance();
+ $listener = Listener::get_instance();
if ( empty( $assoc_args['queue'] ) ) {
$listener->get_sync_queue()->reset();
$listener->get_full_sync_queue()->reset();
+ /* translators: %s is the site URL */
WP_CLI::log( sprintf( __( 'Reset Full Sync and Regular Queues Queue on %s', 'jetpack' ), get_site_url() ) );
break;
}
@@ -741,10 +879,12 @@ class Jetpack_CLI extends WP_CLI_Command {
switch ( $assoc_args['queue'] ) {
case 'regular':
$listener->get_sync_queue()->reset();
+ /* translators: %s is the site URL */
WP_CLI::log( sprintf( __( 'Reset Regular Sync Queue on %s', 'jetpack' ), get_site_url() ) );
break;
case 'full':
$listener->get_full_sync_queue()->reset();
+ /* translators: %s is the site URL */
WP_CLI::log( sprintf( __( 'Reset Full Sync Queue on %s', 'jetpack' ), get_site_url() ) );
break;
default:
@@ -755,8 +895,8 @@ class Jetpack_CLI extends WP_CLI_Command {
break;
case 'start':
- if ( ! Jetpack_Sync_Actions::sync_allowed() ) {
- if( ! Jetpack_Sync_Settings::get_setting( 'disable' ) ) {
+ if ( ! Actions::sync_allowed() ) {
+ if ( ! Settings::get_setting( 'disable' ) ) {
WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site. It is currently disabled. Run `wp jetpack sync enable` to enable it.', 'jetpack' ) );
return;
}
@@ -764,30 +904,29 @@ class Jetpack_CLI extends WP_CLI_Command {
WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site. Jetpack is not connected.', 'jetpack' ) );
return;
}
- if ( Jetpack::is_development_mode() ) {
+ if ( ( new Status() )->is_development_mode() ) {
WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site. The site is in development mode.', 'jetpack' ) );
return;
}
- if ( Jetpack::is_staging_site() ) {
+ if ( Jetpack::is_staging_site() ) {
WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site. The site is in staging mode.', 'jetpack' ) );
return;
}
-
}
// Get the original settings so that we can restore them later
- $original_settings = Jetpack_Sync_Settings::get_settings();
+ $original_settings = Settings::get_settings();
// Initialize sync settigns so we can sync as quickly as possible
$sync_settings = wp_parse_args(
- array_intersect_key( $assoc_args, Jetpack_Sync_Settings::$valid_settings ),
+ array_intersect_key( $assoc_args, Settings::$valid_settings ),
array(
- 'sync_wait_time' => 0,
- 'enqueue_wait_time' => 0,
- 'queue_max_writes_sec' => 10000,
- 'max_queue_size_full_sync' => 100000
+ 'sync_wait_time' => 0,
+ 'enqueue_wait_time' => 0,
+ 'queue_max_writes_sec' => 10000,
+ 'max_queue_size_full_sync' => 100000,
)
);
- Jetpack_Sync_Settings::update_settings( $sync_settings );
+ Settings::update_settings( $sync_settings );
// Convert comma-delimited string of modules to an array
if ( ! empty( $assoc_args['modules'] ) ) {
@@ -804,7 +943,7 @@ class Jetpack_CLI extends WP_CLI_Command {
isset( $assoc_args[ $module_name ] ) &&
'initial' === $assoc_args[ $module_name ]
) {
- $modules[ 'users' ] = 'initial';
+ $modules['users'] = 'initial';
} elseif ( isset( $assoc_args[ $module_name ] ) ) {
$ids = explode( ',', $assoc_args[ $module_name ] );
if ( count( $ids ) > 0 ) {
@@ -818,8 +957,9 @@ class Jetpack_CLI extends WP_CLI_Command {
}
// Kick off a full sync
- if ( Jetpack_Sync_Actions::do_full_sync( $modules ) ) {
+ if ( Actions::do_full_sync( $modules ) ) {
if ( $modules ) {
+ /* translators: %s is a comma separated list of Jetpack modules */
WP_CLI::log( sprintf( __( 'Initialized a new full sync with modules: %s', 'jetpack' ), join( ', ', array_keys( $modules ) ) ) );
} else {
WP_CLI::log( __( 'Initialized a new full sync', 'jetpack' ) );
@@ -827,9 +967,10 @@ class Jetpack_CLI extends WP_CLI_Command {
} else {
// Reset sync settings to original.
- Jetpack_Sync_Settings::update_settings( $original_settings );
+ Settings::update_settings( $original_settings );
if ( $modules ) {
+ /* translators: %s is a comma separated list of Jetpack modules */
WP_CLI::error( sprintf( __( 'Could not start a new full sync with modules: %s', 'jetpack' ), join( ', ', $modules ) ) );
} else {
WP_CLI::error( __( 'Could not start a new full sync', 'jetpack' ) );
@@ -839,10 +980,11 @@ class Jetpack_CLI extends WP_CLI_Command {
// Keep sending to WPCOM until there's nothing to send
$i = 1;
do {
- $result = Jetpack_Sync_Actions::$sender->do_full_sync();
+ $result = Actions::$sender->do_full_sync();
if ( is_wp_error( $result ) ) {
$queue_empty_error = ( 'empty_queue_full_sync' == $result->get_error_code() );
if ( ! $queue_empty_error || ( $queue_empty_error && ( 1 == $i ) ) ) {
+ /* translators: %s is an error code */
WP_CLI::error( sprintf( __( 'Sync errored with code: %s', 'jetpack' ), $result->get_error_code() ) );
}
} else {
@@ -856,7 +998,7 @@ class Jetpack_CLI extends WP_CLI_Command {
} while ( $result && ! is_wp_error( $result ) );
// Reset sync settings to original.
- Jetpack_Sync_Settings::update_settings( $original_settings );
+ Settings::update_settings( $original_settings );
WP_CLI::success( __( 'Finished syncing to WordPress.com', 'jetpack' ) );
break;
@@ -877,30 +1019,29 @@ class Jetpack_CLI extends WP_CLI_Command {
* @synopsis <incremental|full_sync> <peek>
*/
public function sync_queue( $args, $assoc_args ) {
- if ( ! Jetpack_Sync_Actions::sync_allowed() ) {
+ if ( ! Actions::sync_allowed() ) {
WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site.', 'jetpack' ) );
}
$queue_name = isset( $args[0] ) ? $args[0] : 'sync';
- $action = isset( $args[1] ) ? $args[1] : 'peek';
+ $action = isset( $args[1] ) ? $args[1] : 'peek';
// We map the queue name that way we can support more friendly queue names in the commands, but still use
// the queue name that the code expects.
- $queue_name_map = $allowed_queues = array(
+ $queue_name_map = $allowed_queues = array(
'incremental' => 'sync',
'full' => 'full_sync',
);
$mapped_queue_name = isset( $queue_name_map[ $queue_name ] ) ? $queue_name_map[ $queue_name ] : $queue_name;
- switch( $action ) {
+ switch ( $action ) {
case 'peek':
- require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php';
- $queue = new Jetpack_Sync_Queue( $mapped_queue_name );
+ $queue = new Queue( $mapped_queue_name );
$items = $queue->peek( 100 );
if ( empty( $items ) ) {
/* translators: %s is the name of the queue, either 'incremental' or 'full' */
- WP_CLI::log( sprintf( __( 'Nothing is in the queue: %s', 'jetpack' ), $queue_name ) );
+ WP_CLI::log( sprintf( __( 'Nothing is in the queue: %s', 'jetpack' ), $queue_name ) );
} else {
$collection = array();
foreach ( $items as $item ) {
@@ -944,7 +1085,8 @@ class Jetpack_CLI extends WP_CLI_Command {
list( $token_json ) = $args;
if ( ! $token_json || ! ( $token = json_decode( $token_json ) ) ) {
- $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) );
+ /* translators: %s is the invalid JSON string */
+ $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) );
}
if ( isset( $token->error ) ) {
@@ -956,10 +1098,12 @@ class Jetpack_CLI extends WP_CLI_Command {
}
if ( Jetpack::validate_sync_error_idc_option() ) {
- $this->partner_provision_error( new WP_Error(
- 'site_in_safe_mode',
- esc_html__( 'Can not cancel a plan while in safe mode. See: https://jetpack.com/support/safe-mode/', 'jetpack' )
- ) );
+ $this->partner_provision_error(
+ new WP_Error(
+ 'site_in_safe_mode',
+ esc_html__( 'Can not cancel a plan while in safe mode. See: https://jetpack.com/support/safe-mode/', 'jetpack' )
+ )
+ );
}
$site_identifier = Jetpack_Options::get_option( 'id' );
@@ -970,7 +1114,7 @@ class Jetpack_CLI extends WP_CLI_Command {
$request = array(
'headers' => array(
- 'Authorization' => "Bearer " . $token->access_token,
+ 'Authorization' => 'Bearer ' . $token->access_token,
'Host' => 'public-api.wordpress.com',
),
'timeout' => 60,
@@ -982,7 +1126,7 @@ class Jetpack_CLI extends WP_CLI_Command {
$url = esc_url_raw( add_query_arg( 'partner_tracking_id', $named_args['partner_tracking_id'], $url ) );
}
- $result = Jetpack_Client::_wp_remote_request( $url, $request );
+ $result = Client::_wp_remote_request( $url, $request );
Jetpack_Options::delete_option( 'onboarding' );
@@ -1032,7 +1176,8 @@ class Jetpack_CLI extends WP_CLI_Command {
list( $token_json ) = $args;
if ( ! $token_json || ! ( $token = json_decode( $token_json ) ) ) {
- $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) );
+ /* translators: %s is the invalid JSON string */
+ $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) );
}
if ( isset( $token->error ) ) {
@@ -1051,11 +1196,15 @@ class Jetpack_CLI extends WP_CLI_Command {
$body_json = Jetpack_Provision::partner_provision( $token->access_token, $named_args );
if ( is_wp_error( $body_json ) ) {
- error_log( json_encode( array(
- 'success' => false,
- 'error_code' => $body_json->get_error_code(),
- 'error_message' => $body_json->get_error_message()
- ) ) );
+ error_log(
+ json_encode(
+ array(
+ 'success' => false,
+ 'error_code' => $body_json->get_error_code(),
+ 'error_message' => $body_json->get_error_message(),
+ )
+ )
+ );
exit( 1 );
}
@@ -1118,7 +1267,7 @@ class Jetpack_CLI extends WP_CLI_Command {
$is_master_user = ! Jetpack::is_active();
$current_user_id = get_current_user_id();
- Jetpack::update_user_token( $current_user_id, sprintf( '%s.%d', $named_args['token'], $current_user_id ), $is_master_user );
+ Connection_Utils::update_user_token( $current_user_id, sprintf( '%s.%d', $named_args['token'], $current_user_id ), $is_master_user );
WP_CLI::log( wp_json_encode( $named_args ) );
@@ -1197,31 +1346,35 @@ class Jetpack_CLI extends WP_CLI_Command {
? $named_args['resource']
: sprintf( $named_args['resource'], Jetpack_Options::get_option( 'id' ) );
- $response = Jetpack_Client::wpcom_json_api_request_as_blog(
+ $response = Client::wpcom_json_api_request_as_blog(
$resource_url,
- empty( $named_args['api_version'] ) ? Jetpack_Client::WPCOM_JSON_API_VERSION : $named_args['api_version'],
+ empty( $named_args['api_version'] ) ? Client::WPCOM_JSON_API_VERSION : $named_args['api_version'],
$other_args,
empty( $decoded_body ) ? null : $decoded_body,
empty( $named_args['base_api_path'] ) ? 'rest' : $named_args['base_api_path']
);
if ( is_wp_error( $response ) ) {
- WP_CLI::error( sprintf(
- /* translators: %1$s is an endpoint route (ex. /sites/123456), %2$d is an error code, %3$s is an error message. */
- __( 'Request to %1$s returned an error: (%2$d) %3$s.', 'jetpack' ),
- $resource_url,
- $response->get_error_code(),
- $response->get_error_message()
- ) );
+ WP_CLI::error(
+ sprintf(
+ /* translators: %1$s is an endpoint route (ex. /sites/123456), %2$d is an error code, %3$s is an error message. */
+ __( 'Request to %1$s returned an error: (%2$d) %3$s.', 'jetpack' ),
+ $resource_url,
+ $response->get_error_code(),
+ $response->get_error_message()
+ )
+ );
}
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
- WP_CLI::error( sprintf(
- /* translators: %1$s is an endpoint route (ex. /sites/123456), %2$d is an HTTP status code. */
- __( 'Request to %1$s returned a non-200 response code: %2$d.', 'jetpack' ),
- $resource_url,
- wp_remote_retrieve_response_code( $response )
- ) );
+ WP_CLI::error(
+ sprintf(
+ /* translators: %1$s is an endpoint route (ex. /sites/123456), %2$d is an HTTP status code. */
+ __( 'Request to %1$s returned a non-200 response code: %2$d.', 'jetpack' ),
+ $resource_url,
+ wp_remote_retrieve_response_code( $response )
+ )
+ );
}
$output = wp_remote_retrieve_body( $response );
@@ -1360,10 +1513,12 @@ class Jetpack_CLI extends WP_CLI_Command {
public function get_stats( $args, $named_args ) {
$selected_args = array_intersect_key(
$named_args,
- array_flip( array(
- 'quantity',
- 'date',
- ) )
+ array_flip(
+ array(
+ 'quantity',
+ 'date',
+ )
+ )
);
// The API expects unit, but period seems to be more correct.
@@ -1455,7 +1610,7 @@ class Jetpack_CLI extends WP_CLI_Command {
WP_CLI::error( __( 'The publicize module is not active.', 'jetpack' ) );
}
- if ( Jetpack::is_development_mode() ) {
+ if ( ( new Status() )->is_development_mode() ) {
if (
! defined( 'JETPACK_DEV_DEBUG' ) &&
! has_filter( 'jetpack_development_mode' ) &&
@@ -1588,11 +1743,13 @@ class Jetpack_CLI extends WP_CLI_Command {
foreach ( $connections as $id => $connection ) {
if ( false === $publicize->disconnect( false, $id ) ) {
- WP_CLI::error( sprintf(
- /* translators: %1$d is a numeric ID and %2$s is a lowercase string for a social network. */
- __( 'Publicize connection %d could not be disconnected', 'jetpack' ),
- $id
- ) );
+ WP_CLI::error(
+ sprintf(
+ /* translators: %1$d is a numeric ID and %2$s is a lowercase string for a social network. */
+ __( 'Publicize connection %d could not be disconnected', 'jetpack' ),
+ $id
+ )
+ );
}
$progress->tick();
@@ -1626,11 +1783,15 @@ class Jetpack_CLI extends WP_CLI_Command {
}
private function partner_provision_error( $error ) {
- WP_CLI::log( json_encode( array(
- 'success' => false,
- 'error_code' => $error->get_error_code(),
- 'error_message' => $error->get_error_message()
- ) ) );
+ WP_CLI::log(
+ json_encode(
+ array(
+ 'success' => false,
+ 'error_code' => $error->get_error_code(),
+ 'error_message' => $error->get_error_message(),
+ )
+ )
+ );
exit( 1 );
}
@@ -1668,6 +1829,7 @@ class Jetpack_CLI extends WP_CLI_Command {
$this->block( $args, $assoc_args );
break;
default:
+ /* translators: %s is the subcommand */
WP_CLI::error( sprintf( esc_html__( 'Invalid subcommand %s.', 'jetpack' ), $args[0] ) . ' 👻' );
exit( 1 );
}
@@ -1707,6 +1869,7 @@ class Jetpack_CLI extends WP_CLI_Command {
$path = JETPACK__PLUGIN_DIR . "extensions/blocks/$slug";
if ( $wp_filesystem->exists( $path ) && $wp_filesystem->is_dir( $path ) ) {
+ /* translators: %s is path to the conflicting block */
WP_CLI::error( sprintf( esc_html__( 'Name conflicts with the existing block %s', 'jetpack' ), $path ) . ' ⛔️' );
exit( 1 );
}
@@ -1716,34 +1879,50 @@ class Jetpack_CLI extends WP_CLI_Command {
$hasKeywords = isset( $assoc_args['keywords'] );
$files = array(
- "$path/$slug.php" => $this->render_block_file( 'block-register-php', array(
- 'slug' => $slug,
- 'title' => $title,
- 'underscoredSlug' => str_replace( '-', '_', $slug ),
- ) ),
- "$path/index.js" => $this->render_block_file( 'block-index-js', array(
- 'slug' => $slug,
- 'title' => $title,
- 'description' => isset( $assoc_args['description'] )
- ? $assoc_args['description']
- : $title,
- 'keywords' => $hasKeywords
- ? array_map( function( $keyword ) {
- // Construction necessary for Mustache lists
- return array( 'keyword' => trim( $keyword ) );
- }, explode( ',', $assoc_args['keywords'], 3 ) )
+ "$path/$slug.php" => $this->render_block_file(
+ 'block-register-php',
+ array(
+ 'slug' => $slug,
+ 'title' => $title,
+ 'underscoredSlug' => str_replace( '-', '_', $slug ),
+ 'jetpackVersion' => substr( JETPACK__VERSION, 0, strpos( JETPACK__VERSION, '.' ) ) . '.x',
+ )
+ ),
+ "$path/index.js" => $this->render_block_file(
+ 'block-index-js',
+ array(
+ 'slug' => $slug,
+ 'title' => $title,
+ 'description' => isset( $assoc_args['description'] )
+ ? $assoc_args['description']
+ : $title,
+ 'keywords' => $hasKeywords
+ ? array_map(
+ function( $keyword ) {
+ // Construction necessary for Mustache lists
+ return array( 'keyword' => trim( $keyword ) );
+ },
+ explode( ',', $assoc_args['keywords'], 3 )
+ )
: '',
- 'hasKeywords' => $hasKeywords
- ) ),
- "$path/editor.js" => $this->render_block_file( 'block-editor-js' ),
- "$path/editor.scss" => $this->render_block_file( 'block-editor-scss', array(
- 'slug' => $slug,
- 'title' => $title,
- ) ),
- "$path/edit.js" => $this->render_block_file( 'block-edit-js', array(
- 'title' => $title,
- 'className' => str_replace( ' ', '', ucwords( str_replace( '-', ' ', $slug ) ) ),
- ) )
+ 'hasKeywords' => $hasKeywords,
+ )
+ ),
+ "$path/editor.js" => $this->render_block_file( 'block-editor-js' ),
+ "$path/editor.scss" => $this->render_block_file(
+ 'block-editor-scss',
+ array(
+ 'slug' => $slug,
+ 'title' => $title,
+ )
+ ),
+ "$path/edit.js" => $this->render_block_file(
+ 'block-edit-js',
+ array(
+ 'title' => $title,
+ 'className' => str_replace( ' ', '', ucwords( str_replace( '-', ' ', $slug ) ) ),
+ )
+ ),
);
$files_written = array();
@@ -1752,6 +1931,7 @@ class Jetpack_CLI extends WP_CLI_Command {
if ( $wp_filesystem->put_contents( $filename, $contents ) ) {
$files_written[] = $filename;
} else {
+ /* translators: %s is a file name */
WP_CLI::error( sprintf( esc_html__( 'Error creating %s', 'jetpack' ), $filename ) );
}
}
@@ -1761,35 +1941,52 @@ class Jetpack_CLI extends WP_CLI_Command {
} else {
// Load index.json and insert the slug of the new block in the production array
$block_list_path = JETPACK__PLUGIN_DIR . 'extensions/index.json';
- $block_list = $wp_filesystem->get_contents( $block_list_path );
+ $block_list = $wp_filesystem->get_contents( $block_list_path );
if ( empty( $block_list ) ) {
+ /* translators: %s is the path to the file with the block list */
WP_CLI::error( sprintf( esc_html__( 'Error fetching contents of %s', 'jetpack' ), $block_list_path ) );
- } else if ( false === stripos( $block_list, $slug ) ) {
- $new_block_list = json_decode( $block_list );
+ } elseif ( false === stripos( $block_list, $slug ) ) {
+ $new_block_list = json_decode( $block_list );
$new_block_list->beta[] = $slug;
- if ( ! $wp_filesystem->put_contents( $block_list_path, wp_json_encode( $new_block_list ) ) ) {
+
+ // Format the JSON to match our coding standards.
+ $new_block_list_formatted = wp_json_encode( $new_block_list, JSON_PRETTY_PRINT ) . "\n";
+ $new_block_list_formatted = preg_replace_callback(
+ // Find all occurrences of multiples of 4 spaces a the start of the line.
+ '/^((?: )+)/m',
+ function ( $matches ) {
+ // Replace each occurrence of 4 spaces with a tab character.
+ return str_repeat( "\t", substr_count( $matches[0], ' ' ) );
+ },
+ $new_block_list_formatted
+ );
+
+ if ( ! $wp_filesystem->put_contents( $block_list_path, $new_block_list_formatted ) ) {
+ /* translators: %s is the path to the file with the block list */
WP_CLI::error( sprintf( esc_html__( 'Error writing new %s', 'jetpack' ), $block_list_path ) );
}
}
- WP_CLI::success( sprintf(
- /* translators: the placeholders are a human readable title, and a series of words separated by dashes */
- esc_html__( 'Successfully created block %s with slug %s', 'jetpack' ) . ' 🎉' . "\n" .
- "--------------------------------------------------------------------------------------------------------------------\n" .
- /* translators: the placeholder is a directory path */
- esc_html__( 'The files were created at %s', 'jetpack' ) . "\n" .
- esc_html__( 'To start using the block, build the blocks with yarn run build-extensions', 'jetpack' ) . "\n" .
- /* translators: the placeholder is a file path */
- esc_html__( 'The block slug has been added to the beta list at %s', 'jetpack' ) . "\n" .
- esc_html__( 'To load the block, add the constant JETPACK_BETA_BLOCKS as true to your wp-config.php file', 'jetpack' ) . "\n" .
- /* translators: the placeholder is a URL */
- "\n" . esc_html__( 'Read more at %s', 'jetpack' ) . "\n",
- $title,
- $slug,
- $path,
- $block_list_path,
- 'https://github.com/Automattic/jetpack/blob/master/extensions/README.md#develop-new-blocks'
- ) . '--------------------------------------------------------------------------------------------------------------------' );
+ WP_CLI::success(
+ sprintf(
+ /* translators: the placeholders are a human readable title, and a series of words separated by dashes */
+ esc_html__( 'Successfully created block %1$s with slug %2$s', 'jetpack' ) . ' 🎉' . "\n" .
+ "--------------------------------------------------------------------------------------------------------------------\n" .
+ /* translators: the placeholder is a directory path */
+ esc_html__( 'The files were created at %s', 'jetpack' ) . "\n" .
+ esc_html__( 'To start using the block, build the blocks with yarn run build-extensions', 'jetpack' ) . "\n" .
+ /* translators: the placeholder is a file path */
+ esc_html__( 'The block slug has been added to the beta list at %s', 'jetpack' ) . "\n" .
+ esc_html__( 'To load the block, add the constant JETPACK_BETA_BLOCKS as true to your wp-config.php file', 'jetpack' ) . "\n" .
+ /* translators: the placeholder is a URL */
+ "\n" . esc_html__( 'Read more at %s', 'jetpack' ) . "\n",
+ $title,
+ $slug,
+ $path,
+ $block_list_path,
+ 'https://github.com/Automattic/jetpack/blob/master/extensions/README.md#develop-new-blocks'
+ ) . '--------------------------------------------------------------------------------------------------------------------'
+ );
}
}
@@ -1797,7 +1994,7 @@ class Jetpack_CLI extends WP_CLI_Command {
* Built the file replacing the placeholders in the template with the data supplied.
*
* @param string $template
- * @param array $data
+ * @param array $data
*
* @return string mixed
*/
@@ -1825,7 +2022,7 @@ function jetpack_cli_are_you_sure( $flagged = false, $error_msg = false ) {
. ' '
. $cli->green_open
. 'jetpack.com/support'
- . $cli->color_close;
+ . $cli->color_close;
}
if ( ! $flagged ) {
@@ -1835,9 +2032,9 @@ function jetpack_cli_are_you_sure( $flagged = false, $error_msg = false ) {
}
WP_CLI::line( $prompt_message );
- $handle = fopen( "php://stdin", "r" );
- $line = fgets( $handle );
- if ( 'yes' != trim( $line ) ){
+ $handle = fopen( 'php://stdin', 'r' );
+ $line = fgets( $handle );
+ if ( 'yes' != trim( $line ) ) {
WP_CLI::error( $error_msg );
}
}