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