Нет описания

class-wpcom-user-profile-fields-revert.php 6.3KB

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