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_publicize_post', $callable ); |
||
36 | add_action( 'jetpack_published_post', $callable, 10, 2 ); |
||
37 | add_action( 'transition_post_status', array( $this, 'save_published' ), 10, 3 ); |
||
38 | add_filter( 'jetpack_sync_before_enqueue_wp_insert_post', array( $this, 'filter_blacklisted_post_types' ) ); |
||
39 | |||
40 | // listen for meta changes |
||
41 | $this->init_listeners_for_meta_type( 'post', $callable ); |
||
42 | $this->init_meta_whitelist_handler( 'post', array( $this, 'filter_meta' ) ); |
||
43 | } |
||
44 | |||
45 | public function init_full_sync_listeners( $callable ) { |
||
48 | |||
49 | public function init_before_send() { |
||
55 | |||
56 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
61 | |||
62 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
70 | |||
71 | View Code Duplication | private function get_where_sql( $config ) { |
|
81 | |||
82 | function get_full_sync_actions() { |
||
85 | |||
86 | /** |
||
87 | * Process content before send |
||
88 | * |
||
89 | * @param array $args wp_insert_post arguments |
||
90 | * |
||
91 | * @return array |
||
92 | */ |
||
93 | function expand_wp_insert_post( $args ) { |
||
96 | |||
97 | function filter_blacklisted_post_types( $args ) { |
||
106 | |||
107 | // Meta |
||
108 | function filter_meta( $args ) { |
||
115 | |||
116 | function is_whitelisted_post_meta( $meta_key ) { |
||
120 | |||
121 | function is_post_type_allowed( $post_id ) { |
||
122 | $post = get_post( $post_id ); |
||
123 | |||
124 | return ! in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ); |
||
125 | } |
||
126 | |||
127 | View Code Duplication | function remove_embed() { |
|
128 | global $wp_embed; |
||
129 | remove_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 ); |
||
130 | // remove the embed shortcode since we would do the part later. |
||
131 | remove_shortcode( 'embed' ); |
||
132 | // Attempts to embed all URLs in a post |
||
133 | remove_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 ); |
||
134 | } |
||
135 | |||
136 | View Code Duplication | function add_embed() { |
|
137 | global $wp_embed; |
||
138 | add_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 ); |
||
139 | // Shortcode placeholder for strip_shortcodes() |
||
140 | add_shortcode( 'embed', '__return_false' ); |
||
141 | // Attempts to embed all URLs in a post |
||
142 | add_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 ); |
||
143 | } |
||
144 | |||
145 | // Expands wp_insert_post to include filtered content |
||
146 | function filter_post_content_and_add_links( $post_object ) { |
||
147 | global $post; |
||
148 | $post = $post_object; |
||
149 | |||
150 | // return non existant post |
||
151 | $post_type = get_post_type_object( $post->post_type ); |
||
152 | View Code Duplication | if ( empty( $post_type ) || ! is_object( $post_type ) ) { |
|
153 | $non_existant_post = new stdClass(); |
||
154 | $non_existant_post->ID = $post->ID; |
||
155 | $non_existant_post->post_modified = $post->post_modified; |
||
156 | $non_existant_post->post_modified_gmt = $post->post_modified_gmt; |
||
157 | $non_existant_post->post_status = 'jetpack_sync_non_registered_post_type'; |
||
158 | |||
159 | return $non_existant_post; |
||
160 | } |
||
161 | /** |
||
162 | * Filters whether to prevent sending post data to .com |
||
163 | * |
||
164 | * Passing true to the filter will prevent the post data from being sent |
||
165 | * to the WordPress.com. |
||
166 | * Instead we pass data that will still enable us to do a checksum against the |
||
167 | * Jetpacks data but will prevent us from displaying the data on in the API as well as |
||
168 | * other services. |
||
169 | * @since 4.2.0 |
||
170 | * |
||
171 | * @param boolean false prevent post data from being synced to WordPress.com |
||
172 | * @param mixed $post WP_POST object |
||
173 | */ |
||
174 | View Code Duplication | if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) { |
|
175 | // We only send the bare necessary object to be able to create a checksum. |
||
176 | $blocked_post = new stdClass(); |
||
177 | $blocked_post->ID = $post->ID; |
||
178 | $blocked_post->post_modified = $post->post_modified; |
||
179 | $blocked_post->post_modified_gmt = $post->post_modified_gmt; |
||
180 | $blocked_post->post_status = 'jetpack_sync_blocked'; |
||
181 | |||
182 | return $blocked_post; |
||
183 | } |
||
184 | |||
185 | // lets not do oembed just yet. |
||
186 | $this->remove_embed(); |
||
187 | |||
188 | if ( 0 < strlen( $post->post_password ) ) { |
||
189 | $post->post_password = 'auto-' . wp_generate_password( 10, false ); |
||
190 | } |
||
191 | |||
192 | /** This filter is already documented in core. wp-includes/post-template.php */ |
||
193 | if ( Jetpack_Sync_Settings::get_setting( 'render_filtered_content' ) && $post_type->public ) { |
||
194 | global $shortcode_tags; |
||
195 | /** |
||
196 | * Filter prevents some shortcodes from expanding. |
||
197 | * |
||
198 | * Since we can can expand some type of shortcode better on the .com side and make the |
||
199 | * expansion more relevant to contexts. For example [galleries] and subscription emails |
||
200 | * |
||
201 | * @since 4.5.0 |
||
202 | * |
||
203 | * @param array of shortcode tags to remove. |
||
204 | */ |
||
205 | $shortcodes_to_remove = apply_filters( 'jetpack_sync_do_not_expand_shortcodes', array( |
||
206 | 'gallery', |
||
207 | 'slideshow' |
||
208 | ) ); |
||
209 | $removed_shortcode_callbacks = array(); |
||
210 | foreach ( $shortcodes_to_remove as $shortcode ) { |
||
211 | if ( isset ( $shortcode_tags[ $shortcode ] ) ) { |
||
212 | $removed_shortcode_callbacks[ $shortcode ] = $shortcode_tags[ $shortcode ]; |
||
213 | } |
||
214 | } |
||
215 | |||
216 | array_map( 'remove_shortcode', array_keys( $removed_shortcode_callbacks ) ); |
||
217 | |||
218 | $post->post_content_filtered = apply_filters( 'the_content', $post->post_content ); |
||
219 | $post->post_excerpt_filtered = apply_filters( 'the_excerpt', $post->post_excerpt ); |
||
220 | |||
221 | foreach ( $removed_shortcode_callbacks as $shortcode => $callback ) { |
||
222 | add_shortcode( $shortcode, $callback ); |
||
223 | } |
||
224 | } |
||
225 | |||
226 | $this->add_embed(); |
||
227 | |||
228 | if ( has_post_thumbnail( $post->ID ) ) { |
||
229 | $image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); |
||
230 | if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) { |
||
231 | $post->featured_image = $image_attributes[0]; |
||
232 | } |
||
233 | } |
||
234 | |||
235 | $post->permalink = get_permalink( $post->ID ); |
||
236 | $post->shortlink = wp_get_shortlink( $post->ID ); |
||
237 | |||
238 | return $post; |
||
239 | } |
||
240 | |||
241 | public function save_published( $new_status, $old_status, $post ) { |
||
242 | if ( 'publish' === $new_status && 'publish' !== $old_status ) { |
||
243 | $this->just_published[] = $post->ID; |
||
244 | } |
||
245 | } |
||
246 | |||
247 | public function wp_insert_post( $post_ID, $post, $update ) { |
||
248 | call_user_func( $this->action_handler, $post_ID, $post, $update ); |
||
249 | $this->send_published( $post_ID, $post ); |
||
250 | } |
||
251 | |||
252 | public function send_published( $post_ID, $post ) { |
||
253 | if ( ! in_array( $post_ID, $this->just_published ) ) { |
||
254 | return; |
||
255 | } |
||
256 | |||
257 | // Post revisions cause race conditions where this send_published add the action before the actual post gets synced |
||
258 | if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
||
259 | return; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Filter that is used to add to the post flags ( meta data ) when a post gets published |
||
264 | * |
||
265 | * @since 4.4.0 |
||
266 | * |
||
267 | * @param mixed array post flags that are added to the post |
||
268 | * @param mixed $post WP_POST object |
||
269 | */ |
||
270 | $flags = apply_filters( 'jetpack_published_post_flags', array(), $post ); |
||
271 | |||
272 | /** |
||
273 | * Action that gets synced when a post type gets published. |
||
274 | * |
||
275 | * @since 4.4.0 |
||
276 | * |
||
277 | * @param int $post_ID |
||
278 | * @param mixed array $flags post flags that are added to the post |
||
279 | */ |
||
280 | do_action( 'jetpack_published_post', $post_ID, $flags ); |
||
281 | |||
282 | $this->just_published = array_diff( $this->just_published, array( $post_ID ) ); |
||
283 | } |
||
284 | |||
285 | public function expand_post_ids( $args ) { |
||
298 | } |
||
299 |