Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. The admin updates the user option page:
  2.  
  3. -> edit_user_profile_update or personal_options_update hooks activated
  4. -> edit_user() function is called
  5. -> wp_update_user() function is called within edit_user()
  6. -> wp_insert_user() function is called within wp_update_user()
  7. -> profile_update hook activated within wp_insert_user()
  8. for user updates, not user inserts
  9. -> wp_redirect() function called on successful user updates
  10. -> wp_redirect filter activated
  11. -> The page reloads
  12. -> admin_notices hook activated
  13.  
  14. add_filter( 'show_password_fields', 'wpse_notification_html' );
  15.  
  16. function wpse_notification_html( $show )
  17. {
  18. if( current_user_can( 'manage_options' ) ):
  19. ?>
  20. <tr>
  21. <th scope="row">
  22. <label for="wpse_send_notification"><?php _e('Send a notification?') ?></label>
  23. </th>
  24. <td>
  25. <label for="wpse_send_notification">
  26. <input type="checkbox" name="wpse_send_notification" id="wpse_send_notification" value="1" />
  27. <?php _e( 'Send an email to user and notify that the password has changed.' ); ?>
  28. </label>
  29. </td>
  30. </tr>
  31. <?php
  32. endif;
  33. return $show;
  34. }
  35.  
  36. add_action( 'edit_user_profile_update', 'wpse_user_update' );
  37. add_action( 'personal_options_update', 'wpse_user_update' );
  38.  
  39. function wpse_user_update( $user_id )
  40. {
  41. if( current_user_can( 'manage_options' ) )
  42. add_action( 'profile_update', 'wpse_controller', 10, 2 );
  43. }
  44.  
  45. function wpse_controller( $user_id, $old_user_data )
  46. {
  47. // Input:
  48. $pass1 = filter_input( INPUT_POST, 'pass1' );
  49. $pass2 = filter_input( INPUT_POST, 'pass2' );
  50. $send = filter_input( INPUT_POST, 'wpse_send_notification', FILTER_SANITIZE_NUMBER_INT );
  51.  
  52. // Run this action only once:
  53. remove_action( current_action(), __FUNCTION__ );
  54.  
  55. // Send the notification:
  56. if( 1 == $send )
  57. {
  58. if( ! empty( $pass1 )
  59. && $pass1 === $pass2
  60. && sanitize_text_field( $pass1 ) === $pass1
  61. ):
  62. if( wpse_user_password_notification( $user_id, wp_unslash( sanitize_text_field( $pass1 ) ) ) )
  63. add_filter( 'wp_redirect', 'wpse_redirect_notification_success' );
  64. else
  65. add_filter( 'wp_redirect', 'wpse_redirect_notification_error' );
  66. else:
  67. add_filter( 'wp_redirect', 'wpse_redirect_pass_validation_error' );
  68. endif;
  69. }
  70. }
  71.  
  72. function wpse_redirect_notification_success( $location )
  73. {
  74. return add_query_arg( 'wpse_notification', 'mail_success', $location );
  75. }
  76.  
  77. function wpse_redirect_notification_error( $location )
  78. {
  79. return add_query_arg( 'wpse_notification', 'mail_error', $location );
  80. }
  81.  
  82. function wpse_redirect_pass_validation_error( $location )
  83. {
  84. return add_query_arg( 'wpse_notification', 'pass_validation_error', $location );
  85. }
  86.  
  87. function wpse_user_password_notification( $user_id, $plaintext_pass = '' )
  88. {
  89. if ( empty( $plaintext_pass ) )
  90. return false;
  91.  
  92. $user = get_userdata( $user_id );
  93. $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
  94. $message = sprintf( __( 'Username: %s' ), $user->user_login ) . "rn";
  95. $message .= sprintf( __( 'New Password: %s' ), $plaintext_pass ) . "rn";
  96. $message .= wp_login_url() . "rn";
  97. return wp_mail( $user->user_email, sprintf(__('[%s] Your username and new password'), $blogname), $message );
  98. }
  99.  
  100. add_action( 'admin_notices', 'wpse_admin_notices' );
  101.  
  102. function wpse_admin_notices()
  103. {
  104. $status = filter_input( INPUT_GET, 'wpse_notification', FILTER_SANITIZE_STRING );
  105.  
  106. switch ( $status )
  107. {
  108. case 'mail_success':
  109. ?><div id="message" class="updated"><p><strong>Notification Sent!</strong>: Notification email successfully sent to the user</p></div><?php
  110. break;
  111. case 'mail_error':
  112. ?><div class="error"><p><strong>ERROR</strong>: Notification email not sent to the user</p></div><?php
  113. break;
  114. case 'pass_validation_error':
  115. ?><div class="error"><p><strong>ERROR</strong>: Notification email not sent to the user, because of symbol chars in the password </p></div><?php
  116. break;
  117. } // end switch
  118. }
  119.  
  120. $pass_change_email = apply_filters( 'password_change_email', $pass_change_email, $user, $userdata );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement