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 $previous_status = array(); |
9
|
|
|
private $action_handler; |
10
|
|
|
private $import_end = false; |
11
|
|
|
|
12
|
|
|
const DEFAULT_PREVIOUS_STATE = 'new'; |
13
|
|
|
|
14
|
|
|
public function name() { |
15
|
|
|
return 'posts'; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function get_object_by_id( $object_type, $id ) { |
19
|
|
|
if ( $object_type === 'post' && $post = get_post( intval( $id ) ) ) { |
20
|
|
|
return $this->filter_post_content_and_add_links( $post ); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
return false; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function set_defaults() { |
27
|
|
|
$this->import_end = false; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function init_listeners( $callable ) { |
31
|
|
|
$this->action_handler = $callable; |
32
|
|
|
|
33
|
|
|
// Core < 4.7 doesn't deal with nested wp_insert_post calls very well |
34
|
|
|
global $wp_version; |
35
|
|
|
$priority = version_compare( $wp_version, '4.7-alpha', '<' ) ? 0 : 11; |
36
|
|
|
|
37
|
|
|
add_action( 'wp_insert_post', array( $this, 'wp_insert_post' ), $priority, 3 ); |
38
|
|
|
add_action( 'jetpack_sync_save_post', $callable, 10, 4 ); |
39
|
|
|
|
40
|
|
|
add_action( 'deleted_post', $callable, 10 ); |
41
|
|
|
add_action( 'jetpack_published_post', $callable, 10, 2 ); |
42
|
|
|
|
43
|
|
|
add_action( 'transition_post_status', array( $this, 'save_published' ), 10, 3 ); |
44
|
|
|
add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_save_post', array( $this, 'filter_blacklisted_post_types' ) ); |
45
|
|
|
|
46
|
|
|
// listen for meta changes |
47
|
|
|
$this->init_listeners_for_meta_type( 'post', $callable ); |
48
|
|
|
$this->init_meta_whitelist_handler( 'post', array( $this, 'filter_meta' ) ); |
49
|
|
|
|
50
|
|
|
add_action( 'export_wp', $callable ); |
51
|
|
|
add_action( 'jetpack_sync_import_end', $callable, 10, 2 ); |
52
|
|
|
|
53
|
|
|
// Movable type, RSS, Livejournal |
54
|
|
|
add_action( 'import_done', array( $this, 'sync_import_done' ) ); |
55
|
|
|
|
56
|
|
|
// WordPress, Blogger, Livejournal, woo tax rate |
57
|
|
|
add_action( 'import_end', array( $this, 'sync_import_end' ) ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function sync_import_done( $importer ) { |
61
|
|
|
// We already ran an send the import |
62
|
|
|
if ( $this->import_end ) { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$importer_name = $this->get_importer_name( $importer ); |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Sync Event that tells that the import is finished |
70
|
|
|
* |
71
|
|
|
* @since 5.0.0 |
72
|
|
|
* |
73
|
|
|
* $param string $importer |
74
|
|
|
*/ |
75
|
|
|
do_action( 'jetpack_sync_import_end', $importer, $importer_name ); |
76
|
|
|
$this->import_end = true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function sync_import_end() { |
80
|
|
|
// We already ran an send the import |
81
|
|
|
if ( $this->import_end ) { |
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->import_end = true; |
86
|
|
|
$importer = 'unknown'; |
87
|
|
|
$backtrace = wp_debug_backtrace_summary( null, 0, false ); |
88
|
|
|
if ( $this->is_importer( $backtrace, 'Blogger_Importer' ) ) { |
89
|
|
|
$importer = 'blogger'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ( 'unknown' === $importer && $this->is_importer( $backtrace, 'WC_Tax_Rate_Importer' ) ) { |
93
|
|
|
$importer = 'woo-tax-rate'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if ( 'unknown' === $importer && $this->is_importer( $backtrace, 'WP_Import' ) ) { |
97
|
|
|
$importer = 'wordpress'; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$importer_name = $this->get_importer_name( $importer ); |
101
|
|
|
|
102
|
|
|
/** This filter is already documented in sync/class.jetpack-sync-module-posts.php */ |
103
|
|
|
do_action( 'jetpack_sync_import_end', $importer, $importer_name ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function get_importer_name( $importer ) { |
107
|
|
|
$importers = get_importers(); |
108
|
|
|
return isset( $importers[ $importer ] ) ? $importers[ $importer ][0] : 'Unknown Importer'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private function is_importer( $backtrace, $class_name ) { |
112
|
|
|
foreach ( $backtrace as $trace ) { |
113
|
|
|
if ( strpos( $trace, $class_name ) !== false ) { |
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function init_full_sync_listeners( $callable ) { |
122
|
|
|
add_action( 'jetpack_full_sync_posts', $callable ); // also sends post meta |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function init_before_send() { |
126
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_sync_save_post', array( $this, 'expand_jetpack_sync_save_post' ) ); |
127
|
|
|
|
128
|
|
|
// full sync |
129
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_posts', array( $this, 'expand_post_ids' ) ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
133
|
|
|
global $wpdb; |
134
|
|
|
|
135
|
|
|
return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_posts', $wpdb->posts, 'ID', $this->get_where_sql( $config ), $max_items_to_enqueue, $state ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
View Code Duplication |
public function estimate_full_sync_actions( $config ) { |
139
|
|
|
global $wpdb; |
140
|
|
|
|
141
|
|
|
$query = "SELECT count(*) FROM $wpdb->posts WHERE " . $this->get_where_sql( $config ); |
142
|
|
|
$count = $wpdb->get_var( $query ); |
143
|
|
|
|
144
|
|
|
return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
View Code Duplication |
private function get_where_sql( $config ) { |
148
|
|
|
$where_sql = Jetpack_Sync_Settings::get_blacklisted_post_types_sql(); |
149
|
|
|
|
150
|
|
|
// config is a list of post IDs to sync |
151
|
|
|
if ( is_array( $config ) ) { |
152
|
|
|
$where_sql .= ' AND ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')'; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $where_sql; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
function get_full_sync_actions() { |
159
|
|
|
return array( 'jetpack_full_sync_posts' ); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Process content before send |
164
|
|
|
* |
165
|
|
|
* @param array $args wp_insert_post arguments |
166
|
|
|
* |
167
|
|
|
* @return array |
168
|
|
|
*/ |
169
|
|
|
function expand_jetpack_sync_save_post( $args ) { |
170
|
|
|
list( $post_id, $post, $update, $previous_state ) = $args; |
171
|
|
|
return array( $post_id, $this->filter_post_content_and_add_links( $post ), $update, $previous_state ); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
function filter_blacklisted_post_types( $args ) { |
175
|
|
|
$post = $args[1]; |
176
|
|
|
|
177
|
|
|
if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) { |
178
|
|
|
return false; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $args; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// Meta |
185
|
|
|
function filter_meta( $args ) { |
186
|
|
|
if ( $this->is_post_type_allowed( $args[1] ) && $this->is_whitelisted_post_meta( $args[2] ) ) { |
187
|
|
|
return $args; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return false; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
function is_whitelisted_post_meta( $meta_key ) { |
194
|
|
|
// _wpas_skip_ is used by publicize |
195
|
|
|
return in_array( $meta_key, Jetpack_Sync_Settings::get_setting( 'post_meta_whitelist' ) ) || wp_startswith( $meta_key, '_wpas_skip_' ); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
function is_post_type_allowed( $post_id ) { |
199
|
|
|
$post = get_post( intval( $post_id ) ); |
200
|
|
|
if( $post->post_type ) { |
201
|
|
|
return ! in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ); |
202
|
|
|
} |
203
|
|
|
return false; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
View Code Duplication |
function remove_embed() { |
207
|
|
|
global $wp_embed; |
208
|
|
|
remove_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 ); |
209
|
|
|
// remove the embed shortcode since we would do the part later. |
210
|
|
|
remove_shortcode( 'embed' ); |
211
|
|
|
// Attempts to embed all URLs in a post |
212
|
|
|
remove_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 ); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
View Code Duplication |
function add_embed() { |
216
|
|
|
global $wp_embed; |
217
|
|
|
add_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 ); |
218
|
|
|
// Shortcode placeholder for strip_shortcodes() |
219
|
|
|
add_shortcode( 'embed', '__return_false' ); |
220
|
|
|
// Attempts to embed all URLs in a post |
221
|
|
|
add_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 ); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
// Expands wp_insert_post to include filtered content |
225
|
|
|
function filter_post_content_and_add_links( $post_object ) { |
226
|
|
|
global $post; |
227
|
|
|
$post = $post_object; |
228
|
|
|
|
229
|
|
|
// return non existant post |
230
|
|
|
$post_type = get_post_type_object( $post->post_type ); |
231
|
|
View Code Duplication |
if ( empty( $post_type ) || ! is_object( $post_type ) ) { |
232
|
|
|
$non_existant_post = new stdClass(); |
233
|
|
|
$non_existant_post->ID = $post->ID; |
234
|
|
|
$non_existant_post->post_modified = $post->post_modified; |
235
|
|
|
$non_existant_post->post_modified_gmt = $post->post_modified_gmt; |
236
|
|
|
$non_existant_post->post_status = 'jetpack_sync_non_registered_post_type'; |
237
|
|
|
|
238
|
|
|
return $non_existant_post; |
239
|
|
|
} |
240
|
|
|
/** |
241
|
|
|
* Filters whether to prevent sending post data to .com |
242
|
|
|
* |
243
|
|
|
* Passing true to the filter will prevent the post data from being sent |
244
|
|
|
* to the WordPress.com. |
245
|
|
|
* Instead we pass data that will still enable us to do a checksum against the |
246
|
|
|
* Jetpacks data but will prevent us from displaying the data on in the API as well as |
247
|
|
|
* other services. |
248
|
|
|
* @since 4.2.0 |
249
|
|
|
* |
250
|
|
|
* @param boolean false prevent post data from being synced to WordPress.com |
251
|
|
|
* @param mixed $post WP_POST object |
252
|
|
|
*/ |
253
|
|
View Code Duplication |
if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) { |
254
|
|
|
// We only send the bare necessary object to be able to create a checksum. |
255
|
|
|
$blocked_post = new stdClass(); |
256
|
|
|
$blocked_post->ID = $post->ID; |
257
|
|
|
$blocked_post->post_modified = $post->post_modified; |
258
|
|
|
$blocked_post->post_modified_gmt = $post->post_modified_gmt; |
259
|
|
|
$blocked_post->post_status = 'jetpack_sync_blocked'; |
260
|
|
|
|
261
|
|
|
return $blocked_post; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
// lets not do oembed just yet. |
265
|
|
|
$this->remove_embed(); |
266
|
|
|
|
267
|
|
|
if ( 0 < strlen( $post->post_password ) ) { |
268
|
|
|
$post->post_password = 'auto-' . wp_generate_password( 10, false ); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** This filter is already documented in core. wp-includes/post-template.php */ |
272
|
|
|
if ( Jetpack_Sync_Settings::get_setting( 'render_filtered_content' ) && $post_type->public ) { |
273
|
|
|
global $shortcode_tags; |
274
|
|
|
/** |
275
|
|
|
* Filter prevents some shortcodes from expanding. |
276
|
|
|
* |
277
|
|
|
* Since we can can expand some type of shortcode better on the .com side and make the |
278
|
|
|
* expansion more relevant to contexts. For example [galleries] and subscription emails |
279
|
|
|
* |
280
|
|
|
* @since 4.5.0 |
281
|
|
|
* |
282
|
|
|
* @param array of shortcode tags to remove. |
283
|
|
|
*/ |
284
|
|
|
$shortcodes_to_remove = apply_filters( 'jetpack_sync_do_not_expand_shortcodes', array( |
285
|
|
|
'gallery', |
286
|
|
|
'slideshow' |
287
|
|
|
) ); |
288
|
|
|
$removed_shortcode_callbacks = array(); |
289
|
|
|
foreach ( $shortcodes_to_remove as $shortcode ) { |
290
|
|
|
if ( isset ( $shortcode_tags[ $shortcode ] ) ) { |
291
|
|
|
$removed_shortcode_callbacks[ $shortcode ] = $shortcode_tags[ $shortcode ]; |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
array_map( 'remove_shortcode', array_keys( $removed_shortcode_callbacks ) ); |
296
|
|
|
|
297
|
|
|
$post->post_content_filtered = apply_filters( 'the_content', $post->post_content ); |
298
|
|
|
$post->post_excerpt_filtered = apply_filters( 'the_excerpt', $post->post_excerpt ); |
299
|
|
|
|
300
|
|
|
foreach ( $removed_shortcode_callbacks as $shortcode => $callback ) { |
301
|
|
|
add_shortcode( $shortcode, $callback ); |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
$this->add_embed(); |
306
|
|
|
|
307
|
|
|
if ( has_post_thumbnail( $post->ID ) ) { |
308
|
|
|
$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); |
309
|
|
|
if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) { |
310
|
|
|
$post->featured_image = $image_attributes[0]; |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
$post->permalink = get_permalink( $post->ID ); |
315
|
|
|
$post->shortlink = wp_get_shortlink( $post->ID ); |
316
|
|
|
|
317
|
|
|
return $post; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
public function save_published( $new_status, $old_status, $post ) { |
321
|
|
|
if ( 'publish' === $new_status && 'publish' !== $old_status ) { |
322
|
|
|
$this->just_published[ $post->ID ] = true; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
$this->previous_status[ $post->ID ] = $old_status; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
public function wp_insert_post( $post_ID, $post = null, $update = null ) { |
329
|
|
|
if ( ! is_numeric( $post_ID ) || is_null( $post ) ) { |
330
|
|
|
return; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
// workaround for https://github.com/woocommerce/woocommerce/issues/18007 |
334
|
|
|
if ( $post && 'shop_order' === $post->post_type ) { |
335
|
|
|
$post = get_post( $post_ID ); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
$previous_status = isset( $this->previous_status[ $post_ID ] ) ? |
339
|
|
|
$this->previous_status[ $post_ID ] : |
340
|
|
|
self::DEFAULT_PREVIOUS_STATE; |
341
|
|
|
|
342
|
|
|
$just_published = isset( $this->just_published[ $post_ID ] ) ? |
343
|
|
|
$this->just_published[ $post_ID ] : |
344
|
|
|
false; |
345
|
|
|
|
346
|
|
|
$state = array( |
347
|
|
|
'is_auto_save' => (bool) Jetpack_Constants::get_constant( 'DOING_AUTOSAVE' ), |
348
|
|
|
'previous_status' => $previous_status, |
349
|
|
|
'just_published' => $just_published |
350
|
|
|
); |
351
|
|
|
/** |
352
|
|
|
* Filter that is used to add to the post flags ( meta data ) when a post gets published |
353
|
|
|
* |
354
|
|
|
* @since 5.8.0 |
355
|
|
|
* |
356
|
|
|
* @param int $post_ID the post ID |
357
|
|
|
* @param mixed $post WP_POST object |
358
|
|
|
* @param bool $update Whether this is an existing post being updated or not. |
359
|
|
|
* @param mixed $state state |
360
|
|
|
* |
361
|
|
|
* @module sync |
362
|
|
|
*/ |
363
|
|
|
do_action( 'jetpack_sync_save_post', $post_ID, $post, $update, $state ); |
364
|
|
|
unset( $this->previous_status[ $post_ID ] ); |
365
|
|
|
$this->send_published( $post_ID, $post ); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
public function send_published( $post_ID, $post ) { |
369
|
|
|
if ( ! isset( $this->just_published[ $post_ID ] ) ) { |
370
|
|
|
return; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
// Post revisions cause race conditions where this send_published add the action before the actual post gets synced |
374
|
|
|
if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
375
|
|
|
return; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
$post_flags = array( |
379
|
|
|
'post_type' => $post->post_type |
380
|
|
|
); |
381
|
|
|
|
382
|
|
|
$author_user_object = get_user_by( 'id', $post->post_author ); |
383
|
|
|
if ( $author_user_object ) { |
384
|
|
|
$post_flags['author'] = array( |
385
|
|
|
'id' => $post->post_author, |
386
|
|
|
'wpcom_user_id' => get_user_meta( $post->post_author, 'wpcom_user_id', true ), |
387
|
|
|
'display_name' => $author_user_object->display_name, |
388
|
|
|
'email' => $author_user_object->user_email, |
389
|
|
|
'translated_role' => Jetpack::translate_user_to_role( $author_user_object ), |
390
|
|
|
); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Filter that is used to add to the post flags ( meta data ) when a post gets published |
395
|
|
|
* |
396
|
|
|
* @since 4.4.0 |
397
|
|
|
* |
398
|
|
|
* @param mixed array post flags that are added to the post |
399
|
|
|
* @param mixed $post WP_POST object |
400
|
|
|
*/ |
401
|
|
|
$flags = apply_filters( 'jetpack_published_post_flags', $post_flags, $post ); |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* Action that gets synced when a post type gets published. |
405
|
|
|
* |
406
|
|
|
* @since 4.4.0 |
407
|
|
|
* |
408
|
|
|
* @param int $post_ID |
409
|
|
|
* @param mixed array $flags post flags that are added to the post |
410
|
|
|
*/ |
411
|
|
|
do_action( 'jetpack_published_post', $post_ID, $flags ); |
412
|
|
|
unset( $this->just_published[ $post_ID ] ); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
public function expand_post_ids( $args ) { |
416
|
|
|
$post_ids = $args[0]; |
417
|
|
|
|
418
|
|
|
$posts = array_filter( array_map( array( 'WP_Post', 'get_instance' ), $post_ids ) ); |
419
|
|
|
$posts = array_map( array( $this, 'filter_post_content_and_add_links' ), $posts ); |
420
|
|
|
$posts = array_values( $posts ); // reindex in case posts were deleted |
421
|
|
|
|
422
|
|
|
return array( |
423
|
|
|
$posts, |
424
|
|
|
$this->get_metadata( $post_ids, 'post', Jetpack_Sync_Settings::get_setting( 'post_meta_whitelist' ) ), |
425
|
|
|
$this->get_term_relationships( $post_ids ), |
426
|
|
|
); |
427
|
|
|
} |
428
|
|
|
} |
429
|
|
|
|