/**
 * Ajax delete a cover image for a given object and item id.
 *
 * @since 2.4.0
 *
 * @return string|null A json object containing success data if the cover image was deleted
 *                     error message otherwise.
 */
function bp_attachments_cover_image_ajax_delete()
{
    // Bail if not a POST action.
    if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) {
        wp_send_json_error();
    }
    $cover_image_data = $_POST;
    if (empty($cover_image_data['object']) || empty($cover_image_data['item_id'])) {
        wp_send_json_error();
    }
    // Check the nonce
    check_admin_referer('bp_delete_cover_image', 'nonce');
    // Capability check
    if (!bp_attachments_current_user_can('edit_cover_image', $cover_image_data)) {
        wp_send_json_error();
    }
    // Set object for the user's case
    if ('user' === $cover_image_data['object']) {
        $component = 'xprofile';
        $dir = 'members';
        // Set it for any other cases
    } else {
        $component = $cover_image_data['object'] . 's';
        $dir = $component;
    }
    // Handle delete
    if (bp_attachments_delete_file(array('item_id' => $cover_image_data['item_id'], 'object_dir' => $dir, 'type' => 'cover-image'))) {
        // Defaults no cover image
        $response = array('reset_url' => '', 'feedback_code' => 3);
        // Get cover image settings in case there's a default header
        $cover_params = bp_attachments_get_cover_image_settings($component);
        // Check if there's a default cover
        if (!empty($cover_params['default_cover'])) {
            $response['reset_url'] = $cover_params['default_cover'];
        }
        // Finally send the reset url
        wp_send_json_success($response);
    } else {
        wp_send_json_error(array('feedback_code' => 2));
    }
}
/**
 * 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));
    }
}
Beispiel #3
0
/**
 * 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));
    }
}