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() { | ||
| 18 | |||
| 19 | // everything below this point should only happen if we're a valid sync site | ||
| 20 | 		if ( ! self::sync_allowed() ) { | ||
| 21 | return; | ||
| 22 | } | ||
| 23 | |||
| 24 | 		if ( self::sync_via_cron_allowed() ) { | ||
| 25 | self::init_sync_cron_jobs(); | ||
| 26 | 		} else if ( wp_next_scheduled( 'jetpack_sync_cron' ) ) { | ||
| 27 | self::clear_sync_cron_jobs(); | ||
| 28 | } | ||
| 29 | // When importing via cron, do not sync | ||
| 30 | add_action( 'wp_cron_importer_hook', array( __CLASS__, 'set_is_importing_true' ), 1 ); | ||
| 31 | |||
| 32 | // Sync connected user role changes to .com | ||
| 33 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-users.php'; | ||
| 34 | |||
| 35 | // publicize filter to prevent publicizing blacklisted post types | ||
| 36 | add_filter( 'publicize_should_publicize_published_post', array( __CLASS__, 'prevent_publicize_blacklisted_posts' ), 10, 2 ); | ||
| 37 | |||
| 38 | /** | ||
| 39 | * Fires on every request before default loading sync listener code. | ||
| 40 | * Return false to not load sync listener code that monitors common | ||
| 41 | * WP actions to be serialized. | ||
| 42 | * | ||
| 43 | * By default this returns true for cron jobs, non-GET-requests, or requests where the | ||
| 44 | * user is logged-in. | ||
| 45 | * | ||
| 46 | * @since 4.2.0 | ||
| 47 | * | ||
| 48 | * @param bool should we load sync listener code for this request | ||
| 49 | */ | ||
| 50 | 		if ( apply_filters( 'jetpack_sync_listener_should_load', true ) ) { | ||
| 51 | self::initialize_listener(); | ||
| 52 | } | ||
| 53 | |||
| 54 | add_action( 'init', array( __CLASS__, 'add_sender_shutdown' ), 90 ); | ||
| 55 | } | ||
| 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( 'jetpack_sync_sender_should_load', | ||
| 71 | ( | ||
| 72 | ( isset( $_SERVER["REQUEST_METHOD"] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) | ||
| 73 | || | ||
| 74 | current_user_can( 'manage_options' ) | ||
| 75 | || | ||
| 76 | is_admin() | ||
| 77 | || | ||
| 78 | defined( 'PHPUNIT_JETPACK_TESTSUITE' ) | ||
| 79 | ) | ||
| 80 | 		) ) { | ||
| 81 | self::initialize_sender(); | ||
| 82 | add_action( 'shutdown', array( self::$sender, 'do_sync' ) ); | ||
| 83 | add_action( 'shutdown', array( self::$sender, 'do_full_sync' ) ); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | 	static function sync_allowed() { | ||
| 88 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; | ||
| 89 | return ( ! Jetpack_Sync_Settings::get_setting( 'disable' ) | ||
| 90 | && ( doing_action( 'jetpack_user_authorized' ) || Jetpack::is_active() ) | ||
| 91 | && ! ( Jetpack::is_development_mode() || Jetpack::is_staging_site() ) ) | ||
| 92 | || defined( 'PHPUNIT_JETPACK_TESTSUITE' ); | ||
| 93 | } | ||
| 94 | |||
| 95 | 	static function sync_via_cron_allowed() { | ||
| 96 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; | ||
| 97 | return ( Jetpack_Sync_Settings::get_setting( 'sync_via_cron' ) ); | ||
| 98 | } | ||
| 99 | |||
| 100 | 	static function prevent_publicize_blacklisted_posts( $should_publicize, $post ) { | ||
| 101 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; | ||
| 102 | 		if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) { | ||
| 103 | return false; | ||
| 104 | } | ||
| 105 | |||
| 106 | return $should_publicize; | ||
| 107 | } | ||
| 108 | |||
| 109 | 	static function set_is_importing_true() { | ||
| 110 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; | ||
| 111 | Jetpack_Sync_Settings::set_importing( true ); | ||
| 112 | } | ||
| 113 | |||
| 114 | 	static function send_data( $data, $codec_name, $sent_timestamp, $queue_id, $checkout_duration, $preprocess_duration ) { | ||
| 115 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-functions.php'; | ||
| 116 | Jetpack::load_xml_rpc_client(); | ||
| 117 | |||
| 118 | $query_args = array( | ||
| 119 | 'sync' => '1', // add an extra parameter to the URL so we can tell it's a sync action | ||
| 120 | 'codec' => $codec_name, // send the name of the codec used to encode the data | ||
| 121 | 'timestamp' => $sent_timestamp, // send current server time so we can compensate for clock differences | ||
| 122 | 'queue' => $queue_id, // sync or full_sync | ||
| 123 | 'home' => Jetpack_Sync_Functions::home_url(), // Send home url option to check for Identity Crisis server-side | ||
| 124 | 'siteurl' => Jetpack_Sync_Functions::site_url(), // Send siteurl option to check for Identity Crisis server-side | ||
| 125 | 'cd' => sprintf( '%.4f', $checkout_duration), // Time spent retrieving queue items from the DB | ||
| 126 | 'pd' => sprintf( '%.4f', $preprocess_duration), // Time spent converting queue items into data to send | ||
| 127 | ); | ||
| 128 | |||
| 129 | // Has the site opted in to IDC mitigation? | ||
| 130 | 		if ( Jetpack::sync_idc_optin() ) { | ||
| 131 | $query_args['idc'] = true; | ||
| 132 | } | ||
| 133 | |||
| 134 | 		if ( Jetpack_Options::get_option( 'migrate_for_idc', false ) ) { | ||
| 135 | $query_args['migrate_for_idc'] = true; | ||
| 136 | } | ||
| 137 | |||
| 138 | $query_args['timeout'] = Jetpack_Sync_Settings::is_doing_cron() ? 30 : 15; | ||
| 139 | |||
| 140 | /** | ||
| 141 | * Filters query parameters appended to the Sync request URL sent to WordPress.com. | ||
| 142 | * | ||
| 143 | * @since 4.7.0 | ||
| 144 | * | ||
| 145 | * @param array $query_args associative array of query parameters. | ||
| 146 | */ | ||
| 147 | $query_args = apply_filters( 'jetpack_sync_send_data_query_args', $query_args ); | ||
| 148 | |||
| 149 | $url = add_query_arg( $query_args, Jetpack::xmlrpc_api_url() ); | ||
| 150 | |||
| 151 | $rpc = new Jetpack_IXR_Client( array( | ||
| 152 | 'url' => $url, | ||
| 153 | 'user_id' => JETPACK_MASTER_USER, | ||
| 154 | 'timeout' => $query_args['timeout'], | ||
| 155 | ) ); | ||
| 156 | |||
| 157 | $result = $rpc->query( 'jetpack.syncActions', $data ); | ||
| 158 | |||
| 159 | 		if ( ! $result ) { | ||
| 160 | return $rpc->get_jetpack_error(); | ||
| 161 | } | ||
| 162 | |||
| 163 | $response = $rpc->getResponse(); | ||
| 164 | |||
| 165 | // Check if WordPress.com IDC mitigation blocked the sync request | ||
| 166 | 		if ( is_array( $response ) && isset( $response['error_code'] ) ) { | ||
| 167 | $error_code = $response['error_code']; | ||
| 168 | $allowed_idc_error_codes = array( | ||
| 169 | 'jetpack_url_mismatch', | ||
| 170 | 'jetpack_home_url_mismatch', | ||
| 171 | 'jetpack_site_url_mismatch' | ||
| 172 | ); | ||
| 173 | |||
| 174 | 			if ( in_array( $error_code, $allowed_idc_error_codes ) ) { | ||
| 175 | Jetpack_Options::update_option( | ||
| 176 | 'sync_error_idc', | ||
| 177 | Jetpack::get_sync_error_idc_option( $response ) | ||
| 178 | ); | ||
| 179 | } | ||
| 180 | |||
| 181 | return new WP_Error( | ||
| 182 | 'sync_error_idc', | ||
| 183 | esc_html__( 'Sync has been blocked from WordPress.com because it would cause an identity crisis', 'jetpack' ) | ||
| 184 | ); | ||
| 185 | } | ||
| 186 | |||
| 187 | return $response; | ||
| 188 | } | ||
| 189 | |||
| 190 | 	static function do_initial_sync() { | ||
| 191 | // Lets not sync if we are not suppose to. | ||
| 192 | 		if ( ! self::sync_allowed() ) { | ||
| 193 | return false; | ||
| 194 | } | ||
| 195 | |||
| 196 | $initial_sync_config = array( | ||
| 197 | 'options' => true, | ||
| 198 | 'functions' => true, | ||
| 199 | 'constants' => true, | ||
| 200 | 'users' => array( get_current_user_id() ), | ||
| 201 | ); | ||
| 202 | |||
| 203 | 		if ( is_multisite() ) { | ||
| 204 | $initial_sync_config['network_options'] = true; | ||
| 205 | } | ||
| 206 | |||
| 207 | self::do_full_sync( $initial_sync_config ); | ||
| 208 | } | ||
| 209 | |||
| 210 | 	static function do_full_sync( $modules = null ) { | ||
| 211 | 		if ( ! self::sync_allowed() ) { | ||
| 212 | return false; | ||
| 213 | } | ||
| 214 | |||
| 215 | $full_sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' ); | ||
| 216 | |||
| 217 | 		if ( ! $full_sync_module ) { | ||
| 218 | return false; | ||
| 219 | } | ||
| 220 | |||
| 221 | self::initialize_listener(); | ||
| 222 | |||
| 223 | $full_sync_module->start( $modules ); | ||
| 224 | |||
| 225 | return true; | ||
| 226 | } | ||
| 227 | |||
| 228 | 	static function jetpack_cron_schedule( $schedules ) { | ||
| 229 | 		if ( ! isset( $schedules[ self::DEFAULT_SYNC_CRON_INTERVAL_NAME ] ) ) { | ||
| 230 | $schedules[ self::DEFAULT_SYNC_CRON_INTERVAL_NAME ] = array( | ||
| 231 | 'interval' => self::DEFAULT_SYNC_CRON_INTERVAL_VALUE, | ||
| 232 | 'display' => sprintf( | ||
| 233 | esc_html( _n( 'Every minute', 'Every %d minutes', intval( self::DEFAULT_SYNC_CRON_INTERVAL_VALUE / 60 ), 'jetpack' ) ), | ||
| 234 | intval( self::DEFAULT_SYNC_CRON_INTERVAL_VALUE / 60 ) | ||
| 235 | ) | ||
| 236 | ); | ||
| 237 | } | ||
| 238 | return $schedules; | ||
| 239 | } | ||
| 240 | |||
| 241 | 	static function do_cron_sync() { | ||
| 242 | self::do_cron_sync_by_type( 'sync' ); | ||
| 243 | } | ||
| 244 | |||
| 245 | 	static function do_cron_full_sync() { | ||
| 246 | self::do_cron_sync_by_type( 'full_sync' ); | ||
| 247 | } | ||
| 248 | |||
| 249 | /** | ||
| 250 | * Try to send actions until we run out of things to send, | ||
| 251 | * or have to wait more than 15s before sending again, | ||
| 252 | * or we hit a lock or some other sending issue | ||
| 253 | * | ||
| 254 | * @param string $type Sync type. Can be `sync` or `full_sync`. | ||
| 255 | */ | ||
| 256 | 	static function do_cron_sync_by_type( $type ) { | ||
| 257 | 		if ( ! self::sync_allowed() || ( 'sync' !== $type && 'full_sync' !== $type ) ) { | ||
| 258 | return; | ||
| 259 | } | ||
| 260 | |||
| 261 | self::initialize_sender(); | ||
| 262 | |||
| 263 | $time_limit = Jetpack_Sync_Settings::get_setting( 'cron_sync_time_limit' ); | ||
| 264 | $start_time = time(); | ||
| 265 | |||
| 266 | 		do { | ||
| 267 | $next_sync_time = self::$sender->get_next_sync_time( $type ); | ||
| 268 | |||
| 269 | 			if ( $next_sync_time ) { | ||
| 270 | $delay = $next_sync_time - time() + 1; | ||
| 271 | 				if ( $delay > 15 ) { | ||
| 272 | break; | ||
| 273 | 				} elseif ( $delay > 0 ) { | ||
| 274 | sleep( $delay ); | ||
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 | $result = 'full_sync' === $type ? self::$sender->do_full_sync() : self::$sender->do_sync(); | ||
| 279 | } while ( $result && ! is_wp_error( $result ) && ( $start_time + $time_limit ) > time() ); | ||
| 280 | } | ||
| 281 | |||
| 282 | 	static function initialize_listener() { | ||
| 283 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php'; | ||
| 284 | self::$listener = Jetpack_Sync_Listener::get_instance(); | ||
| 285 | } | ||
| 286 | |||
| 287 | 	static function initialize_sender() { | ||
| 288 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-sender.php'; | ||
| 289 | self::$sender = Jetpack_Sync_Sender::get_instance(); | ||
| 290 | |||
| 291 | // bind the sending process | ||
| 292 | add_filter( 'jetpack_sync_send_data', array( __CLASS__, 'send_data' ), 10, 6 ); | ||
| 293 | } | ||
| 294 | |||
| 295 | 	static function initialize_woocommerce() { | ||
| 296 | 		if ( false === class_exists( 'WooCommerce' ) ) { | ||
| 297 | return; | ||
| 298 | } | ||
| 299 | add_filter( 'jetpack_sync_modules', array( 'Jetpack_Sync_Actions', 'add_woocommerce_sync_module' ) ); | ||
| 300 | } | ||
| 301 | |||
| 302 | 	static function add_woocommerce_sync_module( $sync_modules ) { | ||
| 303 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-module-woocommerce.php'; | ||
| 304 | $sync_modules[] = 'Jetpack_Sync_Module_WooCommerce'; | ||
| 305 | return $sync_modules; | ||
| 306 | } | ||
| 307 | |||
| 308 | 	static function initialize_wp_super_cache() { | ||
| 309 | 		if ( false === function_exists( 'wp_cache_is_enabled' ) ) { | ||
| 310 | return; | ||
| 311 | } | ||
| 312 | add_filter( 'jetpack_sync_modules', array( 'Jetpack_Sync_Actions', 'add_wp_super_cache_sync_module' ) ); | ||
| 313 | } | ||
| 314 | |||
| 315 | 	static function add_wp_super_cache_sync_module( $sync_modules ) { | ||
| 316 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-module-wp-super-cache.php'; | ||
| 317 | $sync_modules[] = 'Jetpack_Sync_Module_WP_Super_Cache'; | ||
| 318 | return $sync_modules; | ||
| 319 | } | ||
| 320 | |||
| 321 | 	static function sanitize_filtered_sync_cron_schedule( $schedule ) { | ||
| 332 | |||
| 333 | 	static function get_start_time_offset( $schedule = '', $hook = '' ) { | ||
| 334 | $start_time_offset = is_multisite() | ||
| 335 | ? mt_rand( 0, ( 2 * self::DEFAULT_SYNC_CRON_INTERVAL_VALUE ) ) | ||
| 336 | : 0; | ||
| 337 | |||
| 338 | /** | ||
| 339 | * Allows overriding the offset that the sync cron jobs will first run. This can be useful when scheduling | ||
| 340 | * cron jobs across multiple sites in a network. | ||
| 341 | * | ||
| 342 | * @since 4.5 | ||
| 343 | * | ||
| 344 | * @param int $start_time_offset | ||
| 345 | * @param string $hook | ||
| 346 | * @param string $schedule | ||
| 347 | */ | ||
| 348 | return intval( apply_filters( | ||
| 349 | 'jetpack_sync_cron_start_time_offset', | ||
| 350 | $start_time_offset, | ||
| 351 | $hook, | ||
| 355 | |||
| 356 | 	static function maybe_schedule_sync_cron( $schedule, $hook ) { | ||
| 372 | |||
| 373 | 	static function clear_sync_cron_jobs() { | ||
| 377 | |||
| 378 | 	static function init_sync_cron_jobs() { | ||
| 404 | |||
| 405 | 	static function cleanup_on_upgrade( $new_version = null, $old_version = null ) { | ||
| 419 | |||
| 420 | 	static function get_sync_status() { | ||
| 444 | } | ||
| 445 | |||
| 465 | 
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.