Skip to:
Content

BuddyPress.org

Ticket #5202: 5202.patch

File 5202.patch, 2.8 KB (added by imath, 8 years ago)
  • src/bp-core/bp-core-avatars.php

    diff --git src/bp-core/bp-core-avatars.php src/bp-core/bp-core-avatars.php
    index bef7735..3667af5 100644
    function bp_avatar_ajax_set() { 
    12361236add_action( 'wp_ajax_bp_avatar_set', 'bp_avatar_ajax_set' );
    12371237
    12381238/**
     1239 * Use the absolute path to an image to set an object's avatar
     1240 *
     1241 * @since BuddyPress (?)
     1242 *
     1243 * @param  array  $args {
     1244 *     @type int    $item_id   The ID of the object
     1245 *     @type string $type      The type of the object (eg: group, user, blog)
     1246 *     @type string $component The component for the object (eg: groups, xprofile, blogs)
     1247 *     @type string $image     The absolute path to the image
     1248 *     @type int $crop_x       The horizontal starting point of the crop. Default: 0.
     1249 *     @type int $crop_y       The vertical starting point of the crop. Default: 0.
     1250 * }
     1251 * @return bool  true on success, false otherwise
     1252 */
     1253function bp_avatar_create_item_avatar( $args = array() ) {
     1254        $r = bp_parse_args( $args, array(
     1255                'item_id'   => 0,
     1256                'type'      => 'user',
     1257                'component' => 'xprofile',
     1258                'image'     => '',
     1259                'crop_x'    => 0,
     1260                'crop_y'    => 0
     1261        ), 'create_item_avatar' );
     1262
     1263        if ( empty( $r['item_id'] ) || ! file_exists( $r['image'] ) || ! @getimagesize( $r['image'] ) ) {
     1264                return false;
     1265        }
     1266
     1267        if ( is_callable( $r['component'] . '_avatar_upload_dir' ) ) {
     1268                $dir_args = array( $r['item_id'] );
     1269
     1270                // In case  of xprofile, we need an extra argument
     1271                if ( 'xprofile' === $r['component'] ) {
     1272                        $dir_args = array( false, $r['item_id'] );
     1273                }
     1274
     1275                $avatar_data = call_user_func_array( $r['component'] . '_avatar_upload_dir', $dir_args );
     1276        }
     1277
     1278        if ( ! isset( $avatar_data['path'] ) || ! isset( $avatar_data['subdir'] ) ) {
     1279                return false;
     1280        }
     1281
     1282        // It's not a regular upload, we may need to create this folder
     1283        if( ! is_dir( $avatar_data['path'] ) ) {
     1284                if ( ! wp_mkdir_p( $avatar_data['path'] ) ) {
     1285                        return false;
     1286                }
     1287        }
     1288
     1289        $image_file_name = wp_unique_filename( $avatar_data['path'], basename( $r['image'] ) );
     1290
     1291        // Copy the image file into the avatar dir
     1292        copy( $r['image'], $avatar_data['path'] . '/' . $image_file_name );
     1293
     1294        // Check the original file is copied
     1295        if ( ! file_exists( $avatar_data['path'] . '/' . $image_file_name ) ) {
     1296                return false;
     1297        }
     1298
     1299        if ( ! bp_core_avatar_handle_crop( array(
     1300                'object'        => $r['type'],
     1301                'avatar_dir'    => trim( dirname( $avatar_data['subdir'] ), '/' ),
     1302                'item_id'       => (int) $r['item_id'],
     1303                'original_file' => trailingslashit( $avatar_data['subdir'] ) . $image_file_name,
     1304                'crop_x'        => $r['crop_x'],
     1305                'crop_y'        => $r['crop_y']
     1306        ) ) ) {
     1307                return false;
     1308        } else {
     1309                return true;
     1310        }
     1311}
     1312
     1313/**
    12391314 * Replace default WordPress avatars with BP avatars, if available.
    12401315 *
    12411316 * Filters 'get_avatar'.