Completed
Push — add/accept-invite-endpoint ( a292c1...74729c )
by
unknown
15:13 queued 06:00
created

Jetpack_Sync_Module_Posts   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 326
Duplicated Lines 16.26 %

Coupling/Cohesion

Components 3
Dependencies 2

Importance

Changes 0
Metric Value
dl 53
loc 326
rs 6.8
c 0
b 0
f 0
wmc 55
lcom 3
cbo 2

23 Methods

Rating   Name   Duplication   Size   Complexity  
A init_full_sync_listeners() 0 3 1
A init_before_send() 0 6 1
A enqueue_full_sync_actions() 0 5 1
A estimate_full_sync_actions() 8 8 1
A get_where_sql() 10 10 2
A get_full_sync_actions() 0 3 1
A expand_wp_insert_post() 0 3 1
A filter_blacklisted_post_types() 0 9 2
A filter_meta() 0 7 3
A is_whitelisted_post_meta() 0 4 2
A is_post_type_allowed() 0 5 1
A remove_embed() 8 8 1
A add_embed() 8 8 1
D filter_post_content_and_add_links() 19 94 13
A name() 0 3 1
A get_object_by_id() 0 7 3
A set_defaults() 0 2 1
A init_listeners() 0 20 2
B save_published() 0 9 5
A wp_insert_post() 0 8 3
B send_published() 0 32 4
A send_trashed() 0 21 4
A expand_post_ids() 0 13 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

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 Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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
2
3
require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php';
4
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
11
	public function name() {
12
		return 'posts';
13
	}
14
15
	public function get_object_by_id( $object_type, $id ) {
16
		if ( $object_type === 'post' && $post = get_post( intval( $id ) ) ) {
17
			return $this->filter_post_content_and_add_links( $post );
18
		}
19
20
		return false;
21
	}
22
23
	public function set_defaults() {
24
	}
25
26
	public function init_listeners( $callable ) {
27
		$this->action_handler = $callable;
28
29
		// Core < 4.7 doesn't deal with nested wp_insert_post calls very well
30
		global $wp_version;
31
		$priority = version_compare( $wp_version, '4.7-alpha', '<' ) ? 0 : 11;
32
33
		add_action( 'wp_insert_post', array( $this, 'wp_insert_post' ), $priority, 3 );
34
35
		add_action( 'deleted_post', $callable, 10 );
36
		add_action( 'jetpack_published_post', $callable, 10, 2 );
37
		add_action( 'jetpack_trashed_post', $callable, 10, 2 );
38
39
		add_action( 'transition_post_status', array( $this, 'save_published' ), 10, 3 );
40
		add_filter( 'jetpack_sync_before_enqueue_wp_insert_post', array( $this, 'filter_blacklisted_post_types' ) );
41
42
		// listen for meta changes
43
		$this->init_listeners_for_meta_type( 'post', $callable );
44
		$this->init_meta_whitelist_handler( 'post', array( $this, 'filter_meta' ) );
45
	}
46
47
	public function init_full_sync_listeners( $callable ) {
48
		add_action( 'jetpack_full_sync_posts', $callable ); // also sends post meta
49
	}
50
51
	public function init_before_send() {
52
		add_filter( 'jetpack_sync_before_send_wp_insert_post', array( $this, 'expand_wp_insert_post' ) );
53
54
		// full sync
55
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_posts', array( $this, 'expand_post_ids' ) );
56
	}
57
58
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
59
		global $wpdb;
60
61
		return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_posts', $wpdb->posts, 'ID', $this->get_where_sql( $config ), $max_items_to_enqueue, $state );
62
	}
63
64 View Code Duplication
	public function estimate_full_sync_actions( $config ) {
65
		global $wpdb;
66
67
		$query = "SELECT count(*) FROM $wpdb->posts WHERE " . $this->get_where_sql( $config );
68
		$count = $wpdb->get_var( $query );
69
70
		return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
71
	}
72
73 View Code Duplication
	private function get_where_sql( $config ) {
74
		$where_sql = Jetpack_Sync_Settings::get_blacklisted_post_types_sql();
75
76
		// config is a list of post IDs to sync
77
		if ( is_array( $config ) ) {
78
			$where_sql .= ' AND ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
79
		}
80
81
		return $where_sql;
82
	}
83
84
	function get_full_sync_actions() {
85
		return array( 'jetpack_full_sync_posts' );
86
	}
87
88
	/**
89
	 * Process content before send
90
	 *
91
	 * @param array $args wp_insert_post arguments
92
	 *
93
	 * @return array
94
	 */
95
	function expand_wp_insert_post( $args ) {
96
		return array( $args[0], $this->filter_post_content_and_add_links( $args[1] ), $args[2] );
97
	}
98
99
	function filter_blacklisted_post_types( $args ) {
100
		$post = $args[1];
101
102
		if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) {
103
			return false;
104
		}
105
106
		return $args;
107
	}
108
109
	// Meta
110
	function filter_meta( $args ) {
111
		if ( $this->is_post_type_allowed( $args[1] ) && $this->is_whitelisted_post_meta( $args[2] ) ) {
112
			return $args;
113
		}
114
115
		return false;
116
	}
117
118
	function is_whitelisted_post_meta( $meta_key ) {
119
		// _wpas_skip_ is used by publicize
120
		return in_array( $meta_key, Jetpack_Sync_Settings::get_setting( 'post_meta_whitelist' ) ) || wp_startswith( $meta_key, '_wpas_skip_' );
121
	}
122
123
	function is_post_type_allowed( $post_id ) {
124
		$post = get_post( $post_id );
125
126
		return ! in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) );
127
	}
128
129 View Code Duplication
	function remove_embed() {
130
		global $wp_embed;
131
		remove_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 );
132
		// remove the embed shortcode since we would do the part later.
133
		remove_shortcode( 'embed' );
134
		// Attempts to embed all URLs in a post
135
		remove_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 );
136
	}
137
138 View Code Duplication
	function add_embed() {
139
		global $wp_embed;
140
		add_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 );
141
		// Shortcode placeholder for strip_shortcodes()
142
		add_shortcode( 'embed', '__return_false' );
143
		// Attempts to embed all URLs in a post
144
		add_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 );
145
	}
146
147
	// Expands wp_insert_post to include filtered content
148
	function filter_post_content_and_add_links( $post_object ) {
149
		global $post;
150
		$post = $post_object;
151
152
		// return non existant post 
153
		$post_type = get_post_type_object( $post->post_type );
154 View Code Duplication
		if ( empty( $post_type ) || ! is_object( $post_type ) ) {
155
			$non_existant_post                    = new stdClass();
156
			$non_existant_post->ID                = $post->ID;
157
			$non_existant_post->post_modified     = $post->post_modified;
158
			$non_existant_post->post_modified_gmt = $post->post_modified_gmt;
159
			$non_existant_post->post_status       = 'jetpack_sync_non_registered_post_type';
160
161
			return $non_existant_post;
162
		}
163
		/**
164
		 * Filters whether to prevent sending post data to .com
165
		 *
166
		 * Passing true to the filter will prevent the post data from being sent
167
		 * to the WordPress.com.
168
		 * Instead we pass data that will still enable us to do a checksum against the
169
		 * Jetpacks data but will prevent us from displaying the data on in the API as well as
170
		 * other services.
171
		 * @since 4.2.0
172
		 *
173
		 * @param boolean false prevent post data from being synced to WordPress.com
174
		 * @param mixed $post WP_POST object
175
		 */
176 View Code Duplication
		if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) {
177
			// We only send the bare necessary object to be able to create a checksum.
178
			$blocked_post                    = new stdClass();
179
			$blocked_post->ID                = $post->ID;
180
			$blocked_post->post_modified     = $post->post_modified;
181
			$blocked_post->post_modified_gmt = $post->post_modified_gmt;
182
			$blocked_post->post_status       = 'jetpack_sync_blocked';
183
184
			return $blocked_post;
185
		}
186
187
		// lets not do oembed just yet.
188
		$this->remove_embed();
189
190
		if ( 0 < strlen( $post->post_password ) ) {
191
			$post->post_password = 'auto-' . wp_generate_password( 10, false );
192
		}
193
194
		/** This filter is already documented in core. wp-includes/post-template.php */
195
		if ( Jetpack_Sync_Settings::get_setting( 'render_filtered_content' ) && $post_type->public ) {
196
			global $shortcode_tags;
197
			/**
198
			 * Filter prevents some shortcodes from expanding.
199
			 *
200
			 * Since we can can expand some type of shortcode better on the .com side and make the
201
			 * expansion more relevant to contexts. For example [galleries] and subscription emails
202
			 *
203
			 * @since 4.5.0
204
			 *
205
			 * @param array of shortcode tags to remove.
206
			 */
207
			$shortcodes_to_remove        = apply_filters( 'jetpack_sync_do_not_expand_shortcodes', array(
208
				'gallery',
209
				'slideshow'
210
			) );
211
			$removed_shortcode_callbacks = array();
212
			foreach ( $shortcodes_to_remove as $shortcode ) {
213
				if ( isset ( $shortcode_tags[ $shortcode ] ) ) {
214
					$removed_shortcode_callbacks[ $shortcode ] = $shortcode_tags[ $shortcode ];
215
				}
216
			}
217
218
			array_map( 'remove_shortcode', array_keys( $removed_shortcode_callbacks ) );
219
220
			$post->post_content_filtered = apply_filters( 'the_content', $post->post_content );
221
			$post->post_excerpt_filtered = apply_filters( 'the_excerpt', $post->post_excerpt );
222
223
			foreach ( $removed_shortcode_callbacks as $shortcode => $callback ) {
224
				add_shortcode( $shortcode, $callback );
225
			}
226
		}
227
228
		$this->add_embed();
229
230
		if ( has_post_thumbnail( $post->ID ) ) {
231
			$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
232
			if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) {
233
				$post->featured_image = $image_attributes[0];
234
			}
235
		}
236
237
		$post->permalink = get_permalink( $post->ID );
238
		$post->shortlink = wp_get_shortlink( $post->ID );
239
240
		return $post;
241
	}
242
243
	public function save_published( $new_status, $old_status, $post ) {
244
		if ( 'publish' === $new_status && 'publish' !== $old_status ) {
245
			$this->just_published[] = $post->ID;
246
		}
247
248
		if ( 'trash' === $new_status && 'trash' !== $old_status ) {
249
			$this->just_trashed[] = $post->ID;
250
		}
251
	}
252
253
	public function wp_insert_post( $post_ID, $post = null , $update = null ) {
254
		if ( ! is_numeric( $post_ID ) || is_null( $post ) ) {
255
			return;
256
		}
257
		call_user_func( $this->action_handler, $post_ID, $post, $update );
258
		$this->send_published( $post_ID, $post );
259
		$this->send_trashed( $post_ID, $post );
260
	}
261
262
	public function send_published( $post_ID, $post ) {
263
		if ( ! in_array( $post_ID, $this->just_published ) ) {
264
			return;
265
		}
266
267
		// Post revisions cause race conditions where this send_published add the action before the actual post gets synced
268
		if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
269
			return;
270
		}
271
272
		/**
273
		 * Filter that is used to add to the post flags ( meta data ) when a post gets published
274
		 *
275
		 * @since 4.4.0
276
		 *
277
		 * @param mixed array post flags that are added to the post
278
		 * @param mixed $post WP_POST object
279
		 */
280
		$flags = apply_filters( 'jetpack_published_post_flags', array(), $post );
281
282
		/**
283
		 * Action that gets synced when a post type gets published.
284
		 *
285
		 * @since 4.4.0
286
		 *
287
		 * @param int $post_ID
288
		 * @param mixed array $flags post flags that are added to the post
289
		 */
290
		do_action( 'jetpack_published_post', $post_ID, $flags );
291
292
		$this->just_published = array_diff( $this->just_published, array( $post_ID ) );
293
	}
294
295
	public function send_trashed( $post_ID, $post ) {
296
		if ( ! in_array( $post_ID, $this->just_trashed ) ) {
297
			return;
298
		}
299
300
		// Post revisions cause race conditions where this send_published add the action before the actual post gets synced
301
		if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
302
			return;
303
		}
304
305
		/**
306
		 * Action that gets synced when a post type gets trashed.
307
		 *
308
		 * @since 4.9.0
309
		 *
310
		 * @param int $post_ID
311
		 */
312
		do_action( 'jetpack_trashed_post', $post_ID );
313
314
		$this->just_trashed = array_diff( $this->just_trashed, array( $post_ID ) );
315
	}
316
317
	public function expand_post_ids( $args ) {
318
		$post_ids = $args[0];
319
320
		$posts = array_filter( array_map( array( 'WP_Post', 'get_instance' ), $post_ids ) );
321
		$posts = array_map( array( $this, 'filter_post_content_and_add_links' ), $posts );
322
		$posts = array_values( $posts ); // reindex in case posts were deleted
323
324
		return array(
325
			$posts,
326
			$this->get_metadata( $post_ids, 'post', Jetpack_Sync_Settings::get_setting( 'post_meta_whitelist' ) ),
327
			$this->get_term_relationships( $post_ids ),
328
		);
329
	}
330
}
331