[ 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( $args['args'], array(
 274              'profile_group_id' => 0,
 275              'user_id'          => $user->ID
 276          ), 'bp_xprofile_user_admin_profile_loop_args' );
 277  
 278          // We really need these args.
 279          if ( empty( $r['profile_group_id'] ) || empty( $r['user_id'] ) ) {
 280              return;
 281          }
 282  
 283          // Bail if no profile fields are available.
 284          if ( ! bp_has_profile( $r ) ) {
 285              return;
 286          }
 287  
 288          // Loop through profile groups & fields.
 289          while ( bp_profile_groups() ) : bp_the_profile_group(); ?>
 290  
 291              <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() ); ?>" />
 292  
 293              <?php if ( bp_get_the_profile_group_description() ) : ?>
 294  
 295                  <p class="description"><?php bp_the_profile_group_description(); ?></p>
 296  
 297              <?php endif; ?>
 298  
 299              <?php while ( bp_profile_fields() ) : bp_the_profile_field(); ?>
 300  
 301                  <div<?php bp_field_css_class( 'bp-profile-field' ); ?>>
 302                      <fieldset>
 303  
 304                      <?php
 305  
 306                      $field_type = bp_xprofile_create_field_type( bp_get_the_profile_field_type() );
 307                      $field_type->edit_field_html( array( 'user_id' => $r['user_id'] ) );
 308  
 309                      /**
 310                       * Fires before display of visibility form elements for profile metaboxes.
 311                       *
 312                       * @since 1.7.0
 313                       */
 314                      do_action( 'bp_custom_profile_edit_fields_pre_visibility' );
 315  
 316                      $can_change_visibility = bp_current_user_can( 'bp_xprofile_change_field_visibility' ); ?>
 317  
 318                      <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">
 319  
 320                          <?php
 321                          printf(
 322                              __( 'This field can be seen by: %s', 'buddypress' ),
 323                              '<span class="current-visibility-level">' . bp_get_the_profile_field_visibility_level_label() . '</span>'
 324                          );
 325                          ?>
 326                          </span>
 327  
 328                          <?php if ( $can_change_visibility ) : ?>
 329  
 330                              <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>
 331  
 332                          <?php endif; ?>
 333                      </p>
 334  
 335                      <?php if ( $can_change_visibility ) : ?>
 336  
 337                          <div class="field-visibility-settings" id="field-visibility-settings-<?php bp_the_profile_field_id() ?>">
 338                              <fieldset>
 339                                  <legend><?php _e( 'Who can see this field?', 'buddypress' ); ?></legend>
 340  
 341                                  <?php bp_profile_visibility_radio_buttons(); ?>
 342  
 343                              </fieldset>
 344                              <button type="button" class="button field-visibility-settings-close"><?php esc_html_e( 'Close', 'buddypress' ); ?></button>
 345                          </div>
 346  
 347                      <?php endif; ?>
 348  
 349                      <?php
 350  
 351                      /**
 352                       * Fires at end of custom profile field items on your xprofile screen tab.
 353                       *
 354                       * @since 1.1.0
 355                       */
 356                      do_action( 'bp_custom_profile_edit_fields' ); ?>
 357  
 358                      </fieldset>
 359                  </div>
 360  
 361              <?php endwhile; // End bp_profile_fields(). ?>
 362  
 363          <?php endwhile; // End bp_profile_groups.
 364      }
 365  
 366      /**
 367       * Render the fallback metabox in case a user has been marked as a spammer.
 368       *
 369       * @since 2.0.0
 370       *
 371       * @param WP_User|null $user The WP_User object for the user being edited.
 372       */
 373  	public function user_admin_spammer_metabox( $user = null ) {
 374      ?>
 375          <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>
 376      <?php
 377      }
 378  
 379  }
 380  endif; // End class_exists check.


Generated: Wed Aug 12 01:01:32 2020 Cross-referenced by PHPXref 0.7.1