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