PluginProbe
ElasticPress / 4.2.0
ElasticPress v4.2.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.2.0, at includes/classes/Indexable/Comment/SyncManager.php

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