Changeset 10154 for trunk/src/bp-core/bp-core-attachments.php
- Timestamp:
- 09/29/2015 10:23:27 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/bp-core-attachments.php
r10151 r10154 545 545 */ 546 546 $settings['nav'] = bp_sort_by_key( apply_filters( 'bp_attachments_avatar_nav', $avatar_nav, $object ), 'order', 'num' ); 547 548 // Specific to BuddyPress cover images 549 } elseif ( 'bp_cover_image_upload' === $defaults['multipart_params']['action'] ) { 550 551 // Cover images only need 1 file and 1 only! 552 $defaults['multi_selection'] = false; 553 554 // Default cover component is xprofile 555 $cover_component = 'xprofile'; 556 557 // Get the object we're editing the cover image of 558 $object = $defaults['multipart_params']['bp_params']['object']; 559 560 // Set the cover component according to the object 561 if ( 'group' === $object ) { 562 $cover_component = 'groups'; 563 } elseif ( 'user' !== $object ) { 564 $cover_component = apply_filters( 'bp_attachments_cover_image_ui_component', $cover_component ); 565 } 566 // Get cover image advised dimensions 567 $cover_dimensions = bp_attachments_get_cover_image_dimensions( $cover_component ); 568 569 // Set warning messages 570 $strings['cover_image_warnings'] = apply_filters( 'bp_attachments_cover_image_ui_warnings', array( 571 'dimensions' => sprintf( 572 __( 'For better results, make sure to upload an image that is larger than %1$spx wide, and %2$spx tall.', 'buddypress' ), 573 (int) $cover_dimensions['width'], 574 (int) $cover_dimensions['height'] 575 ), 576 ) ); 547 577 } 548 578 … … 605 635 $can = false; 606 636 607 if ( 'edit_avatar' === $capability ) {637 if ( 'edit_avatar' === $capability || 'edit_cover_image' === $capability ) { 608 638 /** 609 639 * Needed avatar arguments are set. … … 698 728 } 699 729 } 730 731 /** Cover Image ***************************************************************/ 732 733 /** 734 * Get the cover image settings 735 * 736 * @since 2.4.0 737 * 738 * @param string $component the component to get the settings for ("xprofile" for user or "groups") 739 * @return array the cover image settings 740 */ 741 function bp_attachments_get_cover_image_settings( $component = 'xprofile' ) { 742 // Default parameters 743 $args = array(); 744 745 // First look in BP Theme Compat 746 $cover_image = bp_get_theme_compat_feature( 'cover_image' ); 747 748 if ( ! empty( $cover_image ) ) { 749 $args = (array) $cover_image; 750 } 751 752 /** 753 * Then let people override/set the feature using this dynamic filter 754 * 755 * eg: for the user's profile cover image use : 756 * add_filter( 'bp_before_xprofile_cover_image_settings_parse_args', 'your_filter', 10, 1 ); 757 * 758 * @since 2.4.0 759 * 760 * @param array $settings the cover image settings 761 */ 762 $settings = bp_parse_args( $args, array( 763 'components' => array(), 764 'width' => 1300, 765 'height' => 225, 766 'callback' => '', 767 'theme_handle' => '', 768 'default_cover' => '', 769 ), $component . '_cover_image_settings' ); 770 771 if ( empty( $settings['components'] ) || empty( $settings['callback'] ) || empty( $settings['theme_handle'] ) ) { 772 return false; 773 } 774 775 // Current component is not supported 776 if ( ! in_array( $component, $settings['components'] ) ) { 777 return false; 778 } 779 780 // Finally return the settings 781 return $settings; 782 } 783 784 /** 785 * Get cover image Width and Height 786 * 787 * @since 2.4.0 788 * 789 * @param string $component the BuddyPress component concerned ("xprofile" for user or "groups") 790 * @return array an associative array containing the advised width and height for the cover image 791 */ 792 function bp_attachments_get_cover_image_dimensions( $component = 'xprofile' ) { 793 // Let's prevent notices when setting the warning strings 794 $default = array( 'width' => 0, 'height' => 0 ); 795 796 $settings = bp_attachments_get_cover_image_settings( $component ); 797 798 if ( empty( $settings ) ) { 799 return false; 800 } 801 802 // Get width and height 803 $wh = array_intersect_key( $settings, $default ); 804 805 /** 806 * Filter here to edit the cover image dimensions if needed. 807 * 808 * @since 2.4.0 809 * 810 * @param array $wh an associative array containing the width and height values 811 * @param array $settings an associative array containing all the feature settings 812 * @param string $compnent the requested component 813 */ 814 return apply_filters( 'bp_attachments_get_cover_image_dimensions', $wh, $settings, $component ); 815 } 816 817 /** 818 * Are we on a page to edit a cover image ? 819 * 820 * @since 2.4.0 821 * 822 * @return bool True if on a page to edit a cover image, false otherwise 823 */ 824 function bp_attachments_cover_image_is_edit() { 825 $retval = false; 826 827 $current_component = bp_current_component(); 828 if ( 'profile' === $current_component ) { 829 $current_component = 'xprofile'; 830 } 831 832 if ( ! bp_is_active( $current_component, 'cover_image' ) ) { 833 return $retval; 834 } 835 836 if ( bp_is_user_change_cover_image() ) { 837 $retval = ! bp_disable_cover_image_uploads(); 838 } 839 840 if ( ( bp_is_group_admin_page() && 'group-cover-image' == bp_get_group_current_admin_tab() ) 841 || ( bp_is_group_create() && bp_is_group_creation_step( 'group-cover-image' ) ) ) { 842 $retval = ! bp_disable_group_cover_image_uploads(); 843 } 844 845 return apply_filters( 'bp_attachments_cover_image_is_edit', $retval, $current_component ); 846 } 847 848 /** 849 * Does the user has a cover image ? 850 * 851 * @since 2.4.0 852 * 853 * @param int $user_id 854 * @return bool True if the user has a cover image, false otherwise 855 */ 856 function bp_attachments_get_user_has_cover_image( $user_id = 0 ) { 857 if ( empty( $user_id ) ) { 858 $user_id = bp_displayed_user_id(); 859 } 860 861 $cover_src = bp_attachments_get_attachment( 'url', array( 862 'item_id' => $user_id, 863 ) ); 864 865 return (bool) apply_filters( 'bp_attachments_get_user_has_cover_image', $cover_src, $user_id ); 866 } 867 868 /** 869 * Does the group has a cover image ? 870 * 871 * @since 2.4.0 872 * 873 * @param int $group_id 874 * @return bool True if the group has a cover image, false otherwise 875 */ 876 function bp_attachments_get_group_has_cover_image( $group_id = 0 ) { 877 if ( empty( $group_id ) ) { 878 $group_id = bp_get_current_group_id(); 879 } 880 881 $cover_src = bp_attachments_get_attachment( 'url', array( 882 'object_dir' => 'groups', 883 'item_id' => $group_id, 884 ) ); 885 886 return (bool) apply_filters( 'bp_attachments_get_user_has_cover_image', $cover_src, $group_id ); 887 } 888 889 /** 890 * Ajax Upload and set a cover image 891 * 892 * @since 2.4.0 893 * 894 * @return string|null A json object containing success data if the upload succeeded 895 * error message otherwise. 896 */ 897 function bp_attachments_cover_image_ajax_upload() { 898 // Bail if not a POST action 899 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { 900 wp_die(); 901 } 902 903 /** 904 * Sending the json response will be different if 905 * the current Plupload runtime is html4 906 */ 907 $is_html4 = false; 908 if ( ! empty( $_POST['html4' ] ) ) { 909 $is_html4 = true; 910 } 911 912 // Check the nonce 913 check_admin_referer( 'bp-uploader' ); 914 915 // Init the BuddyPress parameters 916 $bp_params = array(); 917 918 // We need it to carry on 919 if ( ! empty( $_POST['bp_params' ] ) ) { 920 $bp_params = bp_parse_args( $_POST['bp_params' ], array( 921 'object' => 'user', 922 'item_id' => bp_loggedin_user_id(), 923 ), 'attachments_cover_image_ajax_upload' ); 924 } else { 925 bp_attachments_json_response( false, $is_html4 ); 926 } 927 928 // We need the object to set the uploads dir filter 929 if ( empty( $bp_params['object'] ) ) { 930 bp_attachments_json_response( false, $is_html4 ); 931 } 932 933 // Capability check 934 if ( ! bp_attachments_current_user_can( 'edit_cover_image', $bp_params ) ) { 935 bp_attachments_json_response( false, $is_html4 ); 936 } 937 938 $bp = buddypress(); 939 $needs_reset = array(); 940 941 // Default object data 942 $object_data = array( 'dir' => 'members', 'component' => 'xprofile' ); 943 if ( 'group' === $bp_params['object'] ) { 944 $object_data = array( 'dir' => 'groups', 'component' => 'groups' ); 945 946 if ( ! bp_get_current_group_id() && ! empty( $bp_params['item_id'] ) ) { 947 $needs_reset = array( 'component' => 'groups', 'key' => 'current_group', 'value' => $bp->groups->current_group ); 948 $bp->groups->current_group = groups_get_group( array( 949 'group_id' => $bp_params['item_id'], 950 'populate_extras' => false, 951 ) ); 952 } 953 } elseif ( 'user' !== $bp_params['object'] ) { 954 $object_data = apply_filters( 'bp_attachments_cover_image_object_dir', $object_data, $bp_params['object'] ); 955 956 if ( ! bp_displayed_user_id() && ! empty( $bp_params['item_id'] ) ) { 957 $needs_reset = array( 'key' => 'displayed_user', 'value' => $bp->displayed_user ); 958 $bp->displayed_user->id = $bp_params['item_id']; 959 } 960 } 961 962 $cover_image_attachment = new BP_Attachment_Cover_Image(); 963 $uploaded = $cover_image_attachment->upload( $_FILES ); 964 965 // Reset objects 966 if ( ! empty( $needs_reset ) ) { 967 if ( ! empty( $needs_reset['component'] ) ) { 968 $bp->{$needs_reset['component']}->{$needs_reset['key']} = $needs_reset['value']; 969 } else { 970 $bp->{$needs_reset['key']} = $needs_reset['value']; 971 } 972 } 973 974 if ( ! empty( $uploaded['error'] ) ) { 975 // Upload error response 976 bp_attachments_json_response( false, $is_html4, array( 977 'type' => 'upload_error', 978 'message' => sprintf( __( 'Upload Failed! Error was: %s', 'buddypress' ), $uploaded['error'] ), 979 ) ); 980 } 981 982 // Get advised dimensions for the cover image 983 $dimensions = bp_attachments_get_cover_image_dimensions( $object_data['component'] ); 984 985 // Resize the image so that it fit with the cover image dimensions 986 $cover_image = $cover_image_attachment->fit( $uploaded['file'], $dimensions ); 987 $is_too_small = false; 988 989 // Image is too small in width and height 990 if ( empty( $cover_image ) ) { 991 $cover_file = $cover_image_attachment->generate_filename( $uploaded['file'] ); 992 @rename( $uploaded['file'], $cover_file ); 993 994 // It's too small! 995 $is_too_small = true; 996 } elseif ( ! empty( $cover_image['path'] ) ) { 997 $cover_file = $cover_image['path']; 998 999 if ( $cover_image['width'] < $dimensions['width'] || $cover_image['height'] < $dimensions['height'] ) { 1000 $is_too_small = true; 1001 } 1002 } 1003 1004 // Default error message 1005 $error_message = __( 'There was a problem uploading the cover image.', 'buddypress' ); 1006 1007 if ( empty( $cover_file ) ) { 1008 // Upload error response 1009 bp_attachments_json_response( false, $is_html4, array( 1010 'type' => 'upload_error', 1011 'message' => $error_message, 1012 ) ); 1013 } 1014 1015 // Set the basename for the cover file 1016 $cover_basename = wp_basename( $cover_file ); 1017 1018 // Get BuddyPress Attachments Uploads Dir datas 1019 $bp_attachments_uploads_dir = bp_attachments_uploads_dir_get(); 1020 1021 $cover_subdir = $object_data['dir'] . '/' . $bp_params['item_id'] . '/cover-image'; 1022 $cover_dir = trailingslashit( $bp_attachments_uploads_dir['basedir'] ) . $cover_subdir; 1023 1024 if ( ! is_dir( $cover_dir ) ) { 1025 // Upload error response 1026 bp_attachments_json_response( false, $is_html4, array( 1027 'type' => 'upload_error', 1028 'message' => $error_message, 1029 ) ); 1030 } 1031 1032 // Clean up the cover dir to only keep the uploaded cover image 1033 if ( $att_dir = opendir( $cover_dir ) ) { 1034 while ( false !== ( $attachment_file = readdir( $att_dir ) ) ) { 1035 // skip directories and the new cover image 1036 if ( 2 < strlen( $attachment_file ) && 0 !== strpos( $attachment_file, '.' ) && $cover_basename !== $attachment_file ) { 1037 @unlink( $cover_dir . '/' . $attachment_file ); 1038 } 1039 } 1040 } 1041 1042 // Build the url to the file 1043 $cover_url = trailingslashit( $bp_attachments_uploads_dir['baseurl'] ) . $cover_subdir . '/' . $cover_basename; 1044 1045 // Init Feedback code, 1 is success 1046 $feedback_code = 1; 1047 1048 // 0 is the size warning 1049 if ( $is_too_small ) { 1050 $feedback_code = 0; 1051 } 1052 1053 // Set the name of the file 1054 $name = $_FILES['file']['name']; 1055 $name_parts = pathinfo( $name ); 1056 $name = trim( substr( $name, 0, - ( 1 + strlen( $name_parts['extension'] ) ) ) ); 1057 1058 // Finally return the cover image url to the UI 1059 bp_attachments_json_response( true, $is_html4, array( 1060 'name' => $name, 1061 'url' => $cover_url, 1062 'feedback_code' => $feedback_code, 1063 ) ); 1064 } 1065 add_action( 'wp_ajax_bp_cover_image_upload', 'bp_attachments_cover_image_ajax_upload' ); 1066 1067 /** 1068 * Ajax delete a cover image for a given object and item id. 1069 * 1070 * @since 2.4.0 1071 * 1072 * @return string|null A json object containing success data if the cover image was deleted 1073 * error message otherwise. 1074 */ 1075 function bp_attachments_cover_image_ajax_delete() { 1076 // Bail if not a POST action. 1077 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { 1078 wp_send_json_error(); 1079 } 1080 1081 $cover_image_data = $_POST; 1082 1083 if ( empty( $cover_image_data['object'] ) || empty( $cover_image_data['item_id'] ) ) { 1084 wp_send_json_error(); 1085 } 1086 1087 // Check the nonce 1088 check_admin_referer( 'bp_delete_cover_image', 'nonce' ); 1089 1090 // Capability check 1091 if ( ! bp_attachments_current_user_can( 'edit_cover_image', $cover_image_data ) ) { 1092 wp_send_json_error(); 1093 } 1094 1095 // Set object for the user's case 1096 if ( 'user' === $cover_image_data['object'] ) { 1097 $component = 'xprofile'; 1098 $dir = 'members'; 1099 1100 // Set it for any other cases 1101 } else { 1102 $component = $cover_image_data['object'] . 's'; 1103 $dir = $component; 1104 } 1105 1106 // Handle delete 1107 if ( bp_attachments_delete_file( array( 'item_id' => $cover_image_data['item_id'], 'object_dir' => $dir, 'type' => 'cover-image' ) ) ) { 1108 1109 // Defaults no cover image 1110 $response = array( 1111 'reset_url' => '', 1112 'feedback_code' => 3 , 1113 ); 1114 1115 // Get cover image settings in case there's a default header 1116 $cover_params = bp_attachments_get_cover_image_settings( $component ); 1117 1118 // Check if there's a default cover 1119 if ( ! empty( $cover_params['default_cover'] ) ) { 1120 $response['reset_url'] = $cover_params['default_cover']; 1121 } 1122 1123 // Finally send the reset url 1124 wp_send_json_success( $response ); 1125 1126 } else { 1127 wp_send_json_error( array( 1128 'feedback_code' => 2, 1129 ) ); 1130 } 1131 } 1132 add_action( 'wp_ajax_bp_cover_image_delete', 'bp_attachments_cover_image_ajax_delete' );
Note: See TracChangeset
for help on using the changeset viewer.