← All changes
|
jetpack_vendor/automattic/jetpack-sync/src/modules/class-users.php
+62
-53
13.3.3
→
16.3-a.7
View file →
| @@ -6,12 +6,17 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace Automattic\Jetpack\Sync\Modules; |
| 9 | 9 | |
| 10 | +use Automattic\Jetpack\Connection\Manager; | |
| 10 | 11 | use Automattic\Jetpack\Constants as Jetpack_Constants; |
| 11 | 12 | use Automattic\Jetpack\Password_Checker; |
| 12 | 13 | use Automattic\Jetpack\Sync\Defaults; |
| 13 | 14 | |
| 15 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 16 | + exit( 0 ); | |
| 17 | +} | |
| 18 | + | |
| 14 | 19 | /** |
| 15 | 20 | * Class to handle sync for users. |
| 16 | 21 | */ |
| 17 | 22 | class Users extends Module { |
| @@ -57,19 +62,33 @@ | ||
| 57 | 62 | return 'users'; |
| 58 | 63 | } |
| 59 | 64 | |
| 60 | 65 | /** |
| 61 | - * The table in the database. | |
| 66 | + * The table name. | |
| 62 | 67 | * |
| 63 | 68 | * @access public |
| 64 | 69 | * |
| 65 | 70 | * @return string |
| 71 | + * @deprecated since 3.11.0 Use table() instead. | |
| 66 | 72 | */ |
| 67 | 73 | public function table_name() { |
| 74 | + _deprecated_function( __METHOD__, '3.11.0', 'Automattic\\Jetpack\\Sync\\Users->table' ); | |
| 68 | 75 | return 'usermeta'; |
| 69 | 76 | } |
| 70 | 77 | |
| 71 | 78 | /** |
| 79 | + * The table in the database with the prefix. | |
| 80 | + * | |
| 81 | + * @access public | |
| 82 | + * | |
| 83 | + * @return string|bool | |
| 84 | + */ | |
| 85 | + public function table() { | |
| 86 | + global $wpdb; | |
| 87 | + return $wpdb->usermeta; | |
| 88 | + } | |
| 89 | + | |
| 90 | + /** | |
| 72 | 91 | * The id field in the database. |
| 73 | 92 | * |
| 74 | 93 | * @access public |
| 75 | 94 | * |
| @@ -122,9 +141,9 @@ | ||
| 122 | 141 | add_action( 'jetpack_sync_user_locale_delete', $callable, 10, 1 ); |
| 123 | 142 | |
| 124 | 143 | add_action( 'deleted_user', array( $this, 'deleted_user_handler' ), 10, 2 ); |
| 125 | 144 | add_action( 'jetpack_deleted_user', $callable, 10, 3 ); |
| 126 | - add_action( 'remove_user_from_blog', array( $this, 'remove_user_from_blog_handler' ), 10, 2 ); | |
| 145 | + add_action( 'remove_user_from_blog', array( $this, 'remove_user_from_blog_handler' ), 10, 3 ); | |
| 127 | 146 | add_action( 'jetpack_removed_user_from_blog', $callable, 10, 2 ); |
| 128 | 147 | |
| 129 | 148 | // User roles. |
| 130 | 149 | add_action( 'add_user_role', array( $this, 'add_user_role_handler' ), 10, 2 ); |
| @@ -179,9 +198,9 @@ | ||
| 179 | 198 | * |
| 180 | 199 | * @access private |
| 181 | 200 | * |
| 182 | 201 | * @param mixed $user User object or ID. |
| 183 | - * @return \WP_User User object, or `null` if user invalid/not found. | |
| 202 | + * @return \WP_User|null User object, or `null` if user invalid/not found. | |
| 184 | 203 | */ |
| 185 | 204 | private function get_user( $user ) { |
| 186 | 205 | if ( is_numeric( $user ) ) { |
| 187 | 206 | $user = get_user_by( 'id', $user ); |
| @@ -234,8 +253,10 @@ | ||
| 234 | 253 | if ( get_locale() !== get_user_locale( $user->ID ) ) { |
| 235 | 254 | $user->locale = get_user_locale( $user->ID ); |
| 236 | 255 | } |
| 237 | 256 | |
| 257 | + $user->is_connected = ( new Manager( 'jetpack' ) )->is_user_connected( $user->ID ); | |
| 258 | + | |
| 238 | 259 | return $user; |
| 239 | 260 | } |
| 240 | 261 | |
| 241 | 262 | /** |
| @@ -342,9 +363,13 @@ | ||
| 342 | 363 | * |
| 343 | 364 | * @param string $user_login The user login. |
| 344 | 365 | * @param \WP_User $user The user object. |
| 345 | 366 | */ |
| 346 | - public function wp_login_handler( $user_login, $user ) { | |
| 367 | + public function wp_login_handler( $user_login, $user = null ) { | |
| 368 | + if ( ! $user instanceof \WP_User || empty( $user->ID ) ) { | |
| 369 | + return; | |
| 370 | + } | |
| 371 | + | |
| 347 | 372 | /** |
| 348 | 373 | * Fires when a user is logged into a site. |
| 349 | 374 | * |
| 350 | 375 | * @since 1.6.3 |
| @@ -527,9 +552,9 @@ | ||
| 527 | 552 | } |
| 528 | 553 | continue; |
| 529 | 554 | } |
| 530 | 555 | |
| 531 | - $flag = isset( $this->user_fields_to_flags_mapping[ $user_field ] ) ? $this->user_fields_to_flags_mapping[ $user_field ] : 'unknown_field_changed'; | |
| 556 | + $flag = $this->user_fields_to_flags_mapping[ $user_field ] ?? 'unknown_field_changed'; | |
| 532 | 557 | |
| 533 | 558 | $this->flags[ $user_id ][ $flag ] = true; |
| 534 | 559 | } |
| 535 | 560 | } |
| @@ -606,9 +631,11 @@ | ||
| 606 | 631 | ) |
| 607 | 632 | ); |
| 608 | 633 | |
| 609 | 634 | // The jetpack_sync_register_user payload is identical to jetpack_sync_save_user, don't send both. |
| 610 | - if ( $this->is_create_user() || $this->is_add_user_to_blog() ) { | |
| 635 | + if ( $this->is_function_in_backtrace( | |
| 636 | + array_merge( $this->get_create_user_functions(), $this->get_add_user_to_blog_functions() ) | |
| 637 | + ) ) { | |
| 611 | 638 | return; |
| 612 | 639 | } |
| 613 | 640 | /** |
| 614 | 641 | * This action is documented already in this file |
| @@ -677,9 +704,11 @@ | ||
| 677 | 704 | if ( isset( $user->cap_key ) && $meta_key === $user->cap_key ) { |
| 678 | 705 | $this->add_flags( $user_id, array( 'capabilities_changed' => true ) ); |
| 679 | 706 | } |
| 680 | 707 | |
| 681 | - if ( $this->is_create_user() || $this->is_add_user_to_blog() || $this->is_delete_user() ) { | |
| 708 | + if ( $this->is_function_in_backtrace( | |
| 709 | + array_merge( $this->get_create_user_functions(), $this->get_add_user_to_blog_functions(), $this->get_delete_user_functions() ) | |
| 710 | + ) ) { | |
| 682 | 711 | return; |
| 683 | 712 | } |
| 684 | 713 | |
| 685 | 714 | if ( isset( $this->flags[ $user_id ] ) ) { |
| @@ -713,9 +742,9 @@ | ||
| 713 | 742 | * |
| 714 | 743 | * @todo Refactor to prepare the SQL query before executing it. |
| 715 | 744 | * |
| 716 | 745 | * @param array $config Full sync configuration for this sync module. |
| 717 | - * @return array Number of items yet to be enqueued. | |
| 746 | + * @return int Number of items yet to be enqueued. | |
| 718 | 747 | */ |
| 719 | 748 | public function estimate_full_sync_actions( $config ) { |
| 720 | 749 | global $wpdb; |
| 721 | 750 | |
| @@ -725,10 +754,10 @@ | ||
| 725 | 754 | if ( $where_sql ) { |
| 726 | 755 | $query .= ' WHERE ' . $where_sql; |
| 727 | 756 | } |
| 728 | 757 | |
| 729 | - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 730 | - $count = $wpdb->get_var( $query ); | |
| 758 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 759 | + $count = (int) $wpdb->get_var( $query ); | |
| 731 | 760 | |
| 732 | 761 | return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); |
| 733 | 762 | } |
| 734 | 763 | |
| @@ -745,9 +774,9 @@ | ||
| 745 | 774 | |
| 746 | 775 | $query = "meta_key = '{$wpdb->prefix}user_level' AND meta_value > 0"; |
| 747 | 776 | |
| 748 | 777 | // The $config variable is a list of user IDs to sync. |
| 749 | - if ( is_array( $config ) ) { | |
| 778 | + if ( is_array( $config ) && ! empty( $config ) ) { | |
| 750 | 779 | $query .= ' AND user_id IN (' . implode( ',', array_map( 'intval', $config ) ) . ')'; |
| 751 | 780 | } |
| 752 | 781 | |
| 753 | 782 | return $query; |
| @@ -817,18 +846,19 @@ | ||
| 817 | 846 | * Handler for user removal from a particular blog. |
| 818 | 847 | * |
| 819 | 848 | * @access public |
| 820 | 849 | * |
| 821 | - * @param int $user_id ID of the user. | |
| 822 | - * @param int $blog_id ID of the blog. | |
| 850 | + * @param int $user_id ID of the user. | |
| 851 | + * @param int $blog_id ID of the blog. | |
| 852 | + * @param int $reassign ID of the user to whom to reassign posts. | |
| 823 | 853 | */ |
| 824 | - public function remove_user_from_blog_handler( $user_id, $blog_id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 854 | + public function remove_user_from_blog_handler( $user_id, $blog_id, $reassign = 0 ) { | |
| 825 | 855 | // User is removed on add, see https://github.com/WordPress/WordPress/blob/0401cee8b36df3def8e807dd766adc02b359dfaf/wp-includes/ms-functions.php#L2114. |
| 826 | 856 | if ( $this->is_add_new_user_to_blog() ) { |
| 827 | 857 | return; |
| 828 | 858 | } |
| 829 | 859 | |
| 830 | - $reassigned_user_id = $this->get_reassigned_network_user_id(); | |
| 860 | + $reassigned_user_id = $reassign; | |
| 831 | 861 | |
| 832 | 862 | // 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. |
| 833 | 863 | /** |
| 834 | 864 | * Fires when a user is removed from a blog on a multisite installation |
| @@ -853,66 +883,45 @@ | ||
| 853 | 883 | return $this->is_function_in_backtrace( 'add_new_user_to_blog' ); |
| 854 | 884 | } |
| 855 | 885 | |
| 856 | 886 | /** |
| 857 | - * Whether we're adding an existing user to a blog in this request. | |
| 887 | + * Get the function names that indicate a user is being created. | |
| 858 | 888 | * |
| 859 | 889 | * @access protected |
| 860 | 890 | * |
| 861 | - * @return boolean | |
| 891 | + * @return array | |
| 862 | 892 | */ |
| 863 | - protected function is_add_user_to_blog() { | |
| 864 | - return $this->is_function_in_backtrace( 'add_user_to_blog' ); | |
| 893 | + protected function get_create_user_functions() { | |
| 894 | + return array( | |
| 895 | + 'add_new_user_to_blog', // Used to suppress jetpack_sync_save_user in save_user_cap_handler when user registered on multi site. | |
| 896 | + 'wp_create_user', // Used to suppress jetpack_sync_save_user in save_user_role_handler when user registered on multi site. | |
| 897 | + '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. | |
| 898 | + ); | |
| 865 | 899 | } |
| 866 | 900 | |
| 867 | 901 | /** |
| 868 | - * Whether we're removing a user from a blog in this request. | |
| 902 | + * Get the function names that indicate a user is being added to a blog. | |
| 869 | 903 | * |
| 870 | 904 | * @access protected |
| 871 | 905 | * |
| 872 | - * @return boolean | |
| 906 | + * @return array | |
| 873 | 907 | */ |
| 874 | - protected function is_delete_user() { | |
| 875 | - return $this->is_function_in_backtrace( array( 'wp_delete_user', 'remove_user_from_blog' ) ); | |
| 908 | + protected function get_add_user_to_blog_functions() { | |
| 909 | + return array( 'add_user_to_blog' ); | |
| 876 | 910 | } |
| 877 | 911 | |
| 878 | 912 | /** |
| 879 | - * Whether we're creating a user or adding a new user to a blog. | |
| 913 | + * Get the function names that indicate a user is being deleted. | |
| 880 | 914 | * |
| 881 | 915 | * @access protected |
| 882 | 916 | * |
| 883 | - * @return boolean | |
| 917 | + * @return array | |
| 884 | 918 | */ |
| 885 | - protected function is_create_user() { | |
| 886 | - $functions = array( | |
| 887 | - 'add_new_user_to_blog', // Used to suppress jetpack_sync_save_user in save_user_cap_handler when user registered on multi site. | |
| 888 | - 'wp_create_user', // Used to suppress jetpack_sync_save_user in save_user_role_handler when user registered on multi site. | |
| 889 | - '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. | |
| 890 | - ); | |
| 891 | - | |
| 892 | - return $this->is_function_in_backtrace( $functions ); | |
| 919 | + protected function get_delete_user_functions() { | |
| 920 | + return array( 'wp_delete_user', 'remove_user_from_blog' ); | |
| 893 | 921 | } |
| 894 | 922 | |
| 895 | 923 | /** |
| 896 | - * Retrieve the ID of the user the removed user's posts are reassigned to (if any). | |
| 897 | - * | |
| 898 | - * @return int ID of the user that got reassigned as the author of the posts. | |
| 899 | - */ | |
| 900 | - protected function get_reassigned_network_user_id() { | |
| 901 | - $backtrace = debug_backtrace( false ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace | |
| 902 | - foreach ( $backtrace as $call ) { | |
| 903 | - if ( | |
| 904 | - 'remove_user_from_blog' === $call['function'] && | |
| 905 | - 3 === count( $call['args'] ) | |
| 906 | - ) { | |
| 907 | - return $call['args'][2]; | |
| 908 | - } | |
| 909 | - } | |
| 910 | - | |
| 911 | - return false; | |
| 912 | - } | |
| 913 | - | |
| 914 | - /** | |
| 915 | 924 | * Checks if one or more function names is in debug_backtrace. |
| 916 | 925 | * |
| 917 | 926 | * @access protected |
| 918 | 927 | * |
| @@ -919,9 +928,9 @@ | ||
| 919 | 928 | * @param array|string $names Mixed string name of function or array of string names of functions. |
| 920 | 929 | * @return bool |
| 921 | 930 | */ |
| 922 | 931 | protected function is_function_in_backtrace( $names ) { |
| 923 | - $backtrace = debug_backtrace( false ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace | |
| 932 | + $backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace | |
| 924 | 933 | if ( ! is_array( $names ) ) { |
| 925 | 934 | $names = array( $names ); |
| 926 | 935 | } |
| 927 | 936 | $names_as_keys = array_flip( $names ); |