class.jetpack-sync-module-users.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. class Jetpack_Sync_Module_Users extends Jetpack_Sync_Module {
  3. const MAX_INITIAL_SYNC_USERS = 100;
  4. protected $flags = array();
  5. function name() {
  6. return 'users';
  7. }
  8. // this is here to support the backfill API
  9. public function get_object_by_id( $object_type, $id ) {
  10. if ( $object_type === 'user' && $user = get_user_by( 'id', intval( $id ) ) ) {
  11. return $this->sanitize_user_and_expand( $user );
  12. }
  13. return false;
  14. }
  15. public function init_listeners( $callable ) {
  16. // users
  17. add_action( 'user_register', array( $this, 'user_register_handler' ) );
  18. add_action( 'profile_update', array( $this, 'save_user_handler' ), 10, 2 );
  19. add_action( 'add_user_to_blog', array( $this, 'add_user_to_blog_handler' ) );
  20. add_action( 'jetpack_sync_add_user', $callable, 10, 2 );
  21. add_action( 'jetpack_sync_add_user', array( $this, 'clear_flags' ), 11 );
  22. add_action( 'jetpack_sync_register_user', $callable, 10, 2 );
  23. add_action( 'jetpack_sync_register_user', array( $this, 'clear_flags' ), 11 );
  24. add_action( 'jetpack_sync_save_user', $callable, 10, 2 );
  25. add_action( 'jetpack_sync_save_user', array( $this, 'clear_flags' ), 11 );
  26. add_action( 'jetpack_sync_user_locale', $callable, 10, 2 );
  27. add_action( 'jetpack_sync_user_locale_delete', $callable, 10, 1 );
  28. add_action( 'deleted_user', array( $this, 'deleted_user_handler' ), 10, 2 );
  29. add_action( 'jetpack_deleted_user', $callable, 10, 3 );
  30. add_action( 'remove_user_from_blog', array( $this, 'remove_user_from_blog_handler' ), 10, 2 );
  31. add_action( 'jetpack_removed_user_from_blog', $callable, 10, 2 );
  32. // user roles
  33. add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 );
  34. add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 );
  35. add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 );
  36. // user capabilities
  37. add_action( 'added_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 );
  38. add_action( 'updated_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 );
  39. add_action( 'deleted_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 );
  40. // user authentication
  41. add_action( 'wp_login', $callable, 10, 2 );
  42. add_action( 'wp_logout', $callable, 10, 0 );
  43. add_action( 'wp_masterbar_logout', $callable, 10, 0 );
  44. // Add on init
  45. add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_add_user', array( $this, 'expand_action' ) );
  46. add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_register_user', array( $this, 'expand_action' ) );
  47. add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_save_user', array( $this, 'expand_action' ) );
  48. }
  49. public function init_full_sync_listeners( $callable ) {
  50. add_action( 'jetpack_full_sync_users', $callable );
  51. }
  52. public function init_before_send() {
  53. add_filter( 'jetpack_sync_before_send_wp_login', array( $this, 'expand_login_username' ), 10, 1 );
  54. add_filter( 'jetpack_sync_before_send_wp_logout', array( $this, 'expand_logout_username' ), 10, 2 );
  55. // full sync
  56. add_filter( 'jetpack_sync_before_send_jetpack_full_sync_users', array( $this, 'expand_users' ) );
  57. }
  58. private function get_user( $user ) {
  59. if ( is_numeric( $user ) ) {
  60. $user = get_user_by( 'id', $user );
  61. }
  62. if ( $user instanceof WP_User ) {
  63. return $user;
  64. }
  65. return null;
  66. }
  67. public function sanitize_user( $user ) {
  68. $user = $this->get_user( $user );
  69. // this create a new user object and stops the passing of the object by reference.
  70. $user = unserialize( serialize( $user ) );
  71. if ( is_object( $user ) && is_object( $user->data ) ) {
  72. unset( $user->data->user_pass );
  73. }
  74. return $user;
  75. }
  76. public function expand_user( $user ) {
  77. if ( ! is_object( $user ) ) {
  78. return null;
  79. }
  80. $user->allowed_mime_types = get_allowed_mime_types( $user );
  81. $user->allcaps = $this->get_real_user_capabilities( $user );
  82. if ( function_exists( 'get_user_locale' ) ) {
  83. // Only set the user locale if it is different from the site local
  84. if ( get_locale() !== get_user_locale( $user->ID ) ) {
  85. $user->locale = get_user_locale( $user->ID );
  86. }
  87. }
  88. return $user;
  89. }
  90. public function get_real_user_capabilities( $user ) {
  91. $user_capabilities = array();
  92. if ( is_wp_error( $user ) ) {
  93. return $user_capabilities;
  94. }
  95. foreach( Jetpack_Sync_Defaults::get_capabilities_whitelist() as $capability ) {
  96. if ( $user_has_capabilities = user_can( $user , $capability ) ) {
  97. $user_capabilities[ $capability ] = true;
  98. }
  99. }
  100. return $user_capabilities;
  101. }
  102. public function sanitize_user_and_expand( $user ) {
  103. $user = $this->get_user( $user );
  104. $user = $this->expand_user( $user );
  105. return $this->sanitize_user( $user );
  106. }
  107. public function expand_action( $args ) {
  108. // the first argument is always the user
  109. list( $user ) = $args;
  110. if ( $user ) {
  111. $args[0] = $this->sanitize_user_and_expand( $user );
  112. return $args;
  113. }
  114. return false;
  115. }
  116. public function expand_login_username( $args ) {
  117. list( $login, $user ) = $args;
  118. $user = $this->sanitize_user( $user );
  119. return array( $login, $user );
  120. }
  121. public function expand_logout_username( $args, $user_id ) {
  122. $user = get_userdata( $user_id );
  123. $user = $this->sanitize_user( $user );
  124. $login = '';
  125. if ( is_object( $user ) && is_object( $user->data ) ) {
  126. $login = $user->data->user_login;
  127. }
  128. // if we don't have a user here lets not send anything.
  129. if ( empty( $login ) ) {
  130. return false;
  131. }
  132. return array( $login, $user );
  133. }
  134. public function deleted_user_handler( $deleted_user_id, $reassigned_user_id = '' ) {
  135. $is_multisite = is_multisite();
  136. /**
  137. * Fires when a user is deleted on a site
  138. *
  139. * @since 5.4.0
  140. *
  141. * @param int $deleted_user_id - ID of the deleted user
  142. * @param int $reassigned_user_id - ID of the user the deleted user's posts is reassigned to (if any)
  143. * @param bool $is_multisite - Whether this site is a multisite installation
  144. */
  145. do_action( 'jetpack_deleted_user', $deleted_user_id, $reassigned_user_id, $is_multisite );
  146. }
  147. function user_register_handler( $user_id, $old_user_data = null ) {
  148. // ensure we only sync users who are members of the current blog
  149. if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) {
  150. return;
  151. }
  152. if ( Jetpack_Constants::is_true( 'JETPACK_INVITE_ACCEPTED' ) ) {
  153. $this->add_flags( $user_id, array( 'invitation_accepted' => true ) );
  154. }
  155. /**
  156. * Fires when a new user is registered on a site
  157. *
  158. * @since 4.9.0
  159. *
  160. * @param object The WP_User object
  161. */
  162. do_action( 'jetpack_sync_register_user', $user_id, $this->get_flags( $user_id ) );
  163. }
  164. function add_user_to_blog_handler( $user_id, $old_user_data = null ) {
  165. // ensure we only sync users who are members of the current blog
  166. if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) {
  167. return;
  168. }
  169. if ( Jetpack_Constants::is_true( 'JETPACK_INVITE_ACCEPTED' ) ) {
  170. $this->add_flags( $user_id, array( 'invitation_accepted' => true ) );
  171. }
  172. /**
  173. * Fires when a user is added on a site
  174. *
  175. * @since 4.9.0
  176. *
  177. * @param object The WP_User object
  178. */
  179. do_action( 'jetpack_sync_add_user', $user_id, $this->get_flags( $user_id ) );
  180. }
  181. function save_user_handler( $user_id, $old_user_data = null ) {
  182. // ensure we only sync users who are members of the current blog
  183. if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) {
  184. return;
  185. }
  186. $user = get_user_by( 'id', $user_id );
  187. // Older versions of WP don't pass the old_user_data in ->data
  188. if ( isset( $old_user_data->data ) ) {
  189. $old_user = $old_user_data->data;
  190. } else {
  191. $old_user = $old_user_data;
  192. }
  193. if ( $old_user !== null && $user->user_pass !== $old_user->user_pass ) {
  194. $this->flags[ $user_id ]['password_changed'] = true;
  195. }
  196. if ( $old_user !== null && $user->data->user_email !== $old_user->user_email ) {
  197. // The '_new_email' user meta is deleted right after the call to wp_update_user
  198. // that got us to this point so if it's still set then this was a user confirming
  199. // their new email address
  200. if ( 1 === intval( get_user_meta( $user->ID, '_new_email', true ) ) ) {
  201. $this->flags[ $user_id ]['email_changed'] = true;
  202. }
  203. }
  204. /**
  205. * Fires when the client needs to sync an updated user
  206. *
  207. * @since 4.2.0
  208. *
  209. * @param object The WP_User object
  210. * @param array state - New since 5.8.0
  211. */
  212. do_action( 'jetpack_sync_save_user', $user_id, $this->get_flags( $user_id ) );
  213. }
  214. function save_user_role_handler( $user_id, $role, $old_roles = null ) {
  215. $this->add_flags( $user_id, array( 'role_changed' => true, 'previous_role' => $old_roles ) );
  216. //The jetpack_sync_register_user payload is identical to jetpack_sync_save_user, don't send both
  217. if ( $this->is_create_user() || $this->is_add_user_to_blog() ) {
  218. return;
  219. }
  220. /**
  221. * This action is documented already in this file
  222. */
  223. do_action( 'jetpack_sync_save_user', $user_id, $this->get_flags( $user_id ) );
  224. }
  225. function get_flags( $user_id ) {
  226. if ( isset( $this->flags[ $user_id ] ) ) {
  227. return $this->flags[ $user_id ];
  228. }
  229. return array();
  230. }
  231. function clear_flags( $user_id ) {
  232. if ( isset( $this->flags[ $user_id ] ) ) {
  233. unset( $this->flags[ $user_id ] );
  234. }
  235. }
  236. function add_flags( $user_id, $flags ) {
  237. $this->flags[ $user_id ] = wp_parse_args( $flags, $this->get_flags( $user_id ) );
  238. }
  239. function maybe_save_user_meta( $meta_id, $user_id, $meta_key, $value ) {
  240. if ( $meta_key === 'locale' ) {
  241. $this->add_flags( $user_id, array( 'locale_changed' => true ) );
  242. }
  243. $user = get_user_by( 'id', $user_id );
  244. if ( $meta_key === $user->cap_key ) {
  245. $this->add_flags( $user_id, array( 'capabilities_changed' => true ) );
  246. }
  247. if ( $this->is_create_user() || $this->is_add_user_to_blog() || $this->is_delete_user() ) {
  248. return;
  249. }
  250. if ( isset( $this->flags[ $user_id ] ) ) {
  251. /**
  252. * This action is documented already in this file
  253. */
  254. do_action( 'jetpack_sync_save_user', $user_id, $this->get_flags( $user_id ) );
  255. }
  256. }
  257. public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
  258. global $wpdb;
  259. 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 );
  260. }
  261. public function estimate_full_sync_actions( $config ) {
  262. global $wpdb;
  263. $query = "SELECT count(*) FROM $wpdb->usermeta";
  264. if ( $where_sql = $this->get_where_sql( $config ) ) {
  265. $query .= ' WHERE ' . $where_sql;
  266. }
  267. $count = $wpdb->get_var( $query );
  268. return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
  269. }
  270. private function get_where_sql( $config ) {
  271. global $wpdb;
  272. $query = "meta_key = '{$wpdb->prefix}capabilities'";
  273. // config is a list of user IDs to sync
  274. if ( is_array( $config ) ) {
  275. $query .= ' AND user_id IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
  276. }
  277. return $query;
  278. }
  279. function get_full_sync_actions() {
  280. return array( 'jetpack_full_sync_users' );
  281. }
  282. function get_initial_sync_user_config() {
  283. global $wpdb;
  284. $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 ) );
  285. if ( count( $user_ids ) <= self::MAX_INITIAL_SYNC_USERS ) {
  286. return $user_ids;
  287. } else {
  288. return false;
  289. }
  290. }
  291. public function expand_users( $args ) {
  292. $user_ids = $args[0];
  293. return array_map( array( $this, 'sanitize_user_and_expand' ), get_users( array( 'include' => $user_ids ) ) );
  294. }
  295. public function remove_user_from_blog_handler( $user_id, $blog_id ) {
  296. //User is removed on add, see https://github.com/WordPress/WordPress/blob/0401cee8b36df3def8e807dd766adc02b359dfaf/wp-includes/ms-functions.php#L2114
  297. if ( $this->is_add_new_user_to_blog() ) {
  298. return;
  299. }
  300. $reassigned_user_id = $this->get_reassigned_network_user_id();
  301. //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
  302. /**
  303. * Fires when a user is removed from a blog on a multisite installation
  304. *
  305. * @since 5.4.0
  306. *
  307. * @param int $user_id - ID of the removed user
  308. * @param int $reassigned_user_id - ID of the user the removed user's posts is reassigned to (if any)
  309. */
  310. do_action( 'jetpack_removed_user_from_blog', $user_id, $reassigned_user_id );
  311. }
  312. protected function is_add_new_user_to_blog() {
  313. return Jetpack::is_function_in_backtrace( 'add_new_user_to_blog' );
  314. }
  315. protected function is_add_user_to_blog() {
  316. return Jetpack::is_function_in_backtrace( 'add_user_to_blog' );
  317. }
  318. protected function is_delete_user() {
  319. return Jetpack::is_function_in_backtrace( array( 'wp_delete_user' , 'remove_user_from_blog' ) );
  320. }
  321. protected function is_create_user() {
  322. $functions = array(
  323. 'add_new_user_to_blog', // Used to suppress jetpack_sync_save_user in save_user_cap_handler when user registered on multi site
  324. 'wp_create_user', // Used to suppress jetpack_sync_save_user in save_user_role_handler when user registered on multi site
  325. '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
  326. );
  327. return Jetpack::is_function_in_backtrace( $functions );
  328. }
  329. protected function get_reassigned_network_user_id() {
  330. $backtrace = debug_backtrace( false ); // phpcs:ignore PHPCompatibility
  331. foreach ( $backtrace as $call ) {
  332. if (
  333. 'remove_user_from_blog' === $call['function'] &&
  334. 3 === count( $call['args'] )
  335. ) {
  336. return $call['args'][2];
  337. }
  338. }
  339. return false;
  340. }
  341. }