← All changes
|
jetpack_vendor/automattic/jetpack-sync/src/modules/class-comments.php
+128
-22
13.7.2
→
16.3-a.5
View file →
| @@ -6,15 +6,21 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace Automattic\Jetpack\Sync\Modules; |
| 9 | 9 | |
| 10 | +use Automattic\Jetpack\Sync\Defaults; | |
| 10 | 11 | use Automattic\Jetpack\Sync\Modules; |
| 11 | 12 | use Automattic\Jetpack\Sync\Settings; |
| 12 | 13 | |
| 14 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 15 | + exit( 0 ); | |
| 16 | +} | |
| 17 | + | |
| 13 | 18 | /** |
| 14 | 19 | * Class to handle sync for comments. |
| 15 | 20 | */ |
| 16 | 21 | class Comments extends Module { |
| 22 | + | |
| 17 | 23 | /** |
| 18 | 24 | * Sync module name. |
| 19 | 25 | * |
| 20 | 26 | * @access public |
| @@ -36,19 +42,33 @@ | ||
| 36 | 42 | return 'comment_ID'; |
| 37 | 43 | } |
| 38 | 44 | |
| 39 | 45 | /** |
| 40 | - * The table in the database. | |
| 46 | + * The table name. | |
| 41 | 47 | * |
| 42 | 48 | * @access public |
| 43 | 49 | * |
| 44 | 50 | * @return string |
| 51 | + * @deprecated since 3.11.0 Use table() instead. | |
| 45 | 52 | */ |
| 46 | 53 | public function table_name() { |
| 54 | + _deprecated_function( __METHOD__, '3.11.0', 'Automattic\\Jetpack\\Sync\\Comments->table' ); | |
| 47 | 55 | return 'comments'; |
| 48 | 56 | } |
| 49 | 57 | |
| 50 | 58 | /** |
| 59 | + * The table in the database with the prefix. | |
| 60 | + * | |
| 61 | + * @access public | |
| 62 | + * | |
| 63 | + * @return string|bool | |
| 64 | + */ | |
| 65 | + public function table() { | |
| 66 | + global $wpdb; | |
| 67 | + return $wpdb->comments; | |
| 68 | + } | |
| 69 | + | |
| 70 | + /** | |
| 51 | 71 | * Retrieve a comment by its ID. |
| 52 | 72 | * |
| 53 | 73 | * @access public |
| 54 | 74 | * |
| @@ -185,26 +205,27 @@ | ||
| 185 | 205 | * Gets a filtered list of comment types that sync can hook into. |
| 186 | 206 | * |
| 187 | 207 | * @access public |
| 188 | 208 | * |
| 189 | - * @return array Defaults to [ '', 'trackback', 'pingback' ]. | |
| 209 | + * @return array Defaults to [ '', 'comment', 'trackback', 'pingback', 'review', 'note' ]. | |
| 190 | 210 | */ |
| 191 | 211 | public function get_whitelisted_comment_types() { |
| 192 | - /** | |
| 193 | - * Comment types present in this list will sync their status changes to WordPress.com. | |
| 194 | - * | |
| 195 | - * @since 1.6.3 | |
| 196 | - * @since-jetpack 7.6.0 | |
| 197 | - * | |
| 198 | - * @param array A list of comment types. | |
| 199 | - */ | |
| 200 | - return apply_filters( | |
| 201 | - 'jetpack_sync_whitelisted_comment_types', | |
| 202 | - array( '', 'comment', 'trackback', 'pingback', 'review' ) | |
| 203 | - ); | |
| 212 | + return Defaults::get_comment_types_whitelist(); | |
| 204 | 213 | } |
| 205 | 214 | |
| 206 | 215 | /** |
| 216 | + * Returns escaped SQL for whitelisted comment types. | |
| 217 | + * Can be injected directly into a WHERE clause. | |
| 218 | + * | |
| 219 | + * @access public | |
| 220 | + * | |
| 221 | + * @return string SQL WHERE clause. | |
| 222 | + */ | |
| 223 | + public function get_whitelisted_comment_types_sql() { | |
| 224 | + return 'comment_type IN (\'' . implode( '\', \'', array_map( 'esc_sql', $this->get_whitelisted_comment_types() ) ) . '\')'; | |
| 225 | + } | |
| 226 | + | |
| 227 | + /** | |
| 207 | 228 | * Prevents any comment types that are not in the whitelist from being enqueued and sent to WordPress.com. |
| 208 | 229 | * |
| 209 | 230 | * @param array $args Arguments passed to wp_insert_comment, deleted_comment, spammed_comment, etc. |
| 210 | 231 | * |
| @@ -210,25 +231,30 @@ | ||
| 210 | 231 | * |
| 211 | 232 | * @return bool or array $args Arguments passed to wp_insert_comment, deleted_comment, spammed_comment, etc. |
| 212 | 233 | */ |
| 213 | 234 | public function only_allow_white_listed_comment_types( $args ) { |
| 235 | + if ( empty( $args ) ) { | |
| 236 | + return false; | |
| 237 | + } | |
| 238 | + | |
| 214 | 239 | $comment = false; |
| 215 | 240 | |
| 216 | 241 | if ( isset( $args[1] ) ) { |
| 217 | 242 | // comment object is available. |
| 218 | 243 | $comment = $args[1]; |
| 219 | - } elseif ( is_numeric( $args[0] ) ) { | |
| 244 | + } elseif ( isset( $args[0] ) && is_numeric( $args[0] ) ) { | |
| 220 | 245 | // comment_id is available. |
| 221 | 246 | $comment = get_comment( $args[0] ); |
| 222 | 247 | } |
| 223 | 248 | |
| 224 | - if ( | |
| 225 | - isset( $comment->comment_type ) | |
| 226 | - && ! in_array( $comment->comment_type, $this->get_whitelisted_comment_types(), true ) | |
| 227 | - ) { | |
| 249 | + if ( ! $comment instanceof \WP_Comment ) { | |
| 228 | 250 | return false; |
| 229 | 251 | } |
| 230 | 252 | |
| 253 | + if ( ! in_array( $comment->comment_type, $this->get_whitelisted_comment_types(), true ) ) { | |
| 254 | + return false; | |
| 255 | + } | |
| 256 | + | |
| 231 | 257 | return $args; |
| 232 | 258 | } |
| 233 | 259 | |
| 234 | 260 | /** |
| @@ -303,10 +329,16 @@ | ||
| 303 | 329 | * |
| 304 | 330 | * @access public |
| 305 | 331 | */ |
| 306 | 332 | public function init_before_send() { |
| 333 | + | |
| 307 | 334 | // Full sync. |
| 308 | - add_filter( 'jetpack_sync_before_send_jetpack_full_sync_comments', array( $this, 'expand_comment_ids' ) ); | |
| 335 | + $sync_module = Modules::get_module( 'full-sync' ); | |
| 336 | + if ( $sync_module instanceof Full_Sync_Immediately ) { | |
| 337 | + add_filter( 'jetpack_sync_before_send_jetpack_full_sync_comments', array( $this, 'extract_comments_and_meta' ) ); | |
| 338 | + } else { | |
| 339 | + add_filter( 'jetpack_sync_before_send_jetpack_full_sync_comments', array( $this, 'expand_comment_ids' ) ); | |
| 340 | + } | |
| 309 | 341 | } |
| 310 | 342 | |
| 311 | 343 | /** |
| 312 | 344 | * Enqueue the comments actions for full sync. |
| @@ -356,13 +388,15 @@ | ||
| 356 | 388 | * @param array $config Full sync configuration for this sync module. |
| 357 | 389 | * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
| 358 | 390 | */ |
| 359 | 391 | public function get_where_sql( $config ) { |
| 360 | - if ( is_array( $config ) ) { | |
| 392 | + $where_sql = $this->get_whitelisted_comment_types_sql(); | |
| 393 | + | |
| 394 | + if ( is_array( $config ) && ! empty( $config ) ) { | |
| 361 | 395 | return 'comment_ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')'; |
| 362 | 396 | } |
| 363 | 397 | |
| 364 | - return '1=1'; | |
| 398 | + return $where_sql; | |
| 365 | 399 | } |
| 366 | 400 | |
| 367 | 401 | /** |
| 368 | 402 | * Retrieve the actions that will be sent for this module during a full sync. |
| @@ -468,8 +502,11 @@ | ||
| 468 | 502 | * @param array $args Hook args. |
| 469 | 503 | * @return array|boolean False if not whitelisted, the original hook args otherwise. |
| 470 | 504 | */ |
| 471 | 505 | public function filter_meta( $args ) { |
| 506 | + if ( ! is_array( $args ) || count( $args ) < 3 ) { | |
| 507 | + return false; | |
| 508 | + } | |
| 472 | 509 | if ( $this->is_comment_type_allowed( $args[1] ) && $this->is_whitelisted_comment_meta( $args[2] ) ) { |
| 473 | 510 | return $args; |
| 474 | 511 | } |
| 475 | 512 | |
| @@ -498,7 +535,76 @@ | ||
| 498 | 535 | return array( |
| 499 | 536 | $comments, |
| 500 | 537 | $this->get_metadata( $comment_ids, 'comment', Settings::get_setting( 'comment_meta_whitelist' ) ), |
| 501 | 538 | $previous_interval_end, |
| 539 | + ); | |
| 540 | + } | |
| 541 | + | |
| 542 | + /** | |
| 543 | + * Expand the comment IDs to comment objects and meta before being serialized and sent to the server. | |
| 544 | + * | |
| 545 | + * @access public | |
| 546 | + * | |
| 547 | + * @param array $args The hook parameters. | |
| 548 | + * @return array The expanded hook parameters. | |
| 549 | + */ | |
| 550 | + public function extract_comments_and_meta( $args ) { | |
| 551 | + list( $filtered_comments, $previous_end ) = $args; | |
| 552 | + return array( | |
| 553 | + $filtered_comments['objects'], | |
| 554 | + $filtered_comments['meta'], | |
| 555 | + $previous_end, | |
| 556 | + ); | |
| 557 | + } | |
| 558 | + | |
| 559 | + /** | |
| 560 | + * Given the Module Configuration and Status return the next chunk of items to send. | |
| 561 | + * This function also expands the posts and metadata and filters them based on the maximum size constraints. | |
| 562 | + * | |
| 563 | + * @param array $config This module Full Sync configuration. | |
| 564 | + * @param array $status This module Full Sync status. | |
| 565 | + * @param int $chunk_size Chunk size. | |
| 566 | + * | |
| 567 | + * @return array | |
| 568 | + */ | |
| 569 | + public function get_next_chunk( $config, $status, $chunk_size ) { | |
| 570 | + | |
| 571 | + $comment_ids = parent::get_next_chunk( $config, $status, $chunk_size ); | |
| 572 | + // If no comment IDs were fetched, return an empty array. | |
| 573 | + if ( empty( $comment_ids ) ) { | |
| 574 | + return array(); | |
| 575 | + } | |
| 576 | + $comments = get_comments( | |
| 577 | + array( | |
| 578 | + 'comment__in' => $comment_ids, | |
| 579 | + 'orderby' => 'comment_ID', | |
| 580 | + 'order' => 'DESC', | |
| 581 | + ) | |
| 582 | + ); | |
| 583 | + // If no comments were fetched, make sure to return the expected structure so that status is updated correctly. | |
| 584 | + if ( empty( $comments ) ) { | |
| 585 | + return array( | |
| 586 | + 'object_ids' => $comment_ids, | |
| 587 | + 'objects' => array(), | |
| 588 | + 'meta' => array(), | |
| 589 | + ); | |
| 590 | + } | |
| 591 | + // Get the comment IDs from the comments that were fetched. | |
| 592 | + $fetched_comment_ids = wp_list_pluck( $comments, 'comment_ID' ); | |
| 593 | + $metadata = $this->get_metadata( $fetched_comment_ids, 'comment', Settings::get_setting( 'comment_meta_whitelist' ) ); | |
| 594 | + | |
| 595 | + // Filter the comments and metadata based on the maximum size constraints. | |
| 596 | + list( $filtered_comment_ids, $filtered_comments, $filtered_comments_metadata ) = $this->filter_objects_and_metadata_by_size( | |
| 597 | + 'comment', | |
| 598 | + $comments, | |
| 599 | + $metadata, | |
| 600 | + self::MAX_META_LENGTH, // Replace with appropriate comment meta length constant. | |
| 601 | + self::MAX_SIZE_FULL_SYNC | |
| 602 | + ); | |
| 603 | + | |
| 604 | + return array( | |
| 605 | + 'object_ids' => $filtered_comment_ids, | |
| 606 | + 'objects' => $filtered_comments, | |
| 607 | + 'meta' => $filtered_comments_metadata, | |
| 502 | 608 | ); |
| 503 | 609 | } |
| 504 | 610 | } |