Completed
Push — add/sync-rest-2 ( a7dbc1...6b7ec7 )
by
unknown
08:39
created

Jetpack_Sync_Posts::clear_post_cache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 2
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( 'save_post', array( 'Jetpack_Sync', 'sync_action' ), 0, 3 );
11
		add_action( 'deleted_post', array( 'Jetpack_Sync', 'sync_action' ), 0 );
12
		add_action( 'transition_post_status', array( 'Jetpack_Sync', 'sync_action' ), 10, 3 );
13
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' ), 0, 6 );
22
23
		// Update comment count
24
		add_action( 'wp_update_comment_count', array( 'Jetpack_Sync', 'sync_action'  ), 0, 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 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...
35
		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...
36
	}
37
38
	static function get_synced_post_types() {
39
		$allowed_post_types = array();
40
		foreach ( get_post_types( array(), 'objects' ) as $post_type => $post_type_object ) {
41
			if ( post_type_supports( $post_type, 'comments' ) ||
42
			     post_type_supports( $post_type, 'publicize' ) ||
43
			     $post_type_object->public
44
			) {
45
				$allowed_post_types[] = $post_type;
46
			}
47
		}
48
		$allowed_post_types = apply_filters( 'jetpack_post_sync_post_type', $allowed_post_types );
49
50
		return array_diff( $allowed_post_types, array( 'revision' ) );
51
	}
52
53
	static function get_synced_post_status() {
54
		$allowed_post_stati = apply_filters( 'jetpack_post_sync_post_status', get_post_stati() );
55
56
		return array_diff( $allowed_post_stati, array( 'auto-draft' ) );
57
	}
58
59
	static function get_post( $post_id, $allowed_post_types = null, $allowed_post_statuses = null ) {
60
		require_once JETPACK__PLUGIN_DIR . 'sal/class.json-api-platform.php';
61
		$sal = wpcom_get_sal_platform();
62
		$site = $sal->get_site( get_current_blog_id() );
63
		$post_obj = $site->get_post_by_id( $post_id, 'display' );
64
65
		if ( ! $post_obj ) {
66
			return false;
67
		}
68
69
		if ( is_null( $allowed_post_types ) ) {
70
			$allowed_post_types = self::get_synced_post_types();
71
			$allowed_post_statuses = self::get_synced_post_status();
72
		}
73
74
		if ( ! in_array( $post_obj->get_type(), $allowed_post_types ) ) {
75
			return false;
76
		}
77
78
		if ( ! in_array( $post_obj->get_status(), $allowed_post_statuses ) ) {
79
			return false;
80
		}
81
82
		$post = $post_obj->to_array();
83
84
		// local optimizations
85
		unset(
86
			$post['post_password'],
87
			$post['filter'],
88
			$post['ancestors'],
89
			$post['post_content_filtered'],
90
			$post['to_ping'],
91
			$post['pinged']
92
		);
93
94
		$post['post_is_public'] = $post_obj->is_public();
95
		$post['post_is_excluded_from_search'] = $post_obj->is_excluded_from_search();
96
97
		$post['tax'] = $post_obj->get_taxonomies();
98
		$post['meta'] = $post_obj->get_meta();
99
100
		$post['extra'] = array(
101
			'author'                  => get_the_author_meta( 'display_name', $post_obj->post_author ),
102
			'author_email'            => get_the_author_meta( 'email', $post_obj->post_author ),
103
			'dont_email_post_to_subs' => get_post_meta( $post_obj->ID, '_jetpack_dont_email_post_to_subs', true ),
104
		);
105
106
		if ( $attachment_id = get_post_thumbnail_id( $post_id ) ) {
107
			$feature = wp_get_attachment_image_src( $attachment_id, 'large' );
108
			if ( ! empty( $feature[0] ) ) {
109
				$post['extra']['featured_image'] = $feature[0];
110
			}
111
112
			$attachment = get_post( $attachment_id );
113
			if ( ! empty( $attachment ) ) {
114
				$metadata = wp_get_attachment_metadata( $attachment_id );
115
116
				$post['extra']['post_thumbnail'] = array(
117
					'ID'        => (int) $attachment_id,
118
					'URL'       => (string) wp_get_attachment_url( $attachment_id ),
119
					'guid'      => (string) $attachment->guid,
120
					'mime_type' => (string) $attachment->post_mime_type,
121
					'width'     => (int) isset( $metadata['width'] ) ? $metadata['width'] : 0,
122
					'height'    => (int) isset( $metadata['height'] ) ? $metadata['height'] : 0,
123
				);
124
125
				if ( isset( $metadata['duration'] ) ) {
126
					$post['extra']['post_thumbnail'] = (int) $metadata['duration'];
127
				}
128
129
				/**
130
				 * Filters the Post Thumbnail information returned for a specific post.
131
				 *
132
				 * @since 3.3.0
133
				 *
134
				 * @param array $post ['extra']['post_thumbnail'] {
135
				 *    Array of details about the Post Thumbnail.
136
				 * @param int ID Post Thumbnail ID.
137
				 * @param string URL Post thumbnail URL.
138
				 * @param string guid Post thumbnail guid.
139
				 * @param string mime_type Post thumbnail mime type.
140
				 * @param int width Post thumbnail width.
141
				 * @param int height Post thumbnail height.
142
				 * }
143
				 */
144
				$post['extra']['post_thumbnail'] = (object) apply_filters( 'get_attachment', $post['extra']['post_thumbnail'] );
145
			}
146
		}
147
148
		$post['permalink'] = get_permalink( $post_obj->ID );
149
		$post['shortlink'] = wp_get_shortlink( $post_obj->ID );
150
		/**
151
		 * Allow modules to send extra info on the sync post process.
152
		 *
153
		 * @since 2.8.0
154
		 *
155
		 * @param array $args Array of custom data to attach to a post.
156
		 * @param Object $post_obj Object returned by get_post() for a given post ID.
157
		 */
158
		$post['module_custom_data']                      = apply_filters( 'jetpack_sync_post_module_custom_data', array(), $post_obj );
159
		$post['module_custom_data']['cpt_publicizeable'] = post_type_supports( $post_obj->post_type, 'publicize' ) ? true : false;
160
161
		return $post;
162
	}
163
164
}
165