bp_attachments_enqueue_scripts

Enqueues the script needed for the Uploader UI.

Description

(null|WP_Error) bp_attachments_enqueue_scripts( (string) $class = '' ); 

Returns (null|WP_Error)

Parameters (1)

0. $class — Optional. (string) => ''
Name of the class extending BP_Attachment (eg: BP_Attachment_Avatar).

Usage

  1. if ( !function_exists( 'bp_attachments_enqueue_scripts' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-attachments.php'; 
  3.  
  4. // Name of the class extending BP_Attachment (eg: BP_Attachment_Avatar). 
  5. $class = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bp_attachments_enqueue_scripts($class); 
  9.  

Defined (1)

The function is defined in the following location(s).

/bp-core/bp-core-attachments.php  
  1. function bp_attachments_enqueue_scripts( $class = '' ) { 
  2. // Enqueue me just once per page, please. 
  3. if ( did_action( 'bp_attachments_enqueue_scripts' ) ) { 
  4. return; 
  5.  
  6. if ( ! $class || ! class_exists( $class ) ) { 
  7. return new WP_Error( 'missing_parameter' ); 
  8.  
  9. // Get an instance of the class and get the script data. 
  10. $attachment = new $class; 
  11. $script_data = $attachment->script_data(); 
  12.  
  13. $args = bp_parse_args( $script_data, array( 
  14. 'action' => '',  
  15. 'file_data_name' => '',  
  16. 'max_file_size' => 0,  
  17. 'browse_button' => 'bp-browse-button',  
  18. 'container' => 'bp-upload-ui',  
  19. 'drop_element' => 'drag-drop-area',  
  20. 'bp_params' => array(),  
  21. 'extra_css' => array(),  
  22. 'extra_js' => array(),  
  23. 'feedback_messages' => array(),  
  24. ), 'attachments_enqueue_scripts' ); 
  25.  
  26. if ( empty( $args['action'] ) || empty( $args['file_data_name'] ) ) { 
  27. return new WP_Error( 'missing_parameter' ); 
  28.  
  29. // Get the BuddyPress uploader strings. 
  30.  
  31. // Get the BuddyPress uploader settings. 
  32.  
  33. // Set feedback messages. 
  34. if ( ! empty( $args['feedback_messages'] ) ) { 
  35. $strings['feedback_messages'] = $args['feedback_messages']; 
  36.  
  37. // Use a temporary var to ease manipulation. 
  38. $defaults = $settings['defaults']; 
  39.  
  40. // Set the upload action. 
  41. $defaults['multipart_params']['action'] = $args['action']; 
  42.  
  43. // Set BuddyPress upload parameters if provided. 
  44. if ( ! empty( $args['bp_params'] ) ) { 
  45. $defaults['multipart_params']['bp_params'] = $args['bp_params']; 
  46.  
  47. // Merge other arguments. 
  48. $ui_args = array_intersect_key( $args, array( 
  49. 'file_data_name' => true,  
  50. 'browse_button' => true,  
  51. 'container' => true,  
  52. 'drop_element' => true,  
  53. ) ); 
  54.  
  55. $defaults = array_merge( $defaults, $ui_args ); 
  56.  
  57. if ( ! empty( $args['max_file_size'] ) ) { 
  58. $defaults['filters']['max_file_size'] = $args['max_file_size'] . 'b'; 
  59.  
  60. // Specific to BuddyPress Avatars. 
  61. if ( 'bp_avatar_upload' === $defaults['multipart_params']['action'] ) { 
  62.  
  63. // Include the cropping informations for avatars. 
  64. $settings['crop'] = array( 
  65. 'full_h' => bp_core_avatar_full_height(),  
  66. 'full_w' => bp_core_avatar_full_width(),  
  67. ); 
  68.  
  69. // Avatar only need 1 file and 1 only! 
  70. $defaults['multi_selection'] = false; 
  71.  
  72. // Does the object already has an avatar set. 
  73. $has_avatar = $defaults['multipart_params']['bp_params']['has_avatar']; 
  74.  
  75. // What is the object the avatar belongs to. 
  76. $object = $defaults['multipart_params']['bp_params']['object']; 
  77.  
  78. // Init the Avatar nav. 
  79. $avatar_nav = array( 
  80. 'upload' => array( 'id' => 'upload', 'caption' => __( 'Upload', buddypress ), 'order' => 0 ),  
  81.  
  82. // The delete view will only show if the object has an avatar. 
  83. 'delete' => array( 'id' => 'delete', 'caption' => __( 'Delete', buddypress ), 'order' => 100, 'hide' => (int) ! $has_avatar ),  
  84. ); 
  85.  
  86. // Create the Camera Nav if the WebCam capture feature is enabled. 
  87. if ( bp_avatar_use_webcam() && 'user' === $object ) { 
  88. $avatar_nav['camera'] = array( 'id' => 'camera', 'caption' => __( 'Take Photo', buddypress ), 'order' => 10 ); 
  89.  
  90. // Set warning messages. 
  91. $strings['camera_warnings'] = array( 
  92. 'requesting' => __( 'Please allow us to access to your camera.', buddypress),  
  93. 'loading' => __( 'Please wait as we access your camera.', buddypress ),  
  94. 'loaded' => __( 'Camera loaded. Click on the "Capture" button to take your photo.', buddypress ),  
  95. '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 ),  
  96. 'errormsg' => __( 'Your browser is not supported. Please upload a photo instead.', buddypress ),  
  97. 'videoerror' => __( 'Video error. Please upload a photo instead.', buddypress ),  
  98. 'ready' => __( 'Your profile photo is ready. Click on the "Save" button to use this photo.', buddypress ),  
  99. 'nocapture' => __( 'No photo was captured. Click on the "Capture" button to take your photo.', buddypress ),  
  100. ); 
  101.  
  102. /** 
  103. * Use this filter to add a navigation to a custom tool to set the object's avatar. 
  104. * @since 2.3.0 
  105. * @param array $avatar_nav { 
  106. * An associative array of available nav items where each item is an array organized this way: 
  107. * $avatar_nav[ $nav_item_id ]. 
  108. * @type string $nav_item_id The nav item id in lower case without special characters or space. 
  109. * @type string $caption The name of the item nav that will be displayed in the nav. 
  110. * @type int $order An integer to specify the priority of the item nav, choose one. 
  111. * between 1 and 99 to be after the uploader nav item and before the delete nav item. 
  112. * @type int $hide If set to 1 the item nav will be hidden 
  113. * (only used for the delete nav item). 
  114. * } 
  115. * @param string $object The object the avatar belongs to (eg: user or group). 
  116. */ 
  117. $settings['nav'] = bp_sort_by_key( apply_filters( 'bp_attachments_avatar_nav', $avatar_nav, $object ), 'order', 'num' ); 
  118.  
  119. // Specific to BuddyPress cover images. 
  120. } elseif ( 'bp_cover_image_upload' === $defaults['multipart_params']['action'] ) { 
  121.  
  122. // Cover images only need 1 file and 1 only! 
  123. $defaults['multi_selection'] = false; 
  124.  
  125. // Default cover component is xprofile. 
  126. $cover_component = 'xprofile'; 
  127.  
  128. // Get the object we're editing the cover image of. 
  129. $object = $defaults['multipart_params']['bp_params']['object']; 
  130.  
  131. // Set the cover component according to the object. 
  132. if ( 'group' === $object ) { 
  133. $cover_component = 'groups'; 
  134. } elseif ( 'user' !== $object ) { 
  135. $cover_component = apply_filters( 'bp_attachments_cover_image_ui_component', $cover_component ); 
  136. // Get cover image advised dimensions. 
  137. $cover_dimensions = bp_attachments_get_cover_image_dimensions( $cover_component ); 
  138.  
  139. // Set warning messages. 
  140. $strings['cover_image_warnings'] = apply_filters( 'bp_attachments_cover_image_ui_warnings', array( 
  141. 'dimensions' => sprintf( 
  142. __( 'For better results, make sure to upload an image that is larger than %1$spx wide, and %2$spx tall.', buddypress ),  
  143. (int) $cover_dimensions['width'],  
  144. (int) $cover_dimensions['height'] 
  145. ),  
  146. ) ); 
  147.  
  148. // Set Plupload settings. 
  149. $settings['defaults'] = $defaults; 
  150.  
  151. /** 
  152. * Enqueue some extra styles if required 
  153. * Extra styles need to be registered. 
  154. */ 
  155. if ( ! empty( $args['extra_css'] ) ) { 
  156. foreach ( (array) $args['extra_css'] as $css ) { 
  157. if ( empty( $css ) ) { 
  158. continue; 
  159.  
  160. wp_enqueue_style( $css ); 
  161.  
  162. wp_enqueue_script ( 'bp-plupload' ); 
  163. wp_localize_script( 'bp-plupload', 'BP_Uploader', array( 'strings' => $strings, 'settings' => $settings ) ); 
  164.  
  165. /** 
  166. * Enqueue some extra scripts if required 
  167. * Extra scripts need to be registered. 
  168. */ 
  169. if ( ! empty( $args['extra_js'] ) ) { 
  170. foreach ( (array) $args['extra_js'] as $js ) { 
  171. if ( empty( $js ) ) { 
  172. continue; 
  173.  
  174.  
  175. /** 
  176. * Fires at the conclusion of bp_attachments_enqueue_scripts() 
  177. * to avoid the scripts to be loaded more than once. 
  178. * @since 2.3.0 
  179. */