/**
 * Add inline css to display the component's single item cover image
 *
 * @since 2.4.0
 *
 * @param  bool $return True to get the inline css.
 * @return string|array the inline css or an associative array containing
 *                      the css rules and the style handle
 */
function bp_add_cover_image_inline_css($return = false)
{
    $bp = buddypress();
    // Find the component of the current item.
    if (bp_is_user()) {
        // User is not allowed to upload cover images
        // no need to carry on.
        if (bp_disable_cover_image_uploads()) {
            return;
        }
        $cover_image_object = array('component' => 'xprofile', 'object' => $bp->displayed_user);
    } elseif (bp_is_group()) {
        // Users are not allowed to upload cover images for their groups
        // no need to carry on.
        if (bp_disable_group_cover_image_uploads()) {
            return;
        }
        $cover_image_object = array('component' => 'groups', 'object' => $bp->groups->current_group);
    } else {
        $cover_image_object = apply_filters('bp_current_cover_image_object_inline_css', array());
    }
    // Bail if no component were found.
    if (empty($cover_image_object['component']) || empty($cover_image_object['object']) || !bp_is_active($cover_image_object['component'], 'cover_image')) {
        return;
    }
    // Get the settings of the cover image feature for the current component.
    $params = bp_attachments_get_cover_image_settings($cover_image_object['component']);
    // Bail if no params.
    if (empty($params)) {
        return;
    }
    // Try to call the callback.
    if (is_callable($params['callback'])) {
        $object_dir = $cover_image_object['component'];
        if ('xprofile' === $object_dir) {
            $object_dir = 'members';
        }
        $cover_image = bp_attachments_get_attachment('url', array('object_dir' => $object_dir, 'item_id' => $cover_image_object['object']->id));
        if (empty($cover_image)) {
            if (!empty($params['default_cover'])) {
                $cover_image = $params['default_cover'];
            }
        }
        $inline_css = call_user_func_array($params['callback'], array(array('cover_image' => esc_url_raw($cover_image), 'component' => sanitize_key($cover_image_object['component']), 'object_id' => (int) $cover_image_object['object']->id, 'width' => (int) $params['width'], 'height' => (int) $params['height'])));
        // Finally add the inline css to the handle.
        if (!empty($inline_css)) {
            // Used to get the css when Ajax setting the cover image.
            if (true === $return) {
                return array('css_rules' => '<style type="text/css">' . "\n" . $inline_css . "\n" . '</style>', 'handle' => $params['theme_handle']);
            }
            wp_add_inline_style($params['theme_handle'], $inline_css);
        } else {
            return false;
        }
    }
}
/**
 * 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));
    }
}