Completed
Push — try/tdd-sync ( dd4b0c )
by
unknown
10:32
created

Jetpack_Sync_Posts   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 22
Bugs 0 Features 7
Metric Value
wmc 32
c 22
b 0
f 7
lcom 1
cbo 3
dl 0
loc 194
rs 9.6

7 Methods

Rating   Name   Duplication   Size   Complexity  
A get_post_diff() 0 3 1
A init() 0 21 1
A clear_post_cache() 0 3 1
D get_actions_to_sync() 0 31 10
B get_synced_post_types() 0 14 5
A get_synced_post_status() 0 5 1
D get_post() 0 104 13
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_actions_to_sync() {
39
		$actions = array();
40
		foreach ( Jetpack_Sync::$actions as $action => $calls ) {
0 ignored issues
show
Bug introduced by
The property actions cannot be accessed from this context as it is declared private in class Jetpack_Sync.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
41
			foreach ( $calls as $args ) {
42
				switch ( $action ) {
43
					case 'save_post' :
44
						$args = array( $args[0], self::get_post( $args[0] ), $args[2] );
45
						break;
46
					case 'transition_post_status' :
47
						list( $new_status, $old_status, $post ) = $args;
48
						if ( $new_status === $old_status ) {
49
							$args = null;
50
							break;
51
						}
52
						$args = array( $args[0], $args[1], self::get_post( $post->ID ) );
53
						break;
54
					case 'set_object_terms' :
55
						list( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) = $args;
0 ignored issues
show
Unused Code introduced by
The assignment to $object_id is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $terms is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $taxonomy is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $append is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
56
						if ( empty( array_diff( $tt_ids, $old_tt_ids ) ) && empty( array_diff( $old_tt_ids, $tt_ids ) ) ) {
57
							$args = null;
58
							break;
59
						}
60
						break;
61
				}
62
				if ( is_null( $args ) ) {
63
					$actions[ $action ][] = $args;
64
				}
65
			}
66
		}
67
		return $actions;
68
	}
69
70
	static function get_synced_post_types() {
71
		$allowed_post_types = array();
72
		foreach ( get_post_types( array(), 'objects' ) as $post_type => $post_type_object ) {
73
			if ( post_type_supports( $post_type, 'comments' ) ||
74
			     post_type_supports( $post_type, 'publicize' ) ||
75
			     $post_type_object->public
76
			) {
77
				$allowed_post_types[] = $post_type;
78
			}
79
		}
80
		$allowed_post_types = apply_filters( 'jetpack_post_sync_post_type', $allowed_post_types );
81
82
		return array_diff( $allowed_post_types, array( 'revision' ) );
83
	}
84
85
	static function get_synced_post_status() {
86
		$allowed_post_stati = apply_filters( 'jetpack_post_sync_post_status', get_post_stati() );
87
88
		return array_diff( $allowed_post_stati, array( 'auto-draft' ) );
89
	}
90
91
	static function get_post( $post_id, $allowed_post_types = null, $allowed_post_statuses = null ) {
92
		require_once JETPACK__PLUGIN_DIR . 'sal/class.json-api-platform.php';
93
		$sal = wpcom_get_sal_platform();
94
		$site = $sal->get_site( get_current_blog_id() );
95
		$post_obj = $site->get_post_by_id( $post_id, 'display' );
96
97
		if ( ! $post_obj || is_wp_error( $post_obj ) ) {
98
			return false;
99
		}
100
101
		if ( is_null( $allowed_post_types ) ) {
102
			$allowed_post_types = self::get_synced_post_types();
103
			$allowed_post_statuses = self::get_synced_post_status();
104
		}
105
106
		if ( ! in_array( $post_obj->post_type, $allowed_post_types ) ) {
107
			return false;
108
		}
109
110
		if ( ! in_array( $post_obj->post_status, $allowed_post_statuses ) ) {
111
			return false;
112
		}
113
114
		$post = $post_obj->to_array();
115
116
		// local optimizations
117
		unset(
118
			$post['post_password'],
119
			$post['filter'],
120
			$post['ancestors'],
121
			$post['post_content_filtered'],
122
			$post['to_ping'],
123
			$post['pinged']
124
		);
125
126
		$post['post_is_public'] = $post_obj->is_public();
127
		$post['post_is_excluded_from_search'] = $post_obj->is_excluded_from_search();
128
129
		$post['tax'] = $post_obj->get_taxonomies();
130
		$post['meta'] = $post_obj->get_meta();
131
132
		$post['extra'] = array(
133
			'author'                  => get_the_author_meta( 'display_name', $post_obj->post_author ),
134
			'author_email'            => get_the_author_meta( 'email', $post_obj->post_author ),
135
			'dont_email_post_to_subs' => get_post_meta( $post_obj->ID, '_jetpack_dont_email_post_to_subs', true ),
136
		);
137
138
		if ( $attachment_id = get_post_thumbnail_id( $post_id ) ) {
139
			$feature = wp_get_attachment_image_src( $attachment_id, 'large' );
140
			if ( ! empty( $feature[0] ) ) {
141
				$post['extra']['featured_image'] = $feature[0];
142
			}
143
144
			$attachment = get_post( $attachment_id );
145
			if ( ! empty( $attachment ) ) {
146
				$metadata = wp_get_attachment_metadata( $attachment_id );
147
148
				$post['extra']['post_thumbnail'] = array(
149
					'ID'        => (int) $attachment_id,
150
					'URL'       => (string) wp_get_attachment_url( $attachment_id ),
151
					'guid'      => (string) $attachment->guid,
152
					'mime_type' => (string) $attachment->post_mime_type,
153
					'width'     => (int) isset( $metadata['width'] ) ? $metadata['width'] : 0,
154
					'height'    => (int) isset( $metadata['height'] ) ? $metadata['height'] : 0,
155
				);
156
157
				if ( isset( $metadata['duration'] ) ) {
158
					$post['extra']['post_thumbnail'] = (int) $metadata['duration'];
159
				}
160
161
				/**
162
				 * Filters the Post Thumbnail information returned for a specific post.
163
				 *
164
				 * @since 3.3.0
165
				 *
166
				 * @param array $post ['extra']['post_thumbnail'] {
167
				 *    Array of details about the Post Thumbnail.
168
				 * @param int ID Post Thumbnail ID.
169
				 * @param string URL Post thumbnail URL.
170
				 * @param string guid Post thumbnail guid.
171
				 * @param string mime_type Post thumbnail mime type.
172
				 * @param int width Post thumbnail width.
173
				 * @param int height Post thumbnail height.
174
				 * }
175
				 */
176
				$post['extra']['post_thumbnail'] = (object) apply_filters( 'get_attachment', $post['extra']['post_thumbnail'] );
177
			}
178
		}
179
180
		$post['permalink'] = get_permalink( $post_obj->ID );
181
		$post['shortlink'] = wp_get_shortlink( $post_obj->ID );
182
		/**
183
		 * Allow modules to send extra info on the sync post process.
184
		 *
185
		 * @since 2.8.0
186
		 *
187
		 * @param array $args Array of custom data to attach to a post.
188
		 * @param Object $post_obj Object returned by get_post() for a given post ID.
189
		 */
190
		$post['module_custom_data']                      = apply_filters( 'jetpack_sync_post_module_custom_data', array(), $post_obj );
191
		$post['module_custom_data']['cpt_publicizeable'] = post_type_supports( $post_obj->post_type, 'publicize' ) ? true : false;
192
193
		return $post;
194
	}
195
196
}
197