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