Completed
Push — add/sync-rest-2 ( ada61b...a7dbc1 )
by
unknown
09:34
created

Jetpack_Sync_Posts   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 20
Bugs 0 Features 7
Metric Value
wmc 23
c 20
b 0
f 7
lcom 0
cbo 3
dl 0
loc 173
rs 10

7 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 clear_post_cache() 0 3 1
B get_synced_post_types() 0 16 5
A get_synced_post_status() 0 5 1
C get_post() 0 108 12
1
<?php
2
3
class Jetpack_Sync_Posts {
4
5
	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...
6
	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...
7
8
	static function init() {
9
10
		add_action( 'post_updated', array( 'Jetpack_Sync', 'sync_action' ), 0, 3 );
11
		add_action( 'transition_post_status', array( __CLASS__, 'transition_post_status' ), 10, 3 );
12
		add_action( 'deleted_post', array( 'Jetpack_Sync', 'sync_action' ) );
13
		// We should change this to 'attachment_updated' introduced in WP 4.4 once it's our latest WP version supported
14
		add_action( 'edit_attachment', array( __CLASS__, 'edit_attachment' ) );
15
		add_action( 'attachment_updated', array( 'Jetpack_Sync', 'sync_action' ) );
16
17
		add_action( 'add_attachment', array( __CLASS__, 'add_attachment' ) );
18
19
		// Mark the post as needs updating when taxonomies get added to it.
20
		add_action( 'set_object_terms', array( 'Jetpack_Sync', 'sync_action' ), 10, 6 );
21
22
		// Update comment count
23
		add_action( 'wp_update_comment_count', array( 'Jetpack_Sync', 'sync_action'  ), 10, 3 );
24
25
		// Sync post when the cache is cleared
26
		// add_action( 'clean_post_cache', array( __CLASS__, 'clear_post_cache' ), 10, 2 );
27
	}
28
29
	static function get_post_diff( $post_after, $post_before ) {
30
		return Jetpack_Sync::array_diff_assoc_recursive( (array)$post_after, (array)$post_before );
31
	}
32
33
	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...
34
		if ( $new_status !== $old_status ) {
35
			Jetpack_Sync::sync_action( 'transition_post_status', $new_status, $old_status );
36
		}
37
	}
38
39
	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...
40
		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...
41
	}
42
43
	static function get_synced_post_types() {
44
		$allowed_post_types = array();
45
		error_log('get_synced_post_adsfsafd');
46
		foreach ( get_post_types( array(), 'objects' ) as $post_type => $post_type_object ) {
47
			error_log( $post_type );
48
			if ( post_type_supports( $post_type, 'comments' ) ||
49
			     post_type_supports( $post_type, 'publicize' ) ||
50
			     $post_type_object->public
51
			) {
52
				$allowed_post_types[] = $post_type;
53
			}
54
		}
55
		$allowed_post_types = apply_filters( 'jetpack_post_sync_post_type', $allowed_post_types );
56
57
		return array_diff( $allowed_post_types, array( 'revision' ) );
58
	}
59
60
	static function get_synced_post_status() {
61
		$allowed_post_stati = apply_filters( 'jetpack_post_sync_post_status', get_post_stati() );
62
63
		return array_diff( $allowed_post_stati, array( 'auto-draft' ) );
64
	}
65
66
	static function get_post( $post_id, $allowed_post_types = null, $allowed_post_statuses = null ) {
67
		require_once JETPACK__PLUGIN_DIR . 'sal/class.json-api-platform.php';
68
		$sal = wpcom_get_sal_platform();
69
		$site = $sal->get_site( get_current_blog_id() );
70
		$post_obj = $site->get_post_by_id( $post_id, 'display' );
71
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
			$allowed_post_statuses = self::get_synced_post_status();
79
		}
80
81
		error_log( print_r( $post_obj, 1 ) );
82
		error_log( print_r( $allowed_post_types, 1 ) );
83
		error_log( print_r( $allowed_post_statuses, 1 ) );
84
85
		if ( ! in_array( $post_obj->get_type(), $allowed_post_types ) ) {
86
			return false;
87
		}
88
89
		if ( ! in_array( $post_obj->get_status(), $allowed_post_statuses ) ) {
90
			return false;
91
		}
92
93
		$post = $post_obj->to_array();
94
95
		// local optimizations
96
		unset(
97
			$post['post_password'],
98
			$post['filter'],
99
			$post['ancestors'],
100
			$post['post_content_filtered'],
101
			$post['to_ping'],
102
			$post['pinged']
103
		);
104
105
		$post['post_is_public'] = $post_obj->is_public();
106
		$post['post_is_excluded_from_search'] = $post_obj->is_excluded_from_search();
107
108
		$post['tax'] = $post_obj->get_taxonomies();
109
		$post['meta'] = $post_obj->get_meta();
110
111
		$post['extra'] = array(
112
			'author'                  => get_the_author_meta( 'display_name', $post_obj->post_author ),
113
			'author_email'            => get_the_author_meta( 'email', $post_obj->post_author ),
114
			'dont_email_post_to_subs' => get_post_meta( $post_obj->ID, '_jetpack_dont_email_post_to_subs', true ),
115
		);
116
117
		if ( $attachment_id = get_post_thumbnail_id( $post_id ) ) {
118
			$feature = wp_get_attachment_image_src( $attachment_id, 'large' );
119
			if ( ! empty( $feature[0] ) ) {
120
				$post['extra']['featured_image'] = $feature[0];
121
			}
122
123
			$attachment = get_post( $attachment_id );
124
			if ( ! empty( $attachment ) ) {
125
				$metadata = wp_get_attachment_metadata( $attachment_id );
126
127
				$post['extra']['post_thumbnail'] = array(
128
					'ID'        => (int) $attachment_id,
129
					'URL'       => (string) wp_get_attachment_url( $attachment_id ),
130
					'guid'      => (string) $attachment->guid,
131
					'mime_type' => (string) $attachment->post_mime_type,
132
					'width'     => (int) isset( $metadata['width'] ) ? $metadata['width'] : 0,
133
					'height'    => (int) isset( $metadata['height'] ) ? $metadata['height'] : 0,
134
				);
135
136
				if ( isset( $metadata['duration'] ) ) {
137
					$post['extra']['post_thumbnail'] = (int) $metadata['duration'];
138
				}
139
140
				/**
141
				 * Filters the Post Thumbnail information returned for a specific post.
142
				 *
143
				 * @since 3.3.0
144
				 *
145
				 * @param array $post ['extra']['post_thumbnail'] {
146
				 *    Array of details about the Post Thumbnail.
147
				 * @param int ID Post Thumbnail ID.
148
				 * @param string URL Post thumbnail URL.
149
				 * @param string guid Post thumbnail guid.
150
				 * @param string mime_type Post thumbnail mime type.
151
				 * @param int width Post thumbnail width.
152
				 * @param int height Post thumbnail height.
153
				 * }
154
				 */
155
				$post['extra']['post_thumbnail'] = (object) apply_filters( 'get_attachment', $post['extra']['post_thumbnail'] );
156
			}
157
		}
158
159
		$post['permalink'] = get_permalink( $post_obj->ID );
160
		$post['shortlink'] = wp_get_shortlink( $post_obj->ID );
161
		/**
162
		 * Allow modules to send extra info on the sync post process.
163
		 *
164
		 * @since 2.8.0
165
		 *
166
		 * @param array $args Array of custom data to attach to a post.
167
		 * @param Object $post_obj Object returned by get_post() for a given post ID.
168
		 */
169
		$post['module_custom_data']                      = apply_filters( 'jetpack_sync_post_module_custom_data', array(), $post_obj );
170
		$post['module_custom_data']['cpt_publicizeable'] = post_type_supports( $post_obj->post_type, 'publicize' ) ? true : false;
171
172
		return $post;
173
	}
174
175
}
176