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

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

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