Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/30/2020 10:13:38 PM (3 years ago)
Author:
imath
Message:

Blogs: use a default "blavatar" when the blog has no site icon

So far when the Blogs component was not supporting the WordPress site icon feature or when the blog had no site icon set, we used to fallback on the Blog Administrator's avatar.

We realized this was a wrong approach, because:

  • the site creator may no longer be a member of the specific site, in which case the lowest id will belong to a different (non-admin) user.
  • The site admin may have changed in some other way.
  • The site creator's account may have been deleted.
  • Etc.

When any of these happen, you get the avatar of a random site user.

That's why just like we do for users or groups, we are now using a default blog avatar (mysterious-blog.png) as a fallback when the site icon for the blog is not available.

Props boonebgorges, vapvarun, johnjamesjacoby

Fixes #8179

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-blogs/bp-blogs-template.php

    r12750 r12772  
    299299     * Get a blog's avatar.
    300300     *
    301      * At the moment, blog avatars are simply the user avatars of the blog
    302      * admin. Filter 'bp_get_blog_avatar_' . $blog_id to customize.
     301     * At the moment, unless the blog has a site icon, the blog's avatar defaults
     302     * to the /bp-core/images/mystery-blog.png image or the Blog's Admin user avatar
     303     * if the `admin_user_id` argument contains the Blog's Admin user ID.
    303304     *
    304305     * @since 2.4.0 Introduced `$title` argument.
    305306     * @since 6.0.0 Introduced the `$blog_id`, `$admin_user_id` and `html` arguments.
     307     * @since 7.0.0 Introduced the Blog's default avatar {@see bp_blogs_default_avatar()}.
     308     *              Removed the `'bp_get_blog_avatar_' . $blog_id` filter (it was deprecated since 1.5).
    306309     *
    307310     * @see bp_core_fetch_avatar() For a description of arguments and
     
    334337        }
    335338
    336         // Set default values.
    337         $author_displayname = '';
    338         $admin_user_id      = 0;
    339         $blog_id            = 0;
    340 
    341         if ( ! $blogs_template && isset( $args['admin_user_id'] ) && $args['admin_user_id'] ) {
    342             $admin_user_id      = (int) $args['admin_user_id'];
    343             $author_displayname = bp_core_get_user_displayname( $admin_user_id );
    344         } else {
    345             $admin_user_id      = $blogs_template->blog->admin_user_id;
    346             $author_displayname = bp_core_get_user_displayname( $blogs_template->blog->admin_user_id );
    347         }
     339        // Set default value for the `alt` attribute.
     340        $alt_attribute = __( 'Site icon for the blog', 'buddypress' );
    348341
    349342        if ( ! $blogs_template && isset( $args['blog_id'] ) && $args['blog_id'] ) {
    350343            $blog_id = (int) $args['blog_id'];
    351344        } else {
    352             $blog_id = bp_get_blog_id();
     345            $blog_id       = bp_get_blog_id();
     346            $alt_attribute = sprintf( __( 'Site icon for %s', 'buddypress' ), bp_get_blog_name() );
    353347        }
    354348
    355349        // Parse the arguments.
    356350        $r = bp_parse_args( $args, array(
    357             'type'    => 'full',
    358             'width'   => false,
    359             'height'  => false,
    360             'class'   => 'avatar',
    361             'id'      => false,
    362             'alt'     => sprintf(
    363                 /* translators: %s: the author display name */
    364                 __( 'Profile picture of site author %s', 'buddypress' ),
    365                 esc_attr( $author_displayname )
    366             ),
    367             'no_grav' => false,
    368             'html'    => true,
    369         ) );
     351            'item_id'    => $blog_id,
     352            'avatar_dir' => 'blog-avatars',
     353            'object'     => 'blog',
     354            'type'       => 'full',
     355            'width'      => false,
     356            'height'     => false,
     357            'class'      => 'avatar',
     358            'id'         => false,
     359            'alt'        => $alt_attribute,
     360            'no_grav'    => false,
     361            'html'       => true,
     362        ), 'blog_avatar' );
     363
     364        /**
     365         * If the `admin_user_id` was provided, make the Blog avatar
     366         * defaults to the Blog's Admin user one.
     367         */
     368        if ( isset( $r['admin_user_id'] ) && $r['admin_user_id'] ) {
     369            $r['item_id']    = (int) $r['admin_user_id'];
     370            $r['avatar_dir'] = 'avatars';
     371            $r['object']     = 'user';
     372        } elseif ( ! $r['no_grav'] ) {
     373            $r['no_grav'] = true;
     374        }
    370375
    371376        // Use site icon if available.
     
    421426                }
    422427
    423                 $alt_attribute = __( 'Site icon for the blog', 'buddypress' );
    424                 if ( $blogs_template ) {
    425                     /* translators: %s is the placeholder for the name of the blog */
    426                     $alt_attribute = sprintf( __( 'Site icon for %s', 'buddypress' ), bp_get_blog_name() );
    427                 }
    428 
    429428                $avatar = sprintf( '<img src="%1$s" class="%2$s" width="%3$s" height="%3$s" alt="%4$s" />',
    430429                    esc_url( $site_icon ),
     
    436435        }
    437436
    438         // Fallback to user ID avatar.
     437        // Fallback to Default blog avatar.
    439438        if ( '' === $avatar ) {
    440             $avatar = bp_core_fetch_avatar( array(
    441                 'item_id' => $admin_user_id,
    442                 // 'avatar_dir' => 'blog-avatars',
    443                 // 'object'     => 'blog',
    444                 'type'    => $r['type'],
    445                 'alt'     => $r['alt'],
    446                 'css_id'  => $r['id'],
    447                 'class'   => $r['class'],
    448                 'width'   => $r['width'],
    449                 'height'  => $r['height'],
    450                 'no_grav' => $r['no_grav'],
    451                 'html'    => $r['html'],
    452             ) );
    453         }
    454 
    455         /**
    456          * In future BuddyPress versions you will be able to set the avatar for a blog.
    457          * Right now you can use a filter with the ID of the blog to change it if you wish.
    458          * By default it will return the avatar for the primary blog admin.
    459          *
    460          * This filter is deprecated as of BuddyPress 1.5 and may be removed in a future version.
    461          * Use the 'bp_get_blog_avatar' filter instead.
    462          */
    463         $avatar = apply_filters( 'bp_get_blog_avatar_' . $blog_id, $avatar );
     439            $avatar = bp_core_fetch_avatar( $r );
     440        }
    464441
    465442        /**
Note: See TracChangeset for help on using the changeset viewer.