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_transient_name = 'jetpack_full_sync_progress'; |
||
|
|
|||
| 20 | static $status_option = 'jetpack_full_sync_status'; |
||
| 21 | static $transient_timeout = 3600; // an hour |
||
| 22 | static $modules = array( |
||
| 23 | 'wp_version', |
||
| 24 | 'constants', |
||
| 25 | 'functions', |
||
| 26 | 'options', |
||
| 27 | 'posts', |
||
| 28 | 'comments', |
||
| 29 | 'themes', |
||
| 30 | 'updates', |
||
| 31 | 'users', |
||
| 32 | 'terms', |
||
| 33 | 'network_options', |
||
| 34 | ); |
||
| 35 | |||
| 36 | // singleton functions |
||
| 37 | private static $instance; |
||
| 38 | private $client; |
||
| 39 | |||
| 40 | public static function getInstance() { |
||
| 41 | if ( null === self::$instance ) { |
||
| 42 | self::$instance = new self(); |
||
| 43 | } |
||
| 44 | |||
| 45 | return self::$instance; |
||
| 46 | } |
||
| 47 | |||
| 48 | protected function __construct() { |
||
| 49 | $this->init(); |
||
| 50 | } |
||
| 51 | |||
| 52 | function init() { |
||
| 53 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_posts', array( $this, 'expand_post_ids' ) ); |
||
| 54 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_comments', array( $this, 'expand_comment_ids' ) ); |
||
| 55 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_options', array( $this, 'expand_options' ) ); |
||
| 56 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_constants', array( $this, 'expand_constants' ) ); |
||
| 57 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_callables', array( $this, 'expand_callables' ) ); |
||
| 58 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_users', array( $this, 'expand_users' ) ); |
||
| 59 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_updates', array( $this, 'expand_updates' ) ); |
||
| 60 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_network_options', array( |
||
| 61 | $this, |
||
| 62 | 'expand_network_options' |
||
| 63 | ) ); |
||
| 64 | |||
| 65 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_terms', array( $this, 'expand_term_ids' ) ); |
||
| 66 | |||
| 67 | add_action( 'jetpack_sync_actions_to_send', array( $this, 'update_sent_progress_action' ) ); |
||
| 68 | } |
||
| 69 | |||
| 70 | function start() { |
||
| 71 | if( ! $this->should_start_full_sync() ) { |
||
| 72 | return; |
||
| 73 | } |
||
| 74 | /** |
||
| 75 | * Fires when a full sync begins. This action is serialized |
||
| 76 | * and sent to the server so that it can clear the replica storage, |
||
| 77 | * and/or reset other data. |
||
| 78 | * |
||
| 79 | * @since 4.1 |
||
| 80 | */ |
||
| 81 | do_action( 'jetpack_full_sync_start' ); |
||
| 82 | $this->set_status_queuing_started(); |
||
| 83 | |||
| 84 | $this->enqueue_all_constants(); |
||
| 85 | $this->enqueue_all_functions(); |
||
| 86 | $this->enqueue_all_options(); |
||
| 87 | |||
| 88 | if ( is_multisite() ) { |
||
| 89 | $this->enqueue_all_network_options(); |
||
| 90 | } |
||
| 91 | |||
| 92 | $this->enqueue_all_terms(); |
||
| 93 | $this->enqueue_all_theme_info(); |
||
| 94 | $this->enqueue_all_users(); |
||
| 95 | $this->enqueue_all_posts(); |
||
| 96 | $this->enqueue_all_comments(); |
||
| 97 | $this->enqueue_all_updates(); |
||
| 98 | |||
| 99 | $this->set_status_queuing_finished(); |
||
| 100 | |||
| 101 | $store = new Jetpack_Sync_WP_Replicastore(); |
||
| 102 | do_action( 'jetpack_full_sync_end', $store->checksum_all() ); |
||
| 103 | } |
||
| 104 | |||
| 105 | private function should_start_full_sync() { |
||
| 106 | $status = $this->get_status(); |
||
| 107 | // We should try sync if we haven't started it yet or if we have finished it. |
||
| 108 | if( is_null( $status['started'] ) || is_integer( $status['finished'] ) ) { |
||
| 109 | return true; |
||
| 110 | } |
||
| 111 | return false; |
||
| 112 | } |
||
| 113 | |||
| 114 | private function get_client() { |
||
| 115 | if ( ! $this->client ) { |
||
| 116 | $this->client = Jetpack_Sync_Client::getInstance(); |
||
| 117 | } |
||
| 118 | |||
| 119 | return $this->client; |
||
| 120 | } |
||
| 121 | |||
| 122 | private function enqueue_all_constants() { |
||
| 123 | $total = $this->get_client()->full_sync_constants(); |
||
| 124 | $this->update_queue_progress( 'constants', $total ); |
||
| 125 | } |
||
| 126 | |||
| 127 | private function enqueue_all_functions() { |
||
| 128 | $total = $this->get_client()->full_sync_callables(); |
||
| 129 | $this->update_queue_progress( 'functions', $total ); |
||
| 130 | } |
||
| 131 | |||
| 132 | private function enqueue_all_options() { |
||
| 133 | $total = $this->get_client()->force_sync_options(); |
||
| 134 | $this->update_queue_progress( 'options', $total ); |
||
| 135 | } |
||
| 136 | |||
| 137 | private function enqueue_all_network_options() { |
||
| 138 | $total = $this->get_client()->force_sync_network_options(); |
||
| 139 | $this->update_queue_progress( 'network_options', $total ); |
||
| 140 | } |
||
| 141 | |||
| 142 | private function enqueue_all_terms() { |
||
| 143 | global $wpdb; |
||
| 144 | |||
| 145 | $taxonomies = get_taxonomies(); |
||
| 146 | $total_chunks_counter = 0; |
||
| 147 | foreach ( $taxonomies as $taxonomy ) { |
||
| 148 | // I hope this is never bigger than RAM... |
||
| 149 | $term_ids = $wpdb->get_col( $wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s", $taxonomy ) ); // Should we set a limit here? |
||
| 150 | // Request posts in groups of N for efficiency |
||
| 151 | $chunked_term_ids = array_chunk( $term_ids, self::ARRAY_CHUNK_SIZE ); |
||
| 152 | |||
| 153 | // Send each chunk as an array of objects |
||
| 154 | foreach ( $chunked_term_ids as $chunk ) { |
||
| 155 | do_action( 'jetpack_full_sync_terms', $chunk, $taxonomy ); |
||
| 156 | $total_chunks_counter++; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | $this->update_queue_progress( 'terms', $total_chunks_counter ); |
||
| 161 | |||
| 162 | } |
||
| 163 | |||
| 164 | private function enqueue_all_posts() { |
||
| 165 | global $wpdb; |
||
| 166 | |||
| 167 | $post_type_sql = Jetpack_Sync_Defaults::get_blacklisted_post_types_sql(); |
||
| 168 | $total = $this->enqueue_all_ids_as_action( 'jetpack_full_sync_posts', $wpdb->posts, 'ID', $post_type_sql ); |
||
| 169 | $this->update_queue_progress( 'posts', $total ); |
||
| 170 | |||
| 171 | } |
||
| 172 | |||
| 173 | private function enqueue_all_ids_as_action( $action_name, $table_name, $id_field, $where_sql ) { |
||
| 174 | global $wpdb; |
||
| 175 | |||
| 176 | if ( ! $where_sql ) { |
||
| 177 | $where_sql = "1 = 1"; |
||
| 178 | } |
||
| 179 | |||
| 180 | $items_per_page = 500; |
||
| 181 | $page = 1; |
||
| 182 | $offset = ( $page * $items_per_page ) - $items_per_page; |
||
| 183 | $chunk_count = 0; |
||
| 184 | while( $ids = $wpdb->get_col( "SELECT {$id_field} FROM {$table_name} WHERE {$where_sql} ORDER BY {$id_field} asc LIMIT {$offset}, {$items_per_page}" ) ) { |
||
| 185 | // Request posts in groups of N for efficiency |
||
| 186 | $chunked_ids = array_chunk( $ids, self::ARRAY_CHUNK_SIZE ); |
||
| 187 | |||
| 188 | // Send each chunk as an array of objects |
||
| 189 | foreach ( $chunked_ids as $chunk ) { |
||
| 190 | /** |
||
| 191 | * Fires with a chunk of object IDs during full sync. |
||
| 192 | * These are expanded to full objects before upload |
||
| 193 | * |
||
| 194 | * @since 4.1 |
||
| 195 | */ |
||
| 196 | do_action( $action_name, $chunk ); |
||
| 197 | $chunk_count++; |
||
| 198 | } |
||
| 199 | |||
| 200 | $page += 1; |
||
| 201 | $offset = ( $page * $items_per_page ) - $items_per_page; |
||
| 202 | } |
||
| 203 | return $chunk_count; |
||
| 204 | } |
||
| 205 | |||
| 206 | public function expand_post_ids( $args ) { |
||
| 207 | $post_ids = $args[0]; |
||
| 208 | |||
| 209 | $posts = array_map( array( 'WP_Post', 'get_instance' ), $post_ids ); |
||
| 210 | $posts = array_map( array( $this->get_client(), 'filter_post_content_and_add_links' ), $posts ); |
||
| 211 | |||
| 212 | return array( |
||
| 213 | 'posts' => $posts, |
||
| 214 | 'post_metas' => $this->get_metadata( $post_ids, 'post' ), |
||
| 215 | 'terms' => $this->get_term_relationships( $post_ids ) |
||
| 216 | ); |
||
| 217 | } |
||
| 218 | |||
| 219 | private function enqueue_all_comments() { |
||
| 220 | global $wpdb; |
||
| 221 | |||
| 222 | $total = $this->enqueue_all_ids_as_action( 'jetpack_full_sync_comments', $wpdb->comments, 'comment_ID', null ); |
||
| 223 | $this->update_queue_progress( 'comments', $total ); |
||
| 224 | } |
||
| 225 | |||
| 226 | public function expand_comment_ids( $args ) { |
||
| 227 | $comment_ids = $args[0]; |
||
| 228 | $comments = get_comments( array( |
||
| 229 | 'include_unapproved' => true, |
||
| 230 | 'comment__in' => $comment_ids, |
||
| 231 | ) ); |
||
| 232 | $comments = array_map( array( $this->get_client(), 'filter_comment_and_add_hc_meta' ), $comments ); |
||
| 233 | |||
| 234 | return array( |
||
| 235 | 'comments' => $comments, |
||
| 236 | 'comment_metas' => $this->get_metadata( $comment_ids, 'comment' ), |
||
| 237 | ); |
||
| 238 | } |
||
| 239 | |||
| 240 | public function expand_term_ids( $args ) { |
||
| 241 | global $wp_version; |
||
| 242 | $term_ids = $args[0]; |
||
| 243 | $taxonomy = $args[1]; |
||
| 244 | // version 4.5 or higher |
||
| 245 | if ( version_compare( $wp_version, 4.5, '>=' ) ) { |
||
| 246 | $terms = get_terms( array( |
||
| 247 | 'taxonomy' => $taxonomy, |
||
| 248 | 'hide_empty' => false, |
||
| 249 | 'include' => $term_ids |
||
| 250 | ) ); |
||
| 251 | } else { |
||
| 252 | $terms = get_terms( $taxonomy, array( |
||
| 253 | 'hide_empty' => false, |
||
| 254 | 'include' => $term_ids |
||
| 255 | ) ); |
||
| 256 | } |
||
| 257 | |||
| 258 | return $terms; |
||
| 259 | } |
||
| 260 | |||
| 261 | public function expand_options( $args ) { |
||
| 262 | if ( $args[0] ) { |
||
| 263 | return $this->get_client()->get_all_options(); |
||
| 264 | } |
||
| 265 | |||
| 266 | return $args; |
||
| 267 | } |
||
| 268 | |||
| 269 | public function expand_constants( $args ) { |
||
| 270 | if ( $args[0] ) { |
||
| 271 | return $this->get_client()->get_all_constants(); |
||
| 272 | } |
||
| 273 | return $args; |
||
| 274 | } |
||
| 275 | |||
| 276 | public function expand_callables( $args ) { |
||
| 277 | if ( $args[0] ) { |
||
| 278 | return $this->get_client()->get_all_callables(); |
||
| 279 | } |
||
| 280 | return $args; |
||
| 281 | } |
||
| 282 | |||
| 283 | private function enqueue_all_users() { |
||
| 284 | global $wpdb; |
||
| 285 | $total = $this->enqueue_all_ids_as_action( 'jetpack_full_sync_users', $wpdb->users, 'ID', null ); |
||
| 286 | $this->update_queue_progress( 'users', $total ); |
||
| 287 | } |
||
| 288 | |||
| 289 | public function expand_users( $args ) { |
||
| 290 | $user_ids = $args[0]; |
||
| 291 | return array_map( array( $this->get_client(), 'sanitize_user' ), get_users( array( 'include' => $user_ids ) ) ); |
||
| 292 | } |
||
| 293 | |||
| 294 | public function expand_network_options( $args ) { |
||
| 295 | if ( $args[0] ) { |
||
| 296 | return $this->get_client()->get_all_network_options(); |
||
| 297 | } |
||
| 298 | |||
| 299 | return $args; |
||
| 300 | } |
||
| 301 | |||
| 302 | private function get_metadata( $ids, $meta_type ) { |
||
| 312 | |||
| 313 | private function get_term_relationships( $ids ) { |
||
| 314 | global $wpdb; |
||
| 315 | |||
| 318 | |||
| 319 | // TODO: |
||
| 320 | private function enqueue_all_theme_info() { |
||
| 321 | $total = $this->get_client()->send_theme_info(); |
||
| 322 | $this->update_queue_progress( 'themes', $total ); |
||
| 323 | } |
||
| 324 | |||
| 325 | private function enqueue_all_updates() { |
||
| 326 | |||
| 327 | // check for updates |
||
| 328 | $total = $this->get_client()->full_sync_updates(); |
||
| 329 | $this->update_queue_progress( 'updates', $total ); |
||
| 330 | } |
||
| 331 | |||
| 332 | public function expand_updates( $args ) { |
||
| 339 | |||
| 340 | function update_sent_progress_action( $action_to_send ) { |
||
| 357 | |||
| 358 | function action_to_modules( $action ) { |
||
| 359 | switch( $action ) { |
||
| 360 | case 'jetpack_full_sync_constants': |
||
| 361 | return 'constants'; |
||
| 362 | break; |
||
| 363 | |||
| 364 | case 'jetpack_full_sync_callables': |
||
| 365 | return 'functions'; |
||
| 366 | break; |
||
| 367 | |||
| 368 | case 'jetpack_full_sync_options': |
||
| 369 | return 'options'; |
||
| 370 | break; |
||
| 371 | |||
| 372 | case 'jetpack_full_sync_network_options': |
||
| 373 | return 'network_options'; |
||
| 374 | break; |
||
| 403 | |||
| 404 | private function set_status_queuing_started() { |
||
| 409 | |||
| 410 | private function set_status_queuing_finished() { |
||
| 413 | |||
| 414 | // these are called by the Sync Client when it sees that the full sync start/end actions have actually been transmitted |
||
| 415 | public function set_status_sending_started() { |
||
| 427 | |||
| 428 | public function set_status_sending_finished() { |
||
| 438 | |||
| 439 | private $initial_status = array( |
||
| 440 | 'started' => null, |
||
| 441 | 'queue_finished' => null, |
||
| 442 | 'sent_started' => null, |
||
| 443 | 'finished' => null, |
||
| 444 | 'sent' => array(), |
||
| 445 | 'queue' => array(), |
||
| 446 | ); |
||
| 447 | |||
| 448 | public function get_status() { |
||
| 451 | |||
| 452 | |||
| 453 | public function update_status( $status ) { |
||
| 459 | |||
| 460 | private function clear_status() { |
||
| 463 | |||
| 464 | public function update_queue_progress( $module, $data ) { |
||
| 469 | |||
| 470 | public function update_sent_progress( $module, $data ) { |
||
| 475 | |||
| 476 | } |
||
| 477 |
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.