PluginProbe
ElasticPress / 4.3.1
ElasticPress v4.3.1
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.3.1, at includes/classes/Indexable/Term/SyncManager.php

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