Skip to content

Instantly share code, notes, and snippets.

@imath

imath/bp-custom.php Secret

Created Jun 28, 2015
Embed
What would you like to do?
Function to add an avatar...
<?php
/**
* Must used functions
*
* Functions that will always be loaded
*/
// Exit if accessed directly
defined( 'ABSPATH' ) or die;
class Sven_Attachment {
public $result = false;
public function __construct( $item_id = 0, $object = '', $file = false ) {
if ( empty( $item_id ) || empty( $object ) ) {
return;
}
$this->item_id = (int) $item_id;
$this->object = sanitize_key( $object );
// Defaults to user
$this->component = 'xprofile';
// might be improvable!
if ( 'user' !== $this->object ) {
$this->component = $this->object . 's';
}
// It's a new upload
if ( is_array( $file ) ) {
$this->upload( $file );
// it's an absolute path to a previously uploaded image file
} elseif ( ! empty( $file ) ) {
$this->crop( $file );
}
}
public function upload( $file = array() ) {
$bp = buddypress();
if ( ! isset( $bp->avatar_admin ) ) {
$bp->avatar_admin = new StdClass;
}
// Skip the test form
add_filter( 'bp_attachment_upload_overrides', array( $this, 'upload_overrides' ), 10, 1 );
$uploaded = bp_core_avatar_handle_upload( $file, array( $this, 'upload_dir_filter' ) );
// Restore the test form
remove_filter( 'bp_attachment_upload_overrides', array( $this, 'upload_overrides' ), 10, 1 );
if ( empty( $uploaded ) || ! isset( $bp->avatar_admin->image->dir ) ) {
$this->result = false;
return $this->result;
}
$this->crop( $bp->avatar_admin->image->dir );
}
public function crop( $image_file = '' ) {
$avatar_dir = sprintf( '%s-avatars', $this->object );
$upload_path = bp_core_avatar_upload_path();
$avatar_dir_path = $upload_path . '/' . $avatar_dir . '/' . $this->item_id;
if ( file_exists( $image_file ) ) {
$image = basename( $image_file );
// It's not a regular upload, we may need to create this folder
if( ! is_dir( $avatar_dir_path ) ) {
if ( ! wp_mkdir_p( $avatar_dir_path ) ) {
return false;
}
}
copy( $image_file, $avatar_dir_path . '/' . $image );
$image_file = '/' . $avatar_dir . '/' . $this->item_id . '/' . $image;
}
if ( ! file_exists( $upload_path . $image_file ) ) {
$this->result = false;
return $this->result;
}
$cropped = bp_core_avatar_handle_crop( array(
'object' => $this->object,
'avatar_dir' => sprintf( '%s-avatars', $this->object ),
'item_id' => $this->item_id,
'original_file' => $image_file,
'crop_x' => 0,
'crop_y' => 0
) );
if ( ! $cropped ) {
$this->result = false;
} else {
$this->result = true;
}
}
public function upload_overrides( $overrides = array() ) {
$overrides = array_merge( $overrides, array( 'test_form' => false ) );
return $overrides;
}
public function upload_dir_filter() {
$callback = $this->component . '_avatar_upload_dir';
if ( is_callable( $this->component . '_avatar_upload_dir' ) ) {
return call_user_func_array( $this->component . '_avatar_upload_dir', array( $this->item_id ) );
// return an error
} else {
return array( 'error' => 'dang!' );
}
}
}
/**
* Add an avatar to a BuddyPress object
*
* @param int $item_id the ID of the user or group
* @param string $object the item type (user or group)
* @param string|array $file the absolute path to a previously uploaded image or $_FILES
* @return bool true if the image was set as the avatar for the item it,
* false otherwise
*/
function svn_add_avatar_by_item_id( $item_id = 0, $object = '', $file ) {
$default_upload_crop = new Sven_Attachment( $item_id, $object, $file );
return $default_upload_crop->result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.