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 | |||
9 | public function name() { |
||
12 | |||
13 | public function get_object_by_id( $object_type, $id ) { |
||
20 | |||
21 | public function set_defaults() { |
||
23 | |||
24 | public function init_listeners( $callable ) { |
||
37 | |||
38 | public function init_full_sync_listeners( $callable ) { |
||
41 | |||
42 | public function init_before_send() { |
||
48 | |||
49 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
54 | |||
55 | public function estimate_full_sync_actions( $config ) { |
||
63 | |||
64 | View Code Duplication | private function get_where_sql( $config ) { |
|
74 | |||
75 | function get_full_sync_actions() { |
||
78 | |||
79 | /** |
||
80 | * Process content before send |
||
81 | */ |
||
82 | |||
83 | function expand_wp_insert_post( $args ) { |
||
86 | |||
87 | function filter_blacklisted_post_types( $args ) { |
||
96 | |||
97 | // Meta |
||
98 | function filter_meta( $args ) { |
||
105 | |||
106 | function is_whitelisted_post_meta( $meta_key ) { |
||
110 | |||
111 | function is_post_type_allowed( $post_id ) { |
||
115 | |||
116 | View Code Duplication | function remove_embed() { |
|
124 | |||
125 | View Code Duplication | function add_embed() { |
|
133 | |||
134 | // Expands wp_insert_post to include filtered content |
||
135 | function filter_post_content_and_add_links( $post_object ) { |
||
136 | global $post, $shortcode_tags; |
||
137 | $post = $post_object; |
||
138 | |||
139 | // return non existant post |
||
140 | $post_type = get_post_type_object( $post->post_type ); |
||
141 | View Code Duplication | if ( empty( $post_type) || ! is_object( $post_type ) ) { |
|
142 | $non_existant_post = new stdClass(); |
||
143 | $non_existant_post->ID = $post->ID; |
||
144 | $non_existant_post->post_modified = $post->post_modified; |
||
145 | $non_existant_post->post_modified_gmt = $post->post_modified_gmt; |
||
146 | $non_existant_post->post_status = 'jetpack_sync_non_registered_post_type'; |
||
147 | |||
148 | return $non_existant_post; |
||
149 | } |
||
150 | /** |
||
151 | * Filters whether to prevent sending post data to .com |
||
152 | * |
||
153 | * Passing true to the filter will prevent the post data from being sent |
||
154 | * to the WordPress.com. |
||
155 | * Instead we pass data that will still enable us to do a checksum against the |
||
156 | * Jetpacks data but will prevent us from displaying the data on in the API as well as |
||
157 | * other services. |
||
158 | * @since 4.2.0 |
||
159 | * |
||
160 | * @param boolean false prevent post data from being synced to WordPress.com |
||
161 | * @param mixed $post WP_POST object |
||
162 | */ |
||
163 | View Code Duplication | if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) { |
|
164 | // We only send the bare necessary object to be able to create a checksum. |
||
165 | $blocked_post = new stdClass(); |
||
166 | $blocked_post->ID = $post->ID; |
||
167 | $blocked_post->post_modified = $post->post_modified; |
||
168 | $blocked_post->post_modified_gmt = $post->post_modified_gmt; |
||
169 | $blocked_post->post_status = 'jetpack_sync_blocked'; |
||
170 | |||
171 | return $blocked_post; |
||
172 | } |
||
173 | |||
174 | // lets not do oembed just yet. |
||
175 | $this->remove_embed(); |
||
176 | |||
177 | if ( 0 < strlen( $post->post_password ) ) { |
||
178 | $post->post_password = 'auto-' . wp_generate_password( 10, false ); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Filter prevents some shortcodes from expanding. |
||
183 | * |
||
184 | * Since we can can expand some type of shortcode better on the .com side and make the |
||
185 | * expansion more relevant to contexts. For example [galleries] and subscription emails |
||
186 | * |
||
187 | * @since 4.5.0 |
||
188 | * |
||
189 | * @param array of shortcode tags to remove. |
||
190 | */ |
||
191 | $shortcodes_to_remove = apply_filters( 'jetpack_sync_do_not_expand_shortcodes', array( 'gallery', 'slideshow' ) ); |
||
192 | foreach ( $shortcodes_to_remove as $shortcode ) { |
||
193 | if ( isset ( $shortcode_tags[ $shortcode ] ) ) { |
||
194 | $shortcodes_and_callbacks_to_remove[ $shortcode ] = $shortcode_tags[ $shortcode ]; |
||
195 | } |
||
196 | } |
||
197 | |||
198 | array_map( 'remove_shortcode' , array_keys( $shortcodes_and_callbacks_to_remove ) ); |
||
199 | |||
200 | /** This filter is already documented in core. wp-includes/post-template.php */ |
||
201 | if ( Jetpack_Sync_Settings::get_setting( 'render_filtered_content' ) && $post_type->public ) { |
||
202 | $post->post_content_filtered = apply_filters( 'the_content', $post->post_content ); |
||
203 | $post->post_excerpt_filtered = apply_filters( 'the_excerpt', $post->post_excerpt ); |
||
204 | } |
||
205 | |||
206 | foreach ( $shortcodes_and_callbacks_to_remove as $shortcode => $callback ) { |
||
207 | add_shortcode( $shortcode, $callback ); |
||
208 | } |
||
209 | |||
210 | $this->add_embed(); |
||
211 | |||
212 | if ( has_post_thumbnail( $post->ID ) ) { |
||
213 | $image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); |
||
214 | if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) { |
||
215 | $post->featured_image = $image_attributes[0]; |
||
216 | } |
||
217 | } |
||
218 | |||
219 | $post->permalink = get_permalink( $post->ID ); |
||
220 | $post->shortlink = wp_get_shortlink( $post->ID ); |
||
221 | $post->dont_email_post_to_subs = Jetpack::is_module_active( 'subscriptions' ) ? |
||
222 | get_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', true ) : |
||
223 | true; // Don't email subscription if the subscription module is not active. |
||
224 | |||
225 | return $post; |
||
226 | } |
||
227 | |||
228 | public function save_published( $new_status, $old_status, $post ) { |
||
233 | |||
234 | public function send_published( $post_ID, $post, $update ) { |
||
273 | |||
274 | public function expand_post_ids( $args ) { |
||
287 | } |
||
288 |
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.