[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-xprofile/classes/ -> class-bp-xprofile-user-admin.php (source)

   1  <?php
   2  /**
   3   * BuddyPress XProfile Admin Class.
   4   *
   5   * @package BuddyPress
   6   * @since 2.0.0
   7   */
   8  
   9  // Exit if accessed directly.
  10  defined( 'ABSPATH' ) || exit;
  11  
  12  if ( ! class_exists( 'BP_XProfile_User_Admin' ) ) :
  13  
  14  /**
  15   * Load xProfile Profile admin area.
  16   *
  17   * @since 2.0.0
  18   */
  19  class BP_XProfile_User_Admin {
  20  
  21      /**
  22       * Setup xProfile User Admin.
  23       *
  24       * @since 2.0.0
  25       *
  26       * @return BP_XProfile_User_Admin
  27       */
  28  	public static function register_xprofile_user_admin() {
  29  
  30          // Bail if not in admin.
  31          if ( ! is_admin() ) {
  32              return;
  33          }
  34  
  35          $bp = buddypress();
  36  
  37          if ( empty( $bp->profile->admin ) ) {
  38              $bp->profile->admin = new self;
  39          }
  40  
  41          return $bp->profile->admin;
  42      }
  43  
  44      /**
  45       * Constructor method.
  46       *
  47       * @since 2.0.0
  48       */
  49  	public function __construct() {
  50          $this->setup_actions();
  51      }
  52  
  53      /**
  54       * Set admin-related actions and filters.
  55       *
  56       * @since 2.0.0
  57       */
  58  	private function setup_actions() {
  59  
  60          // Register the metabox in Member's community admin profile.
  61          add_action( 'bp_members_admin_xprofile_metabox', array( $this, 'register_metaboxes' ), 10, 3 );
  62  
  63          // Saves the profile actions for user ( profile fields ).
  64          add_action( 'bp_members_admin_update_user',      array( $this, 'user_admin_load'    ), 10, 4 );
  65      }
  66  
  67      /**
  68       * Register the xProfile metabox on Community Profile admin page.
  69       *
  70       * @since 2.0.0
  71       *
  72       * @param int         $user_id       ID of the user being edited.
  73       * @param string      $screen_id     Screen ID to load the metabox in.
  74       * @param object|null $stats_metabox Context and priority for the stats metabox.
  75       */
  76  	public function register_metaboxes( $user_id = 0, $screen_id = '', $stats_metabox = null ) {
  77  
  78          // Set the screen ID if none was passed.
  79          if ( empty( $screen_id ) ) {
  80              $screen_id = buddypress()->members->admin->user_page;
  81          }
  82  
  83          // Setup a new metabox class if none was passed.
  84          if ( empty( $stats_metabox ) ) {
  85              $stats_metabox = new StdClass();
  86          }
  87  
  88          // Moving the Stats Metabox.
  89          $stats_metabox->context  = 'side';
  90          $stats_metabox->priority = 'low';
  91  
  92          // Each Group of fields will have his own metabox.
  93          $profile_args = array(
  94              'fetch_fields' => false,
  95              'user_id'      => $user_id,
  96          );
  97  
  98          if ( ! bp_is_user_spammer( $user_id ) && bp_has_profile( $profile_args ) ) {
  99  
 100              // Loop through field groups and add a metabox for each one.
 101              while ( bp_profile_groups() ) : bp_the_profile_group();
 102                  add_meta_box(
 103                      'bp_xprofile_user_admin_fields_' . sanitize_key( bp_get_the_profile_group_slug() ),
 104                      esc_html( bp_get_the_profile_group_name() ),
 105                      array( $this, 'user_admin_profile_metaboxes' ),
 106                      $screen_id,
 107                      'normal',
 108                      'core',
 109                      array( 'profile_group_id' => bp_get_the_profile_group_id() )
 110                  );
 111              endwhile;
 112  
 113  
 114          } else {
 115              // If member is already a spammer, show a generic metabox.
 116              add_meta_box(
 117                  'bp_xprofile_user_admin_empty_profile',
 118                  _x( 'User marked as a spammer', 'xprofile user-admin edit screen', 'buddypress' ),
 119                  array( $this, 'user_admin_spammer_metabox' ),
 120                  $screen_id,
 121                  'normal',
 122                  'core'
 123              );
 124          }
 125      }
 126  
 127      /**
 128       * Save the profile fields in Members community profile page.
 129       *
 130       * Loaded before the page is rendered, this function is processing form
 131       * requests.
 132       *
 133       * @since 2.0.0
 134       * @since 6.0.0 The `delete_avatar` action is now managed into BP_Members_Admin::user_admin_load().
 135       *
 136       * @param string $doaction    Action being run.
 137       * @param int    $user_id     ID for the user whose profile is being saved.
 138       * @param array  $request     Request being made.
 139       * @param string $redirect_to Where to redirect user to.
 140       */
 141  	public function user_admin_load( $doaction = '', $user_id = 0, $request = array(), $redirect_to = '' ) {
 142  
 143          // Update profile fields.
 144          if ( isset( $_POST['field_ids'] ) ) {
 145  
 146              // Check the nonce.
 147              check_admin_referer( 'edit-bp-profile_' . $user_id );
 148  
 149              // Check we have field ID's.
 150              if ( empty( $_POST['field_ids'] ) ) {
 151                  $redirect_to = add_query_arg( 'error', '1', $redirect_to );
 152                  bp_core_redirect( $redirect_to );
 153              }
 154  
 155              /**
 156               * Unlike front-end edit-fields screens, the wp-admin/profile
 157               * displays all groups of fields on a single page, so the list of
 158               * field ids is an array gathering for each group of fields a
 159               * distinct comma separated list of ids.
 160               *
 161               * As a result, before using the wp_parse_id_list() function, we
 162               * must ensure that these ids are "merged" into a single comma
 163               * separated list.
 164               */
 165              $merge_ids = join( ',', $_POST['field_ids'] );
 166  
 167              // Explode the posted field IDs into an array so we know which fields have been submitted.
 168              $posted_field_ids = wp_parse_id_list( $merge_ids );
 169              $is_required      = array();
 170  
 171              // Loop through the posted fields formatting any datebox values then validate the field.
 172              foreach ( (array) $posted_field_ids as $field_id ) {
 173                  bp_xprofile_maybe_format_datebox_post_data( $field_id );
 174  
 175                  $is_required[ $field_id ] = xprofile_check_is_required_field( $field_id ) && ! bp_current_user_can( 'bp_moderate' );
 176                  if ( $is_required[ $field_id ] && empty( $_POST['field_' . $field_id ] ) ) {
 177                      $redirect_to = add_query_arg( 'error', '2', $redirect_to );
 178                      bp_core_redirect( $redirect_to );
 179                  }
 180              }
 181  
 182              // Set the errors var.
 183              $errors = false;
 184  
 185              // Now we've checked for required fields, let's save the values.
 186              $old_values = $new_values = array();
 187              foreach ( (array) $posted_field_ids as $field_id ) {
 188  
 189                  /*
 190                   * Certain types of fields (checkboxes, multiselects) may come
 191                   * through empty. Save them as an empty array so that they don't
 192                   * get overwritten by the default on the next edit.
 193                   */
 194                  $value = isset( $_POST['field_' . $field_id] ) ? $_POST['field_' . $field_id] : '';
 195  
 196                  $visibility_level = ! empty( $_POST['field_' . $field_id . '_visibility'] ) ? $_POST['field_' . $field_id . '_visibility'] : 'public';
 197                  /*
 198                   * Save the old and new values. They will be
 199                   * passed to the filter and used to determine
 200                   * whether an activity item should be posted.
 201                   */
 202                  $old_values[ $field_id ] = array(
 203                      'value'      => xprofile_get_field_data( $field_id, $user_id ),
 204                      'visibility' => xprofile_get_field_visibility_level( $field_id, $user_id ),
 205                  );
 206  
 207                  // Update the field data and visibility level.
 208                  xprofile_set_field_visibility_level( $field_id, $user_id, $visibility_level );
 209                  $field_updated = xprofile_set_field_data( $field_id, $user_id, $value, $is_required[ $field_id ] );
 210                  $value         = xprofile_get_field_data( $field_id, $user_id );
 211  
 212                  $new_values[ $field_id ] = array(
 213                      'value'      => $value,
 214                      'visibility' => xprofile_get_field_visibility_level( $field_id, $user_id ),
 215                  );
 216  
 217                  if ( ! $field_updated ) {
 218                      $errors = true;
 219                  } else {
 220  
 221                      /**
 222                       * Fires after the saving of each profile field, if successful.
 223                       *
 224                       * @since 1.1.0
 225                       *
 226                       * @param int    $field_id ID of the field being updated.
 227                       * @param string $value    Value that was saved to the field.
 228                       */
 229                      do_action( 'xprofile_profile_field_data_updated', $field_id, $value );
 230                  }
 231              }
 232  
 233              /**
 234               * Fires after all XProfile fields have been saved for the current profile.
 235               *
 236               * @since 1.0.0
 237               * @since 2.6.0 Added $old_values and $new_values parameters.
 238               *
 239               * @param int   $user_id          ID for the user whose profile is being saved.
 240               * @param array $posted_field_ids Array of field IDs that were edited.
 241               * @param bool  $errors           Whether or not any errors occurred.
 242               * @param array $old_values       Array of original values before update.
 243               * @param array $new_values       Array of newly saved values after update.
 244               */
 245              do_action( 'xprofile_updated_profile', $user_id, $posted_field_ids, $errors, $old_values, $new_values );
 246  
 247              // Set the feedback messages.
 248              if ( ! empty( $errors ) ) {
 249                  $redirect_to = add_query_arg( 'error',   '3', $redirect_to );
 250              } else {
 251                  $redirect_to = add_query_arg( 'updated', '1', $redirect_to );
 252              }
 253  
 254              bp_core_redirect( $redirect_to );
 255          }
 256      }
 257  
 258      /**
 259       * Render the xprofile metabox for Community Profile screen.
 260       *
 261       * @since 2.0.0
 262       *
 263       * @param WP_User|null $user The WP_User object for the user being edited.
 264       * @param array        $args Aray of arguments for metaboxes.
 265       */
 266  	public function user_admin_profile_metaboxes( $user = null, $args = array() ) {
 267  
 268          // Bail if no user ID.
 269          if ( empty( $user->ID ) ) {
 270              return;
 271          }
 272  
 273          $r = bp_parse_args(
 274              $args['args'],
 275              array(
 276                  'profile_group_id'       => 0,
 277                  'user_id'                => $user->ID,
 278                  'hide_field_types'       => array( 'wp-textbox', 'wp-biography' ),
 279                  'fetch_visibility_level' => bp_current_user_can( 'bp_moderate' ) || (int) get_current_user_id() === (int) $user->ID,
 280              ),
 281              'bp_xprofile_user_admin_profile_loop_args'
 282          );
 283  
 284          // We really need these args.
 285          if ( empty( $r['profile_group_id'] ) || empty( $r['user_id'] ) ) {
 286              return;
 287          }
 288  
 289          // Bail if no profile fields are available.
 290          if ( ! bp_has_profile( $r ) ) {
 291              return;
 292          }
 293  
 294          // Loop through profile groups & fields.
 295          while ( bp_profile_groups() ) : bp_the_profile_group(); ?>
 296  
 297              <input type="hidden" name="field_ids[]" id="<?php echo esc_attr( 'field_ids_' . bp_get_the_profile_group_slug() ); ?>" value="<?php echo esc_attr( bp_get_the_profile_group_field_ids() ); ?>" />
 298  
 299              <?php if ( bp_get_the_profile_group_description() ) : ?>
 300  
 301                  <p class="description"><?php bp_the_profile_group_description(); ?></p>
 302  
 303              <?php endif; ?>
 304  
 305              <?php while ( bp_profile_fields() ) : bp_the_profile_field(); ?>
 306  
 307                  <div<?php bp_field_css_class( 'bp-profile-field' ); ?>>
 308                      <fieldset>
 309  
 310                      <?php
 311  
 312                      $field_type = bp_xprofile_create_field_type( bp_get_the_profile_field_type() );
 313                      $field_type->edit_field_html( array( 'user_id' => $r['user_id'] ) );
 314  
 315                      /**
 316                       * Fires before display of visibility form elements for profile metaboxes.
 317                       *
 318                       * @since 1.7.0
 319                       */
 320                      do_action( 'bp_custom_profile_edit_fields_pre_visibility' );
 321  
 322                      $can_change_visibility = bp_current_user_can( 'bp_xprofile_change_field_visibility' ); ?>
 323  
 324                      <p class="field-visibility-settings-<?php echo $can_change_visibility ? 'toggle' : 'notoggle'; ?>" id="field-visibility-settings-toggle-<?php bp_the_profile_field_id(); ?>"><span id="<?php bp_the_profile_field_input_name(); ?>-2">
 325  
 326                          <?php
 327                          printf(
 328                              __( 'This field can be seen by: %s', 'buddypress' ),
 329                              '<span class="current-visibility-level">' . bp_get_the_profile_field_visibility_level_label() . '</span>'
 330                          );
 331                          ?>
 332                          </span>
 333  
 334                          <?php if ( $can_change_visibility ) : ?>
 335  
 336                              <button type="button" class="button visibility-toggle-link" aria-describedby="<?php bp_the_profile_field_input_name(); ?>-2" aria-expanded="false"><?php esc_html_e( 'Change', 'buddypress' ); ?></button>
 337  
 338                          <?php endif; ?>
 339                      </p>
 340  
 341                      <?php if ( $can_change_visibility ) : ?>
 342  
 343                          <div class="field-visibility-settings" id="field-visibility-settings-<?php bp_the_profile_field_id() ?>">
 344                              <fieldset>
 345                                  <legend><?php _e( 'Who can see this field?', 'buddypress' ); ?></legend>
 346  
 347                                  <?php bp_profile_visibility_radio_buttons(); ?>
 348  
 349                              </fieldset>
 350                              <button type="button" class="button field-visibility-settings-close"><?php esc_html_e( 'Close', 'buddypress' ); ?></button>
 351                          </div>
 352  
 353                      <?php endif; ?>
 354  
 355                      <?php
 356  
 357                      /**
 358                       * Fires at end of custom profile field items on your xprofile screen tab.
 359                       *
 360                       * @since 1.1.0
 361                       */
 362                      do_action( 'bp_custom_profile_edit_fields' ); ?>
 363  
 364                      </fieldset>
 365                  </div>
 366  
 367              <?php endwhile; // End bp_profile_fields(). ?>
 368  
 369          <?php endwhile; // End bp_profile_groups.
 370      }
 371  
 372      /**
 373       * Render the fallback metabox in case a user has been marked as a spammer.
 374       *
 375       * @since 2.0.0
 376       *
 377       * @param WP_User|null $user The WP_User object for the user being edited.
 378       */
 379  	public function user_admin_spammer_metabox( $user = null ) {
 380      ?>
 381          <p><?php printf( __( '%s has been marked as a spammer. All BuddyPress data associated with the user has been removed', 'buddypress' ), esc_html( bp_core_get_user_displayname( $user->ID ) ) ) ;?></p>
 382      <?php
 383      }
 384  
 385  }
 386  endif; // End class_exists check.


Generated: Sat Apr 27 01:00:55 2024 Cross-referenced by PHPXref 0.7.1