Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/22/2016 12:35:27 AM (10 years ago)
Author:
r-a-y
Message:

Blogs: Use WordPress' Site Icons as blog avatars.

If the current install is using WordPress 4.3+, we'll now use WordPress'
Site Icon as the site avatar in the Sites Directory:
​https://codex.wordpress.org/Creating_a_Favicon#WordPress_Version_4.3_or_later

If no site icon is available, we'll fallback to the site owner's user
avatar as before. Under the hood, we are mirroring the site icon URLs to
blogmeta whenever a site icon is updated or deleted.

Devs can disable this functionality with the following filter:
add_filter( 'bp_is_blogs_site-icon_active', '__return_false' );

Fixes #6544.

File:
1 edited

Legend:

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

    r10906 r11150  
    346346                ) );
    347347
    348                 // Fetch the avatar.
    349                 $avatar = bp_core_fetch_avatar( array(
    350                         'item_id'    => $blogs_template->blog->admin_user_id,
    351                         'title'      => $r['title'],
    352                         // 'avatar_dir' => 'blog-avatars',
    353                         // 'object'     => 'blog',
    354                         'type'       => $r['type'],
    355                         'alt'        => $r['alt'],
    356                         'css_id'     => $r['id'],
    357                         'class'      => $r['class'],
    358                         'width'      => $r['width'],
    359                         'height'     => $r['height']
    360                 ) );
     348                // Use site icon if available.
     349                $avatar = '';
     350                if ( bp_is_active( 'blogs', 'site-icon' ) && function_exists( 'has_site_icon' ) ) {
     351                        $site_icon = bp_blogs_get_blogmeta( bp_get_blog_id(), "site_icon_url_{$r['type']}" );
     352
     353                        // Never attempted to fetch site icon before; do it now!
     354                        if ( '' === $site_icon ) {
     355                                switch_to_blog( bp_get_blog_id() );
     356
     357                                // Fetch the other size first.
     358                                if ( 'full' === $r['type'] ) {
     359                                        $size      = bp_core_avatar_thumb_width();
     360                                        $save_size = 'thumb';
     361                                } else {
     362                                        $size      = bp_core_avatar_full_width();
     363                                        $save_size = 'full';
     364                                }
     365
     366                                $site_icon = get_site_icon_url( $size );
     367                                // Empty site icons get saved as integer 0.
     368                                if ( empty( $site_icon ) ) {
     369                                        $site_icon = 0;
     370                                }
     371
     372                                // Sync site icon for other size to blogmeta.
     373                                bp_blogs_update_blogmeta( bp_get_blog_id(), "site_icon_url_{$save_size}", $site_icon );
     374
     375                                // Now, fetch the size we want.
     376                                if ( 0 !== $site_icon ) {
     377                                        $size      = 'full' === $r['type'] ? bp_core_avatar_full_width() : bp_core_avatar_thumb_width();
     378                                        $site_icon = get_site_icon_url( $size );
     379                                }
     380
     381                                // Sync site icon to blogmeta.
     382                                bp_blogs_update_blogmeta( bp_get_blog_id(), "site_icon_url_{$r['type']}", $site_icon );
     383
     384                                restore_current_blog();
     385                        }
     386
     387                        // We have a site icon.
     388                        if ( ! is_numeric( $site_icon ) ) {
     389                                if ( empty( $r['width'] ) && ! isset( $size ) ) {
     390                                        $size = 'full' === $r['type'] ? bp_core_avatar_full_width() : bp_core_avatar_thumb_width();
     391                                } else {
     392                                        $size = (int) $r['width'];
     393                                }
     394
     395                                $avatar = sprintf( '<img src="%1$s" class="%2$s" width="%3$s" height="%3$s" alt="%4$s" title="%4$s" />',
     396                                        esc_url( $site_icon ),
     397                                        esc_attr( "{$r['class']} avatar-{$size}" ),
     398                                        esc_attr( $size ),
     399                                        sprintf( esc_attr__( 'Site icon for %s', 'buddypress' ), bp_get_blog_name() )
     400                                );
     401                        }
     402                }
     403
     404                // Fallback to user ID avatar.
     405                if ( '' === $avatar ) {
     406                        $avatar = bp_core_fetch_avatar( array(
     407                                'item_id'    => $blogs_template->blog->admin_user_id,
     408                                'title'      => $r['title'],
     409                                // 'avatar_dir' => 'blog-avatars',
     410                                // 'object'     => 'blog',
     411                                'type'       => $r['type'],
     412                                'alt'        => $r['alt'],
     413                                'css_id'     => $r['id'],
     414                                'class'      => $r['class'],
     415                                'width'      => $r['width'],
     416                                'height'     => $r['height']
     417                        ) );
     418                }
    361419
    362420                /**
Note: See TracChangeset for help on using the changeset viewer.