Complex classes like Jetpack_Sync_Full 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_Full, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Jetpack_Sync_Full { |
||
| 18 | const ARRAY_CHUNK_SIZE = 10; |
||
| 19 | static $status_option = 'jetpack_full_sync_status'; |
||
|
|
|||
| 20 | static $transient_timeout = 3600; // an hour |
||
| 21 | static $modules = array( |
||
| 22 | 'wp_version', |
||
| 23 | 'constants', |
||
| 24 | 'functions', |
||
| 25 | 'options', |
||
| 26 | 'posts', |
||
| 27 | 'comments', |
||
| 28 | 'themes', |
||
| 29 | 'updates', |
||
| 30 | 'users', |
||
| 31 | 'terms', |
||
| 32 | 'network_options', |
||
| 33 | ); |
||
| 34 | |||
| 35 | // singleton functions |
||
| 36 | private static $instance; |
||
| 37 | private $client; |
||
| 38 | |||
| 39 | public static function getInstance() { |
||
| 46 | |||
| 47 | protected function __construct() { |
||
| 50 | |||
| 51 | function init() { |
||
| 52 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_posts', array( $this, 'expand_post_ids' ) ); |
||
| 53 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_comments', array( $this, 'expand_comment_ids' ) ); |
||
| 54 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_options', array( $this, 'expand_options' ) ); |
||
| 55 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_constants', array( $this, 'expand_constants' ) ); |
||
| 56 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_callables', array( $this, 'expand_callables' ) ); |
||
| 57 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_users', array( $this, 'expand_users' ) ); |
||
| 58 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_updates', array( $this, 'expand_updates' ) ); |
||
| 59 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_network_options', array( |
||
| 60 | $this, |
||
| 61 | 'expand_network_options' |
||
| 62 | ) ); |
||
| 63 | |||
| 64 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_terms', array( $this, 'expand_term_ids' ) ); |
||
| 65 | |||
| 66 | add_action( 'jetpack_sync_processed_actions', array( $this, 'update_sent_progress_action' ) ); |
||
| 67 | } |
||
| 68 | |||
| 69 | function start() { |
||
| 70 | if( ! $this->should_start_full_sync() ) { |
||
| 71 | return; |
||
| 72 | } |
||
| 73 | /** |
||
| 74 | * Fires when a full sync begins. This action is serialized |
||
| 75 | * and sent to the server so that it can clear the replica storage, |
||
| 76 | * and/or reset other data. |
||
| 77 | * |
||
| 78 | * @since 4.1 |
||
| 79 | */ |
||
| 80 | do_action( 'jetpack_full_sync_start' ); |
||
| 81 | $this->set_status_queuing_started(); |
||
| 82 | |||
| 83 | $this->enqueue_all_constants(); |
||
| 84 | $this->enqueue_all_functions(); |
||
| 85 | $this->enqueue_all_options(); |
||
| 86 | |||
| 87 | if ( is_multisite() ) { |
||
| 88 | $this->enqueue_all_network_options(); |
||
| 89 | } |
||
| 90 | |||
| 91 | $this->enqueue_all_terms(); |
||
| 92 | $this->enqueue_all_theme_info(); |
||
| 93 | $this->enqueue_all_users(); |
||
| 94 | $this->enqueue_all_posts(); |
||
| 95 | $this->enqueue_all_comments(); |
||
| 96 | $this->enqueue_all_updates(); |
||
| 97 | |||
| 98 | $this->set_status_queuing_finished(); |
||
| 99 | |||
| 100 | $store = new Jetpack_Sync_WP_Replicastore(); |
||
| 101 | do_action( 'jetpack_full_sync_end', $store->checksum_all() ); |
||
| 102 | } |
||
| 103 | |||
| 104 | private function should_start_full_sync() { |
||
| 105 | $status = $this->get_status(); |
||
| 106 | // We should try sync if we haven't started it yet or if we have finished it. |
||
| 107 | if( is_null( $status['started'] ) || is_integer( $status['finished'] ) ) { |
||
| 108 | return true; |
||
| 109 | } |
||
| 110 | return false; |
||
| 111 | } |
||
| 112 | |||
| 113 | private function get_client() { |
||
| 114 | if ( ! $this->client ) { |
||
| 115 | $this->client = Jetpack_Sync_Client::getInstance(); |
||
| 116 | } |
||
| 117 | |||
| 118 | return $this->client; |
||
| 119 | } |
||
| 120 | |||
| 121 | private function enqueue_all_constants() { |
||
| 122 | $total = $this->get_client()->full_sync_constants(); |
||
| 123 | $this->update_queue_progress( 'constants', $total ); |
||
| 124 | } |
||
| 125 | |||
| 126 | private function enqueue_all_functions() { |
||
| 127 | $total = $this->get_client()->full_sync_callables(); |
||
| 128 | $this->update_queue_progress( 'functions', $total ); |
||
| 129 | } |
||
| 130 | |||
| 131 | private function enqueue_all_options() { |
||
| 132 | $total = $this->get_client()->force_sync_options(); |
||
| 133 | $this->update_queue_progress( 'options', $total ); |
||
| 134 | } |
||
| 135 | |||
| 136 | private function enqueue_all_network_options() { |
||
| 137 | $total = $this->get_client()->force_sync_network_options(); |
||
| 138 | $this->update_queue_progress( 'network_options', $total ); |
||
| 139 | } |
||
| 140 | |||
| 141 | private function enqueue_all_terms() { |
||
| 142 | global $wpdb; |
||
| 143 | |||
| 144 | $taxonomies = get_taxonomies(); |
||
| 145 | $total_chunks_counter = 0; |
||
| 146 | foreach ( $taxonomies as $taxonomy ) { |
||
| 147 | // I hope this is never bigger than RAM... |
||
| 148 | $term_ids = $wpdb->get_col( $wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s", $taxonomy ) ); // Should we set a limit here? |
||
| 149 | // Request posts in groups of N for efficiency |
||
| 150 | $chunked_term_ids = array_chunk( $term_ids, self::ARRAY_CHUNK_SIZE ); |
||
| 151 | |||
| 152 | // Send each chunk as an array of objects |
||
| 153 | foreach ( $chunked_term_ids as $chunk ) { |
||
| 154 | do_action( 'jetpack_full_sync_terms', $chunk, $taxonomy ); |
||
| 155 | $total_chunks_counter++; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | $this->update_queue_progress( 'terms', $total_chunks_counter ); |
||
| 160 | |||
| 161 | } |
||
| 162 | |||
| 163 | private function enqueue_all_posts() { |
||
| 164 | global $wpdb; |
||
| 165 | |||
| 166 | $post_type_sql = Jetpack_Sync_Defaults::get_blacklisted_post_types_sql(); |
||
| 167 | $total = $this->enqueue_all_ids_as_action( 'jetpack_full_sync_posts', $wpdb->posts, 'ID', $post_type_sql ); |
||
| 168 | $this->update_queue_progress( 'posts', $total ); |
||
| 169 | |||
| 170 | } |
||
| 171 | |||
| 172 | private function enqueue_all_ids_as_action( $action_name, $table_name, $id_field, $where_sql ) { |
||
| 173 | global $wpdb; |
||
| 174 | |||
| 175 | if ( ! $where_sql ) { |
||
| 176 | $where_sql = "1 = 1"; |
||
| 177 | } |
||
| 178 | |||
| 179 | $items_per_page = 500; |
||
| 180 | $page = 1; |
||
| 181 | $offset = ( $page * $items_per_page ) - $items_per_page; |
||
| 182 | $chunk_count = 0; |
||
| 183 | while( $ids = $wpdb->get_col( "SELECT {$id_field} FROM {$table_name} WHERE {$where_sql} ORDER BY {$id_field} asc LIMIT {$offset}, {$items_per_page}" ) ) { |
||
| 184 | // Request posts in groups of N for efficiency |
||
| 185 | $chunked_ids = array_chunk( $ids, self::ARRAY_CHUNK_SIZE ); |
||
| 186 | |||
| 187 | // Send each chunk as an array of objects |
||
| 188 | foreach ( $chunked_ids as $chunk ) { |
||
| 189 | /** |
||
| 190 | * Fires with a chunk of object IDs during full sync. |
||
| 191 | * These are expanded to full objects before upload |
||
| 192 | * |
||
| 193 | * @since 4.1 |
||
| 194 | */ |
||
| 195 | do_action( $action_name, $chunk ); |
||
| 196 | $chunk_count++; |
||
| 197 | } |
||
| 198 | |||
| 199 | $page += 1; |
||
| 200 | $offset = ( $page * $items_per_page ) - $items_per_page; |
||
| 201 | } |
||
| 202 | return $chunk_count; |
||
| 203 | } |
||
| 204 | |||
| 205 | public function expand_post_ids( $args ) { |
||
| 206 | $post_ids = $args[0]; |
||
| 207 | |||
| 208 | $posts = array_map( array( 'WP_Post', 'get_instance' ), $post_ids ); |
||
| 209 | $posts = array_map( array( $this->get_client(), 'filter_post_content_and_add_links' ), $posts ); |
||
| 210 | |||
| 211 | return array( |
||
| 212 | 'posts' => $posts, |
||
| 213 | 'post_metas' => $this->get_metadata( $post_ids, 'post' ), |
||
| 214 | 'terms' => $this->get_term_relationships( $post_ids ) |
||
| 215 | ); |
||
| 216 | } |
||
| 217 | |||
| 218 | private function enqueue_all_comments() { |
||
| 219 | global $wpdb; |
||
| 220 | |||
| 221 | $total = $this->enqueue_all_ids_as_action( 'jetpack_full_sync_comments', $wpdb->comments, 'comment_ID', null ); |
||
| 222 | $this->update_queue_progress( 'comments', $total ); |
||
| 223 | } |
||
| 224 | |||
| 225 | public function expand_comment_ids( $args ) { |
||
| 226 | $comment_ids = $args[0]; |
||
| 227 | $comments = get_comments( array( |
||
| 228 | 'include_unapproved' => true, |
||
| 229 | 'comment__in' => $comment_ids, |
||
| 230 | ) ); |
||
| 231 | $comments = array_map( array( $this->get_client(), 'filter_comment_and_add_hc_meta' ), $comments ); |
||
| 232 | |||
| 233 | return array( |
||
| 234 | 'comments' => $comments, |
||
| 235 | 'comment_metas' => $this->get_metadata( $comment_ids, 'comment' ), |
||
| 236 | ); |
||
| 237 | } |
||
| 238 | |||
| 239 | public function expand_term_ids( $args ) { |
||
| 240 | global $wp_version; |
||
| 241 | $term_ids = $args[0]; |
||
| 242 | $taxonomy = $args[1]; |
||
| 243 | // version 4.5 or higher |
||
| 244 | if ( version_compare( $wp_version, 4.5, '>=' ) ) { |
||
| 245 | $terms = get_terms( array( |
||
| 246 | 'taxonomy' => $taxonomy, |
||
| 247 | 'hide_empty' => false, |
||
| 248 | 'include' => $term_ids |
||
| 249 | ) ); |
||
| 250 | } else { |
||
| 251 | $terms = get_terms( $taxonomy, array( |
||
| 252 | 'hide_empty' => false, |
||
| 253 | 'include' => $term_ids |
||
| 254 | ) ); |
||
| 255 | } |
||
| 256 | |||
| 257 | return $terms; |
||
| 258 | } |
||
| 259 | |||
| 260 | public function expand_options( $args ) { |
||
| 261 | if ( $args[0] ) { |
||
| 262 | return $this->get_client()->get_all_options(); |
||
| 263 | } |
||
| 264 | |||
| 265 | return $args; |
||
| 266 | } |
||
| 267 | |||
| 268 | public function expand_constants( $args ) { |
||
| 274 | |||
| 275 | public function expand_callables( $args ) { |
||
| 281 | |||
| 282 | private function enqueue_all_users() { |
||
| 283 | global $wpdb; |
||
| 284 | $total = $this->enqueue_all_ids_as_action( 'jetpack_full_sync_users', $wpdb->users, 'ID', null ); |
||
| 285 | $this->update_queue_progress( 'users', $total ); |
||
| 286 | } |
||
| 287 | |||
| 288 | public function expand_users( $args ) { |
||
| 289 | $user_ids = $args[0]; |
||
| 290 | return array_map( array( $this->get_client(), 'sanitize_user' ), get_users( array( 'include' => $user_ids ) ) ); |
||
| 291 | } |
||
| 292 | |||
| 293 | public function expand_network_options( $args ) { |
||
| 294 | if ( $args[0] ) { |
||
| 295 | return $this->get_client()->get_all_network_options(); |
||
| 296 | } |
||
| 297 | |||
| 298 | return $args; |
||
| 299 | } |
||
| 300 | |||
| 301 | private function get_metadata( $ids, $meta_type ) { |
||
| 302 | global $wpdb; |
||
| 303 | $table = _get_meta_table( $meta_type ); |
||
| 304 | $id = $meta_type . '_id'; |
||
| 305 | if ( ! $table ) { |
||
| 306 | return array(); |
||
| 307 | } |
||
| 308 | |||
| 309 | return $wpdb->get_results( "SELECT $id, meta_key, meta_value, meta_id FROM $table WHERE $id IN ( " . implode( ',', wp_parse_id_list( $ids ) ) . " )", OBJECT ); |
||
| 310 | } |
||
| 311 | |||
| 312 | private function get_term_relationships( $ids ) { |
||
| 313 | global $wpdb; |
||
| 314 | |||
| 315 | return $wpdb->get_results( "SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id IN ( " . implode( ',', wp_parse_id_list( $ids ) ) . " )", OBJECT ); |
||
| 316 | } |
||
| 317 | |||
| 318 | // TODO: |
||
| 319 | private function enqueue_all_theme_info() { |
||
| 320 | $total = $this->get_client()->send_theme_info(); |
||
| 321 | $this->update_queue_progress( 'themes', $total ); |
||
| 322 | } |
||
| 323 | |||
| 324 | private function enqueue_all_updates() { |
||
| 325 | |||
| 326 | // check for updates |
||
| 327 | $total = $this->get_client()->full_sync_updates(); |
||
| 328 | $this->update_queue_progress( 'updates', $total ); |
||
| 329 | } |
||
| 330 | |||
| 331 | public function expand_updates( $args ) { |
||
| 338 | |||
| 339 | function update_sent_progress_action( $actions_sent ) { |
||
| 369 | |||
| 370 | function action_to_modules( $action ) { |
||
| 415 | |||
| 416 | private function set_status_queuing_started() { |
||
| 417 | $status = $this->initial_status; |
||
| 418 | $status[ 'started' ] = time(); |
||
| 419 | $this->update_status( $status ); |
||
| 420 | } |
||
| 421 | |||
| 422 | private function set_status_queuing_finished() { |
||
| 423 | $this->update_status( array( 'queue_finished' => time() ) ); |
||
| 424 | } |
||
| 425 | |||
| 426 | // these are called by the Sync Client when it sees that the full sync start/end actions have actually been transmitted |
||
| 427 | public function set_status_sending_started() { |
||
| 428 | /** |
||
| 429 | * Fires when the full_sync_start action is actually transmitted. |
||
| 430 | * This is useful for telling the user the status of full synchronization. |
||
| 431 | * |
||
| 432 | * @since 4.1 |
||
| 433 | */ |
||
| 434 | |||
| 435 | do_action( 'jetpack_full_sync_start_sent' ); |
||
| 436 | |||
| 437 | } |
||
| 438 | |||
| 439 | public function set_status_sending_finished() { |
||
| 440 | /** |
||
| 441 | * Fires when the full_sync_end action is actually transmitted. |
||
| 442 | * This is useful for telling the user the status of full synchronization. |
||
| 443 | * |
||
| 444 | * @since 4.1 |
||
| 445 | */ |
||
| 446 | do_action( 'jetpack_full_sync_end_sent' ); |
||
| 447 | } |
||
| 448 | |||
| 449 | private $initial_status = array( |
||
| 450 | 'started' => null, |
||
| 451 | 'queue_finished' => null, |
||
| 452 | 'sent_started' => null, |
||
| 453 | 'finished' => null, |
||
| 454 | 'sent' => array(), |
||
| 455 | 'queue' => array(), |
||
| 456 | ); |
||
| 457 | |||
| 458 | public function get_status() { |
||
| 459 | return get_option( self::$status_option, $this->initial_status ); |
||
| 460 | } |
||
| 461 | |||
| 462 | |||
| 463 | public function update_status( $status ) { |
||
| 469 | |||
| 470 | private function clear_status() { |
||
| 473 | |||
| 474 | public function update_queue_progress( $module, $data ) { |
||
| 484 | |||
| 485 | public function update_sent_progress( $module, $data ) { |
||
| 493 | |||
| 494 | } |
||
| 495 |
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.