[ 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          // Enqueue scripts.
  60          add_action( 'bp_members_admin_enqueue_scripts',  array( $this, 'enqueue_scripts'    ), 10, 1 );
  61  
  62          // Register the metabox in Member's community admin profile.
  63          add_action( 'bp_members_admin_xprofile_metabox', array( $this, 'register_metaboxes' ), 10, 3 );
  64  
  65          // Saves the profile actions for user ( avatar, profile fields ).
  66          add_action( 'bp_members_admin_update_user',      array( $this, 'user_admin_load'    ), 10, 4 );
  67      }
  68  
  69      /**
  70       * Enqueue needed scripts.
  71       *
  72       * @since 2.3.0
  73       *
  74       * @param int $screen_id Screen ID being displayed.
  75       */
  76  	public function enqueue_scripts( $screen_id ) {
  77          if ( ( false === strpos( $screen_id, 'users_page_bp-profile-edit' )
  78              && false === strpos( $screen_id, 'profile_page_bp-profile-edit' ) )
  79              || bp_core_get_root_option( 'bp-disable-avatar-uploads' )
  80              || ! buddypress()->avatar->show_avatars
  81              || ! bp_attachments_is_wp_version_supported() ) {
  82              return;
  83          }
  84  
  85          /**
  86           * Get Thickbox.
  87           *
  88           * We cannot simply use add_thickbox() here as WordPress is not playing
  89           * nice with Thickbox width/height see https://core.trac.wordpress.org/ticket/17249
  90           * Using media-upload might be interesting in the future for the send to editor stuff
  91           * and we make sure the tb_window is wide enough
  92           */
  93          wp_enqueue_style ( 'thickbox' );
  94          wp_enqueue_script( 'media-upload' );
  95  
  96          // Get Avatar Uploader.
  97          bp_attachments_enqueue_scripts( 'BP_Attachment_Avatar' );
  98      }
  99  
 100      /**
 101       * Register the xProfile metabox on Community Profile admin page.
 102       *
 103       * @since 2.0.0
 104       *
 105       * @param int         $user_id       ID of the user being edited.
 106       * @param string      $screen_id     Screen ID to load the metabox in.
 107       * @param object|null $stats_metabox Context and priority for the stats metabox.
 108       */
 109  	public function register_metaboxes( $user_id = 0, $screen_id = '', $stats_metabox = null ) {
 110  
 111          // Set the screen ID if none was passed.
 112          if ( empty( $screen_id ) ) {
 113              $screen_id = buddypress()->members->admin->user_page;
 114          }
 115  
 116          // Setup a new metabox class if none was passed.
 117          if ( empty( $stats_metabox ) ) {
 118              $stats_metabox = new StdClass();
 119          }
 120  
 121          // Moving the Stats Metabox.
 122          $stats_metabox->context  = 'side';
 123          $stats_metabox->priority = 'low';
 124  
 125          // Each Group of fields will have his own metabox.
 126          $profile_args = array(
 127              'fetch_fields' => false,
 128              'user_id'      => $user_id,
 129          );
 130  
 131          if ( ! bp_is_user_spammer( $user_id ) && bp_has_profile( $profile_args ) ) {
 132  
 133              // Loop through field groups and add a metabox for each one.
 134              while ( bp_profile_groups() ) : bp_the_profile_group();
 135                  add_meta_box(
 136                      'bp_xprofile_user_admin_fields_' . sanitize_key( bp_get_the_profile_group_slug() ),
 137                      esc_html( bp_get_the_profile_group_name() ),
 138                      array( $this, 'user_admin_profile_metaboxes' ),
 139                      $screen_id,
 140                      'normal',
 141                      'core',
 142                      array( 'profile_group_id' => bp_get_the_profile_group_id() )
 143                  );
 144              endwhile;
 145  
 146  
 147          } else {
 148              // If member is already a spammer, show a generic metabox.
 149              add_meta_box(
 150                  'bp_xprofile_user_admin_empty_profile',
 151                  _x( 'User marked as a spammer', 'xprofile user-admin edit screen', 'buddypress' ),
 152                  array( $this, 'user_admin_spammer_metabox' ),
 153                  $screen_id,
 154                  'normal',
 155                  'core'
 156              );
 157          }
 158  
 159          if ( buddypress()->avatar->show_avatars ) {
 160              // Avatar Metabox.
 161              add_meta_box(
 162                  'bp_xprofile_user_admin_avatar',
 163                  _x( 'Profile Photo', 'xprofile user-admin edit screen', 'buddypress' ),
 164                  array( $this, 'user_admin_avatar_metabox' ),
 165                  $screen_id,
 166                  'side',
 167                  'low'
 168              );
 169          }
 170      }
 171  
 172      /**
 173       * Save the profile fields in Members community profile page.
 174       *
 175       * Loaded before the page is rendered, this function is processing form
 176       * requests.
 177       *
 178       * @since 2.0.0
 179       *
 180       * @param string $doaction    Action being run.
 181       * @param int    $user_id     ID for the user whose profile is being saved.
 182       * @param array  $request     Request being made.
 183       * @param string $redirect_to Where to redirect user to.
 184       */
 185  	public function user_admin_load( $doaction = '', $user_id = 0, $request = array(), $redirect_to = '' ) {
 186  
 187          // Eventually delete avatar.
 188          if ( 'delete_avatar' === $doaction ) {
 189  
 190              check_admin_referer( 'delete_avatar' );
 191  
 192              $redirect_to = remove_query_arg( '_wpnonce', $redirect_to );
 193  
 194              if ( bp_core_delete_existing_avatar( array( 'item_id' => $user_id ) ) ) {
 195                  $redirect_to = add_query_arg( 'updated', 'avatar', $redirect_to );
 196              } else {
 197                  $redirect_to = add_query_arg( 'error', 'avatar', $redirect_to );
 198              }
 199  
 200              bp_core_redirect( $redirect_to );
 201  
 202          } elseif ( isset( $_POST['field_ids'] ) ) {
 203              // Update profile fields.
 204              // Check the nonce.
 205              check_admin_referer( 'edit-bp-profile_' . $user_id );
 206  
 207              // Check we have field ID's.
 208              if ( empty( $_POST['field_ids'] ) ) {
 209                  $redirect_to = add_query_arg( 'error', '1', $redirect_to );
 210                  bp_core_redirect( $redirect_to );
 211              }
 212  
 213              /**
 214               * Unlike front-end edit-fields screens, the wp-admin/profile
 215               * displays all groups of fields on a single page, so the list of
 216               * field ids is an array gathering for each group of fields a
 217               * distinct comma separated list of ids.
 218               *
 219               * As a result, before using the wp_parse_id_list() function, we
 220               * must ensure that these ids are "merged" into a single comma
 221               * separated list.
 222               */
 223              $merge_ids = join( ',', $_POST['field_ids'] );
 224  
 225              // Explode the posted field IDs into an array so we know which fields have been submitted.
 226              $posted_field_ids = wp_parse_id_list( $merge_ids );
 227              $is_required      = array();
 228  
 229              // Loop through the posted fields formatting any datebox values then validate the field.
 230              foreach ( (array) $posted_field_ids as $field_id ) {
 231                  bp_xprofile_maybe_format_datebox_post_data( $field_id );
 232  
 233                  $is_required[ $field_id ] = xprofile_check_is_required_field( $field_id ) && ! bp_current_user_can( 'bp_moderate' );
 234                  if ( $is_required[ $field_id ] && empty( $_POST['field_' . $field_id ] ) ) {
 235                      $redirect_to = add_query_arg( 'error', '2', $redirect_to );
 236                      bp_core_redirect( $redirect_to );
 237                  }
 238              }
 239  
 240              // Set the errors var.
 241              $errors = false;
 242  
 243              // Now we've checked for required fields, let's save the values.
 244              $old_values = $new_values = array();
 245              foreach ( (array) $posted_field_ids as $field_id ) {
 246  
 247                  /*
 248                   * Certain types of fields (checkboxes, multiselects) may come
 249                   * through empty. Save them as an empty array so that they don't
 250                   * get overwritten by the default on the next edit.
 251                   */
 252                  $value = isset( $_POST['field_' . $field_id] ) ? $_POST['field_' . $field_id] : '';
 253  
 254                  $visibility_level = ! empty( $_POST['field_' . $field_id . '_visibility'] ) ? $_POST['field_' . $field_id . '_visibility'] : 'public';
 255                  /*
 256                   * Save the old and new values. They will be
 257                   * passed to the filter and used to determine
 258                   * whether an activity item should be posted.
 259                   */
 260                  $old_values[ $field_id ] = array(
 261                      'value'      => xprofile_get_field_data( $field_id, $user_id ),
 262                      'visibility' => xprofile_get_field_visibility_level( $field_id, $user_id ),
 263                  );
 264  
 265                  // Update the field data and visibility level.
 266                  xprofile_set_field_visibility_level( $field_id, $user_id, $visibility_level );
 267                  $field_updated = xprofile_set_field_data( $field_id, $user_id, $value, $is_required[ $field_id ] );
 268                  $value         = xprofile_get_field_data( $field_id, $user_id );
 269  
 270                  $new_values[ $field_id ] = array(
 271                      'value'      => $value,
 272                      'visibility' => xprofile_get_field_visibility_level( $field_id, $user_id ),
 273                  );
 274  
 275                  if ( ! $field_updated ) {
 276                      $errors = true;
 277                  } else {
 278  
 279                      /**
 280                       * Fires after the saving of each profile field, if successful.
 281                       *
 282                       * @since 1.1.0
 283                       *
 284                       * @param int    $field_id ID of the field being updated.
 285                       * @param string $value    Value that was saved to the field.
 286                       */
 287                      do_action( 'xprofile_profile_field_data_updated', $field_id, $value );
 288                  }
 289              }
 290  
 291              /**
 292               * Fires after all XProfile fields have been saved for the current profile.
 293               *
 294               * @since 1.0.0
 295               * @since 2.6.0 Added $old_values and $new_values parameters.
 296               *
 297               * @param int   $user_id          ID for the user whose profile is being saved.
 298               * @param array $posted_field_ids Array of field IDs that were edited.
 299               * @param bool  $errors           Whether or not any errors occurred.
 300               * @param array $old_values       Array of original values before update.
 301               * @param array $new_values       Array of newly saved values after update.
 302               */
 303              do_action( 'xprofile_updated_profile', $user_id, $posted_field_ids, $errors, $old_values, $new_values );
 304  
 305              // Set the feedback messages.
 306              if ( ! empty( $errors ) ) {
 307                  $redirect_to = add_query_arg( 'error',   '3', $redirect_to );
 308              } else {
 309                  $redirect_to = add_query_arg( 'updated', '1', $redirect_to );
 310              }
 311  
 312              bp_core_redirect( $redirect_to );
 313          }
 314      }
 315  
 316      /**
 317       * Render the xprofile metabox for Community Profile screen.
 318       *
 319       * @since 2.0.0
 320       *
 321       * @param WP_User|null $user The WP_User object for the user being edited.
 322       * @param array        $args Aray of arguments for metaboxes.
 323       */
 324  	public function user_admin_profile_metaboxes( $user = null, $args = array() ) {
 325  
 326          // Bail if no user ID.
 327          if ( empty( $user->ID ) ) {
 328              return;
 329          }
 330  
 331          $r = bp_parse_args( $args['args'], array(
 332              'profile_group_id' => 0,
 333              'user_id'          => $user->ID
 334          ), 'bp_xprofile_user_admin_profile_loop_args' );
 335  
 336          // We really need these args.
 337          if ( empty( $r['profile_group_id'] ) || empty( $r['user_id'] ) ) {
 338              return;
 339          }
 340  
 341          // Bail if no profile fields are available.
 342          if ( ! bp_has_profile( $r ) ) {
 343              return;
 344          }
 345  
 346          // Loop through profile groups & fields.
 347          while ( bp_profile_groups() ) : bp_the_profile_group(); ?>
 348  
 349              <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() ); ?>" />
 350  
 351              <?php if ( bp_get_the_profile_group_description() ) : ?>
 352  
 353                  <p class="description"><?php bp_the_profile_group_description(); ?></p>
 354  
 355              <?php endif; ?>
 356  
 357              <?php while ( bp_profile_fields() ) : bp_the_profile_field(); ?>
 358  
 359                  <div<?php bp_field_css_class( 'bp-profile-field' ); ?>>
 360                      <fieldset>
 361  
 362                      <?php
 363  
 364                      $field_type = bp_xprofile_create_field_type( bp_get_the_profile_field_type() );
 365                      $field_type->edit_field_html( array( 'user_id' => $r['user_id'] ) );
 366  
 367                      /**
 368                       * Fires before display of visibility form elements for profile metaboxes.
 369                       *
 370                       * @since 1.7.0
 371                       */
 372                      do_action( 'bp_custom_profile_edit_fields_pre_visibility' );
 373  
 374                      $can_change_visibility = bp_current_user_can( 'bp_xprofile_change_field_visibility' ); ?>
 375  
 376                      <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">
 377  
 378                          <?php
 379                          printf(
 380                              __( 'This field can be seen by: %s', 'buddypress' ),
 381                              '<span class="current-visibility-level">' . bp_get_the_profile_field_visibility_level_label() . '</span>'
 382                          );
 383                          ?>
 384                          </span>
 385  
 386                          <?php if ( $can_change_visibility ) : ?>
 387  
 388                              <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>
 389  
 390                          <?php endif; ?>
 391                      </p>
 392  
 393                      <?php if ( $can_change_visibility ) : ?>
 394  
 395                          <div class="field-visibility-settings" id="field-visibility-settings-<?php bp_the_profile_field_id() ?>">
 396                              <fieldset>
 397                                  <legend><?php _e( 'Who can see this field?', 'buddypress' ); ?></legend>
 398  
 399                                  <?php bp_profile_visibility_radio_buttons(); ?>
 400  
 401                              </fieldset>
 402                              <button type="button" class="button field-visibility-settings-close"><?php esc_html_e( 'Close', 'buddypress' ); ?></button>
 403                          </div>
 404  
 405                      <?php endif; ?>
 406  
 407                      <?php
 408  
 409                      /**
 410                       * Fires at end of custom profile field items on your xprofile screen tab.
 411                       *
 412                       * @since 1.1.0
 413                       */
 414                      do_action( 'bp_custom_profile_edit_fields' ); ?>
 415  
 416                      </fieldset>
 417                  </div>
 418  
 419              <?php endwhile; // End bp_profile_fields(). ?>
 420  
 421          <?php endwhile; // End bp_profile_groups.
 422      }
 423  
 424      /**
 425       * Render the fallback metabox in case a user has been marked as a spammer.
 426       *
 427       * @since 2.0.0
 428       *
 429       * @param WP_User|null $user The WP_User object for the user being edited.
 430       */
 431  	public function user_admin_spammer_metabox( $user = null ) {
 432      ?>
 433          <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>
 434      <?php
 435      }
 436  
 437      /**
 438       * Render the Avatar metabox to moderate inappropriate images.
 439       *
 440       * @since 2.0.0
 441       *
 442       * @param WP_User|null $user The WP_User object for the user being edited.
 443       */
 444  	public function user_admin_avatar_metabox( $user = null ) {
 445  
 446          if ( empty( $user->ID ) ) {
 447              return;
 448          } ?>
 449  
 450          <div class="avatar">
 451  
 452              <?php echo bp_core_fetch_avatar( array(
 453                  'item_id' => $user->ID,
 454                  'object'  => 'user',
 455                  'type'    => 'full',
 456                  'title'   => $user->display_name
 457              ) ); ?>
 458  
 459              <?php if ( bp_get_user_has_avatar( $user->ID ) ) :
 460  
 461                  $query_args = array(
 462                      'user_id' => $user->ID,
 463                      'action'  => 'delete_avatar'
 464                  );
 465  
 466                  if ( ! empty( $_REQUEST['wp_http_referer'] ) ) {
 467                      $wp_http_referer = wp_unslash( $_REQUEST['wp_http_referer'] );
 468                      $wp_http_referer = remove_query_arg( array( 'action', 'updated' ), $wp_http_referer );
 469                      $wp_http_referer = wp_validate_redirect( esc_url_raw( $wp_http_referer ) );
 470                      $query_args['wp_http_referer'] = urlencode( $wp_http_referer );
 471                  }
 472  
 473                  $community_url = add_query_arg( $query_args, buddypress()->members->admin->edit_profile_url );
 474                  $delete_link   = wp_nonce_url( $community_url, 'delete_avatar' ); ?>
 475  
 476                  <a href="<?php echo esc_url( $delete_link ); ?>" class="bp-xprofile-avatar-user-admin"><?php esc_html_e( 'Delete Profile Photo', 'buddypress' ); ?></a>
 477  
 478              <?php endif;
 479  
 480              // Load the Avatar UI templates if user avatar uploads are enabled and current WordPress version is supported.
 481              if ( ! bp_core_get_root_option( 'bp-disable-avatar-uploads' ) && bp_attachments_is_wp_version_supported() ) : ?>
 482                  <a href="#TB_inline?width=800px&height=400px&inlineId=bp-xprofile-avatar-editor" class="thickbox bp-xprofile-avatar-user-edit"><?php esc_html_e( 'Edit Profile Photo', 'buddypress' ); ?></a>
 483                  <div id="bp-xprofile-avatar-editor" style="display:none;">
 484                      <?php bp_attachments_get_template_part( 'avatars/index' ); ?>
 485                  </div>
 486              <?php endif; ?>
 487  
 488          </div>
 489          <?php
 490      }
 491  
 492  }
 493  endif; // End class_exists check.


Generated: Mon Jul 22 01:01:43 2019 Cross-referenced by PHPXref 0.7.1