Complex classes like Jetpack_Sync_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 Jetpack_Sync_Actions, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Jetpack_Sync_Actions { |
||
11 | static $sender = null; |
||
|
|||
12 | static $listener = null; |
||
13 | const INITIAL_SYNC_MULTISITE_INTERVAL = 10; |
||
14 | |||
15 | static function init() { |
||
16 | |||
17 | // Add a custom "every minute" cron schedule |
||
18 | add_filter( 'cron_schedules', array( __CLASS__, 'minute_cron_schedule' ) ); |
||
19 | |||
20 | // On jetpack authorization, schedule a full sync |
||
21 | add_action( 'jetpack_client_authorized', array( __CLASS__, 'schedule_full_sync' ), 10, 0 ); |
||
22 | |||
23 | // When importing via cron, do not sync |
||
24 | add_action( 'wp_cron_importer_hook', array( __CLASS__, 'set_is_importing_true' ), 1 ); |
||
25 | |||
26 | // Sync connected user role changes to .com |
||
27 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-users.php'; |
||
28 | |||
29 | // everything below this point should only happen if we're a valid sync site |
||
30 | if ( ! self::sync_allowed() ) { |
||
31 | return; |
||
32 | } |
||
33 | |||
34 | // publicize filter to prevent publicizing blacklisted post types |
||
35 | add_filter( 'publicize_should_publicize_published_post', array( __CLASS__, 'prevent_publicize_blacklisted_posts' ), 10, 2 ); |
||
36 | |||
37 | // cron hooks |
||
38 | add_action( 'jetpack_sync_full', array( __CLASS__, 'do_full_sync' ), 10, 1 ); |
||
39 | add_action( 'jetpack_sync_cron', array( __CLASS__, 'do_cron_sync' ) ); |
||
40 | |||
41 | if ( ! wp_next_scheduled( 'jetpack_sync_cron' ) ) { |
||
42 | // Schedule a job to send pending queue items once a minute |
||
43 | wp_schedule_event( time(), '1min', 'jetpack_sync_cron' ); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Fires on every request before default loading sync listener code. |
||
48 | * Return false to not load sync listener code that monitors common |
||
49 | * WP actions to be serialized. |
||
50 | * |
||
51 | * By default this returns true for cron jobs, non-GET-requests, or requests where the |
||
52 | * user is logged-in. |
||
53 | * |
||
54 | * @since 4.2.0 |
||
55 | * |
||
56 | * @param bool should we load sync listener code for this request |
||
57 | */ |
||
58 | if ( apply_filters( 'jetpack_sync_listener_should_load', |
||
59 | ( |
||
60 | ( isset( $_SERVER["REQUEST_METHOD"] ) && 'GET' !== $_SERVER['REQUEST_METHOD'] ) |
||
61 | || |
||
62 | is_user_logged_in() |
||
63 | || |
||
64 | defined( 'PHPUNIT_JETPACK_TESTSUITE' ) |
||
65 | || |
||
66 | defined( 'DOING_CRON' ) && DOING_CRON |
||
67 | ) |
||
68 | ) ) { |
||
69 | self::initialize_listener(); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Fires on every request before default loading sync sender code. |
||
74 | * Return false to not load sync sender code that serializes pending |
||
75 | * data and sends it to WPCOM for processing. |
||
76 | * |
||
77 | * By default this returns true for cron jobs, POST requests, admin requests, or requests |
||
78 | * by users who can manage_options. |
||
79 | * |
||
80 | * @since 4.2.0 |
||
81 | * |
||
82 | * @param bool should we load sync sender code for this request |
||
83 | */ |
||
84 | if ( apply_filters( 'jetpack_sync_sender_should_load', |
||
85 | ( |
||
86 | ( isset( $_SERVER["REQUEST_METHOD"] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) |
||
87 | || |
||
88 | current_user_can( 'manage_options' ) |
||
89 | || |
||
90 | is_admin() |
||
91 | || |
||
92 | defined( 'PHPUNIT_JETPACK_TESTSUITE' ) |
||
93 | || |
||
94 | defined( 'DOING_CRON' ) && DOING_CRON |
||
95 | ) |
||
96 | ) ) { |
||
97 | self::initialize_sender(); |
||
98 | add_action( 'shutdown', array( self::$sender, 'do_sync' ) ); |
||
99 | } |
||
100 | |||
101 | } |
||
102 | |||
103 | static function sync_allowed() { |
||
104 | return ( ! Jetpack_Sync_Settings::get_setting( 'disable' ) && Jetpack::is_active() && ! ( Jetpack::is_development_mode() || Jetpack::is_staging_site() ) ) |
||
105 | || defined( 'PHPUNIT_JETPACK_TESTSUITE' ); |
||
106 | } |
||
107 | |||
108 | static function prevent_publicize_blacklisted_posts( $should_publicize, $post ) { |
||
109 | if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) { |
||
110 | return false; |
||
111 | } |
||
112 | |||
113 | return $should_publicize; |
||
114 | } |
||
115 | |||
116 | static function set_is_importing_true() { |
||
117 | Jetpack_Sync_Settings::set_importing( true ); |
||
118 | } |
||
119 | |||
120 | static function send_data( $data, $codec_name, $sent_timestamp, $queue_id ) { |
||
121 | Jetpack::load_xml_rpc_client(); |
||
122 | |||
123 | $url = add_query_arg( array( |
||
124 | 'sync' => '1', // add an extra parameter to the URL so we can tell it's a sync action |
||
125 | 'codec' => $codec_name, // send the name of the codec used to encode the data |
||
126 | 'timestamp' => $sent_timestamp, // send current server time so we can compensate for clock differences |
||
127 | 'queue' => $queue_id, // sync or full_sync |
||
128 | ), Jetpack::xmlrpc_api_url() ); |
||
129 | |||
130 | $rpc = new Jetpack_IXR_Client( array( |
||
131 | 'url' => $url, |
||
132 | 'user_id' => JETPACK_MASTER_USER, |
||
133 | 'timeout' => 30, |
||
134 | ) ); |
||
135 | |||
136 | $result = $rpc->query( 'jetpack.syncActions', $data ); |
||
137 | |||
138 | if ( ! $result ) { |
||
139 | return $rpc->get_jetpack_error(); |
||
140 | } |
||
141 | |||
142 | return $rpc->getResponse(); |
||
143 | } |
||
144 | |||
145 | static function schedule_initial_sync( $new_version = null, $old_version = null ) { |
||
146 | $initial_sync_config = array( |
||
147 | 'options' => true, |
||
148 | 'network_options' => true, |
||
149 | 'functions' => true, |
||
150 | 'constants' => true, |
||
151 | ); |
||
152 | |||
153 | if ( $old_version && ( version_compare( $old_version, '4.2', '<' ) ) ) { |
||
154 | $initial_sync_config['users'] = 'initial'; |
||
155 | } |
||
156 | |||
157 | // we need this function call here because we have to run this function |
||
158 | // reeeeally early in init, before WP_CRON_LOCK_TIMEOUT is defined. |
||
159 | wp_functionality_constants(); |
||
160 | |||
161 | if ( is_multisite() ) { |
||
162 | // stagger initial syncs for multisite blogs so they don't all pile on top of each other |
||
163 | $time_offset = ( rand() / getrandmax() ) * self::INITIAL_SYNC_MULTISITE_INTERVAL * get_blog_count(); |
||
164 | } else { |
||
165 | $time_offset = 1; |
||
166 | } |
||
167 | |||
168 | self::schedule_full_sync( |
||
169 | $initial_sync_config, |
||
170 | $time_offset |
||
171 | ); |
||
172 | } |
||
173 | |||
174 | static function schedule_full_sync( $modules = null, $time_offset = 1 ) { |
||
175 | if ( ! self::sync_allowed() ) { |
||
176 | return false; |
||
177 | } |
||
178 | |||
179 | if ( self::is_scheduled_full_sync() ) { |
||
180 | self::unschedule_all_full_syncs(); |
||
181 | } |
||
182 | |||
183 | if ( $modules ) { |
||
184 | wp_schedule_single_event( time() + $time_offset, 'jetpack_sync_full', array( $modules ) ); |
||
185 | } else { |
||
186 | wp_schedule_single_event( time() + $time_offset, 'jetpack_sync_full' ); |
||
187 | } |
||
188 | |||
189 | if ( $time_offset === 1 ) { |
||
190 | spawn_cron(); |
||
191 | } |
||
192 | |||
193 | return true; |
||
194 | } |
||
195 | |||
196 | static function unschedule_all_full_syncs() { |
||
197 | foreach ( _get_cron_array() as $timestamp => $cron ) { |
||
198 | if ( ! empty( $cron['jetpack_sync_full'] ) ) { |
||
199 | foreach( $cron['jetpack_sync_full'] as $key => $config ) { |
||
200 | wp_unschedule_event( $timestamp, 'jetpack_sync_full', $config['args'] ); |
||
201 | } |
||
202 | } |
||
203 | } |
||
204 | } |
||
205 | |||
206 | static function is_scheduled_full_sync( $modules = null ) { |
||
207 | if ( is_null( $modules ) ) { |
||
208 | $crons = _get_cron_array(); |
||
209 | |||
210 | foreach ( $crons as $timestamp => $cron ) { |
||
211 | if ( ! empty( $cron['jetpack_sync_full'] ) ) { |
||
212 | return true; |
||
213 | } |
||
214 | } |
||
215 | return false; |
||
216 | } |
||
217 | |||
218 | return !! wp_next_scheduled( 'jetpack_sync_full', array( $modules ) ); |
||
219 | } |
||
220 | |||
221 | static function do_full_sync( $modules = null ) { |
||
230 | |||
231 | static function minute_cron_schedule( $schedules ) { |
||
240 | |||
241 | // try to send actions until we run out of things to send, |
||
242 | // or have to wait more than 15s before sending again, |
||
243 | // or we hit a lock or some other sending issue |
||
244 | static function do_cron_sync() { |
||
245 | if ( ! self::sync_allowed() ) { |
||
246 | return; |
||
247 | } |
||
248 | |||
249 | self::initialize_sender(); |
||
271 | |||
272 | static function initialize_listener() { |
||
276 | |||
277 | static function initialize_sender() { |
||
284 | } |
||
285 | |||
292 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.