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 |
||
20 | class Actions { |
||
21 | /** |
||
22 | * A variable to hold a sync sender object. |
||
23 | * |
||
24 | * @access public |
||
25 | * @static |
||
26 | * |
||
27 | * @var Automattic\Jetpack\Sync\Sender |
||
28 | */ |
||
29 | public static $sender = null; |
||
30 | |||
31 | /** |
||
32 | * A variable to hold a sync listener object. |
||
33 | * |
||
34 | * @access public |
||
35 | * @static |
||
36 | * |
||
37 | * @var Automattic\Jetpack\Sync\Listener |
||
38 | */ |
||
39 | public static $listener = null; |
||
40 | |||
41 | /** |
||
42 | * Name of the sync cron schedule. |
||
43 | * |
||
44 | * @access public |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | const DEFAULT_SYNC_CRON_INTERVAL_NAME = 'jetpack_sync_interval'; |
||
49 | |||
50 | /** |
||
51 | * Interval between the last and the next sync cron action. |
||
52 | * |
||
53 | * @access public |
||
54 | * |
||
55 | * @var int |
||
56 | */ |
||
57 | const DEFAULT_SYNC_CRON_INTERVAL_VALUE = 300; // 5 * MINUTE_IN_SECONDS; |
||
58 | |||
59 | /** |
||
60 | * Initialize Sync for cron jobs, set up listeners for WordPress Actions, |
||
61 | * and set up a shut-down action for sending actions to WordPress.com |
||
62 | * |
||
63 | * @access public |
||
64 | * @static |
||
65 | */ |
||
66 | public static function init() { |
||
67 | // Everything below this point should only happen if we're a valid sync site. |
||
68 | if ( ! self::sync_allowed() ) { |
||
69 | return; |
||
70 | } |
||
71 | |||
72 | if ( self::sync_via_cron_allowed() ) { |
||
73 | self::init_sync_cron_jobs(); |
||
74 | } elseif ( wp_next_scheduled( 'jetpack_sync_cron' ) ) { |
||
75 | self::clear_sync_cron_jobs(); |
||
76 | } |
||
77 | // When importing via cron, do not sync. |
||
78 | add_action( 'wp_cron_importer_hook', array( __CLASS__, 'set_is_importing_true' ), 1 ); |
||
79 | |||
80 | // Sync connected user role changes to WordPress.com. |
||
81 | Users::init(); |
||
82 | |||
83 | // Publicize filter to prevent publicizing blacklisted post types. |
||
84 | add_filter( 'publicize_should_publicize_published_post', array( __CLASS__, 'prevent_publicize_blacklisted_posts' ), 10, 2 ); |
||
85 | |||
86 | /** |
||
87 | * Fires on every request before default loading sync listener code. |
||
88 | * Return false to not load sync listener code that monitors common |
||
89 | * WP actions to be serialized. |
||
90 | * |
||
91 | * By default this returns true for cron jobs, non-GET-requests, or requests where the |
||
92 | * user is logged-in. |
||
93 | * |
||
94 | * @since 4.2.0 |
||
95 | * |
||
96 | * @param bool should we load sync listener code for this request |
||
97 | */ |
||
98 | if ( apply_filters( 'jetpack_sync_listener_should_load', true ) ) { |
||
99 | self::initialize_listener(); |
||
100 | } |
||
101 | |||
102 | add_action( 'init', array( __CLASS__, '' ), 90 ); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Prepares sync to send actions on shutdown for the current request. |
||
107 | * |
||
108 | * @access public |
||
109 | * @static |
||
110 | */ |
||
111 | public static function add_sender_shutdown() { |
||
112 | /** |
||
113 | * Fires on every request before default loading sync sender code. |
||
114 | * Return false to not load sync sender code that serializes pending |
||
115 | * data and sends it to WPCOM for processing. |
||
116 | * |
||
117 | * By default this returns true for cron jobs, POST requests, admin requests, or requests |
||
118 | * by users who can manage_options. |
||
119 | * |
||
120 | * @since 4.2.0 |
||
121 | * |
||
122 | * @param bool should we load sync sender code for this request |
||
123 | */ |
||
124 | if ( apply_filters( |
||
125 | 'jetpack_sync_sender_should_load', |
||
126 | self::should_initialize_sender() |
||
127 | ) ) { |
||
128 | self::initialize_sender(); |
||
129 | add_action( 'shutdown', array( self::$sender, 'do_sync' ) ); |
||
130 | add_action( 'shutdown', array( self::$sender, 'do_full_sync' ), 9999 ); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Define JETPACK_SYNC_READ_ONLY constant if not defined. |
||
136 | * This notifies sync to not run in shutdown if it was initialized during init. |
||
137 | * |
||
138 | * @access public |
||
139 | * @static |
||
140 | */ |
||
141 | public static function mark_sync_read_only() { |
||
142 | Constants::set_constant( 'JETPACK_SYNC_READ_ONLY', true ); |
||
|
|||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Decides if the sender should run on shutdown for this request. |
||
147 | * |
||
148 | * @access public |
||
149 | * @static |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | public static function should_initialize_sender() { |
||
180 | |||
181 | /** |
||
182 | * Decides if the sender should run on shutdown when actions are queued. |
||
183 | * |
||
184 | * @access public |
||
185 | * @static |
||
186 | * |
||
187 | * @return bool |
||
188 | */ |
||
189 | public static function should_initialize_sender_enqueue() { |
||
196 | |||
197 | /** |
||
198 | * Decides if sync should run at all during this request. |
||
199 | * |
||
200 | * @access public |
||
201 | * @static |
||
202 | * |
||
203 | * @return bool |
||
204 | */ |
||
205 | public static function sync_allowed() { |
||
235 | |||
236 | /** |
||
237 | * Helper function to get details as to why sync is not allowed, if it is not allowed. |
||
238 | * |
||
239 | * @return array |
||
240 | */ |
||
241 | public static function get_debug_details() { |
||
271 | |||
272 | /** |
||
273 | * Determines if syncing during a cron job is allowed. |
||
274 | * |
||
275 | * @access public |
||
276 | * @static |
||
277 | * |
||
278 | * @return bool|int |
||
279 | */ |
||
280 | public static function sync_via_cron_allowed() { |
||
283 | |||
284 | /** |
||
285 | * Decides if the given post should be Publicized based on its type. |
||
286 | * |
||
287 | * @access public |
||
288 | * @static |
||
289 | * |
||
290 | * @param bool $should_publicize Publicize status prior to this filter running. |
||
291 | * @param \WP_Post $post The post to test for Publicizability. |
||
292 | * @return bool |
||
293 | */ |
||
294 | public static function prevent_publicize_blacklisted_posts( $should_publicize, $post ) { |
||
301 | |||
302 | /** |
||
303 | * Set an importing flag to `true` in sync settings. |
||
304 | * |
||
305 | * @access public |
||
306 | * @static |
||
307 | */ |
||
308 | public static function set_is_importing_true() { |
||
311 | |||
312 | /** |
||
313 | * Sends data to WordPress.com via an XMLRPC request. |
||
314 | * |
||
315 | * @access public |
||
316 | * @static |
||
317 | * |
||
318 | * @param object $data Data relating to a sync action. |
||
319 | * @param string $codec_name The name of the codec that encodes the data. |
||
320 | * @param float $sent_timestamp Current server time so we can compensate for clock differences. |
||
321 | * @param string $queue_id The queue the action belongs to, sync or full_sync. |
||
322 | * @param float $checkout_duration Time spent retrieving queue items from the DB. |
||
323 | * @param float $preprocess_duration Time spent converting queue items into data to send. |
||
324 | * @param int $queue_size The size of the sync queue at the time of processing. |
||
325 | * @param string $buffer_id The ID of the Queue buffer checked out for processing. |
||
326 | * @return Jetpack_Error|mixed|WP_Error The result of the sending request. |
||
327 | */ |
||
328 | public static function send_data( $data, $codec_name, $sent_timestamp, $queue_id, $checkout_duration, $preprocess_duration, $queue_size = null, $buffer_id = null ) { |
||
414 | |||
415 | /** |
||
416 | * Kicks off the initial sync. |
||
417 | * |
||
418 | * @access public |
||
419 | * @static |
||
420 | * |
||
421 | * @return bool|null False if sync is not allowed. |
||
422 | */ |
||
423 | public static function do_initial_sync() { |
||
445 | |||
446 | /** |
||
447 | * Kicks off a full sync. |
||
448 | * |
||
449 | * @access public |
||
450 | * @static |
||
451 | * |
||
452 | * @param array $modules The sync modules should be included in this full sync. All will be included if null. |
||
453 | * @return bool True if full sync was successfully started. |
||
454 | */ |
||
455 | public static function do_full_sync( $modules = null ) { |
||
472 | |||
473 | /** |
||
474 | * Adds a cron schedule for regular syncing via cron, unless the schedule already exists. |
||
475 | * |
||
476 | * @access public |
||
477 | * @static |
||
478 | * |
||
479 | * @param array $schedules The list of WordPress cron schedules prior to this filter. |
||
480 | * @return array A list of WordPress cron schedules with the Jetpack sync interval added. |
||
481 | */ |
||
482 | public static function jetpack_cron_schedule( $schedules ) { |
||
496 | |||
497 | /** |
||
498 | * Starts an incremental sync via cron. |
||
499 | * |
||
500 | * @access public |
||
501 | * @static |
||
502 | */ |
||
503 | public static function do_cron_sync() { |
||
506 | |||
507 | /** |
||
508 | * Starts a full sync via cron. |
||
509 | * |
||
510 | * @access public |
||
511 | * @static |
||
512 | */ |
||
513 | public static function do_cron_full_sync() { |
||
516 | |||
517 | /** |
||
518 | * Try to send actions until we run out of things to send, |
||
519 | * or have to wait more than 15s before sending again, |
||
520 | * or we hit a lock or some other sending issue |
||
521 | * |
||
522 | * @access public |
||
523 | * @static |
||
524 | * |
||
525 | * @param string $type Sync type. Can be `sync` or `full_sync`. |
||
526 | */ |
||
527 | public static function do_cron_sync_by_type( $type ) { |
||
565 | |||
566 | /** |
||
567 | * Initialize the sync listener. |
||
568 | * |
||
569 | * @access public |
||
570 | * @static |
||
571 | */ |
||
572 | public static function initialize_listener() { |
||
575 | |||
576 | /** |
||
577 | * Initializes the sync sender. |
||
578 | * |
||
579 | * @access public |
||
580 | * @static |
||
581 | */ |
||
582 | public static function initialize_sender() { |
||
586 | |||
587 | /** |
||
588 | * Initializes sync for WooCommerce. |
||
589 | * |
||
590 | * @access public |
||
591 | * @static |
||
592 | */ |
||
593 | public static function initialize_woocommerce() { |
||
599 | |||
600 | /** |
||
601 | * Adds Woo's sync modules to existing modules for sending. |
||
602 | * |
||
603 | * @access public |
||
604 | * @static |
||
605 | * |
||
606 | * @param array $sync_modules The list of sync modules declared prior to this filter. |
||
607 | * @return array A list of sync modules that now includes Woo's modules. |
||
608 | */ |
||
609 | public static function add_woocommerce_sync_module( $sync_modules ) { |
||
613 | |||
614 | /** |
||
615 | * Initializes sync for WP Super Cache. |
||
616 | * |
||
617 | * @access public |
||
618 | * @static |
||
619 | */ |
||
620 | public static function initialize_wp_super_cache() { |
||
626 | |||
627 | /** |
||
628 | * Adds WP Super Cache's sync modules to existing modules for sending. |
||
629 | * |
||
630 | * @access public |
||
631 | * @static |
||
632 | * |
||
633 | * @param array $sync_modules The list of sync modules declared prior to this filer. |
||
634 | * @return array A list of sync modules that now includes WP Super Cache's modules. |
||
635 | */ |
||
636 | public static function add_wp_super_cache_sync_module( $sync_modules ) { |
||
640 | |||
641 | /** |
||
642 | * Sanitizes the name of sync's cron schedule. |
||
643 | * |
||
644 | * @access public |
||
645 | * @static |
||
646 | * |
||
647 | * @param string $schedule The name of a WordPress cron schedule. |
||
648 | * @return string The sanitized name of sync's cron schedule. |
||
649 | */ |
||
650 | public static function sanitize_filtered_sync_cron_schedule( $schedule ) { |
||
661 | |||
662 | /** |
||
663 | * Allows offsetting of start times for sync cron jobs. |
||
664 | * |
||
665 | * @access public |
||
666 | * @static |
||
667 | * |
||
668 | * @param string $schedule The name of a cron schedule. |
||
669 | * @param string $hook The hook that this method is responding to. |
||
670 | * @return int The offset for the sync cron schedule. |
||
671 | */ |
||
672 | public static function get_start_time_offset( $schedule = '', $hook = '' ) { |
||
696 | |||
697 | /** |
||
698 | * Decides if a sync cron should be scheduled. |
||
699 | * |
||
700 | * @access public |
||
701 | * @static |
||
702 | * |
||
703 | * @param string $schedule The name of a cron schedule. |
||
704 | * @param string $hook The hook that this method is responding to. |
||
705 | */ |
||
706 | public static function maybe_schedule_sync_cron( $schedule, $hook ) { |
||
722 | |||
723 | /** |
||
724 | * Clears Jetpack sync cron jobs. |
||
725 | * |
||
726 | * @access public |
||
727 | * @static |
||
728 | */ |
||
729 | public static function clear_sync_cron_jobs() { |
||
733 | |||
734 | /** |
||
735 | * Initializes Jetpack sync cron jobs. |
||
736 | * |
||
737 | * @access public |
||
738 | * @static |
||
739 | */ |
||
740 | public static function init_sync_cron_jobs() { |
||
766 | |||
767 | /** |
||
768 | * Perform maintenance when a plugin upgrade occurs. |
||
769 | * |
||
770 | * @access public |
||
771 | * @static |
||
772 | * |
||
773 | * @param string $new_version New version of the plugin. |
||
774 | * @param string $old_version Old version of the plugin. |
||
775 | */ |
||
776 | public static function cleanup_on_upgrade( $new_version = null, $old_version = null ) { |
||
793 | |||
794 | /** |
||
795 | * Get syncing status for the given fields. |
||
796 | * |
||
797 | * @access public |
||
798 | * @static |
||
799 | * |
||
800 | * @param string|null $fields A comma-separated string of the fields to include in the array from the JSON response. |
||
801 | * @return array An associative array with the status report. |
||
802 | */ |
||
803 | public static function get_sync_status( $fields = null ) { |
||
863 | } |
||
864 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: