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 |
||
| 10 | class Jetpack_Sync_Actions { |
||
| 11 | static $sender = null; |
||
|
|
|||
| 12 | static $listener = null; |
||
| 13 | const INITIAL_SYNC_MULTISITE_INTERVAL = 10; |
||
| 14 | const DEFAULT_SYNC_CRON_INTERVAL = '1min'; |
||
| 15 | |||
| 16 | static function init() { |
||
| 17 | |||
| 18 | // Add a custom "every minute" cron schedule |
||
| 19 | add_filter( 'cron_schedules', array( __CLASS__, 'minute_cron_schedule' ) ); |
||
| 20 | |||
| 21 | // On jetpack authorization, schedule a full sync |
||
| 22 | add_action( 'jetpack_client_authorized', array( __CLASS__, 'schedule_full_sync' ), 10, 0 ); |
||
| 23 | |||
| 24 | // When importing via cron, do not sync |
||
| 25 | add_action( 'wp_cron_importer_hook', array( __CLASS__, 'set_is_importing_true' ), 1 ); |
||
| 26 | |||
| 27 | // Sync connected user role changes to .com |
||
| 28 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-users.php'; |
||
| 29 | |||
| 30 | // everything below this point should only happen if we're a valid sync site |
||
| 31 | if ( ! self::sync_allowed() ) { |
||
| 32 | return; |
||
| 33 | } |
||
| 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 | // cron hooks |
||
| 39 | add_action( 'jetpack_sync_full', array( __CLASS__, 'do_full_sync' ), 10, 1 ); |
||
| 40 | add_action( 'jetpack_sync_cron', array( __CLASS__, 'do_cron_sync' ) ); |
||
| 41 | add_action( 'jetpack_sync_full_cron', array( __CLASS__, 'do_cron_full_sync' ) ); |
||
| 42 | |||
| 43 | self::init_sync_cron_jobs(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Fires on every request before default loading sync listener code. |
||
| 47 | * Return false to not load sync listener code that monitors common |
||
| 48 | * WP actions to be serialized. |
||
| 49 | * |
||
| 50 | * By default this returns true for cron jobs, non-GET-requests, or requests where the |
||
| 51 | * user is logged-in. |
||
| 52 | * |
||
| 53 | * @since 4.2.0 |
||
| 54 | * |
||
| 55 | * @param bool should we load sync listener code for this request |
||
| 56 | */ |
||
| 57 | if ( apply_filters( 'jetpack_sync_listener_should_load', |
||
| 58 | ( |
||
| 59 | ( isset( $_SERVER["REQUEST_METHOD"] ) && 'GET' !== $_SERVER['REQUEST_METHOD'] ) |
||
| 60 | || |
||
| 61 | is_user_logged_in() |
||
| 62 | || |
||
| 63 | defined( 'PHPUNIT_JETPACK_TESTSUITE' ) |
||
| 64 | || |
||
| 65 | defined( 'DOING_CRON' ) && DOING_CRON |
||
| 66 | ) |
||
| 67 | ) ) { |
||
| 68 | self::initialize_listener(); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Fires on every request before default loading sync sender code. |
||
| 73 | * Return false to not load sync sender code that serializes pending |
||
| 74 | * data and sends it to WPCOM for processing. |
||
| 75 | * |
||
| 76 | * By default this returns true for cron jobs, POST requests, admin requests, or requests |
||
| 77 | * by users who can manage_options. |
||
| 78 | * |
||
| 79 | * @since 4.2.0 |
||
| 80 | * |
||
| 81 | * @param bool should we load sync sender code for this request |
||
| 82 | */ |
||
| 83 | if ( apply_filters( 'jetpack_sync_sender_should_load', |
||
| 84 | ( |
||
| 85 | ( isset( $_SERVER["REQUEST_METHOD"] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) |
||
| 86 | || |
||
| 87 | current_user_can( 'manage_options' ) |
||
| 88 | || |
||
| 89 | is_admin() |
||
| 90 | || |
||
| 91 | defined( 'PHPUNIT_JETPACK_TESTSUITE' ) |
||
| 92 | || |
||
| 93 | defined( 'DOING_CRON' ) && DOING_CRON |
||
| 94 | ) |
||
| 95 | ) ) { |
||
| 96 | self::initialize_sender(); |
||
| 97 | add_action( 'shutdown', array( self::$sender, 'do_sync' ) ); |
||
| 98 | } |
||
| 99 | |||
| 100 | } |
||
| 101 | |||
| 102 | static function sync_allowed() { |
||
| 103 | return ( ! Jetpack_Sync_Settings::get_setting( 'disable' ) && Jetpack::is_active() && ! ( Jetpack::is_development_mode() || Jetpack::is_staging_site() ) ) |
||
| 104 | || defined( 'PHPUNIT_JETPACK_TESTSUITE' ); |
||
| 105 | } |
||
| 106 | |||
| 107 | static function prevent_publicize_blacklisted_posts( $should_publicize, $post ) { |
||
| 108 | if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) { |
||
| 109 | return false; |
||
| 110 | } |
||
| 111 | |||
| 112 | return $should_publicize; |
||
| 113 | } |
||
| 114 | |||
| 115 | static function set_is_importing_true() { |
||
| 118 | |||
| 119 | static function send_data( $data, $codec_name, $sent_timestamp, $queue_id ) { |
||
| 143 | |||
| 144 | static function schedule_initial_sync( $new_version = null, $old_version = null ) { |
||
| 145 | $initial_sync_config = array( |
||
| 146 | 'options' => true, |
||
| 147 | 'network_options' => true, |
||
| 148 | 'functions' => true, |
||
| 149 | 'constants' => true, |
||
| 150 | ); |
||
| 151 | |||
| 152 | if ( $old_version && ( version_compare( $old_version, '4.2', '<' ) ) ) { |
||
| 153 | $initial_sync_config['users'] = 'initial'; |
||
| 154 | } |
||
| 155 | |||
| 156 | // we need this function call here because we have to run this function |
||
| 157 | // reeeeally early in init, before WP_CRON_LOCK_TIMEOUT is defined. |
||
| 158 | wp_functionality_constants(); |
||
| 159 | |||
| 160 | if ( is_multisite() ) { |
||
| 161 | // stagger initial syncs for multisite blogs so they don't all pile on top of each other |
||
| 162 | $time_offset = ( rand() / getrandmax() ) * self::INITIAL_SYNC_MULTISITE_INTERVAL * get_blog_count(); |
||
| 163 | } else { |
||
| 164 | $time_offset = 1; |
||
| 165 | } |
||
| 166 | |||
| 167 | self::schedule_full_sync( |
||
| 168 | $initial_sync_config, |
||
| 169 | $time_offset |
||
| 170 | ); |
||
| 171 | } |
||
| 172 | |||
| 173 | static function schedule_full_sync( $modules = null, $time_offset = 1 ) { |
||
| 174 | if ( ! self::sync_allowed() ) { |
||
| 175 | return false; |
||
| 176 | } |
||
| 177 | |||
| 178 | if ( self::is_scheduled_full_sync() ) { |
||
| 179 | self::unschedule_all_full_syncs(); |
||
| 180 | } |
||
| 181 | |||
| 182 | if ( $modules ) { |
||
| 183 | wp_schedule_single_event( time() + $time_offset, 'jetpack_sync_full', array( $modules ) ); |
||
| 184 | } else { |
||
| 185 | wp_schedule_single_event( time() + $time_offset, 'jetpack_sync_full' ); |
||
| 186 | } |
||
| 187 | |||
| 188 | if ( $time_offset === 1 ) { |
||
| 189 | spawn_cron(); |
||
| 190 | } |
||
| 191 | |||
| 192 | return true; |
||
| 193 | } |
||
| 194 | |||
| 195 | static function unschedule_all_full_syncs() { |
||
| 196 | foreach ( _get_cron_array() as $timestamp => $cron ) { |
||
| 197 | if ( ! empty( $cron['jetpack_sync_full'] ) ) { |
||
| 198 | foreach( $cron['jetpack_sync_full'] as $key => $config ) { |
||
| 199 | wp_unschedule_event( $timestamp, 'jetpack_sync_full', $config['args'] ); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | static function is_scheduled_full_sync( $modules = null ) { |
||
| 206 | if ( is_null( $modules ) ) { |
||
| 207 | $crons = _get_cron_array(); |
||
| 208 | |||
| 209 | foreach ( $crons as $timestamp => $cron ) { |
||
| 210 | if ( ! empty( $cron['jetpack_sync_full'] ) ) { |
||
| 211 | return true; |
||
| 212 | } |
||
| 213 | } |
||
| 214 | return false; |
||
| 215 | } |
||
| 216 | |||
| 217 | return !! wp_next_scheduled( 'jetpack_sync_full', array( $modules ) ); |
||
| 218 | } |
||
| 219 | |||
| 220 | static function do_full_sync( $modules = null ) { |
||
| 221 | if ( ! self::sync_allowed() ) { |
||
| 222 | return; |
||
| 223 | } |
||
| 224 | |||
| 225 | self::initialize_listener(); |
||
| 226 | Jetpack_Sync_Modules::get_module( 'full-sync' )->start( $modules ); |
||
| 227 | self::do_cron_full_sync(); // immediately run a cron full sync, which sends pending data |
||
| 228 | } |
||
| 229 | |||
| 230 | static function minute_cron_schedule( $schedules ) { |
||
| 239 | |||
| 240 | // try to send actions until we run out of things to send, |
||
| 241 | // or have to wait more than 15s before sending again, |
||
| 242 | // or we hit a lock or some other sending issue |
||
| 243 | View Code Duplication | static function do_cron_sync() { |
|
| 244 | if ( ! self::sync_allowed() ) { |
||
| 245 | return; |
||
| 270 | |||
| 271 | View Code Duplication | static function do_cron_full_sync() { |
|
| 298 | |||
| 299 | static function initialize_listener() { |
||
| 303 | |||
| 304 | static function initialize_sender() { |
||
| 311 | |||
| 312 | static function sanitize_filtered_sync_cron_schedule( $schedule ) { |
||
| 323 | |||
| 324 | static function maybe_schedule_sync_cron( $schedule, $hook ) { |
||
| 339 | |||
| 340 | static function init_sync_cron_jobs() { |
||
| 361 | } |
||
| 362 | |||
| 378 |
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.