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 $action_handler; |
||
| 9 | |||
| 10 | public function name() { |
||
| 11 | return 'posts'; |
||
| 12 | } |
||
| 13 | |||
| 14 | public function get_object_by_id( $object_type, $id ) { |
||
| 15 | if ( $object_type === 'post' && $post = get_post( intval( $id ) ) ) { |
||
| 16 | return $this->filter_post_content_and_add_links( $post ); |
||
| 17 | } |
||
| 18 | |||
| 19 | return false; |
||
| 20 | } |
||
| 21 | |||
| 22 | public function set_defaults() { |
||
| 23 | } |
||
| 24 | |||
| 25 | public function init_listeners( $callable ) { |
||
| 26 | $this->action_handler = $callable; |
||
| 27 | |||
| 28 | // Core < 4.7 doesn't deal with nested wp_insert_post calls very well |
||
| 29 | global $wp_version; |
||
| 30 | $priority = version_compare( $wp_version, '4.7-alpha', '<' ) ? 0 : 11; |
||
| 31 | |||
| 32 | add_action( 'wp_insert_post', array( $this, 'wp_insert_post' ), $priority, 3 ); |
||
| 33 | |||
| 34 | add_action( 'deleted_post', $callable, 10 ); |
||
| 35 | add_action( 'jetpack_published_post', $callable, 10, 2 ); |
||
| 36 | add_action( 'transition_post_status', array( $this, 'save_published' ), 10, 3 ); |
||
| 37 | add_filter( 'jetpack_sync_before_enqueue_wp_insert_post', array( $this, 'filter_blacklisted_post_types' ) ); |
||
| 38 | |||
| 39 | // listen for meta changes |
||
| 40 | $this->init_listeners_for_meta_type( 'post', $callable ); |
||
| 41 | $this->init_meta_whitelist_handler( 'post', array( $this, 'filter_meta' ) ); |
||
| 42 | } |
||
| 43 | |||
| 44 | public function init_full_sync_listeners( $callable ) { |
||
| 45 | add_action( 'jetpack_full_sync_posts', $callable ); // also sends post meta |
||
| 46 | } |
||
| 47 | |||
| 48 | public function init_before_send() { |
||
| 49 | add_filter( 'jetpack_sync_before_send_wp_insert_post', array( $this, 'expand_wp_insert_post' ) ); |
||
| 50 | |||
| 51 | // full sync |
||
| 52 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_posts', array( $this, 'expand_post_ids' ) ); |
||
| 53 | } |
||
| 54 | |||
| 55 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
| 56 | global $wpdb; |
||
| 57 | |||
| 58 | return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_posts', $wpdb->posts, 'ID', $this->get_where_sql( $config ), $max_items_to_enqueue, $state ); |
||
| 59 | } |
||
| 60 | |||
| 61 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
| 62 | global $wpdb; |
||
| 63 | |||
| 64 | $query = "SELECT count(*) FROM $wpdb->posts WHERE " . $this->get_where_sql( $config ); |
||
| 65 | $count = $wpdb->get_var( $query ); |
||
| 66 | |||
| 67 | return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); |
||
| 68 | } |
||
| 69 | |||
| 70 | View Code Duplication | private function get_where_sql( $config ) { |
|
| 71 | $where_sql = Jetpack_Sync_Settings::get_blacklisted_post_types_sql(); |
||
| 72 | |||
| 73 | // config is a list of post IDs to sync |
||
| 74 | if ( is_array( $config ) ) { |
||
| 75 | $where_sql .= ' AND ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')'; |
||
| 76 | } |
||
| 77 | |||
| 78 | return $where_sql; |
||
| 79 | } |
||
| 80 | |||
| 81 | function get_full_sync_actions() { |
||
| 82 | return array( 'jetpack_full_sync_posts' ); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Process content before send |
||
| 87 | * |
||
| 88 | * @param array $args wp_insert_post arguments |
||
| 89 | * |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | function expand_wp_insert_post( $args ) { |
||
| 93 | return array( $args[0], $this->filter_post_content_and_add_links( $args[1] ), $args[2] ); |
||
| 94 | } |
||
| 95 | |||
| 96 | function filter_blacklisted_post_types( $args ) { |
||
| 97 | $post = $args[1]; |
||
| 98 | |||
| 99 | if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) { |
||
| 100 | return false; |
||
| 101 | } |
||
| 102 | |||
| 103 | return $args; |
||
| 104 | } |
||
| 105 | |||
| 106 | // Meta |
||
| 107 | function filter_meta( $args ) { |
||
| 108 | if ( $this->is_post_type_allowed( $args[1] ) && $this->is_whitelisted_post_meta( $args[2] ) ) { |
||
| 109 | return $args; |
||
| 110 | } |
||
| 111 | |||
| 112 | return false; |
||
| 113 | } |
||
| 114 | |||
| 115 | function is_whitelisted_post_meta( $meta_key ) { |
||
| 116 | // _wpas_skip_ is used by publicize |
||
| 117 | return in_array( $meta_key, Jetpack_Sync_Settings::get_setting( 'post_meta_whitelist' ) ) || wp_startswith( $meta_key, '_wpas_skip_' ); |
||
| 118 | } |
||
| 119 | |||
| 120 | function is_post_type_allowed( $post_id ) { |
||
| 121 | $post = get_post( $post_id ); |
||
| 122 | |||
| 123 | return ! in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ); |
||
| 124 | } |
||
| 125 | |||
| 126 | View Code Duplication | function remove_embed() { |
|
| 134 | |||
| 135 | View Code Duplication | function add_embed() { |
|
| 143 | |||
| 144 | // Expands wp_insert_post to include filtered content |
||
| 145 | function filter_post_content_and_add_links( $post_object ) { |
||
| 146 | global $post; |
||
| 147 | $post = $post_object; |
||
| 148 | |||
| 149 | // return non existant post |
||
| 150 | $post_type = get_post_type_object( $post->post_type ); |
||
| 151 | View Code Duplication | if ( empty( $post_type ) || ! is_object( $post_type ) ) { |
|
| 152 | $non_existant_post = new stdClass(); |
||
| 153 | $non_existant_post->ID = $post->ID; |
||
| 154 | $non_existant_post->post_modified = $post->post_modified; |
||
| 155 | $non_existant_post->post_modified_gmt = $post->post_modified_gmt; |
||
| 156 | $non_existant_post->post_status = 'jetpack_sync_non_registered_post_type'; |
||
| 157 | |||
| 158 | return $non_existant_post; |
||
| 239 | |||
| 240 | public function save_published( $new_status, $old_status, $post ) { |
||
| 245 | |||
| 246 | public function wp_insert_post( $post_ID, $post, $update ) { |
||
| 250 | |||
| 251 | public function send_published( $post_ID, $post ) { |
||
| 283 | |||
| 284 | public function expand_post_ids( $args ) { |
||
| 297 | } |
||
| 298 |