| | 219 | * Use the absolute path to an image to set an attachment type for a given item. |
| | 220 | * |
| | 221 | * @since 2.4.0 |
| | 222 | * |
| | 223 | * @param string $type The attachment type to create (avatar or cover_image). Default: avatar. |
| | 224 | * @param array $args { |
| | 225 | * @type int $item_id The ID of the object (Required). Default: 0. |
| | 226 | * @type string $object The object type (eg: group, user, blog) (Required). Default: 'user'. |
| | 227 | * @type string $component The component for the object (eg: groups, xprofile, blogs). Default: ''. |
| | 228 | * @type string $image The absolute path to the image (Required). Default: ''. |
| | 229 | * @type int $crop_w Crop width. Default: 0. |
| | 230 | * @type int $crop_h Crop height. Default: 0. |
| | 231 | * @type int $crop_x The horizontal starting point of the crop. Default: 0. |
| | 232 | * @type int $crop_y The vertical starting point of the crop. Default: 0. |
| | 233 | * } |
| | 234 | * @return bool True on success, false otherwise. |
| | 235 | */ |
| | 236 | function bp_attachments_create_item_type( $type = 'avatar', $args = array() ) { |
| | 237 | if ( empty( $type ) || ( $type !== 'avatar' && $type !== 'cover_image' ) ) { |
| | 238 | return false; |
| | 239 | } |
| | 240 | |
| | 241 | $r = bp_parse_args( $args, array( |
| | 242 | 'item_id' => 0, |
| | 243 | 'object' => 'user', |
| | 244 | 'component' => '', |
| | 245 | 'image' => '', |
| | 246 | 'crop_w' => 0, |
| | 247 | 'crop_h' => 0, |
| | 248 | 'crop_x' => 0, |
| | 249 | 'crop_y' => 0 |
| | 250 | ), 'create_item_' . $type ); |
| | 251 | |
| | 252 | if ( empty( $r['item_id'] ) || empty( $r['object'] ) || ! file_exists( $r['image'] ) || ! @getimagesize( $r['image'] ) ) { |
| | 253 | return false; |
| | 254 | } |
| | 255 | |
| | 256 | // Set the component if not already done |
| | 257 | if ( empty( $r['component'] ) ) { |
| | 258 | if ( 'user' === $r['object'] ) { |
| | 259 | $r['component'] = 'xprofile'; |
| | 260 | } else { |
| | 261 | $r['component'] = $r['object'] . 's'; |
| | 262 | } |
| | 263 | } |
| | 264 | |
| | 265 | // Get allowed mimes for the Attachment type and check the image one is. |
| | 266 | $allowed_mimes = bp_attachments_get_allowed_mimes( $type ); |
| | 267 | $is_allowed = wp_check_filetype( $r['image'], $allowed_mimes ); |
| | 268 | |
| | 269 | // It's not an image. |
| | 270 | if ( ! $is_allowed['ext'] ) { |
| | 271 | return false; |
| | 272 | } |
| | 273 | |
| | 274 | // Init the Attachment data |
| | 275 | $attachment_data = array(); |
| | 276 | |
| | 277 | if ( 'avatar' === $type ) { |
| | 278 | // Set crop width for the avatar if not given |
| | 279 | if ( empty( $r['crop_w'] ) ) { |
| | 280 | $r['crop_w'] = bp_core_avatar_full_width(); |
| | 281 | } |
| | 282 | |
| | 283 | // Set crop height for the avatar if not given |
| | 284 | if ( empty( $r['crop_h'] ) ) { |
| | 285 | $r['crop_h'] = bp_core_avatar_full_height(); |
| | 286 | } |
| | 287 | |
| | 288 | if ( is_callable( $r['component'] . '_avatar_upload_dir' ) ) { |
| | 289 | $dir_args = array( $r['item_id'] ); |
| | 290 | |
| | 291 | // In case of xprofile, we need an extra argument |
| | 292 | if ( 'xprofile' === $r['component'] ) { |
| | 293 | $dir_args = array( false, $r['item_id'] ); |
| | 294 | } |
| | 295 | |
| | 296 | $attachment_data = call_user_func_array( $r['component'] . '_avatar_upload_dir', $dir_args ); |
| | 297 | } |
| | 298 | } elseif ( 'cover_image' === $type ) { |
| | 299 | $attachment_data = bp_attachments_uploads_dir_get(); |
| | 300 | |
| | 301 | // Default to members for xProfile |
| | 302 | $object_subdir = 'members'; |
| | 303 | |
| | 304 | if ( 'xprofile' !== $r['component'] ) { |
| | 305 | $object_subdir = sanitize_key( $r['component'] ); |
| | 306 | } |
| | 307 | |
| | 308 | // Set Subdir |
| | 309 | $attachment_data['subdir'] = $object_subdir . '/' . $r['item_id'] . '/cover-image'; |
| | 310 | |
| | 311 | // Set Path |
| | 312 | $attachment_data['path'] = trailingslashit( $attachment_data['basedir'] ) . $attachment_data['subdir']; |
| | 313 | } |
| | 314 | |
| | 315 | if ( ! isset( $attachment_data['path'] ) || ! isset( $attachment_data['subdir'] ) ) { |
| | 316 | return false; |
| | 317 | } |
| | 318 | |
| | 319 | // It's not a regular upload, we may need to create some folders |
| | 320 | if ( ! is_dir( $attachment_data['path'] ) ) { |
| | 321 | if ( ! wp_mkdir_p( $attachment_data['path'] ) ) { |
| | 322 | return false; |
| | 323 | } |
| | 324 | } |
| | 325 | |
| | 326 | // Set the image name and path |
| | 327 | $image_file_name = wp_unique_filename( $attachment_data['path'], basename( $r['image'] ) ); |
| | 328 | $image_file_path = $attachment_data['path'] . '/' . $image_file_name; |
| | 329 | |
| | 330 | // Copy the image file into the avatar dir |
| | 331 | if ( ! copy( $r['image'], $image_file_path ) ) { |
| | 332 | return false; |
| | 333 | } |
| | 334 | |
| | 335 | // Init the response |
| | 336 | $created = false; |
| | 337 | |
| | 338 | // It's an avatar, we need to crop it. |
| | 339 | if ( 'avatar' === $type ) { |
| | 340 | $created = bp_core_avatar_handle_crop( array( |
| | 341 | 'object' => $r['object'], |
| | 342 | 'avatar_dir' => trim( dirname( $attachment_data['subdir'] ), '/' ), |
| | 343 | 'item_id' => (int) $r['item_id'], |
| | 344 | 'original_file' => trailingslashit( $attachment_data['subdir'] ) . $image_file_name, |
| | 345 | 'crop_w' => $r['crop_w'], |
| | 346 | 'crop_h' => $r['crop_h'], |
| | 347 | 'crop_x' => $r['crop_x'], |
| | 348 | 'crop_y' => $r['crop_y'] |
| | 349 | ) ); |
| | 350 | |
| | 351 | // It's a cover image we need to fit it to feature's dimensions |
| | 352 | } elseif ( 'cover_image' === $type ) { |
| | 353 | $cover_image = bp_attachments_cover_image_generate_file( array( |
| | 354 | 'file' => $image_file_path, |
| | 355 | 'component' => $r['component'], |
| | 356 | 'cover_image_dir' => $attachment_data['path'] |
| | 357 | ) ); |
| | 358 | |
| | 359 | $created = ! empty( $cover_image['cover_file'] ); |
| | 360 | } |
| | 361 | |
| | 362 | // Remove copied file if it fails |
| | 363 | if ( ! $created ) { |
| | 364 | @unlink( $image_file_path ); |
| | 365 | } |
| | 366 | |
| | 367 | // Return the response |
| | 368 | return $created; |
| | 369 | } |
| | 370 | |
| | 371 | /** |