/bp-xprofile/bp-xprofile-actions.php

  1. <?php 
  2. /** 
  3. * BuddyPress XProfile Actions. 
  4. * 
  5. * Action functions are exactly the same as screen functions, however they do not 
  6. * have a template screen associated with them. Usually they will send the user 
  7. * back to the default screen after execution. 
  8. * 
  9. * @package BuddyPress 
  10. * @subpackage XProfileActions 
  11. * @since 1.5.0 
  12. */ 
  13.  
  14. // Exit if accessed directly. 
  15. defined( 'ABSPATH' ) || exit; 
  16.  
  17. /** 
  18. * This function runs when an action is set for a screen: 
  19. * example.com/members/andy/profile/change-avatar/ [delete-avatar] 
  20. * 
  21. * The function will delete the active avatar for a user. 
  22. * 
  23. * @since 1.0.0 
  24. * 
  25. */ 
  26. function xprofile_action_delete_avatar() { 
  27.  
  28. if ( ! bp_is_user_change_avatar() || ! bp_is_action_variable( 'delete-avatar', 0 ) ) { 
  29. return false; 
  30.  
  31. // Check the nonce. 
  32. check_admin_referer( 'bp_delete_avatar_link' ); 
  33.  
  34. if ( ! bp_is_my_profile() && ! bp_current_user_can( 'bp_moderate' ) ) { 
  35. return false; 
  36.  
  37. if ( bp_core_delete_existing_avatar( array( 'item_id' => bp_displayed_user_id() ) ) ) { 
  38. bp_core_add_message( __( 'Your profile photo was deleted successfully!', 'buddypress' ) ); 
  39. } else { 
  40. bp_core_add_message( __( 'There was a problem deleting your profile photo. Please try again.', 'buddypress' ), 'error' ); 
  41.  
  42. bp_core_redirect( wp_get_referer() ); 
  43. add_action( 'bp_actions', 'xprofile_action_delete_avatar' ); 
  44.  
  45. /** 
  46. * Handles the saving of xprofile field visibilities. 
  47. * 
  48. * @since 1.9.0 
  49. */ 
  50. function bp_xprofile_action_settings() { 
  51.  
  52. // Bail if not a POST action. 
  53. if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { 
  54. return; 
  55.  
  56. // Bail if no submit action. 
  57. if ( ! isset( $_POST['xprofile-settings-submit'] ) ) { 
  58. return; 
  59.  
  60. // Bail if not in settings. 
  61. if ( ! bp_is_user_settings_profile() ) { 
  62. return; 
  63.  
  64. // 404 if there are any additional action variables attached 
  65. if ( bp_action_variables() ) { 
  66. bp_do_404(); 
  67. return; 
  68.  
  69. // Nonce check. 
  70. check_admin_referer( 'bp_xprofile_settings' ); 
  71.  
  72. /** 
  73. * Fires before saving xprofile field visibilities. 
  74. * 
  75. * @since 2.0.0 
  76. */ 
  77. do_action( 'bp_xprofile_settings_before_save' ); 
  78.  
  79. /** Save ******************************************************************/ 
  80.  
  81. // Only save if there are field ID's being posted. 
  82. if ( ! empty( $_POST['field_ids'] ) ) { 
  83.  
  84. // Get the POST'ed field ID's. 
  85. $posted_field_ids = explode( ', ', $_POST['field_ids'] ); 
  86.  
  87. // Backward compatibility: a bug in BP 2.0 caused only a single 
  88. // group's field IDs to be submitted. Look for values submitted 
  89. // in the POST request that may not appear in 'field_ids', and 
  90. // add them to the list of IDs to save. 
  91. foreach ( $_POST as $posted_key => $posted_value ) { 
  92. preg_match( '/^field_([0-9]+)_visibility$/', $posted_key, $matches ); 
  93. if ( ! empty( $matches[1] ) && ! in_array( $matches[1], $posted_field_ids ) ) { 
  94. $posted_field_ids[] = $matches[1]; 
  95.  
  96. // Save the visibility settings. 
  97. foreach ( $posted_field_ids as $field_id ) { 
  98.  
  99. $visibility_level = 'public'; 
  100.  
  101. if ( !empty( $_POST['field_' . $field_id . '_visibility'] ) ) { 
  102. $visibility_level = $_POST['field_' . $field_id . '_visibility']; 
  103.  
  104. xprofile_set_field_visibility_level( $field_id, bp_displayed_user_id(), $visibility_level ); 
  105.  
  106. /** Other *****************************************************************/ 
  107.  
  108. /** 
  109. * Fires after saving xprofile field visibilities. 
  110. * 
  111. * @since 2.0.0 
  112. */ 
  113. do_action( 'bp_xprofile_settings_after_save' ); 
  114.  
  115. // Redirect to the root domain. 
  116. bp_core_redirect( bp_displayed_user_domain() . bp_get_settings_slug() . '/profile' ); 
  117. add_action( 'bp_actions', 'bp_xprofile_action_settings' ); 
.