Completed
Push — add/sync-rest-2 ( 380c9a...e43e66 )
by
unknown
08:54
created

Jetpack_Sync_Posts::update_post_meta()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 4
1
<?php
2
3
4
class Jetpack_Sync_Posts {
5
6
	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...
7
	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...
8
9
	static function init() {
10
11
//		add_action( 'post_updated', array( 'Jetpack_Sync', 'sync_action' ), 0, 3 );
12
		add_action( 'post_updated', array( __CLASS__, 'post_updated' ), 0, 3 );
13
		add_action( 'transition_post_status', array( __CLASS__, 'transition_post_status' ), 10, 3 );
14
		add_action( 'deleted_post', array( 'Jetpack_Sync', 'sync_action' ) );
15
		// We should change this to 'attachment_updated' introduced in WP 4.4 once it's our latest WP version supported
16
		add_action( 'edit_attachment', array( __CLASS__, 'edit_attachment' ) );
17
		add_action( 'attachment_updated', array( 'Jetpack_Sync', 'sync_action' ) );
18
19
		add_action( 'add_attachment', array( __CLASS__, 'add_attachment' ) );
20
21
		// Mark the post as needs updating when taxonomies get added to it.
22
		add_action( 'set_object_terms', array( 'Jetpack_Sync', 'sync_action' ), 10, 6 );
23
24
		// Update comment count
25
		add_action( 'wp_update_comment_count', array( 'Jetpack_Sync', 'sync_action'  ), 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 post_updated( $post_ID, $post_after, $post_before ) {
32
		Jetpack_Sync::sync_action( 'post_updated', $post_ID, Jetpack_Sync::array_diff_assoc_recursive( (array)$post_after, (array)$post_before ) );
33
	}
34
35
	static function transition_post_status( $new_status, $old_status, $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...
36
		if ( $new_status !== $old_status ) {
37
			Jetpack_Sync::sync_action( 'transition_post_status', $new_status, $old_status );
38
		}
39
	}
40
41
	static function sync_attachment( $post_id ) {
42
		self::sync( $post_id );
0 ignored issues
show
Bug introduced by
The method sync() does not seem to exist on object<Jetpack_Sync_Posts>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
	}
44
45
	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...
46
		self::sync( $post_id );
0 ignored issues
show
Bug introduced by
The method sync() does not seem to exist on object<Jetpack_Sync_Posts>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
	}
48
49
	static function get_synced_post_types() {
50
		$allowed_post_types = array();
51
		foreach ( get_post_types( array(), 'objects' ) as $post_type => $post_type_object ) {
52
			if ( post_type_supports( $post_type, 'comments' ) ||
53
			     post_type_supports( $post_type, 'publicize' ) ||
54
			     $post_type_object->public
55
			) {
56
				$allowed_post_types[] = $post_type;
57
			}
58
		}
59
		$allowed_post_types = apply_filters( 'jetpack_post_sync_post_type', $allowed_post_types );
60
61
		return array_diff( $allowed_post_types, array( 'revision' ) );
62
	}
63
64
	static function get_synced_post_status() {
65
		$allowed_post_stati = apply_filters( 'jetpack_post_sync_post_status', get_post_stati() );
66
67
		return array_diff( $allowed_post_stati, array( 'auto-draft' ) );
68
	}
69
70
	static function get_post( $post_id, $allowed_post_types = array(), $allowed_post_statuses = array() ) {
71
		$post_obj = get_post( $post_id );
72
		if ( ! $post_obj ) {
73
			return false;
74
		}
75
76
		if ( is_null( $allowed_post_types ) ) {
77
			$allowed_post_types = self::get_synced_post_types();
78
		}
79
		if ( is_null( $allowed_post_types ) ) {
80
			$allowed_post_statuses = self::get_synced_post_status();
81
		}
82
83
		if ( ! in_array( $post_obj->post_type, $allowed_post_types ) ) {
84
			return false;
85
		}
86
87
		if ( ! in_array( $post_obj->post_status, $allowed_post_statuses ) ) {
88
			return false;
89
		}
90
91
		if ( is_callable( $post_obj, 'to_array' ) ) {
92
			// WP >= 3.5
93
			$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...
94
		} else {
95
			// WP < 3.5
96
			$post = get_object_vars( $post_obj );
97
		}
98
99
		if ( 0 < strlen( $post['post_password'] ) ) {
100
			$post['post_password'] = 'auto-' . wp_generate_password( 10, false ); // We don't want the real password.  Just pass something random.
101
		}
102
103
		// local optimizations
104
		unset(
105
			$post['filter'],
106
			$post['ancestors'],
107
			$post['post_content_filtered'],
108
			$post['to_ping'],
109
			$post['pinged']
110
		);
111
112
		if ( self::is_post_public( $post ) ) {
113
			$post['post_is_public'] = Jetpack_Options::get_option( 'public' );
114
		} else {
115
			//obscure content
116
			$post['post_content']   = '';
117
			$post['post_excerpt']   = '';
118
			$post['post_is_public'] = false;
119
		}
120
		$post_type_obj                        = get_post_type_object( $post['post_type'] );
121
		$post['post_is_excluded_from_search'] = $post_type_obj->exclude_from_search;
122
123
		$post['tax'] = array();
124
		$taxonomies  = get_object_taxonomies( $post_obj );
125
		foreach ( $taxonomies as $taxonomy ) {
126
			$terms = get_object_term_cache( $post_obj->ID, $taxonomy );
127
			if ( empty( $terms ) ) {
128
				$terms = wp_get_object_terms( $post_obj->ID, $taxonomy );
129
			}
130
			$term_names = array();
131
			foreach ( $terms as $term ) {
132
				$term_names[] = $term->name;
133
			}
134
			$post['tax'][ $taxonomy ] = $term_names;
135
		}
136
137
		$meta         = get_post_meta( $post_obj->ID, false );
138
		$post['meta'] = array();
139
		foreach ( $meta as $key => $value ) {
140
			$post['meta'][ $key ] = array_map( 'maybe_unserialize', $value );
141
		}
142
143
		$post['extra'] = array(
144
			'author'                  => get_the_author_meta( 'display_name', $post_obj->post_author ),
145
			'author_email'            => get_the_author_meta( 'email', $post_obj->post_author ),
146
			'dont_email_post_to_subs' => get_post_meta( $post_obj->ID, '_jetpack_dont_email_post_to_subs', true ),
147
		);
148
149
		if ( $attachment_id = get_post_thumbnail_id( $post_id ) ) {
150
			$feature = wp_get_attachment_image_src( $attachment_id, 'large' );
151
			if ( ! empty( $feature[0] ) ) {
152
				$post['extra']['featured_image'] = $feature[0];
153
			}
154
155
			$attachment = get_post( $attachment_id );
156
			if ( ! empty( $attachment ) ) {
157
				$metadata = wp_get_attachment_metadata( $attachment_id );
158
159
				$post['extra']['post_thumbnail'] = array(
160
					'ID'        => (int) $attachment_id,
161
					'URL'       => (string) wp_get_attachment_url( $attachment_id ),
162
					'guid'      => (string) $attachment->guid,
163
					'mime_type' => (string) $attachment->post_mime_type,
164
					'width'     => (int) isset( $metadata['width'] ) ? $metadata['width'] : 0,
165
					'height'    => (int) isset( $metadata['height'] ) ? $metadata['height'] : 0,
166
				);
167
168
				if ( isset( $metadata['duration'] ) ) {
169
					$post['extra']['post_thumbnail'] = (int) $metadata['duration'];
170
				}
171
172
				/**
173
				 * Filters the Post Thumbnail information returned for a specific post.
174
				 *
175
				 * @since 3.3.0
176
				 *
177
				 * @param array $post ['extra']['post_thumbnail'] {
178
				 *    Array of details about the Post Thumbnail.
179
				 * @param int ID Post Thumbnail ID.
180
				 * @param string URL Post thumbnail URL.
181
				 * @param string guid Post thumbnail guid.
182
				 * @param string mime_type Post thumbnail mime type.
183
				 * @param int width Post thumbnail width.
184
				 * @param int height Post thumbnail height.
185
				 * }
186
				 */
187
				$post['extra']['post_thumbnail'] = (object) apply_filters( 'get_attachment', $post['extra']['post_thumbnail'] );
188
			}
189
		}
190
191
		$post['permalink'] = get_permalink( $post_obj->ID );
192
		$post['shortlink'] = wp_get_shortlink( $post_obj->ID );
193
		/**
194
		 * Allow modules to send extra info on the sync post process.
195
		 *
196
		 * @since 2.8.0
197
		 *
198
		 * @param array $args Array of custom data to attach to a post.
199
		 * @param Object $post_obj Object returned by get_post() for a given post ID.
200
		 */
201
		$post['module_custom_data']                      = apply_filters( 'jetpack_sync_post_module_custom_data', array(), $post_obj );
202
		$post['module_custom_data']['cpt_publicizeable'] = post_type_supports( $post_obj->post_type, 'publicize' ) ? true : false;
203
204
		return $post;
205
	}
206
207
	static function is_post_public( $post ) {
208
		if ( ! is_array( $post ) ) {
209
			$post = (array) $post;
210
		}
211
212
		if ( 0 < strlen( $post['post_password'] ) ) {
213
			return false;
214
		}
215
		if ( ! in_array( $post['post_type'], get_post_types( array( 'public' => true ) ) ) ) {
216
			return false;
217
		}
218
		$post_status = get_post_status( $post['ID'] ); // Inherited status is resolved here.
219
		if ( ! in_array( $post_status, get_post_stati( array( 'public' => true ) ) ) ) {
220
			return false;
221
		}
222
223
		return true;
224
	}
225
226
}
227