Changeset 10356 for trunk/src/bp-core/bp-core-attachments.php
- Timestamp:
- 11/15/2015 07:57:03 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/bp-core-attachments.php
r10193 r10356 17 17 * match with our needs. 18 18 * 19 * @since 19 * @since 2.3.0 20 20 * 21 21 * @return bool True if WordPress is 3.9+, false otherwise. … … 26 26 27 27 /** 28 * Get the Attachments Uploads dir data 29 * 30 * @since 31 * 32 * @param string $data The data to get. Possible values are: 'dir', 'basedir' & 'baseurl'33 * 34 * @return string|array 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 35 */ 36 36 function bp_attachments_uploads_dir_get( $data = '' ) { … … 43 43 $upload_data = bp_upload_dir(); 44 44 45 // Build the Upload data array for BuddyPress attachments 45 // Build the Upload data array for BuddyPress attachments. 46 46 foreach ( $upload_data as $key => $value ) { 47 47 if ( 'basedir' === $key || 'baseurl' === $key ) { … … 52 52 } 53 53 54 // Add the dir to the array 54 // Add the dir to the array. 55 55 $upload_data['dir'] = $attachments_dir; 56 56 … … 65 65 * Filter here to edit the Attachments upload dir data. 66 66 * 67 * @since 68 * 69 * @param string|array $retvalThe needed Upload dir data or the full array of data70 * @param string $dataThe data requested67 * @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 71 */ 72 72 return apply_filters( 'bp_attachments_uploads_dir_get', $retval, $data ); … … 74 74 75 75 /** 76 * Get the max upload file size for any attachment 77 * 78 * @since 79 * 80 * @param 81 * we wish to get the max upload file size for82 * @return int max upload file size for any attachment76 * 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 83 */ 84 84 function bp_attachments_get_max_upload_file_size( $type = '' ) { … … 94 94 * Filter here to edit the max upload file size. 95 95 * 96 * @since 97 * 98 * @param int $fileupload_maxk Max upload file size for any attachment99 * @param string $type The attachment type (eg: 'avatar' or 'cover_image')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 100 */ 101 101 return apply_filters( 'bp_attachments_get_max_upload_file_size', $fileupload_maxk, $type ); … … 103 103 104 104 /** 105 * Get allowed types for any attachment 106 * 107 * @since 108 * 109 * @param string $typeThe extension types to get.110 * Default: 'avatar'111 * @return array The list of allowed extensions for attachments105 * 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 112 */ 113 113 function bp_attachments_get_allowed_types( $type = 'avatar' ) { 114 // Defaults to BuddyPress supported image extensions 114 // Defaults to BuddyPress supported image extensions. 115 115 $exts = array( 'jpeg', 'gif', 'png' ); 116 116 117 117 /** 118 118 * It's not a BuddyPress feature, get the allowed extensions 119 * matching the $type requested 119 * matching the $type requested. 120 120 */ 121 121 if ( 'avatar' !== $type && 'cover_image' !== $type ) { 122 // Reset the default exts 122 // Reset the default exts. 123 123 $exts = array(); 124 124 … … 136 136 137 137 /** 138 * Search for allowed mimes matching the type 138 * Search for allowed mimes matching the type. 139 139 * 140 * eg: using 'application/vnd.oasis' as the $type140 * Eg: using 'application/vnd.oasis' as the $type 141 141 * parameter will get all OpenOffice extensions supported 142 142 * by WordPress and allowed for the current user. … … 148 148 $allowed_types = array_keys( $allowed_mimes ); 149 149 150 // Loop to explode keys using '|' 150 // Loop to explode keys using '|'. 151 151 foreach ( $allowed_types as $allowed_type ) { 152 152 $t = explode( '|', $allowed_type ); … … 160 160 * Filter here to edit the allowed extensions by attachment type. 161 161 * 162 * @since 163 * 164 * @param array $exts List of allowed extensions165 * @param string $type The requested file type162 * @since 2.4.0 163 * 164 * @param array $exts List of allowed extensions. 165 * @param string $type The requested file type. 166 166 */ 167 167 return apply_filters( 'bp_attachments_get_allowed_types', $exts, $type ); … … 173 173 * @since 2.4.0 174 174 * 175 * @param string $typeThe extension types to get (Optional).176 * @param array $allowed_types List of allowed extensions177 * @return array List of allowed mime types175 * @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 178 */ 179 179 function bp_attachments_get_allowed_mimes( $type = '', $allowed_types = array() ) { … … 196 196 197 197 /** 198 * Check the uploaded attachment type is allowed 199 * 200 * @since 201 * 202 * @param 203 * @param 204 * 205 * @param array $allowed_mimes The attachment allowed mimes (Required)206 * @return bool 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 207 */ 208 208 function bp_attachments_check_filetype( $file, $filename, $allowed_mimes ) { … … 221 221 * @since 2.4.0 222 222 * 223 * @param string $typeThe attachment type to create (avatar or cover_image). Default: avatar.224 * @param 223 * @param string $type The attachment type to create (avatar or cover_image). Default: avatar. 224 * @param array $args { 225 225 * @type int $item_id The ID of the object (Required). Default: 0. 226 226 * @type string $object The object type (eg: group, user, blog) (Required). Default: 'user'. … … 232 232 * @type int $crop_y The vertical starting point of the crop. Default: 0. 233 233 * } 234 * @return bool 234 * @return bool True on success, false otherwise. 235 235 */ 236 236 function bp_attachments_create_item_type( $type = 'avatar', $args = array() ) { … … 254 254 } 255 255 256 // Make sure the file path is safe 256 // Make sure the file path is safe. 257 257 if ( 0 !== validate_file( $r['image'] ) ) { 258 258 return false; 259 259 } 260 260 261 // Set the component if not already done 261 // Set the component if not already done. 262 262 if ( empty( $r['component'] ) ) { 263 263 if ( 'user' === $r['object'] ) { … … 277 277 } 278 278 279 // Init the Attachment data 279 // Init the Attachment data. 280 280 $attachment_data = array(); 281 281 282 282 if ( 'avatar' === $type ) { 283 // Set crop width for the avatar if not given 283 // Set crop width for the avatar if not given. 284 284 if ( empty( $r['crop_w'] ) ) { 285 285 $r['crop_w'] = bp_core_avatar_full_width(); 286 286 } 287 287 288 // Set crop height for the avatar if not given 288 // Set crop height for the avatar if not given. 289 289 if ( empty( $r['crop_h'] ) ) { 290 290 $r['crop_h'] = bp_core_avatar_full_height(); … … 294 294 $dir_args = array( $r['item_id'] ); 295 295 296 // In case of xprofile, we need an extra argument 296 // In case of xprofile, we need an extra argument. 297 297 if ( 'xprofile' === $r['component'] ) { 298 298 $dir_args = array( false, $r['item_id'] ); … … 304 304 $attachment_data = bp_attachments_uploads_dir_get(); 305 305 306 // Default to members for xProfile 306 // Default to members for xProfile. 307 307 $object_subdir = 'members'; 308 308 … … 311 311 } 312 312 313 // Set Subdir 313 // Set Subdir. 314 314 $attachment_data['subdir'] = $object_subdir . '/' . $r['item_id'] . '/cover-image'; 315 315 316 // Set Path 316 // Set Path. 317 317 $attachment_data['path'] = trailingslashit( $attachment_data['basedir'] ) . $attachment_data['subdir']; 318 318 } … … 322 322 } 323 323 324 // It's not a regular upload, we may need to create some folders 324 // It's not a regular upload, we may need to create some folders. 325 325 if ( ! is_dir( $attachment_data['path'] ) ) { 326 326 if ( ! wp_mkdir_p( $attachment_data['path'] ) ) { … … 329 329 } 330 330 331 // Set the image name and path 331 // Set the image name and path. 332 332 $image_file_name = wp_unique_filename( $attachment_data['path'], basename( $r['image'] ) ); 333 333 $image_file_path = $attachment_data['path'] . '/' . $image_file_name; 334 334 335 // Copy the image file into the avatar dir 335 // Copy the image file into the avatar dir. 336 336 if ( ! copy( $r['image'], $image_file_path ) ) { 337 337 return false; 338 338 } 339 339 340 // Init the response 340 // Init the response. 341 341 $created = false; 342 342 … … 354 354 ) ); 355 355 356 // It's a cover image we need to fit it to feature's dimensions 356 // It's a cover image we need to fit it to feature's dimensions. 357 357 } elseif ( 'cover_image' === $type ) { 358 358 $cover_image = bp_attachments_cover_image_generate_file( array( … … 365 365 } 366 366 367 // Remove copied file if it fails 367 // Remove copied file if it fails. 368 368 if ( ! $created ) { 369 369 @unlink( $image_file_path ); 370 370 } 371 371 372 // Return the response 372 // Return the response. 373 373 return $created; 374 374 } 375 375 376 376 /** 377 * Get the url or the path for a type of attachment 378 * 379 * @since 380 * 381 * @param string $data whether to get the url or the path382 * @param 377 * Get the url or the path for a type of attachment. 378 * 379 * @since 2.4.0 380 * 381 * @param string $data whether to get the url or the path. 382 * @param array $args { 383 383 * @type string $object_dir The object dir (eg: members/groups). Defaults to members. 384 384 * @type int $item_id The object id (eg: a user or a group id). Defaults to current user. … … 387 387 * @type string $file The name of the file. 388 388 * } 389 * @return string|bool the url or the path to the attachment, false otherwise389 * @return string|bool The url or the path to the attachment, false otherwise 390 390 */ 391 391 function bp_attachments_get_attachment( $data = 'url', $args = array() ) { 392 // Default value 392 // Default value. 393 393 $attachment_data = false; 394 394 … … 400 400 ), 'attachments_get_attachment_src' ); 401 401 402 // Get BuddyPress Attachments Uploads Dir datas 402 // Get BuddyPress Attachments Uploads Dir datas. 403 403 $bp_attachments_uploads_dir = bp_attachments_uploads_dir_get(); 404 404 … … 424 424 $file = false; 425 425 426 // Open the directory and get the first file 426 // Open the directory and get the first file. 427 427 if ( $att_dir = opendir( $type_dir ) ) { 428 428 429 429 while ( false !== ( $attachment_file = readdir( $att_dir ) ) ) { 430 // Look for the first file having the type in its name 430 // Look for the first file having the type in its name. 431 431 if ( false !== strpos( $attachment_file, $r['type'] ) && empty( $file ) ) { 432 432 $file = $attachment_file; … … 453 453 * Delete an attachment for the given arguments 454 454 * 455 * @since 2.4.0 456 * 457 * @param array $args 458 * @see bp_attachments_get_attachment() For more information on accepted arguments. 459 * @return bool True if the attachment was deleted, false otherwise 455 * @since 2.4.0 456 * 457 * @see bp_attachments_get_attachment() For more information on accepted arguments. 458 * 459 * @param array $args Array of arguments for the attachment deletion. 460 * @return bool True if the attachment was deleted, false otherwise. 460 461 */ 461 462 function bp_attachments_delete_file( $args = array() ) { … … 473 474 * Get the BuddyPress Plupload settings. 474 475 * 475 * @since 476 * 477 * @return array list of BuddyPress Plupload settings.476 * @since 2.3.0 477 * 478 * @return array List of BuddyPress Plupload settings. 478 479 */ 479 480 function bp_attachments_get_plupload_default_settings() { … … 523 524 * @since 2.3.0 524 525 * 525 * @param array $ params Default Plupload parameters array.526 * @param array $settings Default Plupload parameters array. 526 527 */ 527 528 return apply_filters( 'bp_attachments_get_plupload_default_settings', $settings ); … … 531 532 * Builds localization strings for the BuddyPress Uploader scripts. 532 533 * 533 * @since 534 * @since 2.3.0 534 535 * 535 536 * @return array Plupload default localization strings. 536 537 */ 537 538 function bp_attachments_get_plupload_l10n() { 538 // Localization strings 539 // Localization strings. 539 540 return apply_filters( 'bp_attachments_get_plupload_l10n', array( 540 541 'queue_limit_exceeded' => __( 'You have attempted to queue too many files.', 'buddypress' ), … … 567 568 * Enqueues the script needed for the Uploader UI. 568 569 * 569 * @see 570 * @see BP_Attachment::script_data() && BP_Attachment_Avatar::script_data() for examples showing how 570 571 * to set specific script data. 571 572 * 572 * @since 2.3.0 573 * 574 * @param string $class name of the class extending BP_Attachment (eg: BP_Attachment_Avatar). 575 * 573 * @since 2.3.0 574 * 575 * @param string $class Name of the class extending BP_Attachment (eg: BP_Attachment_Avatar). 576 576 * @return null|WP_Error 577 577 */ … … 586 586 } 587 587 588 // Get an instance of the class and get the script data 588 // Get an instance of the class and get the script data. 589 589 $attachment = new $class; 590 590 $script_data = $attachment->script_data(); … … 607 607 } 608 608 609 // Get the BuddyPress uploader strings 609 // Get the BuddyPress uploader strings. 610 610 $strings = bp_attachments_get_plupload_l10n(); 611 611 612 // Get the BuddyPress uploader settings 612 // Get the BuddyPress uploader settings. 613 613 $settings = bp_attachments_get_plupload_default_settings(); 614 614 615 // Set feedback messages 615 // Set feedback messages. 616 616 if ( ! empty( $args['feedback_messages'] ) ) { 617 617 $strings['feedback_messages'] = $args['feedback_messages']; 618 618 } 619 619 620 // Use a temporary var to ease manipulation 620 // Use a temporary var to ease manipulation. 621 621 $defaults = $settings['defaults']; 622 622 623 // Set the upload action 623 // Set the upload action. 624 624 $defaults['multipart_params']['action'] = $args['action']; 625 625 626 // Set BuddyPress upload parameters if provided 626 // Set BuddyPress upload parameters if provided. 627 627 if ( ! empty( $args['bp_params'] ) ) { 628 628 $defaults['multipart_params']['bp_params'] = $args['bp_params']; 629 629 } 630 630 631 // Merge other arguments 631 // Merge other arguments. 632 632 $ui_args = array_intersect_key( $args, array( 633 633 'file_data_name' => true, … … 643 643 } 644 644 645 // Specific to BuddyPress Avatars 645 // Specific to BuddyPress Avatars. 646 646 if ( 'bp_avatar_upload' === $defaults['multipart_params']['action'] ) { 647 647 648 // Include the cropping informations for avatars 648 // Include the cropping informations for avatars. 649 649 $settings['crop'] = array( 650 650 'full_h' => bp_core_avatar_full_height(), … … 655 655 $defaults['multi_selection'] = false; 656 656 657 // Does the object already has an avatar set 657 // Does the object already has an avatar set. 658 658 $has_avatar = $defaults['multipart_params']['bp_params']['has_avatar']; 659 659 660 // What is the object the avatar belongs to 660 // What is the object the avatar belongs to. 661 661 $object = $defaults['multipart_params']['bp_params']['object']; 662 662 663 // Init the Avatar nav 663 // Init the Avatar nav. 664 664 $avatar_nav = array( 665 665 'upload' => array( 'id' => 'upload', 'caption' => __( 'Upload', 'buddypress' ), 'order' => 0 ), 666 666 667 // The delete view will only show if the object has an avatar 667 // The delete view will only show if the object has an avatar. 668 668 'delete' => array( 'id' => 'delete', 'caption' => __( 'Delete', 'buddypress' ), 'order' => 100, 'hide' => (int) ! $has_avatar ), 669 669 ); 670 670 671 // Create the Camera Nav if the WebCam capture feature is enabled 671 // Create the Camera Nav if the WebCam capture feature is enabled. 672 672 if ( bp_avatar_use_webcam() && 'user' === $object ) { 673 673 $avatar_nav['camera'] = array( 'id' => 'camera', 'caption' => __( 'Take Photo', 'buddypress' ), 'order' => 10 ); 674 674 675 // Set warning messages 675 // Set warning messages. 676 676 $strings['camera_warnings'] = array( 677 677 'requesting' => __( 'Please allow us to access to your camera.', 'buddypress'), … … 691 691 * @since 2.3.0 692 692 * 693 * @param array $avatar_nav An associative array of available nav items where each item is an array organized this way: 694 * $avatar_nav[ $nav_item_id ] { 693 * @param array $avatar_nav { 694 * An associative array of available nav items where each item is an array organized this way: 695 * $avatar_nav[ $nav_item_id ]. 695 696 * @type string $nav_item_id The nav item id in lower case without special characters or space. 696 697 * @type string $caption The name of the item nav that will be displayed in the nav. … … 700 701 * (only used for the delete nav item). 701 702 * } 702 * @param string $object the object the avatar belongs to (eg: user or group)703 * @param string $object The object the avatar belongs to (eg: user or group). 703 704 */ 704 705 $settings['nav'] = bp_sort_by_key( apply_filters( 'bp_attachments_avatar_nav', $avatar_nav, $object ), 'order', 'num' ); 705 706 706 // Specific to BuddyPress cover images 707 // Specific to BuddyPress cover images. 707 708 } elseif ( 'bp_cover_image_upload' === $defaults['multipart_params']['action'] ) { 708 709 … … 710 711 $defaults['multi_selection'] = false; 711 712 712 // Default cover component is xprofile 713 // Default cover component is xprofile. 713 714 $cover_component = 'xprofile'; 714 715 715 // Get the object we're editing the cover image of 716 // Get the object we're editing the cover image of. 716 717 $object = $defaults['multipart_params']['bp_params']['object']; 717 718 718 // Set the cover component according to the object 719 // Set the cover component according to the object. 719 720 if ( 'group' === $object ) { 720 721 $cover_component = 'groups'; … … 722 723 $cover_component = apply_filters( 'bp_attachments_cover_image_ui_component', $cover_component ); 723 724 } 724 // Get cover image advised dimensions 725 // Get cover image advised dimensions. 725 726 $cover_dimensions = bp_attachments_get_cover_image_dimensions( $cover_component ); 726 727 727 // Set warning messages 728 // Set warning messages. 728 729 $strings['cover_image_warnings'] = apply_filters( 'bp_attachments_cover_image_ui_warnings', array( 729 730 'dimensions' => sprintf( … … 735 736 } 736 737 737 // Set Plupload settings 738 // Set Plupload settings. 738 739 $settings['defaults'] = $defaults; 739 740 … … 783 784 * Check the current user's capability to edit an avatar for a given object. 784 785 * 785 * @since 2.3.0 786 * 787 * @param string $capability The capability to check. 788 * @param array $args An array containing the item_id and the object to check. 789 * 786 * @since 2.3.0 787 * 788 * @param string $capability The capability to check. 789 * @param array $args An array containing the item_id and the object to check. 790 790 * @return bool 791 791 */ … … 798 798 */ 799 799 if ( isset( $args['item_id'] ) && isset( $args['object'] ) ) { 800 // Group profile photo 800 // Group profile photo. 801 801 if ( bp_is_active( 'groups' ) && 'group' === $args['object'] ) { 802 802 if ( bp_is_group_create() ) { … … 805 805 $can = (bool) groups_is_user_admin( bp_loggedin_user_id(), $args['item_id'] ) || bp_current_user_can( 'bp_moderate' ); 806 806 } 807 // User profile photo 807 // User profile photo. 808 808 } elseif ( bp_is_active( 'xprofile' ) && 'user' === $args['object'] ) { 809 809 $can = bp_loggedin_user_id() === (int) $args['item_id'] || bp_current_user_can( 'bp_moderate' ); … … 828 828 * Send a JSON response back to an Ajax upload request. 829 829 * 830 * @since 831 * 832 * @param 833 * @param 834 * @param 830 * @since 2.3.0 831 * 832 * @param bool $success True for a success, false otherwise. 833 * @param bool $is_html4 True if the Plupload runtime used is html4, false otherwise. 834 * @param mixed $data Data to encode as JSON, then print and die. 835 835 */ 836 836 function bp_attachments_json_response( $success, $is_html4 = false, $data = null ) { … … 841 841 } 842 842 843 // Send regular json response 843 // Send regular json response. 844 844 if ( ! $is_html4 ) { 845 845 wp_send_json( $response ); … … 863 863 * 864 864 * @param string $slug Template part slug. eg 'uploader' for 'uploader.php'. 865 *866 865 * @return bool 867 866 */ … … 892 891 * Get the cover image settings 893 892 * 894 * @since 895 * 896 * @param string $component the component to get the settings for ("xprofile" for user or "groups")897 * @return array the cover image settings893 * @since 2.4.0 894 * 895 * @param string $component The component to get the settings for ("xprofile" for user or "groups"). 896 * @return array The cover image settings. 898 897 */ 899 898 function bp_attachments_get_cover_image_settings( $component = 'xprofile' ) { 900 // Default parameters 899 // Default parameters. 901 900 $args = array(); 902 901 903 // First look in BP Theme Compat 902 // First look in BP Theme Compat. 904 903 $cover_image = bp_get_theme_compat_feature( 'cover_image' ); 905 904 … … 911 910 * Then let people override/set the feature using this dynamic filter 912 911 * 913 * eg: for the user's profile cover image use:912 * Eg: for the user's profile cover image use: 914 913 * add_filter( 'bp_before_xprofile_cover_image_settings_parse_args', 'your_filter', 10, 1 ); 915 914 * 916 915 * @since 2.4.0 917 916 * 918 * @param array $settings the cover image settings917 * @param array $settings The cover image settings 919 918 */ 920 919 $settings = bp_parse_args( $args, array( … … 931 930 } 932 931 933 // Current component is not supported 932 // Current component is not supported. 934 933 if ( ! in_array( $component, $settings['components'] ) ) { 935 934 return false; 936 935 } 937 936 938 // Finally return the settings 937 // Finally return the settings. 939 938 return $settings; 940 939 } … … 943 942 * Get cover image Width and Height 944 943 * 945 * @since 946 * 947 * @param string $component the BuddyPress component concerned ("xprofile" for user or "groups")948 * @return array an associative array containing the advised width and height for the cover image944 * @since 2.4.0 945 * 946 * @param string $component The BuddyPress component concerned ("xprofile" for user or "groups"). 947 * @return array An associative array containing the advised width and height for the cover image. 949 948 */ 950 949 function bp_attachments_get_cover_image_dimensions( $component = 'xprofile' ) { 951 // Let's prevent notices when setting the warning strings 950 // Let's prevent notices when setting the warning strings. 952 951 $default = array( 'width' => 0, 'height' => 0 ); 953 952 … … 958 957 } 959 958 960 // Get width and height 959 // Get width and height. 961 960 $wh = array_intersect_key( $settings, $default ); 962 961 … … 964 963 * Filter here to edit the cover image dimensions if needed. 965 964 * 966 * @since 967 * 968 * @param array $wh an associative array containing the width and height values969 * @param array $settings an associative array containing all the feature settings970 * @param string $compnent the requested component965 * @since 2.4.0 966 * 967 * @param array $wh An associative array containing the width and height values. 968 * @param array $settings An associative array containing all the feature settings. 969 * @param string $compnent The requested component. 971 970 */ 972 971 return apply_filters( 'bp_attachments_get_cover_image_dimensions', $wh, $settings, $component ); … … 974 973 975 974 /** 976 * Are we on a page to edit a cover image 977 * 978 * @since 979 * 980 * @return bool True if on a page to edit a cover image, false otherwise 975 * Are we on a page to edit a cover image? 976 * 977 * @since 2.4.0 978 * 979 * @return bool True if on a page to edit a cover image, false otherwise. 981 980 */ 982 981 function bp_attachments_cover_image_is_edit() { … … 1005 1004 1006 1005 /** 1007 * Does the user has a cover image 1008 * 1009 * @since 1010 * 1011 * @param int $user_id1012 * @return bool True if the user has a cover image, false otherwise 1006 * Does the user has a cover image? 1007 * 1008 * @since 2.4.0 1009 * 1010 * @param int $user_id User ID to retrieve cover image for. 1011 * @return bool True if the user has a cover image, false otherwise. 1013 1012 */ 1014 1013 function bp_attachments_get_user_has_cover_image( $user_id = 0 ) { … … 1025 1024 1026 1025 /** 1027 * Does the group has a cover image 1028 * 1029 * @since 1030 * 1031 * @param int $group_id1032 * @return bool True if the group has a cover image, false otherwise 1026 * Does the group has a cover image? 1027 * 1028 * @since 2.4.0 1029 * 1030 * @param int $group_id Group ID to check cover image existence for. 1031 * @return bool True if the group has a cover image, false otherwise. 1033 1032 */ 1034 1033 function bp_attachments_get_group_has_cover_image( $group_id = 0 ) { … … 1050 1049 * @since 2.4.0 1051 1050 * 1052 * @param array$args {1051 * @param array $args { 1053 1052 * @type string $file The absolute path to the image. Required. 1054 1053 * @type string $component The component for the object (eg: groups, xprofile). Required. 1055 1054 * @type string $cover_image_dir The Cover image dir to write the image into. Required. 1056 1055 * } 1057 * @param 1058 * @return bool|array 1056 * @param BP_Attachment_Cover_Image $cover_image_class The class to use to fit the cover image. 1057 * @return bool|array An array containing cover image data on success, false otherwise. 1059 1058 */ 1060 1059 function bp_attachments_cover_image_generate_file( $args = array(), $cover_image_class = null ) { 1061 // Bail if an argument is missing 1060 // Bail if an argument is missing. 1062 1061 if ( empty( $args['file'] ) || empty( $args['component'] ) || empty( $args['cover_image_dir'] ) ) { 1063 1062 return false; 1064 1063 } 1065 1064 1066 // Get advised dimensions for the cover image 1065 // Get advised dimensions for the cover image. 1067 1066 $dimensions = bp_attachments_get_cover_image_dimensions( $args['component'] ); 1068 1067 … … 1081 1080 } 1082 1081 1083 // Resize the image so that it fit with the cover image dimensions 1082 // Resize the image so that it fit with the cover image dimensions. 1084 1083 $cover_image = $cover_image_class->fit( $args['file'], $dimensions ); 1085 1084 $is_too_small = false; 1086 1085 1087 // Image is too small in width and height 1086 // Image is too small in width and height. 1088 1087 if ( empty( $cover_image ) ) { 1089 1088 $cover_file = $cover_image_class->generate_filename( $args['file'] ); … … 1095 1094 $cover_file = $cover_image['path']; 1096 1095 1097 // Image is too small in width or height 1096 // Image is too small in width or height. 1098 1097 if ( $cover_image['width'] < $dimensions['width'] || $cover_image['height'] < $dimensions['height'] ) { 1099 1098 $is_too_small = true; … … 1111 1110 if ( $att_dir = opendir( $args['cover_image_dir'] ) ) { 1112 1111 while ( false !== ( $attachment_file = readdir( $att_dir ) ) ) { 1113 // skip directories and the new cover image1112 // Skip directories and the new cover image. 1114 1113 if ( 2 < strlen( $attachment_file ) && 0 !== strpos( $attachment_file, '.' ) && $cover_basename !== $attachment_file ) { 1115 1114 @unlink( $args['cover_image_dir'] . '/' . $attachment_file ); … … 1129 1128 * Ajax Upload and set a cover image 1130 1129 * 1131 * @since 1132 * 1133 * @return 1134 * 1130 * @since 2.4.0 1131 * 1132 * @return string|null A json object containing success data if the upload succeeded 1133 * error message otherwise. 1135 1134 */ 1136 1135 function bp_attachments_cover_image_ajax_upload() { 1137 // Bail if not a POST action 1136 // Bail if not a POST action. 1138 1137 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { 1139 1138 wp_die(); … … 1149 1148 } 1150 1149 1151 // Check the nonce 1150 // Check the nonce. 1152 1151 check_admin_referer( 'bp-uploader' ); 1153 1152 1154 // Init the BuddyPress parameters 1153 // Init the BuddyPress parameters. 1155 1154 $bp_params = array(); 1156 1155 1157 // We need it to carry on 1156 // We need it to carry on. 1158 1157 if ( ! empty( $_POST['bp_params'] ) ) { 1159 1158 $bp_params = bp_parse_args( $_POST['bp_params'], array( … … 1165 1164 } 1166 1165 1167 // We need the object to set the uploads dir filter 1166 // We need the object to set the uploads dir filter. 1168 1167 if ( empty( $bp_params['object'] ) ) { 1169 1168 bp_attachments_json_response( false, $is_html4 ); 1170 1169 } 1171 1170 1172 // Capability check 1171 // Capability check. 1173 1172 if ( ! bp_attachments_current_user_can( 'edit_cover_image', $bp_params ) ) { 1174 1173 bp_attachments_json_response( false, $is_html4 ); … … 1178 1177 $needs_reset = array(); 1179 1178 1180 // Member's cover image 1179 // Member's cover image. 1181 1180 if ( 'user' === $bp_params['object'] ) { 1182 1181 $object_data = array( 'dir' => 'members', 'component' => 'xprofile' ); … … 1187 1186 } 1188 1187 1189 // Group's cover image 1188 // Group's cover image. 1190 1189 } elseif ( 'group' === $bp_params['object'] ) { 1191 1190 $object_data = array( 'dir' => 'groups', 'component' => 'groups' ); … … 1199 1198 } 1200 1199 1201 // Other object's cover image 1200 // Other object's cover image. 1202 1201 } else { 1203 1202 $object_data = apply_filters( 'bp_attachments_cover_image_object_dir', array(), $bp_params['object'] ); 1204 1203 } 1205 1204 1206 // Stop here in case of a missing parameter for the object 1205 // Stop here in case of a missing parameter for the object. 1207 1206 if ( empty( $object_data['dir'] ) || empty( $object_data['component'] ) ) { 1208 1207 bp_attachments_json_response( false, $is_html4 ); … … 1212 1211 $uploaded = $cover_image_attachment->upload( $_FILES ); 1213 1212 1214 // Reset objects 1213 // Reset objects. 1215 1214 if ( ! empty( $needs_reset ) ) { 1216 1215 if ( ! empty( $needs_reset['component'] ) ) { … … 1222 1221 1223 1222 if ( ! empty( $uploaded['error'] ) ) { 1224 // Upload error response 1223 // Upload error response. 1225 1224 bp_attachments_json_response( false, $is_html4, array( 1226 1225 'type' => 'upload_error', … … 1229 1228 } 1230 1229 1231 // Default error message 1230 // Default error message. 1232 1231 $error_message = __( 'There was a problem uploading the cover image.', 'buddypress' ); 1233 1232 1234 // Get BuddyPress Attachments Uploads Dir datas 1233 // Get BuddyPress Attachments Uploads Dir datas. 1235 1234 $bp_attachments_uploads_dir = bp_attachments_uploads_dir_get(); 1236 1235 … … 1239 1238 1240 1239 if ( ! is_dir( $cover_dir ) ) { 1241 // Upload error response 1240 // Upload error response. 1242 1241 bp_attachments_json_response( false, $is_html4, array( 1243 1242 'type' => 'upload_error', … … 1260 1259 1261 1260 if ( ! $cover ) { 1262 // Upload error response 1261 // Upload error response. 1263 1262 bp_attachments_json_response( false, $is_html4, array( 1264 1263 'type' => 'upload_error', … … 1267 1266 } 1268 1267 1269 // Build the url to the file 1268 // Build the url to the file. 1270 1269 $cover_url = trailingslashit( $bp_attachments_uploads_dir['baseurl'] ) . $cover_subdir . '/' . $cover['cover_basename']; 1271 1270 1272 // Init Feedback code, 1 is success 1271 // Init Feedback code, 1 is success. 1273 1272 $feedback_code = 1; 1274 1273 1275 // 0 is the size warning 1274 // 0 is the size warning. 1276 1275 if ( $cover['is_too_small'] ) { 1277 1276 $feedback_code = 0; 1278 1277 } 1279 1278 1280 // Set the name of the file 1279 // Set the name of the file. 1281 1280 $name = $_FILES['file']['name']; 1282 1281 $name_parts = pathinfo( $name ); … … 1297 1296 do_action( $object_data['component'] . '_cover_image_uploaded', (int) $bp_params['item_id'] ); 1298 1297 1299 // Finally return the cover image url to the UI 1298 // Finally return the cover image url to the UI. 1300 1299 bp_attachments_json_response( true, $is_html4, array( 1301 1300 'name' => $name, … … 1326 1325 } 1327 1326 1328 // Check the nonce 1327 // Check the nonce. 1329 1328 check_admin_referer( 'bp_delete_cover_image', 'nonce' ); 1330 1329 1331 // Capability check 1330 // Capability check. 1332 1331 if ( ! bp_attachments_current_user_can( 'edit_cover_image', $cover_image_data ) ) { 1333 1332 wp_send_json_error(); 1334 1333 } 1335 1334 1336 // Set object for the user's case 1335 // Set object for the user's case. 1337 1336 if ( 'user' === $cover_image_data['object'] ) { 1338 1337 $component = 'xprofile'; 1339 1338 $dir = 'members'; 1340 1339 1341 // Set it for any other cases 1340 // Set it for any other cases. 1342 1341 } else { 1343 1342 $component = $cover_image_data['object'] . 's'; … … 1345 1344 } 1346 1345 1347 // Handle delete 1346 // Handle delete. 1348 1347 if ( bp_attachments_delete_file( array( 'item_id' => $cover_image_data['item_id'], 'object_dir' => $dir, 'type' => 'cover-image' ) ) ) { 1349 1348 1350 // Defaults no cover image 1349 // Defaults no cover image. 1351 1350 $response = array( 1352 1351 'reset_url' => '', … … 1354 1353 ); 1355 1354 1356 // Get cover image settings in case there's a default header 1355 // Get cover image settings in case there's a default header. 1357 1356 $cover_params = bp_attachments_get_cover_image_settings( $component ); 1358 1357 1359 // Check if there's a default cover 1358 // Check if there's a default cover. 1360 1359 if ( ! empty( $cover_params['default_cover'] ) ) { 1361 1360 $response['reset_url'] = $cover_params['default_cover']; 1362 1361 } 1363 1362 1364 // Finally send the reset url 1363 // Finally send the reset url. 1365 1364 wp_send_json_success( $response ); 1366 1365
Note: See TracChangeset
for help on using the changeset viewer.