Completed
Push — add/sync-rest-2 ( 0f1049...2638b5 )
by
unknown
09:02
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
class Jetpack_Sync_Posts {
4
5
	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...
6
	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...
7
8
	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...
9
	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...
10
11
	static function init() {
12
13
		add_action( 'transition_post_status', array( __CLASS__, 'transition_post_status' ), 10, 3 );
14
		add_action( 'delete_post', array( __CLASS__, 'delete_post' ) );
15
16
		// Mark the post as needs updating when post meta data changes.
17
		add_action( 'added_post_meta', array( __CLASS__, 'update_post_meta' ), 10, 4 );
18
		add_action( 'updated_postmeta', array( __CLASS__, 'update_post_meta' ), 10, 4 );
19
		add_action( 'deleted_post_meta', array( __CLASS__, 'update_post_meta' ), 10, 4 );
20
21
		// Mark the post as needs updating when taxonomies get added to it.
22
		add_action( 'set_object_terms', array( __CLASS__, 'set_object_terms' ), 10, 6 );
23
24
		// Update comment count
25
		add_action( 'wp_update_comment_count', array( __CLASS__, 'wp_update_comment_count' ), 10, 3 );
26
27
		// Sync post when the cache is cleared
28
		add_action( 'clean_post_cache', array( __CLASS__, 'clear_post_cache' ), 10, 2 );
29
	}
30
31
	static function transition_post_status( $new_status, $old_status, $post ) {
32
		self::sync( $post->ID );
33
	}
34
35
	static function sync( $post_id ) {
36
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
37
			return;
38
		}
39
		self::$sync[] = $post_id;
40
		Jetpack_Sync::schedule_sync();
41
	}
42
43
	static function delete_post( $post_id ) {
44
		self::$delete[] = $post_id;
45
		Jetpack_Sync::schedule_sync();
46
	}
47
48
	/**
49
	 * added_post_meta, update_post_meta, delete_post_meta
50
	 */
51
	static function update_post_meta( $meta_id, $post_id, $meta_key, $_meta_value ) {
0 ignored issues
show
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...
52
		$ignore_meta_keys = array( '_edit_lock', '_pingme', '_encloseme' );
53
		if ( in_array( $meta_key, $ignore_meta_keys ) ) {
54
			return;
55
		}
56
		self::sync( $post_id );
57
	}
58
59
	/**
60
	 * Updates to taxonomies such as categories, etc
61
	 */
62
	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...
63
		self::sync( $object_id );
64
	}
65
66
	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...
67
		self::sync( $post_id );
68
	}
69
70
	static function wp_update_comment_count( $post_id, $new, $old ) {
0 ignored issues
show
Unused Code introduced by
The parameter $new 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 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...
71
		self::sync( $post_id );
72
	}
73
74
	static function get_post_ids_that_changed() {
75
		return Jetpack_Sync::slice_ids( self::$sync, self::$max_to_sync, self::$que_option_name );
76
	}
77
	
78
	static function get_synced_post_types() {
79
		$allowed_post_types = array();
80
		foreach ( get_post_types( array(), 'objects' ) as $post_type => $post_type_object ) {
81
			if ( post_type_supports( $post_type, 'comments' ) ||
82
			     post_type_supports( $post_type, 'publicize' ) ||
83
			     $post_type_object->public ) {
84
				$allowed_post_types[] = $post_type;
85
			}
86
		}
87
		$allowed_post_types = apply_filters( 'jetpack_post_sync_post_type', $allowed_post_types );
88
		return array_diff( $allowed_post_types, array( 'revision' ) );
89
	}
90
91
	static function get_synced_post_status() {
92
		$allowed_post_stati = apply_filters( 'jetpack_post_sync_post_status', get_post_stati() );
93
		return array_diff( $allowed_post_stati, array( 'auto-draft' ) );
94
	}
95
96
	static function posts_to_sync() {
97
		$allowed_post_types = self::get_synced_post_types();
98
		$allowed_post_statuses = self::get_synced_post_status();
99
100
		$global_post     = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
101
		$GLOBALS['post'] = null;
102
103
		$posts = array();
104
		foreach ( self::get_post_ids_that_changed() as $post_id ) {
105
			$sync_post = self::get_post( $post_id, $allowed_post_types, $allowed_post_statuses );
106
			if ( $sync_post !== false ) {
107
				$posts[ $post_id ] = $sync_post;
108
			}
109
		}
110
		$GLOBALS['post'] = $global_post;
111
		unset( $global_post );
112
113
		return $posts;
114
	}
115
116
	static function get_post( $post_id, $allowed_post_types = array(), $allowed_post_statuses = array() ) {
117
		$post_obj = get_post( $post_id );
118
		if ( ! $post_obj ) {
119
			return false;
120
		}
121
122
		if ( is_null( $allowed_post_types ) ) {
123
			$allowed_post_types = self::get_synced_post_types();
124
		}
125
		if ( is_null( $allowed_post_types ) ) {
126
			$allowed_post_statuses = self::get_synced_post_status();
127
		}
128
129
		if ( ! in_array( $post_obj->post_type, $allowed_post_types ) ) {
130
			return false;
131
		}
132
133
		if ( ! in_array( $post_obj->post_status, $allowed_post_statuses ) ) {
134
			return false;
135
		}
136
137
		if ( is_callable( $post_obj, 'to_array' ) ) {
138
			// WP >= 3.5
139
			$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...
140
		} else {
141
			// WP < 3.5
142
			$post = get_object_vars( $post_obj );
143
		}
144
145
		if ( 0 < strlen( $post['post_password'] ) ) {
146
			$post['post_password'] = 'auto-' . wp_generate_password( 10, false ); // We don't want the real password.  Just pass something random.
147
		}
148
149
		// local optimizations
150
		unset(
151
			$post['filter'],
152
			$post['ancestors'],
153
			$post['post_content_filtered'],
154
			$post['to_ping'],
155
			$post['pinged']
156
		);
157
158
		if ( self::is_post_public( $post ) ) {
159
			$post['post_is_public'] = Jetpack_Options::get_option( 'public' );
160
		} else {
161
			//obscure content
162
			$post['post_content']   = '';
163
			$post['post_excerpt']   = '';
164
			$post['post_is_public'] = false;
165
		}
166
		$post_type_obj                        = get_post_type_object( $post['post_type'] );
167
		$post['post_is_excluded_from_search'] = $post_type_obj->exclude_from_search;
168
169
		$post['tax'] = array();
170
		$taxonomies  = get_object_taxonomies( $post_obj );
171
		foreach ( $taxonomies as $taxonomy ) {
172
			$terms = get_object_term_cache( $post_obj->ID, $taxonomy );
173
			if ( empty( $terms ) ) {
174
				$terms = wp_get_object_terms( $post_obj->ID, $taxonomy );
175
			}
176
			$term_names = array();
177
			foreach ( $terms as $term ) {
178
				$term_names[] = $term->name;
179
			}
180
			$post['tax'][ $taxonomy ] = $term_names;
181
		}
182
		
183
		$meta         = get_post_meta( $post_obj->ID, false );
184
		$post['meta'] = array();
185
		foreach ( $meta as $key => $value ) {
186
			$post['meta'][ $key ] = array_map( 'maybe_unserialize', $value );
187
		}
188
189
		$post['extra'] = array(
190
			'author'                  => get_the_author_meta( 'display_name', $post_obj->post_author ),
191
			'author_email'            => get_the_author_meta( 'email', $post_obj->post_author ),
192
			'dont_email_post_to_subs' => get_post_meta( $post_obj->ID, '_jetpack_dont_email_post_to_subs', true ),
193
		);
194
195
		if ( $attachment_id = get_post_thumbnail_id( $post_id ) ) {
196
			$feature = wp_get_attachment_image_src( $attachment_id, 'large' );
197
			if ( ! empty( $feature[0] ) ) {
198
				$post['extra']['featured_image'] = $feature[0];
199
			}
200
201
			$attachment = get_post( $attachment_id );
202
			if ( ! empty( $attachment ) ) {
203
				$metadata = wp_get_attachment_metadata( $attachment_id );
204
205
				$post['extra']['post_thumbnail'] = array(
206
					'ID'        => (int) $attachment_id,
207
					'URL'       => (string) wp_get_attachment_url( $attachment_id ),
208
					'guid'      => (string) $attachment->guid,
209
					'mime_type' => (string) $attachment->post_mime_type,
210
					'width'     => (int) isset( $metadata['width'] ) ? $metadata['width'] : 0,
211
					'height'    => (int) isset( $metadata['height'] ) ? $metadata['height'] : 0,
212
				);
213
214
				if ( isset( $metadata['duration'] ) ) {
215
					$post['extra']['post_thumbnail'] = (int) $metadata['duration'];
216
				}
217
218
				/**
219
				 * Filters the Post Thumbnail information returned for a specific post.
220
				 *
221
				 * @since 3.3.0
222
				 *
223
				 * @param array $post ['extra']['post_thumbnail'] {
224
				 *    Array of details about the Post Thumbnail.
225
				 * @param int ID Post Thumbnail ID.
226
				 * @param string URL Post thumbnail URL.
227
				 * @param string guid Post thumbnail guid.
228
				 * @param string mime_type Post thumbnail mime type.
229
				 * @param int width Post thumbnail width.
230
				 * @param int height Post thumbnail height.
231
				 * }
232
				 */
233
				$post['extra']['post_thumbnail'] = (object) apply_filters( 'get_attachment', $post['extra']['post_thumbnail'] );
234
			}
235
		}
236
237
		$post['permalink'] = get_permalink( $post_obj->ID );
238
		$post['shortlink'] = wp_get_shortlink( $post_obj->ID );
239
		/**
240
		 * Allow modules to send extra info on the sync post process.
241
		 *
242
		 * @since 2.8.0
243
		 *
244
		 * @param array $args Array of custom data to attach to a post.
245
		 * @param Object $post_obj Object returned by get_post() for a given post ID.
246
		 */
247
		$post['module_custom_data'] = apply_filters( 'jetpack_sync_post_module_custom_data', array(), $post_obj );
248
		$post['module_custom_data']['cpt_publicizeable'] = post_type_supports( $post_obj->post_type, 'publicize' ) ? true : false;
249
		return $post;
250
	}
251
252
	static function is_post_public( $post ) {
253
		if ( ! is_array( $post ) ) {
254
			$post = (array) $post;
255
		}
256
257
		if ( 0 < strlen( $post['post_password'] ) ) {
258
			return false;
259
		}
260
		if ( ! in_array( $post['post_type'], get_post_types( array( 'public' => true ) ) ) ) {
261
			return false;
262
		}
263
		$post_status = get_post_status( $post['ID'] ); // Inherited status is resolved here.
264
		if ( ! in_array( $post_status, get_post_stati( array( 'public' => true ) ) ) ) {
265
			return false;
266
		}
267
268
		return true;
269
	}
270
271
272
	static function posts_to_delete() {
273
		return array_unique( self::$delete );
274
	}
275
}
276