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 INITIAL_SYNC_MULTISITE_INTERVAL = 10; |
||
| 13 | const DEFAULT_SYNC_CRON_INTERVAL = '1min'; |
||
| 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__, 'do_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 | add_action( 'jetpack_sync_full_cron', array( __CLASS__, 'do_cron_full_sync' ) ); |
||
| 41 | |||
| 42 | self::init_sync_cron_jobs(); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Fires on every request before default loading sync listener code. |
||
| 46 | * Return false to not load sync listener code that monitors common |
||
| 47 | * WP actions to be serialized. |
||
| 48 | * |
||
| 49 | * By default this returns true for cron jobs, non-GET-requests, or requests where the |
||
| 50 | * user is logged-in. |
||
| 51 | * |
||
| 52 | * @since 4.2.0 |
||
| 53 | * |
||
| 54 | * @param bool should we load sync listener code for this request |
||
| 55 | */ |
||
| 56 | if ( apply_filters( 'jetpack_sync_listener_should_load', |
||
| 57 | ( |
||
| 58 | ( isset( $_SERVER["REQUEST_METHOD"] ) && 'GET' !== $_SERVER['REQUEST_METHOD'] ) |
||
| 59 | || |
||
| 60 | is_user_logged_in() |
||
| 61 | || |
||
| 62 | defined( 'PHPUNIT_JETPACK_TESTSUITE' ) |
||
| 63 | || |
||
| 64 | defined( 'DOING_CRON' ) && DOING_CRON |
||
| 65 | ) |
||
| 66 | ) ) { |
||
| 67 | self::initialize_listener(); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Fires on every request before default loading sync sender code. |
||
| 72 | * Return false to not load sync sender code that serializes pending |
||
| 73 | * data and sends it to WPCOM for processing. |
||
| 74 | * |
||
| 75 | * By default this returns true for cron jobs, POST requests, admin requests, or requests |
||
| 76 | * by users who can manage_options. |
||
| 77 | * |
||
| 78 | * @since 4.2.0 |
||
| 79 | * |
||
| 80 | * @param bool should we load sync sender code for this request |
||
| 81 | */ |
||
| 82 | if ( apply_filters( 'jetpack_sync_sender_should_load', |
||
| 83 | ( |
||
| 84 | ( isset( $_SERVER["REQUEST_METHOD"] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) |
||
| 85 | || |
||
| 86 | current_user_can( 'manage_options' ) |
||
| 87 | || |
||
| 88 | is_admin() |
||
| 89 | || |
||
| 90 | defined( 'PHPUNIT_JETPACK_TESTSUITE' ) |
||
| 91 | || |
||
| 92 | defined( 'DOING_CRON' ) && DOING_CRON |
||
| 93 | ) |
||
| 94 | ) ) { |
||
| 95 | self::initialize_sender(); |
||
| 96 | add_action( 'shutdown', array( self::$sender, 'do_sync' ) ); |
||
| 97 | add_action( 'shutdown', array( self::$sender, 'do_full_sync' ) ); |
||
| 98 | } |
||
| 99 | |||
| 100 | } |
||
| 101 | |||
| 102 | static function sync_allowed() { |
||
| 103 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; |
||
| 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 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; |
||
| 110 | if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) { |
||
| 111 | return false; |
||
| 112 | } |
||
| 113 | |||
| 114 | return $should_publicize; |
||
| 115 | } |
||
| 116 | |||
| 117 | static function set_is_importing_true() { |
||
| 118 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; |
||
| 119 | Jetpack_Sync_Settings::set_importing( true ); |
||
| 120 | } |
||
| 121 | |||
| 122 | static function send_data( $data, $codec_name, $sent_timestamp, $queue_id ) { |
||
| 123 | Jetpack::load_xml_rpc_client(); |
||
| 124 | |||
| 125 | $query_args = array( |
||
| 126 | 'sync' => '1', // add an extra parameter to the URL so we can tell it's a sync action |
||
| 127 | 'codec' => $codec_name, // send the name of the codec used to encode the data |
||
| 128 | 'timestamp' => $sent_timestamp, // send current server time so we can compensate for clock differences |
||
| 129 | 'queue' => $queue_id, // sync or full_sync |
||
| 130 | ); |
||
| 131 | |||
| 132 | if ( Jetpack::sync_idc_optin() ) { |
||
| 133 | $query_args['home'] = get_home_url(); // Send home url option to check for Identity Crisis server-side |
||
| 134 | $query_args['siteurl'] = get_site_url(); // Send home url option to check for Identity Crisis server-side |
||
| 135 | } |
||
| 136 | |||
| 137 | $url = add_query_arg( $query_args, Jetpack::xmlrpc_api_url() ); |
||
| 138 | |||
| 139 | $rpc = new Jetpack_IXR_Client( array( |
||
| 140 | 'url' => $url, |
||
| 141 | 'user_id' => JETPACK_MASTER_USER, |
||
| 142 | 'timeout' => 30, |
||
| 143 | ) ); |
||
| 144 | |||
| 145 | $result = $rpc->query( 'jetpack.syncActions', $data ); |
||
| 146 | |||
| 147 | if ( ! $result ) { |
||
| 148 | $error = $rpc->get_jetpack_error(); |
||
| 149 | if ( 'jetpack_url_mismatch' === $error->get_error_code() ) { |
||
| 150 | Jetpack_Options::update_option( 'sync_error_idc', get_home_url() ); |
||
| 151 | } |
||
| 152 | |||
| 153 | return $error; |
||
| 154 | } |
||
| 155 | |||
| 156 | return $rpc->getResponse(); |
||
| 157 | } |
||
| 158 | |||
| 159 | static function do_initial_sync( $new_version = null, $old_version = null ) { |
||
| 160 | $initial_sync_config = array( |
||
| 161 | 'options' => true, |
||
| 162 | 'network_options' => true, |
||
| 163 | 'functions' => true, |
||
| 164 | 'constants' => true, |
||
| 165 | ); |
||
| 166 | |||
| 167 | if ( $old_version && ( version_compare( $old_version, '4.2', '<' ) ) ) { |
||
| 168 | $initial_sync_config['users'] = 'initial'; |
||
| 169 | } |
||
| 170 | |||
| 171 | self::do_full_sync( $initial_sync_config ); |
||
| 172 | } |
||
| 173 | |||
| 174 | static function do_full_sync( $modules = null ) { |
||
| 175 | if ( ! self::sync_allowed() ) { |
||
| 176 | return false; |
||
| 177 | } |
||
| 178 | |||
| 179 | self::initialize_listener(); |
||
| 180 | Jetpack_Sync_Modules::get_module( 'full-sync' )->start( $modules ); |
||
| 181 | self::do_cron_full_sync(); // immediately run a cron full sync, which sends pending data |
||
| 182 | |||
| 183 | return true; |
||
| 184 | } |
||
| 185 | |||
| 186 | static function minute_cron_schedule( $schedules ) { |
||
| 195 | |||
| 196 | // try to send actions until we run out of things to send, |
||
| 197 | // or have to wait more than 15s before sending again, |
||
| 198 | // or we hit a lock or some other sending issue |
||
| 199 | View Code Duplication | static function do_cron_sync() { |
|
| 200 | if ( ! self::sync_allowed() ) { |
||
| 201 | return; |
||
| 202 | } |
||
| 203 | |||
| 204 | self::initialize_sender(); |
||
| 205 | |||
| 206 | // remove shutdown hook - no need to sync twice |
||
| 207 | if ( has_action( 'shutdown', array( self::$sender, 'do_sync' ) ) ) { |
||
| 208 | remove_action( 'shutdown', array( self::$sender, 'do_sync' ) ); |
||
| 209 | } |
||
| 210 | |||
| 211 | do { |
||
| 212 | $next_sync_time = self::$sender->get_next_sync_time( 'sync' ); |
||
| 213 | |||
| 214 | if ( $next_sync_time ) { |
||
| 215 | $delay = $next_sync_time - time() + 1; |
||
| 216 | if ( $delay > 15 ) { |
||
| 217 | break; |
||
| 218 | } elseif ( $delay > 0 ) { |
||
| 219 | sleep( $delay ); |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | $result = self::$sender->do_sync(); |
||
| 224 | } while ( $result ); |
||
| 225 | } |
||
| 226 | |||
| 227 | View Code Duplication | static function do_cron_full_sync() { |
|
| 228 | if ( ! self::sync_allowed() ) { |
||
| 229 | return; |
||
| 230 | } |
||
| 231 | |||
| 232 | self::initialize_sender(); |
||
| 233 | |||
| 234 | // remove shutdown hook - no need to sync twice |
||
| 235 | if ( has_action( 'shutdown', array( self::$sender, 'do_sync' ) ) ) { |
||
| 236 | remove_action( 'shutdown', array( self::$sender, 'do_sync' ) ); |
||
| 237 | } |
||
| 238 | |||
| 239 | do { |
||
| 240 | $next_sync_time = self::$sender->get_next_sync_time( 'full_sync' ); |
||
| 241 | |||
| 242 | if ( $next_sync_time ) { |
||
| 243 | $delay = $next_sync_time - time() + 1; |
||
| 244 | if ( $delay > 15 ) { |
||
| 245 | break; |
||
| 246 | } elseif ( $delay > 0 ) { |
||
| 247 | sleep( $delay ); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | $result = self::$sender->do_full_sync(); |
||
| 252 | } while ( $result ); |
||
| 253 | } |
||
| 254 | |||
| 255 | static function initialize_listener() { |
||
| 259 | |||
| 260 | static function initialize_sender() { |
||
| 267 | |||
| 268 | static function sanitize_filtered_sync_cron_schedule( $schedule ) { |
||
| 269 | $schedule = sanitize_key( $schedule ); |
||
| 270 | $schedules = wp_get_schedules(); |
||
| 271 | |||
| 272 | // Make sure that the schedule has actually been registered using the `cron_intervals` filter. |
||
| 273 | if ( isset( $schedules[ $schedule ] ) ) { |
||
| 274 | return $schedule; |
||
| 275 | } |
||
| 276 | |||
| 279 | |||
| 280 | static function maybe_schedule_sync_cron( $schedule, $hook ) { |
||
| 295 | |||
| 296 | static function init_sync_cron_jobs() { |
||
| 317 | } |
||
| 318 | |||
| 334 |
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.