Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/06/2015 11:17:06 PM (9 years ago)
Author:
imath
Message:

Introduce a new function to generate an Avatar or a Cover Image for a given User or Group.

You can now use the absolute path to an existing image of your WordPress site to dynamically set the avatar or the cover image of a Group or a User.

Props svenl77, boonebgorges, DJPaul

Fixes #5202

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-attachments.php

    r10192 r10193  
    214214
    215215    return false;
     216}
     217
     218/**
     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 */
     236function 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    // Make sure the file path is safe
     257    if ( 0 !== validate_file( $r['image'] ) ) {
     258        return false;
     259    }
     260
     261    // Set the component if not already done
     262    if ( empty( $r['component'] ) ) {
     263        if ( 'user' === $r['object'] ) {
     264            $r['component'] = 'xprofile';
     265        } else {
     266            $r['component'] = $r['object'] . 's';
     267        }
     268    }
     269
     270    // Get allowed mimes for the Attachment type and check the image one is.
     271    $allowed_mimes = bp_attachments_get_allowed_mimes( $type );
     272    $is_allowed    = wp_check_filetype( $r['image'], $allowed_mimes );
     273
     274    // It's not an image.
     275    if ( ! $is_allowed['ext'] ) {
     276        return false;
     277    }
     278
     279    // Init the Attachment data
     280    $attachment_data = array();
     281
     282    if ( 'avatar' === $type ) {
     283        // Set crop width for the avatar if not given
     284        if ( empty( $r['crop_w'] ) ) {
     285            $r['crop_w'] = bp_core_avatar_full_width();
     286        }
     287
     288        // Set crop height for the avatar if not given
     289        if ( empty( $r['crop_h'] ) ) {
     290            $r['crop_h'] = bp_core_avatar_full_height();
     291        }
     292
     293        if ( is_callable( $r['component'] . '_avatar_upload_dir' ) ) {
     294            $dir_args = array( $r['item_id'] );
     295
     296            // In case  of xprofile, we need an extra argument
     297            if ( 'xprofile' === $r['component'] ) {
     298                $dir_args = array( false, $r['item_id'] );
     299            }
     300
     301            $attachment_data = call_user_func_array( $r['component'] . '_avatar_upload_dir', $dir_args );
     302        }
     303    } elseif ( 'cover_image' === $type ) {
     304        $attachment_data = bp_attachments_uploads_dir_get();
     305
     306        // Default to members for xProfile
     307        $object_subdir = 'members';
     308
     309        if ( 'xprofile' !== $r['component'] ) {
     310            $object_subdir = sanitize_key( $r['component'] );
     311        }
     312
     313        // Set Subdir
     314        $attachment_data['subdir'] = $object_subdir . '/' . $r['item_id'] . '/cover-image';
     315
     316        // Set Path
     317        $attachment_data['path'] = trailingslashit( $attachment_data['basedir'] ) . $attachment_data['subdir'];
     318    }
     319
     320    if ( ! isset( $attachment_data['path'] ) || ! isset( $attachment_data['subdir'] ) ) {
     321        return false;
     322    }
     323
     324    // It's not a regular upload, we may need to create some folders
     325    if ( ! is_dir( $attachment_data['path'] ) ) {
     326        if ( ! wp_mkdir_p( $attachment_data['path'] ) ) {
     327            return false;
     328        }
     329    }
     330
     331    // Set the image name and path
     332    $image_file_name = wp_unique_filename( $attachment_data['path'], basename( $r['image'] ) );
     333    $image_file_path = $attachment_data['path'] . '/' . $image_file_name;
     334
     335    // Copy the image file into the avatar dir
     336    if ( ! copy( $r['image'], $image_file_path ) ) {
     337        return false;
     338    }
     339
     340    // Init the response
     341    $created = false;
     342
     343    // It's an avatar, we need to crop it.
     344    if ( 'avatar' === $type ) {
     345        $created = bp_core_avatar_handle_crop( array(
     346            'object'        => $r['object'],
     347            'avatar_dir'    => trim( dirname( $attachment_data['subdir'] ), '/' ),
     348            'item_id'       => (int) $r['item_id'],
     349            'original_file' => trailingslashit( $attachment_data['subdir'] ) . $image_file_name,
     350            'crop_w'        => $r['crop_w'],
     351            'crop_h'        => $r['crop_h'],
     352            'crop_x'        => $r['crop_x'],
     353            'crop_y'        => $r['crop_y']
     354        ) );
     355
     356    // It's a cover image we need to fit it to feature's dimensions
     357    } elseif ( 'cover_image' === $type ) {
     358        $cover_image = bp_attachments_cover_image_generate_file( array(
     359            'file'            => $image_file_path,
     360            'component'       => $r['component'],
     361            'cover_image_dir' => $attachment_data['path']
     362        ) );
     363
     364        $created = ! empty( $cover_image['cover_file'] );
     365    }
     366
     367    // Remove copied file if it fails
     368    if ( ! $created ) {
     369        @unlink( $image_file_path );
     370    }
     371
     372    // Return the response
     373    return $created;
    216374}
    217375
Note: See TracChangeset for help on using the changeset viewer.