← All changes
|
jetpack_vendor/automattic/jetpack-import/src/endpoints/class-comment.php
+51
-14
12.0.3
→
16.3
View file →
| @@ -6,8 +6,16 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace Automattic\Jetpack\Import\Endpoints; |
| 9 | 9 | |
| 10 | +use WP_Error; | |
| 11 | +use WP_REST_Request; | |
| 12 | +use WP_REST_Response; | |
| 13 | + | |
| 14 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 15 | + exit( 0 ); | |
| 16 | +} | |
| 17 | + | |
| 10 | 18 | /** |
| 11 | 19 | * Class Comment |
| 12 | 20 | */ |
| 13 | 21 | class Comment extends \WP_REST_Comments_Controller { |
| @@ -12,11 +20,16 @@ | ||
| 12 | 20 | */ |
| 13 | 21 | class Comment extends \WP_REST_Comments_Controller { |
| 14 | 22 | |
| 15 | 23 | /** |
| 24 | + * Base class | |
| 25 | + */ | |
| 26 | + use Import; | |
| 27 | + | |
| 28 | + /** | |
| 16 | 29 | * The Import ID add a new item to the schema. |
| 17 | 30 | */ |
| 18 | - use Import; | |
| 31 | + use Import_ID; | |
| 19 | 32 | |
| 20 | 33 | /** |
| 21 | 34 | * Whether the controller supports batching. |
| 22 | 35 | * |
| @@ -34,21 +47,8 @@ | ||
| 34 | 47 | $this->import_id_meta_type = 'comment'; |
| 35 | 48 | } |
| 36 | 49 | |
| 37 | 50 | /** |
| 38 | - * Registers the routes for the objects of the controller. | |
| 39 | - * | |
| 40 | - * @see WP_REST_Comments_Controller::register_rest_route() | |
| 41 | - */ | |
| 42 | - public function register_routes() { | |
| 43 | - register_rest_route( | |
| 44 | - self::$rest_namespace, | |
| 45 | - $this->rest_base, | |
| 46 | - $this->get_route_options() | |
| 47 | - ); | |
| 48 | - } | |
| 49 | - | |
| 50 | - /** | |
| 51 | 51 | * Creates a comment. |
| 52 | 52 | * |
| 53 | 53 | * @param WP_REST_Request $request Full details about the request. |
| 54 | 54 | * @return WP_REST_Response|WP_Error Response object on success, or error object on failure. |
| @@ -53,8 +53,10 @@ | ||
| 53 | 53 | * @param WP_REST_Request $request Full details about the request. |
| 54 | 54 | * @return WP_REST_Response|WP_Error Response object on success, or error object on failure. |
| 55 | 55 | */ |
| 56 | 56 | public function create_item( $request ) { |
| 57 | + // Set the WP_IMPORTING constant to prevent sync notifications | |
| 58 | + $this->set_importing(); | |
| 57 | 59 | // Resolve comment post ID. |
| 58 | 60 | if ( ! empty( $request['post'] ) ) { |
| 59 | 61 | $posts = \get_posts( $this->get_import_db_query( $request['post'] ) ); |
| 60 | 62 | |
| @@ -69,9 +71,44 @@ | ||
| 69 | 71 | // Overwrite the comment parent post ID. |
| 70 | 72 | $request['parent'] = is_array( $comments ) && count( $comments ) ? $comments[0] : 0; |
| 71 | 73 | } |
| 72 | 74 | |
| 75 | + $duplicated_id = null; | |
| 76 | + | |
| 77 | + /** | |
| 78 | + * Core comment creation function doesn't return the duplicated comment ID. | |
| 79 | + * Add a filter to get the ID. | |
| 80 | + * | |
| 81 | + * phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 82 | + */ | |
| 83 | + $get_id_func = function ( $dupe_id, $commentdata ) use ( &$duplicated_id ) { | |
| 84 | + if ( $dupe_id !== null ) { | |
| 85 | + $duplicated_id = $dupe_id; | |
| 86 | + } | |
| 87 | + | |
| 88 | + return $dupe_id; | |
| 89 | + }; | |
| 90 | + | |
| 91 | + // Add the filter. | |
| 92 | + \add_filter( 'duplicate_comment_id', $get_id_func, 10, 2 ); | |
| 93 | + | |
| 73 | 94 | $response = parent::create_item( $request ); |
| 95 | + | |
| 96 | + // Check if the comment is duplicated. | |
| 97 | + if ( | |
| 98 | + $duplicated_id !== null && | |
| 99 | + is_wp_error( $response ) && | |
| 100 | + $response->get_error_code() === 'comment_duplicate' ) { | |
| 101 | + $data = $response->get_error_data( 'comment_duplicate' ); | |
| 102 | + | |
| 103 | + // Add the comment ID. | |
| 104 | + $data['comment_id'] = $duplicated_id; | |
| 105 | + | |
| 106 | + $response->add_data( $data ); | |
| 107 | + } | |
| 108 | + | |
| 109 | + // Remove the filter. | |
| 110 | + \remove_filter( 'duplicate_comment_id', $get_id_func ); | |
| 74 | 111 | |
| 75 | 112 | return $this->add_import_id_metadata( $request, $response ); |
| 76 | 113 | } |
| 77 | 114 | } |