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 |
||
15 | class Jetpack_CLI extends WP_CLI_Command { |
||
16 | // Aesthetics. |
||
17 | public $green_open = "\033[32m"; |
||
18 | public $red_open = "\033[31m"; |
||
19 | public $yellow_open = "\033[33m"; |
||
20 | public $color_close = "\033[0m"; |
||
21 | |||
22 | /** |
||
23 | * Get Jetpack Details |
||
24 | * |
||
25 | * ## OPTIONS |
||
26 | * |
||
27 | * empty: Leave it empty for basic stats |
||
28 | * |
||
29 | * full: View full stats. It's the data from the heartbeat |
||
30 | * |
||
31 | * ## EXAMPLES |
||
32 | * |
||
33 | * wp jetpack status |
||
34 | * wp jetpack status full |
||
35 | */ |
||
36 | public function status( $args, $assoc_args ) { |
||
37 | jetpack_require_lib( 'debugger' ); |
||
38 | |||
39 | /* translators: %s is the site URL */ |
||
40 | WP_CLI::line( sprintf( __( 'Checking status for %s', 'jetpack' ), esc_url( get_home_url() ) ) ); |
||
41 | |||
42 | View Code Duplication | if ( isset( $args[0] ) && 'full' !== $args[0] ) { |
|
43 | /* translators: %s is a command like "prompt" */ |
||
44 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $args[0] ) ); |
||
45 | } |
||
46 | |||
47 | $master_user_email = Jetpack::get_master_user_email(); |
||
48 | |||
49 | $cxntests = new Jetpack_Cxn_Tests(); |
||
50 | |||
51 | if ( $cxntests->pass() ) { |
||
52 | $cxntests->output_results_for_cli(); |
||
53 | |||
54 | WP_CLI::success( __( 'Jetpack is currently connected to WordPress.com', 'jetpack' ) ); |
||
55 | } else { |
||
56 | $error = array(); |
||
57 | foreach ( $cxntests->list_fails() as $fail ) { |
||
|
|||
58 | $error[] = $fail['name'] . ': ' . $fail['message']; |
||
59 | } |
||
60 | WP_CLI::error_multi_line( $error ); |
||
61 | |||
62 | $cxntests->output_results_for_cli(); |
||
63 | |||
64 | WP_CLI::error( __( 'Jetpack connection is broken.', 'jetpack' ) ); // Exit CLI. |
||
65 | } |
||
66 | |||
67 | /* translators: %s is current version of Jetpack, for example 7.3 */ |
||
68 | WP_CLI::line( sprintf( __( 'The Jetpack Version is %s', 'jetpack' ), JETPACK__VERSION ) ); |
||
69 | /* translators: %d is WP.com ID of this blog */ |
||
70 | WP_CLI::line( sprintf( __( 'The WordPress.com blog_id is %d', 'jetpack' ), Jetpack_Options::get_option( 'id' ) ) ); |
||
71 | /* translators: %s is the email address of the connection owner */ |
||
72 | WP_CLI::line( sprintf( __( 'The WordPress.com account for the primary connection is %s', 'jetpack' ), $master_user_email ) ); |
||
73 | |||
74 | /* |
||
75 | * Are they asking for all data? |
||
76 | * |
||
77 | * Loop through heartbeat data and organize by priority. |
||
78 | */ |
||
79 | $all_data = ( isset( $args[0] ) && 'full' == $args[0] ) ? 'full' : false; |
||
80 | if ( $all_data ) { |
||
81 | // Heartbeat data |
||
82 | WP_CLI::line( "\n" . __( 'Additional data: ', 'jetpack' ) ); |
||
83 | |||
84 | // Get the filtered heartbeat data. |
||
85 | // Filtered so we can color/list by severity |
||
86 | $stats = Jetpack::jetpack_check_heartbeat_data(); |
||
87 | |||
88 | // Display red flags first |
||
89 | foreach ( $stats['bad'] as $stat => $value ) { |
||
90 | printf( "$this->red_open%-'.16s %s $this->color_close\n", $stat, $value ); |
||
91 | } |
||
92 | |||
93 | // Display caution warnings next |
||
94 | foreach ( $stats['caution'] as $stat => $value ) { |
||
95 | printf( "$this->yellow_open%-'.16s %s $this->color_close\n", $stat, $value ); |
||
96 | } |
||
97 | |||
98 | // The rest of the results are good! |
||
99 | foreach ( $stats['good'] as $stat => $value ) { |
||
100 | |||
101 | // Modules should get special spacing for aestetics |
||
102 | if ( strpos( $stat, 'odule-' ) ) { |
||
103 | printf( "%-'.30s %s\n", $stat, $value ); |
||
104 | usleep( 4000 ); // For dramatic effect lolz |
||
105 | continue; |
||
106 | } |
||
107 | printf( "%-'.16s %s\n", $stat, $value ); |
||
108 | usleep( 4000 ); // For dramatic effect lolz |
||
109 | } |
||
110 | } else { |
||
111 | // Just the basics |
||
112 | WP_CLI::line( "\n" . _x( "View full status with 'wp jetpack status full'", '"wp jetpack status full" is a command - do not translate', 'jetpack' ) ); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Tests the active connection |
||
118 | * |
||
119 | * 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. |
||
120 | * |
||
121 | * ## EXAMPLES |
||
122 | * |
||
123 | * wp jetpack test-connection |
||
124 | * |
||
125 | * @subcommand test-connection |
||
126 | */ |
||
127 | public function test_connection( $args, $assoc_args ) { |
||
128 | |||
129 | /* translators: %s is the site URL */ |
||
130 | WP_CLI::line( sprintf( __( 'Testing connection for %s', 'jetpack' ), esc_url( get_site_url() ) ) ); |
||
131 | |||
132 | if ( ! Jetpack::is_active() ) { |
||
133 | WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) ); |
||
134 | } |
||
135 | |||
136 | $response = Client::wpcom_json_api_request_as_blog( |
||
137 | sprintf( '/jetpack-blogs/%d/test-connection', Jetpack_Options::get_option( 'id' ) ), |
||
138 | Client::WPCOM_JSON_API_VERSION |
||
139 | ); |
||
140 | |||
141 | View Code Duplication | if ( is_wp_error( $response ) ) { |
|
142 | /* translators: %1$s is the error code, %2$s is the error message */ |
||
143 | WP_CLI::error( sprintf( __( 'Failed to test connection (#%1$s: %2$s)', 'jetpack' ), $response->get_error_code(), $response->get_error_message() ) ); |
||
144 | } |
||
145 | |||
146 | $body = wp_remote_retrieve_body( $response ); |
||
147 | if ( ! $body ) { |
||
148 | WP_CLI::error( __( 'Failed to test connection (empty response body)', 'jetpack' ) ); |
||
149 | } |
||
150 | |||
151 | $result = json_decode( $body ); |
||
152 | $is_connected = (bool) $result->connected; |
||
153 | $message = $result->message; |
||
154 | |||
155 | if ( $is_connected ) { |
||
156 | WP_CLI::success( $message ); |
||
157 | } else { |
||
158 | WP_CLI::error( $message ); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Disconnect Jetpack Blogs or Users |
||
164 | * |
||
165 | * ## OPTIONS |
||
166 | * |
||
167 | * blog: Disconnect the entire blog. |
||
168 | * |
||
169 | * user <user_identifier>: Disconnect a specific user from WordPress.com. |
||
170 | * |
||
171 | * Please note, the primary account that the blog is connected |
||
172 | * to WordPress.com with cannot be disconnected without |
||
173 | * disconnecting the entire blog. |
||
174 | * |
||
175 | * ## EXAMPLES |
||
176 | * |
||
177 | * wp jetpack disconnect blog |
||
178 | * wp jetpack disconnect user 13 |
||
179 | * wp jetpack disconnect user username |
||
180 | * wp jetpack disconnect user [email protected] |
||
181 | * |
||
182 | * @synopsis <blog|user> [<user_identifier>] |
||
183 | */ |
||
184 | public function disconnect( $args, $assoc_args ) { |
||
185 | if ( ! Jetpack::is_active() ) { |
||
186 | WP_CLI::error( __( 'You cannot disconnect, without having first connected.', 'jetpack' ) ); |
||
187 | } |
||
188 | |||
189 | $action = isset( $args[0] ) ? $args[0] : 'prompt'; |
||
190 | if ( ! in_array( $action, array( 'blog', 'user', 'prompt' ) ) ) { |
||
191 | /* translators: %s is a command like "prompt" */ |
||
192 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
||
193 | } |
||
194 | |||
195 | if ( in_array( $action, array( 'user' ) ) ) { |
||
196 | if ( isset( $args[1] ) ) { |
||
197 | $user_id = $args[1]; |
||
198 | if ( ctype_digit( $user_id ) ) { |
||
199 | $field = 'id'; |
||
200 | $user_id = (int) $user_id; |
||
201 | } elseif ( is_email( $user_id ) ) { |
||
202 | $field = 'email'; |
||
203 | $user_id = sanitize_user( $user_id, true ); |
||
204 | } else { |
||
205 | $field = 'login'; |
||
206 | $user_id = sanitize_user( $user_id, true ); |
||
207 | } |
||
208 | if ( ! $user = get_user_by( $field, $user_id ) ) { |
||
209 | WP_CLI::error( __( 'Please specify a valid user.', 'jetpack' ) ); |
||
210 | } |
||
211 | } else { |
||
212 | WP_CLI::error( __( 'Please specify a user by either ID, username, or email.', 'jetpack' ) ); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | switch ( $action ) { |
||
217 | case 'blog': |
||
218 | Jetpack::log( 'disconnect' ); |
||
219 | Jetpack::disconnect(); |
||
220 | WP_CLI::success( |
||
221 | sprintf( |
||
222 | /* translators: %s is the site URL */ |
||
223 | __( 'Jetpack has been successfully disconnected for %s.', 'jetpack' ), |
||
224 | esc_url( get_site_url() ) |
||
225 | ) |
||
226 | ); |
||
227 | break; |
||
228 | case 'user': |
||
229 | if ( Connection_Manager::disconnect_user( $user->ID ) ) { |
||
230 | Jetpack::log( 'unlink', $user->ID ); |
||
231 | WP_CLI::success( __( 'User has been successfully disconnected.', 'jetpack' ) ); |
||
232 | } else { |
||
233 | /* translators: %s is a username */ |
||
234 | WP_CLI::error( sprintf( __( "User %s could not be disconnected. Are you sure they're connected currently?", 'jetpack' ), "{$user->login} <{$user->email}>" ) ); |
||
235 | } |
||
236 | break; |
||
237 | case 'prompt': |
||
238 | WP_CLI::error( __( 'Please specify if you would like to disconnect a blog or user.', 'jetpack' ) ); |
||
239 | break; |
||
240 | } |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Reset Jetpack options and settings to default |
||
245 | * |
||
246 | * ## OPTIONS |
||
247 | * |
||
248 | * modules: Resets modules to default state ( get_default_modules() ) |
||
249 | * |
||
250 | * options: Resets all Jetpack options except: |
||
251 | * - All private options (Blog token, user token, etc...) |
||
252 | * - id (The Client ID/WP.com Blog ID of this site) |
||
253 | * - master_user |
||
254 | * - version |
||
255 | * - activated |
||
256 | * |
||
257 | * ## EXAMPLES |
||
258 | * |
||
259 | * wp jetpack reset options |
||
260 | * wp jetpack reset modules |
||
261 | * wp jetpack reset sync-checksum --dry-run --offset=0 |
||
262 | * |
||
263 | * @synopsis <modules|options|sync-checksum> [--dry-run] [--offset=<offset>] |
||
264 | */ |
||
265 | public function reset( $args, $assoc_args ) { |
||
266 | $action = isset( $args[0] ) ? $args[0] : 'prompt'; |
||
267 | View Code Duplication | if ( ! in_array( $action, array( 'options', 'modules', 'sync-checksum' ), true ) ) { |
|
268 | /* translators: %s is a command like "prompt" */ |
||
269 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
||
270 | } |
||
271 | |||
272 | $is_dry_run = ! empty( $assoc_args['dry-run'] ); |
||
273 | |||
274 | View Code Duplication | if ( $is_dry_run ) { |
|
275 | WP_CLI::warning( |
||
276 | __( "\nThis is a dry run.\n", 'jetpack' ) . |
||
277 | __( "No actions will be taken.\n", 'jetpack' ) . |
||
278 | __( "The following messages will give you preview of what will happen when you run this command.\n\n", 'jetpack' ) |
||
279 | ); |
||
280 | } else { |
||
281 | // We only need to confirm "Are you sure?" when we are not doing a dry run. |
||
282 | jetpack_cli_are_you_sure(); |
||
283 | } |
||
284 | |||
285 | switch ( $action ) { |
||
286 | case 'options': |
||
287 | $options_to_reset = Jetpack_Options::get_options_for_reset(); |
||
288 | // Reset the Jetpack options |
||
289 | WP_CLI::line( |
||
290 | sprintf( |
||
291 | /* translators: %s is the site URL */ |
||
292 | __( "Resetting Jetpack Options for %s...\n", 'jetpack' ), |
||
293 | esc_url( get_site_url() ) |
||
294 | ) |
||
295 | ); |
||
296 | sleep( 1 ); // Take a breath |
||
297 | View Code Duplication | foreach ( $options_to_reset['jp_options'] as $option_to_reset ) { |
|
298 | if ( ! $is_dry_run ) { |
||
299 | Jetpack_Options::delete_option( $option_to_reset ); |
||
300 | usleep( 100000 ); |
||
301 | } |
||
302 | |||
303 | /* translators: This is the result of an action. The option named %s was reset */ |
||
304 | WP_CLI::success( sprintf( __( '%s option reset', 'jetpack' ), $option_to_reset ) ); |
||
305 | } |
||
306 | |||
307 | // Reset the WP options |
||
308 | WP_CLI::line( __( "Resetting the jetpack options stored in wp_options...\n", 'jetpack' ) ); |
||
309 | usleep( 500000 ); // Take a breath |
||
310 | View Code Duplication | foreach ( $options_to_reset['wp_options'] as $option_to_reset ) { |
|
311 | if ( ! $is_dry_run ) { |
||
312 | delete_option( $option_to_reset ); |
||
313 | usleep( 100000 ); |
||
314 | } |
||
315 | /* translators: This is the result of an action. The option named %s was reset */ |
||
316 | WP_CLI::success( sprintf( __( '%s option reset', 'jetpack' ), $option_to_reset ) ); |
||
317 | } |
||
318 | |||
319 | // Reset to default modules |
||
320 | WP_CLI::line( __( "Resetting default modules...\n", 'jetpack' ) ); |
||
321 | usleep( 500000 ); // Take a breath |
||
322 | $default_modules = Jetpack::get_default_modules(); |
||
323 | if ( ! $is_dry_run ) { |
||
324 | Jetpack::update_active_modules( $default_modules ); |
||
325 | } |
||
326 | WP_CLI::success( __( 'Modules reset to default.', 'jetpack' ) ); |
||
327 | break; |
||
328 | View Code Duplication | case 'modules': |
|
329 | if ( ! $is_dry_run ) { |
||
330 | $default_modules = Jetpack::get_default_modules(); |
||
331 | Jetpack::update_active_modules( $default_modules ); |
||
332 | } |
||
333 | |||
334 | WP_CLI::success( __( 'Modules reset to default.', 'jetpack' ) ); |
||
335 | break; |
||
336 | case 'prompt': |
||
337 | WP_CLI::error( __( 'Please specify if you would like to reset your options, modules or sync-checksum', 'jetpack' ) ); |
||
338 | break; |
||
339 | case 'sync-checksum': |
||
340 | $option = 'jetpack_callables_sync_checksum'; |
||
341 | |||
342 | if ( is_multisite() ) { |
||
343 | $offset = isset( $assoc_args['offset'] ) ? (int) $assoc_args['offset'] : 0; |
||
344 | |||
345 | /* |
||
346 | * 1000 is a good limit since we don't expect the number of sites to be more than 1000 |
||
347 | * Offset can be used to paginate and try to clean up more sites. |
||
348 | */ |
||
349 | $sites = get_sites( |
||
350 | array( |
||
351 | 'number' => 1000, |
||
352 | 'offset' => $offset, |
||
353 | ) |
||
354 | ); |
||
355 | $count_fixes = 0; |
||
356 | foreach ( $sites as $site ) { |
||
357 | switch_to_blog( $site->blog_id ); |
||
358 | $count = self::count_option( $option ); |
||
359 | if ( $count > 1 ) { |
||
360 | if ( ! $is_dry_run ) { |
||
361 | delete_option( $option ); |
||
362 | } |
||
363 | WP_CLI::line( |
||
364 | sprintf( |
||
365 | /* translators: %1$d is a number, %2$s is the name of an option, %2$s is the site URL. */ |
||
366 | __( 'Deleted %1$d %2$s options from %3$s', 'jetpack' ), |
||
367 | $count, |
||
368 | $option, |
||
369 | "{$site->domain}{$site->path}" |
||
370 | ) |
||
371 | ); |
||
372 | $count_fixes++; |
||
373 | if ( ! $is_dry_run ) { |
||
374 | /* |
||
375 | * We could be deleting a lot of options rows at the same time. |
||
376 | * Allow some time for replication to catch up. |
||
377 | */ |
||
378 | sleep( 3 ); |
||
379 | } |
||
380 | } |
||
381 | |||
382 | restore_current_blog(); |
||
383 | } |
||
384 | if ( $count_fixes ) { |
||
385 | WP_CLI::success( |
||
386 | sprintf( |
||
387 | /* translators: %1$s is the name of an option, %2$d is a number of sites. */ |
||
388 | __( 'Successfully reset %1$s on %2$d sites.', 'jetpack' ), |
||
389 | $option, |
||
390 | $count_fixes |
||
391 | ) |
||
392 | ); |
||
393 | } else { |
||
394 | WP_CLI::success( __( 'No options were deleted.', 'jetpack' ) ); |
||
395 | } |
||
396 | return; |
||
397 | } |
||
398 | |||
399 | $count = self::count_option( $option ); |
||
400 | if ( $count > 1 ) { |
||
401 | if ( ! $is_dry_run ) { |
||
402 | delete_option( $option ); |
||
403 | } |
||
404 | WP_CLI::success( |
||
405 | sprintf( |
||
406 | /* translators: %1$d is a number, %2$s is the name of an option. */ |
||
407 | __( 'Deleted %1$d %2$s options', 'jetpack' ), |
||
408 | $count, |
||
409 | $option |
||
410 | ) |
||
411 | ); |
||
412 | return; |
||
413 | } |
||
414 | |||
415 | WP_CLI::success( __( 'No options were deleted.', 'jetpack' ) ); |
||
416 | break; |
||
417 | |||
418 | } |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * Return the number of times an option appears |
||
423 | * Normally an option would only appear 1 since the option key is supposed to be unique |
||
424 | * but if a site hasn't updated the DB schema then that would not be the case. |
||
425 | * |
||
426 | * @param string $option Option name. |
||
427 | * |
||
428 | * @return int |
||
429 | */ |
||
430 | private static function count_option( $option ) { |
||
440 | |||
441 | /** |
||
442 | * Manage Jetpack Modules |
||
443 | * |
||
444 | * ## OPTIONS |
||
445 | * |
||
446 | * <list|activate|deactivate|toggle> |
||
447 | * : The action to take. |
||
448 | * --- |
||
449 | * default: list |
||
450 | * options: |
||
451 | * - list |
||
452 | * - activate |
||
453 | * - deactivate |
||
454 | * - toggle |
||
455 | * --- |
||
456 | * |
||
457 | * [<module_slug>] |
||
458 | * : The slug of the module to perform an action on. |
||
459 | * |
||
460 | * [--format=<format>] |
||
461 | * : Allows overriding the output of the command when listing modules. |
||
462 | * --- |
||
463 | * default: table |
||
464 | * options: |
||
465 | * - table |
||
466 | * - json |
||
467 | * - csv |
||
468 | * - yaml |
||
469 | * - ids |
||
470 | * - count |
||
471 | * --- |
||
472 | * |
||
473 | * ## EXAMPLES |
||
474 | * |
||
475 | * wp jetpack module list |
||
476 | * wp jetpack module list --format=json |
||
477 | * wp jetpack module activate stats |
||
478 | * wp jetpack module deactivate stats |
||
479 | * wp jetpack module toggle stats |
||
480 | * wp jetpack module activate all |
||
481 | * wp jetpack module deactivate all |
||
482 | */ |
||
483 | public function module( $args, $assoc_args ) { |
||
557 | |||
558 | /** |
||
559 | * Manage Protect Settings |
||
560 | * |
||
561 | * ## OPTIONS |
||
562 | * |
||
563 | * whitelist: Whitelist an IP address. You can also read or clear the whitelist. |
||
564 | * |
||
565 | * |
||
566 | * ## EXAMPLES |
||
567 | * |
||
568 | * wp jetpack protect whitelist <ip address> |
||
569 | * wp jetpack protect whitelist list |
||
570 | * wp jetpack protect whitelist clear |
||
571 | * |
||
572 | * @synopsis <whitelist> [<ip|ip_low-ip_high|list|clear>] |
||
573 | */ |
||
574 | public function protect( $args, $assoc_args ) { |
||
679 | |||
680 | /** |
||
681 | * Manage Jetpack Options |
||
682 | * |
||
683 | * ## OPTIONS |
||
684 | * |
||
685 | * list : List all jetpack options and their values |
||
686 | * delete : Delete an option |
||
687 | * - can only delete options that are white listed. |
||
688 | * update : update an option |
||
689 | * - can only update option strings |
||
690 | * get : get the value of an option |
||
691 | * |
||
692 | * ## EXAMPLES |
||
693 | * |
||
694 | * wp jetpack options list |
||
695 | * wp jetpack options get <option_name> |
||
696 | * wp jetpack options delete <option_name> |
||
697 | * wp jetpack options update <option_name> [<option_value>] |
||
698 | * |
||
699 | * @synopsis <list|get|delete|update> [<option_name>] [<option_value>] |
||
700 | */ |
||
701 | public function options( $args, $assoc_args ) { |
||
797 | |||
798 | /** |
||
799 | * Get the status of or start a new Jetpack sync. |
||
800 | * |
||
801 | * ## OPTIONS |
||
802 | * |
||
803 | * status : Print the current sync status |
||
804 | * settings : Prints the current sync settings |
||
805 | * start : Start a full sync from this site to WordPress.com |
||
806 | * enable : Enables sync on the site |
||
807 | * disable : Disable sync on a site |
||
808 | * reset : Disables sync and Resets the sync queues on a site |
||
809 | * |
||
810 | * ## EXAMPLES |
||
811 | * |
||
812 | * wp jetpack sync status |
||
813 | * wp jetpack sync settings |
||
814 | * wp jetpack sync start --modules=functions --sync_wait_time=5 |
||
815 | * wp jetpack sync enable |
||
816 | * wp jetpack sync disable |
||
817 | * wp jetpack sync reset |
||
818 | * wp jetpack sync reset --queue=full or regular |
||
819 | * |
||
820 | * @synopsis <status|start> [--<field>=<value>] |
||
821 | */ |
||
822 | public function sync( $args, $assoc_args ) { |
||
1004 | |||
1005 | /** |
||
1006 | * List the contents of a specific Jetpack sync queue. |
||
1007 | * |
||
1008 | * ## OPTIONS |
||
1009 | * |
||
1010 | * peek : List the 100 front-most items on the queue. |
||
1011 | * |
||
1012 | * ## EXAMPLES |
||
1013 | * |
||
1014 | * wp jetpack sync_queue full_sync peek |
||
1015 | * |
||
1016 | * @synopsis <incremental|full_sync> <peek> |
||
1017 | */ |
||
1018 | public function sync_queue( $args, $assoc_args ) { |
||
1068 | |||
1069 | /** |
||
1070 | * Cancel's the current Jetpack plan granted by this partner, if applicable |
||
1071 | * |
||
1072 | * Returns success or error JSON |
||
1073 | * |
||
1074 | * <token_json> |
||
1075 | * : JSON blob of WPCOM API token |
||
1076 | * [--partner_tracking_id=<partner_tracking_id>] |
||
1077 | * : This is an optional ID that a host can pass to help identify a site in logs on WordPress.com |
||
1078 | * |
||
1079 | * * @synopsis <token_json> [--partner_tracking_id=<partner_tracking_id>] |
||
1080 | */ |
||
1081 | public function partner_cancel( $args, $named_args ) { |
||
1136 | |||
1137 | /** |
||
1138 | * Provision a site using a Jetpack Partner license |
||
1139 | * |
||
1140 | * Returns JSON blob |
||
1141 | * |
||
1142 | * ## OPTIONS |
||
1143 | * |
||
1144 | * <token_json> |
||
1145 | * : JSON blob of WPCOM API token |
||
1146 | * [--plan=<plan_name>] |
||
1147 | * : Slug of the requested plan, e.g. premium |
||
1148 | * [--wpcom_user_id=<user_id>] |
||
1149 | * : WordPress.com ID of user to connect as (must be whitelisted against partner key) |
||
1150 | * [--wpcom_user_email=<wpcom_user_email>] |
||
1151 | * : Override the email we send to WordPress.com for registration |
||
1152 | * [--onboarding=<onboarding>] |
||
1153 | * : Guide the user through an onboarding wizard |
||
1154 | * [--force_register=<register>] |
||
1155 | * : Whether to force a site to register |
||
1156 | * [--force_connect=<force_connect>] |
||
1157 | * : Force JPS to not reuse existing credentials |
||
1158 | * [--home_url=<home_url>] |
||
1159 | * : Overrides the home option via the home_url filter, or the WP_HOME constant |
||
1160 | * [--site_url=<site_url>] |
||
1161 | * : Overrides the siteurl option via the site_url filter, or the WP_SITEURL constant |
||
1162 | * [--partner_tracking_id=<partner_tracking_id>] |
||
1163 | * : This is an optional ID that a host can pass to help identify a site in logs on WordPress.com |
||
1164 | * |
||
1165 | * ## EXAMPLES |
||
1166 | * |
||
1167 | * $ wp jetpack partner_provision '{ some: "json" }' premium 1 |
||
1168 | * { success: true } |
||
1169 | * |
||
1170 | * @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>] |
||
1171 | */ |
||
1172 | public function partner_provision( $args, $named_args ) { |
||
1210 | |||
1211 | /** |
||
1212 | * Manages your Jetpack sitemap |
||
1213 | * |
||
1214 | * ## OPTIONS |
||
1215 | * |
||
1216 | * rebuild : Rebuild all sitemaps |
||
1217 | * --purge : if set, will remove all existing sitemap data before rebuilding |
||
1218 | * |
||
1219 | * ## EXAMPLES |
||
1220 | * |
||
1221 | * wp jetpack sitemap rebuild |
||
1222 | * |
||
1223 | * @subcommand sitemap |
||
1224 | * @synopsis <rebuild> [--purge] |
||
1225 | */ |
||
1226 | public function sitemap( $args, $assoc_args ) { |
||
1245 | |||
1246 | /** |
||
1247 | * Allows authorizing a user via the command line and will activate |
||
1248 | * |
||
1249 | * ## EXAMPLES |
||
1250 | * |
||
1251 | * wp jetpack authorize_user --token=123456789abcdef |
||
1252 | * |
||
1253 | * @synopsis --token=<value> |
||
1254 | */ |
||
1255 | public function authorize_user( $args, $named_args ) { |
||
1289 | |||
1290 | /** |
||
1291 | * Allows calling a WordPress.com API endpoint using the current blog's token. |
||
1292 | * |
||
1293 | * ## OPTIONS |
||
1294 | * --resource=<resource> |
||
1295 | * : The resource to call with the current blog's token, where `%d` represents the current blog's ID. |
||
1296 | * |
||
1297 | * [--api_version=<api_version>] |
||
1298 | * : The API version to query against. |
||
1299 | * |
||
1300 | * [--base_api_path=<base_api_path>] |
||
1301 | * : The base API path to query. |
||
1302 | * --- |
||
1303 | * default: rest |
||
1304 | * --- |
||
1305 | * |
||
1306 | * [--body=<body>] |
||
1307 | * : A JSON encoded string representing arguments to send in the body. |
||
1308 | * |
||
1309 | * [--field=<value>] |
||
1310 | * : Any number of arguments that should be passed to the resource. |
||
1311 | * |
||
1312 | * [--pretty] |
||
1313 | * : Will pretty print the results of a successful API call. |
||
1314 | * |
||
1315 | * [--strip-success] |
||
1316 | * : Will remove the green success label from successful API calls. |
||
1317 | * |
||
1318 | * ## EXAMPLES |
||
1319 | * |
||
1320 | * wp jetpack call_api --resource='/sites/%d' |
||
1321 | */ |
||
1322 | public function call_api( $args, $named_args ) { |
||
1392 | |||
1393 | /** |
||
1394 | * Allows uploading SSH Credentials to the current site for backups, restores, and security scanning. |
||
1395 | * |
||
1396 | * ## OPTIONS |
||
1397 | * |
||
1398 | * [--host=<host>] |
||
1399 | * : The SSH server's address. |
||
1400 | * |
||
1401 | * [--ssh-user=<user>] |
||
1402 | * : The username to use to log in to the SSH server. |
||
1403 | * |
||
1404 | * [--pass=<pass>] |
||
1405 | * : The password used to log in, if using a password. (optional) |
||
1406 | * |
||
1407 | * [--kpri=<kpri>] |
||
1408 | * : The private key used to log in, if using a private key. (optional) |
||
1409 | * |
||
1410 | * [--pretty] |
||
1411 | * : Will pretty print the results of a successful API call. (optional) |
||
1412 | * |
||
1413 | * [--strip-success] |
||
1414 | * : Will remove the green success label from successful API calls. (optional) |
||
1415 | * |
||
1416 | * ## EXAMPLES |
||
1417 | * |
||
1418 | * wp jetpack upload_ssh_creds --host=example.com --ssh-user=example --pass=password |
||
1419 | * wp jetpack updload_ssh_creds --host=example.com --ssh-user=example --kpri=key |
||
1420 | */ |
||
1421 | public function upload_ssh_creds( $args, $named_args ) { |
||
1474 | |||
1475 | /** |
||
1476 | * API wrapper for getting stats from the WordPress.com API for the current site. |
||
1477 | * |
||
1478 | * ## OPTIONS |
||
1479 | * |
||
1480 | * [--quantity=<quantity>] |
||
1481 | * : The number of units to include. |
||
1482 | * --- |
||
1483 | * default: 30 |
||
1484 | * --- |
||
1485 | * |
||
1486 | * [--period=<period>] |
||
1487 | * : The unit of time to query stats for. |
||
1488 | * --- |
||
1489 | * default: day |
||
1490 | * options: |
||
1491 | * - day |
||
1492 | * - week |
||
1493 | * - month |
||
1494 | * - year |
||
1495 | * --- |
||
1496 | * |
||
1497 | * [--date=<date>] |
||
1498 | * : The latest date to return stats for. Ex. - 2018-01-01. |
||
1499 | * |
||
1500 | * [--pretty] |
||
1501 | * : Will pretty print the results of a successful API call. |
||
1502 | * |
||
1503 | * [--strip-success] |
||
1504 | * : Will remove the green success label from successful API calls. |
||
1505 | * |
||
1506 | * ## EXAMPLES |
||
1507 | * |
||
1508 | * wp jetpack get_stats |
||
1509 | */ |
||
1510 | public function get_stats( $args, $named_args ) { |
||
1545 | |||
1546 | /** |
||
1547 | * Allows management of publicize connections. |
||
1548 | * |
||
1549 | * ## OPTIONS |
||
1550 | * |
||
1551 | * <list|disconnect> |
||
1552 | * : The action to perform. |
||
1553 | * --- |
||
1554 | * options: |
||
1555 | * - list |
||
1556 | * - disconnect |
||
1557 | * --- |
||
1558 | * |
||
1559 | * [<identifier>] |
||
1560 | * : The connection ID or service to perform an action on. |
||
1561 | * |
||
1562 | * [--format=<format>] |
||
1563 | * : Allows overriding the output of the command when listing connections. |
||
1564 | * --- |
||
1565 | * default: table |
||
1566 | * options: |
||
1567 | * - table |
||
1568 | * - json |
||
1569 | * - csv |
||
1570 | * - yaml |
||
1571 | * - ids |
||
1572 | * - count |
||
1573 | * --- |
||
1574 | * |
||
1575 | * ## EXAMPLES |
||
1576 | * |
||
1577 | * # List all publicize connections. |
||
1578 | * $ wp jetpack publicize list |
||
1579 | * |
||
1580 | * # List publicize connections for a given service. |
||
1581 | * $ wp jetpack publicize list twitter |
||
1582 | * |
||
1583 | * # List all publicize connections for a given user. |
||
1584 | * $ wp --user=1 jetpack publicize list |
||
1585 | * |
||
1586 | * # List all publicize connections for a given user and service. |
||
1587 | * $ wp --user=1 jetpack publicize list twitter |
||
1588 | * |
||
1589 | * # Display details for a given connection. |
||
1590 | * $ wp jetpack publicize list 123456 |
||
1591 | * |
||
1592 | * # Diconnection a given connection. |
||
1593 | * $ wp jetpack publicize disconnect 123456 |
||
1594 | * |
||
1595 | * # Disconnect all connections. |
||
1596 | * $ wp jetpack publicize disconnect all |
||
1597 | * |
||
1598 | * # Disconnect all connections for a given service. |
||
1599 | * $ wp jetpack publicize disconnect twitter |
||
1600 | */ |
||
1601 | public function publicize( $args, $named_args ) { |
||
1776 | |||
1777 | private function get_api_host() { |
||
1781 | |||
1782 | private function partner_provision_error( $error ) { |
||
1794 | |||
1795 | /** |
||
1796 | * Creates the essential files in Jetpack to start building a Gutenberg block or plugin. |
||
1797 | * |
||
1798 | * ## TYPES |
||
1799 | * |
||
1800 | * 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. |
||
1801 | * |
||
1802 | * ## BLOCK TYPE OPTIONS |
||
1803 | * |
||
1804 | * The first parameter is the block title and it's not associative. Add it wrapped in quotes. |
||
1805 | * 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. |
||
1806 | * --slug: Specific slug to identify the block that overrides the one generated based on the title. |
||
1807 | * --description: Allows to provide a text description of the block. |
||
1808 | * --keywords: Provide up to three keywords separated by comma so users can find this block when they search in Gutenberg's inserter. |
||
1809 | * |
||
1810 | * ## BLOCK TYPE EXAMPLES |
||
1811 | * |
||
1812 | * wp jetpack scaffold block "Cool Block" |
||
1813 | * wp jetpack scaffold block "Amazing Rock" --slug="good-music" --description="Rock the best music on your site" |
||
1814 | * wp jetpack scaffold block "Jukebox" --keywords="music, audio, media" |
||
1815 | * |
||
1816 | * @subcommand scaffold block |
||
1817 | * @synopsis <type> <title> [--slug] [--description] [--keywords] |
||
1818 | * |
||
1819 | * @param array $args Positional parameters, when strings are passed, wrap them in quotes. |
||
1820 | * @param array $assoc_args Associative parameters like --slug="nice-block". |
||
1821 | */ |
||
1822 | public function scaffold( $args, $assoc_args ) { |
||
1834 | |||
1835 | /** |
||
1836 | * Creates the essential files in Jetpack to build a Gutenberg block. |
||
1837 | * |
||
1838 | * @param array $args Positional parameters. Only one is used, that corresponds to the block title. |
||
1839 | * @param array $assoc_args Associative parameters defined in the scaffold() method. |
||
1840 | */ |
||
1841 | public function block( $args, $assoc_args ) { |
||
1975 | |||
1976 | /** |
||
1977 | * Built the file replacing the placeholders in the template with the data supplied. |
||
1978 | * |
||
1979 | * @param string $template |
||
1980 | * @param array $data |
||
1981 | * |
||
1982 | * @return string mixed |
||
1983 | */ |
||
1984 | private static function render_block_file( $template, $data = array() ) { |
||
1987 | } |
||
1988 | |||
2024 |
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.