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