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