|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Posts sync module. |
|
4
|
|
|
* |
|
5
|
|
|
* @package automattic/jetpack-sync |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Sync\Modules; |
|
9
|
|
|
|
|
10
|
|
|
use Automattic\Jetpack\Constants as Jetpack_Constants; |
|
11
|
|
|
use Automattic\Jetpack\Roles; |
|
12
|
|
|
use Automattic\Jetpack\Sync\Settings; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class to handle sync for posts. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Posts extends Module { |
|
18
|
|
|
/** |
|
19
|
|
|
* The post IDs of posts that were just published but not synced yet. |
|
20
|
|
|
* |
|
21
|
|
|
* @access private |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $just_published = array(); |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The previous status of posts that we use for calculating post status transitions. |
|
29
|
|
|
* |
|
30
|
|
|
* @access private |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private $previous_status = array(); |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Action handler callable. |
|
38
|
|
|
* |
|
39
|
|
|
* @access private |
|
40
|
|
|
* |
|
41
|
|
|
* @var callable |
|
42
|
|
|
*/ |
|
43
|
|
|
private $action_handler; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Import end. |
|
47
|
|
|
* |
|
48
|
|
|
* @access private |
|
49
|
|
|
* |
|
50
|
|
|
* @todo This appears to be unused - let's remove it. |
|
51
|
|
|
* |
|
52
|
|
|
* @var boolean |
|
53
|
|
|
*/ |
|
54
|
|
|
private $import_end = false; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Default previous post state. |
|
58
|
|
|
* Used for default previous post status. |
|
59
|
|
|
* |
|
60
|
|
|
* @access public |
|
61
|
|
|
* |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
const DEFAULT_PREVIOUS_STATE = 'new'; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Sync module name. |
|
68
|
|
|
* |
|
69
|
|
|
* @access public |
|
70
|
|
|
* |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
public function name() { |
|
74
|
|
|
return 'posts'; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Retrieve a post by its ID. |
|
79
|
|
|
* |
|
80
|
|
|
* @access public |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $object_type Type of the sync object. |
|
83
|
|
|
* @param int $id ID of the sync object. |
|
84
|
|
|
* @return \WP_Post|bool Filtered \WP_Post object, or false if the object is not a post. |
|
85
|
|
|
*/ |
|
86
|
|
|
public function get_object_by_id( $object_type, $id ) { |
|
87
|
|
|
if ( 'post' === $object_type ) { |
|
88
|
|
|
$post = get_post( intval( $id ) ); |
|
89
|
|
|
if ( $post ) { |
|
90
|
|
|
return $this->filter_post_content_and_add_links( $post ); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Initialize posts action listeners. |
|
99
|
|
|
* |
|
100
|
|
|
* @access public |
|
101
|
|
|
* |
|
102
|
|
|
* @param callable $callable Action handler callable. |
|
103
|
|
|
*/ |
|
104
|
|
|
public function init_listeners( $callable ) { |
|
105
|
|
|
$this->action_handler = $callable; |
|
106
|
|
|
|
|
107
|
|
|
add_action( 'wp_insert_post', array( $this, 'wp_insert_post' ), 11, 3 ); |
|
108
|
|
|
add_action( 'jetpack_sync_save_post', $callable, 10, 4 ); |
|
109
|
|
|
|
|
110
|
|
|
add_action( 'deleted_post', $callable, 10 ); |
|
111
|
|
|
add_action( 'jetpack_published_post', $callable, 10, 2 ); |
|
112
|
|
|
|
|
113
|
|
|
add_action( 'transition_post_status', array( $this, 'save_published' ), 10, 3 ); |
|
114
|
|
|
add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_save_post', array( $this, 'filter_blacklisted_post_types' ) ); |
|
115
|
|
|
|
|
116
|
|
|
// Listen for meta changes. |
|
117
|
|
|
$this->init_listeners_for_meta_type( 'post', $callable ); |
|
118
|
|
|
$this->init_meta_whitelist_handler( 'post', array( $this, 'filter_meta' ) ); |
|
119
|
|
|
|
|
120
|
|
|
add_action( 'jetpack_daily_akismet_meta_cleanup_before', array( $this, 'daily_akismet_meta_cleanup_before' ) ); |
|
121
|
|
|
add_action( 'jetpack_daily_akismet_meta_cleanup_after', array( $this, 'daily_akismet_meta_cleanup_after' ) ); |
|
122
|
|
|
add_action( 'jetpack_post_meta_batch_delete', $callable, 10, 2 ); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Before Akismet's daily cleanup of spam detection metadata. |
|
127
|
|
|
* |
|
128
|
|
|
* @access public |
|
129
|
|
|
* |
|
130
|
|
|
* @param array $feedback_ids IDs of feedback posts. |
|
131
|
|
|
*/ |
|
132
|
|
|
public function daily_akismet_meta_cleanup_before( $feedback_ids ) { |
|
133
|
|
|
remove_action( 'deleted_post_meta', $this->action_handler ); |
|
134
|
|
|
/** |
|
135
|
|
|
* Used for syncing deletion of batch post meta |
|
136
|
|
|
* |
|
137
|
|
|
* @since 6.1.0 |
|
138
|
|
|
* |
|
139
|
|
|
* @module sync |
|
140
|
|
|
* |
|
141
|
|
|
* @param array $feedback_ids feedback post IDs |
|
142
|
|
|
* @param string $meta_key to be deleted |
|
143
|
|
|
*/ |
|
144
|
|
|
do_action( 'jetpack_post_meta_batch_delete', $feedback_ids, '_feedback_akismet_values' ); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* After Akismet's daily cleanup of spam detection metadata. |
|
149
|
|
|
* |
|
150
|
|
|
* @access public |
|
151
|
|
|
* |
|
152
|
|
|
* @param array $feedback_ids IDs of feedback posts. |
|
153
|
|
|
*/ |
|
154
|
|
|
public function daily_akismet_meta_cleanup_after( $feedback_ids ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
155
|
|
|
add_action( 'deleted_post_meta', $this->action_handler ); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Initialize posts action listeners for full sync. |
|
160
|
|
|
* |
|
161
|
|
|
* @access public |
|
162
|
|
|
* |
|
163
|
|
|
* @param callable $callable Action handler callable. |
|
164
|
|
|
*/ |
|
165
|
|
|
public function init_full_sync_listeners( $callable ) { |
|
166
|
|
|
add_action( 'jetpack_full_sync_posts', $callable ); // Also sends post meta. |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Initialize the module in the sender. |
|
171
|
|
|
* |
|
172
|
|
|
* @access public |
|
173
|
|
|
*/ |
|
174
|
|
|
public function init_before_send() { |
|
175
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_sync_save_post', array( $this, 'expand_jetpack_sync_save_post' ) ); |
|
176
|
|
|
|
|
177
|
|
|
// Full sync. |
|
178
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_posts', array( $this, 'expand_post_ids' ) ); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Enqueue the posts actions for full sync. |
|
183
|
|
|
* |
|
184
|
|
|
* @access public |
|
185
|
|
|
* |
|
186
|
|
|
* @param array $config Full sync configuration for this sync module. |
|
187
|
|
|
* @param int $max_items_to_enqueue Maximum number of items to enqueue. |
|
188
|
|
|
* @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
|
189
|
|
|
* @return array Number of actions enqueued, and next module state. |
|
190
|
|
|
*/ |
|
191
|
|
|
public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
|
192
|
|
|
global $wpdb; |
|
193
|
|
|
|
|
194
|
|
|
return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_posts', $wpdb->posts, 'ID', $this->get_where_sql( $config ), $max_items_to_enqueue, $state ); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Retrieve an estimated number of actions that will be enqueued. |
|
199
|
|
|
* |
|
200
|
|
|
* @access public |
|
201
|
|
|
* |
|
202
|
|
|
* @todo Use $wpdb->prepare for the SQL query. |
|
203
|
|
|
* |
|
204
|
|
|
* @param array $config Full sync configuration for this sync module. |
|
205
|
|
|
* @return array Number of items yet to be enqueued. |
|
206
|
|
|
*/ |
|
207
|
|
View Code Duplication |
public function estimate_full_sync_actions( $config ) { |
|
208
|
|
|
global $wpdb; |
|
209
|
|
|
|
|
210
|
|
|
$query = "SELECT count(*) FROM $wpdb->posts WHERE " . $this->get_where_sql( $config ); |
|
211
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
212
|
|
|
$count = $wpdb->get_var( $query ); |
|
213
|
|
|
|
|
214
|
|
|
return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Retrieve the WHERE SQL clause based on the module config. |
|
219
|
|
|
* |
|
220
|
|
|
* @access private |
|
221
|
|
|
* |
|
222
|
|
|
* @param array $config Full sync configuration for this sync module. |
|
223
|
|
|
* @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
|
224
|
|
|
*/ |
|
225
|
|
View Code Duplication |
private function get_where_sql( $config ) { |
|
226
|
|
|
$where_sql = Settings::get_blacklisted_post_types_sql(); |
|
227
|
|
|
|
|
228
|
|
|
// Config is a list of post IDs to sync. |
|
229
|
|
|
if ( is_array( $config ) ) { |
|
230
|
|
|
$where_sql .= ' AND ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')'; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
return $where_sql; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Retrieve the actions that will be sent for this module during a full sync. |
|
238
|
|
|
* |
|
239
|
|
|
* @access public |
|
240
|
|
|
* |
|
241
|
|
|
* @return array Full sync actions of this module. |
|
242
|
|
|
*/ |
|
243
|
|
|
public function get_full_sync_actions() { |
|
244
|
|
|
return array( 'jetpack_full_sync_posts' ); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Process content before send. |
|
249
|
|
|
* |
|
250
|
|
|
* @param array $args Arguments of the `wp_insert_post` hook. |
|
251
|
|
|
* |
|
252
|
|
|
* @return array |
|
253
|
|
|
*/ |
|
254
|
|
|
public function expand_jetpack_sync_save_post( $args ) { |
|
255
|
|
|
list( $post_id, $post, $update, $previous_state ) = $args; |
|
256
|
|
|
return array( $post_id, $this->filter_post_content_and_add_links( $post ), $update, $previous_state ); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Filter all blacklisted post types. |
|
261
|
|
|
* |
|
262
|
|
|
* @param array $args Hook arguments. |
|
263
|
|
|
* @return array|false Hook arguments, or false if the post type is a blacklisted one. |
|
264
|
|
|
*/ |
|
265
|
|
|
public function filter_blacklisted_post_types( $args ) { |
|
266
|
|
|
$post = $args[1]; |
|
267
|
|
|
|
|
268
|
|
|
if ( in_array( $post->post_type, Settings::get_setting( 'post_types_blacklist' ), true ) ) { |
|
269
|
|
|
return false; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
return $args; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Filter all meta that is not blacklisted, or is stored for a disallowed post type. |
|
277
|
|
|
* |
|
278
|
|
|
* @param array $args Hook arguments. |
|
279
|
|
|
* @return array|false Hook arguments, or false if meta was filtered. |
|
280
|
|
|
*/ |
|
281
|
|
|
public function filter_meta( $args ) { |
|
282
|
|
|
if ( $this->is_post_type_allowed( $args[1] ) && $this->is_whitelisted_post_meta( $args[2] ) ) { |
|
283
|
|
|
return $args; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
return false; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Whether a post meta key is whitelisted. |
|
291
|
|
|
* |
|
292
|
|
|
* @param string $meta_key Meta key. |
|
293
|
|
|
* @return boolean Whether the post meta key is whitelisted. |
|
294
|
|
|
*/ |
|
295
|
|
|
public function is_whitelisted_post_meta( $meta_key ) { |
|
296
|
|
|
// The _wpas_skip_ meta key is used by Publicize. |
|
297
|
|
|
return in_array( $meta_key, Settings::get_setting( 'post_meta_whitelist' ), true ) || wp_startswith( $meta_key, '_wpas_skip_' ); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* Whether a post type is allowed. |
|
302
|
|
|
* A post type will be disallowed if it's present in the post type blacklist. |
|
303
|
|
|
* |
|
304
|
|
|
* @param int $post_id ID of the post. |
|
305
|
|
|
* @return boolean Whether the post type is allowed. |
|
306
|
|
|
*/ |
|
307
|
|
|
public function is_post_type_allowed( $post_id ) { |
|
308
|
|
|
$post = get_post( intval( $post_id ) ); |
|
309
|
|
|
if ( $post->post_type ) { |
|
310
|
|
|
return ! in_array( $post->post_type, Settings::get_setting( 'post_types_blacklist' ), true ); |
|
311
|
|
|
} |
|
312
|
|
|
return false; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* Remove the embed shortcode. |
|
317
|
|
|
* |
|
318
|
|
|
* @global $wp_embed |
|
319
|
|
|
*/ |
|
320
|
|
View Code Duplication |
public function remove_embed() { |
|
321
|
|
|
global $wp_embed; |
|
322
|
|
|
remove_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 ); |
|
323
|
|
|
// remove the embed shortcode since we would do the part later. |
|
324
|
|
|
remove_shortcode( 'embed' ); |
|
325
|
|
|
// Attempts to embed all URLs in a post. |
|
326
|
|
|
remove_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 ); |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
/** |
|
330
|
|
|
* Add the embed shortcode. |
|
331
|
|
|
* |
|
332
|
|
|
* @global $wp_embed |
|
333
|
|
|
*/ |
|
334
|
|
View Code Duplication |
public function add_embed() { |
|
335
|
|
|
global $wp_embed; |
|
336
|
|
|
add_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 ); |
|
337
|
|
|
// Shortcode placeholder for strip_shortcodes(). |
|
338
|
|
|
add_shortcode( 'embed', '__return_false' ); |
|
339
|
|
|
// Attempts to embed all URLs in a post. |
|
340
|
|
|
add_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 ); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* Expands wp_insert_post to include filtered content |
|
345
|
|
|
* |
|
346
|
|
|
* @param \WP_Post $post_object Post object. |
|
347
|
|
|
*/ |
|
348
|
|
|
public function filter_post_content_and_add_links( $post_object ) { |
|
349
|
|
|
global $post; |
|
350
|
|
|
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
351
|
|
|
$post = $post_object; |
|
352
|
|
|
|
|
353
|
|
|
// Return non existant post. |
|
354
|
|
|
$post_type = get_post_type_object( $post->post_type ); |
|
355
|
|
View Code Duplication |
if ( empty( $post_type ) || ! is_object( $post_type ) ) { |
|
356
|
|
|
$non_existant_post = new \stdClass(); |
|
357
|
|
|
$non_existant_post->ID = $post->ID; |
|
358
|
|
|
$non_existant_post->post_modified = $post->post_modified; |
|
359
|
|
|
$non_existant_post->post_modified_gmt = $post->post_modified_gmt; |
|
360
|
|
|
$non_existant_post->post_status = 'jetpack_sync_non_registered_post_type'; |
|
361
|
|
|
$non_existant_post->post_type = $post->post_type; |
|
362
|
|
|
|
|
363
|
|
|
return $non_existant_post; |
|
364
|
|
|
} |
|
365
|
|
|
/** |
|
366
|
|
|
* Filters whether to prevent sending post data to .com |
|
367
|
|
|
* |
|
368
|
|
|
* Passing true to the filter will prevent the post data from being sent |
|
369
|
|
|
* to the WordPress.com. |
|
370
|
|
|
* Instead we pass data that will still enable us to do a checksum against the |
|
371
|
|
|
* Jetpacks data but will prevent us from displaying the data on in the API as well as |
|
372
|
|
|
* other services. |
|
373
|
|
|
* |
|
374
|
|
|
* @since 4.2.0 |
|
375
|
|
|
* |
|
376
|
|
|
* @param boolean false prevent post data from being synced to WordPress.com |
|
377
|
|
|
* @param mixed $post \WP_Post object |
|
378
|
|
|
*/ |
|
379
|
|
View Code Duplication |
if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) { |
|
380
|
|
|
// We only send the bare necessary object to be able to create a checksum. |
|
381
|
|
|
$blocked_post = new \stdClass(); |
|
382
|
|
|
$blocked_post->ID = $post->ID; |
|
383
|
|
|
$blocked_post->post_modified = $post->post_modified; |
|
384
|
|
|
$blocked_post->post_modified_gmt = $post->post_modified_gmt; |
|
385
|
|
|
$blocked_post->post_status = 'jetpack_sync_blocked'; |
|
386
|
|
|
$blocked_post->post_type = $post->post_type; |
|
387
|
|
|
|
|
388
|
|
|
return $blocked_post; |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
// lets not do oembed just yet. |
|
392
|
|
|
$this->remove_embed(); |
|
393
|
|
|
|
|
394
|
|
|
if ( 0 < strlen( $post->post_password ) ) { |
|
395
|
|
|
$post->post_password = 'auto-' . wp_generate_password( 10, false ); |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
/** This filter is already documented in core. wp-includes/post-template.php */ |
|
399
|
|
|
if ( Settings::get_setting( 'render_filtered_content' ) && $post_type->public ) { |
|
400
|
|
|
global $shortcode_tags; |
|
401
|
|
|
/** |
|
402
|
|
|
* Filter prevents some shortcodes from expanding. |
|
403
|
|
|
* |
|
404
|
|
|
* Since we can can expand some type of shortcode better on the .com side and make the |
|
405
|
|
|
* expansion more relevant to contexts. For example [galleries] and subscription emails |
|
406
|
|
|
* |
|
407
|
|
|
* @since 4.5.0 |
|
408
|
|
|
* |
|
409
|
|
|
* @param array of shortcode tags to remove. |
|
410
|
|
|
*/ |
|
411
|
|
|
$shortcodes_to_remove = apply_filters( |
|
412
|
|
|
'jetpack_sync_do_not_expand_shortcodes', |
|
413
|
|
|
array( |
|
414
|
|
|
'gallery', |
|
415
|
|
|
'slideshow', |
|
416
|
|
|
) |
|
417
|
|
|
); |
|
418
|
|
|
$removed_shortcode_callbacks = array(); |
|
419
|
|
|
foreach ( $shortcodes_to_remove as $shortcode ) { |
|
420
|
|
|
if ( isset( $shortcode_tags[ $shortcode ] ) ) { |
|
421
|
|
|
$removed_shortcode_callbacks[ $shortcode ] = $shortcode_tags[ $shortcode ]; |
|
422
|
|
|
} |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
|
|
array_map( 'remove_shortcode', array_keys( $removed_shortcode_callbacks ) ); |
|
426
|
|
|
|
|
427
|
|
|
$post->post_content_filtered = apply_filters( 'the_content', $post->post_content ); |
|
428
|
|
|
$post->post_excerpt_filtered = apply_filters( 'the_excerpt', $post->post_excerpt ); |
|
429
|
|
|
|
|
430
|
|
|
foreach ( $removed_shortcode_callbacks as $shortcode => $callback ) { |
|
431
|
|
|
add_shortcode( $shortcode, $callback ); |
|
432
|
|
|
} |
|
433
|
|
|
} |
|
434
|
|
|
|
|
435
|
|
|
$this->add_embed(); |
|
436
|
|
|
|
|
437
|
|
|
if ( has_post_thumbnail( $post->ID ) ) { |
|
438
|
|
|
$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); |
|
439
|
|
|
if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) { |
|
440
|
|
|
$post->featured_image = $image_attributes[0]; |
|
441
|
|
|
} |
|
442
|
|
|
} |
|
443
|
|
|
|
|
444
|
|
|
$post->permalink = get_permalink( $post->ID ); |
|
445
|
|
|
$post->shortlink = wp_get_shortlink( $post->ID ); |
|
446
|
|
|
|
|
447
|
|
|
if ( function_exists( 'amp_get_permalink' ) ) { |
|
448
|
|
|
$post->amp_permalink = amp_get_permalink( $post->ID ); |
|
449
|
|
|
} |
|
450
|
|
|
|
|
451
|
|
|
return $post; |
|
452
|
|
|
} |
|
453
|
|
|
|
|
454
|
|
|
/** |
|
455
|
|
|
* Handle transition from another post status to a published one. |
|
456
|
|
|
* |
|
457
|
|
|
* @param string $new_status New post status. |
|
458
|
|
|
* @param string $old_status Old post status. |
|
459
|
|
|
* @param \WP_Post $post Post object. |
|
460
|
|
|
*/ |
|
461
|
|
|
public function save_published( $new_status, $old_status, $post ) { |
|
462
|
|
|
if ( 'publish' === $new_status && 'publish' !== $old_status ) { |
|
463
|
|
|
$this->just_published[ $post->ID ] = true; |
|
464
|
|
|
} |
|
465
|
|
|
|
|
466
|
|
|
$this->previous_status[ $post->ID ] = $old_status; |
|
467
|
|
|
} |
|
468
|
|
|
|
|
469
|
|
|
/** |
|
470
|
|
|
* When publishing or updating a post, the Gutenberg editor sends two requests: |
|
471
|
|
|
* 1. sent to WP REST API endpoint `wp-json/wp/v2/posts/$id` |
|
472
|
|
|
* 2. sent to wp-admin/post.php `?post=$id&action=edit&classic-editor=1&meta_box=1` |
|
473
|
|
|
* |
|
474
|
|
|
* The 2nd request is to update post meta, which is not supported on WP REST API. |
|
475
|
|
|
* When syncing post data, we will include if this was a meta box update. |
|
476
|
|
|
* |
|
477
|
|
|
* @todo Implement nonce verification. |
|
478
|
|
|
* |
|
479
|
|
|
* @return boolean Whether this is a Gutenberg meta box update. |
|
480
|
|
|
*/ |
|
481
|
|
|
public function is_gutenberg_meta_box_update() { |
|
482
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended |
|
483
|
|
|
return ( |
|
484
|
|
|
isset( $_POST['action'], $_GET['classic-editor'], $_GET['meta_box'] ) && |
|
485
|
|
|
'editpost' === $_POST['action'] && |
|
486
|
|
|
'1' === $_GET['classic-editor'] && |
|
487
|
|
|
'1' === $_GET['meta_box'] |
|
488
|
|
|
// phpcs:enable WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended |
|
489
|
|
|
); |
|
490
|
|
|
} |
|
491
|
|
|
|
|
492
|
|
|
/** |
|
493
|
|
|
* Handler for the wp_insert_post hook. |
|
494
|
|
|
* Called upon creation of a new post. |
|
495
|
|
|
* |
|
496
|
|
|
* @param int $post_ID Post ID. |
|
|
|
|
|
|
497
|
|
|
* @param \WP_Post $post Post object. |
|
498
|
|
|
* @param boolean $update Whether this is an existing post being updated or not. |
|
|
|
|
|
|
499
|
|
|
*/ |
|
500
|
|
|
public function wp_insert_post( $post_ID, $post = null, $update = null ) { |
|
501
|
|
|
if ( ! is_numeric( $post_ID ) || is_null( $post ) ) { |
|
502
|
|
|
return; |
|
503
|
|
|
} |
|
504
|
|
|
|
|
505
|
|
|
// Workaround for https://github.com/woocommerce/woocommerce/issues/18007. |
|
506
|
|
|
if ( $post && 'shop_order' === $post->post_type ) { |
|
507
|
|
|
$post = get_post( $post_ID ); |
|
508
|
|
|
} |
|
509
|
|
|
|
|
510
|
|
|
$previous_status = isset( $this->previous_status[ $post_ID ] ) ? |
|
511
|
|
|
$this->previous_status[ $post_ID ] : |
|
512
|
|
|
self::DEFAULT_PREVIOUS_STATE; |
|
513
|
|
|
|
|
514
|
|
|
$just_published = isset( $this->just_published[ $post_ID ] ) ? |
|
515
|
|
|
$this->just_published[ $post_ID ] : |
|
516
|
|
|
false; |
|
517
|
|
|
|
|
518
|
|
|
$state = array( |
|
519
|
|
|
'is_auto_save' => (bool) Jetpack_Constants::get_constant( 'DOING_AUTOSAVE' ), |
|
520
|
|
|
'previous_status' => $previous_status, |
|
521
|
|
|
'just_published' => $just_published, |
|
522
|
|
|
'is_gutenberg_meta_box_update' => $this->is_gutenberg_meta_box_update(), |
|
523
|
|
|
); |
|
524
|
|
|
/** |
|
525
|
|
|
* Filter that is used to add to the post flags ( meta data ) when a post gets published |
|
526
|
|
|
* |
|
527
|
|
|
* @since 5.8.0 |
|
528
|
|
|
* |
|
529
|
|
|
* @param int $post_ID the post ID |
|
530
|
|
|
* @param mixed $post \WP_Post object |
|
531
|
|
|
* @param bool $update Whether this is an existing post being updated or not. |
|
532
|
|
|
* @param mixed $state state |
|
533
|
|
|
* |
|
534
|
|
|
* @module sync |
|
535
|
|
|
*/ |
|
536
|
|
|
do_action( 'jetpack_sync_save_post', $post_ID, $post, $update, $state ); |
|
537
|
|
|
unset( $this->previous_status[ $post_ID ] ); |
|
538
|
|
|
$this->send_published( $post_ID, $post ); |
|
539
|
|
|
} |
|
540
|
|
|
|
|
541
|
|
|
/** |
|
542
|
|
|
* Send a published post for sync. |
|
543
|
|
|
* |
|
544
|
|
|
* @param int $post_ID Post ID. |
|
545
|
|
|
* @param \WP_Post $post Post object. |
|
546
|
|
|
*/ |
|
547
|
|
|
public function send_published( $post_ID, $post ) { |
|
548
|
|
|
if ( ! isset( $this->just_published[ $post_ID ] ) ) { |
|
549
|
|
|
return; |
|
550
|
|
|
} |
|
551
|
|
|
|
|
552
|
|
|
// Post revisions cause race conditions where this send_published add the action before the actual post gets synced. |
|
553
|
|
|
if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
554
|
|
|
return; |
|
555
|
|
|
} |
|
556
|
|
|
|
|
557
|
|
|
$post_flags = array( |
|
558
|
|
|
'post_type' => $post->post_type, |
|
559
|
|
|
); |
|
560
|
|
|
|
|
561
|
|
|
$author_user_object = get_user_by( 'id', $post->post_author ); |
|
562
|
|
|
if ( $author_user_object ) { |
|
563
|
|
|
$roles = new Roles(); |
|
564
|
|
|
|
|
565
|
|
|
$post_flags['author'] = array( |
|
566
|
|
|
'id' => $post->post_author, |
|
567
|
|
|
'wpcom_user_id' => get_user_meta( $post->post_author, 'wpcom_user_id', true ), |
|
568
|
|
|
'display_name' => $author_user_object->display_name, |
|
569
|
|
|
'email' => $author_user_object->user_email, |
|
570
|
|
|
'translated_role' => $roles->translate_user_to_role( $author_user_object ), |
|
571
|
|
|
); |
|
572
|
|
|
} |
|
573
|
|
|
|
|
574
|
|
|
/** |
|
575
|
|
|
* Filter that is used to add to the post flags ( meta data ) when a post gets published |
|
576
|
|
|
* |
|
577
|
|
|
* @since 4.4.0 |
|
578
|
|
|
* |
|
579
|
|
|
* @param mixed array post flags that are added to the post |
|
580
|
|
|
* @param mixed $post \WP_Post object |
|
581
|
|
|
*/ |
|
582
|
|
|
$flags = apply_filters( 'jetpack_published_post_flags', $post_flags, $post ); |
|
583
|
|
|
|
|
584
|
|
|
/** |
|
585
|
|
|
* Action that gets synced when a post type gets published. |
|
586
|
|
|
* |
|
587
|
|
|
* @since 4.4.0 |
|
588
|
|
|
* |
|
589
|
|
|
* @param int $post_ID |
|
590
|
|
|
* @param mixed array $flags post flags that are added to the post |
|
591
|
|
|
*/ |
|
592
|
|
|
do_action( 'jetpack_published_post', $post_ID, $flags ); |
|
593
|
|
|
unset( $this->just_published[ $post_ID ] ); |
|
594
|
|
|
|
|
595
|
|
|
/** |
|
596
|
|
|
* Send additional sync action for Activity Log when post is a Customizer publish |
|
597
|
|
|
*/ |
|
598
|
|
|
if ( 'customize_changeset' === $post->post_type ) { |
|
599
|
|
|
$post_content = json_decode( $post->post_content, true ); |
|
600
|
|
|
foreach ( $post_content as $key => $value ) { |
|
601
|
|
|
// Skip if it isn't a widget. |
|
602
|
|
|
if ( 'widget_' !== substr( $key, 0, strlen( 'widget_' ) ) ) { |
|
603
|
|
|
continue; |
|
604
|
|
|
} |
|
605
|
|
|
// Change key from "widget_archives[2]" to "archives-2". |
|
606
|
|
|
$key = str_replace( 'widget_', '', $key ); |
|
607
|
|
|
$key = str_replace( '[', '-', $key ); |
|
608
|
|
|
$key = str_replace( ']', '', $key ); |
|
609
|
|
|
|
|
610
|
|
|
global $wp_registered_widgets; |
|
611
|
|
|
if ( isset( $wp_registered_widgets[ $key ] ) ) { |
|
612
|
|
|
$widget_data = array( |
|
613
|
|
|
'name' => $wp_registered_widgets[ $key ]['name'], |
|
614
|
|
|
'id' => $key, |
|
615
|
|
|
'title' => $value['value']['title'], |
|
616
|
|
|
); |
|
617
|
|
|
do_action( 'jetpack_widget_edited', $widget_data ); |
|
618
|
|
|
} |
|
619
|
|
|
} |
|
620
|
|
|
} |
|
621
|
|
|
} |
|
622
|
|
|
|
|
623
|
|
|
/** |
|
624
|
|
|
* Expand post IDs to post objects within a hook before they are serialized and sent to the server. |
|
625
|
|
|
* |
|
626
|
|
|
* @access public |
|
627
|
|
|
* |
|
628
|
|
|
* @param array $args The hook parameters. |
|
629
|
|
|
* @return array $args The expanded hook parameters. |
|
630
|
|
|
*/ |
|
631
|
|
|
public function expand_post_ids( $args ) { |
|
632
|
|
|
list( $post_ids, $previous_interval_end) = $args; |
|
633
|
|
|
|
|
634
|
|
|
$posts = array_filter( array_map( array( 'WP_Post', 'get_instance' ), $post_ids ) ); |
|
635
|
|
|
$posts = array_map( array( $this, 'filter_post_content_and_add_links' ), $posts ); |
|
636
|
|
|
$posts = array_values( $posts ); // Reindex in case posts were deleted. |
|
637
|
|
|
|
|
638
|
|
|
return array( |
|
639
|
|
|
$posts, |
|
640
|
|
|
$this->get_metadata( $post_ids, 'post', Settings::get_setting( 'post_meta_whitelist' ) ), |
|
641
|
|
|
$this->get_term_relationships( $post_ids ), |
|
642
|
|
|
$previous_interval_end, |
|
643
|
|
|
); |
|
644
|
|
|
} |
|
645
|
|
|
} |
|
646
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.