/** * get user/group cover photo * * @uses bcp_fetch_cover_photo * @return array * @param $args array */ function bcp_get_cover_photo($args = array()) { // fetch cover photo $cover_photo = bcp_fetch_cover_photo($args); $cover_photo_src = ""; // assign image size $size = isset($args['size']) ? $args['size'] : 'full'; $allowed_size = array('thumb', 'full'); // assign component (user/group) $allowed_component = array('user', 'group'); $type = isset($args['type']) ? $args['type'] : 'user'; // assign user component as default if (!in_array($type, $allowed_component)) { $type = 'user'; } // check if the size entere is allowed // use 'full' size for non valid size if (!in_array($size, $allowed_size)) { $size = 'full'; } if (!empty($cover_photo)) { if (isset($cover_photo)) { $cover_photo_src = $cover_photo[$size]; } } else { // use the default cover photo settings if there are no cover photo $theme_default = plugin_dir_url(__FILE__) . 'img/default.jpg'; $cover_photo_option = '__bcp_default_' . $type . '_cover_photo'; $saved_default_cover_photo = get_option($cover_photo_option); // if there are are cover photo set by the admin, use it. $default_cover_photo = empty($saved_default_cover_photo) ? $theme_default : $saved_default_cover_photo; $cover_photo_src = $default_cover_photo; } return $cover_photo_src; }
/** * cover photo subnav callback function. renders the title content and the template * files that are needed to process the upload and cropping of the photo * * @author dunhakdis<*****@*****.**> * @package bp-cover-photo * @since 1.0 * @return void */ function bcp_cover_photo_screen() { // store buddypress object to $bp // same as global $bp; $bp = buddypress(); // template directory $template = 'members'; // filter function for uploading images $upload_filter = 'xprofile_avatar_upload_dir'; // the id of the user $item_id = bp_displayed_user_id(); if (bp_is_group_single()) { $template = 'groups'; $upload_filter = 'groups_avatar_upload_dir'; $item_id = $bp->groups->current_group->id; } // load jcrop add_action('wp_enqueue_scripts', 'bp_cover_photo_scripts'); //add title and content here - last is to call the members plugin.php template add_action('bp_template_title', 'bp_cover_photo_screen_title'); add_action('bp_template_content', 'bp_cover_photo_screen_content'); // handle uploading of cover photo if (!empty($_FILES)) { // Check the nonce check_admin_referer('bp_avatar_upload'); // create avatar_admin object to prevent notices // from empty variables and objects if (!isset($bp->avatar_admin)) { $bp->avatar_admin = new stdClass(); } // Pass the file to the avatar upload handler $bp = buddypress(); if (bp_core_avatar_handle_upload($_FILES, $upload_filter)) { // adjust current step $bp->avatar_admin->step = 'crop-image'; } } // If the image cropping is done, crop the image and save a full/thumb version if (isset($_POST['avatar-crop-submit'])) { // Check the nonce check_admin_referer('bp_avatar_cropstore'); $groups_slug = bcp_get_groups_slug(); $args = array('item_id' => $item_id, 'original_file' => $_POST['image_src'], 'crop_x' => $_POST['x'], 'crop_y' => $_POST['y'], 'crop_w' => $_POST['w'], 'crop_h' => $_POST['h']); // change avatar path for groups if (bp_is_group_single()) { $args['avatar_dir'] = 'group-avatars'; } if (!bcp_core_avatar_handle_crop($args)) { bp_core_add_message(__('There was a problem cropping your cover photo.', 'buddypress'), 'error'); } else { // update the default cover photo image for groups if (isset($_POST['global-coverphoto'])) { $type = wp_kses($_POST['global-coverphoto'], array()); $args = array('object_id' => $item_id, 'type' => $type); $new_cover_photo = bcp_fetch_cover_photo($args); update_option('__bcp_default_' . $type . '_cover_photo', $new_cover_photo['full']); } if (bp_is_group_single()) { // if its a single group // redirect to group home page $current_displayed_group_url = trailingslashit(get_bloginfo('url') . '/' . $groups_slug . '/' . $bp->groups->current_group->slug . '/'); groups_update_groupmeta($item_id, 'cover-photo-timestamp', md5(time())); bp_core_redirect($current_displayed_group_url); } else { //otherwise, redirect to members profile update_user_meta($item_id, 'cover-photo-timestamp', md5(time())); bp_core_redirect(bp_displayed_user_domain()); } bp_core_add_message(__('Your new cover photo was uploaded successfully.', 'buddypress')); } } bp_core_load_template(apply_filters('bp_core_template_plugin', $template . '/single/plugins')); return; }