Ejemplo n.º 1
0
	/**
	 * populate()
	 *
	 * Populate the instantiated class with data based on the User ID provided.
	 *
	 * @package BuddyPress Core
 	 * @global $userdata WordPress user data for the current logged in user.
	 * @uses bp_core_get_userurl() Returns the URL with no HTML markup for a user based on their user id
	 * @uses bp_core_get_userlink() Returns a HTML formatted link for a user with the user's full name as the link text
	 * @uses bp_core_get_user_email() Returns the email address for the user based on user ID
	 * @uses get_user_meta() WordPress function returns the value of passed usermeta name from usermeta table
	 * @uses bp_core_fetch_avatar() Returns HTML formatted avatar for a user
	 * @uses bp_profile_last_updated_date() Returns the last updated date for a user.
	 */
	function populate() {
		if ( function_exists( 'xprofile_install' ) )
			$this->profile_data = $this->get_profile_data();

		if ( $this->profile_data ) {
			$this->user_url = bp_core_get_user_domain( $this->id, $this->profile_data['user_nicename'], $this->profile_data['user_login'] );
			$this->fullname = esc_attr( $this->profile_data[BP_XPROFILE_FULLNAME_FIELD_NAME]['field_data'] );
			$this->user_link = "<a href='{$this->user_url}' title='{$this->fullname}'>{$this->fullname}</a>";
			$this->email = esc_attr( $this->profile_data['user_email'] );
		} else {
			$this->user_url = bp_core_get_user_domain( $this->id );
			$this->user_link = bp_core_get_userlink( $this->id );
			$this->fullname = esc_attr( bp_core_get_user_displayname( $this->id ) );
			$this->email = esc_attr( bp_core_get_user_email( $this->id ) );
		}

		/* Cache a few things that are fetched often */
		wp_cache_set( 'bp_user_fullname_' . $this->id, $this->fullname, 'bp' );
		wp_cache_set( 'bp_user_email_' . $this->id, $this->email, 'bp' );
		wp_cache_set( 'bp_user_url_' . $this->id, $this->user_url, 'bp' );

		$this->avatar = bp_core_fetch_avatar( array( 'item_id' => $this->id, 'type' => 'full' ) );
		$this->avatar_thumb = bp_core_fetch_avatar( array( 'item_id' => $this->id, 'type' => 'thumb' ) );
		$this->avatar_mini = bp_core_fetch_avatar( array( 'item_id' => $this->id, 'type' => 'thumb', 'width' => 30, 'height' => 30 ) );

		$this->last_active = bp_core_get_last_activity( get_user_meta( $this->id, 'last_activity', true ), __( 'active %s ago', 'buddypress' ) );
	}
Ejemplo n.º 2
0
 /**
  * populate()
  *
  * Populate the instantiated class with data based on the User ID provided.
  * 
  * @package BuddyPress Core
  * @global $userdata WordPress user data for the current logged in user.
  * @uses bp_core_get_userurl() Returns the URL with no HTML markup for a user based on their user id
  * @uses bp_core_get_userlink() Returns a HTML formatted link for a user with the user's full name as the link text
  * @uses bp_core_get_user_email() Returns the email address for the user based on user ID
  * @uses get_usermeta() WordPress function returns the value of passed usermeta name from usermeta table
  * @uses bp_core_get_avatar() Returns HTML formatted avatar for a user
  * @uses bp_profile_last_updated_date() Returns the last updated date for a user.
  */
 function populate()
 {
     $this->user_url = bp_core_get_userurl($this->id);
     $this->user_link = bp_core_get_userlink($this->id);
     $this->fullname = bp_fetch_user_fullname($this->id, false);
     $this->email = bp_core_get_user_email($this->id);
     $this->last_active = bp_core_get_last_activity(get_usermeta($this->id, 'last_activity'), __('active %s ago', 'buddypress'));
     $this->avatar = bp_core_get_avatar($this->id, 2);
     $this->avatar_thumb = bp_core_get_avatar($this->id, 1);
     $this->avatar_mini = bp_core_get_avatar($this->id, 1, 25, 25, false);
 }
Ejemplo n.º 3
0
/**
 * Get an avatar for a BuddyPress object.
 *
 * Supports avatars for users, groups, and blogs by default, but can be
 * extended to support custom components as well.
 *
 * This function gives precedence to locally-uploaded avatars. When a local
 * avatar is not found, Gravatar is queried. To disable Gravatar fallbacks
 * locally:
 *    add_filter( 'bp_core_fetch_avatar_no_grav', '__return_true' );
 *
 * @since 2.4.0 Added 'extra_attr', 'scheme', 'rating' and 'force_default' for $args.
 *              These are inherited from WordPress 4.2.0. See {@link get_avatar()}.
 *
 * @param array|string $args {
 *     An array of arguments. All arguments are technically optional; some
 *     will, if not provided, be auto-detected by bp_core_fetch_avatar(). This
 *     auto-detection is described more below, when discussing specific
 *     arguments.
 *
 *     @type int|bool    $item_id    The numeric ID of the item for which you're requesting
 *                                   an avatar (eg, a user ID). If no 'item_id' is present,
 *                                   the function attempts to infer an ID from the 'object' + the
 *                                   current context: if 'object' is 'user' and the current page is a
 *                                   user page, 'item_id' will default to the displayed user ID; if
 *                                   'group' and on a group page, to the current group ID; if 'blog',
 *                                   to the current blog's ID. If no 'item_id' can be determined in
 *                                   this way, the function returns false. Default: false.
 *     @type string      $object     The kind of object for which you're getting an
 *                                   avatar. BuddyPress natively supports three options: 'user',
 *                                   'group', 'blog'; a plugin may register more.  Default: 'user'.
 *     @type string      $type       When a new avatar is uploaded to BP, 'thumb' and
 *                                   'full' versions are saved. This parameter specifies whether you'd
 *                                   like the 'full' or smaller 'thumb' avatar. Default: 'thumb'.
 *     @type string|bool $avatar_dir The name of the subdirectory where the
 *                                   requested avatar should be found. If no value is passed,
 *                                   'avatar_dir' is inferred from 'object': 'user' becomes 'avatars',
 *                                   'group' becomes 'group-avatars', 'blog' becomes 'blog-avatars'.
 *                                   Remember that this string denotes a subdirectory of BP's main
 *                                   avatar directory (usually based on {@link wp_upload_dir()}); it's a
 *                                   string like 'group-avatars' rather than the full directory path.
 *                                   Generally, it'll only be necessary to override the default value if
 *                                   storing avatars in a non-default location. Defaults to false
 *                                   (auto-detected).
 *     @type int|bool    $width      Requested avatar width. The unit is px. This value
 *                                   is used to build the 'width' attribute for the <img> element. If
 *                                   no value is passed, BP uses the global avatar width for this
 *                                   avatar type. Default: false (auto-detected).
 *     @type int|bool    $height     Requested avatar height. The unit is px. This
 *                                   value is used to build the 'height' attribute for the <img>
 *                                   element. If no value is passed, BP uses the global avatar height
 *                                   for this avatar type. Default: false (auto-detected).
 *     @type string      $class      The CSS class for the <img> element. Note that BP
 *                                   uses the 'avatar' class fairly extensively in its default styling,
 *                                   so if you plan to pass a custom value, consider appending it to
 *                                   'avatar' (eg 'avatar foo') rather than replacing it altogether.
 *                                   Default: 'avatar'.
 *     @type string|bool $css_id     The CSS id for the <img> element.
 *                                   Default: false.
 *     @type string      $title      The title attribute for the <img> element.
 *                                   Default: false.
 *     @type string      $alt        The alt attribute for the <img> element. In BP, this
 *                                   value is generally passed by the wrapper functions, where the data
 *                                   necessary for concatenating the string is at hand; see
 *                                   {@link bp_get_activity_avatar()} for an example. Default: ''.
 *     @type string|bool $email      An email to use in Gravatar queries. Unless
 *                                   otherwise configured, BP uses Gravatar as a fallback for avatars
 *                                   that are not provided locally. Gravatar's API requires using a hash
 *                                   of the user's email address; this argument provides it. If not
 *                                   provided, the function will infer it: for users, by getting the
 *                                   user's email from the database, for groups/blogs, by concatenating
 *                                   "{$item_id}-{$object}@{bp_get_root_domain()}". The user query adds
 *                                   overhead, so it's recommended that wrapper functions provide a
 *                                   value for 'email' when querying user IDs. Default: false.
 *     @type bool       $no_grav     Whether to disable the default Gravatar fallback.
 *                                   By default, BP will fall back on Gravatar when it cannot find a
 *                                   local avatar. In some cases, this may be undesirable, in which
 *                                   case 'no_grav' should be set to true. To disable Gravatar
 *                                   fallbacks globally, see the 'bp_core_fetch_avatar_no_grav' filter.
 *                                   Default: false.
 *     @type bool       $html        Whether to return an <img> HTML element, vs a raw URL
 *                                   to an avatar. If false, <img>-specific arguments (like 'css_id')
 *                                   will be ignored. Default: true.
 *     @type string     $extra_attr  HTML attributes to insert in the IMG element. Not sanitized. Default: ''.
 *     @type string     $scheme      URL scheme to use. See set_url_scheme() for accepted values.
 *                                   Default null.
 *     @type string     $rating      What rating to display Gravatars for. Accepts 'G', 'PG', 'R', 'X'.
 *                                   Default is the value of the 'avatar_rating' option.
 *     @type bool       $force_default Used when creating the Gravatar URL. Whether to force the default
 *                                     image regardless if the Gravatar exists. Default: false.
 * }
 *
 * @return string Formatted HTML <img> element, or raw avatar URL based on $html arg.
 */
function bp_core_fetch_avatar($args = '')
{
    $bp = buddypress();
    // If avatars are disabled for the root site, obey that request and bail
    if (!$bp->avatar->show_avatars) {
        return;
    }
    global $current_blog;
    // Set the default variables array and parse it against incoming $args array.
    $params = wp_parse_args($args, array('item_id' => false, 'object' => 'user', 'type' => 'thumb', 'avatar_dir' => false, 'width' => false, 'height' => false, 'class' => 'avatar', 'css_id' => false, 'alt' => '', 'email' => false, 'no_grav' => false, 'html' => true, 'title' => '', 'extra_attr' => '', 'scheme' => null, 'rating' => get_option('avatar_rating'), 'force_default' => false));
    /** Set item_id ***********************************************************/
    if (empty($params['item_id'])) {
        switch ($params['object']) {
            case 'blog':
                $params['item_id'] = $current_blog->id;
                break;
            case 'group':
                if (bp_is_active('groups')) {
                    $params['item_id'] = $bp->groups->current_group->id;
                } else {
                    $params['item_id'] = false;
                }
                break;
            case 'user':
            default:
                $params['item_id'] = bp_displayed_user_id();
                break;
        }
        /**
         * Filters the ID of the item being requested.
         *
         * @since 1.1.0
         *
         * @param string $value  ID of avatar item being requested.
         * @param string $value  Avatar type being requested.
         * @param array  $params Array of parameters for the request.
         */
        $params['item_id'] = apply_filters('bp_core_avatar_item_id', $params['item_id'], $params['object'], $params);
        if (empty($params['item_id'])) {
            return false;
        }
    }
    /** Set avatar_dir ********************************************************/
    if (empty($params['avatar_dir'])) {
        switch ($params['object']) {
            case 'blog':
                $params['avatar_dir'] = 'blog-avatars';
                break;
            case 'group':
                if (bp_is_active('groups')) {
                    $params['avatar_dir'] = 'group-avatars';
                } else {
                    $params['avatar_dir'] = false;
                }
                break;
            case 'user':
            default:
                $params['avatar_dir'] = 'avatars';
                break;
        }
        /**
         * Filters the avatar directory to use.
         *
         * @since 1.1.0
         *
         * @param string $value  Name of the subdirectory where the requested avatar should be found.
         * @param string $value  Avatar type being requested.
         * @param array  $params Array of parameters for the request.
         */
        $params['avatar_dir'] = apply_filters('bp_core_avatar_dir', $params['avatar_dir'], $params['object'], $params);
        if (empty($params['avatar_dir'])) {
            return false;
        }
    }
    /** <img> alt *************************************************************/
    if (false !== strpos($params['alt'], '%s') || false !== strpos($params['alt'], '%1$s')) {
        switch ($params['object']) {
            case 'blog':
                $item_name = get_blog_option($params['item_id'], 'blogname');
                break;
            case 'group':
                $item_name = bp_get_group_name(groups_get_group(array('group_id' => $params['item_id'])));
                break;
            case 'user':
            default:
                $item_name = bp_core_get_user_displayname($params['item_id']);
                break;
        }
        /**
         * Filters the alt attribute value to be applied to avatar.
         *
         * @since 1.5.0
         *
         * @param string $value  alt to be applied to avatar.
         * @param string $value  ID of avatar item being requested.
         * @param string $value  Avatar type being requested.
         * @param array  $params Array of parameters for the request.
         */
        $item_name = apply_filters('bp_core_avatar_alt', $item_name, $params['item_id'], $params['object'], $params);
        $params['alt'] = sprintf($params['alt'], $item_name);
    }
    /** Sanity Checks *********************************************************/
    // Get a fallback for the 'alt' parameter, create html output.
    if (empty($params['alt'])) {
        $params['alt'] = __('Profile Photo', 'buddypress');
    }
    $html_alt = ' alt="' . esc_attr($params['alt']) . '"';
    // Filter image title and create html string.
    $html_title = '';
    /**
     * Filters the title attribute value to be applied to avatar.
     *
     * @since 1.5.0
     *
     * @param string $value  Title to be applied to avatar.
     * @param string $value  ID of avatar item being requested.
     * @param string $value  Avatar type being requested.
     * @param array  $params Array of parameters for the request.
     */
    $params['title'] = apply_filters('bp_core_avatar_title', $params['title'], $params['item_id'], $params['object'], $params);
    if (!empty($params['title'])) {
        $html_title = ' title="' . esc_attr($params['title']) . '"';
    }
    // Extra attributes
    $extra_attr = !empty($args['extra_attr']) ? ' ' . $args['extra_attr'] : '';
    // Set CSS ID and create html string.
    $html_css_id = '';
    /**
     * Filters the ID attribute to be applied to avatar.
     *
     * @since 2.2.0
     *
     * @param string $value  ID to be applied to avatar.
     * @param string $value  ID of avatar item being requested.
     * @param string $value  Avatar type being requested.
     * @param array  $params Array of parameters for the request.
     */
    $params['css_id'] = apply_filters('bp_core_css_id', $params['css_id'], $params['item_id'], $params['object'], $params);
    if (!empty($params['css_id'])) {
        $html_css_id = ' id="' . esc_attr($params['css_id']) . '"';
    }
    // Set image width
    if (false !== $params['width']) {
        // Width has been specified. No modification necessary.
    } elseif ('thumb' == $params['type']) {
        $params['width'] = bp_core_avatar_thumb_width();
    } else {
        $params['width'] = bp_core_avatar_full_width();
    }
    $html_width = ' width="' . $params['width'] . '"';
    // Set image height
    if (false !== $params['height']) {
        // Height has been specified. No modification necessary.
    } elseif ('thumb' == $params['type']) {
        $params['height'] = bp_core_avatar_thumb_height();
    } else {
        $params['height'] = bp_core_avatar_full_height();
    }
    $html_height = ' height="' . $params['height'] . '"';
    /**
     * Filters the classes to be applied to the avatar.
     *
     * @since 1.6.0
     *
     * @param array|string $value  Class(es) to be applied to the avatar.
     * @param string       $value  ID of the avatar item being requested.
     * @param string       $value  Avatar type being requested.
     * @param array        $params Array of parameters for the request.
     */
    $params['class'] = apply_filters('bp_core_avatar_class', $params['class'], $params['item_id'], $params['object'], $params);
    // Use an alias to leave the param unchanged
    $avatar_classes = $params['class'];
    if (!is_array($avatar_classes)) {
        $avatar_classes = explode(' ', $avatar_classes);
    }
    // merge classes
    $avatar_classes = array_merge($avatar_classes, array($params['object'] . '-' . $params['item_id'] . '-avatar', 'avatar-' . $params['width']));
    // Sanitize each class
    $avatar_classes = array_map('sanitize_html_class', $avatar_classes);
    // populate the class attribute
    $html_class = ' class="' . join(' ', $avatar_classes) . ' photo"';
    // Set img URL and DIR based on prepopulated constants
    $avatar_loc = new stdClass();
    $avatar_loc->path = trailingslashit(bp_core_avatar_upload_path());
    $avatar_loc->url = trailingslashit(bp_core_avatar_url());
    $avatar_loc->dir = trailingslashit($params['avatar_dir']);
    /**
     * Filters the avatar folder directory URL.
     *
     * @since 1.1.0
     *
     * @param string $value Path to the avatar folder URL.
     * @param int    $value ID of the avatar item being requested.
     * @param string $value Avatar type being requested.
     * @param string $value Subdirectory where the requested avatar should be found.
     */
    $avatar_folder_url = apply_filters('bp_core_avatar_folder_url', $avatar_loc->url . $avatar_loc->dir . $params['item_id'], $params['item_id'], $params['object'], $params['avatar_dir']);
    /**
     * Filters the avatar folder directory path.
     *
     * @since 1.1.0
     *
     * @param string $value Path to the avatar folder directory.
     * @param int    $value ID of the avatar item being requested.
     * @param string $value Avatar type being requested.
     * @param string $value Subdirectory where the requested avatar should be found.
     */
    $avatar_folder_dir = apply_filters('bp_core_avatar_folder_dir', $avatar_loc->path . $avatar_loc->dir . $params['item_id'], $params['item_id'], $params['object'], $params['avatar_dir']);
    /**
     * Look for uploaded avatar first. Use it if it exists.
     * Set the file names to search for, to select the full size
     * or thumbnail image.
     */
    $avatar_size = 'full' == $params['type'] ? '-bpfull' : '-bpthumb';
    $legacy_user_avatar_name = 'full' == $params['type'] ? '-avatar2' : '-avatar1';
    $legacy_group_avatar_name = 'full' == $params['type'] ? '-groupavatar-full' : '-groupavatar-thumb';
    // Check for directory
    if (file_exists($avatar_folder_dir)) {
        // Open directory
        if ($av_dir = opendir($avatar_folder_dir)) {
            // Stash files in an array once to check for one that matches
            $avatar_files = array();
            while (false !== ($avatar_file = readdir($av_dir))) {
                // Only add files to the array (skip directories)
                if (2 < strlen($avatar_file)) {
                    $avatar_files[] = $avatar_file;
                }
            }
            // Check for array
            if (0 < count($avatar_files)) {
                // Check for current avatar
                foreach ($avatar_files as $key => $value) {
                    if (strpos($value, $avatar_size) !== false) {
                        $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
                    }
                }
                // Legacy avatar check
                if (!isset($avatar_url)) {
                    foreach ($avatar_files as $key => $value) {
                        if (strpos($value, $legacy_user_avatar_name) !== false) {
                            $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
                        }
                    }
                    // Legacy group avatar check
                    if (!isset($avatar_url)) {
                        foreach ($avatar_files as $key => $value) {
                            if (strpos($value, $legacy_group_avatar_name) !== false) {
                                $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
                            }
                        }
                    }
                }
            }
        }
        // Close the avatar directory
        closedir($av_dir);
        // If we found a locally uploaded avatar
        if (isset($avatar_url)) {
            // Support custom scheme
            $avatar_url = set_url_scheme($avatar_url, $params['scheme']);
            // Return it wrapped in an <img> element
            if (true === $params['html']) {
                /**
                 * Filters an avatar URL wrapped in an <img> element.
                 *
                 * @since 1.1.0
                 *
                 * @param string $value             Full <img> element for an avatar.
                 * @param array  $params            Array of parameters for the request.
                 * @param string $value             ID of the item requested.
                 * @param string $value             Subdirectory where the requested avatar should be found.
                 * @param string $html_css_id       ID attribute for avatar.
                 * @param string $html_width        Width attribute for avatar.
                 * @param string $html_height       Height attribtue for avatar.
                 * @param string $avatar_folder_url Avatar URL path.
                 * @param string $avatar_folder_dir Avatar dir path.
                 */
                return apply_filters('bp_core_fetch_avatar', '<img src="' . $avatar_url . '"' . $html_class . $html_css_id . $html_width . $html_height . $html_alt . $html_title . $extra_attr . ' />', $params, $params['item_id'], $params['avatar_dir'], $html_css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir);
                // ...or only the URL
            } else {
                /**
                 * Filters a locally uploaded avatar URL.
                 *
                 * @since 1.2.5
                 *
                 * @param string $avatar_url URL for a locally uploaded avatar.
                 * @param array  $params     Array of parameters for the request.
                 */
                return apply_filters('bp_core_fetch_avatar_url', $avatar_url, $params);
            }
        }
    }
    /**
     * Filters whether or not to skip Gravatar check.
     *
     * @since 1.5.0
     *
     * @param bool  $value  Whether or not to skip Gravatar.
     * @param array $params Array of parameters for the avatar request.
     */
    if (!apply_filters('bp_core_fetch_avatar_no_grav', $params['no_grav'], $params)) {
        // Set gravatar type
        if (empty($bp->grav_default->{$params['object']})) {
            $default_grav = 'wavatar';
        } elseif ('mystery' == $bp->grav_default->{$params['object']}) {
            /**
             * Filters the Mystery person avatar src value.
             *
             * @since 1.2.0
             *
             * @param string $value Avatar value.
             * @param string $value Width to display avatar at.
             */
            $default_grav = apply_filters('bp_core_mysteryman_src', 'mm', $params['width']);
        } else {
            $default_grav = $bp->grav_default->{$params['object']};
        }
        // Set gravatar object
        if (empty($params['email'])) {
            if ('user' == $params['object']) {
                $params['email'] = bp_core_get_user_email($params['item_id']);
            } elseif ('group' == $params['object'] || 'blog' == $params['object']) {
                $params['email'] = $params['item_id'] . '-' . $params['object'] . '@' . bp_get_root_domain();
            }
        }
        /**
         * Filters the Gravatar email to use.
         *
         * @since 1.1.0
         *
         * @param string $value Email to use in Gravatar request.
         * @param string $value ID of the item being requested.
         * @param string $value Object type being requested.
         */
        $params['email'] = apply_filters('bp_core_gravatar_email', $params['email'], $params['item_id'], $params['object']);
        /**
         * Filters the Gravatar URL host.
         *
         * @since 1.0.2
         *
         * @param string $value Gravatar URL host.
         */
        $gravatar = apply_filters('bp_gravatar_url', '//www.gravatar.com/avatar/');
        // Append email hash to Gravatar
        $gravatar .= md5(strtolower($params['email']));
        // Main Gravatar URL args
        $url_args = array('s' => $params['width']);
        // Custom Gravatar URL args
        if (!empty($params['force_default'])) {
            $url_args['f'] = 'y';
        }
        if (!empty($params['rating'])) {
            $url_args['r'] = strtolower($params['rating']);
        }
        // Only set default image if 'Gravatar Logo' is not requested
        if ('gravatar_default' !== $default_grav) {
            $url_args['d'] = $default_grav;
        }
        // Set up the Gravatar URL
        $gravatar = esc_url(add_query_arg(rawurlencode_deep(array_filter($url_args)), $gravatar));
        // No avatar was found, and we've been told not to use a gravatar.
    } else {
        /**
         * Filters the avatar default when Gravatar is not used.
         *
         * This is a variable filter dependent on the avatar type being requested.
         *
         * @since 1.5.0
         *
         * @param string $value  Default avatar for non-gravatar requests.
         * @param array  $params Array of parameters for the avatar request.
         */
        $gravatar = apply_filters('bp_core_default_avatar_' . $params['object'], bp_core_avatar_default('local'), $params);
    }
    if (true === $params['html']) {
        /** This filter is documented in bp-core/bp-core-avatars.php */
        return apply_filters('bp_core_fetch_avatar', '<img src="' . $gravatar . '"' . $html_css_id . $html_class . $html_width . $html_height . $html_alt . $html_title . $extra_attr . ' />', $params, $params['item_id'], $params['avatar_dir'], $html_css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir);
    } else {
        /** This filter is documented in bp-core/bp-core-avatars.php */
        return apply_filters('bp_core_fetch_avatar_url', $gravatar, $params);
    }
}
Ejemplo n.º 4
0
/**
 * Get an avatar for a BuddyPress object.
 *
 * Supports avatars for users, groups, and blogs by default, but can be
 * extended to support custom components as well.
 *
 * This function gives precedence to locally-uploaded avatars. When a local
 * avatar is not found, Gravatar is queried. To disable Gravatar fallbacks
 * locally:
 *    add_filter( 'bp_core_fetch_avatar_no_grav', '__return_true' );
 *
 * @param array $args {
 *     An array of arguments. All arguments are technically optional; some
 *     will, if not provided, be auto-detected by bp_core_fetch_avatar(). This
 *     auto-detection is described more below, when discussing specific
 *     arguments.
 *
 *     @type int|bool $item_id The numeric ID of the item for which you're
 *           requesting an avatar (eg, a user ID). If no 'item_id' is present,
 *           the function attempts to infer an ID from the 'object' + the
 *           current context: if 'object' is 'user' and the current page is a
 *           user page, 'item_id' will default to the displayed user ID; if
 *           'group' and on a group page, to the current group ID; if 'blog',
 *           to the current blog's ID. If no 'item_id' can be determined in
 *           this way, the function returns false. Default: false.
 *     @type string $object The kind of object for which you're getting an
 *           avatar. BuddyPress natively supports three options: 'user',
 *           'group', 'blog'; a plugin may register more.  Default: 'user'.
 *     @type string $type When a new avatar is uploaded to BP, 'thumb' and
 *           'full' versions are saved. This parameter specifies whether you'd
 *           like the 'full' or smaller 'thumb' avatar. Default: 'thumb'.
 *     @type string|bool $avatar_dir The name of the subdirectory where the
 *           requested avatar should be found. If no value is passed,
 *           'avatar_dir' is inferred from 'object': 'user' becomes 'avatars',
 *           'group' becomes 'group-avatars', 'blog' becomes 'blog-avatars'.
 *           Remember that this string denotes a subdirectory of BP's main
 *           avatar directory (usually based on {@link wp_upload_dir()}); it's a
 *           string like 'group-avatars' rather than the full directory path.
 *           Generally, it'll only be necessary to override the default value if
 *           storing avatars in a non-default location. Defaults to false
 *           (auto-detected).
 *     @type int|bool $width Requested avatar width. The unit is px. This value
 *           is used to build the 'width' attribute for the <img> element. If
 *           no value is passed, BP uses the global avatar width for this
 *           avatar type. Default: false (auto-detected).
 *     @type int|bool $height Requested avatar height. The unit is px. This
 *           value is used to build the 'height' attribute for the <img>
 *           element. If no value is passed, BP uses the global avatar height
 *           for this avatar type. Default: false (auto-detected).
 *     @type string $class The CSS class for the <img> element. Note that BP
 *           uses the 'avatar' class fairly extensively in its default styling,
 *           so if you plan to pass a custom value, consider appending it to
 *           'avatar' (eg 'avatar foo') rather than replacing it altogether.
 *           Default: 'avatar'.
 *     @type string|bool $css_id The CSS id for the <img> element.
 *           Default: false.
 *     @type string $title The title attribute for the <img> element.
 *           Default: false.
 *     @type string $alt The alt attribute for the <img> element. In BP, this
 *           value is generally passed by the wrapper functions, where the data
 *           necessary for concatenating the string is at hand; see
 *           {@link bp_get_activity_avatar()} for an example. Default: ''.
 *     @type string|bool $email An email to use in Gravatar queries. Unless
 *           otherwise configured, BP uses Gravatar as a fallback for avatars
 *           that are not provided locally. Gravatar's API requires using a hash
 *           of the user's email address; this argument provides it. If not
 *           provided, the function will infer it: for users, by getting the
 *           user's email from the database, for groups/blogs, by concatenating
 *           "{$item_id}-{$object}@{bp_get_root_domain()}". The user query adds
 *           overhead, so it's recommended that wrapper functions provide a
 *           value for 'email' when querying user IDs. Default: false.
 *     @type bool $no_grav Whether to disable the default Gravatar fallback.
 *           By default, BP will fall back on Gravatar when it cannot find a
 *           local avatar. In some cases, this may be undesirable, in which
 *           case 'no_grav' should be set to true. To disable Gravatar
 *           fallbacks globally, see the 'bp_core_fetch_avatar_no_grav' filter.
 *           Default: false.
 *     @type bool $html Whether to return an <img> HTML element, vs a raw URL
 *           to an avatar. If false, <img>-specific arguments (like 'css_id')
 *           will be ignored. Default: true.
 * }
 * @return string Formatted HTML <img> element, or raw avatar URL based on $html arg.
 */
function bp_core_fetch_avatar($args = '')
{
    // If avatars are disabled for the root site, obey that request and bail
    if (!buddypress()->avatar->show_avatars) {
        return;
    }
    global $current_blog;
    $bp = buddypress();
    // Set a few default variables
    $def_object = 'user';
    $def_type = 'thumb';
    $def_class = 'avatar';
    // Set the default variables array
    $params = wp_parse_args($args, array('item_id' => false, 'object' => $def_object, 'type' => $def_type, 'avatar_dir' => false, 'width' => false, 'height' => false, 'class' => $def_class, 'css_id' => false, 'alt' => '', 'email' => false, 'no_grav' => false, 'html' => true, 'title' => ''));
    extract($params, EXTR_SKIP);
    /** Set item_id ***********************************************************/
    if (empty($item_id)) {
        switch ($object) {
            case 'blog':
                $item_id = $current_blog->id;
                break;
            case 'group':
                if (bp_is_active('groups')) {
                    $item_id = $bp->groups->current_group->id;
                } else {
                    $item_id = false;
                }
                break;
            case 'user':
            default:
                $item_id = bp_displayed_user_id();
                break;
        }
        $item_id = apply_filters('bp_core_avatar_item_id', $item_id, $object, $params);
        if (empty($item_id)) {
            return false;
        }
    }
    $class = apply_filters('bp_core_avatar_class', $class, $item_id, $object, $params);
    /** Set avatar_dir ********************************************************/
    if (empty($avatar_dir)) {
        switch ($object) {
            case 'blog':
                $avatar_dir = 'blog-avatars';
                break;
            case 'group':
                if (bp_is_active('groups')) {
                    $avatar_dir = 'group-avatars';
                } else {
                    $avatar_dir = false;
                }
                break;
            case 'user':
            default:
                $avatar_dir = 'avatars';
                break;
        }
        $avatar_dir = apply_filters('bp_core_avatar_dir', $avatar_dir, $object, $params);
        if (empty($avatar_dir)) {
            return false;
        }
    }
    /** <img> alt *************************************************************/
    if (false !== strpos($alt, '%s') || false !== strpos($alt, '%1$s')) {
        switch ($object) {
            case 'blog':
                $item_name = get_blog_option($item_id, 'blogname');
                break;
            case 'group':
                $item_name = bp_get_group_name(groups_get_group(array('group_id' => $item_id)));
                break;
            case 'user':
            default:
                $item_name = bp_core_get_user_displayname($item_id);
                break;
        }
        $item_name = apply_filters('bp_core_avatar_alt', $item_name, $item_id, $object, $params);
        $alt = sprintf($alt, $item_name);
    }
    /** Sanity Checks *********************************************************/
    // Get a fallback for the 'alt' parameter
    if (empty($alt)) {
        $alt = __('Profile Photo', 'buddypress');
    }
    $html_alt = ' alt="' . esc_attr($alt) . '"';
    // Set title tag, if it's been provided
    if (!empty($title)) {
        $title = " title='" . esc_attr(apply_filters('bp_core_avatar_title', $title, $item_id, $object, $params)) . "'";
    }
    // Set CSS ID if passed
    if (!empty($css_id)) {
        $css_id = ' id="' . esc_attr($css_id) . '"';
    }
    // Set image width
    if (false !== $width) {
        $html_width = ' width="' . $width . '"';
    } elseif ('thumb' == $type) {
        $html_width = ' width="' . bp_core_avatar_thumb_width() . '"';
    } else {
        $html_width = ' width="' . bp_core_avatar_full_width() . '"';
    }
    // Set image height
    if (false !== $height) {
        $html_height = ' height="' . $height . '"';
    } elseif ('thumb' == $type) {
        $html_height = ' height="' . bp_core_avatar_thumb_height() . '"';
    } else {
        $html_height = ' height="' . bp_core_avatar_full_height() . '"';
    }
    // Set img URL and DIR based on prepopulated constants
    $avatar_loc = new stdClass();
    $avatar_loc->path = trailingslashit(bp_core_avatar_upload_path());
    $avatar_loc->url = trailingslashit(bp_core_avatar_url());
    $avatar_loc->dir = trailingslashit($avatar_dir);
    $avatar_folder_url = apply_filters('bp_core_avatar_folder_url', $avatar_loc->url . $avatar_loc->dir . $item_id, $item_id, $object, $avatar_dir);
    $avatar_folder_dir = apply_filters('bp_core_avatar_folder_dir', $avatar_loc->path . $avatar_loc->dir . $item_id, $item_id, $object, $avatar_dir);
    // Add an identifying class
    $class .= ' ' . $object . '-' . $item_id . '-avatar ' . sanitize_html_class("avatar-{$width}") . ' photo';
    /**
     * Look for uploaded avatar first. Use it if it exists.
     * Set the file names to search for, to select the full size
     * or thumbnail image.
     */
    $avatar_size = 'full' == $type ? '-bpfull' : '-bpthumb';
    $legacy_user_avatar_name = 'full' == $type ? '-avatar2' : '-avatar1';
    $legacy_group_avatar_name = 'full' == $type ? '-groupavatar-full' : '-groupavatar-thumb';
    // Check for directory
    if (file_exists($avatar_folder_dir)) {
        // Open directory
        if ($av_dir = opendir($avatar_folder_dir)) {
            // Stash files in an array once to check for one that matches
            $avatar_files = array();
            while (false !== ($avatar_file = readdir($av_dir))) {
                // Only add files to the array (skip directories)
                if (2 < strlen($avatar_file)) {
                    $avatar_files[] = $avatar_file;
                }
            }
            // Check for array
            if (0 < count($avatar_files)) {
                // Check for current avatar
                foreach ($avatar_files as $key => $value) {
                    if (strpos($value, $avatar_size) !== false) {
                        $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
                    }
                }
                // Legacy avatar check
                if (!isset($avatar_url)) {
                    foreach ($avatar_files as $key => $value) {
                        if (strpos($value, $legacy_user_avatar_name) !== false) {
                            $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
                        }
                    }
                    // Legacy group avatar check
                    if (!isset($avatar_url)) {
                        foreach ($avatar_files as $key => $value) {
                            if (strpos($value, $legacy_group_avatar_name) !== false) {
                                $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
                            }
                        }
                    }
                }
            }
        }
        // Close the avatar directory
        closedir($av_dir);
        // If we found a locally uploaded avatar
        if (isset($avatar_url)) {
            // Return it wrapped in an <img> element
            if (true === $html) {
                return apply_filters('bp_core_fetch_avatar', '<img src="' . $avatar_url . '" class="' . esc_attr($class) . '"' . $css_id . $html_width . $html_height . $html_alt . $title . ' />', $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir);
                // ...or only the URL
            } else {
                return apply_filters('bp_core_fetch_avatar_url', $avatar_url, $params);
            }
        }
    }
    // If no avatars could be found, try to display a gravatar
    // Skips gravatar check if $no_grav is passed
    if (!apply_filters('bp_core_fetch_avatar_no_grav', $no_grav)) {
        // Set gravatar size
        if (false !== $width) {
            $grav_size = $width;
        } else {
            if ('full' == $type) {
                $grav_size = bp_core_avatar_full_width();
            } else {
                if ('thumb' == $type) {
                    $grav_size = bp_core_avatar_thumb_width();
                }
            }
        }
        // Set gravatar type
        if (empty($bp->grav_default->{$object})) {
            $default_grav = 'wavatar';
        } else {
            if ('mystery' == $bp->grav_default->{$object}) {
                $default_grav = apply_filters('bp_core_mysteryman_src', 'mm', $grav_size);
            } else {
                $default_grav = $bp->grav_default->{$object};
            }
        }
        // Set gravatar object
        if (empty($email)) {
            if ('user' == $object) {
                $email = bp_core_get_user_email($item_id);
            } else {
                if ('group' == $object || 'blog' == $object) {
                    $email = "{$item_id}-{$object}@{bp_get_root_domain()}";
                }
            }
        }
        // Set host based on if using ssl
        $host = 'http://gravatar.com/avatar/';
        if (is_ssl()) {
            $host = 'https://secure.gravatar.com/avatar/';
        }
        // Filter gravatar vars
        $email = apply_filters('bp_core_gravatar_email', $email, $item_id, $object);
        $gravatar = apply_filters('bp_gravatar_url', $host) . md5(strtolower($email)) . '?d=' . $default_grav . '&amp;s=' . $grav_size;
        // Gravatar rating; http://bit.ly/89QxZA
        $rating = get_option('avatar_rating');
        if (!empty($rating)) {
            $gravatar .= "&amp;r={$rating}";
        }
        // No avatar was found, and we've been told not to use a gravatar.
    } else {
        $gravatar = apply_filters("bp_core_default_avatar_{$object}", bp_core_avatar_default('local'), $params);
    }
    if (true === $html) {
        return apply_filters('bp_core_fetch_avatar', '<img src="' . $gravatar . '" class="' . esc_attr($class) . '"' . $css_id . $html_width . $html_height . $html_alt . $title . ' />', $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir);
    } else {
        return apply_filters('bp_core_fetch_avatar_url', $gravatar, $params);
    }
}
Ejemplo n.º 5
0
/**
 * bp_core_fetch_avatar()
 *
 * Fetches an avatar from a BuddyPress object. Supports user/group/blog as
 * default, but can be extended to include your own custom components too.
 *
 * @global object $bp
 * @global object $current_blog
 * @param array $args Determine the output of this function
 * @return string Formatted HTML <img> element, or raw avatar URL based on $html arg
 */
function bp_core_fetch_avatar( $args = '' ) {
	global $bp, $current_blog;

	// Set a few default variables
	$def_object		= 'user';
	$def_type		= 'thumb';
	$def_class		= 'avatar';
	$def_alt		= __( 'Avatar Image', 'buddypress' );

	// Set the default variables array
	$defaults = array(
		'item_id'		=> false,
		'object'		=> $def_object,	// user/group/blog/custom type (if you use filters)
		'type'			=> $def_type,	// thumb or full
		'avatar_dir'	=> false,		// Specify a custom avatar directory for your object
		'width'			=> false,		// Custom width (int)
		'height'		=> false,		// Custom height (int)
		'class'			=> $def_class,	// Custom <img> class (string)
		'css_id'		=> false,		// Custom <img> ID (string)
		'alt'			=> $def_alt,	// Custom <img> alt (string)
		'email'			=> false,		// Pass the user email (for gravatar) to prevent querying the DB for it
		'no_grav'		=> false,		// If there is no avatar found, return false instead of a grav?
		'html'			=> true			// Wrap the return img URL in <img />
	);

	// Compare defaults to passed and extract
	$params = wp_parse_args( $args, $defaults );
	extract( $params, EXTR_SKIP );

	// Set item_id if not passed
	if ( !$item_id ) {
		if ( 'user' == $object )
			$item_id = $bp->displayed_user->id;
		else if ( 'group' == $object )
			$item_id = $bp->groups->current_group->id;
		else if ( 'blog' == $object )
			$item_id = $current_blog->id;

		$item_id = apply_filters( 'bp_core_avatar_item_id', $item_id, $object );

		if ( !$item_id ) return false;
	}

	// Set avatar_dir if not passed (uses $object)
	if ( !$avatar_dir ) {
		if ( 'user' == $object )
			$avatar_dir = 'avatars';
		else if ( 'group' == $object )
			$avatar_dir = 'group-avatars';
		else if ( 'blog' == $object )
			$avatar_dir = 'blog-avatars';

		$avatar_dir = apply_filters( 'bp_core_avatar_dir', $avatar_dir, $object );

		if ( !$avatar_dir ) return false;
	}

	// Add an identifying class to each item
	$class .= ' ' . $object . '-' . $item_id . '-avatar';

	// Set CSS ID if passed
	if ( !empty( $css_id ) )
		$css_id = " id='{$css_id}'";

	// Set avatar width
	if ( $width )
		$html_width = " width='{$width}'";
	else
		$html_width = ( 'thumb' == $type ) ? ' width="' . BP_AVATAR_THUMB_WIDTH . '"' : ' width="' . BP_AVATAR_FULL_WIDTH . '"';

	// Set avatar height
	if ( $height )
		$html_height = " height='{$height}'";
	else
		$html_height = ( 'thumb' == $type ) ? ' height="' . BP_AVATAR_THUMB_HEIGHT . '"' : ' height="' . BP_AVATAR_FULL_HEIGHT . '"';

	// Set avatar URL and DIR based on prepopulated constants
	$avatar_folder_url = apply_filters( 'bp_core_avatar_folder_url', BP_AVATAR_URL . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir );
	$avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', BP_AVATAR_UPLOAD_PATH . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir );

	/****
	 * Look for uploaded avatar first. Use it if it exists.
	 * Set the file names to search for, to select the full size
	 * or thumbnail image.
	 */
	$avatar_size = ( 'full' == $type ) ? '-bpfull' : '-bpthumb';
	$legacy_user_avatar_name = ( 'full' == $type ) ? '-avatar2' : '-avatar1';
	$legacy_group_avatar_name = ( 'full' == $type ) ? '-groupavatar-full' : '-groupavatar-thumb';

	// Check for directory
	if ( file_exists( $avatar_folder_dir ) ) {

		// Open directory
		if ( $av_dir = opendir( $avatar_folder_dir ) ) {

			// Stash files in an array once to check for one that matches
			$avatar_files = array();
			while ( false !== ( $avatar_file = readdir($av_dir) ) ) {
				// Only add files to the array (skip directories)
				if ( 2 < strlen( $avatar_file ) )
					$avatar_files[] = $avatar_file;
			}

			// Check for array
			if ( 0 < count( $avatar_files ) ) {

				// Check for current avatar
				foreach( $avatar_files as $key => $value ) {
					if ( strpos ( $value, $avatar_size )!== false )
						$avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
				}

				// Legacy avatar check
				if ( !isset( $avatar_url ) ) {
					foreach( $avatar_files as $key => $value ) {
						if ( strpos ( $value, $legacy_user_avatar_name )!== false )
							$avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
					}

					// Legacy group avatar check
					if ( !isset( $avatar_url ) ) {
						foreach( $avatar_files as $key => $value ) {
							if ( strpos ( $value, $legacy_group_avatar_name )!== false )
								$avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
						}
					}
				}
			}
		}

		// Close the avatar directory
		closedir( $av_dir );

		// If we found a locally uploaded avatar
		if ( $avatar_url ) {

			// Return it wrapped in an <img> element
			if ( true === $html ) {
				return apply_filters( 'bp_core_fetch_avatar', '<img src="' . $avatar_url . '" alt="' . $alt . '" class="' . $class . '"' . $css_id . $html_width . $html_height . ' />', $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );

			// ...or only the URL
			} else {
				return apply_filters( 'bp_core_fetch_avatar_url', $avatar_url );
			}
		}
	}

	// If no avatars could be found, try to display a gravatar

	// Skips gravatar check if $no_grav is passed
	if ( !$no_grav ) {

		// Set gravatar size
		if ( $width )
			$grav_size = $width;
		else if ( 'full' == $type )
			$grav_size = BP_AVATAR_FULL_WIDTH;
		else if ( 'thumb' == $type )
			$grav_size = BP_AVATAR_THUMB_WIDTH;

		// Set gravatar type
		if ( empty( $bp->grav_default->{$object} ) )
			$default_grav = 'wavatar';
		else if ( 'mystery' == $bp->grav_default->{$object} )
			$default_grav = apply_filters( 'bp_core_mysteryman_src', BP_AVATAR_DEFAULT, $grav_size );
		else
			$default_grav = $bp->grav_default->{$object};

		// Set gravatar object
		if ( empty( $email ) ) {
			if ( 'user' == $object ) {
				$email = bp_core_get_user_email( $item_id );
			} else if ( 'group' == $object || 'blog' == $object ) {
				$email = "{$item_id}-{$object}@{$bp->root_domain}";
			}
		}

		// Set host based on if using ssl
		if ( is_ssl() )
			$host = 'https://secure.gravatar.com/avatar/';
		else
			$host = 'http://www.gravatar.com/avatar/';

		// Filter gravatar vars
		$email		= apply_filters( 'bp_core_gravatar_email', $email, $item_id, $object );
		$gravatar	= apply_filters( 'bp_gravatar_url', $host ) . md5( strtolower( $email ) ) . '?d=' . $default_grav . '&amp;s=' . $grav_size;

		// Return gravatar wrapped in <img />
		if ( true === $html )
			return apply_filters( 'bp_core_fetch_avatar', '<img src="' . $gravatar . '" alt="' . $alt . '" class="' . $class . '"' . $css_id . $html_width . $html_height . ' />', $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );

		// ...or only return the gravatar URL
		else
			return apply_filters( 'bp_core_fetch_avatar_url', $gravatar );

	} else {
		return apply_filters( 'bp_core_fetch_avatar', false, $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );
	}
}
Ejemplo n.º 6
0
/**
 * Catches attempts to reaccept an invitation, and redirects appropriately
 *
 * If you attempt to access the register page when logged in, you get bounced
 * to the home page. This is a BP feature. Because accept-invitation is a
 * subpage of register, this happens for accept-invitation pages as well.
 * However, people are more likely to try to visit this page than the vanilla
 * register page, because they've gotten an email inviting them to the site.
 *
 * So this function catches logged-in visits to /register/accept-invitation,
 * and if the email address in the URL matches the logged-in user's email
 * address, redirects them to their invite-anyone page to see the a message.
 *
 * @since 1.0.20
 */
function invite_anyone_already_accepted_redirect($redirect)
{
    global $bp;
    if (!invite_anyone_is_accept_invitation_page()) {
        return $redirect;
    }
    if (empty($_GET['email'])) {
        return $redirect;
    }
    $reg_email = urldecode($_GET['email']);
    if (bp_core_get_user_email(bp_loggedin_user_id()) !== $reg_email) {
        return $redirect;
    }
    $redirect = add_query_arg('already', 'accepted', trailingslashit(bp_loggedin_user_domain() . $bp->invite_anyone->slug));
    return $redirect;
}
Ejemplo n.º 7
0
 /**
  * Populate the instantiated class with data based on the User ID provided.
  *
  * @uses bp_core_get_userurl() Returns the URL with no HTML markup for
  *       a user based on their user id.
  * @uses bp_core_get_userlink() Returns a HTML formatted link for a
  *       user with the user's full name as the link text.
  * @uses bp_core_get_user_email() Returns the email address for the
  *       user based on user ID.
  * @uses bp_get_user_meta() BP function returns the value of passed
  *       usermeta name from usermeta table.
  * @uses bp_core_fetch_avatar() Returns HTML formatted avatar for a user
  * @uses bp_profile_last_updated_date() Returns the last updated date
  *       for a user.
  */
 public function populate()
 {
     if (bp_is_active('xprofile')) {
         $this->profile_data = $this->get_profile_data();
     }
     if (!empty($this->profile_data)) {
         $full_name_field_name = bp_xprofile_fullname_field_name();
         $this->user_url = bp_core_get_user_domain($this->id, $this->profile_data['user_nicename'], $this->profile_data['user_login']);
         $this->fullname = esc_attr($this->profile_data[$full_name_field_name]['field_data']);
         $this->user_link = "<a href='{$this->user_url}' title='{$this->fullname}'>{$this->fullname}</a>";
         $this->email = esc_attr($this->profile_data['user_email']);
     } else {
         $this->user_url = bp_core_get_user_domain($this->id);
         $this->user_link = bp_core_get_userlink($this->id);
         $this->fullname = esc_attr(bp_core_get_user_displayname($this->id));
         $this->email = esc_attr(bp_core_get_user_email($this->id));
     }
     // Cache a few things that are fetched often
     wp_cache_set('bp_user_fullname_' . $this->id, $this->fullname, 'bp');
     wp_cache_set('bp_user_email_' . $this->id, $this->email, 'bp');
     wp_cache_set('bp_user_url_' . $this->id, $this->user_url, 'bp');
     $this->avatar = bp_core_fetch_avatar(array('item_id' => $this->id, 'type' => 'full', 'alt' => sprintf(__('Profile photo of %s', 'buddypress'), $this->fullname)));
     $this->avatar_thumb = bp_core_fetch_avatar(array('item_id' => $this->id, 'type' => 'thumb', 'alt' => sprintf(__('Profile photo of %s', 'buddypress'), $this->fullname)));
     $this->avatar_mini = bp_core_fetch_avatar(array('item_id' => $this->id, 'type' => 'thumb', 'alt' => sprintf(__('Profile photo of %s', 'buddypress'), $this->fullname), 'width' => 30, 'height' => 30));
     $this->last_active = bp_core_get_last_activity(bp_get_user_last_activity($this->id), __('active %s', 'buddypress'));
 }
Ejemplo n.º 8
0
/**
 * bp_core_fetch_avatar()
 *
 * Fetches an avatar from a BuddyPress object. Supports user/group/blog as
 * default, but can be extended to include your own custom components too.
 *
 * @global object $bp Global BuddyPress settings object
 * @global $current_blog NXTClass global containing information and settings for the current blog being viewed.
 * @param array $args Determine the output of this function
 * @return string Formatted HTML <img> element, or raw avatar URL based on $html arg
 */
function bp_core_fetch_avatar($args = '')
{
    global $bp, $current_blog;
    // Set a few default variables
    $def_object = 'user';
    $def_type = 'thumb';
    $def_class = 'avatar';
    $def_alt = __('Avatar Image', 'buddypress');
    // Set the default variables array
    $defaults = array('item_id' => false, 'object' => $def_object, 'type' => $def_type, 'avatar_dir' => false, 'width' => false, 'height' => false, 'class' => $def_class, 'css_id' => false, 'alt' => $def_alt, 'email' => false, 'no_grav' => false, 'html' => true, 'title' => '');
    // Compare defaults to passed and extract
    $params = nxt_parse_args($args, $defaults);
    extract($params, EXTR_SKIP);
    // Set item_id if not passed
    if (!$item_id) {
        if ('user' == $object) {
            $item_id = $bp->displayed_user->id;
        } else {
            if (bp_is_active('groups') && 'group' == $object) {
                $item_id = $bp->groups->current_group->id;
            } else {
                if ('blog' == $object) {
                    $item_id = $current_blog->id;
                }
            }
        }
        $item_id = apply_filters('bp_core_avatar_item_id', $item_id, $object);
        if (!$item_id) {
            return false;
        }
    }
    // Set avatar_dir if not passed (uses $object)
    if (!$avatar_dir) {
        if ('user' == $object) {
            $avatar_dir = 'avatars';
        } else {
            if (bp_is_active('groups') && 'group' == $object) {
                $avatar_dir = 'group-avatars';
            } else {
                if ('blog' == $object) {
                    $avatar_dir = 'blog-avatars';
                }
            }
        }
        $avatar_dir = apply_filters('bp_core_avatar_dir', $avatar_dir, $object);
        if (!$avatar_dir) {
            return false;
        }
    }
    // Add an identifying class to each item
    $class .= ' ' . $object . '-' . $item_id . '-avatar';
    // Get item name for alt/title tags
    $item_name = '';
    if ('user' == $object) {
        $item_name = bp_core_get_user_displayname($item_id);
    } elseif ('group' == $object) {
        $item_name = bp_get_group_name(new BP_Groups_Group($item_id));
    } elseif ('blog' == $object) {
        $item_name = get_blog_option($item_id, 'blogname');
    }
    $alt = sprintf($alt, apply_filters('bp_core_avatar_alt', $item_name, $item_id, $object));
    // Set title tag
    if ($title) {
        $title = " title='" . esc_attr(apply_filters('bp_core_avatar_title', $title, $item_id, $object)) . "'";
    } elseif ($item_name) {
        $title = " title='" . esc_attr(apply_filters('bp_core_avatar_title', $item_name, $item_id, $object)) . "'";
    }
    // Set CSS ID if passed
    if (!empty($css_id)) {
        $css_id = " id='{$css_id}'";
    }
    // Set avatar width
    if ($width) {
        $html_width = " width='{$width}'";
    } else {
        $html_width = 'thumb' == $type ? ' width="' . bp_core_avatar_thumb_width() . '"' : ' width="' . bp_core_avatar_full_width() . '"';
    }
    // Set avatar height
    if ($height) {
        $html_height = " height='{$height}'";
    } else {
        $html_height = 'thumb' == $type ? ' height="' . bp_core_avatar_thumb_height() . '"' : ' height="' . bp_core_avatar_full_height() . '"';
    }
    // Set avatar URL and DIR based on prepopulated constants
    $avatar_folder_url = apply_filters('bp_core_avatar_folder_url', bp_core_avatar_url() . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir);
    $avatar_folder_dir = apply_filters('bp_core_avatar_folder_dir', bp_core_avatar_upload_path() . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir);
    /****
     * Look for uploaded avatar first. Use it if it exists.
     * Set the file names to search for, to select the full size
     * or thumbnail image.
     */
    $avatar_size = 'full' == $type ? '-bpfull' : '-bpthumb';
    $legacy_user_avatar_name = 'full' == $type ? '-avatar2' : '-avatar1';
    $legacy_group_avatar_name = 'full' == $type ? '-groupavatar-full' : '-groupavatar-thumb';
    // Check for directory
    if (file_exists($avatar_folder_dir)) {
        // Open directory
        if ($av_dir = opendir($avatar_folder_dir)) {
            // Stash files in an array once to check for one that matches
            $avatar_files = array();
            while (false !== ($avatar_file = readdir($av_dir))) {
                // Only add files to the array (skip directories)
                if (2 < strlen($avatar_file)) {
                    $avatar_files[] = $avatar_file;
                }
            }
            // Check for array
            if (0 < count($avatar_files)) {
                // Check for current avatar
                foreach ($avatar_files as $key => $value) {
                    if (strpos($value, $avatar_size) !== false) {
                        $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
                    }
                }
                // Legacy avatar check
                if (!isset($avatar_url)) {
                    foreach ($avatar_files as $key => $value) {
                        if (strpos($value, $legacy_user_avatar_name) !== false) {
                            $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
                        }
                    }
                    // Legacy group avatar check
                    if (!isset($avatar_url)) {
                        foreach ($avatar_files as $key => $value) {
                            if (strpos($value, $legacy_group_avatar_name) !== false) {
                                $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
                            }
                        }
                    }
                }
            }
        }
        // Close the avatar directory
        closedir($av_dir);
        // If we found a locally uploaded avatar
        if (isset($avatar_url)) {
            // Return it wrapped in an <img> element
            if (true === $html) {
                return apply_filters('bp_core_fetch_avatar', '<img src="' . $avatar_url . '" alt="' . esc_attr($alt) . '" class="' . esc_attr($class) . '"' . $css_id . $html_width . $html_height . $title . ' />', $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir);
                // ...or only the URL
            } else {
                return apply_filters('bp_core_fetch_avatar_url', $avatar_url);
            }
        }
    }
    // If no avatars could be found, try to display a gravatar
    // Skips gravatar check if $no_grav is passed
    if (!apply_filters('bp_core_fetch_avatar_no_grav', $no_grav)) {
        // Set gravatar size
        if ($width) {
            $grav_size = $width;
        } else {
            if ('full' == $type) {
                $grav_size = bp_core_avatar_full_width();
            } else {
                if ('thumb' == $type) {
                    $grav_size = bp_core_avatar_thumb_width();
                }
            }
        }
        // Set gravatar type
        if (empty($bp->grav_default->{$object})) {
            $default_grav = 'wavatar';
        } else {
            if ('mystery' == $bp->grav_default->{$object}) {
                $default_grav = apply_filters('bp_core_mysteryman_src', bp_core_avatar_default(), $grav_size);
            } else {
                $default_grav = $bp->grav_default->{$object};
            }
        }
        // Set gravatar object
        if (empty($email)) {
            if ('user' == $object) {
                $email = bp_core_get_user_email($item_id);
            } else {
                if ('group' == $object || 'blog' == $object) {
                    $email = "{$item_id}-{$object}@{bp_get_root_domain()}";
                }
            }
        }
        // Set host based on if using ssl
        if (is_ssl()) {
            $host = 'https://secure.gravatar.com/avatar/';
        } else {
            $host = 'http://www.gravatar.com/avatar/';
        }
        // Filter gravatar vars
        $email = apply_filters('bp_core_gravatar_email', $email, $item_id, $object);
        $gravatar = apply_filters('bp_gravatar_url', $host) . md5(strtolower($email)) . '?d=' . $default_grav . '&amp;s=' . $grav_size;
    } else {
        // No avatar was found, and we've been told not to use a gravatar.
        $gravatar = apply_filters("bp_core_default_avatar_{$object}", BP_PLUGIN_URL . '/bp-core/images/mystery-man.jpg', $params);
    }
    if (true === $html) {
        return apply_filters('bp_core_fetch_avatar', '<img src="' . $gravatar . '" alt="' . esc_attr($alt) . '" class="' . esc_attr($class) . '"' . $css_id . $html_width . $html_height . $title . ' />', $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir);
    } else {
        return apply_filters('bp_core_fetch_avatar_url', $gravatar);
    }
}
 function bpcs_bp_profile_email($atts)
 {
     global $wpdb, $bp;
     extract(shortcode_atts(array("user_id" => $bp->loggedin_user->userdata->ID), $atts));
     $user_id = bpcs_get_user_id($user_id);
     return bp_core_get_user_email($user_id);
 }
Ejemplo n.º 10
0
function invite_anyone_activate_user($user_id, $key, $user)
{
    global $bp;
    $email = bp_core_get_user_email($user_id);
    // Fire the query
    $invites = invite_anyone_get_invitations_by_invited_email($email);
    if ($invites->have_posts()) {
        // From the posts returned by the query, get a list of unique inviters
        $inviters = array();
        $groups = array();
        while ($invites->have_posts()) {
            $invites->the_post();
            $inviter_id = get_the_author_meta('ID');
            $inviters[] = $inviter_id;
            $groups_data = wp_get_post_terms(get_the_ID(), invite_anyone_get_invited_groups_tax_name());
            foreach ($groups_data as $group_data) {
                if (!isset($groups[$group_data->name])) {
                    // Keyed by inviter, which means they'll only get one invite per group
                    $groups[$group_data->name] = $inviter_id;
                }
            }
            // Mark as accepted
            update_post_meta(get_the_ID(), 'bp_ia_accepted', date('Y-m-d H:i:s'));
        }
        $inviters = array_unique($inviters);
        // Friendship requests
        if (bp_is_active('friends') && apply_filters('invite_anyone_send_friend_requests_on_acceptance', true)) {
            if (function_exists('friends_add_friend')) {
                foreach ($inviters as $inviter) {
                    friends_add_friend($inviter, $user_id);
                }
            }
        }
        // BuddyPress Followers support
        if (function_exists('bp_follow_start_following') && apply_filters('invite_anyone_send_follow_requests_on_acceptance', true)) {
            foreach ($inviters as $inviter) {
                bp_follow_start_following(array('leader_id' => $user_id, 'follower_id' => $inviter));
                bp_follow_start_following(array('leader_id' => $inviter, 'follower_id' => $user_id));
            }
        }
        // Group invitations
        if (bp_is_active('groups')) {
            foreach ($groups as $group_id => $inviter_id) {
                $args = array('user_id' => $user_id, 'group_id' => $group_id, 'inviter_id' => $inviter_id);
                groups_invite_user($args);
                groups_send_invites($inviter_id, $group_id);
            }
        }
    }
    do_action('accepted_email_invite', $user_id, $inviters);
}
Ejemplo n.º 11
0
/**
 * user_avatar_core_fetch_avatar()
 *
 * Description: Fetches an avatar from a BuddyPress object. Supports user/group/blog as
 * 				default, but can be extended to include your own custom components too.
 *
 * @global object $bp
 * @global object $current_blog
 * @param array $args Determine the output of this function
 * @return string Formatted HTML <img> element, or raw avatar URL based on $html arg
 */
function user_avatar_fetch_avatar($args = '')
{
    //var_dump($args);
    $defaults = array('item_id' => false, 'object' => $def_object, 'type' => $def_type, 'avatar_dir' => false, 'width' => false, 'height' => false, 'class' => $def_class, 'css_id' => false, 'alt' => $def_alt, 'email' => false, 'no_grav' => false, 'html' => true);
    // Compare defaults to passed and extract
    $params = wp_parse_args($args, $defaults);
    extract($params, EXTR_SKIP);
    $avatar_folder_dir = USER_AVATAR_UPLOAD_PATH . "{$item_id}/";
    $avatar_folder_url = USER_AVATAR_URL . "{$item_id}";
    if ($width > 50) {
        $type = "full";
    }
    $avatar_size = 'full' == $type ? '-bpfull' : '-bpthumb';
    // Add an identifying class to each item
    $class .= ' ' . $object . '-' . $item_id . '-avatar';
    // Set CSS ID if passed
    if (!empty($css_id)) {
        $css_id = " id=\"{$css_id}\"";
    }
    // Set avatar width
    if ($width) {
        $html_width = " width=\"{$width}\"";
    } else {
        $html_width = 'thumb' == $type ? ' width="' . USER_AVATAR_THUMB_WIDTH . '"' : ' width="' . USER_AVATAR_FULL_WIDTH . '"';
    }
    // Set avatar height
    if ($height) {
        $html_height = " height=\"{$height}\"";
    } else {
        $html_height = 'thumb' == $type ? ' height="' . USER_AVATAR_THUMB_HEIGHT . '"' : ' height="' . USER_AVATAR_FULL_HEIGHT . '"';
    }
    // Check for directory
    if (file_exists($avatar_folder_dir)) {
        // Open directory
        if ($av_dir = opendir($avatar_folder_dir)) {
            // Stash files in an array once to check for one that matches
            $avatar_files = array();
            while (false !== ($avatar_file = readdir($av_dir))) {
                // Only add files to the array (skip directories)
                if (2 < strlen($avatar_file)) {
                    $avatar_files[] = $avatar_file;
                }
            }
            // Check for array
            if (0 < count($avatar_files)) {
                // Check for current avatar
                if (is_array($avatar_files)) {
                    foreach ($avatar_files as $key => $value) {
                        if (strpos($value, $avatar_size) !== false) {
                            $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
                        }
                    }
                }
            }
        }
        // Close the avatar directory
        closedir($av_dir);
        // If we found a locally uploaded avatar
        if ($avatar_url) {
            // Return it wrapped in an <img> element
            if (true === $html) {
                return '<img src="' . $avatar_url . '" alt="' . $alt . '" class="' . $class . '"' . $css_id . $html_width . $html_height . ' />';
                // ...or only the URL
            } else {
                return $avatar_url;
            }
        }
    }
    // Skips gravatar check if $no_grav is passed
    if (!$no_grav) {
        // Set gravatar size
        if ($width) {
            $grav_size = $width;
        } else {
            if ('full' == $type) {
                $grav_size = USER_AVATAR_FULL_WIDTH;
            } else {
                if ('thumb' == $type) {
                    $grav_size = USER_AVATAR_THUMB_WIDTH;
                }
            }
        }
        // Set gravatar type
        if (empty($bp->grav_default->{$object})) {
            $default_grav = 'wavatar';
        } else {
            if ('mystery' == $bp->grav_default->{$object}) {
                $default_grav = apply_filters('bp_core_mysteryman_src', BP_AVATAR_DEFAULT, $grav_size);
            } else {
                $default_grav = $bp->grav_default->{$object};
            }
        }
        // Set gravatar object
        if (empty($email)) {
            if ('user' == $object) {
                $email = bp_core_get_user_email($item_id);
            } else {
                if ('group' == $object || 'blog' == $object) {
                    $email = "{$item_id}-{$object}@{$bp->root_domain}";
                }
            }
        }
        // Set host based on if using ssl
        if (is_ssl()) {
            $host = 'https://secure.gravatar.com/avatar/';
        } else {
            $host = 'http://www.gravatar.com/avatar/';
        }
        // Filter gravatar vars
        $email = apply_filters('bp_core_gravatar_email', $email, $item_id, $object);
        $gravatar = apply_filters('bp_gravatar_url', $host) . md5(strtolower($email)) . '?d=' . $default_grav . '&amp;s=' . $grav_size;
        // Return gravatar wrapped in <img />
        if (true === $html) {
            return apply_filters('bp_core_fetch_avatar', '<img src="' . $gravatar . '" alt="' . $alt . '" class="' . $class . '"' . $css_id . $html_width . $html_height . ' />', $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir);
        } else {
            return $gravatar;
        }
    } else {
        return apply_filters('bp_core_fetch_avatar', false, $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir);
    }
}
Ejemplo n.º 12
0
    function send_warning(&$cont, $flag_count)
    {
        $sitename = get_blog_option(BP_ROOT_BLOG, 'blogname');
        $siteurl = get_blog_option(BP_ROOT_BLOG, 'siteurl');
        $author_name = bp_core_get_user_displayname($cont->item_author);
        $author_email = bp_core_get_user_email($cont->item_author);
        $subject = "[{$sitename}] " . __('one of your contents has been reported as inappropriate', 'bp-moderation');
        $message = $this->options['warning_message'];
        $message = str_replace('%AUTHORNAME%', $author_name, $message);
        $message = str_replace('%CONTENTURL%', $cont->item_url, $message);
        $message = str_replace('%SITENAME%', $sitename, $message);
        $message = str_replace('%SITEURL%', $siteurl, $message);
        wp_mail($author_email, $subject, $message);
        $cont->status = 'warned';
        $cont->save();
        do_action('bp_moderation_content_warned', $cont->content_id, $cont);
        if ($this->options['warning_forward']) {
            $admin_subject = "[{$sitename}] " . sprintf(__('a warning for inappropriate content has been sent to %s', 'bp-moderation'), $author_name);
            $admin_msg_prefix = sprintf(__('Content url: %1$s
Total flags: %2$s
Author profile: %3$s

----- Message sent to the author -----
', 'bp-moderation'), $cont->item_url, $flag_count, bp_core_get_user_domain($cont->item_author));
            wp_mail($this->options['warning_forward'], $admin_subject, $admin_msg_prefix . $message);
        }
    }
/**
 * Send a screen notifications & email one when an attendee set his preferences
 *
 * @package Rendez Vous
 * @subpackage Notifications
 *
 * @since Rendez Vous (1.0.0)
 */
function rendez_vous_notify_organizer($args = array(), $attendee_id = 0, $rendez_vous = null)
{
    $bp = buddypress();
    if (empty($attendee_id) || empty($rendez_vous) || $attendee_id == $rendez_vous->organizer) {
        return;
    }
    // Screen Notification
    bp_notifications_add_notification(array('user_id' => $rendez_vous->organizer, 'item_id' => $rendez_vous->id, 'secondary_item_id' => $attendee_id, 'component_name' => $bp->rendez_vous->id, 'component_action' => 'rendez_vous_schedule'));
    // Sending Emails
    if ('no' == get_user_meta($rendez_vous->organizer, 'notification_rendez_vous_schedule', true)) {
        return;
    }
    $rendez_vous_link = rendez_vous_get_single_link($rendez_vous->id, $rendez_vous->organizer);
    $rendez_vous_title = stripslashes($rendez_vous->title);
    $rendez_vous_content = wp_kses($rendez_vous_title, array());
    $attendee_name = bp_core_get_user_displayname($attendee_id);
    $attendee_name = stripslashes($attendee_name);
    $organizer_settings = false;
    $organizer_profile = bp_core_get_user_domain($rendez_vous->organizer);
    $organizer_email = bp_core_get_user_email($rendez_vous->organizer);
    $message = sprintf(__("%s set their preferences for the rendez-vous: %s\n\nTo view details, log in and visit: %s\n\n---------------------\n", 'rendez-vous'), $attendee_name, $rendez_vous_content, esc_url($rendez_vous_link));
    $subject = bp_get_email_subject(array('text' => sprintf(__('%s selected date(s) for a rendez-vous', 'rendez-vous'), $attendee_name)));
    // Set up and send the message
    $to = $organizer_email;
    // Only show the disable notifications line if the settings component is enabled
    if (bp_is_active('settings')) {
        $organizer_settings = trailingslashit($organizer_profile . bp_get_settings_slug() . '/notifications');
        $message .= sprintf(__('To disable these notifications please log in and go to: %s', 'rendez-vous'), esc_url($organizer_settings));
    }
    /* Send the message */
    $to = apply_filters('rendez_vous_userset_notify_organizer', $to);
    $subject = apply_filters('rendez_vous_userset_notify_subject', $subject, $attendee_name);
    $mail_content = apply_filters('rendez_vous_userset_notify_message', $message, $rendez_vous, $attendee_name, $rendez_vous_content, $rendez_vous_link, $organizer_settings);
    wp_mail($to, $subject, $mail_content);
}
Ejemplo n.º 14
0
function invite_anyone_activate_user($user_id, $key, $user)
{
    global $bp;
    $email = bp_core_get_user_email($user_id);
    if ($invites = invite_anyone_get_invitations_by_invited_email($email)) {
        // Mark as "is_joined"
        invite_anyone_mark_as_joined($email);
        // Friendship requests
        if (bp_is_active('friends')) {
            $inviters = array();
            foreach ($invites as $invite) {
                if (!in_array($invite->inviter_id, $inviters)) {
                    $inviters[] = $invite->inviter_id;
                }
            }
            if (function_exists('friends_add_friend')) {
                foreach ($inviters as $inviter) {
                    friends_add_friend($inviter, $user_id);
                }
            }
        }
        // BuddyPress Followers support
        if (function_exists('bp_follow_start_following')) {
            $inviters = array();
            foreach ($invites as $invite) {
                if (!in_array($invite->inviter_id, $inviters)) {
                    $inviters[] = $invite->inviter_id;
                }
            }
            foreach ($inviters as $inviter) {
                bp_follow_start_following(array('leader_id' => $user_id, 'follower_id' => $inviter));
                bp_follow_start_following(array('leader_id' => $inviter, 'follower_id' => $user_id));
            }
        }
        // BuddyPress Followers support
        if (function_exists('bp_follow_start_following')) {
            $inviters = array();
            foreach ($invites as $invite) {
                if (!in_array($invite->inviter_id, $inviters)) {
                    $inviters[] = $invite->inviter_id;
                }
            }
            foreach ($inviters as $inviter) {
                bp_follow_start_following(array('leader_id' => $user_id, 'follower_id' => $inviter));
                bp_follow_start_following(array('leader_id' => $inviter, 'follower_id' => $user_id));
            }
        }
        // Group invitations
        if (bp_is_active('groups')) {
            $groups = array();
            foreach ($invites as $invite) {
                if (!$invite->group_invitations[0]) {
                    continue;
                } else {
                    $group_invitations = unserialize($invite->group_invitations);
                }
                foreach ($group_invitations as $group) {
                    if (!in_array($group, array_keys($groups))) {
                        $groups[$group] = $invite->inviter_id;
                    }
                }
            }
            foreach ($groups as $group_id => $inviter_id) {
                $args = array('user_id' => $user_id, 'group_id' => $group_id, 'inviter_id' => $inviter_id);
                groups_invite_user($args);
                groups_send_invites($inviter_id, $group_id);
            }
        }
    }
    do_action('accepted_email_invite', $user_id, $inviters);
}
Ejemplo n.º 15
0
/**
 * Get an avatar for a BuddyPress object.
 *
 * Supports avatars for users, groups, and blogs by default, but can be
 * extended to support custom components as well.
 *
 * This function gives precedence to locally-uploaded avatars. When a local
 * avatar is not found, Gravatar is queried. To disable Gravatar fallbacks
 * locally:
 *    add_filter( 'bp_core_fetch_avatar_no_grav', '__return_true' );
 *
 * @param array $args {
 *     An array of arguments. All arguments are technically optional; some
 *     will, if not provided, be auto-detected by bp_core_fetch_avatar(). This
 *     auto-detection is described more below, when discussing specific
 *     arguments.
 *
 *     @type int|bool $item_id The numeric ID of the item for which you're
 *           requesting an avatar (eg, a user ID). If no 'item_id' is present,
 *           the function attempts to infer an ID from the 'object' + the
 *           current context: if 'object' is 'user' and the current page is a
 *           user page, 'item_id' will default to the displayed user ID; if
 *           'group' and on a group page, to the current group ID; if 'blog',
 *           to the current blog's ID. If no 'item_id' can be determined in
 *           this way, the function returns false. Default: false.
 *     @type string $object The kind of object for which you're getting an
 *           avatar. BuddyPress natively supports three options: 'user',
 *           'group', 'blog'; a plugin may register more.  Default: 'user'.
 *     @type string $type When a new avatar is uploaded to BP, 'thumb' and
 *           'full' versions are saved. This parameter specifies whether you'd
 *           like the 'full' or smaller 'thumb' avatar. Default: 'thumb'.
 *     @type string|bool $avatar_dir The name of the subdirectory where the
 *           requested avatar should be found. If no value is passed,
 *           'avatar_dir' is inferred from 'object': 'user' becomes 'avatars',
 *           'group' becomes 'group-avatars', 'blog' becomes 'blog-avatars'.
 *           Remember that this string denotes a subdirectory of BP's main
 *           avatar directory (usually based on {@link wp_upload_dir()}); it's a
 *           string like 'group-avatars' rather than the full directory path.
 *           Generally, it'll only be necessary to override the default value if
 *           storing avatars in a non-default location. Defaults to false
 *           (auto-detected).
 *     @type int|bool $width Requested avatar width. The unit is px. This value
 *           is used to build the 'width' attribute for the <img> element. If
 *           no value is passed, BP uses the global avatar width for this
 *           avatar type. Default: false (auto-detected).
 *     @type int|bool $height Requested avatar height. The unit is px. This
 *           value is used to build the 'height' attribute for the <img>
 *           element. If no value is passed, BP uses the global avatar height
 *           for this avatar type. Default: false (auto-detected).
 *     @type string $class The CSS class for the <img> element. Note that BP
 *           uses the 'avatar' class fairly extensively in its default styling,
 *           so if you plan to pass a custom value, consider appending it to
 *           'avatar' (eg 'avatar foo') rather than replacing it altogether.
 *           Default: 'avatar'.
 *     @type string|bool $css_id The CSS id for the <img> element.
 *           Default: false.
 *     @type string $title The title attribute for the <img> element.
 *           Default: false.
 *     @type string $alt The alt attribute for the <img> element. In BP, this
 *           value is generally passed by the wrapper functions, where the data
 *           necessary for concatenating the string is at hand; see
 *           {@link bp_get_activity_avatar()} for an example. Default: ''.
 *     @type string|bool $email An email to use in Gravatar queries. Unless
 *           otherwise configured, BP uses Gravatar as a fallback for avatars
 *           that are not provided locally. Gravatar's API requires using a hash
 *           of the user's email address; this argument provides it. If not
 *           provided, the function will infer it: for users, by getting the
 *           user's email from the database, for groups/blogs, by concatenating
 *           "{$item_id}-{$object}@{bp_get_root_domain()}". The user query adds
 *           overhead, so it's recommended that wrapper functions provide a
 *           value for 'email' when querying user IDs. Default: false.
 *     @type bool $no_grav Whether to disable the default Gravatar fallback.
 *           By default, BP will fall back on Gravatar when it cannot find a
 *           local avatar. In some cases, this may be undesirable, in which
 *           case 'no_grav' should be set to true. To disable Gravatar
 *           fallbacks globally, see the 'bp_core_fetch_avatar_no_grav' filter.
 *           Default: false.
 *     @type bool $html Whether to return an <img> HTML element, vs a raw URL
 *           to an avatar. If false, <img>-specific arguments (like 'css_id')
 *           will be ignored. Default: true.
 * }
 * @return string Formatted HTML <img> element, or raw avatar URL based on $html arg.
 */
function bp_core_fetch_avatar($args = '')
{
    $bp = buddypress();
    // If avatars are disabled for the root site, obey that request and bail
    if (!$bp->avatar->show_avatars) {
        return;
    }
    global $current_blog;
    // Set the default variables array and parse it against incoming $args array.
    $params = wp_parse_args($args, array('item_id' => false, 'object' => 'user', 'type' => 'thumb', 'avatar_dir' => false, 'width' => false, 'height' => false, 'class' => 'avatar', 'css_id' => false, 'alt' => '', 'email' => false, 'no_grav' => false, 'html' => true, 'title' => ''));
    /** Set item_id ***********************************************************/
    if (empty($params['item_id'])) {
        switch ($params['object']) {
            case 'blog':
                $params['item_id'] = $current_blog->id;
                break;
            case 'group':
                if (bp_is_active('groups')) {
                    $params['item_id'] = $bp->groups->current_group->id;
                } else {
                    $params['item_id'] = false;
                }
                break;
            case 'user':
            default:
                $params['item_id'] = bp_displayed_user_id();
                break;
        }
        $params['item_id'] = apply_filters('bp_core_avatar_item_id', $params['item_id'], $params['object'], $params);
        if (empty($params['item_id'])) {
            return false;
        }
    }
    /** Set avatar_dir ********************************************************/
    if (empty($params['avatar_dir'])) {
        switch ($params['object']) {
            case 'blog':
                $params['avatar_dir'] = 'blog-avatars';
                break;
            case 'group':
                if (bp_is_active('groups')) {
                    $params['avatar_dir'] = 'group-avatars';
                } else {
                    $params['avatar_dir'] = false;
                }
                break;
            case 'user':
            default:
                $params['avatar_dir'] = 'avatars';
                break;
        }
        $params['avatar_dir'] = apply_filters('bp_core_avatar_dir', $params['avatar_dir'], $params['object'], $params);
        if (empty($params['avatar_dir'])) {
            return false;
        }
    }
    /** <img> alt *************************************************************/
    if (false !== strpos($params['alt'], '%s') || false !== strpos($params['alt'], '%1$s')) {
        switch ($params['object']) {
            case 'blog':
                $item_name = get_blog_option($params['item_id'], 'blogname');
                break;
            case 'group':
                $item_name = bp_get_group_name(groups_get_group(array('group_id' => $params['item_id'])));
                break;
            case 'user':
            default:
                $item_name = bp_core_get_user_displayname($params['item_id']);
                break;
        }
        $item_name = apply_filters('bp_core_avatar_alt', $item_name, $params['item_id'], $params['object'], $params);
        $params['alt'] = sprintf($params['alt'], $item_name);
    }
    /** Sanity Checks *********************************************************/
    // Get a fallback for the 'alt' parameter, create html output
    if (empty($params['alt'])) {
        $params['alt'] = __('Profile Photo', 'buddypress');
    }
    $html_alt = ' alt="' . esc_attr($params['alt']) . '"';
    // Filter image title and create html string
    $html_title = '';
    $params['title'] = apply_filters('bp_core_avatar_title', $params['title'], $params['item_id'], $params['object'], $params);
    if (!empty($params['title'])) {
        $html_title = ' title="' . esc_attr($params['title']) . '"';
    }
    // Set CSS ID and create html string
    $html_css_id = '';
    $params['css_id'] = apply_filters('bp_core_css_id', $params['css_id'], $params['item_id'], $params['object'], $params);
    if (!empty($params['css_id'])) {
        $html_css_id = ' id="' . esc_attr($params['css_id']) . '"';
    }
    // Set image width
    if (false !== $params['width']) {
        // Width has been specified. No modification necessary.
    } elseif ('thumb' == $params['type']) {
        $params['width'] = bp_core_avatar_thumb_width();
    } else {
        $params['width'] = bp_core_avatar_full_width();
    }
    $html_width = ' width="' . $params['width'] . '"';
    // Set image height
    if (false !== $params['height']) {
        // Height has been specified. No modification necessary.
    } elseif ('thumb' == $params['type']) {
        $params['height'] = bp_core_avatar_thumb_height();
    } else {
        $params['height'] = bp_core_avatar_full_height();
    }
    $html_height = ' height="' . $params['height'] . '"';
    // Create CSS class html string
    $params['class'] = apply_filters('bp_core_avatar_class', $params['class'], $params['item_id'], $params['object'], $params);
    // Use an alias to leave the param unchanged
    $avatar_classes = $params['class'];
    if (!is_array($avatar_classes)) {
        $avatar_classes = explode(' ', $avatar_classes);
    }
    // merge classes
    $avatar_classes = array_merge($avatar_classes, array($params['object'] . '-' . $params['item_id'] . '-avatar', 'avatar-' . $params['width']));
    // Sanitize each class
    $avatar_classes = array_map('sanitize_html_class', $avatar_classes);
    // populate the class attribute
    $html_class = ' class="' . join(' ', $avatar_classes) . ' photo"';
    // Set img URL and DIR based on prepopulated constants
    $avatar_loc = new stdClass();
    $avatar_loc->path = trailingslashit(bp_core_avatar_upload_path());
    $avatar_loc->url = trailingslashit(bp_core_avatar_url());
    $avatar_loc->dir = trailingslashit($params['avatar_dir']);
    $avatar_folder_url = apply_filters('bp_core_avatar_folder_url', $avatar_loc->url . $avatar_loc->dir . $params['item_id'], $params['item_id'], $params['object'], $params['avatar_dir']);
    $avatar_folder_dir = apply_filters('bp_core_avatar_folder_dir', $avatar_loc->path . $avatar_loc->dir . $params['item_id'], $params['item_id'], $params['object'], $params['avatar_dir']);
    /**
     * Look for uploaded avatar first. Use it if it exists.
     * Set the file names to search for, to select the full size
     * or thumbnail image.
     */
    $avatar_size = 'full' == $params['type'] ? '-bpfull' : '-bpthumb';
    $legacy_user_avatar_name = 'full' == $params['type'] ? '-avatar2' : '-avatar1';
    $legacy_group_avatar_name = 'full' == $params['type'] ? '-groupavatar-full' : '-groupavatar-thumb';
    // Check for directory
    if (file_exists($avatar_folder_dir)) {
        // Open directory
        if ($av_dir = opendir($avatar_folder_dir)) {
            // Stash files in an array once to check for one that matches
            $avatar_files = array();
            while (false !== ($avatar_file = readdir($av_dir))) {
                // Only add files to the array (skip directories)
                if (2 < strlen($avatar_file)) {
                    $avatar_files[] = $avatar_file;
                }
            }
            // Check for array
            if (0 < count($avatar_files)) {
                // Check for current avatar
                foreach ($avatar_files as $key => $value) {
                    if (strpos($value, $avatar_size) !== false) {
                        $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
                    }
                }
                // Legacy avatar check
                if (!isset($avatar_url)) {
                    foreach ($avatar_files as $key => $value) {
                        if (strpos($value, $legacy_user_avatar_name) !== false) {
                            $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
                        }
                    }
                    // Legacy group avatar check
                    if (!isset($avatar_url)) {
                        foreach ($avatar_files as $key => $value) {
                            if (strpos($value, $legacy_group_avatar_name) !== false) {
                                $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
                            }
                        }
                    }
                }
            }
        }
        // Close the avatar directory
        closedir($av_dir);
        // If we found a locally uploaded avatar
        if (isset($avatar_url)) {
            // Return it wrapped in an <img> element
            if (true === $params['html']) {
                return apply_filters('bp_core_fetch_avatar', '<img src="' . $avatar_url . '"' . $html_class . $html_css_id . $html_width . $html_height . $html_alt . $html_title . ' />', $params, $params['item_id'], $params['avatar_dir'], $html_css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir);
                // ...or only the URL
            } else {
                return apply_filters('bp_core_fetch_avatar_url', $avatar_url, $params);
            }
        }
    }
    // If no avatars could be found, try to display a gravatar
    // Skips gravatar check if $params['no_grav'] is passed
    if (!apply_filters('bp_core_fetch_avatar_no_grav', $params['no_grav'], $params)) {
        // Set gravatar type
        if (empty($bp->grav_default->{$params['object']})) {
            $default_grav = 'wavatar';
        } elseif ('mystery' == $bp->grav_default->{$params['object']}) {
            $default_grav = apply_filters('bp_core_mysteryman_src', 'mm', $params['width']);
        } else {
            $default_grav = $bp->grav_default->{$params['object']};
        }
        // Set gravatar object
        if (empty($params['email'])) {
            if ('user' == $params['object']) {
                $params['email'] = bp_core_get_user_email($params['item_id']);
            } elseif ('group' == $params['object'] || 'blog' == $params['object']) {
                $params['email'] = $params['item_id'] . '-' . $params['object'] . '@' . bp_get_root_domain();
            }
        }
        // Set host based on if using ssl
        $host = 'http://gravatar.com/avatar/';
        if (is_ssl()) {
            $host = 'https://secure.gravatar.com/avatar/';
        }
        // Filter gravatar vars
        $params['email'] = apply_filters('bp_core_gravatar_email', $params['email'], $params['item_id'], $params['object']);
        $gravatar = apply_filters('bp_gravatar_url', $host) . md5(strtolower($params['email'])) . '?d=' . $default_grav . '&amp;s=' . $params['width'];
        // Gravatar rating; http://bit.ly/89QxZA
        $rating = get_option('avatar_rating');
        if (!empty($rating)) {
            $gravatar .= "&amp;r={$rating}";
        }
        // No avatar was found, and we've been told not to use a gravatar.
    } else {
        $gravatar = apply_filters('bp_core_default_avatar_' . $params['object'], bp_core_avatar_default('local'), $params);
    }
    if (true === $params['html']) {
        return apply_filters('bp_core_fetch_avatar', '<img src="' . $gravatar . '"' . $html_css_id . $html_class . $html_width . $html_height . $html_alt . $html_title . ' />', $params, $params['item_id'], $params['avatar_dir'], $html_css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir);
    } else {
        return apply_filters('bp_core_fetch_avatar_url', $gravatar, $params);
    }
}
Ejemplo n.º 16
0
/**
 * Adds a new comment to the database.
 * A copy of wp_new_comment()
 * @see wp_new_comment()
 */
function mpp_add_comment($commentdata)
{
    //basic fields
    //set to default
    if (!isset($commentdata['comment_author'])) {
        $commentdata['comment_author'] = bp_loggedin_user_fullname();
    }
    if (!isset($commentdata['comment_author_email'])) {
        $commentdata['comment_author_email'] = bp_core_get_user_email(get_current_user_id());
    }
    if (!isset($commentdata['comment_author_url'])) {
        $commentdata['comment_author_url'] = bp_loggedin_user_domain();
    }
    /**
     * Filter a comment's data before it is sanitized and inserted into the database.
     *
     * @since 1.5.0
     *
     * @param array $commentdata Comment data.
     */
    $commentdata = apply_filters('preprocess_comment', $commentdata);
    $commentdata['comment_post_ID'] = (int) $commentdata['post_id'];
    //media Id or Gallery ID
    if (isset($commentdata['user_ID'])) {
        $commentdata['user_id'] = $commentdata['user_ID'] = (int) $commentdata['user_ID'];
    } elseif (isset($commentdata['user_id'])) {
        $commentdata['user_id'] = (int) $commentdata['user_id'];
    }
    $commentdata['comment_parent'] = isset($commentdata['comment_parent']) ? absint($commentdata['comment_parent']) : 0;
    //$parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status($commentdata['comment_parent']) : '';
    $commentdata['comment_parent'] = $commentdata['comment_parent'] > 0 ? $commentdata['comment_parent'] : 0;
    $commentdata['comment_author_IP'] = preg_replace('/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR']);
    $commentdata['comment_agent'] = isset($_SERVER['HTTP_USER_AGENT']) ? substr($_SERVER['HTTP_USER_AGENT'], 0, 254) : '';
    $commentdata['comment_date'] = current_time('mysql');
    $commentdata['comment_date_gmt'] = current_time('mysql', 1);
    $commentdata = wp_filter_comment($commentdata);
    $commentdata['comment_approved'] = 1;
    //make approved by default wp_allow_comment($commentdata);
    $comment_ID = wp_insert_comment($commentdata);
    do_action('mpp_comment_added', $comment_ID, $commentdata['comment_approved']);
    return $comment_ID;
}