| 1 |
<?php |
| 2 |
/** |
| 3 |
* Manage syncing of content between WP and Elasticsearch for users |
| 4 |
* |
| 5 |
* @since 3.0 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress\Indexable\User; |
| 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 = 'user'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Setup actions and filters |
| 32 |
* |
| 33 |
* @since 3.0 |
| 34 |
*/ |
| 35 |
public function setup() { |
| 36 |
if ( ! Elasticsearch::factory()->get_elasticsearch_version() ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
add_action( 'delete_user', [ $this, 'action_delete_user' ] ); |
| 41 |
add_action( 'wpmu_delete_user', [ $this, 'action_delete_user' ] ); |
| 42 |
add_action( 'profile_update', [ $this, 'action_sync_on_update' ] ); |
| 43 |
add_action( 'user_register', [ $this, 'action_sync_on_update' ] ); |
| 44 |
add_action( 'updated_user_meta', [ $this, 'action_queue_meta_sync' ], 10, 4 ); |
| 45 |
add_action( 'added_user_meta', [ $this, 'action_queue_meta_sync' ], 10, 4 ); |
| 46 |
add_action( 'deleted_user_meta', [ $this, 'action_queue_meta_sync' ], 10, 4 ); |
| 47 |
|
| 48 |
// Clear index settings cache |
| 49 |
add_action( 'ep_update_index_settings', [ $this, 'clear_index_settings_cache' ] ); |
| 50 |
add_action( 'ep_after_put_mapping', [ $this, 'clear_index_settings_cache' ] ); |
| 51 |
add_action( 'ep_saved_weighting_configuration', [ $this, 'clear_index_settings_cache' ] ); |
| 52 |
|
| 53 |
// @todo Handle deleted meta |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Dummy implementation of site unsetup method (for now) |
| 58 |
*/ |
| 59 |
public function tear_down() { |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* When whitelisted meta is updated/added/deleted, queue the object for reindex |
| 64 |
* |
| 65 |
* @param int $meta_id Meta id. |
| 66 |
* @param int|array $object_id Object id. |
| 67 |
* @param string $meta_key Meta key. |
| 68 |
* @param string $meta_value Meta value. |
| 69 |
* @since 2.0 |
| 70 |
*/ |
| 71 |
public function action_queue_meta_sync( $meta_id, $object_id, $meta_key, $meta_value ) { |
| 72 |
if ( $this->kill_sync() ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
$indexable = Indexables::factory()->get( 'user' ); |
| 77 |
|
| 78 |
$this->add_to_queue( $object_id ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Delete ES user when WP user is deleted |
| 83 |
* |
| 84 |
* @param int $user_id User ID |
| 85 |
* @since 3.0 |
| 86 |
*/ |
| 87 |
public function action_delete_user( $user_id ) { |
| 88 |
if ( $this->kill_sync() ) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
if ( ! current_user_can( 'edit_user', $user_id ) ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
Indexables::factory()->get( 'user' )->delete( $user_id, false ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Sync ES index with what happened to the user being saved |
| 101 |
* |
| 102 |
* @param int $user_id User id. |
| 103 |
* @since 3.0 |
| 104 |
*/ |
| 105 |
public function action_sync_on_update( $user_id ) { |
| 106 |
if ( $this->kill_sync() ) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
if ( ! current_user_can( 'edit_user', $user_id ) ) { |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Filter whether to kill sync for a particular user |
| 116 |
* |
| 117 |
* @hook ep_user_sync_kill |
| 118 |
* @param {bool} $kill True means dont sync |
| 119 |
* @param {int} $user_id User ID |
| 120 |
* @since 3.0 |
| 121 |
* @return {bool} New kill value |
| 122 |
*/ |
| 123 |
if ( apply_filters( 'ep_user_sync_kill', false, $user_id ) ) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Fires before adding user to sync queue |
| 129 |
* |
| 130 |
* @hook ep_sync_user_on_transition |
| 131 |
* @param {int} $user_id User ID |
| 132 |
* @since 3.0 |
| 133 |
*/ |
| 134 |
do_action( 'ep_sync_user_on_transition', $user_id ); |
| 135 |
|
| 136 |
$this->add_to_queue( $user_id ); |
| 137 |
} |
| 138 |
} |
| 139 |
|