bp_attachments_enqueue_scripts( string $class = '' )
Enqueues the script needed for the Uploader UI.
Description
See also
- BP_Attachment::script_data(): && BP_Attachment_Avatar::script_data() for examples showing how to set specific script data.
Parameters
- $class
-
(Optional) Name of the class extending BP_Attachment (eg: BP_Attachment_Avatar).
Default value: ''
Return
(null|WP_Error)
Source
File: bp-core/bp-core-attachments.php
function bp_attachments_enqueue_scripts( $class = '' ) {
// Enqueue me just once per page, please.
if ( did_action( 'bp_attachments_enqueue_scripts' ) ) {
return;
}
if ( ! $class || ! class_exists( $class ) ) {
return new WP_Error( 'missing_parameter' );
}
// Get an instance of the class and get the script data.
$attachment = new $class;
$script_data = $attachment->script_data();
$args = bp_parse_args( $script_data, array(
'action' => '',
'file_data_name' => '',
'max_file_size' => 0,
'browse_button' => 'bp-browse-button',
'container' => 'bp-upload-ui',
'drop_element' => 'drag-drop-area',
'bp_params' => array(),
'extra_css' => array(),
'extra_js' => array(),
'feedback_messages' => array(),
), 'attachments_enqueue_scripts' );
if ( empty( $args['action'] ) || empty( $args['file_data_name'] ) ) {
return new WP_Error( 'missing_parameter' );
}
// Get the BuddyPress uploader strings.
$strings = bp_attachments_get_plupload_l10n();
// Get the BuddyPress uploader settings.
$settings = bp_attachments_get_plupload_default_settings();
// Set feedback messages.
if ( ! empty( $args['feedback_messages'] ) ) {
$strings['feedback_messages'] = $args['feedback_messages'];
}
// Use a temporary var to ease manipulation.
$defaults = $settings['defaults'];
// Set the upload action.
$defaults['multipart_params']['action'] = $args['action'];
// Set BuddyPress upload parameters if provided.
if ( ! empty( $args['bp_params'] ) ) {
$defaults['multipart_params']['bp_params'] = $args['bp_params'];
}
// Merge other arguments.
$ui_args = array_intersect_key( $args, array(
'file_data_name' => true,
'browse_button' => true,
'container' => true,
'drop_element' => true,
) );
$defaults = array_merge( $defaults, $ui_args );
if ( ! empty( $args['max_file_size'] ) ) {
$defaults['filters']['max_file_size'] = $args['max_file_size'] . 'b';
}
// Specific to BuddyPress Avatars.
if ( 'bp_avatar_upload' === $defaults['multipart_params']['action'] ) {
// Include the cropping informations for avatars.
$settings['crop'] = array(
'full_h' => bp_core_avatar_full_height(),
'full_w' => bp_core_avatar_full_width(),
);
// Avatar only need 1 file and 1 only!
$defaults['multi_selection'] = false;
// Does the object already has an avatar set.
$has_avatar = $defaults['multipart_params']['bp_params']['has_avatar'];
// What is the object the avatar belongs to.
$object = $defaults['multipart_params']['bp_params']['object'];
// Init the Avatar nav.
$avatar_nav = array(
'upload' => array( 'id' => 'upload', 'caption' => __( 'Upload', 'buddyboss' ), 'order' => 0 ),
// The delete view will only show if the object has an avatar.
'delete' => array( 'id' => 'delete', 'caption' => __( 'Delete', 'buddyboss' ), 'order' => 100, 'hide' => (int) ! $has_avatar ),
);
// Create the Camera Nav if the WebCam capture feature is enabled.
if ( bp_avatar_use_webcam() && 'user' === $object ) {
$avatar_nav['camera'] = array( 'id' => 'camera', 'caption' => __( 'Take Photo', 'buddyboss' ), 'order' => 10 );
// Set warning messages.
$strings['camera_warnings'] = array(
'requesting' => __( 'Please allow application access to your camera.', 'buddyboss'),
'loading' => __( 'Please wait while your camera connects.', 'buddyboss' ),
'loaded' => __( 'Camera loaded. Click "Capture" to take a photo.', 'buddyboss' ),
'noaccess' => __( 'Webcam not found or permission was denied. Please upload a photo.', 'buddyboss' ),
'errormsg' => __( 'Your browser is not supported. Please upload a photo instead.', 'buddyboss' ),
'videoerror' => __( 'Video error. Please upload a photo instead.', 'buddyboss' ),
'ready' => __( 'Your profile photo is ready. Click "Save" to use this photo.', 'buddyboss' ),
'nocapture' => __( 'No photo captured. Click "Capture" to take your photo.', 'buddyboss' ),
);
}
/**
* Use this filter to add a navigation to a custom tool to set the object's avatar.
*
* @since BuddyPress 2.3.0
*
* @param array $avatar_nav {
* An associative array of available nav items where each item is an array organized this way:
* $avatar_nav[ $nav_item_id ].
* @type string $nav_item_id The nav item id in lower case without special characters or space.
* @type string $caption The name of the item nav that will be displayed in the nav.
* @type int $order An integer to specify the priority of the item nav, choose one.
* between 1 and 99 to be after the uploader nav item and before the delete nav item.
* @type int $hide If set to 1 the item nav will be hidden
* (only used for the delete nav item).
* }
* @param string $object The object the avatar belongs to (eg: user or group).
*/
$settings['nav'] = bp_sort_by_key( apply_filters( 'bp_attachments_avatar_nav', $avatar_nav, $object ), 'order', 'num' );
// Specific to BuddyPress cover photos.
} elseif ( 'bp_cover_image_upload' === $defaults['multipart_params']['action'] ) {
// cover photos only need 1 file and 1 only!
$defaults['multi_selection'] = false;
// Default cover component is xprofile.
$cover_component = 'xprofile';
// Get the object we're editing the cover photo of.
$object = $defaults['multipart_params']['bp_params']['object'];
// Set the cover component according to the object.
if ( 'group' === $object ) {
$cover_component = 'groups';
} elseif ( 'user' !== $object ) {
$cover_component = apply_filters( 'bp_attachments_cover_image_ui_component', $cover_component );
}
// Get cover photo advised dimensions.
$cover_dimensions = bp_attachments_get_cover_image_dimensions( $cover_component );
// Set warning messages.
$strings['cover_image_warnings'] = apply_filters( 'bp_attachments_cover_image_ui_warnings', array(
'dimensions' => sprintf(
__( 'For best results, upload an image that is %1$spx by %2$spx or larger.', 'buddyboss' ),
(int) $cover_dimensions['width'],
(int) $cover_dimensions['height']
),
) );
}
// Set Plupload settings.
$settings['defaults'] = $defaults;
/**
* Enqueue some extra styles if required
*
* Extra styles need to be registered.
*/
if ( ! empty( $args['extra_css'] ) ) {
foreach ( (array) $args['extra_css'] as $css ) {
if ( empty( $css ) ) {
continue;
}
wp_enqueue_style( $css );
}
}
wp_enqueue_script ( 'bp-plupload' );
wp_localize_script( 'bp-plupload', 'BP_Uploader', array( 'strings' => $strings, 'settings' => $settings ) );
/**
* Enqueue some extra scripts if required
*
* Extra scripts need to be registered.
*/
if ( ! empty( $args['extra_js'] ) ) {
foreach ( (array) $args['extra_js'] as $js ) {
if ( empty( $js ) ) {
continue;
}
wp_enqueue_script( $js );
}
}
/**
* Fires at the conclusion of bp_attachments_enqueue_scripts()
* to avoid the scripts to be loaded more than once.
*
* @since BuddyPress 2.3.0
*/
do_action( 'bp_attachments_enqueue_scripts' );
}
Changelog
| Version | Description |
|---|---|
| BuddyPress 2.3.0 | Introduced. |
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.