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
|
|
|
|
9
|
|
|
public function name() { |
10
|
|
|
return 'posts'; |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
public function get_object_by_id( $object_type, $id ) { |
14
|
|
|
if ( $object_type === 'post' && $post = get_post( intval( $id ) ) ) { |
15
|
|
|
return $this->filter_post_content_and_add_links( $post ); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
return false; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function set_defaults() { |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function init_listeners( $callable ) { |
25
|
|
|
global $wp_version; |
26
|
|
|
// Core < 4.7 doesn't deal with nested wp_insert_post calls very well |
27
|
|
|
if ( version_compare( $wp_version, '4.7', '<' ) ) { |
28
|
|
|
add_action( 'wp_insert_post', array( $this, 'send_published' ), 9, 3 ); |
29
|
|
|
} else { |
30
|
|
|
add_action( 'wp_insert_post', array( $this, 'send_published' ), 11, 3 ); |
31
|
|
|
} |
32
|
|
|
add_action( 'deleted_post', $callable, 10 ); |
33
|
|
|
add_action( 'jetpack_publicize_post', $callable ); |
34
|
|
|
add_action( 'jetpack_published_post', $callable, 10, 2 ); |
35
|
|
|
add_action( 'transition_post_status', array( $this, 'save_published' ), 10, 3 ); |
36
|
|
|
add_filter( 'jetpack_sync_before_enqueue_wp_insert_post', array( $this, 'filter_blacklisted_post_types' ) ); |
37
|
|
|
|
38
|
|
|
// listen for meta changes |
39
|
|
|
$this->init_listeners_for_meta_type( 'post', $callable ); |
40
|
|
|
$this->init_meta_whitelist_handler( 'post', array( $this, 'filter_meta' ) ); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function init_full_sync_listeners( $callable ) { |
44
|
|
|
add_action( 'jetpack_full_sync_posts', $callable ); // also sends post meta |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function init_before_send() { |
48
|
|
|
add_filter( 'jetpack_sync_before_send_wp_insert_post', array( $this, 'expand_wp_insert_post' ) ); |
49
|
|
|
|
50
|
|
|
// full sync |
51
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_posts', array( $this, 'expand_post_ids' ) ); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
55
|
|
|
global $wpdb; |
56
|
|
|
|
57
|
|
|
return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_posts', $wpdb->posts, 'ID', $this->get_where_sql( $config ), $max_items_to_enqueue, $state ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
public function estimate_full_sync_actions( $config ) { |
61
|
|
|
global $wpdb; |
62
|
|
|
|
63
|
|
|
$query = "SELECT count(*) FROM $wpdb->posts WHERE " . $this->get_where_sql( $config ); |
64
|
|
|
$count = $wpdb->get_var( $query ); |
65
|
|
|
|
66
|
|
|
return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
private function get_where_sql( $config ) { |
70
|
|
|
$where_sql = Jetpack_Sync_Settings::get_blacklisted_post_types_sql(); |
71
|
|
|
|
72
|
|
|
// config is a list of post IDs to sync |
73
|
|
|
if ( is_array( $config ) ) { |
74
|
|
|
$where_sql .= ' AND ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')'; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $where_sql; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
function get_full_sync_actions() { |
81
|
|
|
return array( 'jetpack_full_sync_posts' ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Process content before send |
86
|
|
|
* @param array, wp_insert_post arguments |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
function expand_wp_insert_post( $args ) { |
90
|
|
|
return array( $args[0], $this->filter_post_content_and_add_links( $args[1] ), $args[2] ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
function filter_blacklisted_post_types( $args ) { |
94
|
|
|
$post = $args[1]; |
95
|
|
|
|
96
|
|
|
if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) { |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $args; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// Meta |
104
|
|
|
function filter_meta( $args ) { |
105
|
|
|
if ( $this->is_post_type_allowed( $args[1] ) && $this->is_whitelisted_post_meta( $args[2] ) ) { |
106
|
|
|
return $args; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
function is_whitelisted_post_meta( $meta_key ) { |
113
|
|
|
// _wpas_skip_ is used by publicize |
114
|
|
|
return in_array( $meta_key, Jetpack_Sync_Settings::get_setting( 'post_meta_whitelist' ) ) || wp_startswith( $meta_key, '_wpas_skip_' ); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
function is_post_type_allowed( $post_id ) { |
118
|
|
|
$post = get_post( $post_id ); |
119
|
|
|
return ! in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
View Code Duplication |
function remove_embed() { |
123
|
|
|
global $wp_embed; |
124
|
|
|
remove_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 ); |
125
|
|
|
// remove the embed shortcode since we would do the part later. |
126
|
|
|
remove_shortcode( 'embed' ); |
127
|
|
|
// Attempts to embed all URLs in a post |
128
|
|
|
remove_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
View Code Duplication |
function add_embed() { |
132
|
|
|
global $wp_embed; |
133
|
|
|
add_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 ); |
134
|
|
|
// Shortcode placeholder for strip_shortcodes() |
135
|
|
|
add_shortcode( 'embed', '__return_false' ); |
136
|
|
|
// Attempts to embed all URLs in a post |
137
|
|
|
add_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 ); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// Expands wp_insert_post to include filtered content |
141
|
|
|
function filter_post_content_and_add_links( $post_object ) { |
142
|
|
|
global $post; |
143
|
|
|
$post = $post_object; |
144
|
|
|
|
145
|
|
|
// return non existant post |
146
|
|
|
$post_type = get_post_type_object( $post->post_type ); |
147
|
|
View Code Duplication |
if ( empty( $post_type) || ! is_object( $post_type ) ) { |
148
|
|
|
$non_existant_post = new stdClass(); |
149
|
|
|
$non_existant_post->ID = $post->ID; |
150
|
|
|
$non_existant_post->post_modified = $post->post_modified; |
151
|
|
|
$non_existant_post->post_modified_gmt = $post->post_modified_gmt; |
152
|
|
|
$non_existant_post->post_status = 'jetpack_sync_non_registered_post_type'; |
153
|
|
|
|
154
|
|
|
return $non_existant_post; |
155
|
|
|
} |
156
|
|
|
/** |
157
|
|
|
* Filters whether to prevent sending post data to .com |
158
|
|
|
* |
159
|
|
|
* Passing true to the filter will prevent the post data from being sent |
160
|
|
|
* to the WordPress.com. |
161
|
|
|
* Instead we pass data that will still enable us to do a checksum against the |
162
|
|
|
* Jetpacks data but will prevent us from displaying the data on in the API as well as |
163
|
|
|
* other services. |
164
|
|
|
* @since 4.2.0 |
165
|
|
|
* |
166
|
|
|
* @param boolean false prevent post data from being synced to WordPress.com |
167
|
|
|
* @param mixed $post WP_POST object |
168
|
|
|
*/ |
169
|
|
View Code Duplication |
if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) { |
170
|
|
|
// We only send the bare necessary object to be able to create a checksum. |
171
|
|
|
$blocked_post = new stdClass(); |
172
|
|
|
$blocked_post->ID = $post->ID; |
173
|
|
|
$blocked_post->post_modified = $post->post_modified; |
174
|
|
|
$blocked_post->post_modified_gmt = $post->post_modified_gmt; |
175
|
|
|
$blocked_post->post_status = 'jetpack_sync_blocked'; |
176
|
|
|
|
177
|
|
|
return $blocked_post; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// lets not do oembed just yet. |
181
|
|
|
$this->remove_embed(); |
182
|
|
|
|
183
|
|
|
if ( 0 < strlen( $post->post_password ) ) { |
184
|
|
|
$post->post_password = 'auto-' . wp_generate_password( 10, false ); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** This filter is already documented in core. wp-includes/post-template.php */ |
188
|
|
|
if ( Jetpack_Sync_Settings::get_setting( 'render_filtered_content' ) && $post_type->public ) { |
189
|
|
|
global $shortcode_tags; |
190
|
|
|
/** |
191
|
|
|
* Filter prevents some shortcodes from expanding. |
192
|
|
|
* |
193
|
|
|
* Since we can can expand some type of shortcode better on the .com side and make the |
194
|
|
|
* expansion more relevant to contexts. For example [galleries] and subscription emails |
195
|
|
|
* |
196
|
|
|
* @since 4.5.0 |
197
|
|
|
* |
198
|
|
|
* @param array, of shortcode tags to remove. |
199
|
|
|
*/ |
200
|
|
|
$shortcodes_to_remove = apply_filters( 'jetpack_sync_do_not_expand_shortcodes', array( 'gallery', 'slideshow' ) ); |
201
|
|
|
$removed_shortcode_callbacks = array(); |
202
|
|
|
foreach ( $shortcodes_to_remove as $shortcode ) { |
203
|
|
|
if ( isset ( $shortcode_tags[ $shortcode ] ) ) { |
204
|
|
|
$removed_shortcode_callbacks[ $shortcode ] = $shortcode_tags[ $shortcode ]; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
array_map( 'remove_shortcode' , array_keys( $removed_shortcode_callbacks ) ); |
209
|
|
|
|
210
|
|
|
$post->post_content_filtered = apply_filters( 'the_content', $post->post_content ); |
211
|
|
|
$post->post_excerpt_filtered = apply_filters( 'the_excerpt', $post->post_excerpt ); |
212
|
|
|
|
213
|
|
|
foreach ( $removed_shortcode_callbacks as $shortcode => $callback ) { |
214
|
|
|
add_shortcode( $shortcode, $callback ); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$this->add_embed(); |
219
|
|
|
|
220
|
|
|
if ( has_post_thumbnail( $post->ID ) ) { |
221
|
|
|
$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); |
222
|
|
|
if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) { |
223
|
|
|
$post->featured_image = $image_attributes[0]; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$post->permalink = get_permalink( $post->ID ); |
228
|
|
|
$post->shortlink = wp_get_shortlink( $post->ID ); |
229
|
|
|
|
230
|
|
|
return $post; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function save_published( $new_status, $old_status, $post ) { |
234
|
|
|
if ( 'publish' === $new_status && 'publish' !== $old_status ) { |
235
|
|
|
$this->just_published[] = $post->ID; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function send_published( $post_ID, $post, $update ) { |
|
|
|
|
240
|
|
|
if ( ! in_array( $post_ID, $this->just_published ) ) { |
241
|
|
|
return; |
242
|
|
|
} |
243
|
|
|
/** |
244
|
|
|
* Filter that is used to add to the post flags ( meta data ) when a post gets published |
245
|
|
|
* |
246
|
|
|
* @since 4.4.0 |
247
|
|
|
* |
248
|
|
|
* @param mixed array post flags that are added to the post |
249
|
|
|
* @param mixed $post WP_POST object |
250
|
|
|
*/ |
251
|
|
|
$flags = apply_filters( 'jetpack_published_post_flags', array(), $post ); |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Action that gets synced when a post type gets published. |
255
|
|
|
* |
256
|
|
|
* @since 4.4.0 |
257
|
|
|
* |
258
|
|
|
* @param int, post_id |
259
|
|
|
* @param mixed array post flags that are added to the post |
260
|
|
|
*/ |
261
|
|
|
do_action( 'jetpack_published_post', $post_ID, $flags ); |
262
|
|
|
|
263
|
|
|
$this->just_published = array_diff( $this->just_published, array( $post_ID ) ); |
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
|
|
|
|
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.