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