Skip to:
Content

BuddyPress.org

Changeset 12772


Ignore:
Timestamp:
10/30/2020 10:13:38 PM (4 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

Location:
trunk
Files:
2 added
3 edited

Legend:

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

    r12173 r12772  
    138138
    139139/**
     140 * Use the mystery blog avatar for blogs.
     141 *
     142 * @since 7.0.0
     143 *
     144 * @param string $avatar Current avatar src.
     145 * @param array  $params Avatar params.
     146 * @return string
     147 */
     148function bp_blogs_default_avatar( $avatar, $params ) {
     149    if ( isset( $params['object'] ) && 'blog' === $params['object'] ) {
     150        if ( isset( $params['type'] ) && 'thumb' === $params['type'] ) {
     151            $file = 'mystery-blog-50.png';
     152        } else {
     153            $file = 'mystery-blog.png';
     154        }
     155
     156        $avatar = buddypress()->plugin_url . "bp-core/images/$file";
     157    }
     158
     159    return $avatar;
     160}
     161add_filter( 'bp_core_avatar_default',       'bp_blogs_default_avatar', 10, 2 );
     162add_filter( 'bp_core_avatar_default_thumb', 'bp_blogs_default_avatar', 10, 2 );
     163
     164/**
    140165 * Filters the column name during blog metadata queries.
    141166 *
  • 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        /**
  • trunk/tests/phpunit/testcases/blogs/template.php

    r12606 r12772  
    398398
    399399        $blogs_template = $reset_blogs_template;
    400 
    401         $this->assertTrue( $avatar === bp_core_fetch_avatar( array(
    402             'type'          => 'full',
    403             'item_id'       => $u,
    404             'alt'           => 'test',
    405             'no_grav'       => true,
    406             'class'         => 'avatar',
    407         ) ) );
     400        $expected       = bp_core_fetch_avatar(
     401            array(
     402                'type'    => 'full',
     403                'item_id' => $u,
     404                'alt'     => 'test',
     405                'no_grav' => true,
     406                'class'   => 'avatar',
     407            )
     408        );
     409
     410        $this->assertTrue( $avatar === $expected );
    408411    }
    409412
     
    445448    }
    446449
     450    /**
     451     * @group avatar
     452     * @group BP_Blogs_Template
     453     * @group bp_get_blog_avatar
     454     */
     455    public function test_bp_get_blog_default_avatar() {
     456        if ( ! is_multisite() ) {
     457            $this->markTestSkipped();
     458        }
     459
     460        global $blogs_template;
     461        $reset_blogs_template = $blogs_template;
     462        $blogs_template = null;
     463
     464        $u = self::factory()->user->create();
     465        $b = self::factory()->blog->create( array(
     466            'title' => 'The Foo Bar Blog',
     467            'user_id' => $u,
     468        ) );
     469
     470        $avatar = bp_get_blog_avatar(
     471            array(
     472                'type'    => 'thumb',
     473                'blog_id' => $b,
     474                'html'    => false,
     475            )
     476        );
     477
     478        $blogs_template = $reset_blogs_template;
     479        $expected       = buddypress()->plugin_url . "bp-core/images/mystery-blog-50.png";
     480
     481        $this->assertTrue( $avatar === $expected );
     482    }
     483
    447484    public function filter_blog_avatar() {
    448485        return BP_TESTS_DIR . 'assets/upside-down.jpg';
Note: See TracChangeset for help on using the changeset viewer.