/** * Build script datas for the Uploader UI. * * @since 2.3.0 * * @return array The javascript localization data. */ public function script_data() { // Get default script data. $script_data = parent::script_data(); // Defaults to Avatar Backbone script. $js_scripts = array('bp-avatar'); // Default object. $object = ''; // Get the possible item ids. $user_id = $this->get_user_id(); $group_id = $this->get_group_id(); if (!empty($user_id)) { // Should we load the the Webcam Avatar javascript file. if (bp_avatar_use_webcam()) { $js_scripts = array('bp-webcam'); } $script_data['bp_params'] = array('object' => 'user', 'item_id' => $user_id, 'has_avatar' => bp_get_user_has_avatar($user_id), 'nonces' => array('set' => wp_create_nonce('bp_avatar_cropstore'), 'remove' => wp_create_nonce('bp_delete_avatar_link'))); // Set feedback messages. $script_data['feedback_messages'] = array(1 => __('There was a problem cropping your profile photo.', 'buddypress'), 2 => __('Your new profile photo was uploaded successfully.', 'buddypress'), 3 => __('There was a problem deleting your profile photo. Please try again.', 'buddypress'), 4 => __('Your profile photo was deleted successfully!', 'buddypress')); } elseif (!empty($group_id)) { $script_data['bp_params'] = array('object' => 'group', 'item_id' => $group_id, 'has_avatar' => bp_get_group_has_avatar($group_id), 'nonces' => array('set' => wp_create_nonce('bp_avatar_cropstore'), 'remove' => wp_create_nonce('bp_group_avatar_delete'))); // Set feedback messages. $script_data['feedback_messages'] = array(1 => __('There was a problem cropping the group profile photo.', 'buddypress'), 2 => __('The group profile photo was uploaded successfully.', 'buddypress'), 3 => __('There was a problem deleting the group profile photo. Please try again.', 'buddypress'), 4 => __('The group profile photo was deleted successfully!', 'buddypress')); } else { /** * Use this filter to include specific BuddyPress params for your object. * e.g. Blavatar. * * @since 2.3.0 * * @param array $value The avatar specific BuddyPress parameters. */ $script_data['bp_params'] = apply_filters('bp_attachment_avatar_params', array()); } // Include the specific css. $script_data['extra_css'] = array('bp-avatar'); // Include the specific css. $script_data['extra_js'] = $js_scripts; // Set the object to contextualize the filter. if (isset($script_data['bp_params']['object'])) { $object = $script_data['bp_params']['object']; } /** * Use this filter to override/extend the avatar script data. * * @since 2.3.0 * * @param array $script_data The avatar script data. * @param string $object The object the avatar belongs to (eg: user or group). */ return apply_filters('bp_attachment_avatar_script_data', $script_data, $object); }
/** * Enqueues the script needed for the Uploader UI. * * @see BP_Attachment::script_data() && BP_Attachment_Avatar::script_data() for examples showing how * to set specific script data. * * @since 2.3.0 * * @param string $class name of the class extending BP_Attachment (eg: BP_Attachment_Avatar). * * @return null|WP_Error */ 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', 'buddypress'), 'order' => 0), 'delete' => array('id' => 'delete', 'caption' => __('Delete', 'buddypress'), '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', 'buddypress'), 'order' => 10); // Set warning messages $strings['camera_warnings'] = array('requesting' => __('Please allow us to access to your camera.', 'buddypress'), 'loading' => __('Please wait as we access your camera.', 'buddypress'), 'loaded' => __('Camera loaded. Click on the "Capture" button to take your photo.', 'buddypress'), 'noaccess' => __('It looks like you do not have a webcam or we were unable to get permission to use your webcam. Please upload a photo instead.', 'buddypress'), 'errormsg' => __('Your browser is not supported. Please upload a photo instead.', 'buddypress'), 'videoerror' => __('Video error. Please upload a photo instead.', 'buddypress'), 'ready' => __('Your profile photo is ready. Click on the "Save" button to use this photo.', 'buddypress'), 'nocapture' => __('No photo was captured. Click on the "Capture" button to take your photo.', 'buddypress')); } /** * Use this filter to add a navigation to a custom tool to set the object's avatar. * * @since 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 images } elseif ('bp_cover_image_upload' === $defaults['multipart_params']['action']) { // Cover images 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 image 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 image 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 better results, make sure to upload an image that is larger than %1$spx wide, and %2$spx tall.', 'buddypress'), (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 2.3.0 */ do_action('bp_attachments_enqueue_scripts'); }