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_Module_Posts 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_Module_Posts, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class Jetpack_Sync_Module_Posts extends Jetpack_Sync_Module { |
||
| 6 | |||
| 7 | private $just_published = array(); |
||
| 8 | private $previous_status = array(); |
||
| 9 | private $action_handler; |
||
| 10 | private $import_end = false; |
||
| 11 | |||
| 12 | const DEFAULT_PREVIOUS_STATE = 'new'; |
||
| 13 | |||
| 14 | public function name() { |
||
| 15 | return 'posts'; |
||
| 16 | } |
||
| 17 | |||
| 18 | public function get_object_by_id( $object_type, $id ) { |
||
| 19 | if ( $object_type === 'post' && $post = get_post( intval( $id ) ) ) { |
||
| 20 | return $this->filter_post_content_and_add_links( $post ); |
||
| 21 | } |
||
| 22 | |||
| 23 | return false; |
||
| 24 | } |
||
| 25 | |||
| 26 | public function set_defaults() { |
||
| 27 | $this->import_end = false; |
||
| 28 | } |
||
| 29 | |||
| 30 | public function init_listeners( $callable ) { |
||
| 31 | $this->action_handler = $callable; |
||
| 32 | |||
| 33 | // Core < 4.7 doesn't deal with nested wp_insert_post calls very well |
||
| 34 | global $wp_version; |
||
| 35 | $priority = version_compare( $wp_version, '4.7-alpha', '<' ) ? 0 : 11; |
||
| 36 | |||
| 37 | add_action( 'wp_insert_post', array( $this, 'wp_insert_post' ), $priority, 3 ); |
||
| 38 | add_action( 'jetpack_sync_save_post', $callable, 10, 4 ); |
||
| 39 | |||
| 40 | add_action( 'deleted_post', $callable, 10 ); |
||
| 41 | add_action( 'jetpack_published_post', $callable, 10, 2 ); |
||
| 42 | |||
| 43 | add_action( 'transition_post_status', array( $this, 'save_published' ), 10, 3 ); |
||
| 44 | add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_save_post', array( $this, 'filter_blacklisted_post_types' ) ); |
||
| 45 | |||
| 46 | // listen for meta changes |
||
| 47 | $this->init_listeners_for_meta_type( 'post', $callable ); |
||
| 48 | $this->init_meta_whitelist_handler( 'post', array( $this, 'filter_meta' ) ); |
||
| 49 | |||
| 50 | add_action( 'export_wp', $callable ); |
||
| 51 | add_action( 'jetpack_sync_import_end', $callable, 10, 2 ); |
||
| 52 | |||
| 53 | // Movable type, RSS, Livejournal |
||
| 54 | add_action( 'import_done', array( $this, 'sync_import_done' ) ); |
||
| 55 | |||
| 56 | // WordPress, Blogger, Livejournal, woo tax rate |
||
| 57 | add_action( 'import_end', array( $this, 'sync_import_end' ) ); |
||
| 58 | } |
||
| 59 | |||
| 60 | public function sync_import_done( $importer ) { |
||
| 61 | // We already ran an send the import |
||
| 62 | if ( $this->import_end ) { |
||
| 63 | return; |
||
| 64 | } |
||
| 65 | |||
| 66 | $importer_name = $this->get_importer_name( $importer ); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Sync Event that tells that the import is finished |
||
| 70 | * |
||
| 71 | * @since 5.0.0 |
||
| 72 | * |
||
| 73 | * $param string $importer |
||
| 74 | */ |
||
| 75 | do_action( 'jetpack_sync_import_end', $importer, $importer_name ); |
||
| 76 | $this->import_end = true; |
||
| 77 | } |
||
| 78 | |||
| 79 | public function sync_import_end() { |
||
| 80 | // We already ran an send the import |
||
| 81 | if ( $this->import_end ) { |
||
| 82 | return; |
||
| 83 | } |
||
| 84 | |||
| 85 | $this->import_end = true; |
||
| 86 | $importer = 'unknown'; |
||
| 87 | $backtrace = wp_debug_backtrace_summary( null, 0, false ); |
||
| 88 | if ( $this->is_importer( $backtrace, 'Blogger_Importer' ) ) { |
||
| 89 | $importer = 'blogger'; |
||
| 90 | } |
||
| 91 | |||
| 92 | if ( 'unknown' === $importer && $this->is_importer( $backtrace, 'WC_Tax_Rate_Importer' ) ) { |
||
| 93 | $importer = 'woo-tax-rate'; |
||
| 94 | } |
||
| 95 | |||
| 96 | if ( 'unknown' === $importer && $this->is_importer( $backtrace, 'WP_Import' ) ) { |
||
| 97 | $importer = 'wordpress'; |
||
| 98 | } |
||
| 99 | |||
| 100 | $importer_name = $this->get_importer_name( $importer ); |
||
| 101 | |||
| 102 | /** This filter is already documented in sync/class.jetpack-sync-module-posts.php */ |
||
| 103 | do_action( 'jetpack_sync_import_end', $importer, $importer_name ); |
||
| 104 | } |
||
| 105 | |||
| 106 | private function get_importer_name( $importer ) { |
||
| 107 | $importers = get_importers(); |
||
| 108 | return isset( $importers[ $importer ] ) ? $importers[ $importer ][0] : 'Unknown Importer'; |
||
| 109 | } |
||
| 110 | |||
| 111 | private function is_importer( $backtrace, $class_name ) { |
||
| 112 | foreach ( $backtrace as $trace ) { |
||
| 113 | if ( strpos( $trace, $class_name ) !== false ) { |
||
| 114 | return true; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | return false; |
||
| 119 | } |
||
| 120 | |||
| 121 | public function init_full_sync_listeners( $callable ) { |
||
| 122 | add_action( 'jetpack_full_sync_posts', $callable ); // also sends post meta |
||
| 123 | } |
||
| 124 | |||
| 125 | public function init_before_send() { |
||
| 126 | add_filter( 'jetpack_sync_before_send_jetpack_sync_save_post', array( $this, 'expand_jetpack_sync_save_post' ) ); |
||
| 127 | |||
| 128 | // full sync |
||
| 129 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_posts', array( $this, 'expand_post_ids' ) ); |
||
| 130 | } |
||
| 131 | |||
| 132 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
| 137 | |||
| 138 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
| 146 | |||
| 147 | View Code Duplication | private function get_where_sql( $config ) { |
|
| 157 | |||
| 158 | function get_full_sync_actions() { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Process content before send |
||
| 164 | * |
||
| 165 | * @param array $args wp_insert_post arguments |
||
| 166 | * |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | function expand_jetpack_sync_save_post( $args ) { |
||
| 170 | list( $post_id, $post, $update, $previous_state ) = $args; |
||
| 171 | return array( $post_id, $this->filter_post_content_and_add_links( $post ), $update, $previous_state ); |
||
| 172 | } |
||
| 173 | |||
| 174 | function filter_blacklisted_post_types( $args ) { |
||
| 175 | $post = $args[1]; |
||
| 176 | |||
| 177 | if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) { |
||
| 178 | return false; |
||
| 179 | } |
||
| 180 | |||
| 181 | return $args; |
||
| 182 | } |
||
| 183 | |||
| 184 | // Meta |
||
| 185 | function filter_meta( $args ) { |
||
| 186 | if ( $this->is_post_type_allowed( $args[1] ) && $this->is_whitelisted_post_meta( $args[2] ) ) { |
||
| 187 | return $args; |
||
| 188 | } |
||
| 189 | |||
| 190 | return false; |
||
| 191 | } |
||
| 192 | |||
| 193 | function is_whitelisted_post_meta( $meta_key ) { |
||
| 194 | // _wpas_skip_ is used by publicize |
||
| 195 | return in_array( $meta_key, Jetpack_Sync_Settings::get_setting( 'post_meta_whitelist' ) ) || wp_startswith( $meta_key, '_wpas_skip_' ); |
||
| 196 | } |
||
| 197 | |||
| 198 | function is_post_type_allowed( $post_id ) { |
||
| 199 | $post = get_post( intval( $post_id ) ); |
||
| 200 | if( $post->post_type ) { |
||
| 201 | return ! in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ); |
||
| 202 | } |
||
| 203 | return false; |
||
| 204 | } |
||
| 205 | |||
| 206 | View Code Duplication | function remove_embed() { |
|
| 214 | |||
| 215 | View Code Duplication | function add_embed() { |
|
| 223 | |||
| 224 | // Expands wp_insert_post to include filtered content |
||
| 225 | function filter_post_content_and_add_links( $post_object ) { |
||
| 319 | |||
| 320 | public function save_published( $new_status, $old_status, $post ) { |
||
| 321 | if ( 'publish' === $new_status && 'publish' !== $old_status ) { |
||
| 322 | $this->just_published[ $post->ID ] = true; |
||
| 323 | } |
||
| 324 | |||
| 325 | $this->previous_status[ $post->ID ] = $old_status; |
||
| 326 | } |
||
| 327 | |||
| 328 | public function wp_insert_post( $post_ID, $post = null, $update = null ) { |
||
| 329 | if ( ! is_numeric( $post_ID ) || is_null( $post ) ) { |
||
| 330 | return; |
||
| 331 | } |
||
| 332 | |||
| 333 | // workaround for https://github.com/woocommerce/woocommerce/issues/18007 |
||
| 334 | if ( $post && 'shop_order' === $post->post_type ) { |
||
| 367 | |||
| 368 | public function send_published( $post_ID, $post ) { |
||
| 414 | |||
| 415 | public function expand_post_ids( $args ) { |
||
| 428 | } |
||
| 429 |