BP_Attachment_Cover_Image

BP Attachment Cover Image class.

Defined (1)

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

/bp-core/classes/class-bp-attachment-cover-image.php  
  1. class BP_Attachment_Cover_Image extends BP_Attachment { 
  2. /** 
  3. * The constuctor. 
  4. * @since 2.4.0 
  5. */ 
  6. public function __construct() { 
  7. // Allowed cover image types & upload size. 
  8. $allowed_types = bp_attachments_get_allowed_types( 'cover_image' ); 
  9. $max_upload_file_size = bp_attachments_get_max_upload_file_size( 'cover_image' ); 
  10.  
  11. parent::__construct( array( 
  12. 'action' => 'bp_cover_image_upload',  
  13. 'file_input' => 'file',  
  14. 'original_max_filesize' => $max_upload_file_size,  
  15. 'base_dir' => bp_attachments_uploads_dir_get( 'dir' ),  
  16. 'required_wp_files' => array( 'file', 'image' ),  
  17.  
  18. // Specific errors for cover images. 
  19. 'upload_error_strings' => array( 
  20. 11 => sprintf( __( 'That image is too big. Please upload one smaller than %s', 'buddypress' ), size_format( $max_upload_file_size ) ),  
  21. 12 => sprintf( _n( 'Please upload only this file type: %s.', 'Please upload only these file types: %s.', count( $allowed_types ), 'buddypress' ), self::get_cover_image_types( $allowed_types ) ),  
  22. ),  
  23. ) ); 
  24.  
  25. /** 
  26. * Gets the available cover image types. 
  27. * @since 2.4.0 
  28. * @param array $allowed_types Array of allowed cover image types. 
  29. * @return string $value Comma-separated list of allowed cover image types. 
  30. */ 
  31. public static function get_cover_image_types( $allowed_types = array() ) { 
  32. $types = array_map( 'strtoupper', $allowed_types ); 
  33. $comma = _x( ', ', 'cover image types separator', 'buddypress' ); 
  34. return join( $comma . ' ', $types ); 
  35.  
  36. /** 
  37. * Cover image specific rules. 
  38. * Adds an error if the cover image size or type don't match BuddyPress needs. 
  39. * The error code is the index of $upload_error_strings. 
  40. * @since 2.4.0 
  41. * @param array $file The temporary file attributes (before it has been moved). 
  42. * @return array $file The file with extra errors if needed. 
  43. */ 
  44. public function validate_upload( $file = array() ) { 
  45. // Bail if already an error. 
  46. if ( ! empty( $file['error'] ) ) { 
  47. return $file; 
  48.  
  49. // File size is too big. 
  50. if ( $file['size'] > $this->original_max_filesize ) { 
  51. $file['error'] = 11; 
  52.  
  53. // File is of invalid type. 
  54. } elseif ( ! bp_attachments_check_filetype( $file['tmp_name'], $file['name'], bp_attachments_get_allowed_mimes( 'cover_image' ) ) ) { 
  55. $file['error'] = 12; 
  56.  
  57. // Return with error code attached. 
  58. return $file; 
  59.  
  60. /** 
  61. * Set the directory when uploading a file. 
  62. * @since 2.4.0 
  63. * @param array $upload_dir The original Uploads dir. 
  64. * @return array $value Upload data (path, url, basedir...). 
  65. */ 
  66. public function upload_dir_filter( $upload_dir = array() ) { 
  67. // Default values are for profiles. 
  68. $object_id = bp_displayed_user_id(); 
  69.  
  70. if ( empty( $object_id ) ) { 
  71. $object_id = bp_loggedin_user_id(); 
  72.  
  73. $object_directory = 'members'; 
  74.  
  75. // We're in a group, edit default values. 
  76. if ( bp_is_group() || bp_is_group_create() ) { 
  77. $object_id = bp_get_current_group_id(); 
  78. $object_directory = 'groups'; 
  79.  
  80. // Set the subdir. 
  81. $subdir = '/' . $object_directory . '/' . $object_id . '/cover-image'; 
  82.  
  83. /** 
  84. * Filters the cover image upload directory. 
  85. * @since 2.4.0 
  86. * @param array $value Array containing the path, URL, and other helpful settings. 
  87. * @param array $upload_dir The original Uploads dir. 
  88. */ 
  89. return apply_filters( 'bp_attachments_cover_image_upload_dir', array( 
  90. 'path' => $this->upload_path . $subdir,  
  91. 'url' => $this->url . $subdir,  
  92. 'subdir' => $subdir,  
  93. 'basedir' => $this->upload_path,  
  94. 'baseurl' => $this->url,  
  95. 'error' => false 
  96. ), $upload_dir ); 
  97.  
  98. /** 
  99. * Adjust the cover image to fit with advised width & height. 
  100. * @since 2.4.0 
  101. * @param string $file The absolute path to the file. 
  102. * @param array $dimensions Array of dimensions for the cover image. 
  103. * @return mixed 
  104. */ 
  105. public function fit( $file = '', $dimensions = array() ) { 
  106. if ( empty( $dimensions['width'] ) || empty( $dimensions['height'] ) ) { 
  107. return false; 
  108.  
  109. // Get image size. 
  110. $cover_data = parent::get_image_data( $file ); 
  111.  
  112. // Init the edit args. 
  113. $edit_args = array(); 
  114.  
  115. // Do we need to resize the image? 
  116. if ( ( isset( $cover_data['width'] ) && $cover_data['width'] > $dimensions['width'] ) || 
  117. ( isset( $cover_data['height'] ) && $cover_data['height'] > $dimensions['height'] ) ) { 
  118. $edit_args = array( 
  119. 'max_w' => $dimensions['width'],  
  120. 'max_h' => $dimensions['height'],  
  121. 'crop' => true,  
  122. ); 
  123.  
  124. // Do we need to rotate the image? 
  125. $angles = array( 
  126. 3 => 180,  
  127. 6 => -90,  
  128. 8 => 90,  
  129. ); 
  130.  
  131. if ( isset( $cover_data['meta']['orientation'] ) && isset( $angles[ $cover_data['meta']['orientation'] ] ) ) { 
  132. $edit_args['rotate'] = $angles[ $cover_data['meta']['orientation'] ]; 
  133.  
  134. // No need to edit the avatar, original file will be used. 
  135. if ( empty( $edit_args ) ) { 
  136. return false; 
  137.  
  138. // Add the file to the edit arguments. 
  139. } else { 
  140. $edit_args = array_merge( $edit_args, array( 'file' => $file, 'save' => false ) ); 
  141.  
  142. // Get the editor so that we can use a specific save method. 
  143. $editor = parent::edit_image( 'cover_image', $edit_args ); 
  144.  
  145. if ( is_wp_error( $editor ) ) { 
  146. return $editor; 
  147. } elseif ( ! is_a( $editor, 'WP_Image_Editor' ) ) { 
  148. return false; 
  149.  
  150. // Save the new image file. 
  151. return $editor->save( $this->generate_filename( $file ) ); 
  152.  
  153. /** 
  154. * Generate a filename for the cover image. 
  155. * @since 2.4.0 
  156. * @param string $file The absolute path to the file. 
  157. * @return string $value The absolute path to the new file name. 
  158. */ 
  159. public function generate_filename( $file = '' ) { 
  160. if ( empty( $file ) || ! file_exists( $file ) ) { 
  161. return false; 
  162.  
  163. $info = pathinfo( $file ); 
  164. $ext = strtolower( $info['extension'] ); 
  165. $name = wp_unique_filename( $info['dirname'], uniqid() . "-bp-cover-image.$ext" ); 
  166.  
  167. return trailingslashit( $info['dirname'] ) . $name; 
  168.  
  169. /** 
  170. * Build script datas for the Uploader UI. 
  171. * @since 2.4.0 
  172. * @return array The javascript localization data 
  173. */ 
  174. public function script_data() { 
  175. // Get default script data. 
  176. $script_data = parent::script_data(); 
  177.  
  178. if ( bp_is_user() ) { 
  179. $item_id = bp_displayed_user_id(); 
  180.  
  181. $script_data['bp_params'] = array( 
  182. 'object' => 'user',  
  183. 'item_id' => $item_id,  
  184. 'has_cover_image' => bp_attachments_get_user_has_cover_image( $item_id ),  
  185. 'nonces' => array( 
  186. 'remove' => wp_create_nonce( 'bp_delete_cover_image' ),  
  187. ),  
  188. ); 
  189.  
  190. // Set feedback messages. 
  191. $script_data['feedback_messages'] = array( 
  192. 1 => __( 'Your new cover image was uploaded successfully.', 'buddypress' ),  
  193. 2 => __( 'There was a problem deleting your cover image. Please try again.', 'buddypress' ),  
  194. 3 => __( 'Your cover image was deleted successfully!', 'buddypress' ),  
  195. ); 
  196. } elseif ( bp_is_group() ) { 
  197. $item_id = bp_get_current_group_id(); 
  198.  
  199. $script_data['bp_params'] = array( 
  200. 'object' => 'group',  
  201. 'item_id' => bp_get_current_group_id(),  
  202. 'has_cover_image' => bp_attachments_get_group_has_cover_image( $item_id ),  
  203. 'nonces' => array( 
  204. 'remove' => wp_create_nonce( 'bp_delete_cover_image' ),  
  205. ),  
  206. ); 
  207.  
  208. // Set feedback messages. 
  209. $script_data['feedback_messages'] = array( 
  210. 1 => __( 'The group cover image was uploaded successfully.', 'buddypress' ),  
  211. 2 => __( 'There was a problem deleting the group cover image. Please try again.', 'buddypress' ),  
  212. 3 => __( 'The group cover image was deleted successfully!', 'buddypress' ),  
  213. ); 
  214. } else { 
  215.  
  216. /** 
  217. * Filters the cover image params to include specific BuddyPress params for your object. 
  218. * e.g. Cover image for blogs single item. 
  219. * @since 2.4.0 
  220. * @param array $value The cover image specific BuddyPress parameters. 
  221. */ 
  222. $script_data['bp_params'] = apply_filters( 'bp_attachment_cover_image_params', array() ); 
  223.  
  224. // Include our specific js & css. 
  225. $script_data['extra_js'] = array( 'bp-cover-image' ); 
  226. $script_data['extra_css'] = array( 'bp-avatar' ); 
  227.  
  228. /** 
  229. * Filters the cover image script data. 
  230. * @since 2.4.0 
  231. * @param array $script_data Array of data for the cover image. 
  232. */ 
  233. return apply_filters( 'bp_attachments_cover_image_script_data', $script_data );