PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.2
Jetpack – WP Security, Backup, Speed, & Growth v16.2
16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 All 502 releases
← All changes | class.jetpack-cli.php +120 -89 13.7.216.2 View file →
@@ -9,8 +9,9 @@
9 9 use Automattic\Jetpack\Connection\Manager as Connection_Manager;
10 10 use Automattic\Jetpack\Connection\Tokens;
11 11 use Automattic\Jetpack\Identity_Crisis;
12 12 use Automattic\Jetpack\IP\Utils as IP_Utils;
13 +use Automattic\Jetpack\Publicize\Connections;
13 14 use Automattic\Jetpack\Publicize\Publicize;
14 15 use Automattic\Jetpack\Status;
15 16 use Automattic\Jetpack\Sync\Actions;
16 17 use Automattic\Jetpack\Sync\Listener;
@@ -22,9 +23,8 @@
22 23 if ( ! class_exists( 'WP_CLI_Command' ) ) {
23 24 return;
24 25 }
25 26
26 -// @phan-suppress-next-line PhanUndeclaredFunctionInCallable -- https://github.com/phan/phan/issues/4763
27 27 WP_CLI::add_command( 'jetpack', 'Jetpack_CLI' );
28 28
29 29 /**
30 30 * Control your local Jetpack installation.
@@ -86,9 +86,9 @@
86 86 }
87 87
88 88 $master_user_email = Jetpack::get_master_user_email();
89 89
90 - $cxntests = new Jetpack_Cxn_Tests();
90 + $cxntests = new Automattic\Jetpack\Connection\Connection_Health_Tests();
91 91
92 92 if ( $cxntests->pass() ) {
93 93 $cxntests->output_results_for_cli();
94 94
@@ -231,9 +231,9 @@
231 231 WP_CLI::success( __( 'The site is not currently connected, so nothing to do!', 'jetpack' ) );
232 232 return;
233 233 }
234 234
235 - $action = isset( $args[0] ) ? $args[0] : 'prompt';
235 + $action = $args[0] ?? 'prompt';
236 236 if ( ! in_array( $action, array( 'blog', 'user', 'prompt' ), true ) ) {
237 237 /* translators: %s is a command like "prompt" */
238 238 WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
239 239 }
@@ -325,9 +325,9 @@
325 325 * @param array $args Positional args.
326 326 * @param array $assoc_args Named args.
327 327 */
328 328 public function reset( $args, $assoc_args ) {
329 - $action = isset( $args[0] ) ? $args[0] : 'prompt';
329 + $action = $args[0] ?? 'prompt';
330 330 if ( ! in_array( $action, array( 'options', 'modules', 'sync-checksum' ), true ) ) {
331 331 /* translators: %s is a command like "prompt" */
332 332 WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
333 333 }
@@ -546,9 +546,9 @@
546 546 * @param array $assoc_args Named args.
547 547 */
548 548 public function module( $args, $assoc_args ) {
549 549 $module_slug = null;
550 - $action = isset( $args[0] ) ? $args[0] : 'list';
550 + $action = $args[0] ?? 'list';
551 551
552 552 if ( isset( $args[1] ) ) {
553 553 $module_slug = $args[1];
554 554 if ( 'all' !== $module_slug && ! Jetpack::is_module( $module_slug ) ) {
@@ -639,9 +639,9 @@
639 639 *
640 640 * @param array $args Positional args.
641 641 */
642 642 public function protect( $args ) {
643 - $action = isset( $args[0] ) ? $args[0] : 'prompt';
643 + $action = $args[0] ?? 'prompt';
644 644 if ( ! in_array( $action, array( 'whitelist', 'allow' ), true ) ) { // Still allow "whitelist" for legacy support.
645 645 /* translators: %s is a command like "prompt" */
646 646 WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
647 647 }
@@ -768,9 +768,9 @@
768 768 *
769 769 * @param array $args Positional args.
770 770 */
771 771 public function options( $args ) {
772 - $action = isset( $args[0] ) ? $args[0] : 'list';
772 + $action = $args[0] ?? 'list';
773 773 $safe_to_modify = Jetpack_Options::get_options_for_reset();
774 774
775 775 // Is the option flagged as unsafe?
776 776 $flagged = ! in_array( $args[1], $safe_to_modify, true );
@@ -894,9 +894,9 @@
894 894 * @param array $assoc_args Named args.
895 895 */
896 896 public function sync( $args, $assoc_args ) {
897 897
898 - $action = isset( $args[0] ) ? $args[0] : 'status';
898 + $action = $args[0] ?? 'status';
899 899
900 900 switch ( $action ) {
901 901 case 'status':
902 902 $status = Actions::get_sync_status();
@@ -903,9 +903,9 @@
903 903 $collection = array();
904 904 foreach ( $status as $key => $item ) {
905 905 $collection[] = array(
906 906 'option' => $key,
907 - 'value' => is_scalar( $item ) ? $item : wp_json_encode( $item ),
907 + 'value' => is_scalar( $item ) ? $item : wp_json_encode( $item, JSON_UNESCAPED_SLASHES ),
908 908 );
909 909 }
910 910 WP_CLI::log( __( 'Sync Status:', 'jetpack' ) );
911 911 WP_CLI\Utils\format_items( 'table', $collection, array( 'option', 'value' ) );
@@ -915,9 +915,9 @@
915 915 $settings = array();
916 916 foreach ( Settings::get_settings() as $setting => $item ) {
917 917 $settings[] = array(
918 918 'setting' => $setting,
919 - 'value' => is_scalar( $item ) ? $item : wp_json_encode( $item ),
919 + 'value' => is_scalar( $item ) ? $item : wp_json_encode( $item, JSON_UNESCAPED_SLASHES ),
920 920 );
921 921 }
922 922 WP_CLI\Utils\format_items( 'table', $settings, array( 'setting', 'value' ) );
923 923 break;
@@ -1035,11 +1035,11 @@
1035 1035 $modules = null;
1036 1036 }
1037 1037
1038 1038 // Kick off a full sync.
1039 - if ( Actions::do_full_sync( $modules ) ) {
1039 + if ( Actions::do_full_sync( $modules, 'jetpack_cli' ) ) {
1040 1040 if ( $modules ) {
1041 - /* translators: %s is a comma separated list of Jetpack modules */
1041 + /* translators: %s is a comma-separated list of Jetpack modules */
1042 1042 WP_CLI::log( sprintf( __( 'Initialized a new full sync with modules: %s', 'jetpack' ), implode( ', ', array_keys( $modules ) ) ) );
1043 1043 } else {
1044 1044 WP_CLI::log( __( 'Initialized a new full sync', 'jetpack' ) );
1045 1045 }
@@ -1048,9 +1048,9 @@
1048 1048 // Reset sync settings to original.
1049 1049 Settings::update_settings( $original_settings );
1050 1050
1051 1051 if ( $modules ) {
1052 - /* translators: %s is a comma separated list of Jetpack modules */
1052 + /* translators: %s is a comma-separated list of Jetpack modules */
1053 1053 WP_CLI::error( sprintf( __( 'Could not start a new full sync with modules: %s', 'jetpack' ), implode( ', ', $modules ) ) );
1054 1054 } else {
1055 1055 WP_CLI::error( __( 'Could not start a new full sync', 'jetpack' ) );
1056 1056 }
@@ -1108,10 +1108,10 @@
1108 1108 if ( ! Actions::sync_allowed() ) {
1109 1109 WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site.', 'jetpack' ) );
1110 1110 }
1111 1111
1112 - $queue_name = isset( $args[0] ) ? $args[0] : 'sync';
1113 - $action = isset( $args[1] ) ? $args[1] : 'peek';
1112 + $queue_name = $args[0] ?? 'sync';
1113 + $action = $args[1] ?? 'peek';
1114 1114
1115 1115 // We map the queue name that way we can support more friendly queue names in the commands, but still use
1116 1116 // the queue name that the code expects.
1117 1117 $allowed_queues = array(
@@ -1118,9 +1118,9 @@
1118 1118 'incremental' => 'sync',
1119 1119 'full' => 'full_sync',
1120 1120 );
1121 1121 $queue_name_map = $allowed_queues;
1122 - $mapped_queue_name = isset( $queue_name_map[ $queue_name ] ) ? $queue_name_map[ $queue_name ] : $queue_name;
1122 + $mapped_queue_name = $queue_name_map[ $queue_name ] ?? $queue_name;
1123 1123
1124 1124 switch ( $action ) {
1125 1125 case 'peek':
1126 1126 $queue = new Queue( $mapped_queue_name );
@@ -1133,9 +1133,9 @@
1133 1133 $collection = array();
1134 1134 foreach ( $items as $item ) {
1135 1135 $collection[] = array(
1136 1136 'action' => $item[0],
1137 - 'args' => wp_json_encode( $item[1] ),
1137 + 'args' => wp_json_encode( $item[1], JSON_UNESCAPED_SLASHES ),
1138 1138 'current_user_id' => $item[2],
1139 1139 'microtime' => $item[3],
1140 1140 'importing' => (string) $item[4],
1141 1141 );
@@ -1191,9 +1191,9 @@
1191 1191 if ( Identity_Crisis::validate_sync_error_idc_option() ) {
1192 1192 $this->partner_provision_error(
1193 1193 new WP_Error(
1194 1194 'site_in_safe_mode',
1195 - esc_html__( 'Can not cancel a plan while in safe mode. See: https://jetpack.com/support/safe-mode/', 'jetpack' )
1195 + esc_html__( 'Cannot cancel a plan while in safe mode. See: https://jetpack.com/support/safe-mode/', 'jetpack' )
1196 1196 )
1197 1197 );
1198 1198 }
1199 1199
@@ -1217,12 +1217,11 @@
1217 1217 if ( ! empty( $named_args ) && ! empty( $named_args['partner_tracking_id'] ) ) {
1218 1218 $url = esc_url_raw( add_query_arg( 'partner_tracking_id', $named_args['partner_tracking_id'], $url ) );
1219 1219 }
1220 1220
1221 + // @phan-suppress-next-line PhanAccessMethodInternal -- Phan is correct, but the usage is intentional.
1221 1222 $result = Client::_wp_remote_request( $url, $request );
1222 1223
1223 - Jetpack_Options::delete_option( 'onboarding' );
1224 -
1225 1224 if ( is_wp_error( $result ) ) {
1226 1225 $this->partner_provision_error( $result );
1227 1226 }
1228 1227
@@ -1243,10 +1242,8 @@
1243 1242 * [--wpcom_user_id=<user_id>]
1244 1243 * : WordPress.com ID of user to connect as (must be whitelisted against partner key)
1245 1244 * [--wpcom_user_email=<wpcom_user_email>]
1246 1245 * : Override the email we send to WordPress.com for registration
1247 - * [--onboarding=<onboarding>]
1248 - * : Guide the user through an onboarding wizard
1249 1246 * [--force_register=<register>]
1250 1247 * : Whether to force a site to register
1251 1248 * [--force_connect=<force_connect>]
1252 1249 * : Force JPS to not reuse existing credentials
@@ -1261,9 +1258,9 @@
1261 1258 *
1262 1259 * $ wp jetpack partner_provision '{ some: "json" }' premium 1
1263 1260 * { success: true }
1264 1261 *
1265 - * @synopsis <token_json> [--wpcom_user_id=<user_id>] [--plan=<plan_name>] [--onboarding=<onboarding>] [--force_register=<register>] [--force_connect=<force_connect>] [--home_url=<home_url>] [--site_url=<site_url>] [--wpcom_user_email=<wpcom_user_email>] [--partner_tracking_id=<partner_tracking_id>]
1262 + * @synopsis <token_json> [--wpcom_user_id=<user_id>] [--plan=<plan_name>] [--force_register=<register>] [--force_connect=<force_connect>] [--home_url=<home_url>] [--site_url=<site_url>] [--wpcom_user_email=<wpcom_user_email>] [--partner_tracking_id=<partner_tracking_id>]
1266 1263 *
1267 1264 * @param array $args Positional args.
1268 1265 * @param array $named_args Named args.
1269 1266 */
@@ -1276,11 +1273,9 @@
1276 1273 $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) );
1277 1274 }
1278 1275
1279 1276 if ( isset( $token->error ) ) {
1280 - $message = isset( $token->message )
1281 - ? $token->message
1282 - : '';
1277 + $message = $token->message ?? '';
1283 1278 $this->partner_provision_error( new WP_Error( $token->error, $message ) );
1284 1279 }
1285 1280
1286 1281 if ( ! isset( $token->access_token ) ) {
@@ -1297,15 +1292,16 @@
1297 1292 array(
1298 1293 'success' => false,
1299 1294 'error_code' => $body_json->get_error_code(),
1300 1295 'error_message' => $body_json->get_error_message(),
1301 - )
1296 + ),
1297 + JSON_UNESCAPED_SLASHES
1302 1298 )
1303 1299 );
1304 1300 exit( 1 );
1305 1301 }
1306 1302
1307 - WP_CLI::log( wp_json_encode( $body_json ) );
1303 + WP_CLI::log( wp_json_encode( $body_json, JSON_UNESCAPED_SLASHES ) );
1308 1304 }
1309 1305
1310 1306 /**
1311 1307 * Manages your Jetpack sitemap
@@ -1313,23 +1309,23 @@
1313 1309 * ## OPTIONS
1314 1310 *
1315 1311 * rebuild : Rebuild all sitemaps
1316 1312 * --purge : if set, will remove all existing sitemap data before rebuilding
1313 + * --monitor : if set, will output elapsed time, peak memory usage, CPU time (user/system), and average CPU utilization
1314 + * --suspend-cache-addition : if set, will suspend cache additions during sitemap generation
1317 1315 *
1318 1316 * ## EXAMPLES
1319 1317 *
1320 1318 * wp jetpack sitemap rebuild
1319 + * wp jetpack sitemap rebuild --monitor
1321 1320 *
1322 1321 * @subcommand sitemap
1323 - * @synopsis <rebuild> [--purge]
1322 + * @synopsis <rebuild> [--purge] [--monitor] [--suspend-cache-addition]
1324 1323 *
1325 1324 * @param array $args Positional args.
1326 1325 * @param array $assoc_args Named args.
1327 1326 */
1328 1327 public function sitemap( $args, $assoc_args ) {
1329 - if ( ! Jetpack::is_connection_ready() ) {
1330 - WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
1331 - }
1332 1328 if ( ! Jetpack::is_module_active( 'sitemaps' ) ) {
1333 1329 WP_CLI::error( __( 'Jetpack Sitemaps module is not currently active. Activate it first if you want to work with sitemaps.', 'jetpack' ) );
1334 1330 }
1335 1331 if ( ! class_exists( 'Jetpack_Sitemap_Builder' ) ) {
@@ -1335,15 +1331,65 @@
1335 1331 if ( ! class_exists( 'Jetpack_Sitemap_Builder' ) ) {
1336 1332 WP_CLI::error( __( 'Jetpack Sitemaps module is active, but unavailable. This can happen if your site is set to discourage search engine indexing. Please enable search engine indexing to allow sitemap generation.', 'jetpack' ) );
1337 1333 }
1338 1334
1335 + if ( isset( $assoc_args['suspend-cache-addition'] ) && $assoc_args['suspend-cache-addition'] ) {
1336 + add_filter( 'jetpack_sitemap_suspend_cache_addition', '__return_true' );
1337 + WP_CLI::success( 'Suspending cache addition.' );
1338 + }
1339 +
1340 + $monitor = isset( $assoc_args['monitor'] ) && $assoc_args['monitor'];
1341 +
1342 + if ( $monitor ) {
1343 + $start_time = microtime( true );
1344 + $rusage_start = function_exists( 'getrusage' ) ? getrusage() : null;
1345 + }
1346 +
1339 1347 if ( isset( $assoc_args['purge'] ) && $assoc_args['purge'] ) {
1340 1348 $librarian = new Jetpack_Sitemap_Librarian();
1341 1349 $librarian->delete_all_stored_sitemap_data();
1350 +
1351 + // Clear sitemap-related transients
1352 + delete_transient( 'jetpack_news_sitemap_xml' );
1353 + delete_transient( 'jetpack-sitemap-state-lock' );
1354 + WP_CLI::success( __( 'Purged all sitemap data and cleared sitemap transients.', 'jetpack' ) );
1342 1355 }
1343 1356
1344 1357 $sitemap_builder = new Jetpack_Sitemap_Builder();
1345 1358 $sitemap_builder->update_sitemap();
1359 +
1360 + WP_CLI::success( __( 'Sitemap rebuilt successfully.', 'jetpack' ) );
1361 +
1362 + if ( $monitor && isset( $start_time ) ) {
1363 + $end_time = microtime( true );
1364 + $peak_memory = memory_get_peak_usage();
1365 + $elapsed_time = $end_time - $start_time;
1366 + $rusage_end = function_exists( 'getrusage' ) ? getrusage() : null;
1367 +
1368 + WP_CLI::log( '----------------------------------' );
1369 + WP_CLI::log( __( 'Performance Metrics:', 'jetpack' ) );
1370 + /* translators: %s is a float representing seconds */
1371 + WP_CLI::log( sprintf( __( 'Elapsed Time: %.4f seconds', 'jetpack' ), $elapsed_time ) );
1372 + /* translators: %s is a human-readable memory size (e.g., 128MB) */
1373 + WP_CLI::log( sprintf( __( 'Peak Memory Usage: %s', 'jetpack' ), size_format( $peak_memory ) ) );
1374 +
1375 + if ( ! empty( $rusage_start ) && ! empty( $rusage_end ) ) {
1376 + $user_cpu_time = ( $rusage_end['ru_utime.tv_sec'] * 1e6 + $rusage_end['ru_utime.tv_usec'] ) - ( $rusage_start['ru_utime.tv_sec'] * 1e6 + $rusage_start['ru_utime.tv_usec'] );
1377 + $system_cpu_time = ( $rusage_end['ru_stime.tv_sec'] * 1e6 + $rusage_end['ru_stime.tv_usec'] ) - ( $rusage_start['ru_stime.tv_sec'] * 1e6 + $rusage_start['ru_stime.tv_usec'] );
1378 +
1379 + /* translators: %d is an integer representing microseconds */
1380 + WP_CLI::log( sprintf( __( 'CPU time (user): %d microseconds', 'jetpack' ), $user_cpu_time ) );
1381 + /* translators: %d is an integer representing microseconds */
1382 + WP_CLI::log( sprintf( __( 'CPU time (system): %d microseconds', 'jetpack' ), $system_cpu_time ) );
1383 +
1384 + // Average CPU utilization over the elapsed wall time.
1385 + $total_cpu_sec = ( $user_cpu_time + $system_cpu_time ) / 1e6;
1386 + $avg_cpu_pct = $elapsed_time > 0 ? ( $total_cpu_sec / $elapsed_time ) * 100 : 0.0;
1387 + /* translators: %s is a percentage like 83.4 */
1388 + WP_CLI::log( sprintf( __( 'Average CPU Utilization: %.1f%%', 'jetpack' ), $avg_cpu_pct ) );
1389 + }
1390 + WP_CLI::log( '----------------------------------' );
1391 + }
1346 1392 }
1347 1393
1348 1394 /**
1349 1395 * Allows authorizing a user via the command line and will activate
@@ -1370,9 +1416,9 @@
1370 1416 $current_user_id = get_current_user_id();
1371 1417
1372 1418 ( new Tokens() )->update_user_token( $current_user_id, sprintf( '%s.%d', $named_args['token'], $current_user_id ), $is_connection_owner );
1373 1419
1374 - WP_CLI::log( wp_json_encode( $named_args ) );
1420 + WP_CLI::log( wp_json_encode( $named_args, JSON_UNESCAPED_SLASHES ) );
1375 1421
1376 1422 if ( $is_connection_owner ) {
1377 1423 /**
1378 1424 * Auto-enable SSO module for new Jetpack Start connections
@@ -1485,9 +1531,9 @@
1485 1531 $output = wp_remote_retrieve_body( $response );
1486 1532 if ( isset( $named_args['pretty'] ) ) {
1487 1533 $decoded_output = json_decode( $output );
1488 1534 if ( $decoded_output ) {
1489 - $output = wp_json_encode( $decoded_output, JSON_PRETTY_PRINT );
1535 + $output = wp_json_encode( $decoded_output, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT );
1490 1536 }
1491 1537 }
1492 1538
1493 1539 if ( isset( $named_args['strip-success'] ) ) {
@@ -1573,9 +1619,9 @@
1573 1619 array(
1574 1620 'resource' => '/activity-log/%d/update-credentials',
1575 1621 'method' => 'POST',
1576 1622 'api_version' => '1.1',
1577 - 'body' => wp_json_encode( $values ),
1623 + 'body' => wp_json_encode( $values, JSON_UNESCAPED_SLASHES ),
1578 1624 'timeout' => 30,
1579 1625 ),
1580 1626 $named_args
1581 1627 );
@@ -1672,8 +1718,11 @@
1672 1718 *
1673 1719 * [<identifier>]
1674 1720 * : The connection ID or service to perform an action on.
1675 1721 *
1722 + * [--ignore-cache]
1723 + * : Whether to ignore connections cache.
1724 + *
1676 1725 * [--format=<format>]
1677 1726 * : Allows overriding the output of the command when listing connections.
1678 1727 * ---
1679 1728 * default: table
@@ -1690,8 +1739,11 @@
1690 1739 *
1691 1740 * # List all publicize connections.
1692 1741 * $ wp jetpack publicize list
1693 1742 *
1743 + * # List all publicize connections, ignoring the cache.
1744 + * $ wp jetpack publicize list --ignore-cache
1745 + *
1694 1746 * # List publicize connections for a given service.
1695 1747 * $ wp jetpack publicize list linkedin
1696 1748 *
1697 1749 * # List all publicize connections for a given user.
@@ -1726,9 +1778,8 @@
1726 1778
1727 1779 if ( ( new Status() )->is_offline_mode() ) {
1728 1780 if (
1729 1781 ! defined( 'JETPACK_DEV_DEBUG' ) &&
1730 - ! has_filter( 'jetpack_development_mode' ) &&
1731 1782 ! has_filter( 'jetpack_offline_mode' ) &&
1732 1783 ! str_contains( site_url(), '.' )
1733 1784 ) {
1734 1785 WP_CLI::error( __( "Jetpack is current in offline mode because the site url does not contain a '.', which often occurs when dynamically setting the WP_SITEURL constant. While in offline mode, the Jetpack Social module will not load.", 'jetpack' ) );
@@ -1748,30 +1799,23 @@
1748 1799 $id_is_service = in_array( $identifier, $services, true );
1749 1800
1750 1801 switch ( $action ) {
1751 1802 case 'list':
1752 - $connections_to_return = array();
1753 -
1803 + $_args = array(
1804 + 'ignore_cache' => $named_args['ignore-cache'] ?? false,
1805 + );
1754 1806 // For the CLI command, let's return all connections when a user isn't specified. This
1755 1807 // differs from the logic in the Publicize class.
1756 - $option_connections = is_user_logged_in()
1757 - ? (array) $publicize->get_all_connections_for_user()
1758 - : (array) $publicize->get_all_connections();
1808 + $connections_to_return = is_user_logged_in()
1809 + ? Connections::get_all_for_user( $_args )
1810 + : Connections::get_all( $_args );
1759 1811
1760 - foreach ( $option_connections as $service_name => $connections ) {
1761 - foreach ( (array) $connections as $id => $connection ) {
1762 - $connection['id'] = $id;
1763 - $connection['service'] = $service_name;
1764 - $connections_to_return[] = $connection;
1765 - }
1766 - }
1767 -
1768 1812 if ( $id_is_service && ! empty( $identifier ) && ! empty( $connections_to_return ) ) {
1769 1813 $temp_connections = $connections_to_return;
1770 1814 $connections_to_return = array();
1771 1815
1772 1816 foreach ( $temp_connections as $connection ) {
1773 - if ( $identifier === $connection['service'] ) {
1817 + if ( $identifier === $connection['service_name'] ) {
1774 1818 $connections_to_return[] = $connection;
1775 1819 }
1776 1820 }
1777 1821 }
@@ -1776,27 +1820,22 @@
1776 1820 }
1777 1821 }
1778 1822
1779 1823 if ( $identifier && ! $id_is_service && ! empty( $connections_to_return ) ) {
1780 - $connections_to_return = wp_list_filter( $connections_to_return, array( 'id' => $identifier ) );
1824 + $connections_to_return = wp_list_filter( $connections_to_return, array( 'connection_id' => $identifier ) );
1781 1825 }
1782 1826
1783 1827 $expected_keys = array(
1784 - 'id',
1785 - 'service',
1786 - 'user_id',
1787 - 'provider',
1788 - 'issued',
1789 - 'expires',
1828 + 'connection_id',
1829 + 'service_name',
1830 + 'display_name',
1790 1831 'external_id',
1791 - 'external_name',
1792 - 'external_display',
1793 - 'type',
1794 - 'connection_data',
1832 + 'wpcom_user_id',
1833 + 'shared',
1795 1834 );
1796 1835
1797 1836 // Somehow, a test site ended up in a state where $connections_to_return looked like:
1798 - // array( array( array( 'id' => 0, 'service' => 0 ) ) ) // phpcs:ignore Squiz.PHP.CommentedOutCode.Found
1837 + // array( array( array( 'id' => 0, 'service' => 0 ) ) )
1799 1838 // This caused the CLI command to error when running WP_CLI\Utils\format_items() below. So
1800 1839 // to minimize future issues, this nested loop will remove any connections that don't contain
1801 1840 // any keys that we expect.
1802 1841 foreach ( (array) $connections_to_return as $connection_key => $connection ) {
@@ -1830,23 +1869,15 @@
1830 1869 }
1831 1870
1832 1871 jetpack_cli_are_you_sure();
1833 1872
1834 - $connections = array();
1835 1873 $service = $identifier;
1874 + $connections = is_user_logged_in()
1875 + ? Connections::get_all_for_user()
1876 + : Connections::get_all();
1836 1877
1837 - $option_connections = is_user_logged_in()
1838 - ? (array) $publicize->get_all_connections_for_user()
1839 - : (array) $publicize->get_all_connections();
1840 -
1841 - if ( 'all' === $service ) {
1842 - foreach ( (array) $option_connections as $service_name => $service_connections ) {
1843 - foreach ( $service_connections as $id => $connection ) {
1844 - $connections[ $id ] = $connection;
1845 - }
1846 - }
1847 - } elseif ( ! empty( $option_connections[ $service ] ) ) {
1848 - $connections = $option_connections[ $service ];
1878 + if ( 'all' !== $service ) {
1879 + $connections = wp_list_filter( $connections, array( 'service_name' => $service ) );
1849 1880 }
1850 1881
1851 1882 if ( ! empty( $connections ) ) {
1852 1883 $count = is_countable( $connections ) ? count( $connections ) : 0;
@@ -1855,9 +1886,10 @@
1855 1886 sprintf( __( 'Disconnecting all connections to %s.', 'jetpack' ), $service ),
1856 1887 $count
1857 1888 );
1858 1889
1859 - foreach ( $connections as $id => $connection ) {
1890 + foreach ( $connections as $connection ) {
1891 + $id = $connection['connection_id'];
1860 1892 if ( false === $publicize->disconnect( false, $id ) ) {
1861 1893 WP_CLI::error(
1862 1894 sprintf(
1863 1895 /* translators: %1$d is a numeric ID and %2$s is a lowercase string for a social network. */
@@ -1877,9 +1909,9 @@
1877 1909 if ( 'all' === $service ) {
1878 1910 WP_CLI::success( __( 'All Jetpack Social connections were successfully disconnected.', 'jetpack' ) );
1879 1911 } else {
1880 1912 /* translators: %s is a lowercase string for a social network. */
1881 - WP_CLI::success( __( 'All Jetpack Social connections to %s were successfully disconnected.', 'jetpack' ), $service );
1913 + WP_CLI::success( sprintf( __( 'All Jetpack Social connections to %s were successfully disconnected.', 'jetpack' ), $service ) );
1882 1914 }
1883 1915 }
1884 1916 } elseif ( false !== $publicize->disconnect( false, $identifier ) ) {
1885 1917 /* translators: %d is a numeric ID. Example: 1234. */
@@ -1914,9 +1946,10 @@
1914 1946 array(
1915 1947 'success' => false,
1916 1948 'error_code' => $error->get_error_code(),
1917 1949 'error_message' => $error->get_error_message(),
1918 - )
1950 + ),
1951 + JSON_UNESCAPED_SLASHES
1919 1952 )
1920 1953 );
1921 1954 exit( 1 );
1922 1955 }
@@ -1969,19 +2002,17 @@
1969 2002 * @param array $args Positional parameters. Only one is used, that corresponds to the block title.
1970 2003 * @param array $assoc_args Associative parameters defined in the scaffold() method.
1971 2004 */
1972 2005 public function block( $args, $assoc_args ) {
1973 - if ( isset( $args[1] ) ) {
1974 - $title = ucwords( $args[1] );
1975 - } else {
2006 + if ( ! isset( $args[1] ) ) {
1976 2007 WP_CLI::error( esc_html__( 'The title parameter is required.', 'jetpack' ) . ' 👻' );
1977 2008 exit( 1 );
1978 2009 }
1979 2010
1980 - $slug = isset( $assoc_args['slug'] )
1981 - ? $assoc_args['slug']
1982 - : sanitize_title( $title );
2011 + $title = ucwords( $args[1] );
1983 2012
2013 + $slug = $assoc_args['slug'] ?? sanitize_title( $title );
2014 +
1984 2015 $next_version = "\x24\x24next-version$$"; // Escapes to hide the string from tools/replace-next-version-tag.sh
1985 2016
1986 2017 $variation_options = array( 'production', 'experimental', 'beta' );
1987 2018 $variation = ( isset( $assoc_args['variation'] ) && in_array( $assoc_args['variation'], $variation_options, true ) )
@@ -2024,14 +2055,14 @@
2024 2055 "$path/block.json" => self::render_block_file(
2025 2056 'block-block-json',
2026 2057 array(
2027 2058 'slug' => $slug,
2028 - 'title' => wp_json_encode( $title, JSON_UNESCAPED_UNICODE ),
2059 + 'title' => wp_json_encode( $title, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ),
2029 2060 'description' => isset( $assoc_args['description'] )
2030 - ? wp_json_encode( $assoc_args['description'], JSON_UNESCAPED_UNICODE )
2031 - : wp_json_encode( $title, JSON_UNESCAPED_UNICODE ),
2061 + ? wp_json_encode( $assoc_args['description'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE )
2062 + : wp_json_encode( $title, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ),
2032 2063 'nextVersion' => $next_version,
2033 - 'keywords' => wp_json_encode( $keywords, JSON_UNESCAPED_UNICODE ),
2064 + 'keywords' => wp_json_encode( $keywords, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ),
2034 2065 )
2035 2066 ),
2036 2067 "$path/$slug.php" => self::render_block_file(
2037 2068 'block-register-php',
@@ -2082,9 +2113,9 @@
2082 2113 $new_block_list = json_decode( $block_list );
2083 2114 $new_block_list->{ $variation }[] = $slug;
2084 2115
2085 2116 // Format the JSON to match our coding standards.
2086 - $new_block_list_formatted = wp_json_encode( $new_block_list, JSON_PRETTY_PRINT ) . "\n";
2117 + $new_block_list_formatted = wp_json_encode( $new_block_list, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ) . "\n";
2087 2118 $new_block_list_formatted = preg_replace_callback(
2088 2119 // Find all occurrences of multiples of 4 spaces a the start of the line.
2089 2120 '/^((?: )+)/m',
2090 2121 function ( $matches ) {