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 |
||
11 | class Jetpack_Sync_Actions { |
||
12 | static $sender = null; |
||
13 | static $listener = null; |
||
14 | const DEFAULT_SYNC_CRON_INTERVAL_NAME = 'jetpack_sync_interval'; |
||
15 | const DEFAULT_SYNC_CRON_INTERVAL_VALUE = 300; // 5 * MINUTE_IN_SECONDS; |
||
16 | |||
17 | static function init() { |
||
56 | |||
57 | static function add_sender_shutdown() { |
||
58 | /** |
||
59 | * Fires on every request before default loading sync sender code. |
||
60 | * Return false to not load sync sender code that serializes pending |
||
61 | * data and sends it to WPCOM for processing. |
||
62 | * |
||
63 | * By default this returns true for cron jobs, POST requests, admin requests, or requests |
||
64 | * by users who can manage_options. |
||
65 | * |
||
66 | * @since 4.2.0 |
||
67 | * |
||
68 | * @param bool should we load sync sender code for this request |
||
69 | */ |
||
70 | if ( apply_filters( |
||
71 | 'jetpack_sync_sender_should_load', |
||
72 | ( |
||
73 | ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) |
||
74 | || |
||
75 | current_user_can( 'manage_options' ) |
||
76 | || |
||
77 | is_admin() |
||
78 | || |
||
79 | defined( 'PHPUNIT_JETPACK_TESTSUITE' ) |
||
80 | ) |
||
81 | ) ) { |
||
82 | self::initialize_sender(); |
||
83 | add_action( 'shutdown', array( self::$sender, 'do_sync' ) ); |
||
84 | add_action( 'shutdown', array( self::$sender, 'do_full_sync' ) ); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | static function sync_allowed() { |
||
89 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; |
||
90 | return ( ! Jetpack_Sync_Settings::get_setting( 'disable' ) |
||
91 | && ( doing_action( 'jetpack_user_authorized' ) || Jetpack::is_active() ) |
||
92 | && ! ( Jetpack::is_development_mode() || Jetpack::is_staging_site() ) ) |
||
93 | || defined( 'PHPUNIT_JETPACK_TESTSUITE' ); |
||
94 | } |
||
95 | |||
96 | static function sync_via_cron_allowed() { |
||
97 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; |
||
98 | return ( Jetpack_Sync_Settings::get_setting( 'sync_via_cron' ) ); |
||
99 | } |
||
100 | |||
101 | static function prevent_publicize_blacklisted_posts( $should_publicize, $post ) { |
||
102 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; |
||
103 | if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) { |
||
104 | return false; |
||
105 | } |
||
106 | |||
107 | return $should_publicize; |
||
108 | } |
||
109 | |||
110 | static function set_is_importing_true() { |
||
111 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; |
||
112 | Jetpack_Sync_Settings::set_importing( true ); |
||
113 | } |
||
114 | |||
115 | static function send_data( $data, $codec_name, $sent_timestamp, $queue_id, $checkout_duration, $preprocess_duration ) { |
||
116 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-functions.php'; |
||
117 | Jetpack::load_xml_rpc_client(); |
||
118 | |||
119 | $query_args = array( |
||
120 | 'sync' => '1', // add an extra parameter to the URL so we can tell it's a sync action |
||
121 | 'codec' => $codec_name, // send the name of the codec used to encode the data |
||
122 | 'timestamp' => $sent_timestamp, // send current server time so we can compensate for clock differences |
||
123 | 'queue' => $queue_id, // sync or full_sync |
||
124 | 'home' => Jetpack_Sync_Functions::home_url(), // Send home url option to check for Identity Crisis server-side |
||
125 | 'siteurl' => Jetpack_Sync_Functions::site_url(), // Send siteurl option to check for Identity Crisis server-side |
||
126 | 'cd' => sprintf( '%.4f', $checkout_duration ), // Time spent retrieving queue items from the DB |
||
127 | 'pd' => sprintf( '%.4f', $preprocess_duration ), // Time spent converting queue items into data to send |
||
128 | ); |
||
129 | |||
130 | // Has the site opted in to IDC mitigation? |
||
131 | if ( Jetpack::sync_idc_optin() ) { |
||
132 | $query_args['idc'] = true; |
||
133 | } |
||
134 | |||
135 | if ( Jetpack_Options::get_option( 'migrate_for_idc', false ) ) { |
||
136 | $query_args['migrate_for_idc'] = true; |
||
137 | } |
||
138 | |||
139 | $query_args['timeout'] = Jetpack_Sync_Settings::is_doing_cron() ? 30 : 15; |
||
140 | |||
141 | /** |
||
142 | * Filters query parameters appended to the Sync request URL sent to WordPress.com. |
||
143 | * |
||
144 | * @since 4.7.0 |
||
145 | * |
||
146 | * @param array $query_args associative array of query parameters. |
||
147 | */ |
||
148 | $query_args = apply_filters( 'jetpack_sync_send_data_query_args', $query_args ); |
||
149 | |||
150 | $url = add_query_arg( $query_args, Jetpack::xmlrpc_api_url() ); |
||
151 | |||
152 | $rpc = new Jetpack_IXR_Client( |
||
153 | array( |
||
154 | 'url' => $url, |
||
155 | 'user_id' => JETPACK_MASTER_USER, |
||
156 | 'timeout' => $query_args['timeout'], |
||
157 | ) |
||
158 | ); |
||
159 | |||
160 | $result = $rpc->query( 'jetpack.syncActions', $data ); |
||
161 | |||
162 | if ( ! $result ) { |
||
163 | return $rpc->get_jetpack_error(); |
||
164 | } |
||
165 | |||
166 | $response = $rpc->getResponse(); |
||
167 | |||
168 | // Check if WordPress.com IDC mitigation blocked the sync request |
||
169 | if ( is_array( $response ) && isset( $response['error_code'] ) ) { |
||
170 | $error_code = $response['error_code']; |
||
171 | $allowed_idc_error_codes = array( |
||
172 | 'jetpack_url_mismatch', |
||
173 | 'jetpack_home_url_mismatch', |
||
174 | 'jetpack_site_url_mismatch', |
||
175 | ); |
||
176 | |||
177 | if ( in_array( $error_code, $allowed_idc_error_codes ) ) { |
||
178 | Jetpack_Options::update_option( |
||
179 | 'sync_error_idc', |
||
180 | Jetpack::get_sync_error_idc_option( $response ) |
||
181 | ); |
||
182 | } |
||
183 | |||
184 | return new WP_Error( |
||
185 | 'sync_error_idc', |
||
186 | esc_html__( 'Sync has been blocked from WordPress.com because it would cause an identity crisis', 'jetpack' ) |
||
187 | ); |
||
188 | } |
||
189 | |||
190 | return $response; |
||
191 | } |
||
192 | |||
193 | static function do_initial_sync() { |
||
212 | |||
213 | static function do_full_sync( $modules = null ) { |
||
230 | |||
231 | static function jetpack_cron_schedule( $schedules ) { |
||
243 | |||
244 | static function do_cron_sync() { |
||
247 | |||
248 | static function do_cron_full_sync() { |
||
251 | |||
252 | /** |
||
253 | * Try to send actions until we run out of things to send, |
||
254 | * or have to wait more than 15s before sending again, |
||
255 | * or we hit a lock or some other sending issue |
||
256 | * |
||
257 | * @param string $type Sync type. Can be `sync` or `full_sync`. |
||
258 | */ |
||
259 | static function do_cron_sync_by_type( $type ) { |
||
284 | |||
285 | static function initialize_listener() { |
||
289 | |||
290 | static function initialize_sender() { |
||
291 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-sender.php'; |
||
292 | self::$sender = Jetpack_Sync_Sender::get_instance(); |
||
293 | |||
294 | // bind the sending process |
||
295 | add_filter( 'jetpack_sync_send_data', array( __CLASS__, 'send_data' ), 10, 6 ); |
||
297 | |||
298 | static function initialize_woocommerce() { |
||
304 | |||
305 | static function add_woocommerce_sync_module( $sync_modules ) { |
||
310 | |||
311 | static function initialize_wp_super_cache() { |
||
317 | |||
318 | static function add_wp_super_cache_sync_module( $sync_modules ) { |
||
323 | |||
324 | static function sanitize_filtered_sync_cron_schedule( $schedule ) { |
||
335 | |||
336 | static function get_start_time_offset( $schedule = '', $hook = '' ) { |
||
360 | |||
361 | static function maybe_schedule_sync_cron( $schedule, $hook ) { |
||
377 | |||
378 | static function clear_sync_cron_jobs() { |
||
382 | |||
383 | static function init_sync_cron_jobs() { |
||
409 | |||
410 | static function cleanup_on_upgrade( $new_version = null, $old_version = null ) { |
||
426 | |||
427 | static function get_sync_status() { |
||
451 | } |
||
452 | |||
471 |
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.