Changeset 10151 for trunk/src/bp-core/bp-core-attachments.php
- Timestamp:
- 09/29/2015 09:43:13 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/bp-core-attachments.php
r10108 r10151 23 23 function bp_attachments_is_wp_version_supported() { 24 24 return (bool) version_compare( bp_get_major_wp_version(), '3.9', '>=' ); 25 } 26 27 /** 28 * Get the Attachments Uploads dir data 29 * 30 * @since 2.4.0 31 * 32 * @param string $data The data to get. Possible values are: 'dir', 'basedir' & 'baseurl' 33 * Leave empty to get all datas. 34 * @return string|array The needed Upload dir data. 35 */ 36 function bp_attachments_uploads_dir_get( $data = '' ) { 37 $attachments_dir = 'buddypress'; 38 $retval = ''; 39 40 if ( 'dir' === $data ) { 41 $retval = $attachments_dir; 42 } else { 43 $upload_data = bp_upload_dir(); 44 45 // Build the Upload data array for BuddyPress attachments 46 foreach ( $upload_data as $key => $value ) { 47 if ( 'basedir' === $key || 'baseurl' === $key ) { 48 $upload_data[ $key ] = trailingslashit( $value ) . $attachments_dir; 49 } else { 50 unset( $upload_data[ $key ] ); 51 } 52 } 53 54 // Add the dir to the array 55 $upload_data['dir'] = $attachments_dir; 56 57 if ( empty( $data ) ) { 58 $retval = $upload_data; 59 } elseif ( isset( $upload_data[ $data ] ) ) { 60 $retval = $upload_data[ $data ]; 61 } 62 } 63 64 /** 65 * Filter here to edit the Attachments upload dir data. 66 * 67 * @since 2.4.0 68 * 69 * @param string|array $retval The needed Upload dir data or the full array of data 70 * @param string $data The data requested 71 */ 72 return apply_filters( 'bp_attachments_uploads_dir_get', $retval, $data ); 73 } 74 75 /** 76 * Get the max upload file size for any attachment 77 * 78 * @since 2.4.0 79 * 80 * @param string $type A string to inform about the type of attachment 81 * we wish to get the max upload file size for 82 * @return int max upload file size for any attachment 83 */ 84 function bp_attachments_get_max_upload_file_size( $type = '' ) { 85 $fileupload_maxk = bp_core_get_root_option( 'fileupload_maxk' ); 86 87 if ( '' === $fileupload_maxk ) { 88 $fileupload_maxk = 5120000; // 5mb; 89 } else { 90 $fileupload_maxk = $fileupload_maxk * 1024; 91 } 92 93 /** 94 * Filter here to edit the max upload file size. 95 * 96 * @since 2.4.0 97 * 98 * @param int $fileupload_maxk Max upload file size for any attachment 99 * @param string $type The attachment type (eg: 'avatar' or 'cover_image') 100 */ 101 return apply_filters( 'bp_attachments_get_max_upload_file_size', $fileupload_maxk, $type ); 102 } 103 104 /** 105 * Get allowed types for any attachment 106 * 107 * @since 2.4.0 108 * 109 * @param string $type The extension types to get. 110 * Default: 'avatar' 111 * @return array The list of allowed extensions for attachments 112 */ 113 function bp_attachments_get_allowed_types( $type = 'avatar' ) { 114 // Defaults to BuddyPress supported image extensions 115 $exts = array( 'jpeg', 'gif', 'png' ); 116 117 /** 118 * It's not a BuddyPress feature, get the allowed extensions 119 * matching the $type requested 120 */ 121 if ( 'avatar' !== $type && 'cover_image' !== $type ) { 122 // Reset the default exts 123 $exts = array(); 124 125 switch ( $type ) { 126 case 'video' : 127 $exts = wp_get_video_extensions(); 128 break; 129 130 case 'audio' : 131 $exts = wp_get_video_extensions(); 132 break; 133 134 default: 135 $allowed_mimes = get_allowed_mime_types(); 136 137 /** 138 * Search for allowed mimes matching the type 139 * 140 * eg: using 'application/vnd.oasis' as the $type 141 * parameter will get all OpenOffice extensions supported 142 * by WordPress and allowed for the current user. 143 */ 144 if ( '' !== $type ) { 145 $allowed_mimes = preg_grep( '/' . addcslashes( $type, '/.+-' ) . '/', $allowed_mimes ); 146 } 147 148 $allowed_types = array_keys( $allowed_mimes ); 149 150 // Loop to explode keys using '|' 151 foreach ( $allowed_types as $allowed_type ) { 152 $t = explode( '|', $allowed_type ); 153 $exts = array_merge( $exts, (array) $t ); 154 } 155 break; 156 } 157 } 158 159 /** 160 * Filter here to edit the allowed extensions by attachment type. 161 * 162 * @since 2.4.0 163 * 164 * @param array $exts List of allowed extensions 165 * @param string $type The requested file type 166 */ 167 return apply_filters( 'bp_attachments_get_allowed_types', $exts, $type ); 168 } 169 170 /** 171 * Get allowed attachment mime types. 172 * 173 * @since 2.4.0 174 * 175 * @param string $type The extension types to get (Optional). 176 * @param array $allowed_types List of allowed extensions 177 * @return array List of allowed mime types 178 */ 179 function bp_attachments_get_allowed_mimes( $type = '', $allowed_types = array() ) { 180 if ( empty( $allowed_types ) ) { 181 $allowed_types = bp_attachments_get_allowed_types( $type ); 182 } 183 184 $validate_mimes = wp_match_mime_types( join( ',', $allowed_types ), wp_get_mime_types() ); 185 $allowed_mimes = array_map( 'implode', $validate_mimes ); 186 187 /** 188 * Include jpg type if jpeg is set 189 */ 190 if ( isset( $allowed_mimes['jpeg'] ) && ! isset( $allowed_mimes['jpg'] ) ) { 191 $allowed_mimes['jpg'] = $allowed_mimes['jpeg']; 192 } 193 194 return $allowed_mimes; 195 } 196 197 /** 198 * Check the uploaded attachment type is allowed 199 * 200 * @since 2.4.0 201 * 202 * @param string $file Full path to the file. 203 * @param string $filename The name of the file (may differ from $file due to $file being 204 * in a tmp directory). 205 * @param array $allowed_mimes The attachment allowed mimes (Required) 206 * @return bool True if the attachment type is allowed. False otherwise 207 */ 208 function bp_attachments_check_filetype( $file, $filename, $allowed_mimes ) { 209 $filetype = wp_check_filetype_and_ext( $file, $filename, $allowed_mimes ); 210 211 if ( ! empty( $filetype['ext'] ) && ! empty( $filetype['type'] ) ) { 212 return true; 213 } 214 215 return false; 216 } 217 218 /** 219 * Get the url or the path for a type of attachment 220 * 221 * @since 2.4.0 222 * 223 * @param string $data whether to get the url or the path 224 * @param array $args { 225 * @type string $object_dir The object dir (eg: members/groups). Defaults to members. 226 * @type int $item_id The object id (eg: a user or a group id). Defaults to current user. 227 * @type string $type The type of the attachment which is also the subdir where files are saved. 228 * Defaults to 'cover-image' 229 * @type string $file The name of the file. 230 * } 231 * @return string|bool the url or the path to the attachment, false otherwise 232 */ 233 function bp_attachments_get_attachment( $data = 'url', $args = array() ) { 234 // Default value 235 $attachment_data = false; 236 237 $r = bp_parse_args( $args, array( 238 'object_dir' => 'members', 239 'item_id' => bp_loggedin_user_id(), 240 'type' => 'cover-image', 241 'file' => '', 242 ), 'attachments_get_attachment_src' ); 243 244 // Get BuddyPress Attachments Uploads Dir datas 245 $bp_attachments_uploads_dir = bp_attachments_uploads_dir_get(); 246 247 $type_subdir = $r['object_dir'] . '/' . $r['item_id'] . '/' . $r['type']; 248 $type_dir = trailingslashit( $bp_attachments_uploads_dir['basedir'] ) . $type_subdir; 249 250 if ( ! is_dir( $type_dir ) ) { 251 return $attachment_data; 252 } 253 254 if ( ! empty( $r['file'] ) ) { 255 if ( ! file_exists( trailingslashit( $type_dir ) . $r['file'] ) ) { 256 return $attachment_data; 257 } 258 259 if ( 'url' === $data ) { 260 $attachment_data = trailingslashit( $bp_attachments_uploads_dir['baseurl'] ) . $type_subdir . '/' . $r['file']; 261 } else { 262 $attachment_data = trailingslashit( $type_dir ) . $r['file']; 263 } 264 265 } else { 266 $file = false; 267 268 // Open the directory and get the first file 269 if ( $att_dir = opendir( $type_dir ) ) { 270 271 while ( false !== ( $attachment_file = readdir( $att_dir ) ) ) { 272 // Look for the first file having the type in its name 273 if ( false !== strpos( $attachment_file, $r['type'] ) && empty( $file ) ) { 274 $file = $attachment_file; 275 break; 276 } 277 } 278 } 279 280 if ( empty( $file ) ) { 281 return $attachment_data; 282 } 283 284 if ( 'url' === $data ) { 285 $attachment_data = trailingslashit( $bp_attachments_uploads_dir['baseurl'] ) . $type_subdir . '/' . $file; 286 } else { 287 $attachment_data = trailingslashit( $type_dir ) . $file; 288 } 289 } 290 291 return $attachment_data; 292 } 293 294 /** 295 * Delete an attachment for the given arguments 296 * 297 * @since 2.4.0 298 * 299 * @param array $args 300 * @see bp_attachments_get_attachment() For more information on accepted arguments. 301 * @return bool True if the attachment was deleted, false otherwise 302 */ 303 function bp_attachments_delete_file( $args = array() ) { 304 $attachment_path = bp_attachments_get_attachment( 'path', $args ); 305 306 if ( empty( $attachment_path ) ) { 307 return false; 308 } 309 310 @unlink( $attachment_path ); 311 return true; 25 312 } 26 313
Note: See TracChangeset
for help on using the changeset viewer.