Skip to:
Content

BuddyPress.org

Changeset 9755


Ignore:
Timestamp:
04/16/2015 10:11:05 PM (11 years ago)
Author:
imath
Message:

Avatar UI: Add Conditional functions and new BP Attachment methods

  • bp_avatar_is_front_edit() checks if the user is editing his or a group avatar on front-end
  • bp_avatar_use_webcam() checks if the Camera feature should be enabled. By default, it is not the case for touch devices as it can introduce some confustions with the possibility of using the camera of the device to take a selfie, save it as an image an upload it using the Browse button of the Avatar UI. Chrome and Firefox are the only browsers supporting getUserMedia.

NB: these two functions can be filtered to disallow the Camera feature or to completely disallow the new Avatar UI if you prefer to carry on using the legacy UI.

BP_Attachment::script_data() and BP_Attachment_Avatar::script_data() are two new methods to build the javascript localization data. The bp_attachment_avatar_params filter is fired if the current object is not a group or a user and can be used to build the params of another object such as a blog. You can also use the filter bp_attachment_avatar_script_data if you need to override data for any object.

Props boonebgorges, DJPaul.

See #6290
See #6278

Location:
trunk/src/bp-core
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-avatars.php

    r9740 r9755  
    13741374}
    13751375add_action( 'bp_parse_query', 'bp_core_avatar_reset_query', 10, 1 );
     1376
     1377/**
     1378 * Checks whether Avatar UI should be loaded
     1379 *
     1380 * @since  BuddyPress (2.3.0)
     1381 *
     1382 * @return bool True if Avatar UI should load, false otherwise
     1383 */
     1384function bp_avatar_is_front_edit() {
     1385        $retval = false;
     1386
     1387        if ( bp_is_user_change_avatar() && 'crop-image' !== bp_get_avatar_admin_step() ) {
     1388                $retval = ! bp_core_get_root_option( 'bp-disable-avatar-uploads' );
     1389        }
     1390
     1391        if ( bp_is_active( 'groups' ) ) {
     1392                // Group creation
     1393                if ( bp_is_group_create() && bp_is_group_creation_step( 'group-avatar' ) && 'crop-image' !== bp_get_avatar_admin_step() ) {
     1394                        $retval = ! bp_core_get_root_option( 'bp-disable-avatar-uploads' );
     1395
     1396                // Group Manage
     1397                } elseif ( bp_is_group_admin_page() && bp_is_group_admin_screen( 'group-avatar' ) && 'crop-image' !== bp_get_avatar_admin_step() ) {
     1398                        $retval = ! bp_core_get_root_option( 'bp-disable-avatar-uploads' );
     1399                }
     1400        }
     1401
     1402        /**
     1403         * Use this filter if you need to :
     1404         * - Load the avatar UI for a component that is !groups or !user (return true regarding your conditions)
     1405         * - Completely disable the avatar UI introduced in 2.3 (eg: __return_false())
     1406         *
     1407         * @since  BuddyPress (2.3.0)
     1408         *
     1409         * @var  bool whether to load the Avatar UI
     1410         */
     1411        return apply_filters( 'bp_avatar_is_front_edit', $retval );
     1412}
     1413
     1414/**
     1415 * Checks whether the Webcam Avatar UI part should be loaded
     1416 *
     1417 * @since  BuddyPress (2.3.0)
     1418 *
     1419 * @return bool True to load the Webcam Avatar UI part. False otherwise.
     1420 */
     1421function bp_avatar_use_webcam() {
     1422        /**
     1423         * Do not use the webcam feature for mobile devices
     1424         * to avoid possible confusions.
     1425         */
     1426        if ( wp_is_mobile() ) {
     1427                return false;
     1428        }
     1429
     1430        /**
     1431         * Use this filter if you need to disable the webcam capture feature
     1432         * by returning false.
     1433         *
     1434         * @since  BuddyPress (2.3.0)
     1435         *
     1436         * @var  bool whether to load Webcam Avatar UI part
     1437         */
     1438        return apply_filters( 'bp_avatar_use_webcam', true );
     1439}
  • trunk/src/bp-core/classes/class-bp-attachment-avatar.php

    r9715 r9755  
    257257                return $avatar_types;
    258258        }
     259
     260        /**
     261         * Get the user id to set its avatar
     262         *
     263         * @since BuddyPress (2.3.0)
     264         *
     265         * @return integer the user ID
     266         */
     267        private function get_user_id() {
     268                $bp = buddypress();
     269                $user_id = 0;
     270
     271                if ( bp_is_user() ) {
     272                        $user_id = bp_displayed_user_id();
     273                }
     274
     275                if ( ! empty( $bp->members->admin->user_id ) ) {
     276                        $user_id = $bp->members->admin->user_id;
     277                }
     278
     279                return $user_id;
     280        }
     281
     282        /**
     283         * Get the group id to set its avatar
     284         *
     285         * @since BuddyPress (2.3.0)
     286         *
     287         * @return integer the group id
     288         */
     289        private function get_group_id() {
     290                $group_id = 0;
     291
     292                if ( bp_is_group() ) {
     293                        $group_id = bp_get_current_group_id();
     294                }
     295
     296                return $group_id;
     297        }
     298
     299        /**
     300         * Build script datas for the Uploader UI
     301         *
     302         * @since BuddyPress (2.3.0)
     303         *
     304         * @return array the javascript localization data
     305         */
     306        public function script_data() {
     307                // Get default script data
     308                $script_data = parent::script_data();
     309
     310                // Defaults to Avatar Backbone script
     311                $js_scripts = array( 'bp-avatar' );
     312
     313                // Default object
     314                $object = '';
     315
     316                // Get the possible item ids
     317                $user_id  = $this->get_user_id();
     318                $group_id = $this->get_group_id();
     319
     320                if ( ! empty( $user_id ) ) {
     321                        // Should we load the the Webcam Avatar javascript file
     322                        if ( bp_avatar_use_webcam() ) {
     323                                $js_scripts = array( 'bp-webcam' );
     324                        }
     325
     326                        $script_data['bp_params'] = array(
     327                                'object'     => 'user',
     328                                'item_id'    => $user_id,
     329                                'has_avatar' => bp_get_user_has_avatar( $user_id ),
     330                                'nonces'  => array(
     331                                        'set'    => wp_create_nonce( 'bp_avatar_cropstore' ),
     332                                        'remove' => wp_create_nonce( 'bp_delete_avatar_link' ),
     333                                ),
     334                        );
     335
     336                        // Set feedback messages
     337                        $script_data['feedback_messages'] = array(
     338                                1 => __( 'There was a problem cropping your profile photo.', 'buddypress' ),
     339                                2 => __( 'Your new profile photo was uploaded successfully.', 'buddypress' ),
     340                                3 => __( 'There was a problem deleting your profile photo. Please try again.', 'buddypress' ),
     341                                4 => __( 'Your profile photo was deleted successfully!', 'buddypress' ),
     342                        );
     343                } elseif ( ! empty( $group_id ) ) {
     344                        $script_data['bp_params'] = array(
     345                                'object'     => 'group',
     346                                'item_id'    => $group_id,
     347                                'has_avatar' => bp_get_group_has_avatar( $group_id ),
     348                                'nonces'     => array(
     349                                        'set'    => wp_create_nonce( 'bp_avatar_cropstore' ),
     350                                        'remove' => wp_create_nonce( 'bp_group_avatar_delete' ),
     351                                ),
     352                        );
     353
     354                        // Set feedback messages
     355                        $script_data['feedback_messages'] = array(
     356                                1 => __( 'There was a problem cropping the group profile photo.', 'buddypress' ),
     357                                2 => __( 'The group profile photo was uploaded successfully.', 'buddypress' ),
     358                                3 => __( 'There was a problem deleting the group profile photo. Please try again.', 'buddypress' ),
     359                                4 => __( 'The group profile photo was deleted successfully!', 'buddypress' ),
     360                        );
     361                } else {
     362                        /**
     363                         * Use this filter to include specific BuddyPress params for your object
     364                         * e.g. Blavatar
     365                         *
     366                         * @since BuddyPress (2.3.0)
     367                         *
     368                         * @param array the avatar specific BuddyPress parameters
     369                         */
     370                        $script_data['bp_params'] = apply_filters( 'bp_attachment_avatar_params', array() );
     371                }
     372
     373                // Include the specific css
     374                $script_data['extra_css'] = array( 'bp-avatar' );
     375
     376                // Include the specific css
     377                $script_data['extra_js']  = $js_scripts;
     378
     379                // Set the object to contextualize the filter
     380                if ( isset( $script_data['bp_params']['object'] ) ) {
     381                        $object = $script_data['bp_params']['object'];
     382                }
     383
     384                /**
     385                 * Use this filter to override/extend the avatar script data
     386                 *
     387                 * @since BuddyPress (2.3.0)
     388                 *
     389                 * @param array  $script_data the avatar script data
     390                 * @param string $object      the object the avatar belongs to (eg: user or group)
     391                 */
     392                return apply_filters( 'bp_attachment_avatar_script_data', $script_data, $object );
     393        }
    259394}
  • trunk/src/bp-core/classes/class-bp-attachment.php

    r9660 r9755  
    478478                return wp_crop_image( $r['original_file'], (int) $r['crop_x'], (int) $r['crop_y'], (int) $r['crop_w'], (int) $r['crop_h'], (int) $r['dst_w'], (int) $r['dst_h'], $r['src_abs'], $r['dst_file'] );
    479479        }
     480
     481        /**
     482         * Build script datas for the Uploader UI
     483         *
     484         * Override this method from your child class to build the script datas
     485         *
     486         * @since BuddyPress (2.3.0)
     487         *
     488         * @return array the javascript localization data
     489         */
     490        public function script_data() {
     491                $script_data = array(
     492                        'action'            => $this->action,
     493                        'file_data_name'    => $this->file_input,
     494                        'max_file_size'     => $this->original_max_filesize,
     495                        'feedback_messages' => array(
     496                                1 => __( 'Sorry, uploading the file failed.', 'buddypress' ),
     497                                2 => __( 'File successfully uploaded.', 'buddypress' ),
     498                        ),
     499                );
     500
     501                return $script_data;
     502        }
    480503}
Note: See TracChangeset for help on using the changeset viewer.