Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
04/16/2015 10:11:05 PM (10 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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.