diff options
Diffstat (limited to 'plugins/jetpack/sync/class.jetpack-sync-module-users.php')
-rw-r--r-- | plugins/jetpack/sync/class.jetpack-sync-module-users.php | 218 |
1 files changed, 104 insertions, 114 deletions
diff --git a/plugins/jetpack/sync/class.jetpack-sync-module-users.php b/plugins/jetpack/sync/class.jetpack-sync-module-users.php index a3f000e3..005a9fd4 100644 --- a/plugins/jetpack/sync/class.jetpack-sync-module-users.php +++ b/plugins/jetpack/sync/class.jetpack-sync-module-users.php @@ -3,6 +3,8 @@ class Jetpack_Sync_Module_Users extends Jetpack_Sync_Module { const MAX_INITIAL_SYNC_USERS = 100; + protected $flags = array(); + function name() { return 'users'; } @@ -17,18 +19,20 @@ class Jetpack_Sync_Module_Users extends Jetpack_Sync_Module { } public function init_listeners( $callable ) { + // users - add_action( 'user_register', array( $this, 'save_user_handler' ) ); + add_action( 'user_register', array( $this, 'user_register_handler' ) ); add_action( 'profile_update', array( $this, 'save_user_handler' ), 10, 2 ); - add_action( 'add_user_to_blog', array( $this, 'save_user_handler' ) ); + + add_action( 'add_user_to_blog', array( $this, 'add_user_to_blog_handler' ) ); add_action( 'jetpack_sync_add_user', $callable, 10, 2 ); + add_action( 'jetpack_sync_add_user', array( $this, 'clear_flags' ), 11 ); + add_action( 'jetpack_sync_register_user', $callable, 10, 2 ); - add_action( 'jetpack_sync_save_user', $callable ); + add_action( 'jetpack_sync_register_user', array( $this, 'clear_flags' ), 11 ); - //Edit user info, see https://github.com/WordPress/WordPress/blob/c05f1dc805bddcc0e76fd90c4aaf2d9ea76dc0fb/wp-admin/user-edit.php#L126 - add_action( 'personal_options_update', array( $this, 'edited_user_handler' ) ); - add_action( 'edit_user_profile_update', array( $this, 'edited_user_handler' ) ); - add_action( 'jetpack_user_edited', $callable ); + add_action( 'jetpack_sync_save_user', $callable, 10, 2 ); + add_action( 'jetpack_sync_save_user', array( $this, 'clear_flags' ), 11 ); add_action( 'jetpack_sync_user_locale', $callable, 10, 2 ); add_action( 'jetpack_sync_user_locale_delete', $callable, 10, 1 ); @@ -52,6 +56,11 @@ class Jetpack_Sync_Module_Users extends Jetpack_Sync_Module { add_action( 'wp_login', $callable, 10, 2 ); add_action( 'wp_logout', $callable, 10, 0 ); add_action( 'wp_masterbar_logout', $callable, 10, 0 ); + + // Add on init + add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_add_user', array( $this, 'expand_action' ) ); + add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_register_user', array( $this, 'expand_action' ) ); + add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_save_user', array( $this, 'expand_action' ) ); } public function init_full_sync_listeners( $callable ) { @@ -59,9 +68,8 @@ class Jetpack_Sync_Module_Users extends Jetpack_Sync_Module { } public function init_before_send() { - add_filter( 'jetpack_sync_before_send_jetpack_sync_add_user', array( $this, 'expand_user' ) ); - add_filter( 'jetpack_sync_before_send_jetpack_sync_register_user', array( $this, 'expand_user' ) ); - add_filter( 'jetpack_sync_before_send_jetpack_sync_save_user', array( $this, 'expand_user' ), 10, 2 ); + + add_filter( 'jetpack_sync_before_send_wp_login', array( $this, 'expand_login_username' ), 10, 1 ); add_filter( 'jetpack_sync_before_send_wp_logout', array( $this, 'expand_logout_username' ), 10, 2 ); @@ -69,14 +77,8 @@ class Jetpack_Sync_Module_Users extends Jetpack_Sync_Module { add_filter( 'jetpack_sync_before_send_jetpack_full_sync_users', array( $this, 'expand_users' ) ); } - public function sanitize_user_and_expand( $user ) { - $user = $this->get_user( $user ); - $user = $this->add_to_user( $user ); - return $this->sanitize_user( $user ); - } - private function get_user( $user ) { - if ( $user && ! is_object( $user ) && is_numeric( $user ) ) { + if ( is_numeric( $user ) ) { $user = get_user_by( 'id', $user ); } if ( $user instanceof WP_User ) { @@ -93,17 +95,15 @@ class Jetpack_Sync_Module_Users extends Jetpack_Sync_Module { if ( is_object( $user ) && is_object( $user->data ) ) { unset( $user->data->user_pass ); } - if ( $user ) { - $user->allcaps = $this->get_real_user_capabilities( $user ); - } return $user; } - public function add_to_user( $user ) { + public function expand_user( $user ) { if ( ! is_object( $user ) ) { return null; } $user->allowed_mime_types = get_allowed_mime_types( $user ); + $user->allcaps = $this->get_real_user_capabilities( $user ); if ( function_exists( 'get_user_locale' ) ) { @@ -129,11 +129,18 @@ class Jetpack_Sync_Module_Users extends Jetpack_Sync_Module { return $user_capabilities; } - public function expand_user( $args ) { - list( $user ) = $args; + public function sanitize_user_and_expand( $user ) { + $user = $this->get_user( $user ); + $user = $this->expand_user( $user ); + return $this->sanitize_user( $user ); + } + public function expand_action( $args ) { + // the first argument is always the user + list( $user ) = $args; if ( $user ) { - return array( $this->add_to_user( $user ) ); + $args[0] = $this->sanitize_user_and_expand( $user ); + return $args; } return false; @@ -149,7 +156,7 @@ class Jetpack_Sync_Module_Users extends Jetpack_Sync_Module { public function expand_logout_username( $args, $user_id ) { $user = get_userdata( $user_id ); $user = $this->sanitize_user( $user ); - + $login = ''; if ( is_object( $user ) && is_object( $user->data ) ) { $login = $user->data->user_login; @@ -176,24 +183,52 @@ class Jetpack_Sync_Module_Users extends Jetpack_Sync_Module { do_action( 'jetpack_deleted_user', $deleted_user_id, $reassigned_user_id, $is_multisite ); } - public function edited_user_handler( $user_id ) { + function user_register_handler( $user_id, $old_user_data = null ) { + // ensure we only sync users who are members of the current blog + if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) { + return; + } + + if ( Jetpack_Constants::is_true( 'JETPACK_INVITE_ACCEPTED' ) ) { + $this->add_flags( $user_id, array( 'invitation_accepted' => true ) ); + } /** - * Fires when a user is edited on a site + * Fires when a new user is registered on a site * - * @since 5.4.0 + * @since 4.9.0 + * + * @param object The WP_User object + */ + do_action( 'jetpack_sync_register_user', $user_id, $this->get_flags( $user_id ) ); + + } + + function add_user_to_blog_handler( $user_id, $old_user_data = null ) { + // ensure we only sync users who are members of the current blog + if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) { + return; + } + + if ( Jetpack_Constants::is_true( 'JETPACK_INVITE_ACCEPTED' ) ) { + $this->add_flags( $user_id, array( 'invitation_accepted' => true ) ); + } + /** + * Fires when a user is added on a site * - * @param int $user_id - ID of the edited user + * @since 4.9.0 + * + * @param object The WP_User object */ - do_action( 'jetpack_user_edited', $user_id ); + do_action( 'jetpack_sync_add_user', $user_id, $this->get_flags( $user_id ) ); } - + function save_user_handler( $user_id, $old_user_data = null ) { // ensure we only sync users who are members of the current blog if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) { return; } - $user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); + $user = get_user_by( 'id', $user_id ); // Older versions of WP don't pass the old_user_data in ->data if ( isset( $old_user_data->data ) ) { @@ -201,38 +236,8 @@ class Jetpack_Sync_Module_Users extends Jetpack_Sync_Module { } else { $old_user = $old_user_data; } - - if ( $old_user !== null ) { - unset( $old_user->user_pass ); - if ( serialize( $old_user ) === serialize( $user->data ) ) { - return; - } - } - - if ( 'user_register' === current_filter() ) { - /** - * Fires when a new user is registered on a site - * - * @since 4.9.0 - * - * @param object The WP_User object - */ - do_action( 'jetpack_sync_register_user', $user ); - - return; - } - /* MU Sites add users instead of register them to sites */ - if ( 'add_user_to_blog' === current_filter() ) { - /** - * Fires when a new user is added to a site. (WordPress Multisite) - * - * @since 4.9.0 - * - * @param object The WP_User object - */ - do_action( 'jetpack_sync_add_user', $user ); - - return; + if ( $old_user !== null && $user->user_pass !== $old_user->user_pass ) { + $this->flags[ $user_id ]['password_changed'] = true; } /** @@ -241,79 +246,60 @@ class Jetpack_Sync_Module_Users extends Jetpack_Sync_Module { * @since 4.2.0 * * @param object The WP_User object + * @param array state - New since 5.8.0 */ - do_action( 'jetpack_sync_save_user', $user ); + do_action( 'jetpack_sync_save_user', $user_id, $this->get_flags( $user_id ) ); } function save_user_role_handler( $user_id, $role, $old_roles = null ) { + $this->add_flags( $user_id, array( 'role_changed' => true, 'previous_role' => $old_roles ) ); + //The jetpack_sync_register_user payload is identical to jetpack_sync_save_user, don't send both if ( $this->is_create_user() || $this->is_add_user_to_blog() ) { return; } - - $user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); /** - * Fires when the client needs to sync an updated user - * - * @since 4.2.0 - * - * @param object The WP_User object + * This action is documented already in this file */ - do_action( 'jetpack_sync_save_user', $user ); + do_action( 'jetpack_sync_save_user', $user_id, $this->get_flags( $user_id ) ); + } + + function get_flags( $user_id ) { + if ( isset( $this->flags[ $user_id ] ) ) { + return $this->flags[ $user_id ]; + } + return array(); + } + + function clear_flags( $user_id ) { + if ( isset( $this->flags[ $user_id ] ) ) { + unset( $this->flags[ $user_id ] ); + } + } + + function add_flags( $user_id, $flags ) { + $this->flags[ $user_id ] = wp_parse_args( $flags, $this->get_flags( $user_id ) ); } function maybe_save_user_meta( $meta_id, $user_id, $meta_key, $value ) { if ( $meta_key === 'locale' ) { - if ( current_filter() === 'deleted_user_meta' ) { - /** - * Allow listeners to listen for user local delete changes - * - * @since 4.8.0 - * - * @param int $user_id - The ID of the user whos locale is being deleted - */ - do_action( 'jetpack_sync_user_locale_delete', $user_id ); - } else { - /** - * Allow listeners to listen for user local changes - * - * @since 4.8.0 - * - * @param int $user_id - The ID of the user whos locale is being changed - * @param int $value - The value of the new locale - */ - do_action( 'jetpack_sync_user_locale', $user_id, $value ); - } + $this->add_flags( $user_id, array( 'locale_changed' => true ) ); } - $this->save_user_cap_handler( $meta_id, $user_id, $meta_key, $value ); - } - function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) { - //The jetpack_sync_register_user payload is identical to jetpack_sync_save_user, don't send both - if ( $this->is_create_user() || $this->is_add_user_to_blog() ) { - return; + $user = get_user_by( 'id', $user_id ); + if ( $meta_key === $user->cap_key ) { + $this->add_flags( $user_id, array( 'capabilities_changed' => true ) ); } - // if a user is currently being removed as a member of this blog, we don't fire the event - if ( current_filter() === 'deleted_user_meta' - && - preg_match( '/capabilities|user_level/', $meta_key ) - && - ! is_user_member_of_blog( $user_id, get_current_blog_id() ) - ) { + if ( $this->is_create_user() || $this->is_add_user_to_blog() || $this->is_delete_user() ) { return; } - $user = get_user_by( 'id', $user_id ); - if ( $meta_key === $user->cap_key ) { + if ( isset( $this->flags[ $user_id ] ) ) { /** - * Fires when the client needs to sync an updated user - * - * @since 4.2.0 - * - * @param object The Sanitized WP_User object + * This action is documented already in this file */ - do_action( 'jetpack_sync_save_user', $this->sanitize_user( $user ) ); + do_action( 'jetpack_sync_save_user', $user_id, $this->get_flags( $user_id ) ); } } @@ -400,6 +386,10 @@ class Jetpack_Sync_Module_Users extends Jetpack_Sync_Module { return Jetpack::is_function_in_backtrace( 'add_user_to_blog' ); } + private function is_delete_user() { + return Jetpack::is_function_in_backtrace( array( 'wp_delete_user' , 'remove_user_from_blog' ) ); + } + private function is_create_user() { $functions = array( 'add_new_user_to_blog', // Used to suppress jetpack_sync_save_user in save_user_cap_handler when user registered on multi site |