BP_XProfile_User_Admin

Load xProfile Profile admin area.

Description

Source

File: bp-xprofile/classes/class-bp-xprofile-user-admin.php

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

Changelog

Changelog
Version Description
BuddyPress 2.0.0 Introduced.

Methods

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.