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