ed' => true ) ); } /** * Fires when a new user is registered on a site * * @since 1.6.3 * @since-jetpack 4.9.0 * * @param object The WP_User object */ do_action( 'jetpack_sync_register_user', $user_id, $this->get_flags( $user_id ) ); $this->clear_flags( $user_id ); } /** * Handler for user addition to the current blog. * * @access public * * @param int $user_id ID of the user. */ public function add_user_to_blog_handler( $user_id ) { // 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 * * @since 1.6.3 * @since-jetpack 4.9.0 * * @param object The WP_User object */ do_action( 'jetpack_sync_add_user', $user_id, $this->get_flags( $user_id ) ); $this->clear_flags( $user_id ); } /** * Handler for user save. * * @access public * * @param int $user_id ID of the user. * @param \WP_User $old_user_data User object before the changes. */ public 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 = 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 ) ) { $old_user = $old_user_data->data; } else { $old_user = $old_user_data; } if ( ! is_object( $old_user ) ) { return; } $old_user_array = get_object_vars( $old_user ); foreach ( $old_user_array as $user_field => $field_value ) { if ( false === $user->has_prop( $user_field ) ) { continue; } if ( $user->$user_field !== $field_value ) { if ( 'user_email' === $user_field ) { /** * The '_new_email' user meta is deleted right after the call to wp_update_user * that got us to this point so if it's still set then this was a user confirming * their new email address. */ if ( 1 === (int) get_user_meta( $user->ID, '_new_email', true ) ) { $this->flags[ $user_id ]['email_changed'] = true; } continue; } $flag = isset( $this->user_fields_to_flags_mapping[ $user_field ] ) ? $this->user_fields_to_flags_mapping[ $user_field ] : 'unknown_field_changed'; $this->flags[ $user_id ][ $flag ] = true; } } if ( isset( $this->flags[ $user_id ] ) ) { /** * Fires when the client needs to sync an updated user. * * @since 1.6.3 * @since-jetpack 4.2.0 * * @param \WP_User The WP_User object * @param array State - New since 5.8.0 */ do_action( 'jetpack_sync_save_user', $user_id, $this->get_flags( $user_id ) ); $this->clear_flags( $user_id ); } } /** * Handler for user role change. * * @access public * * @param int $user_id ID of the user. * @param string $role New user role. * @param array $old_roles Previous user roles. */ public 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; } /** * This action is documented already in this file */ do_action( 'jetpack_sync_save_user', $user_id, $this->get_flags( $user_id ) ); $this->clear_flags( $user_id ); } /** * Retrieve current flags for a particular user. * * @access public * * @param int $user_id ID of the user. * @return array Current flags of the user. */ public function get_flags( $user_id ) { if ( isset( $this->flags[ $user_id ] ) ) { return $this->flags[ $user_id ]; } return array(); } /** * Clear the flags of a particular user. * * @access public * * @param int $user_id ID of the user. */ public function clear_flags( $user_id ) { if ( isset( $this->flags[ $user_id ] ) ) { unset( $this->flags[ $user_id ] ); } } /** * Add flags to a particular user. * * @access public * * @param int $user_id ID of the user. * @param array $flags New flags to add for the user. */ public function add_flags( $user_id, $flags ) { $this->flags[ $user_id ] = wp_parse_args( $flags, $this->get_flags( $user_id ) ); } /** * Save the user meta, if we're interested in it. * Also uses the time to add flags for the user. * * @access public * * @param int $meta_id ID of the meta object. * @param int $user_id ID of the user. * @param string $meta_key Meta key. * @param mixed $value Meta value. */ public function maybe_save_user_meta( $meta_id, $user_id, $meta_key, $value ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable if ( 'locale' === $meta_key ) { $this->add_flags( $user_id, array( 'locale_changed' => true ) ); } $user = get_user_by( 'id', $user_id ); if ( isset( $user->cap_key ) && $meta_key === $user->cap_key ) { $this->add_flags( $user_id, array( 'capabilities_changed' => true ) ); } if ( $this->is_create_user() || $this->is_add_user_to_blog() || $this->is_delete_user() ) { return; } if ( isset( $this->flags[ $user_id ] ) ) { /** * This action is documented already in this file */ do_action( 'jetpack_sync_save_user', $user_id, $this->get_flags( $user_id ) ); } } /** * Enqueue the users actions for full sync. * * @access public * * @param array $config Full sync configuration for this sync module. * @param int $max_items_to_enqueue Maximum number of items to enqueue. * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. * @return array Number of actions enqueued, and next module state. */ public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { global $wpdb; return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_users', $wpdb->usermeta, 'user_id', $this->get_where_sql( $config ), $max_items_to_enqueue, $state ); } /** * Retrieve an estimated number of actions that will be enqueued. * * @access public * * @todo Refactor to prepare the SQL query before executing it. * * @param array $config Full sync configuration for this sync module. * @return array Number of items yet to be enqueued. */ public function estimate_full_sync_actions( $config ) { global $wpdb; $query = "SELECT count(*) FROM $wpdb->usermeta"; $where_sql = $this->get_where_sql( $config ); if ( $where_sql ) { $query .= ' WHERE ' . $where_sql; } // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared $count = $wpdb->get_var( $query ); return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); } /** * Retrieve the WHERE SQL clause based on the module config. * * @access public * * @param array $config Full sync configuration for this sync module. * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. */ public function get_where_sql( $config ) { global $wpdb; $query = "meta_key = '{$wpdb->prefix}user_level' AND meta_value > 0"; // The $config variable is a list of user IDs to sync. if ( is_array( $config ) ) { $query .= ' AND user_id IN (' . implode( ',', array_map( 'intval', $config ) ) . ')'; } return $query; } /** * Retrieve the actions that will be sent for this module during a full sync. * * @access public * * @return array Full sync actions of this module. */ public function get_full_sync_actions() { return array( 'jetpack_full_sync_users' ); } /** * Retrieve initial sync user config. * * @access public * * @todo Refactor the SQL query to call $wpdb->prepare() before execution. * * @return array|boolean IDs of users to initially sync, or false if tbe number of users exceed the maximum. */ public function get_initial_sync_user_config() { global $wpdb; // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared $user_ids = $wpdb->get_col( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}user_level' AND meta_value > 0 LIMIT " . ( self::MAX_INITIAL_SYNC_USERS + 1 ) ); $user_ids_count = is_countable( $user_ids ) ? count( $user_ids ) : 0; if ( $user_ids_count <= self::MAX_INITIAL_SYNC_USERS ) { return $user_ids; } else { return false; } } /** * Expand the users within a hook before they are serialized and sent to the server. * * @access public * * @param array $args The hook arguments. * @return array $args The hook arguments. */ public function expand_users( $args ) { list( $user_ids, $previous_end ) = $args; return array( 'users' => array_map( array( $this, 'sanitize_user_and_expand' ), get_users( array( 'include' => $user_ids, 'orderby' => 'ID', 'order' => 'DESC', ) ) ), 'previous_end' => $previous_end, ); } /** * Handler for user removal from a particular blog. * * @access public * * @param int $user_id ID of the user. * @param int $blog_id ID of the blog. */ public function remove_user_from_blog_handler( $user_id, $blog_id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable // User is removed on add, see https://github.com/WordPress/WordPress/blob/0401cee8b36df3def8e807dd766adc02b359dfaf/wp-includes/ms-functions.php#L2114. if ( $this->is_add_new_user_to_blog() ) { return; } $reassigned_user_id = $this->get_reassigned_network_user_id(); // Note that we are in the context of the blog the user is removed from, see https://github.com/WordPress/WordPress/blob/473e1ba73bc5c18c72d7f288447503713d518790/wp-includes/ms-functions.php#L233. /** * Fires when a user is removed from a blog on a multisite installation * * @since 1.6.3 * @since-jetpack 5.4.0 * * @param int $user_id - ID of the removed user * @param int $reassigned_user_id - ID of the user the removed user's posts are reassigned to (if any). */ do_action( 'jetpack_removed_user_from_blog', $user_id, $reassigned_user_id ); } /** * Whether we're adding a new user to a blog in this request. * * @access protected * * @return boolean */ protected function is_add_new_user_to_blog() { return $this->is_function_in_backtrace( 'add_new_user_to_blog' ); } /** * Whether we're adding an existing user to a blog in this request. * * @access protected * * @return boolean */ protected function is_add_user_to_blog() { return $this->is_function_in_backtrace( 'add_user_to_blog' ); } /** * Whether we're removing a user from a blog in this request. * * @access protected * * @return boolean */ protected function is_delete_user() { return $this->is_function_in_backtrace( array( 'wp_delete_user', 'remove_user_from_blog' ) ); } /** * Whether we're creating a user or adding a new user to a blog. * * @access protected * * @return boolean */ protected 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. 'wp_create_user', // Used to suppress jetpack_sync_save_user in save_user_role_handler when user registered on multi site. 'wp_insert_user', // Used to suppress jetpack_sync_save_user in save_user_cap_handler and save_user_role_handler when user registered on single site. ); return $this->is_function_in_backtrace( $functions ); } /** * Retrieve the ID of the user the removed user's posts are reassigned to (if any). * * @return int ID of the user that got reassigned as the author of the posts. */ protected function get_reassigned_network_user_id() { $backtrace = debug_backtrace( false ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace foreach ( $backtrace as $call ) { if ( 'remove_user_from_blog' === $call['function'] && 3 === count( $call['args'] ) ) { return $call['args'][2]; } } return false; } /** * Checks if one or more function names is in debug_backtrace. * * @access protected * * @param array|string $names Mixed string name of function or array of string names of functions. * @return bool */ protected function is_function_in_backtrace( $names ) { $backtrace = debug_backtrace( false ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace if ( ! is_array( $names ) ) { $names = array( $names ); } $names_as_keys = array_flip( $names ); $backtrace_functions = array_column( $backtrace, 'function' ); $backtrace_functions_as_keys = array_flip( $backtrace_functions ); $intersection = array_intersect_key( $backtrace_functions_as_keys, $names_as_keys ); return ! empty( $intersection ); } }