PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/buddypress/bp-settings/bp-settings-actions.php

https://bitbucket.org/Red54/dianjihun
PHP | 351 lines | 237 code | 47 blank | 67 comment | 57 complexity | 44467f71af5425402fe3f3f27e7d4b52 MD5 | raw file
  1. <?php
  2. /**
  3. * BuddyPress Settings Actions
  4. *
  5. * @todo split actions into separate screen functions
  6. * @package BuddyPress
  7. * @subpackage SettingsActions
  8. */
  9. // Exit if accessed directly
  10. if ( !defined( 'ABSPATH' ) ) exit;
  11. /**
  12. * Handles the changing and saving of user email addressos and passwords
  13. *
  14. * We do quite a bit of logic and error handling here to make sure that users
  15. * do not accidentally lock themselves out of their accounts. We also try to
  16. * provide as accurate of feedback as possible without exposing anyone else's
  17. * inforation to them.
  18. *
  19. * Special considerations are made for super admins that are able to edit any
  20. * users accounts already, without knowing their existing password.
  21. *
  22. * @global BuddyPress $bp
  23. * @return If no reason to proceed
  24. */
  25. function bp_settings_action_general() {
  26. // Bail if not a POST action
  27. if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
  28. return;
  29. // Bail if no submit action
  30. if ( ! isset( $_POST['submit'] ) )
  31. return;
  32. // Bail if not in settings
  33. if ( ! bp_is_settings_component() || ! bp_is_current_action( 'general' ) )
  34. return;
  35. // 404 if there are any additional action variables attached
  36. if ( bp_action_variables() ) {
  37. bp_do_404();
  38. return;
  39. }
  40. // Define local defaults
  41. $bp = buddypress(); // The instance
  42. $email_error = false; // invalid|blocked|taken|empty|nochange
  43. $pass_error = false; // invalid|mismatch|empty|nochange
  44. $pass_changed = false; // true if the user changes their password
  45. $email_changed = false; // true if the user changes their email
  46. $feedback_type = 'error'; // success|error
  47. $feedback = array(); // array of strings for feedback
  48. // Nonce check
  49. check_admin_referer('bp_settings_general');
  50. // Validate the user again for the current password when making a big change
  51. if ( ( is_super_admin() ) || ( !empty( $_POST['pwd'] ) && wp_check_password( $_POST['pwd'], $bp->displayed_user->userdata->user_pass, bp_displayed_user_id() ) ) ) {
  52. $update_user = get_userdata( bp_displayed_user_id() );
  53. /** Email Change Attempt ******************************************/
  54. if ( !empty( $_POST['email'] ) ) {
  55. // What is missing from the profile page vs signup - lets double check the goodies
  56. $user_email = sanitize_email( esc_html( trim( $_POST['email'] ) ) );
  57. // User is changing email address
  58. if ( $bp->displayed_user->userdata->user_email != $user_email ) {
  59. // Run some tests on the email address
  60. $email_checks = bp_core_validate_email_address( $user_email );
  61. if ( true !== $email_checks ) {
  62. if ( isset( $email_checks['invalid'] ) ) {
  63. $email_error = 'invalid';
  64. }
  65. if ( isset( $email_checks['domain_banned'] ) || isset( $email_checks['domain_not_allowed'] ) ) {
  66. $email_error = 'blocked';
  67. }
  68. if ( isset( $email_checks['in_use'] ) ) {
  69. $email_error = 'taken';
  70. }
  71. }
  72. // Yay we made it!
  73. if ( false === $email_error ) {
  74. $update_user->user_email = $user_email;
  75. $email_changed = true;
  76. }
  77. // No change
  78. } else {
  79. $email_error = false;
  80. }
  81. // Email address cannot be empty
  82. } else {
  83. $email_error = 'empty';
  84. }
  85. /** Password Change Attempt ***************************************/
  86. if ( !empty( $_POST['pass1'] ) && !empty( $_POST['pass2'] ) ) {
  87. // Password change attempt is successful
  88. if ( ( $_POST['pass1'] == $_POST['pass2'] ) && !strpos( " " . $_POST['pass1'], "\\" ) ) {
  89. $update_user->user_pass = $_POST['pass1'];
  90. $pass_changed = true;
  91. // Password change attempt was unsuccessful
  92. } else {
  93. $pass_error = 'mismatch';
  94. }
  95. // Both password fields were empty
  96. } elseif ( empty( $_POST['pass1'] ) && empty( $_POST['pass2'] ) ) {
  97. $pass_error = false;
  98. // One of the password boxes was left empty
  99. } elseif ( ( empty( $_POST['pass1'] ) && !empty( $_POST['pass2'] ) ) || ( !empty( $_POST['pass1'] ) && empty( $_POST['pass2'] ) ) ) {
  100. $pass_error = 'empty';
  101. }
  102. // The structure of the $update_user object changed in WP 3.3, but
  103. // wp_update_user() still expects the old format
  104. if ( isset( $update_user->data ) && is_object( $update_user->data ) ) {
  105. $update_user = $update_user->data;
  106. $update_user = get_object_vars( $update_user );
  107. // Unset the password field to prevent it from emptying out the
  108. // user's user_pass field in the database.
  109. // @see wp_update_user()
  110. if ( false === $pass_changed ) {
  111. unset( $update_user['user_pass'] );
  112. }
  113. }
  114. // Make sure these changes are in $bp for the current page load
  115. if ( ( false === $email_error ) && ( false === $pass_error ) && ( wp_update_user( $update_user ) ) ) {
  116. $bp->displayed_user->userdata = bp_core_get_core_userdata( bp_displayed_user_id() );
  117. }
  118. // Password Error
  119. } else {
  120. $pass_error = 'invalid';
  121. }
  122. // Email feedback
  123. switch ( $email_error ) {
  124. case 'invalid' :
  125. $feedback['email_invalid'] = __( 'That email address is invalid. Check the formatting and try again.', 'buddypress' );
  126. break;
  127. case 'blocked' :
  128. $feedback['email_blocked'] = __( 'That email address is currently unavailable for use.', 'buddypress' );
  129. break;
  130. case 'taken' :
  131. $feedback['email_taken'] = __( 'That email address is already taken.', 'buddypress' );
  132. break;
  133. case 'empty' :
  134. $feedback['email_empty'] = __( 'Email address cannot be empty.', 'buddypress' );
  135. break;
  136. case false :
  137. // No change
  138. break;
  139. }
  140. // Password feedback
  141. switch ( $pass_error ) {
  142. case 'invalid' :
  143. $feedback['pass_error'] = __( 'Your current password is invalid.', 'buddypress' );
  144. break;
  145. case 'mismatch' :
  146. $feedback['pass_mismatch'] = __( 'The new password fields did not match.', 'buddypress' );
  147. break;
  148. case 'empty' :
  149. $feedback['pass_empty'] = __( 'One of the password fields was empty.', 'buddypress' );
  150. break;
  151. case false :
  152. // No change
  153. break;
  154. }
  155. // No errors so show a simple success message
  156. if ( ( ( false === $email_error ) || ( false == $pass_error ) ) && ( ( true === $pass_changed ) || ( true === $email_changed ) ) ) {
  157. $feedback[] = __( 'Your settings have been saved.', 'buddypress' );
  158. $feedback_type = 'success';
  159. // Some kind of errors occurred
  160. } elseif ( ( ( false === $email_error ) || ( false === $pass_error ) ) && ( ( false === $pass_changed ) || ( false === $email_changed ) ) ) {
  161. if ( bp_is_my_profile() ) {
  162. $feedback['nochange'] = __( 'No changes were made to your account.', 'buddypress' );
  163. } else {
  164. $feedback['nochange'] = __( 'No changes were made to this account.', 'buddypress' );
  165. }
  166. }
  167. // Set the feedback
  168. bp_core_add_message( implode( '</p><p>', $feedback ), $feedback_type );
  169. // Execute additional code
  170. do_action( 'bp_core_general_settings_after_save' );
  171. // Redirect to prevent issues with browser back button
  172. bp_core_redirect( trailingslashit( bp_displayed_user_domain() . bp_get_settings_slug() . '/general' ) );
  173. }
  174. add_action( 'bp_actions', 'bp_settings_action_general' );
  175. /**
  176. * Handles the changing and saving of user notification settings
  177. *
  178. * @return If no reason to proceed
  179. */
  180. function bp_settings_action_notifications() {
  181. // Bail if not a POST action
  182. if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
  183. return;
  184. // Bail if no submit action
  185. if ( ! isset( $_POST['submit'] ) )
  186. return;
  187. // Bail if not in settings
  188. if ( ! bp_is_settings_component() || ! bp_is_current_action( 'notifications' ) )
  189. return false;
  190. // 404 if there are any additional action variables attached
  191. if ( bp_action_variables() ) {
  192. bp_do_404();
  193. return;
  194. }
  195. check_admin_referer( 'bp_settings_notifications' );
  196. if ( isset( $_POST['notifications'] ) ) {
  197. foreach ( (array) $_POST['notifications'] as $key => $value ) {
  198. bp_update_user_meta( (int) bp_displayed_user_id(), $key, $value );
  199. }
  200. }
  201. // Switch feedback for super admins
  202. if ( bp_is_my_profile() ) {
  203. bp_core_add_message( __( 'Your notification settings have been saved.', 'buddypress' ), 'success' );
  204. } else {
  205. bp_core_add_message( __( "This user's notification settings have been saved.", 'buddypress' ), 'success' );
  206. }
  207. do_action( 'bp_core_notification_settings_after_save' );
  208. bp_core_redirect( bp_displayed_user_domain() . bp_get_settings_slug() . '/notifications/' );
  209. }
  210. add_action( 'bp_actions', 'bp_settings_action_notifications' );
  211. /**
  212. * Handles the setting of user capabilities, spamming, hamming, role, etc...
  213. *
  214. * @return If no reason to proceed
  215. */
  216. function bp_settings_action_capabilities() {
  217. // Bail if not a POST action
  218. if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
  219. return;
  220. // Bail if no submit action
  221. if ( ! isset( $_POST['capabilities-submit'] ) )
  222. return;
  223. // Bail if not in settings
  224. if ( ! bp_is_settings_component() || ! bp_is_current_action( 'capabilities' ) )
  225. return false;
  226. // 404 if there are any additional action variables attached
  227. if ( bp_action_variables() ) {
  228. bp_do_404();
  229. return;
  230. }
  231. // Nonce check
  232. check_admin_referer( 'capabilities' );
  233. do_action( 'bp_settings_capabilities_before_save' );
  234. /** Spam **************************************************************/
  235. $is_spammer = !empty( $_POST['user-spammer'] ) ? true : false;
  236. if ( bp_is_user_spammer( bp_displayed_user_id() ) != $is_spammer ) {
  237. $status = ( true == $is_spammer ) ? 'spam' : 'ham';
  238. bp_core_process_spammer_status( bp_displayed_user_id(), $status );
  239. do_action( 'bp_core_action_set_spammer_status', bp_displayed_user_id(), $status );
  240. }
  241. /** Other *************************************************************/
  242. do_action( 'bp_settings_capabilities_after_save' );
  243. // Redirect to the root domain
  244. bp_core_redirect( bp_displayed_user_domain() . bp_get_settings_slug() . '/capabilities/' );
  245. }
  246. add_action( 'bp_actions', 'bp_settings_action_capabilities' );
  247. /**
  248. * Handles the deleting of a user
  249. *
  250. * @return If no reason to proceed
  251. */
  252. function bp_settings_action_delete_account() {
  253. // Bail if not a POST action
  254. if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
  255. return;
  256. // Bail if no submit action
  257. if ( ! isset( $_POST['delete-account-understand'] ) )
  258. return;
  259. // Bail if not in settings
  260. if ( ! bp_is_settings_component() || ! bp_is_current_action( 'delete-account' ) )
  261. return false;
  262. // 404 if there are any additional action variables attached
  263. if ( bp_action_variables() ) {
  264. bp_do_404();
  265. return;
  266. }
  267. // Nonce check
  268. check_admin_referer( 'delete-account' );
  269. // Get username now because it might be gone soon!
  270. $username = bp_get_displayed_user_fullname();
  271. // delete the users account
  272. if ( bp_core_delete_account( bp_displayed_user_id() ) ) {
  273. // Add feedback ater deleting a user
  274. bp_core_add_message( sprintf( __( '%s was successfully deleted.', 'buddypress' ), $username ), 'success' );
  275. // Redirect to the root domain
  276. bp_core_redirect( bp_get_root_domain() );
  277. }
  278. }
  279. add_action( 'bp_actions', 'bp_settings_action_delete_account' );