/** * Set Upload Dir data for avatars. * * @since 2.3.0 * * @uses bp_core_avatar_upload_path() * @uses bp_core_avatar_url() * @uses bp_upload_dir() * @uses BP_Attachment::set_upload_dir() */ public function set_upload_dir() { if (bp_core_avatar_upload_path() && bp_core_avatar_url()) { $this->upload_path = bp_core_avatar_upload_path(); $this->url = bp_core_avatar_url(); $this->upload_dir = bp_upload_dir(); } else { parent::set_upload_dir(); } }
/** * @ticket BP4948 */ function test_avatars_on_non_root_blog() { // Do not pass 'Go', do not collect $200 if (!is_multisite()) { return; } $u = $this->factory->user->create(); // get BP root blog's upload directory data $upload_dir = wp_upload_dir(); // create new subsite $blog_id = $this->factory->blog->create(array('user_id' => $u, 'title' => 'Test Title', 'path' => '/path' . rand() . time() . '/')); // emulate a page load on the new sub-site $this->go_to(get_blog_option($blog_id, 'siteurl')); // test to see if the upload dir is correct $this->assertEquals($upload_dir['baseurl'], bp_core_avatar_url()); // reset globals $this->go_to('/'); }
function bp_core_set_avatar_constants() { global $bp; if (!defined('BP_AVATAR_UPLOAD_PATH')) { define('BP_AVATAR_UPLOAD_PATH', bp_core_avatar_upload_path()); } if (!defined('BP_AVATAR_URL')) { define('BP_AVATAR_URL', bp_core_avatar_url()); } if (!defined('BP_AVATAR_THUMB_WIDTH')) { define('BP_AVATAR_THUMB_WIDTH', 32); } if (!defined('BP_AVATAR_THUMB_HEIGHT')) { define('BP_AVATAR_THUMB_HEIGHT', 32); } if (!defined('BP_AVATAR_FULL_WIDTH')) { define('BP_AVATAR_FULL_WIDTH', 184); } if (!defined('BP_AVATAR_FULL_HEIGHT')) { define('BP_AVATAR_FULL_HEIGHT', 184); } if (!defined('BP_AVATAR_ORIGINAL_MAX_WIDTH')) { define('BP_AVATAR_ORIGINAL_MAX_WIDTH', 450); } if (!defined('BP_AVATAR_ORIGINAL_MAX_FILESIZE')) { if (!$bp->site_options['fileupload_maxk']) { define('BP_AVATAR_ORIGINAL_MAX_FILESIZE', 5120000); } else { define('BP_AVATAR_ORIGINAL_MAX_FILESIZE', $bp->site_options['fileupload_maxk'] * 1024); } } if (!defined('BP_AVATAR_DEFAULT')) { define('BP_AVATAR_DEFAULT', BP_PLUGIN_URL . '/bp-core/images/mystery-man.jpg'); } if (!defined('BP_AVATAR_DEFAULT_THUMB')) { define('BP_AVATAR_DEFAULT_THUMB', BP_PLUGIN_URL . '/bp-core/images/mystery-man-50.jpg'); } }
/** * Setup the avatar upload directory for a user. * * @since BuddyPress (1.0.0) * * @package BuddyPress Core * * @param string $directory The root directory name. Optional. * @param int $user_id The user ID. Optional. * * @return array() Array containing the path, URL, and other helpful settings. */ function xprofile_avatar_upload_dir($directory = 'avatars', $user_id = 0) { // Use displayed user if no user ID was passed if (empty($user_id)) { $user_id = bp_displayed_user_id(); } // Failsafe against accidentally nooped $directory parameter if (empty($directory)) { $directory = 'avatars'; } $path = bp_core_avatar_upload_path() . '/' . $directory . '/' . $user_id; $newbdir = $path; $newurl = bp_core_avatar_url() . '/' . $directory . '/' . $user_id; $newburl = $newurl; $newsubdir = '/' . $directory . '/' . $user_id; /** * Filters the avatar upload directory for a user. * * @since BuddyPress (1.1.0) * * @param array $value Array containing the path, URL, and other helpful settings. */ return apply_filters('xprofile_avatar_upload_dir', array('path' => $path, 'url' => $newurl, 'subdir' => $newsubdir, 'basedir' => $newbdir, 'baseurl' => $newburl, 'error' => false)); }
/** * Handle avatar uploading. * * The functions starts off by checking that the file has been uploaded * properly using bp_core_check_avatar_upload(). It then checks that the file * size is within limits, and that it has an accepted file extension (jpg, gif, * png). If everything checks out, crop the image and move it to its real * location. * * @see bp_core_check_avatar_upload() * @see bp_core_check_avatar_type() * * @param array $file The appropriate entry the from $_FILES superglobal. * @param string $upload_dir_filter A filter to be applied to 'upload_dir'. * @return bool True on success, false on failure. */ function bp_core_avatar_handle_upload($file, $upload_dir_filter) { /*** * You may want to hook into this filter if you want to override this function. * Make sure you return false. */ if (!apply_filters('bp_core_pre_avatar_handle_upload', true, $file, $upload_dir_filter)) { return true; } require_once ABSPATH . '/wp-admin/includes/file.php'; $uploadErrors = array(0 => __('The image was uploaded successfully', 'buddypress'), 1 => __('The image exceeds the maximum allowed file size of: ', 'buddypress') . size_format(bp_core_avatar_original_max_filesize()), 2 => __('The image exceeds the maximum allowed file size of: ', 'buddypress') . size_format(bp_core_avatar_original_max_filesize()), 3 => __('The uploaded file was only partially uploaded.', 'buddypress'), 4 => __('The image was not uploaded.', 'buddypress'), 6 => __('Missing a temporary folder.', 'buddypress')); if (!bp_core_check_avatar_upload($file)) { bp_core_add_message(sprintf(__('Your upload failed, please try again. Error was: %s', 'buddypress'), $uploadErrors[$file['file']['error']]), 'error'); return false; } if (!bp_core_check_avatar_size($file)) { bp_core_add_message(sprintf(__('The file you uploaded is too big. Please upload a file under %s', 'buddypress'), size_format(bp_core_avatar_original_max_filesize())), 'error'); return false; } if (!bp_core_check_avatar_type($file)) { bp_core_add_message(__('Please upload only JPG, GIF or PNG photos.', 'buddypress'), 'error'); return false; } // Filter the upload location add_filter('upload_dir', $upload_dir_filter, 10, 0); $bp = buddypress(); $bp->avatar_admin->original = wp_handle_upload($file['file'], array('action' => 'bp_avatar_upload')); // Remove the upload_dir filter, so that other upload URLs on the page // don't break remove_filter('upload_dir', $upload_dir_filter, 10, 0); // Move the file to the correct upload location. if (!empty($bp->avatar_admin->original['error'])) { bp_core_add_message(sprintf(__('Upload Failed! Error was: %s', 'buddypress'), $bp->avatar_admin->original['error']), 'error'); return false; } // Get image size $size = @getimagesize($bp->avatar_admin->original['file']); $error = false; // Check image size and shrink if too large if ($size[0] > bp_core_avatar_original_max_width()) { $editor = wp_get_image_editor($bp->avatar_admin->original['file']); if (!is_wp_error($editor)) { $editor->set_quality(100); $resized = $editor->resize(bp_core_avatar_original_max_width(), bp_core_avatar_original_max_width(), false); if (!is_wp_error($resized)) { $thumb = $editor->save($editor->generate_filename()); } else { $error = $resized; } // Check for thumbnail creation errors if (false === $error && is_wp_error($thumb)) { $error = $thumb; } // Thumbnail is good so proceed if (false === $error) { $bp->avatar_admin->resized = $thumb; } } else { $error = $editor; } if (false !== $error) { bp_core_add_message(sprintf(__('Upload Failed! Error was: %s', 'buddypress'), $error->get_error_message()), 'error'); return false; } } if (!isset($bp->avatar_admin->image)) { $bp->avatar_admin->image = new stdClass(); } // We only want to handle one image after resize. if (empty($bp->avatar_admin->resized)) { $bp->avatar_admin->image->dir = str_replace(bp_core_avatar_upload_path(), '', $bp->avatar_admin->original['file']); } else { $bp->avatar_admin->image->dir = str_replace(bp_core_avatar_upload_path(), '', $bp->avatar_admin->resized['path']); @unlink($bp->avatar_admin->original['file']); } // Check for WP_Error on what should be an image if (is_wp_error($bp->avatar_admin->image->dir)) { bp_core_add_message(sprintf(__('Upload failed! Error was: %s', 'buddypress'), $bp->avatar_admin->image->dir->get_error_message()), 'error'); return false; } // If the uploaded image is smaller than the "full" dimensions, throw // a warning $uploaded_image = @getimagesize(bp_core_avatar_upload_path() . buddypress()->avatar_admin->image->dir); $full_width = bp_core_avatar_full_width(); $full_height = bp_core_avatar_full_height(); if (isset($uploaded_image[0]) && $uploaded_image[0] < $full_width || $uploaded_image[1] < $full_height) { bp_core_add_message(sprintf(__('You have selected an image that is smaller than recommended. For best results, upload a picture larger than %d x %d pixels.', 'buddypress'), $full_width, $full_height), 'error'); } // Set the url value for the image $bp->avatar_admin->image->url = bp_core_avatar_url() . $bp->avatar_admin->image->dir; return true; }
/** * Get the avatar storage directory for use during registration. * * @since 1.1.0 * * @return string|bool Directory path on success, false on failure. */ function bp_core_signup_avatar_upload_dir() { $bp = buddypress(); if (empty($bp->signup->avatar_dir)) { return false; } $directory = 'avatars/signups'; $path = bp_core_avatar_upload_path() . '/' . $directory . '/' . $bp->signup->avatar_dir; $newbdir = $path; $newurl = bp_core_avatar_url() . '/' . $directory . '/' . $bp->signup->avatar_dir; $newburl = $newurl; $newsubdir = '/' . $directory . '/' . $bp->signup->avatar_dir; /** * Filters the avatar storage directory for use during registration. * * @since 1.1.1 * * @param array $value Array of path and URL values for created storage directory. */ return apply_filters('bp_core_signup_avatar_upload_dir', array('path' => $path, 'url' => $newurl, 'subdir' => $newsubdir, 'basedir' => $newbdir, 'baseurl' => $newburl, 'error' => false)); }
/** * Setup the avatar upload directory for a user. * * @package BuddyPress Core * @param $directory The root directory name * @param $user_id The user ID. * @return array() containing the path and URL plus some other settings. */ function xprofile_avatar_upload_dir($directory = false, $user_id = 0) { global $bp; if (empty($user_id)) { $user_id = $bp->displayed_user->id; } if (empty($directory)) { $directory = 'avatars'; } $path = bp_core_avatar_upload_path() . '/avatars/' . $user_id; $newbdir = $path; if (!file_exists($path)) { @nxt_mkdir_p($path); } $newurl = bp_core_avatar_url() . '/avatars/' . $user_id; $newburl = $newurl; $newsubdir = '/avatars/' . $user_id; return apply_filters('xprofile_avatar_upload_dir', array('path' => $path, 'url' => $newurl, 'subdir' => $newsubdir, 'basedir' => $newbdir, 'baseurl' => $newburl, 'error' => false)); }
function bp_links_embed_download_avatar($url) { global $bp; require_once ABSPATH . '/wp-admin/includes/image.php'; $bp->avatar_admin->original = bp_links_embed_upload_from_url($url); // Move the file to the correct upload location. if (!empty($bp->avatar_admin->original['error'])) { bp_core_add_message(sprintf('%1$s %2$s', __('Upload Failed! Error was:', 'buddypress-links'), $bp->avatar_admin->original['error']), 'error'); return false; } // Resize the image down to something manageable and then delete the original if (getimagesize($bp->avatar_admin->original['file']) > BP_AVATAR_ORIGINAL_MAX_WIDTH) { $bp->avatar_admin->resized = wp_create_thumbnail($bp->avatar_admin->original['file'], BP_AVATAR_ORIGINAL_MAX_WIDTH); } $bp->avatar_admin->image = new stdClass(); // We only want to handle one image after resize. if (empty($bp->avatar_admin->resized)) { $bp->avatar_admin->image->dir = $bp->avatar_admin->original['file']; } else { $bp->avatar_admin->image->dir = $bp->avatar_admin->resized; @unlink($bp->avatar_admin->original['file']); } /* Set the url value for the image */ $bp->avatar_admin->image->url = str_replace(bp_core_avatar_upload_path(), bp_core_avatar_url(), $bp->avatar_admin->image->dir); return true; }
function bp_core_signup_avatar_upload_dir() { global $bp; if (!$bp->signup->avatar_dir) { return false; } $path = bp_core_avatar_upload_path() . '/avatars/signups/' . $bp->signup->avatar_dir; $newbdir = $path; if (!file_exists($path)) { @wp_mkdir_p($path); } $newurl = bp_core_avatar_url() . '/avatars/signups/' . $bp->signup->avatar_dir; $newburl = $newurl; $newsubdir = '/avatars/signups/' . $bp->signup->avatar_dir; return apply_filters('bp_core_signup_avatar_upload_dir', array('path' => $path, 'url' => $newurl, 'subdir' => $newsubdir, 'basedir' => $newbdir, 'baseurl' => $newburl, 'error' => false)); }
function groups_avatar_upload_dir($group_id = 0) { global $bp; if (!$group_id) { $group_id = $bp->groups->current_group->id; } $path = bp_core_avatar_upload_path() . '/group-avatars/' . $group_id; $newbdir = $path; if (!file_exists($path)) { @wp_mkdir_p($path); } $newurl = bp_core_avatar_url() . '/group-avatars/' . $group_id; $newburl = $newurl; $newsubdir = '/group-avatars/' . $group_id; return apply_filters('groups_avatar_upload_dir', array('path' => $path, 'url' => $newurl, 'subdir' => $newsubdir, 'basedir' => $newbdir, 'baseurl' => $newburl, 'error' => false)); }
/** * Handles avatar uploading. * * The functions starts off by checking that the file has been uploaded properly using bp_core_check_avatar_upload(). * It then checks that the file size is within limits, and that it has an accepted file extension (jpg, gif, png). * If everything checks out, crop the image and move it to its real location. * * @global object $bp BuddyPress global settings * @param array $file The appropriate entry the from $_FILES superglobal. * @param string $upload_dir_filter A filter to be applied to upload_dir * @return bool Success/failure * @see bp_core_check_avatar_upload() * @see bp_core_check_avatar_type() */ function bp_core_avatar_handle_upload($file, $upload_dir_filter) { global $bp; /*** * You may want to hook into this filter if you want to override this function. * Make sure you return false. */ if (!apply_filters('bp_core_pre_avatar_handle_upload', true, $file, $upload_dir_filter)) { return true; } require_once ABSPATH . '/nxt-admin/includes/image.php'; require_once ABSPATH . '/nxt-admin/includes/file.php'; $uploadErrors = array(0 => __("There is no error, the file uploaded with success", 'buddypress'), 1 => __("Your image was bigger than the maximum allowed file size of: ", 'buddypress') . size_format(bp_core_avatar_original_max_filesize()), 2 => __("Your image was bigger than the maximum allowed file size of: ", 'buddypress') . size_format(bp_core_avatar_original_max_filesize()), 3 => __("The uploaded file was only partially uploaded", 'buddypress'), 4 => __("No file was uploaded", 'buddypress'), 6 => __("Missing a temporary folder", 'buddypress')); if (!bp_core_check_avatar_upload($file)) { bp_core_add_message(sprintf(__('Your upload failed, please try again. Error was: %s', 'buddypress'), $uploadErrors[$file['file']['error']]), 'error'); return false; } if (!bp_core_check_avatar_size($file)) { bp_core_add_message(sprintf(__('The file you uploaded is too big. Please upload a file under %s', 'buddypress'), size_format(bp_core_avatar_original_max_filesize())), 'error'); return false; } if (!bp_core_check_avatar_type($file)) { bp_core_add_message(__('Please upload only JPG, GIF or PNG photos.', 'buddypress'), 'error'); return false; } // Filter the upload location add_filter('upload_dir', $upload_dir_filter, 10, 0); $bp->avatar_admin->original = nxt_handle_upload($file['file'], array('action' => 'bp_avatar_upload')); // Move the file to the correct upload location. if (!empty($bp->avatar_admin->original['error'])) { bp_core_add_message(sprintf(__('Upload Failed! Error was: %s', 'buddypress'), $bp->avatar_admin->original['error']), 'error'); return false; } // Get image size $size = @getimagesize($bp->avatar_admin->original['file']); // Check image size and shrink if too large if ($size[0] > bp_core_avatar_original_max_width()) { $thumb = nxt_create_thumbnail($bp->avatar_admin->original['file'], bp_core_avatar_original_max_width()); // Check for thumbnail creation errors if (is_nxt_error($thumb)) { bp_core_add_message(sprintf(__('Upload Failed! Error was: %s', 'buddypress'), $thumb->get_error_message()), 'error'); return false; } // Thumbnail is good so proceed $bp->avatar_admin->resized = $thumb; } // We only want to handle one image after resize. if (empty($bp->avatar_admin->resized)) { $bp->avatar_admin->image->dir = str_replace(bp_core_avatar_upload_path(), '', $bp->avatar_admin->original['file']); } else { $bp->avatar_admin->image->dir = str_replace(bp_core_avatar_upload_path(), '', $bp->avatar_admin->resized); @unlink($bp->avatar_admin->original['file']); } // Check for nxt_Error on what should be an image if (is_nxt_error($bp->avatar_admin->image->dir)) { bp_core_add_message(sprintf(__('Upload failed! Error was: %s', 'buddypress'), $bp->avatar_admin->image->dir->get_error_message()), 'error'); return false; } // Set the url value for the image $bp->avatar_admin->image->url = bp_core_avatar_url() . $bp->avatar_admin->image->dir; return true; }
/** * Ajax set an avatar for a given object and item id * * @since BuddyPress (2.3.0) * * @return string a json object containing success data if the crop/capture succeeded * error message otherwise */ function bp_avatar_ajax_set() { // Bail if not a POST action if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) { wp_send_json_error(); } // Check the nonce check_admin_referer('bp_avatar_cropstore', 'nonce'); $avatar_data = wp_parse_args($_POST, array('crop_w' => bp_core_avatar_full_width(), 'crop_h' => bp_core_avatar_full_height(), 'crop_x' => 0, 'crop_y' => 0)); if (empty($avatar_data['object']) || empty($avatar_data['item_id']) || empty($avatar_data['original_file'])) { wp_send_json_error(); } // Capability check if (!bp_attachments_current_user_can('edit_avatar', $avatar_data)) { wp_send_json_error(); } if (!empty($avatar_data['type']) && 'camera' === $avatar_data['type'] && 'user' === $avatar_data['object']) { $webcam_avatar = false; if (!empty($avatar_data['original_file'])) { $webcam_avatar = str_replace(array('data:image/png;base64,', ' '), array('', '+'), $avatar_data['original_file']); $webcam_avatar = base64_decode($webcam_avatar); } if (!bp_avatar_handle_capture($webcam_avatar, $avatar_data['item_id'])) { wp_send_json_error(array('feedback_code' => 1)); } else { $return = array('avatar' => html_entity_decode(bp_core_fetch_avatar(array('object' => $avatar_data['object'], 'item_id' => $avatar_data['item_id'], 'html' => false, 'type' => 'full'))), 'feedback_code' => 2, 'item_id' => $avatar_data['item_id']); do_action('xprofile_screen_change_avatar'); wp_send_json_success($return); } return; } $original_file = str_replace(bp_core_avatar_url(), '', $avatar_data['original_file']); // Set avatars dir & feedback part if ('user' === $avatar_data['object']) { $avatar_dir = 'avatars'; // Defaults to object-avatars dir } else { $avatar_dir = sanitize_key($avatar_data['object']) . '-avatars'; } // Crop args $r = array('item_id' => $avatar_data['item_id'], 'object' => $avatar_data['object'], 'avatar_dir' => $avatar_dir, 'original_file' => $original_file, 'crop_w' => $avatar_data['crop_w'], 'crop_h' => $avatar_data['crop_h'], 'crop_x' => $avatar_data['crop_x'], 'crop_y' => $avatar_data['crop_y']); // Handle crop if (bp_core_avatar_handle_crop($r)) { $return = array('avatar' => html_entity_decode(bp_core_fetch_avatar(array('object' => $avatar_data['object'], 'item_id' => $avatar_data['item_id'], 'html' => false, 'type' => 'full'))), 'feedback_code' => 2, 'item_id' => $avatar_data['item_id']); if ('user' === $avatar_data['object']) { do_action('xprofile_screen_change_avatar'); } wp_send_json_success($return); } else { wp_send_json_error(array('feedback_code' => 1)); } }
/** * Setup the avatar upload directory for a user. * * @since BuddyPress (1.0.0) * * @package BuddyPress Core * * @param string $directory The root directory name. Optional. * @param int $user_id The user ID. Optional. * * @return array() Array containing the path, URL, and other helpful settings. */ function xprofile_avatar_upload_dir($directory = 'avatars', $user_id = 0) { // Use displayed user if no user ID was passed if (empty($user_id)) { $user_id = bp_displayed_user_id(); } // Failsafe against accidentally nooped $directory parameter if (empty($directory)) { $directory = 'avatars'; } $path = bp_core_avatar_upload_path() . '/' . $directory . '/' . $user_id; $newbdir = $path; if (!file_exists($path)) { @wp_mkdir_p($path); } $newurl = bp_core_avatar_url() . '/' . $directory . '/' . $user_id; $newburl = $newurl; $newsubdir = '/' . $directory . '/' . $user_id; return apply_filters('xprofile_avatar_upload_dir', array('path' => $path, 'url' => $newurl, 'subdir' => $newsubdir, 'basedir' => $newbdir, 'baseurl' => $newburl, 'error' => false)); }
/** * Ajax set an avatar for a given object and item id. * * @since 2.3.0 * * @return string|null A json object containing success data if the crop/capture succeeded * error message otherwise. */ function bp_avatar_ajax_set() { // Bail if not a POST action if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) { wp_send_json_error(); } // Check the nonce check_admin_referer('bp_avatar_cropstore', 'nonce'); $avatar_data = wp_parse_args($_POST, array('crop_w' => bp_core_avatar_full_width(), 'crop_h' => bp_core_avatar_full_height(), 'crop_x' => 0, 'crop_y' => 0)); if (empty($avatar_data['object']) || empty($avatar_data['item_id']) || empty($avatar_data['original_file'])) { wp_send_json_error(); } // Capability check if (!bp_attachments_current_user_can('edit_avatar', $avatar_data)) { wp_send_json_error(); } if (!empty($avatar_data['type']) && 'camera' === $avatar_data['type'] && 'user' === $avatar_data['object']) { $webcam_avatar = false; if (!empty($avatar_data['original_file'])) { $webcam_avatar = str_replace(array('data:image/png;base64,', ' '), array('', '+'), $avatar_data['original_file']); $webcam_avatar = base64_decode($webcam_avatar); } if (!bp_avatar_handle_capture($webcam_avatar, $avatar_data['item_id'])) { wp_send_json_error(array('feedback_code' => 1)); } else { $return = array('avatar' => html_entity_decode(bp_core_fetch_avatar(array('object' => $avatar_data['object'], 'item_id' => $avatar_data['item_id'], 'html' => false, 'type' => 'full'))), 'feedback_code' => 2, 'item_id' => $avatar_data['item_id']); /** * Fires if the new avatar was successfully captured. * * @since 1.1.0 Used to inform the avatar was successfully cropped * @since 2.3.4 Add two new parameters to inform about the user id and * about the way the avatar was set (eg: 'crop' or 'camera') * Move the action at the right place, once the avatar is set * * @param string $item_id Inform about the user id the avatar was set for * @param string $type Inform about the way the avatar was set ('camera') */ do_action('xprofile_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type']); wp_send_json_success($return); } return; } $original_file = str_replace(bp_core_avatar_url(), '', $avatar_data['original_file']); // Set avatars dir & feedback part if ('user' === $avatar_data['object']) { $avatar_dir = 'avatars'; // Defaults to object-avatars dir } else { $avatar_dir = sanitize_key($avatar_data['object']) . '-avatars'; } // Crop args $r = array('item_id' => $avatar_data['item_id'], 'object' => $avatar_data['object'], 'avatar_dir' => $avatar_dir, 'original_file' => $original_file, 'crop_w' => $avatar_data['crop_w'], 'crop_h' => $avatar_data['crop_h'], 'crop_x' => $avatar_data['crop_x'], 'crop_y' => $avatar_data['crop_y']); // Handle crop if (bp_core_avatar_handle_crop($r)) { $return = array('avatar' => html_entity_decode(bp_core_fetch_avatar(array('object' => $avatar_data['object'], 'item_id' => $avatar_data['item_id'], 'html' => false, 'type' => 'full'))), 'feedback_code' => 2, 'item_id' => $avatar_data['item_id']); if ('user' === $avatar_data['object']) { /** * Fires if the new avatar was successfully cropped. * * @since 1.1.0 Used to inform the avatar was successfully cropped * @since 2.3.4 Add two new parameters to inform about the user id and * about the way the avatar was set (eg: 'crop' or 'camera') * Move the action at the right place, once the avatar is set * * @param string $item_id Inform about the user id the avatar was set for * @param string $type Inform about the way the avatar was set ('crop') */ do_action('xprofile_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type']); } wp_send_json_success($return); } else { wp_send_json_error(array('feedback_code' => 1)); } }
public function handle_crop() { require_once ABSPATH . '/wp-admin/includes/image.php'; $user_id = bp_displayed_user_id(); $cover_photo = get_transient('profile_cover_photo_' . $user_id); // Get the file extension $data = @getimagesize($cover_photo); $ext = $data['mime'] == 'image/png' ? 'png' : 'jpg'; $base_filename = basename($cover_photo, '.' . $ext); // create a new filename but, if it's already been cropped, strip out the -cropped $new_filename = str_replace('-cropped', '', $base_filename) . '-cropped.' . $ext; $new_filepath = bp_core_avatar_upload_path() . '/cover-photo/' . $user_id . '/' . $new_filename; $new_fileurl = bp_core_avatar_url() . '/cover-photo/' . $user_id . '/' . $new_filename; $crop_fileurl = str_replace(trailingslashit(get_home_url()), '', bp_core_avatar_url()) . '/cover-photo/' . $user_id . '/' . $new_filename; // delete the old cover photo if it exists if (file_exists($new_filepath)) { @unlink($new_filepath); } $cropped_header = wp_crop_image($cover_photo, $_POST['x'], $_POST['y'], $_POST['w'], $_POST['h'], $this->width, $this->height, false, $crop_fileurl); if (!is_wp_error($cropped_header)) { $old_file_path = get_user_meta(bp_loggedin_user_id(), 'profile_cover_photo_path', true); if (file_exists($old_file_path)) { @unlink($old_file_path); } // update with the new image and path bp_update_user_meta(bp_loggedin_user_id(), 'profile_cover_photo', $new_fileurl); bp_update_user_meta(bp_loggedin_user_id(), 'profile_cover_photo_path', $new_filepath); delete_transient('is_cover_photo_uploaded_' . bp_displayed_user_id()); delete_transient('profile_cover_photo_' . bp_displayed_user_id()); } }
/** * Generate the avatar upload directory path for a given group. * * @param int $group_id Optional. ID of the group. Default: ID of the * current group. * @return string */ function groups_avatar_upload_dir($group_id = 0) { if (empty($group_id)) { $group_id = bp_get_current_group_id(); } $directory = 'group-avatars'; $path = bp_core_avatar_upload_path() . '/' . $directory . '/' . $group_id; $newbdir = $path; $newurl = bp_core_avatar_url() . '/' . $directory . '/' . $group_id; $newburl = $newurl; $newsubdir = '/' . $directory . '/' . $group_id; /** * Filters the avatar upload directory path for a given group. * * @since 1.1.0 * * @param array $value Array of parts related to the groups avatar upload directory. */ return apply_filters('groups_avatar_upload_dir', array('path' => $path, 'url' => $newurl, 'subdir' => $newsubdir, 'basedir' => $newbdir, 'baseurl' => $newburl, 'error' => false)); }
function bp_links_avatar_upload_dir($link_id = false) { global $bp; if (!$link_id) { $link_id = $bp->links->current_link->id; } $subdir = '/link-avatars/' . $link_id; $path = bp_core_avatar_upload_path() . $subdir; $url = str_replace(bp_core_avatar_upload_path(), bp_core_avatar_url(), $path); if (!file_exists($path)) { @wp_mkdir_p($path); } return apply_filters('bp_links_avatar_upload_dir', array('path' => $path, 'url' => $url, 'subdir' => $subdir, 'basedir' => $path, 'baseurl' => $url, 'error' => false)); }