| 1 |
<?php |
| 2 |
/** |
| 3 |
* Manage syncing of content between WP and Elasticsearch for posts |
| 4 |
* |
| 5 |
* @since 1.0 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress\Indexable\Post; |
| 10 |
|
| 11 |
use ElasticPress\Elasticsearch as Elasticsearch; |
| 12 |
use ElasticPress\Indexables as Indexables; |
| 13 |
use ElasticPress\SyncManager as SyncManagerAbstract; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
// @codeCoverageIgnoreStart |
| 17 |
exit; // Exit if accessed directly. |
| 18 |
// @codeCoverageIgnoreEnd |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Sync manager class |
| 23 |
*/ |
| 24 |
class SyncManager extends SyncManagerAbstract { |
| 25 |
|
| 26 |
/** |
| 27 |
* Indexable slug |
| 28 |
* |
| 29 |
* @since 3.0 |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
public $indexable_slug = 'post'; |
| 33 |
|
| 34 |
/** |
| 35 |
* Delete all post meta from other posts associated with a deleted post. Useful for attachments. |
| 36 |
* |
| 37 |
* @var bool |
| 38 |
*/ |
| 39 |
public $delete_all_meta = false; |
| 40 |
|
| 41 |
/** |
| 42 |
* Setup actions and filters |
| 43 |
* |
| 44 |
* @since 0.1.2 |
| 45 |
*/ |
| 46 |
public function setup() { |
| 47 |
if ( ! Elasticsearch::factory()->get_elasticsearch_version() ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
if ( ! $this->can_index_site() ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
add_action( 'wp_insert_post', array( $this, 'action_sync_on_update' ), 999, 3 ); |
| 56 |
add_action( 'add_attachment', array( $this, 'action_sync_on_update' ), 999, 3 ); |
| 57 |
add_action( 'edit_attachment', array( $this, 'action_sync_on_update' ), 999, 3 ); |
| 58 |
add_action( 'delete_post', array( $this, 'action_delete_post' ) ); |
| 59 |
add_action( 'updated_post_meta', array( $this, 'action_queue_meta_sync' ), 10, 4 ); |
| 60 |
add_action( 'added_post_meta', array( $this, 'action_queue_meta_sync' ), 10, 4 ); |
| 61 |
// Called just because we need to know somehow if $delete_all is set before action_queue_meta_sync() runs. |
| 62 |
add_filter( 'delete_post_metadata', array( $this, 'maybe_delete_meta_for_all' ), 10, 5 ); |
| 63 |
add_action( 'deleted_post_meta', array( $this, 'action_queue_meta_sync' ), 10, 4 ); |
| 64 |
add_action( 'set_object_terms', array( $this, 'action_set_object_terms' ), 10, 6 ); |
| 65 |
add_action( 'edited_term', array( $this, 'action_edited_term' ), 10, 3 ); |
| 66 |
add_action( 'deleted_term_relationships', array( $this, 'action_deleted_term_relationships' ), 10, 3 ); |
| 67 |
add_action( 'wp_initialize_site', array( $this, 'action_create_blog_index' ) ); |
| 68 |
|
| 69 |
add_filter( 'ep_sync_insert_permissions_bypass', array( $this, 'filter_bypass_permission_checks_for_machines' ) ); |
| 70 |
add_filter( 'ep_sync_delete_permissions_bypass', array( $this, 'filter_bypass_permission_checks_for_machines' ) ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Un-setup actions and filters (for multisite). |
| 75 |
* |
| 76 |
* @since 4.0 |
| 77 |
*/ |
| 78 |
public function tear_down() { |
| 79 |
remove_action( 'wp_insert_post', array( $this, 'action_sync_on_update' ), 999 ); |
| 80 |
remove_action( 'add_attachment', array( $this, 'action_sync_on_update' ), 999 ); |
| 81 |
remove_action( 'edit_attachment', array( $this, 'action_sync_on_update' ), 999 ); |
| 82 |
remove_action( 'delete_post', array( $this, 'action_delete_post' ) ); |
| 83 |
remove_action( 'updated_post_meta', array( $this, 'action_queue_meta_sync' ) ); |
| 84 |
remove_action( 'added_post_meta', array( $this, 'action_queue_meta_sync' ) ); |
| 85 |
remove_filter( 'delete_post_metadata', array( $this, 'maybe_delete_meta_for_all' ) ); |
| 86 |
remove_action( 'deleted_post_meta', array( $this, 'action_queue_meta_sync' ) ); |
| 87 |
remove_action( 'wp_initialize_site', array( $this, 'action_create_blog_index' ) ); |
| 88 |
remove_filter( 'ep_sync_insert_permissions_bypass', array( $this, 'filter_bypass_permission_checks_for_machines' ) ); |
| 89 |
remove_filter( 'ep_sync_delete_permissions_bypass', array( $this, 'filter_bypass_permission_checks_for_machines' ) ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Whether to delete all meta from other posts that is associated with the deleted post. |
| 94 |
* |
| 95 |
* @param bool $check Whether to allow metadata deletion of the given type. |
| 96 |
* @param int $object_id ID of the object metadata is for. |
| 97 |
* @param string $meta_key Metadata key. |
| 98 |
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
| 99 |
* @param bool $delete_all Whether to delete the matching metadata entries |
| 100 |
* for all objects, ignoring the specified $object_id |
| 101 |
* @return bool |
| 102 |
*/ |
| 103 |
public function maybe_delete_meta_for_all( $check, $object_id, $meta_key, $meta_value, $delete_all ) { |
| 104 |
$this->delete_all_meta = $delete_all; |
| 105 |
return $check; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Filter to allow cron and WP CLI processes to index/delete documents |
| 110 |
* |
| 111 |
* @param boolean $bypass The current filtered value |
| 112 |
* @return boolean Boolean indicating if permission checking should be bypased or not |
| 113 |
* @since 3.6.0 |
| 114 |
*/ |
| 115 |
public function filter_bypass_permission_checks_for_machines( $bypass ) { |
| 116 |
// Allow index/delete during cron |
| 117 |
if ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
| 118 |
return true; |
| 119 |
} |
| 120 |
|
| 121 |
// Allow index/delete during WP CLI commands |
| 122 |
if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
return $bypass; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* When whitelisted meta is updated, queue the post for reindex |
| 131 |
* |
| 132 |
* @param int|array $meta_id Meta id. |
| 133 |
* @param int $object_id Object id. |
| 134 |
* @param string $meta_key Meta key. |
| 135 |
* @param string $meta_value Meta value. |
| 136 |
* @since 2.0 |
| 137 |
*/ |
| 138 |
public function action_queue_meta_sync( $meta_id, $object_id, $meta_key, $meta_value ) { |
| 139 |
if ( $this->kill_sync() ) { |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
$indexable = Indexables::factory()->get( $this->indexable_slug ); |
| 144 |
|
| 145 |
/** |
| 146 |
* Filter to whether skip a sync during autosave, defaults to true |
| 147 |
* |
| 148 |
* @hook ep_skip_autosave_sync |
| 149 |
* @since 4.3.0 |
| 150 |
* @param {bool} $skip True means to disable sync for autosaves |
| 151 |
* @param {string} $function Function applying filter |
| 152 |
* @return {boolean} New value |
| 153 |
*/ |
| 154 |
if ( apply_filters( 'ep_skip_autosave_sync', true, __FUNCTION__ ) ) { |
| 155 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 156 |
// Bypass saving if doing autosave |
| 157 |
// @codeCoverageIgnoreStart |
| 158 |
return; |
| 159 |
// @codeCoverageIgnoreEnd |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
$post = get_post( $object_id ); |
| 164 |
|
| 165 |
/** |
| 166 |
* Filter to allow skipping a sync triggered by meta changes |
| 167 |
* |
| 168 |
* @hook ep_skip_post_meta_sync |
| 169 |
* @param {bool} $skip True means kill sync for post |
| 170 |
* @param {WP_Post} $post The post that's attempting to be synced |
| 171 |
* @param {int} $meta_id ID of the meta that triggered the sync |
| 172 |
* @param {string} $meta_key The key of the meta that triggered the sync |
| 173 |
* @param {string} $meta_value The value of the meta that triggered the sync |
| 174 |
* @return {boolean} New value |
| 175 |
*/ |
| 176 |
if ( apply_filters( 'ep_skip_post_meta_sync', false, $post, $meta_id, $meta_key, $meta_value ) ) { |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
if ( empty( $object_id ) && $this->delete_all_meta ) { |
| 181 |
add_filter( 'ep_is_integrated_request', '__return_true' ); |
| 182 |
|
| 183 |
$query = new \WP_Query( |
| 184 |
[ |
| 185 |
'ep_integrate' => true, |
| 186 |
'meta_key' => $meta_key, |
| 187 |
'meta_value' => $meta_value, |
| 188 |
'fields' => 'ids', |
| 189 |
] |
| 190 |
); |
| 191 |
|
| 192 |
remove_filter( 'ep_is_integrated_request', '__return_true' ); |
| 193 |
|
| 194 |
if ( $query->have_posts() && $query->elasticsearch_success ) { |
| 195 |
$posts_to_be_synced = array_filter( |
| 196 |
$query->posts, |
| 197 |
function( $object_id ) { |
| 198 |
return ! apply_filters( 'ep_post_sync_kill', false, $object_id, $object_id ); |
| 199 |
} |
| 200 |
); |
| 201 |
if ( ! empty( $posts_to_be_synced ) ) { |
| 202 |
$indexable->bulk_index( $posts_to_be_synced ); |
| 203 |
} |
| 204 |
} |
| 205 |
} else { |
| 206 |
$indexable_post_statuses = $indexable->get_indexable_post_status(); |
| 207 |
$post_type = get_post_type( $object_id ); |
| 208 |
|
| 209 |
$is_meta_allowed = $indexable->is_meta_allowed( $meta_key, $post ); |
| 210 |
if ( ! $is_meta_allowed ) { |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
if ( in_array( $post->post_status, $indexable_post_statuses, true ) ) { |
| 215 |
$indexable_post_types = $indexable->get_indexable_post_types(); |
| 216 |
|
| 217 |
if ( in_array( $post_type, $indexable_post_types, true ) ) { |
| 218 |
/** |
| 219 |
* Filter to kill post sync |
| 220 |
* |
| 221 |
* @hook ep_post_sync_kill |
| 222 |
* @param {bool} $skip True meanas kill sync for post |
| 223 |
* @param {int} $object_id ID of post |
| 224 |
* @param {int} $object_id ID of post |
| 225 |
* @return {boolean} New value |
| 226 |
*/ |
| 227 |
if ( apply_filters( 'ep_post_sync_kill', false, $object_id, $object_id ) ) { |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
$this->add_to_queue( $object_id ); |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Delete ES post when WP post is deleted |
| 239 |
* |
| 240 |
* @param int $post_id Post id. |
| 241 |
* @since 0.1.0 |
| 242 |
*/ |
| 243 |
public function action_delete_post( $post_id ) { |
| 244 |
if ( $this->kill_sync() ) { |
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Filter whether to skip the permissions check on deleting a post |
| 250 |
* |
| 251 |
* @hook ep_sync_delete_permissions_bypass |
| 252 |
* @param {bool} $bypass True to bypass |
| 253 |
* @param {int} $post_id ID of post |
| 254 |
* @return {boolean} New value |
| 255 |
*/ |
| 256 |
if ( ! current_user_can( 'edit_post', $post_id ) && ! apply_filters( 'ep_sync_delete_permissions_bypass', false, $post_id ) ) { |
| 257 |
return; |
| 258 |
} |
| 259 |
|
| 260 |
$indexable = Indexables::factory()->get( $this->indexable_slug ); |
| 261 |
$post_type = get_post_type( $post_id ); |
| 262 |
|
| 263 |
$indexable_post_types = $indexable->get_indexable_post_types(); |
| 264 |
|
| 265 |
if ( ! in_array( $post_type, $indexable_post_types, true ) ) { |
| 266 |
// If not an indexable post type, skip delete. |
| 267 |
return; |
| 268 |
} |
| 269 |
|
| 270 |
Indexables::factory()->get( $this->indexable_slug )->delete( $post_id, false ); |
| 271 |
|
| 272 |
/** |
| 273 |
* Make sure to remove this post from the sync queue in case an shutdown happens |
| 274 |
* before a redirect when a redirect has already been triggered. |
| 275 |
*/ |
| 276 |
if ( isset( $this->sync_queue[ $post_id ] ) ) { |
| 277 |
unset( $this->sync_queue[ $post_id ] ); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Sync ES index with what happened to the post being saved |
| 283 |
* |
| 284 |
* @param int $post_id Post id. |
| 285 |
* @since 0.1.0 |
| 286 |
*/ |
| 287 |
public function action_sync_on_update( $post_id ) { |
| 288 |
if ( $this->kill_sync() ) { |
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
$indexable = Indexables::factory()->get( $this->indexable_slug ); |
| 293 |
$post_type = get_post_type( $post_id ); |
| 294 |
|
| 295 |
/** |
| 296 |
* Filter to whether skip a sync during autosave, defaults to true |
| 297 |
* |
| 298 |
* @hook ep_skip_autosave_sync |
| 299 |
* @since 4.3.0 |
| 300 |
* @param {bool} $skip True means to disable sync for autosaves |
| 301 |
* @param {string} $function Function applying filter |
| 302 |
* @return {boolean} New value |
| 303 |
*/ |
| 304 |
if ( apply_filters( 'ep_skip_autosave_sync', true, __FUNCTION__ ) ) { |
| 305 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 306 |
// Bypass saving if doing autosave |
| 307 |
// @codeCoverageIgnoreStart |
| 308 |
return; |
| 309 |
// @codeCoverageIgnoreEnd |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* Filter whether to skip the permissions check on updating a post |
| 315 |
* |
| 316 |
* @hook ep_sync_insert_permissions_bypass |
| 317 |
* @param {bool} $bypass True to bypass |
| 318 |
* @param {int} $post_id ID of post |
| 319 |
* @return {boolean} New value |
| 320 |
*/ |
| 321 |
if ( ! current_user_can( 'edit_post', $post_id ) && ! apply_filters( 'ep_sync_insert_permissions_bypass', false, $post_id ) ) { |
| 322 |
return; |
| 323 |
} |
| 324 |
|
| 325 |
$post = get_post( $post_id ); |
| 326 |
|
| 327 |
$indexable_post_statuses = $indexable->get_indexable_post_status(); |
| 328 |
|
| 329 |
// Our post was published, but is no longer, so let's remove it from the Elasticsearch index. |
| 330 |
if ( ! in_array( $post->post_status, $indexable_post_statuses, true ) ) { |
| 331 |
$this->action_delete_post( $post_id ); |
| 332 |
} else { |
| 333 |
$indexable_post_types = $indexable->get_indexable_post_types(); |
| 334 |
|
| 335 |
if ( in_array( $post_type, $indexable_post_types, true ) ) { |
| 336 |
/** |
| 337 |
* Fire before post is queued for syncing |
| 338 |
* |
| 339 |
* @hook ep_sync_on_transition |
| 340 |
* @param {int} $post_id ID of post |
| 341 |
*/ |
| 342 |
do_action( 'ep_sync_on_transition', $post_id ); |
| 343 |
|
| 344 |
/** |
| 345 |
* Filter to kill post sync |
| 346 |
* |
| 347 |
* @hook ep_post_sync_kill |
| 348 |
* @param {bool} $skip True means kill sync for post |
| 349 |
* @param {int} $object_id ID of post |
| 350 |
* @param {int} $object_id ID of post |
| 351 |
* @return {boolean} New value |
| 352 |
*/ |
| 353 |
if ( apply_filters( 'ep_post_sync_kill', false, $post_id, $post_id ) ) { |
| 354 |
return; |
| 355 |
} |
| 356 |
|
| 357 |
$this->add_to_queue( $post_id ); |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* When a post's terms are changed, re-index. |
| 364 |
* |
| 365 |
* This catches term deletions via wp_delete_term(), because that function internally loops over all attached objects |
| 366 |
* and updates their terms. It will also end up firing whenever set_object_terms is called, but the queue will de-duplicate |
| 367 |
* multiple instances per post. This won't happen for taxonomies that has a default term (like Uncategorized for categories), |
| 368 |
* hence why we also have `action_deleted_term_relationships`. |
| 369 |
* |
| 370 |
* @see set_object_terms |
| 371 |
* @param int $post_id Post ID. |
| 372 |
* @param array $terms An array of object terms. |
| 373 |
* @param array $tt_ids An array of term taxonomy IDs. |
| 374 |
* @param string $taxonomy Taxonomy slug. |
| 375 |
* @param bool $append Whether to append new terms to the old terms. |
| 376 |
* @param array $old_tt_ids Old array of term taxonomy IDs. |
| 377 |
* @since 4.0.0 |
| 378 |
*/ |
| 379 |
public function action_set_object_terms( $post_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) { |
| 380 |
if ( $this->kill_sync() ) { |
| 381 |
return; |
| 382 |
} |
| 383 |
|
| 384 |
/** |
| 385 |
* Filter to whether skip a sync during autosave, defaults to true |
| 386 |
* |
| 387 |
* @hook ep_skip_autosave_sync |
| 388 |
* @since 4.3.0 |
| 389 |
* @param {bool} $skip True means to disable sync for autosaves |
| 390 |
* @param {string} $function Function applying filter |
| 391 |
* @return {boolean} New value |
| 392 |
*/ |
| 393 |
if ( apply_filters( 'ep_skip_autosave_sync', true, __FUNCTION__ ) ) { |
| 394 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 395 |
// Bypass saving if doing autosave |
| 396 |
// @codeCoverageIgnoreStart |
| 397 |
return; |
| 398 |
// @codeCoverageIgnoreEnd |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
/** |
| 403 |
* Filter to allow skipping this action in case of custom handling |
| 404 |
* |
| 405 |
* @hook ep_skip_action_set_object_terms |
| 406 |
* @param {bool} $skip True means kill sync for post |
| 407 |
* @param {int} $post_id ID of post |
| 408 |
* @param {array} $terms An array of object terms. |
| 409 |
* @param {array} $tt_ids An array of term taxonomy IDs. |
| 410 |
* @param {string} $taxonomy Taxonomy slug. |
| 411 |
* @param {bool} $append Whether to append new terms to the old terms. |
| 412 |
* @param {array} $old_tt_ids Old array of term taxonomy IDs. |
| 413 |
* @return {boolean} New value |
| 414 |
*/ |
| 415 |
if ( apply_filters( 'ep_skip_action_set_object_terms', false, $post_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) ) { |
| 416 |
return; |
| 417 |
} |
| 418 |
|
| 419 |
if ( ! $this->should_reindex_post( $post_id, $taxonomy ) ) { |
| 420 |
return; |
| 421 |
} |
| 422 |
|
| 423 |
/** |
| 424 |
* Fire before post is queued for syncing |
| 425 |
* |
| 426 |
* @since 4.0.0 |
| 427 |
* @hook ep_sync_on_set_object_terms |
| 428 |
* @param {int} $post_id ID of post |
| 429 |
* @param {array} $terms An array of object terms. |
| 430 |
* @param {array} $tt_ids An array of term taxonomy IDs. |
| 431 |
* @param {string} $taxonomy Taxonomy slug. |
| 432 |
* @param {bool} $append Whether to append new terms to the old terms. |
| 433 |
* @param {array} $old_tt_ids Old array of term taxonomy IDs. |
| 434 |
*/ |
| 435 |
do_action( 'ep_sync_on_set_object_terms', $post_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ); |
| 436 |
|
| 437 |
$this->add_to_queue( $post_id ); |
| 438 |
} |
| 439 |
|
| 440 |
/** |
| 441 |
* When a term is updated, re-index all posts attached to that term |
| 442 |
* |
| 443 |
* @param int $term_id Term id. |
| 444 |
* @param int $tt_id Term Taxonomy id. |
| 445 |
* @param string $taxonomy Taxonomy name. |
| 446 |
* @since 4.0.0 |
| 447 |
*/ |
| 448 |
public function action_edited_term( $term_id, $tt_id, $taxonomy ) { |
| 449 |
global $wpdb; |
| 450 |
|
| 451 |
/** |
| 452 |
* Filter to whether skip a sync during autosave, defaults to true |
| 453 |
* |
| 454 |
* @hook ep_skip_autosave_sync |
| 455 |
* @since 4.3.0 |
| 456 |
* @param {bool} $skip True means to disable sync for autosaves |
| 457 |
* @param {string} $function Function applying filter |
| 458 |
* @return {boolean} New value |
| 459 |
*/ |
| 460 |
if ( apply_filters( 'ep_skip_autosave_sync', true, __FUNCTION__ ) ) { |
| 461 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 462 |
// Bypass saving if doing autosave |
| 463 |
// @codeCoverageIgnoreStart |
| 464 |
return; |
| 465 |
// @codeCoverageIgnoreEnd |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
// Find ID of all attached posts (query lifted from wp_delete_term()) |
| 470 |
$object_ids = (array) $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) ); |
| 471 |
|
| 472 |
if ( ! count( $object_ids ) ) { |
| 473 |
return; |
| 474 |
} |
| 475 |
|
| 476 |
/** |
| 477 |
* Filter to allow skipping this action in case of custom handling |
| 478 |
* |
| 479 |
* @hook ep_skip_action_edited_term |
| 480 |
* @param {bool} $skip Current value of whether to skip running action_edited_term or not |
| 481 |
* @param {int} $term_id Term id. |
| 482 |
* @param {int} $tt_id Term Taxonomy id. |
| 483 |
* @param {string} $taxonomy Taxonomy name. |
| 484 |
* @param {array} $object_ids IDs of the objects attached to the term id. |
| 485 |
* @return {bool} New value of whether to skip running action_edited_term or not |
| 486 |
*/ |
| 487 |
if ( apply_filters( 'ep_skip_action_edited_term', false, $term_id, $tt_id, $taxonomy, $object_ids ) ) { |
| 488 |
return; |
| 489 |
} |
| 490 |
|
| 491 |
$indexable = Indexables::factory()->get( $this->indexable_slug ); |
| 492 |
|
| 493 |
// Add all of them to the queue |
| 494 |
foreach ( $object_ids as $post_id ) { |
| 495 |
if ( ! $this->should_reindex_post( $post_id, $taxonomy ) ) { |
| 496 |
continue; |
| 497 |
} |
| 498 |
|
| 499 |
/** |
| 500 |
* Fire before post is queued for syncing |
| 501 |
* |
| 502 |
* @hook ep_sync_on_edited_term |
| 503 |
* @param {int} $post_id ID of post |
| 504 |
* @param {int} $term_id ID of the term that was edited |
| 505 |
* @param {int} $tt_id Taxonomy Term ID of the term that was edited |
| 506 |
* @param {int} $taxonomy Taxonomy of the term that was edited |
| 507 |
*/ |
| 508 |
do_action( 'ep_sync_on_edited_term', $post_id, $term_id, $tt_id, $taxonomy ); |
| 509 |
|
| 510 |
$this->add_to_queue( $post_id ); |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
/** |
| 515 |
* When a term relationship is deleted, re-index all posts attached to that term |
| 516 |
* |
| 517 |
* @param int $post_id Post ID. |
| 518 |
* @param array $tt_ids An array of term taxonomy IDs. |
| 519 |
* @param string $taxonomy Taxonomy slug. |
| 520 |
* @since 4.0.0 |
| 521 |
*/ |
| 522 |
public function action_deleted_term_relationships( $post_id, $tt_ids, $taxonomy ) { |
| 523 |
if ( $this->kill_sync() ) { |
| 524 |
return; |
| 525 |
} |
| 526 |
|
| 527 |
/** |
| 528 |
* Filter to whether skip a sync during autosave, defaults to true |
| 529 |
* |
| 530 |
* @hook ep_skip_autosave_sync |
| 531 |
* @since 4.3.0 |
| 532 |
* @param {bool} $skip True means to disable sync for autosaves |
| 533 |
* @param {string} $function Function applying filter |
| 534 |
* @return {boolean} New value |
| 535 |
*/ |
| 536 |
if ( apply_filters( 'ep_skip_autosave_sync', true, __FUNCTION__ ) ) { |
| 537 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 538 |
// Bypass saving if doing autosave |
| 539 |
// @codeCoverageIgnoreStart |
| 540 |
return; |
| 541 |
// @codeCoverageIgnoreEnd |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
/** |
| 546 |
* Filter to allow skipping this action in case of custom handling |
| 547 |
* |
| 548 |
* @hook ep_skip_action_deleted_term_relationships |
| 549 |
* @param {bool} $skip Current value of whether to skip running action_edited_term or not |
| 550 |
* @param {int} $post_id Post ID. |
| 551 |
* @param {array} $tt_ids An array of term taxonomy IDs. |
| 552 |
* @param {string} $taxonomy Taxonomy slug. |
| 553 |
* @return {bool} New value of whether to skip running action_deleted_term_relationships or not |
| 554 |
*/ |
| 555 |
if ( apply_filters( 'ep_skip_action_deleted_term_relationships', false, $post_id, $tt_ids, $taxonomy ) ) { |
| 556 |
return; |
| 557 |
} |
| 558 |
|
| 559 |
if ( ! $this->should_reindex_post( $post_id, $taxonomy ) ) { |
| 560 |
return; |
| 561 |
} |
| 562 |
|
| 563 |
/** |
| 564 |
* Fire before post is queued for syncing |
| 565 |
* |
| 566 |
* @hook ep_sync_on_deleted_term_relationships |
| 567 |
* @since 4.0.0 |
| 568 |
* @param {int} $post_id ID of post |
| 569 |
* @param {array} $tt_ids An array of term taxonomy IDs. |
| 570 |
* @param {string} $taxonomy Taxonomy of the term that was edited |
| 571 |
*/ |
| 572 |
do_action( 'ep_sync_on_deleted_term_relationships', $post_id, $tt_ids, $taxonomy ); |
| 573 |
|
| 574 |
$this->add_to_queue( $post_id ); |
| 575 |
} |
| 576 |
|
| 577 |
/** |
| 578 |
* Create mapping and network alias when a new blog is created. |
| 579 |
* |
| 580 |
* @param WP_Site $blog New site object. |
| 581 |
*/ |
| 582 |
public function action_create_blog_index( $blog ) { |
| 583 |
if ( ! defined( 'EP_IS_NETWORK' ) || ! EP_IS_NETWORK ) { |
| 584 |
// @codeCoverageIgnoreStart |
| 585 |
return; |
| 586 |
// @codeCoverageIgnoreEnd |
| 587 |
} |
| 588 |
|
| 589 |
if ( $this->kill_sync() ) { |
| 590 |
return; |
| 591 |
} |
| 592 |
|
| 593 |
$non_global_indexable_objects = Indexables::factory()->get_all( false ); |
| 594 |
|
| 595 |
switch_to_blog( $blog->blog_id ); |
| 596 |
|
| 597 |
foreach ( $non_global_indexable_objects as $indexable ) { |
| 598 |
$indexable->delete_index(); |
| 599 |
$indexable->put_mapping(); |
| 600 |
|
| 601 |
$index_name = $indexable->get_index_name( $blog->blog_id ); |
| 602 |
$indexable->create_network_alias( [ $index_name ] ); |
| 603 |
} |
| 604 |
|
| 605 |
restore_current_blog(); |
| 606 |
} |
| 607 |
|
| 608 |
/** |
| 609 |
* Check if post attributes (post status, taxonomy, and type) match what is needed to reindex or not. |
| 610 |
* |
| 611 |
* @param int $post_id The post ID. |
| 612 |
* @param string $taxonomy The taxonomy slug. |
| 613 |
* @return boolean |
| 614 |
*/ |
| 615 |
protected function should_reindex_post( $post_id, $taxonomy ) { |
| 616 |
/** |
| 617 |
* Filter to kill post sync |
| 618 |
* |
| 619 |
* @hook ep_post_sync_kill |
| 620 |
* @param {bool} $skip True meanas kill sync for post |
| 621 |
* @param {int} $object_id ID of post |
| 622 |
* @param {int} $object_id ID of post |
| 623 |
* @return {boolean} New value |
| 624 |
*/ |
| 625 |
if ( apply_filters( 'ep_post_sync_kill', false, $post_id, $post_id ) ) { |
| 626 |
return false; |
| 627 |
} |
| 628 |
|
| 629 |
$post = get_post( $post_id ); |
| 630 |
if ( ! is_object( $post ) ) { |
| 631 |
return false; |
| 632 |
} |
| 633 |
|
| 634 |
$indexable = Indexables::factory()->get( $this->indexable_slug ); |
| 635 |
|
| 636 |
// Check post status |
| 637 |
$indexable_post_statuses = $indexable->get_indexable_post_status(); |
| 638 |
if ( ! in_array( $post->post_status, $indexable_post_statuses, true ) ) { |
| 639 |
return false; |
| 640 |
} |
| 641 |
|
| 642 |
// Only re-index if the taxonomy is indexed for this post |
| 643 |
$indexable_taxonomies = $indexable->get_indexable_post_taxonomies( $post ); |
| 644 |
$indexable_taxonomy_names = wp_list_pluck( $indexable_taxonomies, 'name' ); |
| 645 |
if ( ! in_array( $taxonomy, $indexable_taxonomy_names, true ) ) { |
| 646 |
return false; |
| 647 |
} |
| 648 |
|
| 649 |
// Check post type |
| 650 |
$indexable_post_types = $indexable->get_indexable_post_types(); |
| 651 |
if ( ! in_array( $post->post_type, $indexable_post_types, true ) ) { |
| 652 |
return false; |
| 653 |
} |
| 654 |
|
| 655 |
return true; |
| 656 |
} |
| 657 |
} |
| 658 |
|