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 / Term / SyncManager.php

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

250 lines 6.8 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 Terms
4 *
5 * @since 3.1
6 * @package elasticpress
7 */
8
9 namespace ElasticPress\Indexable\Term;
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 = 'term';
30
31 /**
32 * Setup actions and filters
33 *
34 * @since 3.1
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( 'created_term', [ $this, 'action_sync_on_update' ] );
46 add_action( 'edited_terms', [ $this, 'action_sync_on_update' ] );
47 add_action( 'added_term_meta', [ $this, 'action_queue_meta_sync' ], 10, 2 );
48 add_action( 'deleted_term_meta', [ $this, 'action_queue_meta_sync' ], 10, 2 );
49 add_action( 'updated_term_meta', [ $this, 'action_queue_meta_sync' ], 10, 2 );
50 add_action( 'pre_delete_term', [ $this, 'action_queue_children_sync' ] );
51 add_action( 'pre_delete_term', [ $this, 'action_sync_on_delete' ] );
52 add_action( 'set_object_terms', [ $this, 'action_sync_on_object_update' ], 10, 2 );
53
54 // Clear index settings cache
55 add_action( 'ep_update_index_settings', [ $this, 'clear_index_settings_cache' ] );
56 add_action( 'ep_after_put_mapping', [ $this, 'clear_index_settings_cache' ] );
57 add_action( 'ep_saved_weighting_configuration', [ $this, 'clear_index_settings_cache' ] );
58 }
59
60 /**
61 * Un-setup actions and filters (for multisite).
62 *
63 * @since 4.0
64 */
65 public function tear_down() {
66 remove_action( 'created_term', [ $this, 'action_sync_on_update' ] );
67 remove_action( 'edited_terms', [ $this, 'action_sync_on_update' ] );
68 remove_action( 'added_term_meta', [ $this, 'action_queue_meta_sync' ] );
69 remove_action( 'deleted_term_meta', [ $this, 'action_queue_meta_sync' ] );
70 remove_action( 'updated_term_meta', [ $this, 'action_queue_meta_sync' ] );
71 remove_action( 'pre_delete_term', [ $this, 'action_queue_children_sync' ] );
72 remove_action( 'pre_delete_term', [ $this, 'action_sync_on_delete' ] );
73 remove_action( 'set_object_terms', [ $this, 'action_sync_on_object_update' ] );
74
75 // Clear index settings cache
76 remove_action( 'ep_update_index_settings', [ $this, 'clear_index_settings_cache' ] );
77 remove_action( 'ep_after_put_mapping', [ $this, 'clear_index_settings_cache' ] );
78 remove_action( 'ep_saved_weighting_configuration', [ $this, 'clear_index_settings_cache' ] );
79 }
80
81 /**
82 * Sync ES index with changes to the term being saved
83 *
84 * @param int $term_id Term ID.
85 * @since 3.1
86 */
87 public function action_sync_on_update( $term_id ) {
88 if ( $this->kill_sync() ) {
89 return;
90 }
91
92 if ( ! current_user_can( 'edit_term', $term_id ) ) {
93 return;
94 }
95
96 if ( apply_filters( 'ep_term_sync_kill', false, $term_id ) ) {
97 return;
98 }
99
100 do_action( 'ep_sync_term_on_transition', $term_id );
101
102 $this->sync_queue[ $term_id ] = true;
103
104 // Find all terms in the hierarchy so we resync those as well
105 $term = get_term( $term_id );
106 $children = get_term_children( $term_id, $term->taxonomy );
107 $ancestors = get_ancestors( $term_id, $term->taxonomy, 'taxonomy' );
108 $hierarchy = array_merge( $ancestors, $children );
109
110 foreach ( $hierarchy as $hierarchy_term_id ) {
111 if ( ! current_user_can( 'edit_term', $hierarchy_term_id ) ) {
112 return;
113 }
114
115 if ( apply_filters( 'ep_term_sync_kill', false, $hierarchy_term_id ) ) {
116 return;
117 }
118
119 do_action( 'ep_sync_term_on_transition', $hierarchy_term_id );
120
121 $this->sync_queue[ $hierarchy_term_id ] = true;
122 }
123 }
124
125 /**
126 * When term relationships are updated, queue the terms for reindex
127 *
128 * @param int $object_id Object ID.
129 * @param array $terms An array of term objects.
130 * @since 3.1
131 */
132 public function action_sync_on_object_update( $object_id, $terms ) {
133 if ( $this->kill_sync() ) {
134 return;
135 }
136
137 if ( empty( $terms ) ) {
138 return;
139 }
140
141 foreach ( $terms as $term ) {
142 $term_info = term_exists( $term );
143
144 if ( ! $term_info ) {
145 continue;
146 }
147
148 $term = get_term( $term_info );
149
150 if ( ! current_user_can( 'edit_term', $term->term_id ) ) {
151 return;
152 }
153
154 if ( apply_filters( 'ep_term_sync_kill', false, $term->term_id ) ) {
155 return;
156 }
157
158 do_action( 'ep_sync_term_on_transition', $term->term_id );
159
160 $this->sync_queue[ $term->term_id ] = true;
161
162 // Find all terms in the hierarchy so we resync those as well
163 $children = get_term_children( $term->term_id, $term->taxonomy );
164 $ancestors = get_ancestors( $term->term_id, $term->taxonomy, 'taxonomy' );
165 $hierarchy = array_merge( $ancestors, $children );
166
167 foreach ( $hierarchy as $hierarchy_term_id ) {
168 if ( ! current_user_can( 'edit_term', $hierarchy_term_id ) ) {
169 return;
170 }
171
172 if ( apply_filters( 'ep_term_sync_kill', false, $hierarchy_term_id ) ) {
173 return;
174 }
175
176 do_action( 'ep_sync_term_on_transition', $hierarchy_term_id );
177
178 $this->sync_queue[ $hierarchy_term_id ] = true;
179 }
180 }
181 }
182
183 /**
184 * When term meta is updated/added/deleted, queue the term for reindex
185 *
186 * @param int $meta_id Meta ID.
187 * @param int $term_id Term ID.
188 * @since 3.1
189 */
190 public function action_queue_meta_sync( $meta_id, $term_id ) {
191 if ( $this->kill_sync() ) {
192 return;
193 }
194
195 $this->sync_queue[ $term_id ] = true;
196 }
197
198 /**
199 * Delete term from ES when deleted in WP
200 *
201 * @param int $term_id Term ID.
202 * @since 3.1
203 */
204 public function action_sync_on_delete( $term_id ) {
205 if ( $this->kill_sync() ) {
206 return;
207 }
208
209 if ( ! current_user_can( 'delete_term', $term_id ) ) {
210 return;
211 }
212
213 Indexables::factory()->get( 'term' )->delete( $term_id, false );
214 }
215
216 /**
217 * Enqueue sync of children terms in hierchy when deleting parent. Children terms will be reasigned to
218 * a different parent and we want to reflect that change in ElasticSearch
219 *
220 * @param int $term_id Term ID.
221 * @since 3.6.3
222 */
223 public function action_queue_children_sync( $term_id ) {
224 if ( $this->kill_sync() ) {
225 return;
226 }
227
228 // Find all terms in the hierarchy so we resync those as well
229 $term = get_term( $term_id );
230 $children = get_term_children( $term->term_id, $term->taxonomy );
231 $ancestors = get_ancestors( $term->term_id, $term->taxonomy, 'taxonomy' );
232 $hierarchy = array_merge( $ancestors, $children );
233
234 foreach ( $hierarchy as $hierarchy_term_id ) {
235 if ( apply_filters( 'ep_term_sync_kill', false, $hierarchy_term_id ) ) {
236 return;
237 }
238
239 if ( ! current_user_can( 'edit_term', $term_id ) && ! apply_filters( 'ep_sync_insert_permissions_bypass', false, $term_id, 'term' ) ) {
240 continue;
241 }
242
243 do_action( 'ep_sync_term_on_transition', $hierarchy_term_id );
244
245 $this->sync_queue[ $hierarchy_term_id ] = true;
246 }
247 }
248
249 }
250