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 $just_trashed = array(); |
||
9 | private $action_handler; |
||
10 | private $import_end = false; |
||
11 | |||
12 | public function name() { |
||
15 | |||
16 | public function get_object_by_id( $object_type, $id ) { |
||
23 | |||
24 | public function set_defaults() { |
||
27 | |||
28 | public function init_listeners( $callable ) { |
||
57 | |||
58 | public function sync_import_done( $importer ) { |
||
59 | // We already ran an send the import |
||
60 | if ( $this->import_end ) { |
||
61 | return; |
||
62 | } |
||
63 | |||
64 | $importer_name = $this->get_importer_name( $importer ); |
||
65 | |||
66 | /** |
||
67 | * Sync Event that tells that the import is finished |
||
68 | * |
||
69 | * @since 5.0.0 |
||
70 | * |
||
71 | * $param string $importer |
||
72 | */ |
||
73 | do_action( 'jetpack_sync_import_end', $importer, $importer_name ); |
||
74 | $this->import_end = true; |
||
75 | } |
||
76 | |||
77 | public function sync_import_end() { |
||
78 | // We already ran an send the import |
||
79 | if ( $this->import_end ) { |
||
80 | return; |
||
81 | } |
||
82 | |||
83 | $this->import_end = true; |
||
84 | $importer = 'unknown'; |
||
85 | $backtrace = wp_debug_backtrace_summary( null, 0, false ); |
||
86 | if ( $this->is_importer( $backtrace, 'Blogger_Importer' ) ) { |
||
87 | $importer = 'blogger'; |
||
88 | } |
||
89 | |||
90 | if ( 'unknown' === $importer && $this->is_importer( $backtrace, 'WC_Tax_Rate_Importer' ) ) { |
||
91 | $importer = 'woo-tax-rate'; |
||
92 | } |
||
93 | |||
94 | if ( 'unknown' === $importer && $this->is_importer( $backtrace, 'WP_Import' ) ) { |
||
95 | $importer = 'wordpress'; |
||
96 | } |
||
97 | |||
98 | $importer_name = $this->get_importer_name( $importer ); |
||
99 | |||
100 | /** This filter is already documented in sync/class.jetpack-sync-module-posts.php */ |
||
101 | do_action( 'jetpack_sync_import_end', $importer, $importer_name ); |
||
102 | } |
||
103 | |||
104 | private function get_importer_name( $importer ) { |
||
105 | $importers = get_importers(); |
||
106 | return isset( $importers[ $importer ] ) ? $importers[ $importer ][0] : 'Unknown Importer'; |
||
107 | } |
||
108 | |||
109 | private function is_importer( $backtrace, $class_name ) { |
||
110 | foreach ( $backtrace as $trace ) { |
||
111 | if ( strpos( $trace, $class_name ) !== false ) { |
||
112 | return true; |
||
113 | } |
||
114 | } |
||
115 | |||
116 | return false; |
||
117 | } |
||
118 | |||
119 | public function init_full_sync_listeners( $callable ) { |
||
122 | |||
123 | public function init_before_send() { |
||
124 | add_filter( 'jetpack_sync_before_send_wp_insert_post', array( $this, 'expand_wp_insert_post' ) ); |
||
125 | |||
126 | // full sync |
||
127 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_posts', array( $this, 'expand_post_ids' ) ); |
||
128 | } |
||
129 | |||
130 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
131 | global $wpdb; |
||
132 | |||
133 | return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_posts', $wpdb->posts, 'ID', $this->get_where_sql( $config ), $max_items_to_enqueue, $state ); |
||
134 | } |
||
135 | |||
136 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
137 | global $wpdb; |
||
138 | |||
139 | $query = "SELECT count(*) FROM $wpdb->posts WHERE " . $this->get_where_sql( $config ); |
||
140 | $count = $wpdb->get_var( $query ); |
||
141 | |||
142 | return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); |
||
143 | } |
||
144 | |||
145 | View Code Duplication | private function get_where_sql( $config ) { |
|
146 | $where_sql = Jetpack_Sync_Settings::get_blacklisted_post_types_sql(); |
||
147 | |||
148 | // config is a list of post IDs to sync |
||
149 | if ( is_array( $config ) ) { |
||
150 | $where_sql .= ' AND ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')'; |
||
151 | } |
||
152 | |||
153 | return $where_sql; |
||
154 | } |
||
155 | |||
156 | function get_full_sync_actions() { |
||
157 | return array( 'jetpack_full_sync_posts' ); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Process content before send |
||
162 | * |
||
163 | * @param array $args wp_insert_post arguments |
||
164 | * |
||
165 | * @return array |
||
166 | */ |
||
167 | function expand_wp_insert_post( $args ) { |
||
168 | $post_id = $args[0]; |
||
169 | $post = $args[1]; |
||
170 | $update = $args[2]; |
||
171 | $is_auto_save = isset( $args[3] ) ? $args[3] : false; //See https://github.com/Automattic/jetpack/issues/7372 |
||
172 | $just_published = isset( $args[4] ) ? $args[4] : false; //Preventative in light of above issue |
||
173 | |||
174 | return array( $post_id, $this->filter_post_content_and_add_links( $post ), $update, $is_auto_save, $just_published ); |
||
175 | } |
||
176 | |||
177 | function filter_blacklisted_post_types( $args ) { |
||
178 | $post = $args[1]; |
||
179 | |||
180 | if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) { |
||
181 | return false; |
||
182 | } |
||
183 | |||
184 | return $args; |
||
185 | } |
||
186 | |||
187 | // Meta |
||
188 | function filter_meta( $args ) { |
||
189 | if ( $this->is_post_type_allowed( $args[1] ) && $this->is_whitelisted_post_meta( $args[2] ) ) { |
||
190 | return $args; |
||
191 | } |
||
192 | |||
193 | return false; |
||
194 | } |
||
195 | |||
196 | function is_whitelisted_post_meta( $meta_key ) { |
||
197 | // _wpas_skip_ is used by publicize |
||
198 | return in_array( $meta_key, Jetpack_Sync_Settings::get_setting( 'post_meta_whitelist' ) ) || wp_startswith( $meta_key, '_wpas_skip_' ); |
||
199 | } |
||
200 | |||
201 | function is_post_type_allowed( $post_id ) { |
||
202 | $post = get_post( $post_id ); |
||
203 | |||
204 | return ! in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ); |
||
205 | } |
||
206 | |||
207 | View Code Duplication | function remove_embed() { |
|
208 | global $wp_embed; |
||
209 | remove_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 ); |
||
210 | // remove the embed shortcode since we would do the part later. |
||
211 | remove_shortcode( 'embed' ); |
||
212 | // Attempts to embed all URLs in a post |
||
213 | remove_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 ); |
||
214 | } |
||
215 | |||
216 | View Code Duplication | function add_embed() { |
|
217 | global $wp_embed; |
||
218 | add_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 ); |
||
219 | // Shortcode placeholder for strip_shortcodes() |
||
220 | add_shortcode( 'embed', '__return_false' ); |
||
221 | // Attempts to embed all URLs in a post |
||
222 | add_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 ); |
||
223 | } |
||
224 | |||
225 | // Expands wp_insert_post to include filtered content |
||
226 | function filter_post_content_and_add_links( $post_object ) { |
||
227 | global $post; |
||
228 | $post = $post_object; |
||
229 | |||
230 | // return non existant post |
||
231 | $post_type = get_post_type_object( $post->post_type ); |
||
232 | View Code Duplication | if ( empty( $post_type ) || ! is_object( $post_type ) ) { |
|
233 | $non_existant_post = new stdClass(); |
||
234 | $non_existant_post->ID = $post->ID; |
||
235 | $non_existant_post->post_modified = $post->post_modified; |
||
236 | $non_existant_post->post_modified_gmt = $post->post_modified_gmt; |
||
237 | $non_existant_post->post_status = 'jetpack_sync_non_registered_post_type'; |
||
238 | |||
239 | return $non_existant_post; |
||
240 | } |
||
241 | /** |
||
242 | * Filters whether to prevent sending post data to .com |
||
243 | * |
||
244 | * Passing true to the filter will prevent the post data from being sent |
||
245 | * to the WordPress.com. |
||
246 | * Instead we pass data that will still enable us to do a checksum against the |
||
247 | * Jetpacks data but will prevent us from displaying the data on in the API as well as |
||
248 | * other services. |
||
249 | * @since 4.2.0 |
||
250 | * |
||
251 | * @param boolean false prevent post data from being synced to WordPress.com |
||
252 | * @param mixed $post WP_POST object |
||
253 | */ |
||
254 | View Code Duplication | if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) { |
|
255 | // We only send the bare necessary object to be able to create a checksum. |
||
256 | $blocked_post = new stdClass(); |
||
257 | $blocked_post->ID = $post->ID; |
||
258 | $blocked_post->post_modified = $post->post_modified; |
||
259 | $blocked_post->post_modified_gmt = $post->post_modified_gmt; |
||
260 | $blocked_post->post_status = 'jetpack_sync_blocked'; |
||
261 | |||
262 | return $blocked_post; |
||
263 | } |
||
264 | |||
265 | // lets not do oembed just yet. |
||
266 | $this->remove_embed(); |
||
267 | |||
268 | if ( 0 < strlen( $post->post_password ) ) { |
||
269 | $post->post_password = 'auto-' . wp_generate_password( 10, false ); |
||
270 | } |
||
271 | |||
272 | /** This filter is already documented in core. wp-includes/post-template.php */ |
||
273 | if ( Jetpack_Sync_Settings::get_setting( 'render_filtered_content' ) && $post_type->public ) { |
||
274 | global $shortcode_tags; |
||
275 | /** |
||
276 | * Filter prevents some shortcodes from expanding. |
||
277 | * |
||
278 | * Since we can can expand some type of shortcode better on the .com side and make the |
||
279 | * expansion more relevant to contexts. For example [galleries] and subscription emails |
||
280 | * |
||
281 | * @since 4.5.0 |
||
282 | * |
||
283 | * @param array of shortcode tags to remove. |
||
284 | */ |
||
285 | $shortcodes_to_remove = apply_filters( 'jetpack_sync_do_not_expand_shortcodes', array( |
||
286 | 'gallery', |
||
287 | 'slideshow' |
||
288 | ) ); |
||
289 | $removed_shortcode_callbacks = array(); |
||
290 | foreach ( $shortcodes_to_remove as $shortcode ) { |
||
291 | if ( isset ( $shortcode_tags[ $shortcode ] ) ) { |
||
292 | $removed_shortcode_callbacks[ $shortcode ] = $shortcode_tags[ $shortcode ]; |
||
293 | } |
||
294 | } |
||
295 | |||
296 | array_map( 'remove_shortcode', array_keys( $removed_shortcode_callbacks ) ); |
||
297 | |||
298 | $post->post_content_filtered = apply_filters( 'the_content', $post->post_content ); |
||
299 | $post->post_excerpt_filtered = apply_filters( 'the_excerpt', $post->post_excerpt ); |
||
300 | |||
301 | foreach ( $removed_shortcode_callbacks as $shortcode => $callback ) { |
||
302 | add_shortcode( $shortcode, $callback ); |
||
303 | } |
||
304 | } |
||
305 | |||
306 | $this->add_embed(); |
||
307 | |||
308 | if ( has_post_thumbnail( $post->ID ) ) { |
||
309 | $image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); |
||
310 | if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) { |
||
311 | $post->featured_image = $image_attributes[0]; |
||
312 | } |
||
313 | } |
||
314 | |||
315 | $post->permalink = get_permalink( $post->ID ); |
||
316 | $post->shortlink = wp_get_shortlink( $post->ID ); |
||
317 | |||
318 | return $post; |
||
319 | } |
||
320 | |||
321 | public function save_published( $new_status, $old_status, $post ) { |
||
322 | if ( 'publish' === $new_status && 'publish' !== $old_status ) { |
||
323 | $this->just_published[] = $post->ID; |
||
324 | } |
||
325 | |||
326 | if ( 'trash' === $new_status && 'trash' !== $old_status ) { |
||
327 | $this->just_trashed[] = $post->ID; |
||
328 | } |
||
329 | } |
||
330 | |||
331 | public function wp_insert_post( $post_ID, $post = null, $update = null ) { |
||
332 | if ( ! is_numeric( $post_ID ) || is_null( $post ) ) { |
||
333 | return; |
||
334 | } |
||
335 | |||
336 | if ( Jetpack_Constants::get_constant( 'DOING_AUTOSAVE' ) ) { |
||
337 | $is_auto_save = true; |
||
338 | } else { |
||
339 | $is_auto_save = false; |
||
340 | } |
||
341 | |||
342 | if ( ! in_array( $post_ID, $this->just_published ) ) { |
||
343 | $just_published = false; |
||
344 | } else { |
||
345 | $just_published = true; |
||
346 | } |
||
347 | |||
348 | call_user_func( $this->action_handler, $post_ID, $post, $update, $is_auto_save, $just_published ); |
||
349 | $this->send_published( $post_ID, $post ); |
||
350 | $this->send_trashed( $post_ID, $post ); |
||
351 | } |
||
352 | |||
353 | public function send_published( $post_ID, $post ) { |
||
354 | if ( ! in_array( $post_ID, $this->just_published ) ) { |
||
355 | return; |
||
356 | } |
||
357 | |||
358 | // Post revisions cause race conditions where this send_published add the action before the actual post gets synced |
||
359 | if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
||
360 | return; |
||
361 | } |
||
362 | |||
363 | $post_flags = array( |
||
364 | 'post_type' => $post->post_type |
||
365 | ); |
||
366 | |||
367 | /** |
||
368 | * Filter that is used to add to the post flags ( meta data ) when a post gets published |
||
369 | * |
||
370 | * @since 4.4.0 |
||
371 | * |
||
372 | * @param mixed array post flags that are added to the post |
||
373 | * @param mixed $post WP_POST object |
||
374 | */ |
||
375 | $flags = apply_filters( 'jetpack_published_post_flags', $post_flags, $post ); |
||
376 | |||
377 | /** |
||
378 | * Action that gets synced when a post type gets published. |
||
379 | * |
||
380 | * @since 4.4.0 |
||
381 | * |
||
382 | * @param int $post_ID |
||
383 | * @param mixed array $flags post flags that are added to the post |
||
384 | */ |
||
385 | do_action( 'jetpack_published_post', $post_ID, $flags ); |
||
386 | |||
387 | $this->just_published = array_diff( $this->just_published, array( $post_ID ) ); |
||
388 | } |
||
389 | |||
390 | public function send_trashed( $post_ID, $post ) { |
||
411 | |||
412 | public function expand_post_ids( $args ) { |
||
425 | } |
||
426 |