Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 = '1min'; |
||
| 13 | |||
| 14 | static function init() { |
||
| 15 | |||
| 16 | // Add a custom "every minute" cron schedule |
||
| 17 | add_filter( 'cron_schedules', array( __CLASS__, 'minute_cron_schedule' ) ); |
||
| 18 | |||
| 19 | // On jetpack authorization, schedule a full sync |
||
| 20 | add_action( 'jetpack_client_authorized', array( __CLASS__, 'do_full_sync' ), 10, 0 ); |
||
| 21 | |||
| 22 | // When importing via cron, do not sync |
||
| 23 | add_action( 'wp_cron_importer_hook', array( __CLASS__, 'set_is_importing_true' ), 1 ); |
||
| 24 | |||
| 25 | // Sync connected user role changes to .com |
||
| 26 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-users.php'; |
||
| 27 | |||
| 28 | // everything below this point should only happen if we're a valid sync site |
||
| 29 | if ( ! self::sync_allowed() ) { |
||
| 30 | return; |
||
| 31 | } |
||
| 32 | |||
| 33 | // publicize filter to prevent publicizing blacklisted post types |
||
| 34 | add_filter( 'publicize_should_publicize_published_post', array( __CLASS__, 'prevent_publicize_blacklisted_posts' ), 10, 2 ); |
||
| 35 | |||
| 36 | // cron hooks |
||
| 37 | add_action( 'jetpack_sync_full', array( __CLASS__, 'do_full_sync' ), 10, 1 ); |
||
| 38 | add_action( 'jetpack_sync_cron', array( __CLASS__, 'do_cron_sync' ) ); |
||
| 39 | add_action( 'jetpack_sync_full_cron', array( __CLASS__, 'do_cron_full_sync' ) ); |
||
| 40 | |||
| 41 | self::init_sync_cron_jobs(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Fires on every request before default loading sync listener code. |
||
| 45 | * Return false to not load sync listener code that monitors common |
||
| 46 | * WP actions to be serialized. |
||
| 47 | * |
||
| 48 | * By default this returns true for cron jobs, non-GET-requests, or requests where the |
||
| 49 | * user is logged-in. |
||
| 50 | * |
||
| 51 | * @since 4.2.0 |
||
| 52 | * |
||
| 53 | * @param bool should we load sync listener code for this request |
||
| 54 | */ |
||
| 55 | if ( apply_filters( 'jetpack_sync_listener_should_load', true ) ) { |
||
| 56 | self::initialize_listener(); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Fires on every request before default loading sync sender code. |
||
| 61 | * Return false to not load sync sender code that serializes pending |
||
| 62 | * data and sends it to WPCOM for processing. |
||
| 63 | * |
||
| 64 | * By default this returns true for cron jobs, POST requests, admin requests, or requests |
||
| 65 | * by users who can manage_options. |
||
| 66 | * |
||
| 67 | * @since 4.2.0 |
||
| 68 | * |
||
| 69 | * @param bool should we load sync sender code for this request |
||
| 70 | */ |
||
| 71 | if ( apply_filters( '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 | defined( 'DOING_CRON' ) && DOING_CRON |
||
| 82 | ) |
||
| 83 | ) ) { |
||
| 84 | self::initialize_sender(); |
||
| 85 | add_action( 'shutdown', array( self::$sender, 'do_sync' ) ); |
||
| 86 | add_action( 'shutdown', array( self::$sender, 'do_full_sync' ) ); |
||
| 87 | } |
||
| 88 | |||
| 89 | } |
||
| 90 | |||
| 91 | static function sync_allowed() { |
||
| 92 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; |
||
| 93 | return ( ! Jetpack_Sync_Settings::get_setting( 'disable' ) && Jetpack::is_active() && ! ( Jetpack::is_development_mode() || Jetpack::is_staging_site() ) ) |
||
| 94 | || defined( 'PHPUNIT_JETPACK_TESTSUITE' ); |
||
| 95 | } |
||
| 96 | |||
| 97 | static function prevent_publicize_blacklisted_posts( $should_publicize, $post ) { |
||
| 98 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; |
||
| 99 | if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) { |
||
| 100 | return false; |
||
| 101 | } |
||
| 102 | |||
| 103 | return $should_publicize; |
||
| 104 | } |
||
| 105 | |||
| 106 | static function set_is_importing_true() { |
||
| 107 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; |
||
| 108 | Jetpack_Sync_Settings::set_importing( true ); |
||
| 109 | } |
||
| 110 | |||
| 111 | static function send_data( $data, $codec_name, $sent_timestamp, $queue_id ) { |
||
| 112 | Jetpack::load_xml_rpc_client(); |
||
| 113 | |||
| 114 | $query_args = array( |
||
| 115 | 'sync' => '1', // add an extra parameter to the URL so we can tell it's a sync action |
||
| 116 | 'codec' => $codec_name, // send the name of the codec used to encode the data |
||
| 117 | 'timestamp' => $sent_timestamp, // send current server time so we can compensate for clock differences |
||
| 118 | 'queue' => $queue_id, // sync or full_sync |
||
| 119 | 'home' => get_home_url(), // Send home url option to check for Identity Crisis server-side |
||
| 120 | 'siteurl' => get_site_url(), // Send siteurl option to check for Identity Crisis server-side |
||
| 121 | ); |
||
| 122 | |||
| 123 | // Has the site opted in to IDC mitigation? |
||
| 124 | if ( Jetpack::sync_idc_optin() ) { |
||
| 125 | $query_args['idc'] = true; |
||
| 126 | } |
||
| 127 | |||
| 128 | $url = add_query_arg( $query_args, 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 | $response = $rpc->getResponse(); |
||
| 143 | |||
| 144 | // Check if WordPress.com IDC mitigation blocked the sync request |
||
| 145 | if ( is_array( $response ) && isset( $response['error_code'] ) ) { |
||
| 146 | $error_code = $response['error_code']; |
||
| 147 | $allowed_idc_error_codes = array( |
||
| 148 | 'jetpack_url_mismatch', |
||
| 149 | 'jetpack_home_url_mismatch', |
||
| 150 | 'jetpack_site_url_mismatch' |
||
| 151 | ); |
||
| 152 | |||
| 153 | if ( in_array( $error_code, $allowed_idc_error_codes ) ) { |
||
| 154 | Jetpack_Options::update_option( |
||
| 155 | 'sync_error_idc', |
||
| 156 | Jetpack::get_sync_error_idc_option( $response ) |
||
| 157 | ); |
||
| 158 | } |
||
| 159 | |||
| 160 | return new WP_Error( |
||
| 161 | 'sync_error_idc', |
||
| 162 | __( 'Sync has been blocked from WordPress.com because it would cause an identity crisis' ) |
||
| 163 | ); |
||
| 164 | } |
||
| 165 | |||
| 166 | return $response; |
||
| 167 | } |
||
| 168 | |||
| 169 | static function do_initial_sync( $new_version = null, $old_version = null ) { |
||
| 170 | $initial_sync_config = array( |
||
| 171 | 'options' => true, |
||
| 172 | 'network_options' => true, |
||
| 173 | 'functions' => true, |
||
| 174 | 'constants' => true, |
||
| 175 | ); |
||
| 176 | |||
| 177 | if ( $old_version && ( version_compare( $old_version, '4.2', '<' ) ) ) { |
||
| 178 | $initial_sync_config['users'] = 'initial'; |
||
| 179 | } |
||
| 180 | |||
| 181 | self::do_full_sync( $initial_sync_config ); |
||
| 182 | } |
||
| 183 | |||
| 184 | static function do_full_sync( $modules = null ) { |
||
| 185 | if ( ! self::sync_allowed() ) { |
||
| 186 | return false; |
||
| 187 | } |
||
| 188 | |||
| 189 | self::initialize_listener(); |
||
| 190 | Jetpack_Sync_Modules::get_module( 'full-sync' )->start( $modules ); |
||
| 191 | |||
| 192 | return true; |
||
| 193 | } |
||
| 194 | |||
| 195 | static function minute_cron_schedule( $schedules ) { |
||
| 204 | |||
| 205 | // try to send actions until we run out of things to send, |
||
| 206 | // or have to wait more than 15s before sending again, |
||
| 207 | // or we hit a lock or some other sending issue |
||
| 208 | View Code Duplication | static function do_cron_sync() { |
|
| 235 | |||
| 236 | View Code Duplication | static function do_cron_full_sync() { |
|
| 263 | |||
| 264 | static function initialize_listener() { |
||
| 268 | |||
| 269 | static function initialize_sender() { |
||
| 276 | |||
| 277 | static function sanitize_filtered_sync_cron_schedule( $schedule ) { |
||
| 288 | |||
| 289 | static function maybe_schedule_sync_cron( $schedule, $hook ) { |
||
| 304 | |||
| 305 | static function init_sync_cron_jobs() { |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * If the site is using alternate cron, we need to init the listener and sender before cron |
||
| 330 | * runs. So, we init at a priority of 9. |
||
| 331 | * |
||
| 332 | * If the site is using a regular cron job, we init at a priority of 90 which gives plugins a chance |
||
| 333 | * to add filters before we initialize. |
||
| 343 |
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.