[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-xprofile/screens/ -> edit.php (source)

   1  <?php
   2  /**
   3   * XProfile: User's "Profile > Edit" screen handler
   4   *
   5   * @package BuddyPress
   6   * @subpackage XProfileScreens
   7   * @since 3.0.0
   8   */
   9  
  10  /**
  11   * Handles the display of the profile edit page by loading the correct template file.
  12   * Also checks to make sure this can only be accessed for the logged in users profile.
  13   *
  14   * @since 1.0.0
  15   *
  16   */
  17  function xprofile_screen_edit_profile() {
  18  
  19      if ( ! bp_is_my_profile() && ! bp_current_user_can( 'bp_moderate' ) ) {
  20          return false;
  21      }
  22  
  23      // Make sure a group is set.
  24      if ( ! bp_action_variable( 1 ) ) {
  25          bp_core_redirect( trailingslashit( bp_displayed_user_domain() . bp_get_profile_slug() . '/edit/group/1' ) );
  26      }
  27  
  28      // Check the field group exists.
  29      if ( ! bp_is_action_variable( 'group' ) || ! xprofile_get_field_group( bp_action_variable( 1 ) ) ) {
  30          bp_do_404();
  31          return;
  32      }
  33  
  34      // No errors.
  35      $errors = false;
  36  
  37      // Check to see if any new information has been submitted.
  38      if ( isset( $_POST['field_ids'] ) ) {
  39  
  40          // Check the nonce.
  41          check_admin_referer( 'bp_xprofile_edit' );
  42  
  43          // Check we have field ID's.
  44          if ( empty( $_POST['field_ids'] ) ) {
  45              bp_core_redirect( trailingslashit( bp_displayed_user_domain() . bp_get_profile_slug() . '/edit/group/' . bp_action_variable( 1 ) ) );
  46          }
  47  
  48          // Explode the posted field IDs into an array so we know which
  49          // fields have been submitted.
  50          $posted_field_ids = wp_parse_id_list( $_POST['field_ids'] );
  51          $is_required      = array();
  52  
  53          $bp_displayed_user = bp_get_displayed_user();
  54          $bp_displayed_user->updated_keys = array();
  55  
  56          // Loop through the posted fields formatting any datebox values then validate the field.
  57          foreach ( (array) $posted_field_ids as $field_id ) {
  58              bp_xprofile_maybe_format_datebox_post_data( $field_id );
  59  
  60              $is_required[ $field_id ] = xprofile_check_is_required_field( $field_id ) && ! bp_current_user_can( 'bp_moderate' );
  61              if ( $is_required[$field_id] && empty( $_POST['field_' . $field_id] ) ) {
  62                  $errors = true;
  63              }
  64          }
  65  
  66          // There are errors.
  67          if ( !empty( $errors ) ) {
  68              bp_core_add_message( __( 'Your changes have not been saved. Please fill in all required fields, and save your changes again.', 'buddypress' ), 'error' );
  69  
  70          // No errors.
  71          } else {
  72  
  73              // Reset the errors var.
  74              $errors = false;
  75  
  76              // Now we've checked for required fields, lets save the values.
  77              $old_values = $new_values = array();
  78              foreach ( (array) $posted_field_ids as $field_id ) {
  79  
  80                  // Certain types of fields (checkboxes, multiselects) may come through empty. Save them as an empty array so that they don't get overwritten by the default on the next edit.
  81                  $value = isset( $_POST['field_' . $field_id] ) ? $_POST['field_' . $field_id] : '';
  82  
  83                  $visibility_level = !empty( $_POST['field_' . $field_id . '_visibility'] ) ? $_POST['field_' . $field_id . '_visibility'] : 'public';
  84  
  85                  // Save the old and new values. They will be
  86                  // passed to the filter and used to determine
  87                  // whether an activity item should be posted.
  88                  $old_values[ $field_id ] = array(
  89                      'value'      => xprofile_get_field_data( $field_id, bp_displayed_user_id() ),
  90                      'visibility' => xprofile_get_field_visibility_level( $field_id, bp_displayed_user_id() ),
  91                  );
  92  
  93                  // Update the field data and visibility level.
  94                  xprofile_set_field_visibility_level( $field_id, bp_displayed_user_id(), $visibility_level );
  95                  $field_updated = xprofile_set_field_data( $field_id, bp_displayed_user_id(), $value, $is_required[ $field_id ] );
  96                  $value         = xprofile_get_field_data( $field_id, bp_displayed_user_id() );
  97  
  98                  $new_values[ $field_id ] = array(
  99                      'value'      => $value,
 100                      'visibility' => xprofile_get_field_visibility_level( $field_id, bp_displayed_user_id() ),
 101                  );
 102  
 103                  if ( ! $field_updated ) {
 104                      $errors = true;
 105                  } else {
 106  
 107                      /**
 108                       * Fires on each iteration of an XProfile field being saved with no error.
 109                       *
 110                       * @since 1.1.0
 111                       *
 112                       * @param int    $field_id ID of the field that was saved.
 113                       * @param string $value    Value that was saved to the field.
 114                       */
 115                      do_action( 'xprofile_profile_field_data_updated', $field_id, $value );
 116                  }
 117              }
 118  
 119              /**
 120               * Fires after all XProfile fields have been saved for the current profile.
 121               *
 122               * @since 1.0.0
 123               *
 124               * @param int   $value            Displayed user ID.
 125               * @param array $posted_field_ids Array of field IDs that were edited.
 126               * @param bool  $errors           Whether or not any errors occurred.
 127               * @param array $old_values       Array of original values before updated.
 128               * @param array $new_values       Array of newly saved values after update.
 129               */
 130              do_action( 'xprofile_updated_profile', bp_displayed_user_id(), $posted_field_ids, $errors, $old_values, $new_values );
 131  
 132              // Some WP User keys have been updated: let's update the WP fiels all together.
 133              if ( $bp_displayed_user->updated_keys ) {
 134                  $user_id = wp_update_user(
 135                      array_merge(
 136                          array(
 137                              'ID' => bp_displayed_user_id(),
 138                          ),
 139                          $bp_displayed_user->updated_keys
 140                      )
 141                  );
 142  
 143                  $bp_displayed_user->updated_keys = array();
 144  
 145                  if ( is_wp_error( $user_id ) ) {
 146                      $errors = true;
 147                  }
 148              }
 149  
 150              // Set the feedback messages.
 151              if ( !empty( $errors ) ) {
 152                  bp_core_add_message( __( 'There was a problem updating some of your profile information. Please try again.', 'buddypress' ), 'error' );
 153              } else {
 154                  bp_core_add_message( __( 'Changes saved.', 'buddypress' ) );
 155              }
 156  
 157              // Redirect back to the edit screen to display the updates and message.
 158              bp_core_redirect( trailingslashit( bp_displayed_user_domain() . bp_get_profile_slug() . '/edit/group/' . bp_action_variable( 1 ) ) );
 159          }
 160      }
 161  
 162      /**
 163       * Fires right before the loading of the XProfile edit screen template file.
 164       *
 165       * @since 1.0.0
 166       */
 167      do_action( 'xprofile_screen_edit_profile' );
 168  
 169      /**
 170       * Filters the template to load for the XProfile edit screen.
 171       *
 172       * @since 1.0.0
 173       *
 174       * @param string $template Path to the XProfile edit template to load.
 175       */
 176      bp_core_load_template( apply_filters( 'xprofile_template_edit_profile', 'members/single/home' ) );
 177  }


Generated: Fri Nov 22 01:00:56 2024 Cross-referenced by PHPXref 0.7.1