PluginProbe
ElasticPress / 4.7.0
ElasticPress v4.7.0
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / includes / classes / Indexable / Comment / SyncManager.php

SyncManager.php in ElasticPress 4.7.0, at includes/classes/Indexable/Comment/SyncManager.php

233 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Manage syncing of content between WP and Elasticsearch for Comments
4 *
5 * @since 3.6.0
6 * @package elasticpress
7 */
8
9 namespace ElasticPress\Indexable\Comment;
10
11 use ElasticPress\Indexables as Indexables;
12 use ElasticPress\Elasticsearch as Elasticsearch;
13 use ElasticPress\SyncManager as SyncManagerAbstract;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // Exit if accessed directly.
17 }
18
19 /**
20 * Sync manager class
21 */
22 class SyncManager extends SyncManagerAbstract {
23 /**
24 * Indexable slug
25 *
26 * @since 4.7.0
27 * @var string
28 */
29 public $indexable_slug = 'comment';
30
31 /**
32 * Setup actions and filters
33 *
34 * @since 3.6.0
35 */
36 public function setup() {
37 if ( ! Elasticsearch::factory()->get_elasticsearch_version() ) {
38 return;
39 }
40
41 if ( ! $this->can_index_site() ) {
42 return;
43 }
44
45 add_action( 'wp_insert_comment', [ $this, 'action_sync_on_insert' ] );
46 add_action( 'edit_comment', [ $this, 'action_sync_on_update' ] );
47 add_action( 'transition_comment_status', [ $this, 'action_sync_on_transition_comment_status' ], 10, 3 );
48
49 add_action( 'trashed_comment', [ $this, 'action_sync_on_delete' ] );
50 add_action( 'deleted_comment', [ $this, 'action_sync_on_delete' ] );
51
52 add_action( 'added_comment_meta', [ $this, 'action_queue_meta_sync' ], 10, 2 );
53 add_action( 'deleted_comment_meta', [ $this, 'action_queue_meta_sync' ], 10, 2 );
54 add_action( 'updated_comment_meta', [ $this, 'action_queue_meta_sync' ], 10, 2 );
55
56 // Clear index settings cache
57 add_action( 'ep_update_index_settings', [ $this, 'clear_index_settings_cache' ] );
58 add_action( 'ep_after_put_mapping', [ $this, 'clear_index_settings_cache' ] );
59 add_action( 'ep_saved_weighting_configuration', [ $this, 'clear_index_settings_cache' ] );
60 }
61
62 /**
63 * Un-setup actions and filters (for multisite).
64 *
65 * @since 4.0
66 */
67 public function tear_down() {
68 remove_action( 'wp_insert_comment', [ $this, 'action_sync_on_insert' ] );
69 remove_action( 'edit_comment', [ $this, 'action_sync_on_update' ] );
70 remove_action( 'transition_comment_status', [ $this, 'action_sync_on_transition_comment_status' ] );
71 remove_action( 'trashed_comment', [ $this, 'action_sync_on_delete' ] );
72 remove_action( 'deleted_comment', [ $this, 'action_sync_on_delete' ] );
73 remove_action( 'added_comment_meta', [ $this, 'action_queue_meta_sync' ] );
74 remove_action( 'deleted_comment_meta', [ $this, 'action_queue_meta_sync' ] );
75 remove_action( 'updated_comment_meta', [ $this, 'action_queue_meta_sync' ] );
76
77 // Clear index settings cache
78 remove_action( 'ep_update_index_settings', [ $this, 'clear_index_settings_cache' ] );
79 remove_action( 'ep_after_put_mapping', [ $this, 'clear_index_settings_cache' ] );
80 remove_action( 'ep_saved_weighting_configuration', [ $this, 'clear_index_settings_cache' ] );
81 }
82
83 /**
84 * Sync ES index when new comments are saved
85 *
86 * @param int $comment_id Comment ID.
87 * @since 3.6.3
88 */
89 public function action_sync_on_insert( $comment_id ) {
90 if ( $this->kill_sync() ) {
91 return;
92 }
93
94 $this->maybe_index_comment( $comment_id );
95 }
96
97 /**
98 * Sync ES index with changes to the comment being saved
99 *
100 * @param int $comment_id Comment ID.
101 * @since 3.6.0
102 */
103 public function action_sync_on_update( $comment_id ) {
104 if ( $this->kill_sync() ) {
105 return;
106 }
107
108 if ( ! current_user_can( 'edit_comment', $comment_id ) ) {
109 return;
110 }
111
112 $this->maybe_index_comment( $comment_id );
113 }
114
115 /**
116 * Sync ES index with changes to the comment status
117 *
118 * @param int|string $new_status The new comment status.
119 * @param int|string $old_status The old comment status.
120 * @param WP_Comment $comment Comment object.
121 * @since 3.6.3
122 */
123 public function action_sync_on_transition_comment_status( $new_status, $old_status, $comment ) {
124 if ( $this->kill_sync() ) {
125 return;
126 }
127
128 if ( current_user_can( 'edit_comment', $comment->comment_ID ) || current_user_can( 'moderate_comments', $comment->comment_ID ) ) {
129 $this->maybe_index_comment( $comment->comment_ID );
130 } else {
131 return;
132 }
133 }
134
135 /**
136 * Delete comment from ES when deleted or trashed in WP
137 *
138 * @param int $comment_id Comment ID.
139 * @since 3.6.0
140 */
141 public function action_sync_on_delete( $comment_id ) {
142 if ( $this->kill_sync() ) {
143 return;
144 }
145
146 if ( ! current_user_can( 'moderate_comments', $comment_id ) ) {
147 return;
148 }
149
150 Indexables::factory()->get( 'comment' )->delete( $comment_id, false );
151 }
152
153 /**
154 * When comment meta is updated/added/deleted, queue the comment for reindex
155 *
156 * @param int $meta_id Meta ID.
157 * @param int $comment_id Comment ID.
158 * @since 3.6.0
159 */
160 public function action_queue_meta_sync( $meta_id, $comment_id ) {
161 if ( $this->kill_sync() ) {
162 return;
163 }
164
165 if ( ! current_user_can( 'edit_comment_meta', $comment_id ) ) {
166 return;
167 }
168
169 $this->maybe_index_comment( $comment_id );
170 }
171
172 /**
173 * Maybe index a comment
174 *
175 * Check if comment could be indexed.
176 *
177 * @param int $comment_id Comment ID.
178 * @since 3.6.0
179 */
180 protected function maybe_index_comment( $comment_id ) {
181 $comment = get_comment( $comment_id );
182
183 if ( ! empty( $comment ) ) {
184
185 $comment_status = $comment->comment_approved;
186 $post_status = get_post_status( $comment->comment_post_ID );
187
188 $indexable_comment_statuses = Indexables::factory()->get( 'comment' )->get_indexable_comment_status();
189 $indexable_post_statuses = Indexables::factory()->get( 'post' )->get_indexable_post_status();
190
191 $has_allowed_comment_status = [ 'all' ] === $indexable_comment_statuses ? true : in_array( $comment_status, $indexable_comment_statuses, true );
192 $has_allowed_post_status = in_array( $post_status, $indexable_post_statuses, true );
193
194 if ( ! $has_allowed_comment_status || ! $has_allowed_post_status ) {
195 $this->action_sync_on_delete( $comment_id );
196 } else {
197 $comment_type = $comment->comment_type;
198 $post_type = get_post_type( $comment->comment_post_ID );
199
200 $indexable_comment_types = Indexables::factory()->get( 'comment' )->get_indexable_comment_types();
201 $indexable_post_types = Indexables::factory()->get( 'post' )->get_indexable_post_types();
202
203 if ( in_array( $comment_type, $indexable_comment_types, true ) && in_array( $post_type, $indexable_post_types, true ) ) {
204 /**
205 * Fire before comment is queued for syncing
206 *
207 * @hook ep_sync_comment_on_transition
208 * @since 3.6.0
209 * @param {int} $comment_id Comment ID
210 */
211 do_action( 'ep_sync_comment_on_transition', $comment_id );
212
213 /**
214 * Filter to kill comment sync
215 *
216 * @hook ep_comment_sync_kill
217 * @since 3.6.0
218 * @param {bool} $skip True means kill sync for comment
219 * @param {int} $comment_id Comment ID
220 * @return {boolean} New value
221 */
222 if ( apply_filters( 'ep_comment_sync_kill', false, $comment_id ) ) {
223 return;
224 }
225
226 $this->add_to_queue( $comment_id );
227 }
228 }
229 }
230 }
231
232 }
233