PluginProbe
ElasticPress / 4.3.0
ElasticPress v4.3.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.3.0, at includes/classes/Indexable/User/SyncManager.php

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