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