Completed
Push — update/jetpack_published_post_... ( 5cfe6f...e53f06 )
by
unknown
16:30
created

Jetpack_Sync_Module_Posts::send_published()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
1
<?php
2
3
require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php';
4
5
class Jetpack_Sync_Module_Posts extends Jetpack_Sync_Module {
6
7
	private $just_published = array();
8
	private $action_handler;
9
10
	public function name() {
11
		return 'posts';
12
	}
13
14
	public function get_object_by_id( $object_type, $id ) {
15
		if ( $object_type === 'post' && $post = get_post( intval( $id ) ) ) {
16
			return $this->filter_post_content_and_add_links( $post );
17
		}
18
19
		return false;
20
	}
21
22
	public function set_defaults() {
23
	}
24
25
	public function init_listeners( $callable ) {
26
		$this->action_handler = $callable;
27
		add_action( 'wp_insert_post', array( $this, 'wp_insert_post' ), 10, 3 );
28
		add_action( 'deleted_post', $callable, 10 );
29
		add_action( 'jetpack_publicize_post', $callable );
30
		add_action( 'jetpack_published_post', $callable, 10, 2 );
31
		add_action( 'transition_post_status', array( $this, 'save_published' ), 10, 3 );
32
		add_filter( 'jetpack_sync_before_enqueue_wp_insert_post', array( $this, 'filter_blacklisted_post_types' ) );
33
		
34
		// listen for meta changes
35
		$this->init_listeners_for_meta_type( 'post', $callable );
36
		$this->init_meta_whitelist_handler( 'post', array( $this, 'filter_meta' ) );
37
	}
38
39
	public function init_full_sync_listeners( $callable ) {
40
		add_action( 'jetpack_full_sync_posts', $callable ); // also sends post meta
41
	}
42
43
	public function init_before_send() {
44
		add_filter( 'jetpack_sync_before_send_wp_insert_post', array( $this, 'expand_wp_insert_post' ) );
45
46
		// full sync
47
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_posts', array( $this, 'expand_post_ids' ) );
48
	}
49
50
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
51
		global $wpdb;
52
53
		return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_posts', $wpdb->posts, 'ID', $this->get_where_sql( $config ), $max_items_to_enqueue, $state );
54
	}
55
56 View Code Duplication
	public function estimate_full_sync_actions( $config ) {
57
		global $wpdb;
58
59
		$query = "SELECT count(*) FROM $wpdb->posts WHERE " . $this->get_where_sql( $config );
60
		$count = $wpdb->get_var( $query );
61
62
		return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
63
	}
64
65 View Code Duplication
	private function get_where_sql( $config ) {
66
		$where_sql = Jetpack_Sync_Settings::get_blacklisted_post_types_sql();
67
68
		// config is a list of post IDs to sync
69
		if ( is_array( $config ) ) {
70
			$where_sql   .= ' AND ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 3 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
71
		}
72
73
		return $where_sql;
74
	}
75
76
	function get_full_sync_actions() {
77
		return array( 'jetpack_full_sync_posts' );
78
	}
79
80
	/**
81
	 * Process content before send
82
	 * @param array, wp_insert_post arguments
83
	 * @return array
84
	 */
85
	function expand_wp_insert_post( $args ) {
86
		return array( $args[0], $this->filter_post_content_and_add_links( $args[1] ), $args[2] );
87
	}
88
89
	function filter_blacklisted_post_types( $args ) {
90
		$post = $args[1];
91
92
		if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) {
93
			return false;
94
		}
95
96
		return $args;
97
	}
98
99
	// Meta
100
	function filter_meta( $args ) {
101
		if ( $this->is_post_type_allowed( $args[1] ) && $this->is_whitelisted_post_meta( $args[2] ) ) {
102
			return $args;
103
		}
104
105
		return false;
106
	}
107
108
	function is_whitelisted_post_meta( $meta_key ) {
109
		// _wpas_skip_ is used by publicize
110
		return in_array( $meta_key, Jetpack_Sync_Settings::get_setting( 'post_meta_whitelist' ) ) || wp_startswith( $meta_key, '_wpas_skip_' );
111
	}
112
113
	function is_post_type_allowed( $post_id ) {
114
		$post = get_post( $post_id );
115
		return ! in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) );
116
	}
117
118 View Code Duplication
	function remove_embed() {
119
		global $wp_embed;
120
		remove_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 );
121
		// remove the embed shortcode since we would do the part later.
122
		remove_shortcode( 'embed' );
123
		// Attempts to embed all URLs in a post
124
		remove_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 );
125
	}
126
127 View Code Duplication
	function add_embed() {
128
		global $wp_embed;
129
		add_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 );
130
		// Shortcode placeholder for strip_shortcodes()
131
		add_shortcode( 'embed', '__return_false' );
132
		// Attempts to embed all URLs in a post
133
		add_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 );
134
	}
135
136
	// Expands wp_insert_post to include filtered content
137
	function filter_post_content_and_add_links( $post_object ) {
138
		global $post;
139
		$post = $post_object;
140
141
		// return non existant post 
142
		$post_type = get_post_type_object( $post->post_type );
143 View Code Duplication
		if ( empty( $post_type) || ! is_object( $post_type ) ) {
144
			$non_existant_post                    = new stdClass();
145
			$non_existant_post->ID                = $post->ID;
146
			$non_existant_post->post_modified     = $post->post_modified;
147
			$non_existant_post->post_modified_gmt = $post->post_modified_gmt;
148
			$non_existant_post->post_status       = 'jetpack_sync_non_registered_post_type';
149
			
150
			return $non_existant_post;
151
		}
152
		/**
153
		 * Filters whether to prevent sending post data to .com
154
		 *
155
		 * Passing true to the filter will prevent the post data from being sent
156
		 * to the WordPress.com.
157
		 * Instead we pass data that will still enable us to do a checksum against the
158
		 * Jetpacks data but will prevent us from displaying the data on in the API as well as
159
		 * other services.
160
		 * @since 4.2.0
161
		 *
162
		 * @param boolean false prevent post data from being synced to WordPress.com
163
		 * @param mixed $post WP_POST object
164
		 */
165 View Code Duplication
		if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) {
166
			// We only send the bare necessary object to be able to create a checksum.
167
			$blocked_post                    = new stdClass();
168
			$blocked_post->ID                = $post->ID;
169
			$blocked_post->post_modified     = $post->post_modified;
170
			$blocked_post->post_modified_gmt = $post->post_modified_gmt;
171
			$blocked_post->post_status       = 'jetpack_sync_blocked';
172
173
			return $blocked_post;
174
		}
175
176
		// lets not do oembed just yet.
177
		$this->remove_embed();
178
179
		if ( 0 < strlen( $post->post_password ) ) {
180
			$post->post_password = 'auto-' . wp_generate_password( 10, false );
181
		}
182
183
		/** This filter is already documented in core. wp-includes/post-template.php */
184
		if ( Jetpack_Sync_Settings::get_setting( 'render_filtered_content' ) && $post_type->public  ) {
185
			global $shortcode_tags;
186
			/**
187
			 * Filter prevents some shortcodes from expanding.
188
			 *
189
			 * Since we can can expand some type of shortcode better on the .com side and make the
190
			 * expansion more relevant to contexts. For example [galleries] and subscription emails
191
			 *
192
			 * @since 4.5.0
193
			 *
194
			 * @param array, of shortcode tags to remove.
195
			 */
196
			$shortcodes_to_remove = apply_filters( 'jetpack_sync_do_not_expand_shortcodes', array( 'gallery', 'slideshow' ) );
197
			$removed_shortcode_callbacks = array();
198
			foreach ( $shortcodes_to_remove as $shortcode ) {
199
				if ( isset ( $shortcode_tags[ $shortcode ] )  ) {
200
					$removed_shortcode_callbacks[ $shortcode ] =  $shortcode_tags[ $shortcode ];
201
				}
202
			}
203
204
			array_map( 'remove_shortcode' , array_keys( $removed_shortcode_callbacks ) );
205
206
			$post->post_content_filtered   = apply_filters( 'the_content', $post->post_content );
207
			$post->post_excerpt_filtered   = apply_filters( 'the_excerpt', $post->post_excerpt );
208
209
			foreach ( $removed_shortcode_callbacks as $shortcode => $callback ) {
210
			add_shortcode( $shortcode, $callback );
211
		}
212
		}
213
214
		$this->add_embed();
215
216
		if ( has_post_thumbnail( $post->ID ) ) {
217
			$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
218
			if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) {
219
				$post->featured_image = $image_attributes[0];
220
			}
221
		}
222
223
		$post->permalink               = get_permalink( $post->ID );
224
		$post->shortlink               = wp_get_shortlink( $post->ID );
225
226
		return $post;
227
	}
228
229
	public function wp_insert_post( $post_ID, $post, $update ) {
230
		call_user_func( $this->action_handler, $post_ID, $post, $update );
231
232
		if ( in_array( $post_ID, $this->just_published ) ) {
233
			$this->send_published( $post_ID, $post );
234
			$this->just_published = array_diff( $this->just_published, array( $post_ID ) );
235
		}
236
	}
237
238
	public function save_published( $new_status, $old_status, $post ) {
239
		if ( 'publish' === $new_status && 'publish' !== $old_status ) {
240
			$this->just_published[] = $post->ID;
241
		}
242
	}
243
244
	public function send_published( $post_ID, $post ) {
245
		/**
246
		 * Filter that is used to add to the post flags ( meta data ) when a post gets published
247
		 *
248
		 * @since 4.4.0
249
		 *
250
		 * @param mixed array post flags that are added to the post
251
		 * @param mixed $post WP_POST object
252
		 */
253
		$flags = apply_filters( 'jetpack_published_post_flags', array(), $post );
254
255
		/**
256
		 * Action that gets synced when a post type gets published.
257
		 *
258
		 * @since 4.4.0
259
		 *
260
		 * @param int, post_id
261
		 * @param mixed array post flags that are added to the post
262
		 */
263
		do_action( 'jetpack_published_post', $post_ID, $flags );
264
	}
265
266
	public function expand_post_ids( $args ) {
267
		$post_ids = $args[0];
268
269
		$posts = array_filter( array_map( array( 'WP_Post', 'get_instance' ), $post_ids ) );
270
		$posts = array_map( array( $this, 'filter_post_content_and_add_links' ), $posts );
271
		$posts = array_values( $posts ); // reindex in case posts were deleted
272
273
		return array(
274
			$posts,
275
			$this->get_metadata( $post_ids, 'post', Jetpack_Sync_Settings::get_setting( 'post_meta_whitelist' ) ),
276
			$this->get_term_relationships( $post_ids ),
277
		);
278
	}
279
}
280