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 |
||
| 9 | class Jetpack_Sync_Actions { |
||
| 10 | static $sender = null; |
||
|
|
|||
| 11 | static $listener = null; |
||
| 12 | const DEFAULT_SYNC_CRON_INTERVAL_NAME = 'jetpack_sync_interval'; |
||
| 13 | const DEFAULT_SYNC_CRON_INTERVAL_VALUE = 300; // 5 * MINUTE_IN_SECONDS; |
||
| 14 | |||
| 15 | static function init() { |
||
| 16 | |||
| 17 | // everything below this point should only happen if we're a valid sync site |
||
| 18 | if ( ! self::sync_allowed() ) { |
||
| 19 | return; |
||
| 20 | } |
||
| 21 | |||
| 22 | if ( self::sync_via_cron_allowed() ) { |
||
| 23 | self::init_sync_cron_jobs(); |
||
| 24 | } else if ( wp_next_scheduled( 'jetpack_sync_cron' ) ) { |
||
| 25 | wp_clear_scheduled_hook( 'jetpack_sync_cron' ); |
||
| 26 | wp_clear_scheduled_hook( 'jetpack_sync_full_cron' ); |
||
| 27 | } |
||
| 28 | |||
| 29 | // On jetpack authorization, schedule a full sync |
||
| 30 | add_action( 'jetpack_client_authorized', array( __CLASS__, 'do_full_sync' ), 10, 0 ); |
||
| 31 | |||
| 32 | // When importing via cron, do not sync |
||
| 33 | add_action( 'wp_cron_importer_hook', array( __CLASS__, 'set_is_importing_true' ), 1 ); |
||
| 34 | |||
| 35 | // Sync connected user role changes to .com |
||
| 36 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-users.php'; |
||
| 37 | |||
| 38 | // publicize filter to prevent publicizing blacklisted post types |
||
| 39 | add_filter( 'publicize_should_publicize_published_post', array( __CLASS__, 'prevent_publicize_blacklisted_posts' ), 10, 2 ); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Fires on every request before default loading sync listener code. |
||
| 43 | * Return false to not load sync listener code that monitors common |
||
| 44 | * WP actions to be serialized. |
||
| 45 | * |
||
| 46 | * By default this returns true for cron jobs, non-GET-requests, or requests where the |
||
| 47 | * user is logged-in. |
||
| 48 | * |
||
| 49 | * @since 4.2.0 |
||
| 50 | * |
||
| 51 | * @param bool should we load sync listener code for this request |
||
| 52 | */ |
||
| 53 | if ( apply_filters( 'jetpack_sync_listener_should_load', true ) ) { |
||
| 54 | self::initialize_listener(); |
||
| 55 | } |
||
| 56 | |||
| 57 | add_action( 'init', array( __CLASS__, 'add_sender_shutdown' ), 90 ); |
||
| 58 | |||
| 59 | } |
||
| 60 | |||
| 61 | static function add_sender_shutdown() { |
||
| 62 | /** |
||
| 63 | * Fires on every request before default loading sync sender code. |
||
| 64 | * Return false to not load sync sender code that serializes pending |
||
| 65 | * data and sends it to WPCOM for processing. |
||
| 66 | * |
||
| 67 | * By default this returns true for cron jobs, POST requests, admin requests, or requests |
||
| 68 | * by users who can manage_options. |
||
| 69 | * |
||
| 70 | * @since 4.2.0 |
||
| 71 | * |
||
| 72 | * @param bool should we load sync sender code for this request |
||
| 73 | */ |
||
| 74 | if ( apply_filters( 'jetpack_sync_sender_should_load', |
||
| 75 | ( |
||
| 76 | ( isset( $_SERVER["REQUEST_METHOD"] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) |
||
| 77 | || |
||
| 78 | current_user_can( 'manage_options' ) |
||
| 79 | || |
||
| 80 | is_admin() |
||
| 81 | || |
||
| 82 | defined( 'PHPUNIT_JETPACK_TESTSUITE' ) |
||
| 83 | ) |
||
| 84 | ) ) { |
||
| 85 | self::initialize_sender(); |
||
| 86 | add_action( 'shutdown', array( self::$sender, 'do_sync' ) ); |
||
| 87 | add_action( 'shutdown', array( self::$sender, 'do_full_sync' ) ); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | static function sync_allowed() { |
||
| 96 | |||
| 97 | static function sync_via_cron_allowed() { |
||
| 98 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; |
||
| 101 | |||
| 102 | static function prevent_publicize_blacklisted_posts( $should_publicize, $post ) { |
||
| 110 | |||
| 111 | static function set_is_importing_true() { |
||
| 115 | |||
| 116 | static function send_data( $data, $codec_name, $sent_timestamp, $queue_id ) { |
||
| 179 | |||
| 180 | static function do_initial_sync( $new_version = null, $old_version = null ) { |
||
| 194 | |||
| 195 | static function do_full_sync( $modules = null ) { |
||
| 205 | |||
| 206 | static function jetpack_cron_schedule( $schedules ) { |
||
| 218 | |||
| 219 | // try to send actions until we run out of things to send, |
||
| 220 | // or have to wait more than 15s before sending again, |
||
| 221 | // or we hit a lock or some other sending issue |
||
| 222 | static function do_cron_sync() { |
||
| 230 | |||
| 231 | static function do_cron_full_sync() { |
||
| 239 | |||
| 240 | static function initialize_listener() { |
||
| 244 | |||
| 245 | static function initialize_sender() { |
||
| 252 | |||
| 253 | static function sanitize_filtered_sync_cron_schedule( $schedule ) { |
||
| 264 | |||
| 265 | static function maybe_schedule_sync_cron( $schedule, $hook ) { |
||
| 280 | |||
| 281 | static function init_sync_cron_jobs() { |
||
| 311 | |||
| 312 | static function cleanup_on_upgrade() { |
||
| 317 | |||
| 318 | static function get_sync_status() { |
||
| 341 | } |
||
| 342 | |||
| 355 |
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.