class-admin-user-profile.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. * @since 1.8.0
  7. */
  8. /**
  9. * Customizes user profile.
  10. */
  11. class WPSEO_Admin_User_Profile {
  12. /**
  13. * Class constructor.
  14. */
  15. public function __construct() {
  16. add_action( 'show_user_profile', [ $this, 'user_profile' ] );
  17. add_action( 'edit_user_profile', [ $this, 'user_profile' ] );
  18. add_action( 'personal_options_update', [ $this, 'process_user_option_update' ] );
  19. add_action( 'edit_user_profile_update', [ $this, 'process_user_option_update' ] );
  20. add_action( 'update_user_meta', [ $this, 'clear_author_sitemap_cache' ], 10, 3 );
  21. }
  22. /**
  23. * Clear author sitemap cache when settings are changed.
  24. *
  25. * @since 3.1
  26. *
  27. * @param int $meta_id The ID of the meta option changed.
  28. * @param int $object_id The ID of the user.
  29. * @param string $meta_key The key of the meta field changed.
  30. */
  31. public function clear_author_sitemap_cache( $meta_id, $object_id, $meta_key ) {
  32. if ( '_yoast_wpseo_profile_updated' === $meta_key ) {
  33. WPSEO_Sitemaps_Cache::clear( [ 'author' ] );
  34. }
  35. }
  36. /**
  37. * Filter POST variables.
  38. *
  39. * @param string $var_name Name of the variable to filter.
  40. *
  41. * @return mixed
  42. */
  43. private function filter_input_post( $var_name ) {
  44. $val = filter_input( INPUT_POST, $var_name );
  45. if ( $val ) {
  46. return WPSEO_Utils::sanitize_text_field( $val );
  47. }
  48. return '';
  49. }
  50. /**
  51. * Updates the user metas that (might) have been set on the user profile page.
  52. *
  53. * @param int $user_id User ID of the updated user.
  54. */
  55. public function process_user_option_update( $user_id ) {
  56. update_user_meta( $user_id, '_yoast_wpseo_profile_updated', time() );
  57. $nonce_value = $this->filter_input_post( 'wpseo_nonce' );
  58. if ( empty( $nonce_value ) ) { // Submit from alternate forms.
  59. return;
  60. }
  61. check_admin_referer( 'wpseo_user_profile_update', 'wpseo_nonce' );
  62. update_user_meta( $user_id, 'wpseo_title', $this->filter_input_post( 'wpseo_author_title' ) );
  63. update_user_meta( $user_id, 'wpseo_metadesc', $this->filter_input_post( 'wpseo_author_metadesc' ) );
  64. update_user_meta( $user_id, 'wpseo_noindex_author', $this->filter_input_post( 'wpseo_noindex_author' ) );
  65. update_user_meta( $user_id, 'wpseo_content_analysis_disable', $this->filter_input_post( 'wpseo_content_analysis_disable' ) );
  66. update_user_meta( $user_id, 'wpseo_keyword_analysis_disable', $this->filter_input_post( 'wpseo_keyword_analysis_disable' ) );
  67. }
  68. /**
  69. * Add the inputs needed for SEO values to the User Profile page.
  70. *
  71. * @param WP_User $user User instance to output for.
  72. */
  73. public function user_profile( $user ) {
  74. wp_nonce_field( 'wpseo_user_profile_update', 'wpseo_nonce' );
  75. require_once WPSEO_PATH . 'admin/views/user-profile.php';
  76. }
  77. }