Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
78.57% |
22 / 28 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| WPCOM_User_Profile_Fields_Revert | |
78.57% |
22 / 28 |
|
60.00% |
3 / 5 |
14.66 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| revert_user_data_on_wp_admin_profile_update | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
| revert_user_meta_on_wp_admin_profile_change | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| disable_send_email_change_email | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| disable_email_notification | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Manage User profile fields. |
| 4 | * |
| 5 | * @package automattic/jetpack-masterbar |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Masterbar; |
| 9 | |
| 10 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 11 | |
| 12 | /** |
| 13 | * Responsible with preventing the back-end default implementation to save the fields that are managed on WP.com profiles. |
| 14 | * |
| 15 | * Class Profile_Edit_Filter_Fields |
| 16 | * |
| 17 | * @phan-constructor-used-for-side-effects |
| 18 | */ |
| 19 | class WPCOM_User_Profile_Fields_Revert { |
| 20 | |
| 21 | /** |
| 22 | * Jetpack connection manager object. |
| 23 | * |
| 24 | * @var Connection_Manager |
| 25 | */ |
| 26 | private $connection_manager; |
| 27 | |
| 28 | /** |
| 29 | * Profile_Edit_Filter_Fields constructor. |
| 30 | * |
| 31 | * @param Connection_Manager $connection_manager The connection manager. |
| 32 | */ |
| 33 | public function __construct( Connection_Manager $connection_manager ) { |
| 34 | $this->connection_manager = $connection_manager; |
| 35 | |
| 36 | \add_filter( 'wp_pre_insert_user_data', array( $this, 'revert_user_data_on_wp_admin_profile_update' ), 10, 3 ); |
| 37 | \add_filter( 'insert_user_meta', array( $this, 'revert_user_meta_on_wp_admin_profile_change' ), 10, 3 ); |
| 38 | |
| 39 | /** |
| 40 | * Core sends two E-mail notifications that have to be disabled: |
| 41 | * - To the existing e-mail address |
| 42 | * - To the new email address |
| 43 | */ |
| 44 | \add_filter( 'send_email_change_email', array( $this, 'disable_send_email_change_email' ), 10, 3 ); |
| 45 | \add_action( 'personal_options_update', array( $this, 'disable_email_notification' ), 1, 1 ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Filter the built-in user profile fields. |
| 50 | * |
| 51 | * @param array $data { |
| 52 | * Values and keys for the user. |
| 53 | * |
| 54 | * @type string $user_login The user's login. Only included if $update == false |
| 55 | * @type string $user_pass The user's password. |
| 56 | * @type string $user_email The user's email. |
| 57 | * @type string $user_url The user's url. |
| 58 | * @type string $user_nicename The user's nice name. Defaults to a URL-safe version of user's login |
| 59 | * @type string $display_name The user's display name. |
| 60 | * @type string $user_registered MySQL timestamp describing the moment when the user registered. Defaults to |
| 61 | * the current UTC timestamp. |
| 62 | * } |
| 63 | * |
| 64 | * @param bool $update Whether the user is being updated rather than created. |
| 65 | * @param int|null $id ID of the user to be updated, or NULL if the user is being created. |
| 66 | * |
| 67 | * @return array |
| 68 | */ |
| 69 | public function revert_user_data_on_wp_admin_profile_update( $data, $update, $id ) { |
| 70 | |
| 71 | // bail if the id is null, meaning that this was triggered in the context of user create. |
| 72 | // bail if the user is not connected (e.g. non-WP.com users or disconnected users). |
| 73 | if ( ! $update || null === $id || ! $this->connection_manager->is_user_connected( $id ) ) { |
| 74 | return $data; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Revert the data in the form submission with the data from the database. |
| 79 | */ |
| 80 | $user = \get_userdata( $id ); |
| 81 | |
| 82 | /** |
| 83 | * E-mail has a different flow for changing it's value. It stores it in an option until the user confirms it via e-mail. |
| 84 | * Based on this, it displays in the UI a section mentioning the e-mail pending change. |
| 85 | * We hide the entire section, but we should also clean it up just in case. |
| 86 | */ |
| 87 | \delete_user_meta( $id, '_new_email' ); |
| 88 | |
| 89 | $data['user_email'] = $user->user_email; |
| 90 | $data['user_url'] = $user->user_url; |
| 91 | $data['user_nicename'] = $user->user_nicename; |
| 92 | $data['display_name'] = $user->display_name; |
| 93 | |
| 94 | return $data; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Revert the first_name, last_name and description since this is managed by WP.com. |
| 99 | * |
| 100 | * @param array $meta { |
| 101 | * Default meta values and keys for the user. |
| 102 | * |
| 103 | * @type string $nickname The user's nickname. Default is the user's username. |
| 104 | * @type string $first_name The user's first name. |
| 105 | * @type string $last_name The user's last name. |
| 106 | * @type string $description The user's description. |
| 107 | * @type string $rich_editing Whether to enable the rich-editor for the user. Default 'true'. |
| 108 | * @type string $syntax_highlighting Whether to enable the rich code editor for the user. Default 'true'. |
| 109 | * @type string $comment_shortcuts Whether to enable keyboard shortcuts for the user. Default 'false'. |
| 110 | * @type string $admin_color The color scheme for a user's admin screen. Default 'fresh'. |
| 111 | * @type int|bool $use_ssl Whether to force SSL on the user's admin area. 0|false if SSL |
| 112 | * is not forced. |
| 113 | * @type string $show_admin_bar_front Whether to show the admin bar on the front end for the user. |
| 114 | * Default 'true'. |
| 115 | * @type string $locale User's locale. Default empty. |
| 116 | * } |
| 117 | * @param \WP_User $user User object. |
| 118 | * @param bool $update Whether the user is being updated rather than created. |
| 119 | * |
| 120 | * @return array |
| 121 | */ |
| 122 | public function revert_user_meta_on_wp_admin_profile_change( $meta, $user, $update ) { |
| 123 | |
| 124 | // bail if not in update context. |
| 125 | if ( ! $update || ! $this->connection_manager->is_user_connected( $user->ID ) ) { |
| 126 | return $meta; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Revert the data in the form submission with the data from the database. |
| 131 | */ |
| 132 | $database_user = \get_userdata( $user->ID ); |
| 133 | |
| 134 | $meta['first_name'] = $database_user->first_name; |
| 135 | $meta['last_name'] = $database_user->last_name; |
| 136 | $meta['description'] = $database_user->description; |
| 137 | $meta['nickname'] = $database_user->nickname; |
| 138 | |
| 139 | return $meta; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Disable the e-mail notification. |
| 144 | * |
| 145 | * @param bool $send Whether to send or not the email. |
| 146 | * @param array $user User data. |
| 147 | */ |
| 148 | public function disable_send_email_change_email( $send, $user ) { |
| 149 | if ( ! isset( $user['ID'] ) || ! $this->connection_manager->is_user_connected( $user['ID'] ) ) { |
| 150 | return $send; |
| 151 | } |
| 152 | |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Disable notification on E-mail changes for Atomic WP-Admin Edit Profile. (for WP.com we use a different section for changing the E-mail). |
| 158 | * |
| 159 | * We need this because WP.org uses a custom flow for E-mail changes. |
| 160 | * |
| 161 | * @param int $user_id The id of the user that's updated. |
| 162 | */ |
| 163 | public function disable_email_notification( $user_id ) { |
| 164 | // Don't remove the notification for non-WP.com connected users. |
| 165 | if ( ! $this->connection_manager->is_user_connected( $user_id ) ) { |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | \remove_action( 'personal_options_update', 'send_confirmation_on_profile_email' ); |
| 170 | } |
| 171 | } |