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 |
||
17 | class Jetpack_CLI extends WP_CLI_Command { |
||
18 | // Aesthetics. |
||
19 | public $green_open = "\033[32m"; |
||
20 | public $red_open = "\033[31m"; |
||
21 | public $yellow_open = "\033[33m"; |
||
22 | public $color_close = "\033[0m"; |
||
23 | |||
24 | /** |
||
25 | * Get Jetpack Details |
||
26 | * |
||
27 | * ## OPTIONS |
||
28 | * |
||
29 | * empty: Leave it empty for basic stats |
||
30 | * |
||
31 | * full: View full stats. It's the data from the heartbeat |
||
32 | * |
||
33 | * ## EXAMPLES |
||
34 | * |
||
35 | * wp jetpack status |
||
36 | * wp jetpack status full |
||
37 | */ |
||
38 | public function status( $args, $assoc_args ) { |
||
117 | |||
118 | /** |
||
119 | * Tests the active connection |
||
120 | * |
||
121 | * 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. |
||
122 | * |
||
123 | * ## EXAMPLES |
||
124 | * |
||
125 | * wp jetpack test-connection |
||
126 | * |
||
127 | * @subcommand test-connection |
||
128 | */ |
||
129 | public function test_connection( $args, $assoc_args ) { |
||
130 | |||
131 | /* translators: %s is the site URL */ |
||
132 | WP_CLI::line( sprintf( __( 'Testing connection for %s', 'jetpack' ), esc_url( get_site_url() ) ) ); |
||
133 | |||
134 | if ( ! Jetpack::is_active() ) { |
||
135 | WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) ); |
||
136 | } |
||
137 | |||
138 | $response = Client::wpcom_json_api_request_as_blog( |
||
139 | sprintf( '/jetpack-blogs/%d/test-connection', Jetpack_Options::get_option( 'id' ) ), |
||
140 | Client::WPCOM_JSON_API_VERSION |
||
141 | ); |
||
142 | |||
143 | if ( is_wp_error( $response ) ) { |
||
144 | /* translators: %1$s is the error code, %2$s is the error message */ |
||
145 | WP_CLI::error( sprintf( __( 'Failed to test connection (#%1$s: %2$s)', 'jetpack' ), $response->get_error_code(), $response->get_error_message() ) ); |
||
146 | } |
||
147 | |||
148 | $body = wp_remote_retrieve_body( $response ); |
||
149 | if ( ! $body ) { |
||
150 | WP_CLI::error( __( 'Failed to test connection (empty response body)', 'jetpack' ) ); |
||
151 | } |
||
152 | |||
153 | $result = json_decode( $body ); |
||
154 | $is_connected = (bool) $result->connected; |
||
155 | $message = $result->message; |
||
156 | |||
157 | if ( $is_connected ) { |
||
158 | WP_CLI::success( $message ); |
||
159 | } else { |
||
160 | WP_CLI::error( $message ); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Disconnect Jetpack Blogs or Users |
||
166 | * |
||
167 | * ## OPTIONS |
||
168 | * |
||
169 | * blog: Disconnect the entire blog. |
||
170 | * |
||
171 | * user <user_identifier>: Disconnect a specific user from WordPress.com. |
||
172 | * |
||
173 | * Please note, the primary account that the blog is connected |
||
174 | * to WordPress.com with cannot be disconnected without |
||
175 | * disconnecting the entire blog. |
||
176 | * |
||
177 | * ## EXAMPLES |
||
178 | * |
||
179 | * wp jetpack disconnect blog |
||
180 | * wp jetpack disconnect user 13 |
||
181 | * wp jetpack disconnect user username |
||
182 | * wp jetpack disconnect user [email protected] |
||
183 | * |
||
184 | * @synopsis <blog|user> [<user_identifier>] |
||
185 | */ |
||
186 | public function disconnect( $args, $assoc_args ) { |
||
187 | if ( ! Jetpack::is_active() ) { |
||
188 | WP_CLI::success( __( 'The site is not currently connected, so nothing to do!', 'jetpack' ) ); |
||
189 | return; |
||
190 | } |
||
191 | |||
192 | $action = isset( $args[0] ) ? $args[0] : 'prompt'; |
||
193 | View Code Duplication | if ( ! in_array( $action, array( 'blog', 'user', 'prompt' ) ) ) { |
|
194 | /* translators: %s is a command like "prompt" */ |
||
195 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
||
196 | } |
||
197 | |||
198 | if ( in_array( $action, array( 'user' ) ) ) { |
||
199 | if ( isset( $args[1] ) ) { |
||
200 | $user_id = $args[1]; |
||
201 | if ( ctype_digit( $user_id ) ) { |
||
202 | $field = 'id'; |
||
203 | $user_id = (int) $user_id; |
||
204 | } elseif ( is_email( $user_id ) ) { |
||
205 | $field = 'email'; |
||
206 | $user_id = sanitize_user( $user_id, true ); |
||
207 | } else { |
||
208 | $field = 'login'; |
||
209 | $user_id = sanitize_user( $user_id, true ); |
||
210 | } |
||
211 | if ( ! $user = get_user_by( $field, $user_id ) ) { |
||
212 | WP_CLI::error( __( 'Please specify a valid user.', 'jetpack' ) ); |
||
213 | } |
||
214 | } else { |
||
215 | WP_CLI::error( __( 'Please specify a user by either ID, username, or email.', 'jetpack' ) ); |
||
216 | } |
||
217 | } |
||
218 | |||
219 | switch ( $action ) { |
||
220 | case 'blog': |
||
221 | Jetpack::log( 'disconnect' ); |
||
222 | Jetpack::disconnect(); |
||
223 | WP_CLI::success( |
||
224 | sprintf( |
||
225 | /* translators: %s is the site URL */ |
||
226 | __( 'Jetpack has been successfully disconnected for %s.', 'jetpack' ), |
||
227 | esc_url( get_site_url() ) |
||
228 | ) |
||
229 | ); |
||
230 | break; |
||
231 | case 'user': |
||
232 | if ( Connection_Manager::disconnect_user( $user->ID ) ) { |
||
233 | Jetpack::log( 'unlink', $user->ID ); |
||
234 | WP_CLI::success( __( 'User has been successfully disconnected.', 'jetpack' ) ); |
||
235 | } else { |
||
236 | /* translators: %s is a username */ |
||
237 | WP_CLI::error( sprintf( __( "User %s could not be disconnected. Are you sure they're connected currently?", 'jetpack' ), "{$user->login} <{$user->email}>" ) ); |
||
238 | } |
||
239 | break; |
||
240 | case 'prompt': |
||
241 | WP_CLI::error( __( 'Please specify if you would like to disconnect a blog or user.', 'jetpack' ) ); |
||
242 | break; |
||
243 | } |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Reset Jetpack options and settings to default |
||
248 | * |
||
249 | * ## OPTIONS |
||
250 | * |
||
251 | * modules: Resets modules to default state ( get_default_modules() ) |
||
252 | * |
||
253 | * options: Resets all Jetpack options except: |
||
254 | * - All private options (Blog token, user token, etc...) |
||
255 | * - id (The Client ID/WP.com Blog ID of this site) |
||
256 | * - master_user |
||
257 | * - version |
||
258 | * - activated |
||
259 | * |
||
260 | * ## EXAMPLES |
||
261 | * |
||
262 | * wp jetpack reset options |
||
263 | * wp jetpack reset modules |
||
264 | * wp jetpack reset sync-checksum --dry-run --offset=0 |
||
265 | * |
||
266 | * @synopsis <modules|options|sync-checksum> [--dry-run] [--offset=<offset>] |
||
267 | */ |
||
268 | public function reset( $args, $assoc_args ) { |
||
423 | |||
424 | /** |
||
425 | * Return the number of times an option appears |
||
426 | * Normally an option would only appear 1 since the option key is supposed to be unique |
||
427 | * but if a site hasn't updated the DB schema then that would not be the case. |
||
428 | * |
||
429 | * @param string $option Option name. |
||
430 | * |
||
431 | * @return int |
||
432 | */ |
||
433 | private static function count_option( $option ) { |
||
443 | |||
444 | /** |
||
445 | * Manage Jetpack Modules |
||
446 | * |
||
447 | * ## OPTIONS |
||
448 | * |
||
449 | * <list|activate|deactivate|toggle> |
||
450 | * : The action to take. |
||
451 | * --- |
||
452 | * default: list |
||
453 | * options: |
||
454 | * - list |
||
455 | * - activate |
||
456 | * - deactivate |
||
457 | * - toggle |
||
458 | * --- |
||
459 | * |
||
460 | * [<module_slug>] |
||
461 | * : The slug of the module to perform an action on. |
||
462 | * |
||
463 | * [--format=<format>] |
||
464 | * : Allows overriding the output of the command when listing modules. |
||
465 | * --- |
||
466 | * default: table |
||
467 | * options: |
||
468 | * - table |
||
469 | * - json |
||
470 | * - csv |
||
471 | * - yaml |
||
472 | * - ids |
||
473 | * - count |
||
474 | * --- |
||
475 | * |
||
476 | * ## EXAMPLES |
||
477 | * |
||
478 | * wp jetpack module list |
||
479 | * wp jetpack module list --format=json |
||
480 | * wp jetpack module activate stats |
||
481 | * wp jetpack module deactivate stats |
||
482 | * wp jetpack module toggle stats |
||
483 | * wp jetpack module activate all |
||
484 | * wp jetpack module deactivate all |
||
485 | */ |
||
486 | public function module( $args, $assoc_args ) { |
||
560 | |||
561 | /** |
||
562 | * Manage Protect Settings |
||
563 | * |
||
564 | * ## OPTIONS |
||
565 | * |
||
566 | * whitelist: Whitelist an IP address. You can also read or clear the whitelist. |
||
567 | * |
||
568 | * |
||
569 | * ## EXAMPLES |
||
570 | * |
||
571 | * wp jetpack protect whitelist <ip address> |
||
572 | * wp jetpack protect whitelist list |
||
573 | * wp jetpack protect whitelist clear |
||
574 | * |
||
575 | * @synopsis <whitelist> [<ip|ip_low-ip_high|list|clear>] |
||
576 | */ |
||
577 | public function protect( $args, $assoc_args ) { |
||
682 | |||
683 | /** |
||
684 | * Manage Jetpack Options |
||
685 | * |
||
686 | * ## OPTIONS |
||
687 | * |
||
688 | * list : List all jetpack options and their values |
||
689 | * delete : Delete an option |
||
690 | * - can only delete options that are white listed. |
||
691 | * update : update an option |
||
692 | * - can only update option strings |
||
693 | * get : get the value of an option |
||
694 | * |
||
695 | * ## EXAMPLES |
||
696 | * |
||
697 | * wp jetpack options list |
||
698 | * wp jetpack options get <option_name> |
||
699 | * wp jetpack options delete <option_name> |
||
700 | * wp jetpack options update <option_name> [<option_value>] |
||
701 | * |
||
702 | * @synopsis <list|get|delete|update> [<option_name>] [<option_value>] |
||
703 | */ |
||
704 | public function options( $args, $assoc_args ) { |
||
800 | |||
801 | /** |
||
802 | * Get the status of or start a new Jetpack sync. |
||
803 | * |
||
804 | * ## OPTIONS |
||
805 | * |
||
806 | * status : Print the current sync status |
||
807 | * settings : Prints the current sync settings |
||
808 | * start : Start a full sync from this site to WordPress.com |
||
809 | * enable : Enables sync on the site |
||
810 | * disable : Disable sync on a site |
||
811 | * reset : Disables sync and Resets the sync queues on a site |
||
812 | * |
||
813 | * ## EXAMPLES |
||
814 | * |
||
815 | * wp jetpack sync status |
||
816 | * wp jetpack sync settings |
||
817 | * wp jetpack sync start --modules=functions --sync_wait_time=5 |
||
818 | * wp jetpack sync enable |
||
819 | * wp jetpack sync disable |
||
820 | * wp jetpack sync reset |
||
821 | * wp jetpack sync reset --queue=full or regular |
||
822 | * |
||
823 | * @synopsis <status|start> [--<field>=<value>] |
||
824 | */ |
||
825 | public function sync( $args, $assoc_args ) { |
||
826 | |||
827 | $action = isset( $args[0] ) ? $args[0] : 'status'; |
||
828 | |||
829 | switch ( $action ) { |
||
830 | case 'status': |
||
831 | $status = Actions::get_sync_status(); |
||
832 | $collection = array(); |
||
833 | foreach ( $status as $key => $item ) { |
||
834 | $collection[] = array( |
||
835 | 'option' => $key, |
||
836 | 'value' => is_scalar( $item ) ? $item : json_encode( $item ), |
||
837 | ); |
||
838 | } |
||
839 | WP_CLI::log( __( 'Sync Status:', 'jetpack' ) ); |
||
840 | WP_CLI\Utils\format_items( 'table', $collection, array( 'option', 'value' ) ); |
||
841 | break; |
||
842 | case 'settings': |
||
843 | WP_CLI::log( __( 'Sync Settings:', 'jetpack' ) ); |
||
844 | foreach ( Settings::get_settings() as $setting => $item ) { |
||
845 | $settings[] = array( |
||
846 | 'setting' => $setting, |
||
847 | 'value' => is_scalar( $item ) ? $item : json_encode( $item ), |
||
848 | ); |
||
849 | } |
||
850 | WP_CLI\Utils\format_items( 'table', $settings, array( 'setting', 'value' ) ); |
||
851 | |||
852 | case 'disable': |
||
853 | // Don't set it via the Settings since that also resets the queues. |
||
854 | update_option( 'jetpack_sync_settings_disable', 1 ); |
||
855 | /* translators: %s is the site URL */ |
||
856 | WP_CLI::log( sprintf( __( 'Sync Disabled on %s', 'jetpack' ), get_site_url() ) ); |
||
857 | break; |
||
858 | case 'enable': |
||
859 | Settings::update_settings( array( 'disable' => 0 ) ); |
||
860 | /* translators: %s is the site URL */ |
||
861 | WP_CLI::log( sprintf( __( 'Sync Enabled on %s', 'jetpack' ), get_site_url() ) ); |
||
862 | break; |
||
863 | case 'reset': |
||
864 | // Don't set it via the Settings since that also resets the queues. |
||
865 | update_option( 'jetpack_sync_settings_disable', 1 ); |
||
866 | |||
867 | /* translators: %s is the site URL */ |
||
868 | WP_CLI::log( sprintf( __( 'Sync Disabled on %s. Use `wp jetpack sync enable` to enable syncing again.', 'jetpack' ), get_site_url() ) ); |
||
869 | $listener = Listener::get_instance(); |
||
870 | if ( empty( $assoc_args['queue'] ) ) { |
||
871 | $listener->get_sync_queue()->reset(); |
||
872 | $listener->get_full_sync_queue()->reset(); |
||
873 | /* translators: %s is the site URL */ |
||
874 | WP_CLI::log( sprintf( __( 'Reset Full Sync and Regular Queues Queue on %s', 'jetpack' ), get_site_url() ) ); |
||
875 | break; |
||
876 | } |
||
877 | |||
878 | if ( ! empty( $assoc_args['queue'] ) ) { |
||
879 | switch ( $assoc_args['queue'] ) { |
||
880 | View Code Duplication | case 'regular': |
|
881 | $listener->get_sync_queue()->reset(); |
||
882 | /* translators: %s is the site URL */ |
||
883 | WP_CLI::log( sprintf( __( 'Reset Regular Sync Queue on %s', 'jetpack' ), get_site_url() ) ); |
||
884 | break; |
||
885 | View Code Duplication | case 'full': |
|
886 | $listener->get_full_sync_queue()->reset(); |
||
887 | /* translators: %s is the site URL */ |
||
888 | WP_CLI::log( sprintf( __( 'Reset Full Sync Queue on %s', 'jetpack' ), get_site_url() ) ); |
||
889 | break; |
||
890 | default: |
||
891 | WP_CLI::error( __( 'Please specify what type of queue do you want to reset: `full` or `regular`.', 'jetpack' ) ); |
||
892 | break; |
||
893 | } |
||
894 | } |
||
895 | |||
896 | break; |
||
897 | case 'start': |
||
898 | if ( ! Actions::sync_allowed() ) { |
||
899 | if ( ! Settings::get_setting( 'disable' ) ) { |
||
900 | WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site. It is currently disabled. Run `wp jetpack sync enable` to enable it.', 'jetpack' ) ); |
||
901 | return; |
||
902 | } |
||
903 | if ( doing_action( 'jetpack_user_authorized' ) || Jetpack::is_active() ) { |
||
904 | WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site. Jetpack is not connected.', 'jetpack' ) ); |
||
905 | return; |
||
906 | } |
||
907 | |||
908 | $status = new Status(); |
||
909 | |||
910 | if ( $status->is_development_mode() ) { |
||
911 | WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site. The site is in development mode.', 'jetpack' ) ); |
||
912 | return; |
||
913 | } |
||
914 | if ( $status->is_staging_site() ) { |
||
915 | WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site. The site is in staging mode.', 'jetpack' ) ); |
||
916 | return; |
||
917 | } |
||
918 | } |
||
919 | // Get the original settings so that we can restore them later |
||
920 | $original_settings = Settings::get_settings(); |
||
921 | |||
922 | // Initialize sync settigns so we can sync as quickly as possible |
||
923 | $sync_settings = wp_parse_args( |
||
924 | array_intersect_key( $assoc_args, Settings::$valid_settings ), |
||
925 | array( |
||
926 | 'sync_wait_time' => 0, |
||
927 | 'enqueue_wait_time' => 0, |
||
928 | 'queue_max_writes_sec' => 10000, |
||
929 | 'max_queue_size_full_sync' => 100000, |
||
930 | 'full_sync_send_duration' => HOUR_IN_SECONDS, |
||
931 | ) |
||
932 | ); |
||
933 | Settings::update_settings( $sync_settings ); |
||
934 | |||
935 | // Convert comma-delimited string of modules to an array |
||
936 | View Code Duplication | if ( ! empty( $assoc_args['modules'] ) ) { |
|
937 | $modules = array_map( 'trim', explode( ',', $assoc_args['modules'] ) ); |
||
938 | |||
939 | // Convert the array so that the keys are the module name and the value is true to indicate |
||
940 | // that we want to sync the module |
||
941 | $modules = array_map( '__return_true', array_flip( $modules ) ); |
||
942 | } |
||
943 | |||
944 | View Code Duplication | foreach ( array( 'posts', 'comments', 'users' ) as $module_name ) { |
|
945 | if ( |
||
946 | 'users' === $module_name && |
||
947 | isset( $assoc_args[ $module_name ] ) && |
||
948 | 'initial' === $assoc_args[ $module_name ] |
||
949 | ) { |
||
950 | $modules['users'] = 'initial'; |
||
951 | } elseif ( isset( $assoc_args[ $module_name ] ) ) { |
||
952 | $ids = explode( ',', $assoc_args[ $module_name ] ); |
||
953 | if ( count( $ids ) > 0 ) { |
||
954 | $modules[ $module_name ] = $ids; |
||
955 | } |
||
956 | } |
||
957 | } |
||
958 | |||
959 | if ( empty( $modules ) ) { |
||
960 | $modules = null; |
||
961 | } |
||
962 | |||
963 | // Kick off a full sync |
||
964 | if ( Actions::do_full_sync( $modules ) ) { |
||
965 | View Code Duplication | if ( $modules ) { |
|
966 | /* translators: %s is a comma separated list of Jetpack modules */ |
||
967 | WP_CLI::log( sprintf( __( 'Initialized a new full sync with modules: %s', 'jetpack' ), join( ', ', array_keys( $modules ) ) ) ); |
||
968 | } else { |
||
969 | WP_CLI::log( __( 'Initialized a new full sync', 'jetpack' ) ); |
||
970 | } |
||
971 | View Code Duplication | } else { |
|
972 | |||
973 | // Reset sync settings to original. |
||
974 | Settings::update_settings( $original_settings ); |
||
975 | |||
976 | if ( $modules ) { |
||
977 | /* translators: %s is a comma separated list of Jetpack modules */ |
||
978 | WP_CLI::error( sprintf( __( 'Could not start a new full sync with modules: %s', 'jetpack' ), join( ', ', $modules ) ) ); |
||
979 | } else { |
||
980 | WP_CLI::error( __( 'Could not start a new full sync', 'jetpack' ) ); |
||
981 | } |
||
982 | } |
||
983 | |||
984 | // Keep sending to WPCOM until there's nothing to send |
||
985 | $i = 1; |
||
986 | do { |
||
987 | $result = Actions::$sender->do_full_sync(); |
||
988 | if ( is_wp_error( $result ) ) { |
||
989 | $queue_empty_error = ( 'empty_queue_full_sync' == $result->get_error_code() ); |
||
990 | if ( ! $queue_empty_error || ( $queue_empty_error && ( 1 == $i ) ) ) { |
||
991 | /* translators: %s is an error code */ |
||
992 | WP_CLI::error( sprintf( __( 'Sync errored with code: %s', 'jetpack' ), $result->get_error_code() ) ); |
||
993 | } |
||
994 | } else { |
||
995 | if ( 1 == $i ) { |
||
996 | WP_CLI::log( __( 'Sent data to WordPress.com', 'jetpack' ) ); |
||
997 | } else { |
||
998 | WP_CLI::log( __( 'Sent more data to WordPress.com', 'jetpack' ) ); |
||
999 | } |
||
1000 | } |
||
1001 | $i++; |
||
1002 | } while ( $result && ! is_wp_error( $result ) ); |
||
1003 | |||
1004 | // Reset sync settings to original. |
||
1005 | Settings::update_settings( $original_settings ); |
||
1006 | |||
1007 | WP_CLI::success( __( 'Finished syncing to WordPress.com', 'jetpack' ) ); |
||
1008 | break; |
||
1009 | } |
||
1010 | } |
||
1011 | |||
1012 | /** |
||
1013 | * List the contents of a specific Jetpack sync queue. |
||
1014 | * |
||
1015 | * ## OPTIONS |
||
1016 | * |
||
1017 | * peek : List the 100 front-most items on the queue. |
||
1018 | * |
||
1019 | * ## EXAMPLES |
||
1020 | * |
||
1021 | * wp jetpack sync_queue full_sync peek |
||
1022 | * |
||
1023 | * @synopsis <incremental|full_sync> <peek> |
||
1024 | */ |
||
1025 | public function sync_queue( $args, $assoc_args ) { |
||
1075 | |||
1076 | /** |
||
1077 | * Cancel's the current Jetpack plan granted by this partner, if applicable |
||
1078 | * |
||
1079 | * Returns success or error JSON |
||
1080 | * |
||
1081 | * <token_json> |
||
1082 | * : JSON blob of WPCOM API token |
||
1083 | * [--partner_tracking_id=<partner_tracking_id>] |
||
1084 | * : This is an optional ID that a host can pass to help identify a site in logs on WordPress.com |
||
1085 | * |
||
1086 | * * @synopsis <token_json> [--partner_tracking_id=<partner_tracking_id>] |
||
1087 | */ |
||
1088 | public function partner_cancel( $args, $named_args ) { |
||
1143 | |||
1144 | /** |
||
1145 | * Provision a site using a Jetpack Partner license |
||
1146 | * |
||
1147 | * Returns JSON blob |
||
1148 | * |
||
1149 | * ## OPTIONS |
||
1150 | * |
||
1151 | * <token_json> |
||
1152 | * : JSON blob of WPCOM API token |
||
1153 | * [--plan=<plan_name>] |
||
1154 | * : Slug of the requested plan, e.g. premium |
||
1155 | * [--wpcom_user_id=<user_id>] |
||
1156 | * : WordPress.com ID of user to connect as (must be whitelisted against partner key) |
||
1157 | * [--wpcom_user_email=<wpcom_user_email>] |
||
1158 | * : Override the email we send to WordPress.com for registration |
||
1159 | * [--onboarding=<onboarding>] |
||
1160 | * : Guide the user through an onboarding wizard |
||
1161 | * [--force_register=<register>] |
||
1162 | * : Whether to force a site to register |
||
1163 | * [--force_connect=<force_connect>] |
||
1164 | * : Force JPS to not reuse existing credentials |
||
1165 | * [--home_url=<home_url>] |
||
1166 | * : Overrides the home option via the home_url filter, or the WP_HOME constant |
||
1167 | * [--site_url=<site_url>] |
||
1168 | * : Overrides the siteurl option via the site_url filter, or the WP_SITEURL constant |
||
1169 | * [--partner_tracking_id=<partner_tracking_id>] |
||
1170 | * : This is an optional ID that a host can pass to help identify a site in logs on WordPress.com |
||
1171 | * |
||
1172 | * ## EXAMPLES |
||
1173 | * |
||
1174 | * $ wp jetpack partner_provision '{ some: "json" }' premium 1 |
||
1175 | * { success: true } |
||
1176 | * |
||
1177 | * @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>] |
||
1178 | */ |
||
1179 | public function partner_provision( $args, $named_args ) { |
||
1217 | |||
1218 | /** |
||
1219 | * Manages your Jetpack sitemap |
||
1220 | * |
||
1221 | * ## OPTIONS |
||
1222 | * |
||
1223 | * rebuild : Rebuild all sitemaps |
||
1224 | * --purge : if set, will remove all existing sitemap data before rebuilding |
||
1225 | * |
||
1226 | * ## EXAMPLES |
||
1227 | * |
||
1228 | * wp jetpack sitemap rebuild |
||
1229 | * |
||
1230 | * @subcommand sitemap |
||
1231 | * @synopsis <rebuild> [--purge] |
||
1232 | */ |
||
1233 | public function sitemap( $args, $assoc_args ) { |
||
1252 | |||
1253 | /** |
||
1254 | * Allows authorizing a user via the command line and will activate |
||
1255 | * |
||
1256 | * ## EXAMPLES |
||
1257 | * |
||
1258 | * wp jetpack authorize_user --token=123456789abcdef |
||
1259 | * |
||
1260 | * @synopsis --token=<value> |
||
1261 | */ |
||
1262 | public function authorize_user( $args, $named_args ) { |
||
1296 | |||
1297 | /** |
||
1298 | * Allows calling a WordPress.com API endpoint using the current blog's token. |
||
1299 | * |
||
1300 | * ## OPTIONS |
||
1301 | * --resource=<resource> |
||
1302 | * : The resource to call with the current blog's token, where `%d` represents the current blog's ID. |
||
1303 | * |
||
1304 | * [--api_version=<api_version>] |
||
1305 | * : The API version to query against. |
||
1306 | * |
||
1307 | * [--base_api_path=<base_api_path>] |
||
1308 | * : The base API path to query. |
||
1309 | * --- |
||
1310 | * default: rest |
||
1311 | * --- |
||
1312 | * |
||
1313 | * [--body=<body>] |
||
1314 | * : A JSON encoded string representing arguments to send in the body. |
||
1315 | * |
||
1316 | * [--field=<value>] |
||
1317 | * : Any number of arguments that should be passed to the resource. |
||
1318 | * |
||
1319 | * [--pretty] |
||
1320 | * : Will pretty print the results of a successful API call. |
||
1321 | * |
||
1322 | * [--strip-success] |
||
1323 | * : Will remove the green success label from successful API calls. |
||
1324 | * |
||
1325 | * ## EXAMPLES |
||
1326 | * |
||
1327 | * wp jetpack call_api --resource='/sites/%d' |
||
1328 | */ |
||
1329 | public function call_api( $args, $named_args ) { |
||
1330 | if ( ! Jetpack::is_active() ) { |
||
1331 | WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) ); |
||
1332 | } |
||
1333 | |||
1334 | $consumed_args = array( |
||
1335 | 'resource', |
||
1336 | 'api_version', |
||
1337 | 'base_api_path', |
||
1338 | 'body', |
||
1339 | 'pretty', |
||
1340 | ); |
||
1341 | |||
1342 | // Get args that should be passed to resource. |
||
1343 | $other_args = array_diff_key( $named_args, array_flip( $consumed_args ) ); |
||
1344 | |||
1345 | $decoded_body = ! empty( $named_args['body'] ) |
||
1346 | ? json_decode( $named_args['body'], true ) |
||
1347 | : false; |
||
1348 | |||
1349 | $resource_url = ( false === strpos( $named_args['resource'], '%d' ) ) |
||
1350 | ? $named_args['resource'] |
||
1351 | : sprintf( $named_args['resource'], Jetpack_Options::get_option( 'id' ) ); |
||
1352 | |||
1353 | $response = Client::wpcom_json_api_request_as_blog( |
||
1354 | $resource_url, |
||
1355 | empty( $named_args['api_version'] ) ? Client::WPCOM_JSON_API_VERSION : $named_args['api_version'], |
||
1356 | $other_args, |
||
1357 | empty( $decoded_body ) ? null : $decoded_body, |
||
1358 | empty( $named_args['base_api_path'] ) ? 'rest' : $named_args['base_api_path'] |
||
1359 | ); |
||
1360 | |||
1361 | View Code Duplication | if ( is_wp_error( $response ) ) { |
|
1362 | WP_CLI::error( |
||
1363 | sprintf( |
||
1364 | /* translators: %1$s is an endpoint route (ex. /sites/123456), %2$d is an error code, %3$s is an error message. */ |
||
1365 | __( 'Request to %1$s returned an error: (%2$d) %3$s.', 'jetpack' ), |
||
1366 | $resource_url, |
||
1367 | $response->get_error_code(), |
||
1368 | $response->get_error_message() |
||
1369 | ) |
||
1370 | ); |
||
1371 | } |
||
1372 | |||
1373 | if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
||
1374 | WP_CLI::error( |
||
1375 | sprintf( |
||
1376 | /* translators: %1$s is an endpoint route (ex. /sites/123456), %2$d is an HTTP status code. */ |
||
1377 | __( 'Request to %1$s returned a non-200 response code: %2$d.', 'jetpack' ), |
||
1378 | $resource_url, |
||
1379 | wp_remote_retrieve_response_code( $response ) |
||
1380 | ) |
||
1381 | ); |
||
1382 | } |
||
1383 | |||
1384 | $output = wp_remote_retrieve_body( $response ); |
||
1385 | if ( isset( $named_args['pretty'] ) ) { |
||
1386 | $decoded_output = json_decode( $output ); |
||
1387 | if ( $decoded_output ) { |
||
1388 | $output = wp_json_encode( $decoded_output, JSON_PRETTY_PRINT ); |
||
1389 | } |
||
1390 | } |
||
1391 | |||
1392 | if ( isset( $named_args['strip-success'] ) ) { |
||
1393 | WP_CLI::log( $output ); |
||
1394 | WP_CLI::halt( 0 ); |
||
1395 | } |
||
1396 | |||
1397 | WP_CLI::success( $output ); |
||
1398 | } |
||
1399 | |||
1400 | /** |
||
1401 | * Allows uploading SSH Credentials to the current site for backups, restores, and security scanning. |
||
1402 | * |
||
1403 | * ## OPTIONS |
||
1404 | * |
||
1405 | * [--host=<host>] |
||
1406 | * : The SSH server's address. |
||
1407 | * |
||
1408 | * [--ssh-user=<user>] |
||
1409 | * : The username to use to log in to the SSH server. |
||
1410 | * |
||
1411 | * [--pass=<pass>] |
||
1412 | * : The password used to log in, if using a password. (optional) |
||
1413 | * |
||
1414 | * [--kpri=<kpri>] |
||
1415 | * : The private key used to log in, if using a private key. (optional) |
||
1416 | * |
||
1417 | * [--pretty] |
||
1418 | * : Will pretty print the results of a successful API call. (optional) |
||
1419 | * |
||
1420 | * [--strip-success] |
||
1421 | * : Will remove the green success label from successful API calls. (optional) |
||
1422 | * |
||
1423 | * ## EXAMPLES |
||
1424 | * |
||
1425 | * wp jetpack upload_ssh_creds --host=example.com --ssh-user=example --pass=password |
||
1426 | * wp jetpack updload_ssh_creds --host=example.com --ssh-user=example --kpri=key |
||
1427 | */ |
||
1428 | public function upload_ssh_creds( $args, $named_args ) { |
||
1481 | |||
1482 | /** |
||
1483 | * API wrapper for getting stats from the WordPress.com API for the current site. |
||
1484 | * |
||
1485 | * ## OPTIONS |
||
1486 | * |
||
1487 | * [--quantity=<quantity>] |
||
1488 | * : The number of units to include. |
||
1489 | * --- |
||
1490 | * default: 30 |
||
1491 | * --- |
||
1492 | * |
||
1493 | * [--period=<period>] |
||
1494 | * : The unit of time to query stats for. |
||
1495 | * --- |
||
1496 | * default: day |
||
1497 | * options: |
||
1498 | * - day |
||
1499 | * - week |
||
1500 | * - month |
||
1501 | * - year |
||
1502 | * --- |
||
1503 | * |
||
1504 | * [--date=<date>] |
||
1505 | * : The latest date to return stats for. Ex. - 2018-01-01. |
||
1506 | * |
||
1507 | * [--pretty] |
||
1508 | * : Will pretty print the results of a successful API call. |
||
1509 | * |
||
1510 | * [--strip-success] |
||
1511 | * : Will remove the green success label from successful API calls. |
||
1512 | * |
||
1513 | * ## EXAMPLES |
||
1514 | * |
||
1515 | * wp jetpack get_stats |
||
1516 | */ |
||
1517 | public function get_stats( $args, $named_args ) { |
||
1552 | |||
1553 | /** |
||
1554 | * Allows management of publicize connections. |
||
1555 | * |
||
1556 | * ## OPTIONS |
||
1557 | * |
||
1558 | * <list|disconnect> |
||
1559 | * : The action to perform. |
||
1560 | * --- |
||
1561 | * options: |
||
1562 | * - list |
||
1563 | * - disconnect |
||
1564 | * --- |
||
1565 | * |
||
1566 | * [<identifier>] |
||
1567 | * : The connection ID or service to perform an action on. |
||
1568 | * |
||
1569 | * [--format=<format>] |
||
1570 | * : Allows overriding the output of the command when listing connections. |
||
1571 | * --- |
||
1572 | * default: table |
||
1573 | * options: |
||
1574 | * - table |
||
1575 | * - json |
||
1576 | * - csv |
||
1577 | * - yaml |
||
1578 | * - ids |
||
1579 | * - count |
||
1580 | * --- |
||
1581 | * |
||
1582 | * ## EXAMPLES |
||
1583 | * |
||
1584 | * # List all publicize connections. |
||
1585 | * $ wp jetpack publicize list |
||
1586 | * |
||
1587 | * # List publicize connections for a given service. |
||
1588 | * $ wp jetpack publicize list twitter |
||
1589 | * |
||
1590 | * # List all publicize connections for a given user. |
||
1591 | * $ wp --user=1 jetpack publicize list |
||
1592 | * |
||
1593 | * # List all publicize connections for a given user and service. |
||
1594 | * $ wp --user=1 jetpack publicize list twitter |
||
1595 | * |
||
1596 | * # Display details for a given connection. |
||
1597 | * $ wp jetpack publicize list 123456 |
||
1598 | * |
||
1599 | * # Diconnection a given connection. |
||
1600 | * $ wp jetpack publicize disconnect 123456 |
||
1601 | * |
||
1602 | * # Disconnect all connections. |
||
1603 | * $ wp jetpack publicize disconnect all |
||
1604 | * |
||
1605 | * # Disconnect all connections for a given service. |
||
1606 | * $ wp jetpack publicize disconnect twitter |
||
1607 | */ |
||
1608 | public function publicize( $args, $named_args ) { |
||
1783 | |||
1784 | private function get_api_host() { |
||
1788 | |||
1789 | private function partner_provision_error( $error ) { |
||
1801 | |||
1802 | /** |
||
1803 | * Creates the essential files in Jetpack to start building a Gutenberg block or plugin. |
||
1804 | * |
||
1805 | * ## TYPES |
||
1806 | * |
||
1807 | * 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. |
||
1808 | * |
||
1809 | * ## BLOCK TYPE OPTIONS |
||
1810 | * |
||
1811 | * The first parameter is the block title and it's not associative. Add it wrapped in quotes. |
||
1812 | * 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. |
||
1813 | * --slug: Specific slug to identify the block that overrides the one generated based on the title. |
||
1814 | * --description: Allows to provide a text description of the block. |
||
1815 | * --keywords: Provide up to three keywords separated by comma so users can find this block when they search in Gutenberg's inserter. |
||
1816 | * --variation: Allows to decide whether the block should be a production block, experimental, or beta. Defaults to Beta when arg not provided. |
||
1817 | * |
||
1818 | * ## BLOCK TYPE EXAMPLES |
||
1819 | * |
||
1820 | * wp jetpack scaffold block "Cool Block" |
||
1821 | * wp jetpack scaffold block "Amazing Rock" --slug="good-music" --description="Rock the best music on your site" |
||
1822 | * wp jetpack scaffold block "Jukebox" --keywords="music, audio, media" |
||
1823 | * wp jetpack scaffold block "Jukebox" --variation="experimental" |
||
1824 | * |
||
1825 | * @subcommand scaffold block |
||
1826 | * @synopsis <type> <title> [--slug] [--description] [--keywords] [--variation] |
||
1827 | * |
||
1828 | * @param array $args Positional parameters, when strings are passed, wrap them in quotes. |
||
1829 | * @param array $assoc_args Associative parameters like --slug="nice-block". |
||
1830 | */ |
||
1831 | public function scaffold( $args, $assoc_args ) { |
||
1843 | |||
1844 | /** |
||
1845 | * Creates the essential files in Jetpack to build a Gutenberg block. |
||
1846 | * |
||
1847 | * @param array $args Positional parameters. Only one is used, that corresponds to the block title. |
||
1848 | * @param array $assoc_args Associative parameters defined in the scaffold() method. |
||
1849 | */ |
||
1850 | public function block( $args, $assoc_args ) { |
||
2018 | |||
2019 | /** |
||
2020 | * Built the file replacing the placeholders in the template with the data supplied. |
||
2021 | * |
||
2022 | * @param string $template |
||
2023 | * @param array $data |
||
2024 | * |
||
2025 | * @return string mixed |
||
2026 | */ |
||
2027 | private static function render_block_file( $template, $data = array() ) { |
||
2030 | } |
||
2031 | |||
2067 |
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.