Completed
Push — add/sync-rest-2 ( 5ec2d9...380c9a )
by
unknown
08:41
created

Jetpack_Sync_Posts::set_object_terms()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 6
1
<?php
2
3
4
class Jetpack_Sync_Posts {
5
6
	static $sync = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $sync.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
7
	static $sync_comment_count = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $sync_comment_count.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
8
	static $delete = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $delete.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
9
10
	static $max_to_sync = 10;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $max_to_sync.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
11
	static $que_option_name = 'jetpack_sync_post_ids_que';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $que_option_name.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
12
13
	static function init() {
14
15
		add_action( 'transition_post_status', array( __CLASS__, 'transition_post_status' ), 10, 3 );
16
		add_action( 'delete_post', array( __CLASS__, 'delete_post' ) );
17
		add_action( 'edit_attachment', array( __CLASS__, 'sync_attachment' ) );
18
		add_action( 'add_attachment', array( __CLASS__, 'sync_attachment' ) );
19
20
		// Mark the post as needs updating when taxonomies get added to it.
21
		add_action( 'set_object_terms', array( __CLASS__, 'set_object_terms' ), 10, 6 );
22
23
		// Update comment count
24
		add_action( 'wp_update_comment_count', array( __CLASS__, 'wp_update_comment_count' ), 10, 3 );
25
26
		// Sync post when the cache is cleared
27
		// add_action( 'clean_post_cache', array( __CLASS__, 'clear_post_cache' ), 10, 2 );
28
	}
29
30
	static function transition_post_status( $new_status, $old_status, $post ) {
31
		if ( 'revision' !== $post->post_type ) {
32
			self::sync( $post->ID );
33
		}
34
	}
35
36
	static function sync_attachment( $post_id ) {
37
		self::sync( $post_id );
38
	}
39
40
	static function sync( $post_id ) {
41
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
42
			return;
43
		}
44
		// error_log( current_action() );
45
		self::$sync[] = $post_id;
46
		Jetpack_Sync::schedule_sync();
47
	}
48
49
	static function delete_post( $post_id ) {
50
		self::$delete[] = $post_id;
51
		Jetpack_Sync::schedule_sync();
52
	}
53
54
	/**
55
	 * added_post_meta, update_post_meta, delete_post_meta
56
	 */
57
	static function update_post_meta( $meta_id, $post_id, $meta_key, $_meta_value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $meta_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $_meta_value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
		$ignore_meta_keys = array( '_edit_lock', '_pingme', '_encloseme' );
59
		if ( in_array( $meta_key, $ignore_meta_keys ) ) {
60
			return;
61
		}
62
		self::sync( $post_id );
63
	}
64
65
	/**
66
	 * Updates to taxonomies such as categories, etc
67
	 */
68
	static function set_object_terms( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {
0 ignored issues
show
Unused Code introduced by
The parameter $terms is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $tt_ids is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $taxonomy is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $append is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $old_tt_ids is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
		self::sync( $object_id );
70
	}
71
72
	static function clear_post_cache( $post_id, $post ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
73
		self::sync( $post_id );
74
	}
75
76
	static function wp_update_comment_count( $post_id, $new, $old ) {
0 ignored issues
show
Unused Code introduced by
The parameter $old is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
77
		self::$sync_comment_count[ $post_id ] = $new;
78
		Jetpack_Sync::schedule_sync();
79
	}
80
81
	static function get_post_ids_that_changed() {
82
		return Jetpack_Sync::slice_ids( self::$sync, self::$max_to_sync, self::$que_option_name );
83
	}
84
85
	static function get_synced_post_types() {
86
		$allowed_post_types = array();
87
		foreach ( get_post_types( array(), 'objects' ) as $post_type => $post_type_object ) {
88
			if ( post_type_supports( $post_type, 'comments' ) ||
89
			     post_type_supports( $post_type, 'publicize' ) ||
90
			     $post_type_object->public
91
			) {
92
				$allowed_post_types[] = $post_type;
93
			}
94
		}
95
		$allowed_post_types = apply_filters( 'jetpack_post_sync_post_type', $allowed_post_types );
96
97
		return array_diff( $allowed_post_types, array( 'revision' ) );
98
	}
99
100
	static function get_synced_post_status() {
101
		$allowed_post_stati = apply_filters( 'jetpack_post_sync_post_status', get_post_stati() );
102
103
		return array_diff( $allowed_post_stati, array( 'auto-draft' ) );
104
	}
105
106
	static function posts_to_sync() {
107
		$allowed_post_types    = self::get_synced_post_types();
108
		$allowed_post_statuses = self::get_synced_post_status();
109
110
		$global_post     = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
111
		$GLOBALS['post'] = null;
112
113
		$posts = array();
114
		foreach ( self::get_post_ids_that_changed() as $post_id ) {
115
			$sync_post = self::get_post( $post_id, $allowed_post_types, $allowed_post_statuses );
116
			if ( $sync_post !== false ) {
117
				$posts[ $post_id ] = $sync_post;
118
			}
119
		}
120
		$GLOBALS['post'] = $global_post;
121
		unset( $global_post );
122
123
		return $posts;
124
	}
125
126
	static function post_comment_count_to_sync() {
127
		return self::$sync_comment_count;
128
	}
129
130
	static function posts_to_delete() {
131
		return array_unique( self::$delete );
132
	}
133
134
	static function get_post( $post_id, $allowed_post_types = array(), $allowed_post_statuses = array() ) {
135
		$post_obj = get_post( $post_id );
136
		if ( ! $post_obj ) {
137
			return false;
138
		}
139
140
		if ( is_null( $allowed_post_types ) ) {
141
			$allowed_post_types = self::get_synced_post_types();
142
		}
143
		if ( is_null( $allowed_post_types ) ) {
144
			$allowed_post_statuses = self::get_synced_post_status();
145
		}
146
147
		if ( ! in_array( $post_obj->post_type, $allowed_post_types ) ) {
148
			return false;
149
		}
150
151
		if ( ! in_array( $post_obj->post_status, $allowed_post_statuses ) ) {
152
			return false;
153
		}
154
155
		if ( is_callable( $post_obj, 'to_array' ) ) {
156
			// WP >= 3.5
157
			$post = $post_obj->to_array();
0 ignored issues
show
Bug introduced by
The method to_array cannot be called on $post_obj (of type callable).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
158
		} else {
159
			// WP < 3.5
160
			$post = get_object_vars( $post_obj );
161
		}
162
163
		if ( 0 < strlen( $post['post_password'] ) ) {
164
			$post['post_password'] = 'auto-' . wp_generate_password( 10, false ); // We don't want the real password.  Just pass something random.
165
		}
166
167
		// local optimizations
168
		unset(
169
			$post['filter'],
170
			$post['ancestors'],
171
			$post['post_content_filtered'],
172
			$post['to_ping'],
173
			$post['pinged']
174
		);
175
176
		if ( self::is_post_public( $post ) ) {
177
			$post['post_is_public'] = Jetpack_Options::get_option( 'public' );
178
		} else {
179
			//obscure content
180
			$post['post_content']   = '';
181
			$post['post_excerpt']   = '';
182
			$post['post_is_public'] = false;
183
		}
184
		$post_type_obj                        = get_post_type_object( $post['post_type'] );
185
		$post['post_is_excluded_from_search'] = $post_type_obj->exclude_from_search;
186
187
		$post['tax'] = array();
188
		$taxonomies  = get_object_taxonomies( $post_obj );
189
		foreach ( $taxonomies as $taxonomy ) {
190
			$terms = get_object_term_cache( $post_obj->ID, $taxonomy );
191
			if ( empty( $terms ) ) {
192
				$terms = wp_get_object_terms( $post_obj->ID, $taxonomy );
193
			}
194
			$term_names = array();
195
			foreach ( $terms as $term ) {
196
				$term_names[] = $term->name;
197
			}
198
			$post['tax'][ $taxonomy ] = $term_names;
199
		}
200
201
		$meta         = get_post_meta( $post_obj->ID, false );
202
		$post['meta'] = array();
203
		foreach ( $meta as $key => $value ) {
204
			$post['meta'][ $key ] = array_map( 'maybe_unserialize', $value );
205
		}
206
207
		$post['extra'] = array(
208
			'author'                  => get_the_author_meta( 'display_name', $post_obj->post_author ),
209
			'author_email'            => get_the_author_meta( 'email', $post_obj->post_author ),
210
			'dont_email_post_to_subs' => get_post_meta( $post_obj->ID, '_jetpack_dont_email_post_to_subs', true ),
211
		);
212
213
		if ( $attachment_id = get_post_thumbnail_id( $post_id ) ) {
214
			$feature = wp_get_attachment_image_src( $attachment_id, 'large' );
215
			if ( ! empty( $feature[0] ) ) {
216
				$post['extra']['featured_image'] = $feature[0];
217
			}
218
219
			$attachment = get_post( $attachment_id );
220
			if ( ! empty( $attachment ) ) {
221
				$metadata = wp_get_attachment_metadata( $attachment_id );
222
223
				$post['extra']['post_thumbnail'] = array(
224
					'ID'        => (int) $attachment_id,
225
					'URL'       => (string) wp_get_attachment_url( $attachment_id ),
226
					'guid'      => (string) $attachment->guid,
227
					'mime_type' => (string) $attachment->post_mime_type,
228
					'width'     => (int) isset( $metadata['width'] ) ? $metadata['width'] : 0,
229
					'height'    => (int) isset( $metadata['height'] ) ? $metadata['height'] : 0,
230
				);
231
232
				if ( isset( $metadata['duration'] ) ) {
233
					$post['extra']['post_thumbnail'] = (int) $metadata['duration'];
234
				}
235
236
				/**
237
				 * Filters the Post Thumbnail information returned for a specific post.
238
				 *
239
				 * @since 3.3.0
240
				 *
241
				 * @param array $post ['extra']['post_thumbnail'] {
242
				 *    Array of details about the Post Thumbnail.
243
				 * @param int ID Post Thumbnail ID.
244
				 * @param string URL Post thumbnail URL.
245
				 * @param string guid Post thumbnail guid.
246
				 * @param string mime_type Post thumbnail mime type.
247
				 * @param int width Post thumbnail width.
248
				 * @param int height Post thumbnail height.
249
				 * }
250
				 */
251
				$post['extra']['post_thumbnail'] = (object) apply_filters( 'get_attachment', $post['extra']['post_thumbnail'] );
252
			}
253
		}
254
255
		$post['permalink'] = get_permalink( $post_obj->ID );
256
		$post['shortlink'] = wp_get_shortlink( $post_obj->ID );
257
		/**
258
		 * Allow modules to send extra info on the sync post process.
259
		 *
260
		 * @since 2.8.0
261
		 *
262
		 * @param array $args Array of custom data to attach to a post.
263
		 * @param Object $post_obj Object returned by get_post() for a given post ID.
264
		 */
265
		$post['module_custom_data']                      = apply_filters( 'jetpack_sync_post_module_custom_data', array(), $post_obj );
266
		$post['module_custom_data']['cpt_publicizeable'] = post_type_supports( $post_obj->post_type, 'publicize' ) ? true : false;
267
268
		return $post;
269
	}
270
271
	static function is_post_public( $post ) {
272
		if ( ! is_array( $post ) ) {
273
			$post = (array) $post;
274
		}
275
276
		if ( 0 < strlen( $post['post_password'] ) ) {
277
			return false;
278
		}
279
		if ( ! in_array( $post['post_type'], get_post_types( array( 'public' => true ) ) ) ) {
280
			return false;
281
		}
282
		$post_status = get_post_status( $post['ID'] ); // Inherited status is resolved here.
283
		if ( ! in_array( $post_status, get_post_stati( array( 'public' => true ) ) ) ) {
284
			return false;
285
		}
286
287
		return true;
288
	}
289
290
}
291