Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack_CLI often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_CLI, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Jetpack_CLI extends WP_CLI_Command { |
||
| 12 | // Aesthetics |
||
| 13 | public $green_open = "\033[32m"; |
||
| 14 | public $red_open = "\033[31m"; |
||
| 15 | public $yellow_open = "\033[33m"; |
||
| 16 | public $color_close = "\033[0m"; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Get Jetpack Details |
||
| 20 | * |
||
| 21 | * ## OPTIONS |
||
| 22 | * |
||
| 23 | * empty: Leave it empty for basic stats |
||
| 24 | * |
||
| 25 | * full: View full stats. It's the data from the heartbeat |
||
| 26 | * |
||
| 27 | * ## EXAMPLES |
||
| 28 | * |
||
| 29 | * wp jetpack status |
||
| 30 | * wp jetpack status full |
||
| 31 | * |
||
| 32 | */ |
||
| 33 | public function status( $args, $assoc_args ) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Tests the active connection |
||
| 111 | * |
||
| 112 | * 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. |
||
| 113 | * |
||
| 114 | * ## EXAMPLES |
||
| 115 | * |
||
| 116 | * wp jetpack test-connection |
||
| 117 | * |
||
| 118 | * @subcommand test-connection |
||
| 119 | */ |
||
| 120 | public function test_connection( $args, $assoc_args ) { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Disconnect Jetpack Blogs or Users |
||
| 156 | * |
||
| 157 | * ## OPTIONS |
||
| 158 | * |
||
| 159 | * blog: Disconnect the entire blog. |
||
| 160 | * |
||
| 161 | * user <user_identifier>: Disconnect a specific user from WordPress.com. |
||
| 162 | * |
||
| 163 | * Please note, the primary account that the blog is connected |
||
| 164 | * to WordPress.com with cannot be disconnected without |
||
| 165 | * disconnecting the entire blog. |
||
| 166 | * |
||
| 167 | * ## EXAMPLES |
||
| 168 | * |
||
| 169 | * wp jetpack disconnect blog |
||
| 170 | * wp jetpack disconnect user 13 |
||
| 171 | * wp jetpack disconnect user username |
||
| 172 | * wp jetpack disconnect user [email protected] |
||
| 173 | * |
||
| 174 | * @synopsis <blog|user> [<user_identifier>] |
||
| 175 | */ |
||
| 176 | public function disconnect( $args, $assoc_args ) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Reset Jetpack options and settings to default |
||
| 234 | * |
||
| 235 | * ## OPTIONS |
||
| 236 | * |
||
| 237 | * modules: Resets modules to default state ( get_default_modules() ) |
||
| 238 | * |
||
| 239 | * options: Resets all Jetpack options except: |
||
| 240 | * - All private options (Blog token, user token, etc...) |
||
| 241 | * - id (The Client ID/WP.com Blog ID of this site) |
||
| 242 | * - master_user |
||
| 243 | * - version |
||
| 244 | * - activated |
||
| 245 | * |
||
| 246 | * ## EXAMPLES |
||
| 247 | * |
||
| 248 | * wp jetpack reset options |
||
| 249 | * wp jetpack reset modules |
||
| 250 | * |
||
| 251 | * @synopsis <modules|options> |
||
| 252 | */ |
||
| 253 | public function reset( $args, $assoc_args ) { |
||
| 254 | $action = isset( $args[0] ) ? $args[0] : 'prompt'; |
||
| 255 | View Code Duplication | if ( ! in_array( $action, array( 'options', 'modules' ) ) ) { |
|
| 256 | /* translators: %s is a command like "prompt" */ |
||
| 257 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
||
| 258 | } |
||
| 259 | |||
| 260 | // Are you sure? |
||
| 261 | jetpack_cli_are_you_sure(); |
||
| 262 | |||
| 263 | switch ( $action ) { |
||
| 264 | case 'options': |
||
| 265 | $options_to_reset = Jetpack_Options::get_options_for_reset(); |
||
| 266 | |||
| 267 | // Reset the Jetpack options |
||
| 268 | WP_CLI::line( sprintf( |
||
| 269 | __( "Resetting Jetpack Options for %s...\n", "jetpack" ), |
||
| 270 | esc_url( get_site_url() ) |
||
| 271 | ) ); |
||
| 272 | sleep(1); // Take a breath |
||
| 273 | foreach ( $options_to_reset['jp_options'] as $option_to_reset ) { |
||
| 274 | Jetpack_Options::delete_option( $option_to_reset ); |
||
| 275 | usleep( 100000 ); |
||
| 276 | /* translators: This is the result of an action. The option named %s was reset */ |
||
| 277 | WP_CLI::success( sprintf( __( '%s option reset', 'jetpack' ), $option_to_reset ) ); |
||
| 278 | } |
||
| 279 | |||
| 280 | // Reset the WP options |
||
| 281 | WP_CLI::line( __( "Resetting the jetpack options stored in wp_options...\n", "jetpack" ) ); |
||
| 282 | usleep( 500000 ); // Take a breath |
||
| 283 | foreach ( $options_to_reset['wp_options'] as $option_to_reset ) { |
||
| 284 | delete_option( $option_to_reset ); |
||
| 285 | usleep( 100000 ); |
||
| 286 | /* translators: This is the result of an action. The option named %s was reset */ |
||
| 287 | WP_CLI::success( sprintf( __( '%s option reset', 'jetpack' ), $option_to_reset ) ); |
||
| 288 | } |
||
| 289 | |||
| 290 | // Reset to default modules |
||
| 291 | WP_CLI::line( __( "Resetting default modules...\n", "jetpack" ) ); |
||
| 292 | usleep( 500000 ); // Take a breath |
||
| 293 | $default_modules = Jetpack::get_default_modules(); |
||
| 294 | Jetpack::update_active_modules( $default_modules ); |
||
| 295 | WP_CLI::success( __( 'Modules reset to default.', 'jetpack' ) ); |
||
| 296 | |||
| 297 | // Jumpstart option is special |
||
| 298 | Jetpack_Options::update_option( 'jumpstart', 'new_connection' ); |
||
| 299 | WP_CLI::success( __( 'jumpstart option reset', 'jetpack' ) ); |
||
| 300 | break; |
||
| 301 | case 'modules': |
||
| 302 | $default_modules = Jetpack::get_default_modules(); |
||
| 303 | Jetpack::update_active_modules( $default_modules ); |
||
| 304 | WP_CLI::success( __( 'Modules reset to default.', 'jetpack' ) ); |
||
| 305 | break; |
||
| 306 | case 'prompt': |
||
| 307 | WP_CLI::error( __( 'Please specify if you would like to reset your options, or modules', 'jetpack' ) ); |
||
| 308 | break; |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Manage Jetpack Modules |
||
| 314 | * |
||
| 315 | * ## OPTIONS |
||
| 316 | * |
||
| 317 | * <list|activate|deactivate|toggle> |
||
| 318 | * : The action to take. |
||
| 319 | * --- |
||
| 320 | * default: list |
||
| 321 | * options: |
||
| 322 | * - list |
||
| 323 | * - activate |
||
| 324 | * - deactivate |
||
| 325 | * - toggle |
||
| 326 | * --- |
||
| 327 | * |
||
| 328 | * [<module_slug>] |
||
| 329 | * : The slug of the module to perform an action on. |
||
| 330 | * |
||
| 331 | * [--format=<format>] |
||
| 332 | * : Allows overriding the output of the command when listing modules. |
||
| 333 | * --- |
||
| 334 | * default: table |
||
| 335 | * options: |
||
| 336 | * - table |
||
| 337 | * - json |
||
| 338 | * - csv |
||
| 339 | * - yaml |
||
| 340 | * - ids |
||
| 341 | * - count |
||
| 342 | * --- |
||
| 343 | * |
||
| 344 | * ## EXAMPLES |
||
| 345 | * |
||
| 346 | * wp jetpack module list |
||
| 347 | * wp jetpack module list --format=json |
||
| 348 | * wp jetpack module activate stats |
||
| 349 | * wp jetpack module deactivate stats |
||
| 350 | * wp jetpack module toggle stats |
||
| 351 | * wp jetpack module activate all |
||
| 352 | * wp jetpack module deactivate all |
||
| 353 | */ |
||
| 354 | public function module( $args, $assoc_args ) { |
||
| 355 | $action = isset( $args[0] ) ? $args[0] : 'list'; |
||
| 356 | |||
| 357 | if ( isset( $args[1] ) ) { |
||
| 358 | $module_slug = $args[1]; |
||
| 359 | if ( 'all' !== $module_slug && ! Jetpack::is_module( $module_slug ) ) { |
||
| 360 | /* translators: %s is a module slug like "stats" */ |
||
| 361 | WP_CLI::error( sprintf( __( '%s is not a valid module.', 'jetpack' ), $module_slug ) ); |
||
| 362 | } |
||
| 363 | if ( 'toggle' === $action ) { |
||
| 364 | $action = Jetpack::is_module_active( $module_slug ) |
||
| 365 | ? 'deactivate' |
||
| 366 | : 'activate'; |
||
| 367 | } |
||
| 368 | if ( 'all' === $args[1] ) { |
||
| 369 | $action = ( 'deactivate' === $action ) |
||
| 370 | ? 'deactivate_all' |
||
| 371 | : 'activate_all'; |
||
| 372 | } |
||
| 373 | } elseif ( 'list' !== $action ) { |
||
| 374 | WP_CLI::line( __( 'Please specify a valid module.', 'jetpack' ) ); |
||
| 375 | $action = 'list'; |
||
| 376 | } |
||
| 377 | |||
| 378 | switch ( $action ) { |
||
| 379 | case 'list': |
||
| 380 | $modules_list = array(); |
||
| 381 | $modules = Jetpack::get_available_modules(); |
||
| 382 | sort( $modules ); |
||
| 383 | foreach ( (array) $modules as $module_slug ) { |
||
| 384 | if ( 'vaultpress' === $module_slug ) { |
||
| 385 | continue; |
||
| 386 | } |
||
| 387 | $modules_list[] = array( |
||
| 388 | 'slug' => $module_slug, |
||
| 389 | 'status' => Jetpack::is_module_active( $module_slug ) |
||
| 390 | ? __( 'Active', 'jetpack' ) |
||
| 391 | : __( 'Inactive', 'jetpack' ), |
||
| 392 | ); |
||
| 393 | } |
||
| 394 | WP_CLI\Utils\format_items( $assoc_args['format'], $modules_list, array( 'slug', 'status' ) ); |
||
| 395 | break; |
||
| 396 | case 'activate': |
||
| 397 | $module = Jetpack::get_module( $module_slug ); |
||
| 398 | Jetpack::log( 'activate', $module_slug ); |
||
| 399 | if ( Jetpack::activate_module( $module_slug, false, false ) ) { |
||
| 400 | WP_CLI::success( sprintf( __( '%s has been activated.', 'jetpack' ), $module['name'] ) ); |
||
| 401 | } else { |
||
| 402 | WP_CLI::error( sprintf( __( '%s could not be activated.', 'jetpack' ), $module['name'] ) ); |
||
| 403 | } |
||
| 404 | break; |
||
| 405 | case 'activate_all': |
||
| 406 | $modules = Jetpack::get_available_modules(); |
||
| 407 | Jetpack::update_active_modules( $modules ); |
||
| 408 | WP_CLI::success( __( 'All modules activated!', 'jetpack' ) ); |
||
| 409 | break; |
||
| 410 | case 'deactivate': |
||
| 411 | $module = Jetpack::get_module( $module_slug ); |
||
| 412 | Jetpack::log( 'deactivate', $module_slug ); |
||
| 413 | Jetpack::deactivate_module( $module_slug ); |
||
| 414 | WP_CLI::success( sprintf( __( '%s has been deactivated.', 'jetpack' ), $module['name'] ) ); |
||
| 415 | break; |
||
| 416 | case 'deactivate_all': |
||
| 417 | Jetpack::delete_active_modules(); |
||
| 418 | WP_CLI::success( __( 'All modules deactivated!', 'jetpack' ) ); |
||
| 419 | break; |
||
| 420 | case 'toggle': |
||
| 421 | // Will never happen, should have been handled above and changed to activate or deactivate. |
||
| 422 | break; |
||
| 423 | } |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Manage Protect Settings |
||
| 428 | * |
||
| 429 | * ## OPTIONS |
||
| 430 | * |
||
| 431 | * whitelist: Whitelist an IP address. You can also read or clear the whitelist. |
||
| 432 | * |
||
| 433 | * |
||
| 434 | * ## EXAMPLES |
||
| 435 | * |
||
| 436 | * wp jetpack protect whitelist <ip address> |
||
| 437 | * wp jetpack protect whitelist list |
||
| 438 | * wp jetpack protect whitelist clear |
||
| 439 | * |
||
| 440 | * @synopsis <whitelist> [<ip|ip_low-ip_high|list|clear>] |
||
| 441 | */ |
||
| 442 | public function protect( $args, $assoc_args ) { |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Manage Jetpack Options |
||
| 549 | * |
||
| 550 | * ## OPTIONS |
||
| 551 | * |
||
| 552 | * list : List all jetpack options and their values |
||
| 553 | * delete : Delete an option |
||
| 554 | * - can only delete options that are white listed. |
||
| 555 | * update : update an option |
||
| 556 | * - can only update option strings |
||
| 557 | * get : get the value of an option |
||
| 558 | * |
||
| 559 | * ## EXAMPLES |
||
| 560 | * |
||
| 561 | * wp jetpack options list |
||
| 562 | * wp jetpack options get <option_name> |
||
| 563 | * wp jetpack options delete <option_name> |
||
| 564 | * wp jetpack options update <option_name> [<option_value>] |
||
| 565 | * |
||
| 566 | * @synopsis <list|get|delete|update> [<option_name>] [<option_value>] |
||
| 567 | */ |
||
| 568 | public function options( $args, $assoc_args ) { |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Get the status of or start a new Jetpack sync. |
||
| 668 | * |
||
| 669 | * ## OPTIONS |
||
| 670 | * |
||
| 671 | * status : Print the current sync status |
||
| 672 | * settings : Prints the current sync settings |
||
| 673 | * start : Start a full sync from this site to WordPress.com |
||
| 674 | * enable : Enables sync on the site |
||
| 675 | * disable : Disable sync on a site |
||
| 676 | * reset : Disables sync and Resets the sync queues on a site |
||
| 677 | * |
||
| 678 | * ## EXAMPLES |
||
| 679 | * |
||
| 680 | * wp jetpack sync status |
||
| 681 | * wp jetpack sync settings |
||
| 682 | * wp jetpack sync start --modules=functions --sync_wait_time=5 |
||
| 683 | * wp jetpack sync enable |
||
| 684 | * wp jetpack sync disable |
||
| 685 | * wp jetpack sync reset |
||
| 686 | * wp jetpack sync reset --queue=full or regular |
||
| 687 | * |
||
| 688 | * @synopsis <status|start> [--<field>=<value>] |
||
| 689 | */ |
||
| 690 | public function sync( $args, $assoc_args ) { |
||
| 865 | |||
| 866 | /** |
||
| 867 | * List the contents of a specific Jetpack sync queue. |
||
| 868 | * |
||
| 869 | * ## OPTIONS |
||
| 870 | * |
||
| 871 | * peek : List the 100 front-most items on the queue. |
||
| 872 | * |
||
| 873 | * ## EXAMPLES |
||
| 874 | * |
||
| 875 | * wp jetpack sync_queue full_sync peek |
||
| 876 | * |
||
| 877 | * @synopsis <incremental|full_sync> <peek> |
||
| 878 | */ |
||
| 879 | public function sync_queue( $args, $assoc_args ) { |
||
| 930 | |||
| 931 | /** |
||
| 932 | * Cancel's the current Jetpack plan granted by this partner, if applicable |
||
| 933 | * |
||
| 934 | * Returns success or error JSON |
||
| 935 | * |
||
| 936 | * <token_json> |
||
| 937 | * : JSON blob of WPCOM API token |
||
| 938 | * [--partner_tracking_id=<partner_tracking_id>] |
||
| 939 | * : This is an optional ID that a host can pass to help identify a site in logs on WordPress.com |
||
| 940 | * |
||
| 941 | * * @synopsis <token_json> [--partner_tracking_id=<partner_tracking_id>] |
||
| 942 | */ |
||
| 943 | public function partner_cancel( $args, $named_args ) { |
||
| 995 | |||
| 996 | /** |
||
| 997 | * Provision a site using a Jetpack Partner license |
||
| 998 | * |
||
| 999 | * Returns JSON blob |
||
| 1000 | * |
||
| 1001 | * ## OPTIONS |
||
| 1002 | * |
||
| 1003 | * <token_json> |
||
| 1004 | * : JSON blob of WPCOM API token |
||
| 1005 | * [--plan=<plan_name>] |
||
| 1006 | * : Slug of the requested plan, e.g. premium |
||
| 1007 | * [--wpcom_user_id=<user_id>] |
||
| 1008 | * : WordPress.com ID of user to connect as (must be whitelisted against partner key) |
||
| 1009 | * [--wpcom_user_email=<wpcom_user_email>] |
||
| 1010 | * : Override the email we send to WordPress.com for registration |
||
| 1011 | * [--onboarding=<onboarding>] |
||
| 1012 | * : Guide the user through an onboarding wizard |
||
| 1013 | * [--force_register=<register>] |
||
| 1014 | * : Whether to force a site to register |
||
| 1015 | * [--force_connect=<force_connect>] |
||
| 1016 | * : Force JPS to not reuse existing credentials |
||
| 1017 | * [--home_url=<home_url>] |
||
| 1018 | * : Overrides the home option via the home_url filter, or the WP_HOME constant |
||
| 1019 | * [--site_url=<site_url>] |
||
| 1020 | * : Overrides the siteurl option via the site_url filter, or the WP_SITEURL constant |
||
| 1021 | * [--partner_tracking_id=<partner_tracking_id>] |
||
| 1022 | * : This is an optional ID that a host can pass to help identify a site in logs on WordPress.com |
||
| 1023 | * |
||
| 1024 | * ## EXAMPLES |
||
| 1025 | * |
||
| 1026 | * $ wp jetpack partner_provision '{ some: "json" }' premium 1 |
||
| 1027 | * { success: true } |
||
| 1028 | * |
||
| 1029 | * @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>] |
||
| 1030 | */ |
||
| 1031 | public function partner_provision( $args, $named_args ) { |
||
| 1064 | |||
| 1065 | /** |
||
| 1066 | * Manages your Jetpack sitemap |
||
| 1067 | * |
||
| 1068 | * ## OPTIONS |
||
| 1069 | * |
||
| 1070 | * rebuild : Rebuild all sitemaps |
||
| 1071 | * --purge : if set, will remove all existing sitemap data before rebuilding |
||
| 1072 | * |
||
| 1073 | * ## EXAMPLES |
||
| 1074 | * |
||
| 1075 | * wp jetpack sitemap rebuild |
||
| 1076 | * |
||
| 1077 | * @subcommand sitemap |
||
| 1078 | * @synopsis <rebuild> [--purge] |
||
| 1079 | */ |
||
| 1080 | public function sitemap( $args, $assoc_args ) { |
||
| 1099 | |||
| 1100 | /** |
||
| 1101 | * Allows authorizing a user via the command line and will activate |
||
| 1102 | * |
||
| 1103 | * ## EXAMPLES |
||
| 1104 | * |
||
| 1105 | * wp jetpack authorize_user --token=123456789abcdef |
||
| 1106 | * |
||
| 1107 | * @synopsis --token=<value> |
||
| 1108 | */ |
||
| 1109 | public function authorize_user( $args, $named_args ) { |
||
| 1143 | |||
| 1144 | /** |
||
| 1145 | * Allows calling a WordPress.com API endpoint using the current blog's token. |
||
| 1146 | * |
||
| 1147 | * ## OPTIONS |
||
| 1148 | * --resource=<resource> |
||
| 1149 | * : The resource to call with the current blog's token, where `%d` represents the current blog's ID. |
||
| 1150 | * |
||
| 1151 | * [--api_version=<api_version>] |
||
| 1152 | * : The API version to query against. |
||
| 1153 | * |
||
| 1154 | * [--base_api_path=<base_api_path>] |
||
| 1155 | * : The base API path to query. |
||
| 1156 | * --- |
||
| 1157 | * default: rest |
||
| 1158 | * --- |
||
| 1159 | * |
||
| 1160 | * [--body=<body>] |
||
| 1161 | * : A JSON encoded string representing arguments to send in the body. |
||
| 1162 | * |
||
| 1163 | * [--field=<value>] |
||
| 1164 | * : Any number of arguments that should be passed to the resource. |
||
| 1165 | * |
||
| 1166 | * [--pretty] |
||
| 1167 | * : Will pretty print the results of a successful API call. |
||
| 1168 | * |
||
| 1169 | * [--strip-success] |
||
| 1170 | * : Will remove the green success label from successful API calls. |
||
| 1171 | * |
||
| 1172 | * ## EXAMPLES |
||
| 1173 | * |
||
| 1174 | * wp jetpack call_api --resource='/sites/%d' |
||
| 1175 | */ |
||
| 1176 | public function call_api( $args, $named_args ) { |
||
| 1242 | |||
| 1243 | /** |
||
| 1244 | * Allows uploading SSH Credentials to the current site for backups, restores, and security scanning. |
||
| 1245 | * |
||
| 1246 | * ## OPTIONS |
||
| 1247 | * |
||
| 1248 | * [--host=<host>] |
||
| 1249 | * : The SSH server's address. |
||
| 1250 | * |
||
| 1251 | * [--ssh-user=<user>] |
||
| 1252 | * : The username to use to log in to the SSH server. |
||
| 1253 | * |
||
| 1254 | * [--pass=<pass>] |
||
| 1255 | * : The password used to log in, if using a password. (optional) |
||
| 1256 | * |
||
| 1257 | * [--kpri=<kpri>] |
||
| 1258 | * : The private key used to log in, if using a private key. (optional) |
||
| 1259 | * |
||
| 1260 | * [--pretty] |
||
| 1261 | * : Will pretty print the results of a successful API call. (optional) |
||
| 1262 | * |
||
| 1263 | * [--strip-success] |
||
| 1264 | * : Will remove the green success label from successful API calls. (optional) |
||
| 1265 | * |
||
| 1266 | * ## EXAMPLES |
||
| 1267 | * |
||
| 1268 | * wp jetpack upload_ssh_creds --host=example.com --ssh-user=example --pass=password |
||
| 1269 | * wp jetpack updload_ssh_creds --host=example.com --ssh-user=example --kpri=key |
||
| 1270 | */ |
||
| 1271 | public function upload_ssh_creds( $args, $named_args ) { |
||
| 1324 | |||
| 1325 | /** |
||
| 1326 | * API wrapper for getting stats from the WordPress.com API for the current site. |
||
| 1327 | * |
||
| 1328 | * ## OPTIONS |
||
| 1329 | * |
||
| 1330 | * [--quantity=<quantity>] |
||
| 1331 | * : The number of units to include. |
||
| 1332 | * --- |
||
| 1333 | * default: 30 |
||
| 1334 | * --- |
||
| 1335 | * |
||
| 1336 | * [--period=<period>] |
||
| 1337 | * : The unit of time to query stats for. |
||
| 1338 | * --- |
||
| 1339 | * default: day |
||
| 1340 | * options: |
||
| 1341 | * - day |
||
| 1342 | * - week |
||
| 1343 | * - month |
||
| 1344 | * - year |
||
| 1345 | * --- |
||
| 1346 | * |
||
| 1347 | * [--date=<date>] |
||
| 1348 | * : The latest date to return stats for. Ex. - 2018-01-01. |
||
| 1349 | * |
||
| 1350 | * [--pretty] |
||
| 1351 | * : Will pretty print the results of a successful API call. |
||
| 1352 | * |
||
| 1353 | * [--strip-success] |
||
| 1354 | * : Will remove the green success label from successful API calls. |
||
| 1355 | * |
||
| 1356 | * ## EXAMPLES |
||
| 1357 | * |
||
| 1358 | * wp jetpack get_stats |
||
| 1359 | */ |
||
| 1360 | public function get_stats( $args, $named_args ) { |
||
| 1393 | |||
| 1394 | /** |
||
| 1395 | * Allows management of publicize connections. |
||
| 1396 | * |
||
| 1397 | * ## OPTIONS |
||
| 1398 | * |
||
| 1399 | * <list|disconnect> |
||
| 1400 | * : The action to perform. |
||
| 1401 | * --- |
||
| 1402 | * options: |
||
| 1403 | * - list |
||
| 1404 | * - disconnect |
||
| 1405 | * --- |
||
| 1406 | * |
||
| 1407 | * [<identifier>] |
||
| 1408 | * : The connection ID or service to perform an action on. |
||
| 1409 | * |
||
| 1410 | * [--format=<format>] |
||
| 1411 | * : Allows overriding the output of the command when listing connections. |
||
| 1412 | * --- |
||
| 1413 | * default: table |
||
| 1414 | * options: |
||
| 1415 | * - table |
||
| 1416 | * - json |
||
| 1417 | * - csv |
||
| 1418 | * - yaml |
||
| 1419 | * - ids |
||
| 1420 | * - count |
||
| 1421 | * --- |
||
| 1422 | * |
||
| 1423 | * ## EXAMPLES |
||
| 1424 | * |
||
| 1425 | * # List all publicize connections. |
||
| 1426 | * $ wp jetpack publicize list |
||
| 1427 | * |
||
| 1428 | * # List publicize connections for a given service. |
||
| 1429 | * $ wp jetpack publicize list twitter |
||
| 1430 | * |
||
| 1431 | * # List all publicize connections for a given user. |
||
| 1432 | * $ wp --user=1 jetpack publicize list |
||
| 1433 | * |
||
| 1434 | * # List all publicize connections for a given user and service. |
||
| 1435 | * $ wp --user=1 jetpack publicize list twitter |
||
| 1436 | * |
||
| 1437 | * # Display details for a given connection. |
||
| 1438 | * $ wp jetpack publicize list 123456 |
||
| 1439 | * |
||
| 1440 | * # Diconnection a given connection. |
||
| 1441 | * $ wp jetpack publicize disconnect 123456 |
||
| 1442 | * |
||
| 1443 | * # Disconnect all connections. |
||
| 1444 | * $ wp jetpack publicize disconnect all |
||
| 1445 | * |
||
| 1446 | * # Disconnect all connections for a given service. |
||
| 1447 | * $ wp jetpack publicize disconnect twitter |
||
| 1448 | */ |
||
| 1449 | public function publicize( $args, $named_args ) { |
||
| 1622 | |||
| 1623 | private function get_api_host() { |
||
| 1627 | |||
| 1628 | private function partner_provision_error( $error ) { |
||
| 1636 | |||
| 1637 | /** |
||
| 1638 | * Creates the essential files in Jetpack to start building a Gutenberg block or plugin. |
||
| 1639 | * |
||
| 1640 | * ## TYPES |
||
| 1641 | * |
||
| 1642 | * block: it creates a Jetpack block. All files will be created in a directory under extensions/blocks named based on the block title or a specific given slug. |
||
| 1643 | * |
||
| 1644 | * ## BLOCK TYPE OPTIONS |
||
| 1645 | * |
||
| 1646 | * The first parameter is the block title and it's not associative. Add it wrapped in quotes. |
||
| 1647 | * The title is also used to create the slug and the edit PHP class name. If it's something like "Logo gallery", the slug will be 'logo-gallery' and the class name will be LogoGalleryEdit. |
||
| 1648 | * --slug: Specific slug to identify the block that overrides the one generated based on the title. |
||
| 1649 | * --description: Allows to provide a text description of the block. |
||
| 1650 | * --keywords: Provide up to three keywords separated by comma so users can find this block when they search in Gutenberg's inserter. |
||
| 1651 | * |
||
| 1652 | * ## BLOCK TYPE EXAMPLES |
||
| 1653 | * |
||
| 1654 | * wp jetpack scaffold block "Cool Block" |
||
| 1655 | * wp jetpack scaffold block "Amazing Rock" --slug="good-music" --description="Rock the best music on your site" |
||
| 1656 | * wp jetpack scaffold block "Jukebox" --keywords="music, audio, media" |
||
| 1657 | * |
||
| 1658 | * @subcommand scaffold block |
||
| 1659 | * @synopsis <type> <title> [--slug] [--description] [--keywords] |
||
| 1660 | * |
||
| 1661 | * @param array $args Positional parameters, when strings are passed, wrap them in quotes. |
||
| 1662 | * @param array $assoc_args Associative parameters like --slug="nice-block". |
||
| 1663 | */ |
||
| 1664 | public function scaffold( $args, $assoc_args ) { |
||
| 1675 | |||
| 1676 | /** |
||
| 1677 | * Creates the essential files in Jetpack to build a Gutenberg block. |
||
| 1678 | * |
||
| 1679 | * @param array $args Positional parameters. Only one is used, that corresponds to the block title. |
||
| 1680 | * @param array $assoc_args Associative parameters defined in the scaffold() method. |
||
| 1681 | */ |
||
| 1682 | public function block( $args, $assoc_args ) { |
||
| 1683 | View Code Duplication | if ( isset( $args[1] ) ) { |
|
| 1684 | $title = ucwords( $args[1] ); |
||
| 1685 | } else { |
||
| 1686 | WP_CLI::error( esc_html__( 'The title parameter is required.', 'jetpack' ) . ' 👻' ); |
||
| 1687 | exit( 1 ); |
||
| 1688 | } |
||
| 1689 | |||
| 1690 | $slug = isset( $assoc_args['slug'] ) |
||
| 1691 | ? $assoc_args['slug'] |
||
| 1692 | : sanitize_title( $title ); |
||
| 1693 | |||
| 1694 | if ( preg_match( '#^jetpack/#', $slug ) ) { |
||
| 1695 | $slug = preg_replace( '#^jetpack/#', '', $slug ); |
||
| 1696 | } |
||
| 1697 | |||
| 1698 | if ( ! preg_match( '/^[a-z][a-z0-9\-]*$/', $slug ) ) { |
||
| 1699 | WP_CLI::error( esc_html__( 'Invalid block slug. They can contain only lowercase alphanumeric characters or dashes, and start with a letter', 'jetpack' ) . ' 👻' ); |
||
| 1700 | } |
||
| 1701 | |||
| 1702 | global $wp_filesystem; |
||
| 1703 | if ( ! WP_Filesystem() ) { |
||
| 1704 | WP_CLI::error( esc_html__( "Can't write files", 'jetpack' ) . ' 😱' ); |
||
| 1705 | } |
||
| 1706 | |||
| 1707 | $path = JETPACK__PLUGIN_DIR . "extensions/blocks/$slug"; |
||
| 1708 | |||
| 1709 | if ( $wp_filesystem->exists( $path ) && $wp_filesystem->is_dir( $path ) ) { |
||
| 1710 | WP_CLI::error( sprintf( esc_html__( 'Name conflicts with the existing block %s', 'jetpack' ), $path ) . ' ⛔️' ); |
||
| 1711 | exit( 1 ); |
||
| 1712 | } |
||
| 1713 | |||
| 1714 | $wp_filesystem->mkdir( $path ); |
||
| 1715 | |||
| 1716 | $hasKeywords = isset( $assoc_args['keywords'] ); |
||
| 1717 | |||
| 1718 | $files = array( |
||
| 1719 | "$path/$slug.php" => $this->render_block_file( 'block-register-php', array( |
||
| 1720 | 'slug' => $slug, |
||
| 1721 | 'title' => $title, |
||
| 1722 | 'underscoredSlug' => str_replace( '-', '_', $slug ), |
||
| 1723 | ) ), |
||
| 1724 | "$path/index.js" => $this->render_block_file( 'block-index-js', array( |
||
| 1725 | 'slug' => $slug, |
||
| 1726 | 'title' => $title, |
||
| 1727 | 'description' => isset( $assoc_args['description'] ) |
||
| 1728 | ? $assoc_args['description'] |
||
| 1729 | : $title, |
||
| 1730 | 'keywords' => $hasKeywords |
||
| 1731 | ? array_map( function( $keyword ) { |
||
| 1732 | // Construction necessary for Mustache lists |
||
| 1733 | return array( 'keyword' => trim( $keyword ) ); |
||
| 1734 | }, explode( ',', $assoc_args['keywords'], 3 ) ) |
||
| 1735 | : '', |
||
| 1736 | 'hasKeywords' => $hasKeywords |
||
| 1737 | ) ), |
||
| 1738 | "$path/editor.js" => $this->render_block_file( 'block-editor-js' ), |
||
| 1739 | "$path/editor.scss" => $this->render_block_file( 'block-editor-scss', array( |
||
| 1740 | 'slug' => $slug, |
||
| 1741 | 'title' => $title, |
||
| 1742 | ) ), |
||
| 1743 | "$path/edit.js" => $this->render_block_file( 'block-edit-js', array( |
||
| 1744 | 'title' => $title, |
||
| 1745 | 'className' => str_replace( ' ', '', ucwords( str_replace( '-', ' ', $slug ) ) ), |
||
| 1746 | ) ) |
||
| 1747 | ); |
||
| 1748 | |||
| 1749 | $files_written = array(); |
||
| 1750 | |||
| 1751 | foreach ( $files as $filename => $contents ) { |
||
| 1752 | if ( $wp_filesystem->put_contents( $filename, $contents ) ) { |
||
| 1753 | $files_written[] = $filename; |
||
| 1754 | } else { |
||
| 1755 | WP_CLI::error( sprintf( esc_html__( 'Error creating %s', 'jetpack' ), $filename ) ); |
||
| 1756 | } |
||
| 1757 | } |
||
| 1758 | |||
| 1759 | if ( empty( $files_written ) ) { |
||
| 1760 | WP_CLI::log( esc_html__( 'No files were created', 'jetpack' ) ); |
||
| 1761 | } else { |
||
| 1762 | // Load index.json and insert the slug of the new block in the production array |
||
| 1763 | $block_list_path = JETPACK__PLUGIN_DIR . 'extensions/index.json'; |
||
| 1764 | $block_list = $wp_filesystem->get_contents( $block_list_path ); |
||
| 1765 | if ( empty( $block_list ) ) { |
||
| 1766 | WP_CLI::error( sprintf( esc_html__( 'Error fetching contents of %s', 'jetpack' ), $block_list_path ) ); |
||
| 1767 | } else if ( false === stripos( $block_list, $slug ) ) { |
||
| 1768 | $new_block_list = json_decode( $block_list ); |
||
| 1769 | $new_block_list->beta[] = $slug; |
||
| 1770 | if ( ! $wp_filesystem->put_contents( $block_list_path, wp_json_encode( $new_block_list ) ) ) { |
||
| 1771 | WP_CLI::error( sprintf( esc_html__( 'Error writing new %s', 'jetpack' ), $block_list_path ) ); |
||
| 1772 | } |
||
| 1773 | } |
||
| 1774 | |||
| 1775 | WP_CLI::success( sprintf( |
||
| 1776 | /* translators: the placeholders are a human readable title, and a series of words separated by dashes */ |
||
| 1777 | esc_html__( 'Successfully created block %s with slug %s', 'jetpack' ) . ' 🎉' . "\n" . |
||
| 1778 | "--------------------------------------------------------------------------------------------------------------------\n" . |
||
| 1779 | /* translators: the placeholder is a directory path */ |
||
| 1780 | esc_html__( 'The files were created at %s', 'jetpack' ) . "\n" . |
||
| 1781 | esc_html__( 'To start using the block, build the blocks with yarn run build-extensions', 'jetpack' ) . "\n" . |
||
| 1782 | /* translators: the placeholder is a file path */ |
||
| 1783 | esc_html__( 'The block slug has been added to the beta list at %s', 'jetpack' ) . "\n" . |
||
| 1784 | esc_html__( 'To load the block, add the constant JETPACK_BETA_BLOCKS as true to your wp-config.php file', 'jetpack' ) . "\n" . |
||
| 1785 | /* translators: the placeholder is a URL */ |
||
| 1786 | "\n" . esc_html__( 'Read more at %s', 'jetpack' ) . "\n", |
||
| 1787 | $title, |
||
| 1788 | $slug, |
||
| 1789 | $path, |
||
| 1790 | $block_list_path, |
||
| 1791 | 'https://github.com/Automattic/jetpack/blob/master/extensions/README.md#develop-new-blocks' |
||
| 1792 | ) . '--------------------------------------------------------------------------------------------------------------------' ); |
||
| 1793 | } |
||
| 1794 | } |
||
| 1795 | |||
| 1796 | /** |
||
| 1797 | * Built the file replacing the placeholders in the template with the data supplied. |
||
| 1798 | * |
||
| 1799 | * @param string $template |
||
| 1800 | * @param array $data |
||
| 1801 | * |
||
| 1802 | * @return string mixed |
||
| 1803 | */ |
||
| 1804 | private static function render_block_file( $template, $data = array() ) { |
||
| 1807 | } |
||
| 1808 | |||
| 1809 | /* |
||
| 1810 | * Standard "ask for permission to continue" function. |
||
| 1844 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.