Completed
Push — add/sync-rest-2 ( e43e66...ada61b )
by
unknown
08:36
created

Jetpack_Sync_Posts   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 222
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 19
Bugs 0 Features 7
Metric Value
wmc 37
c 19
b 0
f 7
lcom 1
cbo 2
dl 0
loc 222
rs 8.6

9 Methods

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