Complex classes like Actions 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 Actions, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Actions { |
||
23 | /** |
||
24 | * A variable to hold a sync sender object. |
||
25 | * |
||
26 | * @access public |
||
27 | * @static |
||
28 | * |
||
29 | * @var Automattic\Jetpack\Sync\Sender |
||
30 | */ |
||
31 | public static $sender = null; |
||
32 | |||
33 | /** |
||
34 | * A variable to hold a sync listener object. |
||
35 | * |
||
36 | * @access public |
||
37 | * @static |
||
38 | * |
||
39 | * @var Automattic\Jetpack\Sync\Listener |
||
40 | */ |
||
41 | public static $listener = null; |
||
42 | |||
43 | /** |
||
44 | * Name of the sync cron schedule. |
||
45 | * |
||
46 | * @access public |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | const DEFAULT_SYNC_CRON_INTERVAL_NAME = 'jetpack_sync_interval'; |
||
51 | |||
52 | /** |
||
53 | * Interval between the last and the next sync cron action. |
||
54 | * |
||
55 | * @access public |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | const DEFAULT_SYNC_CRON_INTERVAL_VALUE = 300; // 5 * MINUTE_IN_SECONDS; |
||
60 | |||
61 | /** |
||
62 | * Initialize Sync for cron jobs, set up listeners for WordPress Actions, |
||
63 | * and set up a shut-down action for sending actions to WordPress.com |
||
64 | * |
||
65 | * @access public |
||
66 | * @static |
||
67 | */ |
||
68 | public static function init() { |
||
69 | // Everything below this point should only happen if we're a valid sync site. |
||
70 | if ( ! self::sync_allowed() ) { |
||
71 | return; |
||
72 | } |
||
73 | |||
74 | if ( self::sync_via_cron_allowed() ) { |
||
75 | self::init_sync_cron_jobs(); |
||
76 | } elseif ( wp_next_scheduled( 'jetpack_sync_cron' ) ) { |
||
77 | self::clear_sync_cron_jobs(); |
||
78 | } |
||
79 | // When importing via cron, do not sync. |
||
80 | add_action( 'wp_cron_importer_hook', array( __CLASS__, 'set_is_importing_true' ), 1 ); |
||
81 | |||
82 | // Sync connected user role changes to WordPress.com. |
||
83 | Users::init(); |
||
84 | |||
85 | // Publicize filter to prevent publicizing blacklisted post types. |
||
86 | add_filter( 'publicize_should_publicize_published_post', array( __CLASS__, 'prevent_publicize_blacklisted_posts' ), 10, 2 ); |
||
87 | |||
88 | /** |
||
89 | * Fires on every request before default loading sync listener code. |
||
90 | * Return false to not load sync listener code that monitors common |
||
91 | * WP actions to be serialized. |
||
92 | * |
||
93 | * By default this returns true for cron jobs, non-GET-requests, or requests where the |
||
94 | * user is logged-in. |
||
95 | * |
||
96 | * @since 4.2.0 |
||
97 | * |
||
98 | * @param bool should we load sync listener code for this request |
||
99 | */ |
||
100 | if ( apply_filters( 'jetpack_sync_listener_should_load', true ) ) { |
||
101 | self::initialize_listener(); |
||
102 | } |
||
103 | |||
104 | add_action( 'init', array( __CLASS__, 'add_sender_shutdown' ), 90 ); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Prepares sync to send actions on shutdown for the current request. |
||
109 | * |
||
110 | * @access public |
||
111 | * @static |
||
112 | */ |
||
113 | public static function add_sender_shutdown() { |
||
114 | /** |
||
115 | * Fires on every request before default loading sync sender code. |
||
116 | * Return false to not load sync sender code that serializes pending |
||
117 | * data and sends it to WPCOM for processing. |
||
118 | * |
||
119 | * By default this returns true for cron jobs, POST requests, admin requests, or requests |
||
120 | * by users who can manage_options. |
||
121 | * |
||
122 | * @since 4.2.0 |
||
123 | * |
||
124 | * @param bool should we load sync sender code for this request |
||
125 | */ |
||
126 | if ( apply_filters( |
||
127 | 'jetpack_sync_sender_should_load', |
||
128 | self::should_initialize_sender() |
||
129 | ) ) { |
||
130 | self::initialize_sender(); |
||
131 | add_action( 'shutdown', array( self::$sender, 'do_sync' ) ); |
||
132 | add_action( 'shutdown', array( self::$sender, 'do_full_sync' ), 9999 ); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Decides if the sender should run on shutdown for this request. |
||
138 | * |
||
139 | * @access public |
||
140 | * @static |
||
141 | * |
||
142 | * @return bool |
||
143 | */ |
||
144 | public static function should_initialize_sender() { |
||
145 | if ( Constants::is_true( 'DOING_CRON' ) ) { |
||
146 | return self::sync_via_cron_allowed(); |
||
147 | } |
||
148 | |||
149 | if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) { |
||
150 | return true; |
||
151 | } |
||
152 | |||
153 | if ( current_user_can( 'manage_options' ) ) { |
||
154 | return true; |
||
155 | } |
||
156 | |||
157 | if ( is_admin() ) { |
||
158 | return true; |
||
159 | } |
||
160 | |||
161 | if ( defined( 'PHPUNIT_JETPACK_TESTSUITE' ) ) { |
||
162 | return true; |
||
163 | } |
||
164 | |||
165 | if ( Constants::get_constant( 'WP_CLI' ) ) { |
||
166 | return true; |
||
167 | } |
||
168 | |||
169 | return false; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Decides if sync should run at all during this request. |
||
174 | * |
||
175 | * @access public |
||
176 | * @static |
||
177 | * |
||
178 | * @return bool |
||
179 | */ |
||
180 | public static function sync_allowed() { |
||
181 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
182 | return false; |
||
183 | } |
||
184 | |||
185 | if ( defined( 'PHPUNIT_JETPACK_TESTSUITE' ) ) { |
||
186 | return true; |
||
187 | } |
||
188 | |||
189 | if ( ! Settings::is_sync_enabled() ) { |
||
190 | return false; |
||
191 | } |
||
192 | |||
193 | if ( ( new Status() )->is_development_mode() ) { |
||
194 | return false; |
||
195 | } |
||
196 | |||
197 | if ( ( new Status() )->is_staging_site() ) { |
||
198 | return false; |
||
199 | } |
||
200 | |||
201 | $connection = new Jetpack_Connection(); |
||
202 | if ( ! $connection->is_active() ) { |
||
203 | if ( ! doing_action( 'jetpack_user_authorized' ) ) { |
||
204 | return false; |
||
205 | } |
||
206 | } |
||
207 | |||
208 | return true; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Determines if syncing during a cron job is allowed. |
||
213 | * |
||
214 | * @access public |
||
215 | * @static |
||
216 | * |
||
217 | * @return bool|int |
||
218 | */ |
||
219 | public static function sync_via_cron_allowed() { |
||
220 | return ( Settings::get_setting( 'sync_via_cron' ) ); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Decides if the given post should be Publicized based on its type. |
||
225 | * |
||
226 | * @access public |
||
227 | * @static |
||
228 | * |
||
229 | * @param bool $should_publicize Publicize status prior to this filter running. |
||
230 | * @param \WP_Post $post The post to test for Publicizability. |
||
231 | * @return bool |
||
232 | */ |
||
233 | public static function prevent_publicize_blacklisted_posts( $should_publicize, $post ) { |
||
234 | if ( in_array( $post->post_type, Settings::get_setting( 'post_types_blacklist' ), true ) ) { |
||
235 | return false; |
||
236 | } |
||
237 | |||
238 | return $should_publicize; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Set an importing flag to `true` in sync settings. |
||
243 | * |
||
244 | * @access public |
||
245 | * @static |
||
246 | */ |
||
247 | public static function set_is_importing_true() { |
||
248 | Settings::set_importing( true ); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Sends data to WordPress.com via an XMLRPC request. |
||
253 | * |
||
254 | * @access public |
||
255 | * @static |
||
256 | * |
||
257 | * @param object $data Data relating to a sync action. |
||
258 | * @param string $codec_name The name of the codec that encodes the data. |
||
259 | * @param float $sent_timestamp Current server time so we can compensate for clock differences. |
||
260 | * @param string $queue_id The queue the action belongs to, sync or full_sync. |
||
261 | * @param float $checkout_duration Time spent retrieving queue items from the DB. |
||
262 | * @param float $preprocess_duration Time spent converting queue items into data to send. |
||
263 | * @param int $queue_size The size of the sync queue at the time of processing. |
||
|
|||
264 | * @return Jetpack_Error|mixed|WP_Error The result of the sending request. |
||
265 | */ |
||
266 | public static function send_data( $data, $codec_name, $sent_timestamp, $queue_id, $checkout_duration, $preprocess_duration, $queue_size = null ) { |
||
267 | $query_args = array( |
||
268 | 'sync' => '1', // Add an extra parameter to the URL so we can tell it's a sync action. |
||
269 | 'codec' => $codec_name, |
||
270 | 'timestamp' => $sent_timestamp, |
||
271 | 'queue' => $queue_id, |
||
272 | 'home' => Functions::home_url(), // Send home url option to check for Identity Crisis server-side. |
||
273 | 'siteurl' => Functions::site_url(), // Send siteurl option to check for Identity Crisis server-side. |
||
274 | 'cd' => sprintf( '%.4f', $checkout_duration ), |
||
275 | 'pd' => sprintf( '%.4f', $preprocess_duration ), |
||
276 | 'queue_size' => $queue_size, |
||
277 | ); |
||
278 | |||
279 | // Has the site opted in to IDC mitigation? |
||
280 | if ( \Jetpack::sync_idc_optin() ) { |
||
281 | $query_args['idc'] = true; |
||
282 | } |
||
283 | |||
284 | if ( \Jetpack_Options::get_option( 'migrate_for_idc', false ) ) { |
||
285 | $query_args['migrate_for_idc'] = true; |
||
286 | } |
||
287 | |||
288 | $query_args['timeout'] = Settings::is_doing_cron() ? 30 : 15; |
||
289 | |||
290 | /** |
||
291 | * Filters query parameters appended to the Sync request URL sent to WordPress.com. |
||
292 | * |
||
293 | * @since 4.7.0 |
||
294 | * |
||
295 | * @param array $query_args associative array of query parameters. |
||
296 | */ |
||
297 | $query_args = apply_filters( 'jetpack_sync_send_data_query_args', $query_args ); |
||
298 | |||
299 | $connection = new Jetpack_Connection(); |
||
300 | $url = add_query_arg( $query_args, $connection->xmlrpc_api_url() ); |
||
301 | |||
302 | // If we're currently updating to Jetpack 7.7, the IXR client may be missing briefly |
||
303 | // because since 7.7 it's being autoloaded with Composer. |
||
304 | if ( ! class_exists( '\\Jetpack_IXR_Client' ) ) { |
||
305 | return new \WP_Error( |
||
306 | 'ixr_client_missing', |
||
307 | esc_html__( 'Sync has been aborted because the IXR client is missing.', 'jetpack' ) |
||
308 | ); |
||
309 | } |
||
310 | |||
311 | $rpc = new \Jetpack_IXR_Client( |
||
312 | array( |
||
313 | 'url' => $url, |
||
314 | 'user_id' => JETPACK_MASTER_USER, |
||
315 | 'timeout' => $query_args['timeout'], |
||
316 | ) |
||
317 | ); |
||
318 | |||
319 | $result = $rpc->query( 'jetpack.syncActions', $data ); |
||
320 | |||
321 | if ( ! $result ) { |
||
322 | return $rpc->get_jetpack_error(); |
||
323 | } |
||
324 | |||
325 | $response = $rpc->getResponse(); |
||
326 | |||
327 | // Check if WordPress.com IDC mitigation blocked the sync request. |
||
328 | if ( is_array( $response ) && isset( $response['error_code'] ) ) { |
||
329 | $error_code = $response['error_code']; |
||
330 | $allowed_idc_error_codes = array( |
||
331 | 'jetpack_url_mismatch', |
||
332 | 'jetpack_home_url_mismatch', |
||
333 | 'jetpack_site_url_mismatch', |
||
334 | ); |
||
335 | |||
336 | if ( in_array( $error_code, $allowed_idc_error_codes, true ) ) { |
||
337 | \Jetpack_Options::update_option( |
||
338 | 'sync_error_idc', |
||
339 | \Jetpack::get_sync_error_idc_option( $response ) |
||
340 | ); |
||
341 | } |
||
342 | |||
343 | return new \WP_Error( |
||
344 | 'sync_error_idc', |
||
345 | esc_html__( 'Sync has been blocked from WordPress.com because it would cause an identity crisis', 'jetpack' ) |
||
346 | ); |
||
347 | } |
||
348 | |||
349 | return $response; |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * Kicks off the initial sync. |
||
354 | * |
||
355 | * @access public |
||
356 | * @static |
||
357 | * |
||
358 | * @return bool|null False if sync is not allowed. |
||
359 | */ |
||
360 | public static function do_initial_sync() { |
||
385 | |||
386 | /** |
||
387 | * Kicks off a full sync. |
||
388 | * |
||
389 | * @access public |
||
390 | * @static |
||
391 | * |
||
392 | * @param array $modules The sync modules should be included in this full sync. All will be included if null. |
||
393 | * @return bool True if full sync was successfully started. |
||
394 | */ |
||
395 | public static function do_full_sync( $modules = null ) { |
||
412 | |||
413 | /** |
||
414 | * Adds a cron schedule for regular syncing via cron, unless the schedule already exists. |
||
415 | * |
||
416 | * @access public |
||
417 | * @static |
||
418 | * |
||
419 | * @param array $schedules The list of WordPress cron schedules prior to this filter. |
||
420 | * @return array A list of WordPress cron schedules with the Jetpack sync interval added. |
||
421 | */ |
||
422 | public static function jetpack_cron_schedule( $schedules ) { |
||
436 | |||
437 | /** |
||
438 | * Starts an incremental sync via cron. |
||
439 | * |
||
440 | * @access public |
||
441 | * @static |
||
442 | */ |
||
443 | public static function do_cron_sync() { |
||
446 | |||
447 | /** |
||
448 | * Starts a full sync via cron. |
||
449 | * |
||
450 | * @access public |
||
451 | * @static |
||
452 | */ |
||
453 | public static function do_cron_full_sync() { |
||
456 | |||
457 | /** |
||
458 | * Try to send actions until we run out of things to send, |
||
459 | * or have to wait more than 15s before sending again, |
||
460 | * or we hit a lock or some other sending issue |
||
461 | * |
||
462 | * @access public |
||
463 | * @static |
||
464 | * |
||
465 | * @param string $type Sync type. Can be `sync` or `full_sync`. |
||
466 | */ |
||
467 | public static function do_cron_sync_by_type( $type ) { |
||
492 | |||
493 | /** |
||
494 | * Initialize the sync listener. |
||
495 | * |
||
496 | * @access public |
||
497 | * @static |
||
498 | */ |
||
499 | public static function initialize_listener() { |
||
502 | |||
503 | /** |
||
504 | * Initializes the sync sender. |
||
505 | * |
||
506 | * @access public |
||
507 | * @static |
||
508 | */ |
||
509 | public static function initialize_sender() { |
||
513 | |||
514 | /** |
||
515 | * Initializes sync for WooCommerce. |
||
516 | * |
||
517 | * @access public |
||
518 | * @static |
||
519 | */ |
||
520 | public static function initialize_woocommerce() { |
||
526 | |||
527 | /** |
||
528 | * Adds Woo's sync modules to existing modules for sending. |
||
529 | * |
||
530 | * @access public |
||
531 | * @static |
||
532 | * |
||
533 | * @param array $sync_modules The list of sync modules declared prior to this filter. |
||
534 | * @return array A list of sync modules that now includes Woo's modules. |
||
535 | */ |
||
536 | public static function add_woocommerce_sync_module( $sync_modules ) { |
||
540 | |||
541 | /** |
||
542 | * Initializes sync for WP Super Cache. |
||
543 | * |
||
544 | * @access public |
||
545 | * @static |
||
546 | */ |
||
547 | public static function initialize_wp_super_cache() { |
||
553 | |||
554 | /** |
||
555 | * Adds WP Super Cache's sync modules to existing modules for sending. |
||
556 | * |
||
557 | * @access public |
||
558 | * @static |
||
559 | * |
||
560 | * @param array $sync_modules The list of sync modules declared prior to this filer. |
||
561 | * @return array A list of sync modules that now includes WP Super Cache's modules. |
||
562 | */ |
||
563 | public static function add_wp_super_cache_sync_module( $sync_modules ) { |
||
567 | |||
568 | /** |
||
569 | * Sanitizes the name of sync's cron schedule. |
||
570 | * |
||
571 | * @access public |
||
572 | * @static |
||
573 | * |
||
574 | * @param string $schedule The name of a WordPress cron schedule. |
||
575 | * @return string The sanitized name of sync's cron schedule. |
||
576 | */ |
||
577 | public static function sanitize_filtered_sync_cron_schedule( $schedule ) { |
||
588 | |||
589 | /** |
||
590 | * Allows offsetting of start times for sync cron jobs. |
||
591 | * |
||
592 | * @access public |
||
593 | * @static |
||
594 | * |
||
595 | * @param string $schedule The name of a cron schedule. |
||
596 | * @param string $hook The hook that this method is responding to. |
||
597 | * @return int The offset for the sync cron schedule. |
||
598 | */ |
||
599 | public static function get_start_time_offset( $schedule = '', $hook = '' ) { |
||
623 | |||
624 | /** |
||
625 | * Decides if a sync cron should be scheduled. |
||
626 | * |
||
627 | * @access public |
||
628 | * @static |
||
629 | * |
||
630 | * @param string $schedule The name of a cron schedule. |
||
631 | * @param string $hook The hook that this method is responding to. |
||
632 | */ |
||
633 | public static function maybe_schedule_sync_cron( $schedule, $hook ) { |
||
649 | |||
650 | /** |
||
651 | * Clears Jetpack sync cron jobs. |
||
652 | * |
||
653 | * @access public |
||
654 | * @static |
||
655 | */ |
||
656 | public static function clear_sync_cron_jobs() { |
||
660 | |||
661 | /** |
||
662 | * Initializes Jetpack sync cron jobs. |
||
663 | * |
||
664 | * @access public |
||
665 | * @static |
||
666 | */ |
||
667 | public static function init_sync_cron_jobs() { |
||
693 | |||
694 | /** |
||
695 | * Perform maintenance when a plugin upgrade occurs. |
||
696 | * |
||
697 | * @access public |
||
698 | * @static |
||
699 | * |
||
700 | * @param string $new_version New version of the plugin. |
||
701 | * @param string $old_version Old version of the plugin. |
||
702 | */ |
||
703 | public static function cleanup_on_upgrade( $new_version = null, $old_version = null ) { |
||
720 | |||
721 | /** |
||
722 | * Get syncing status for the given fields. |
||
723 | * |
||
724 | * @access public |
||
725 | * @static |
||
726 | * |
||
727 | * @param string|null $fields A comma-separated string of the fields to include in the array from the JSON response. |
||
728 | * @return array An associative array with the status report. |
||
729 | */ |
||
730 | public static function get_sync_status( $fields = null ) { |
||
784 | } |
||
785 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.