Jetpack_Sync_Module_Users

The WordPress Core Jetpack Sync Module Users class.

Defined (1)

The class is defined in the following location(s).

/sync/class.jetpack-sync-module-users.php  
  1. class Jetpack_Sync_Module_Users extends Jetpack_Sync_Module { 
  2. const MAX_INITIAL_SYNC_USERS = 100; 
  3.  
  4. function name() { 
  5. return 'users'; 
  6.  
  7. // this is here to support the backfill API 
  8. public function get_object_by_id( $object_type, $id ) { 
  9. if ( $object_type === 'user' && $user = get_user_by( 'id', intval( $id ) ) ) { 
  10. return $this->sanitize_user_and_expand( $user ); 
  11.  
  12. return false; 
  13.  
  14. public function init_listeners( $callable ) { 
  15. // users 
  16. add_action( 'user_register', array( $this, 'save_user_handler' ) ); 
  17. add_action( 'profile_update', array( $this, 'save_user_handler' ), 10, 2 ); 
  18. add_action( 'add_user_to_blog', array( $this, 'save_user_handler' ) ); 
  19. add_action( 'jetpack_sync_add_user', $callable, 10, 2 ); 
  20. add_action( 'jetpack_sync_register_user', $callable, 10, 2 ); 
  21. add_action( 'jetpack_sync_save_user', $callable, 10, 2 ); 
  22.  
  23. add_action( 'jetpack_sync_user_locale', $callable, 10, 2 ); 
  24. add_action( 'jetpack_sync_user_locale_delete', $callable, 10, 1 ); 
  25.  
  26. add_action( 'deleted_user', $callable, 10, 2 ); 
  27. add_action( 'remove_user_from_blog', $callable, 10, 2 ); 
  28.  
  29. // user roles 
  30. add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); 
  31. add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 ); 
  32. add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); 
  33.  
  34. // user capabilities 
  35. add_action( 'added_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 ); 
  36. add_action( 'updated_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 ); 
  37. add_action( 'deleted_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 ); 
  38.  
  39. // user authentication 
  40. add_action( 'wp_login', $callable, 10, 2 ); 
  41. add_action( 'wp_login_failed', $callable, 10, 2 ); 
  42. add_action( 'wp_logout', $callable, 10, 0 ); 
  43. add_action( 'wp_masterbar_logout', $callable, 10, 0 ); 
  44.  
  45. public function init_full_sync_listeners( $callable ) { 
  46. add_action( 'jetpack_full_sync_users', $callable ); 
  47.  
  48. public function init_before_send() { 
  49. add_filter( 'jetpack_sync_before_send_jetpack_sync_add_user', array( $this, 'expand_user' ) ); 
  50. add_filter( 'jetpack_sync_before_send_jetpack_sync_register_user', array( $this, 'expand_user' ) ); 
  51. add_filter( 'jetpack_sync_before_send_jetpack_sync_save_user', array( $this, 'expand_user' ) ); 
  52. add_filter( 'jetpack_sync_before_send_wp_login', array( $this, 'expand_login_username' ), 10, 1 ); 
  53. add_filter( 'jetpack_sync_before_send_wp_logout', array( $this, 'expand_logout_username' ), 10, 2 ); 
  54.  
  55. // full sync 
  56. add_filter( 'jetpack_sync_before_send_jetpack_full_sync_users', array( $this, 'expand_users' ) ); 
  57.  
  58. public function sanitize_user_and_expand( $user ) { 
  59. $user = $this->sanitize_user( $user ); 
  60.  
  61. return $this->add_to_user( $user ); 
  62.  
  63. public function sanitize_user( $user ) { 
  64. // this create a new user object and stops the passing of the object by reference. 
  65. $user = unserialize( serialize( $user ) ); 
  66.  
  67. if ( is_object( $user ) && is_object( $user->data ) ) { 
  68. unset( $user->data->user_pass ); 
  69.  
  70. return $user; 
  71.  
  72. public function add_to_user( $user ) { 
  73. $user->allowed_mime_types = get_allowed_mime_types( $user ); 
  74.  
  75. if ( function_exists( 'get_user_locale' ) ) { 
  76.  
  77. // Only set the user locale if it is different from the site local 
  78. if ( get_locale() !== get_user_locale( $user->ID ) ) { 
  79. $user->locale = get_user_locale( $user->ID ); 
  80. return $user; 
  81.  
  82. public function expand_user( $args ) { 
  83. list( $user ) = $args; 
  84.  
  85. if ( $user ) { 
  86. return array( $this->add_to_user( $user ) ); 
  87.  
  88. return false; 
  89.  
  90. public function expand_login_username( $args ) { 
  91. list( $login, $user ) = $args; 
  92. $user = $this->sanitize_user( $user ); 
  93.  
  94. return array( $login, $user ); 
  95.  
  96. public function expand_logout_username( $args, $user_id ) { 
  97. $user = get_userdata( $user_id ); 
  98. $user = $this->sanitize_user( $user ); 
  99. $login = ''; 
  100. if( is_object( $user ) && is_object( $user->data ) ) { 
  101. $login = $user->data->user_login; 
  102.  
  103. return array( $login, $user ); 
  104.  
  105. function save_user_handler( $user_id, $old_user_data = null ) { 
  106. // ensure we only sync users who are members of the current blog 
  107. if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) { 
  108. return; 
  109.  
  110. $user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); 
  111.  
  112. // Older versions of WP don't pass the old_user_data in ->data 
  113. if ( isset( $old_user_data->data ) ) { 
  114. $old_user = $old_user_data->data; 
  115. } else { 
  116. $old_user = $old_user_data; 
  117.  
  118. if ( $old_user !== null ) { 
  119. unset( $old_user->user_pass ); 
  120. if ( serialize( $old_user ) === serialize( $user->data ) ) { 
  121. return; 
  122.  
  123. if ( 'user_register' === current_filter() ) { 
  124. /** 
  125. * Fires when a new user is registered on a site 
  126. * @since 4.9.0 
  127. * @param object The WP_User object 
  128. */ 
  129. do_action( 'jetpack_sync_register_user', $user ); 
  130. return; 
  131. /** MU Sites add users instead of register them to sites */ 
  132. if ( 'add_user_to_blog' === current_filter() ) { 
  133. /** 
  134. * Fires when a new user is added to a site. (WordPress Multisite) 
  135. * @since 4.9.0 
  136. * @param object The WP_User object 
  137. */ 
  138. do_action( 'jetpack_sync_add_user', $user ); 
  139. return; 
  140. /** 
  141. * Fires when the client needs to sync an updated user 
  142. * @since 4.2.0 
  143. * @param object The WP_User object 
  144. */ 
  145. do_action( 'jetpack_sync_save_user', $user ); 
  146.  
  147. function save_user_role_handler( $user_id, $role, $old_roles = null ) { 
  148. $user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); 
  149.  
  150. /** 
  151. * Fires when the client needs to sync an updated user 
  152. * @since 4.2.0 
  153. * @param object The WP_User object 
  154. */ 
  155. do_action( 'jetpack_sync_save_user', $user ); 
  156.  
  157. function maybe_save_user_meta( $meta_id, $user_id, $meta_key, $value ) { 
  158. if ( $meta_key === 'locale' ) { 
  159. if ( current_filter() === 'deleted_user_meta' ) { 
  160. /** 
  161. * Allow listeners to listen for user local delete changes 
  162. * @since 4.8.0 
  163. * @param int $user_id - The ID of the user whos locale is being deleted 
  164. */ 
  165. do_action( 'jetpack_sync_user_locale_delete', $user_id ); 
  166. } else { 
  167. /** 
  168. * Allow listeners to listen for user local changes 
  169. * @since 4.8.0 
  170. * @param int $user_id - The ID of the user whos locale is being changed 
  171. * @param int $value - The value of the new locale 
  172. */ 
  173. do_action( 'jetpack_sync_user_locale', $user_id, $value ); 
  174. $this->save_user_cap_handler( $meta_id, $user_id, $meta_key, $value ); 
  175.  
  176. function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) { 
  177. // if a user is currently being removed as a member of this blog, we don't fire the event 
  178. if ( current_filter() === 'deleted_user_meta' 
  179. && 
  180. preg_match( '/capabilities|user_level/', $meta_key ) 
  181. && 
  182. ! is_user_member_of_blog( $user_id, get_current_blog_id() ) 
  183. ) { 
  184. return; 
  185.  
  186. $user = get_user_by( 'id', $user_id ); 
  187. if ( $meta_key === $user->cap_key ) { 
  188. /** 
  189. * Fires when the client needs to sync an updated user 
  190. * @since 4.2.0 
  191. * @param object The Sanitized WP_User object 
  192. */ 
  193. do_action( 'jetpack_sync_save_user', $this->sanitize_user( $user ) ); 
  194.  
  195. public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { 
  196. global $wpdb; 
  197. 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 ); 
  198.  
  199. public function estimate_full_sync_actions( $config ) { 
  200. global $wpdb; 
  201.  
  202. $query = "SELECT count(*) FROM $wpdb->usermeta"; 
  203.  
  204. if ( $where_sql = $this->get_where_sql( $config ) ) { 
  205. $query .= ' WHERE ' . $where_sql; 
  206.  
  207. $count = $wpdb->get_var( $query ); 
  208.  
  209. return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); 
  210.  
  211. private function get_where_sql( $config ) { 
  212. global $wpdb; 
  213.  
  214. $query = "meta_key = '{$wpdb->prefix}capabilities'"; 
  215.  
  216. // config is a list of user IDs to sync 
  217. if ( is_array( $config ) ) { 
  218. $query .= ' AND user_id IN (' . implode( ', ', array_map( 'intval', $config ) ) . ')'; 
  219.  
  220. return $query; 
  221.  
  222. function get_full_sync_actions() { 
  223. return array( 'jetpack_full_sync_users' ); 
  224.  
  225. function get_initial_sync_user_config() { 
  226. global $wpdb; 
  227.  
  228. $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 ) ); 
  229.  
  230. if ( count( $user_ids ) <= self::MAX_INITIAL_SYNC_USERS ) { 
  231. return $user_ids; 
  232. } else { 
  233. return false; 
  234.  
  235. public function expand_users( $args ) { 
  236. $user_ids = $args[0]; 
  237.  
  238. return array_map( array( $this, 'sanitize_user_and_expand' ), get_users( array( 'include' => $user_ids ) ) );