bp_avatar_ajax_upload()

Ajax upload an avatar.

Description

Return

(string|null) A JSON object containing success data if the upload succeeded error message otherwise.

Source

File: bp-core/bp-core-avatars.php

1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
function bp_avatar_ajax_upload() {
    if ( ! bp_is_post_request() ) {
        wp_die();
    }
 
    /**
     * Sending the json response will be different if
     * the current Plupload runtime is html4.
     */
    $is_html4 = false;
    if ( ! empty( $_POST['html4' ] ) ) {
        $is_html4 = true;
    }
 
    // Check the nonce.
    check_admin_referer( 'bp-uploader' );
 
    // Init the BuddyPress parameters.
    $bp_params = array();
 
    // We need it to carry on.
    if ( ! empty( $_POST['bp_params' ] ) ) {
        $bp_params = $_POST['bp_params' ];
    } else {
        bp_attachments_json_response( false, $is_html4 );
    }
 
    // We need the object to set the uploads dir filter.
    if ( empty( $bp_params['object'] ) ) {
        bp_attachments_json_response( false, $is_html4 );
    }
 
    // Capability check.
    if ( ! bp_attachments_current_user_can( 'edit_avatar', $bp_params ) ) {
        bp_attachments_json_response( false, $is_html4 );
    }
 
    $bp = buddypress();
    $bp_params['upload_dir_filter'] = '';
    $needs_reset = array();
 
    if ( 'user' === $bp_params['object'] && bp_is_active( 'xprofile' ) ) {
        $bp_params['upload_dir_filter'] = 'xprofile_avatar_upload_dir';
 
        if ( ! bp_displayed_user_id() && ! empty( $bp_params['item_id'] ) ) {
            $needs_reset = array( 'key' => 'displayed_user', 'value' => $bp->displayed_user );
            $bp->displayed_user->id = $bp_params['item_id'];
        }
    } elseif ( 'group' === $bp_params['object'] && bp_is_active( 'groups' ) ) {
        $bp_params['upload_dir_filter'] = 'groups_avatar_upload_dir';
 
        if ( ! bp_get_current_group_id() && ! empty( $bp_params['item_id'] ) ) {
            $needs_reset = array( 'component' => 'groups', 'key' => 'current_group', 'value' => $bp->groups->current_group );
            $bp->groups->current_group = groups_get_group( $bp_params['item_id'] );
        }
    } else {
        /**
         * Filter here to deal with other components.
         *
         * @since BuddyPress 2.3.0
         *
         * @var array $bp_params the BuddyPress Ajax parameters.
         */
        $bp_params = apply_filters( 'bp_core_avatar_ajax_upload_params', $bp_params );
    }
 
    if ( ! isset( $bp->avatar_admin ) ) {
        $bp->avatar_admin = new stdClass();
    }
 
    /**
     * The BuddyPress upload parameters is including the Avatar UI Available width,
     * add it to the avatar_admin global for a later use.
     */
    if ( isset( $bp_params['ui_available_width'] ) ) {
        $bp->avatar_admin->ui_available_width =  (int) $bp_params['ui_available_width'];
    }
 
    // Upload the avatar.
    $avatar = bp_core_avatar_handle_upload( $_FILES, $bp_params['upload_dir_filter'] );
 
    // Reset objects.
    if ( ! empty( $needs_reset ) ) {
        if ( ! empty( $needs_reset['component'] ) ) {
            $bp->{$needs_reset['component']}->{$needs_reset['key']} = $needs_reset['value'];
        } else {
            $bp->{$needs_reset['key']} = $needs_reset['value'];
        }
    }
 
    // Init the feedback message.
    $feedback_message = false;
 
    if ( ! empty( $bp->template_message ) ) {
        $feedback_message = $bp->template_message;
 
        // Remove template message.
        $bp->template_message      = false;
        $bp->template_message_type = false;
 
        @setcookie( 'bp-message', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
        @setcookie( 'bp-message-type', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
    }
 
    if ( empty( $avatar ) ) {
        // Default upload error.
        $message = __( 'Upload failed.', 'buddyboss' );
 
        // Use the template message if set.
        if ( ! empty( $feedback_message ) ) {
            $message = $feedback_message;
        }
 
        // Upload error reply.
        bp_attachments_json_response( false, $is_html4, array(
            'type'    => 'upload_error',
            'message' => $message,
        ) );
    }
 
    if ( empty( $bp->avatar_admin->image->file ) ) {
        bp_attachments_json_response( false, $is_html4 );
    }
 
    $uploaded_image = @getimagesize( $bp->avatar_admin->image->file );
 
    // Set the name of the file.
    $name = $_FILES['file']['name'];
    $name_parts = pathinfo( $name );
    $name = trim( substr( $name, 0, - ( 1 + strlen( $name_parts['extension'] ) ) ) );
 
    // Finally return the avatar to the editor.
    bp_attachments_json_response( true, $is_html4, array(
        'name'      => $name,
        'url'       => $bp->avatar_admin->image->url,
        'width'     => $uploaded_image[0],
        'height'    => $uploaded_image[1],
        'feedback'  => $feedback_message,
    ) );
}

Changelog

Changelog
Version Description
BuddyPress 2.3.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.