[ 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          // Loop through the posted fields formatting any datebox values then validate the field.
  54          foreach ( (array) $posted_field_ids as $field_id ) {
  55              bp_xprofile_maybe_format_datebox_post_data( $field_id );
  56  
  57              $is_required[ $field_id ] = xprofile_check_is_required_field( $field_id ) && ! bp_current_user_can( 'bp_moderate' );
  58              if ( $is_required[$field_id] && empty( $_POST['field_' . $field_id] ) ) {
  59                  $errors = true;
  60              }
  61          }
  62  
  63          // There are errors.
  64          if ( !empty( $errors ) ) {
  65              bp_core_add_message( __( 'Your changes have not been saved. Please fill in all required fields, and save your changes again.', 'buddypress' ), 'error' );
  66  
  67          // No errors.
  68          } else {
  69  
  70              // Reset the errors var.
  71              $errors = false;
  72  
  73              // Now we've checked for required fields, lets save the values.
  74              $old_values = $new_values = array();
  75              foreach ( (array) $posted_field_ids as $field_id ) {
  76  
  77                  // 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.
  78                  $value = isset( $_POST['field_' . $field_id] ) ? $_POST['field_' . $field_id] : '';
  79  
  80                  $visibility_level = !empty( $_POST['field_' . $field_id . '_visibility'] ) ? $_POST['field_' . $field_id . '_visibility'] : 'public';
  81  
  82                  // Save the old and new values. They will be
  83                  // passed to the filter and used to determine
  84                  // whether an activity item should be posted.
  85                  $old_values[ $field_id ] = array(
  86                      'value'      => xprofile_get_field_data( $field_id, bp_displayed_user_id() ),
  87                      'visibility' => xprofile_get_field_visibility_level( $field_id, bp_displayed_user_id() ),
  88                  );
  89  
  90                  // Update the field data and visibility level.
  91                  xprofile_set_field_visibility_level( $field_id, bp_displayed_user_id(), $visibility_level );
  92                  $field_updated = xprofile_set_field_data( $field_id, bp_displayed_user_id(), $value, $is_required[ $field_id ] );
  93                  $value         = xprofile_get_field_data( $field_id, bp_displayed_user_id() );
  94  
  95                  $new_values[ $field_id ] = array(
  96                      'value'      => $value,
  97                      'visibility' => xprofile_get_field_visibility_level( $field_id, bp_displayed_user_id() ),
  98                  );
  99  
 100                  if ( ! $field_updated ) {
 101                      $errors = true;
 102                  } else {
 103  
 104                      /**
 105                       * Fires on each iteration of an XProfile field being saved with no error.
 106                       *
 107                       * @since 1.1.0
 108                       *
 109                       * @param int    $field_id ID of the field that was saved.
 110                       * @param string $value    Value that was saved to the field.
 111                       */
 112                      do_action( 'xprofile_profile_field_data_updated', $field_id, $value );
 113                  }
 114              }
 115  
 116              /**
 117               * Fires after all XProfile fields have been saved for the current profile.
 118               *
 119               * @since 1.0.0
 120               *
 121               * @param int   $value            Displayed user ID.
 122               * @param array $posted_field_ids Array of field IDs that were edited.
 123               * @param bool  $errors           Whether or not any errors occurred.
 124               * @param array $old_values       Array of original values before updated.
 125               * @param array $new_values       Array of newly saved values after update.
 126               */
 127              do_action( 'xprofile_updated_profile', bp_displayed_user_id(), $posted_field_ids, $errors, $old_values, $new_values );
 128  
 129              // Set the feedback messages.
 130              if ( !empty( $errors ) ) {
 131                  bp_core_add_message( __( 'There was a problem updating some of your profile information. Please try again.', 'buddypress' ), 'error' );
 132              } else {
 133                  bp_core_add_message( __( 'Changes saved.', 'buddypress' ) );
 134              }
 135  
 136              // Redirect back to the edit screen to display the updates and message.
 137              bp_core_redirect( trailingslashit( bp_displayed_user_domain() . bp_get_profile_slug() . '/edit/group/' . bp_action_variable( 1 ) ) );
 138          }
 139      }
 140  
 141      /**
 142       * Fires right before the loading of the XProfile edit screen template file.
 143       *
 144       * @since 1.0.0
 145       */
 146      do_action( 'xprofile_screen_edit_profile' );
 147  
 148      /**
 149       * Filters the template to load for the XProfile edit screen.
 150       *
 151       * @since 1.0.0
 152       *
 153       * @param string $template Path to the XProfile edit template to load.
 154       */
 155      bp_core_load_template( apply_filters( 'xprofile_template_edit_profile', 'members/single/home' ) );
 156  }


Generated: Thu Dec 12 01:01:36 2019 Cross-referenced by PHPXref 0.7.1