Index: src/bp-core/bp-core-avatars.php
===================================================================
--- src/bp-core/bp-core-avatars.php
+++ src/bp-core/bp-core-avatars.php
@@ -103,6 +103,9 @@
* 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
@@ -172,6 +175,13 @@
* @type bool $html Whether to return an HTML element, vs a raw URL
* to an avatar. If false, -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 element, or raw avatar URL based on $html arg.
@@ -188,19 +198,23 @@
// 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' => '',
+ '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 ***********************************************************/
@@ -346,6 +360,9 @@
$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 = '';
@@ -505,6 +522,8 @@
// 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 element
if ( true === $params['html'] ) {
@@ -524,7 +543,7 @@
* @param string $avatar_folder_url Avatar URL path.
* @param string $avatar_folder_dir Avatar dir path.
*/
- return apply_filters( 'bp_core_fetch_avatar', '', $params, $params['item_id'], $params['avatar_dir'], $html_css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );
+ return apply_filters( 'bp_core_fetch_avatar', '', $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 {
@@ -579,8 +598,6 @@
}
}
- $host = '//www.gravatar.com/avatar/';
-
/**
* Filters the Gravatar email to use.
*
@@ -593,20 +610,37 @@
$params['email'] = apply_filters( 'bp_core_gravatar_email', $params['email'], $params['item_id'], $params['object'] );
/**
- * Filters the Gravatar URL path.
+ * Filters the Gravatar URL host.
*
* @since 1.0.2
*
- * @param string $value Gravatar URL path.
+ * @param string $value Gravatar URL host.
*/
- $gravatar = apply_filters( 'bp_gravatar_url', $host ) . md5( strtolower( $params['email'] ) ) . '?d=' . $default_grav . '&s=' . $params['width'];
+ $gravatar = apply_filters( 'bp_gravatar_url', '//www.gravatar.com/avatar/' );
+
+ // Append email hash to Gravatar
+ $gravatar .= md5( strtolower( $params['email'] ) );
- // Gravatar rating; http://bit.ly/89QxZA
- $rating = get_option( 'avatar_rating' );
- if ( ! empty( $rating ) ) {
- $gravatar .= "&r={$rating}";
+ // Main Gravatar URL args
+ $url_args = array(
+ 'd' => $default_grav,
+ 's' => $params['width']
+ );
+
+ // Custom Gravatar URL args
+ if ( ! empty( $args['force_default'] ) ) {
+ $url_args['f'] = 'y';
+ }
+ if ( ! empty( $args['rating'] ) ) {
+ $url_args['r'] = strtolower( $args['rating'] );
}
+ // 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 {
@@ -626,7 +660,7 @@
if ( true === $params['html'] ) {
/** This filter is documented in bp-core/bp-core-avatars.php */
- return apply_filters( 'bp_core_fetch_avatar', '', $params, $params['item_id'], $params['avatar_dir'], $html_css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );
+ return apply_filters( 'bp_core_fetch_avatar', '', $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 */
@@ -1281,17 +1315,20 @@
/**
* Replace default WordPress avatars with BP avatars, if available.
*
- * Filters 'get_avatar'.
+ * See 'get_avatar' filter description in wp-includes/pluggable.php.
+ *
+ * @since 2.4 Added $args parameter to coincide with WordPress 4.2.
*
* @param string $avatar The avatar path passed to 'get_avatar'.
* @param int|string|object $user A user ID, email address, or comment object.
* @param int $size Size of the avatar image ('thumb' or 'full').
* @param string $default URL to a default image to use if no avatar is available.
* @param string $alt Alternate text to use in image tag. Default: ''.
+ * @param array $args Arguments passed to get_avatar_data(), after processing.
*
* @return string BP avatar path, if found; else the original avatar path.
*/
-function bp_core_fetch_avatar_filter( $avatar, $user, $size, $default, $alt = '' ) {
+function bp_core_fetch_avatar_filter( $avatar, $user, $size, $default, $alt = '', $args = array() ) {
global $pagenow;
// Do not filter if inside WordPress options page
@@ -1332,19 +1369,47 @@
$type = 'full';
}
- // Let BuddyPress handle the fetching of the avatar
- $bp_avatar = bp_core_fetch_avatar( array(
+ $avatar_args = array(
'item_id' => $id,
'type' => $type,
'width' => $size,
'height' => $size,
'alt' => $alt,
- ) );
+ );
+
+ // Support new arguments as of WordPress 4.2.0
+ if ( ! empty( $args['width'] ) ) {
+ $avatar_args['width'] = $args['width'];
+ }
+ if ( ! empty( $args['height'] ) ) {
+ $avatar_args['height'] = $args['height'];
+ }
+ if ( ! empty( $args['class'] ) ) {
+ $avatar_args['class'] = $args['class'];
+ }
+ if ( ! empty( $args['class'] ) ) {
+ $avatar_args['class'] = $args['class'];
+ }
+ if ( ! empty( $args['extra_attr'] ) ) {
+ $avatar_args['extra_attr'] = $args['extra_attr'];
+ }
+ if ( ! empty( $args['scheme'] ) ) {
+ $avatar_args['scheme'] = $args['scheme'];
+ }
+ if ( ! empty( $args['force_default'] ) ) {
+ $avatar_args['force_default'] = $args['force_default'];
+ }
+ if ( ! empty( $args['rating'] ) ) {
+ $avatar_args['rating'] = $args['rating'];
+ }
+
+ // Let BuddyPress handle the fetching of the avatar
+ $bp_avatar = bp_core_fetch_avatar( $avatar_args );
// If BuddyPress found an avatar, use it. If not, use the result of get_avatar
return ( !$bp_avatar ) ? $avatar : $bp_avatar;
}
-add_filter( 'get_avatar', 'bp_core_fetch_avatar_filter', 10, 5 );
+add_filter( 'get_avatar', 'bp_core_fetch_avatar_filter', 10, 6 );
/**
* Is the current avatar upload error-free?