Skip to:
Content

BuddyPress.org

Changeset 11150


Ignore:
Timestamp:
09/22/2016 12:35:27 AM (8 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.

Location:
trunk/src/bp-blogs
Files:
4 edited

Legend:

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

    r10825 r11150  
    122122add_filter( 'bp_activity_post_pre_publish', 'bp_blogs_post_pre_publish', 10, 4 );
    123123add_filter( 'bp_activity_post_pre_comment', 'bp_blogs_post_pre_publish', 10, 4 );
     124
     125/**
     126 * Registers our custom thumb size with WP's Site Icon feature.
     127 *
     128 * @since 2.7.0
     129 *
     130 * @param  array $sizes Current array of custom site icon sizes.
     131 * @return array
     132 */
     133function bp_blogs_register_custom_site_icon_size( $sizes ) {
     134    $sizes[] = bp_core_avatar_thumb_width();
     135    return $sizes;
     136}
     137add_filter( 'site_icon_image_sizes', 'bp_blogs_register_custom_site_icon_size' );
  • trunk/src/bp-blogs/bp-blogs-functions.php

    r10971 r11150  
    477477
    478478/**
     479 * Syncs site icon URLs to blogmeta.
     480 *
     481 * @since 2.7.0
     482 *
     483 * @param int|string $old_value Old value
     484 * @param int|string $new_value New value
     485 */
     486function bp_blogs_update_option_site_icon( $old_value, $new_value ) {
     487    if ( 0 === $new_value ) {
     488        bp_blogs_update_blogmeta( get_current_blog_id(), 'site_icon_url_thumb', 0 );
     489        bp_blogs_update_blogmeta( get_current_blog_id(), 'site_icon_url_full',  0 );
     490    } else {
     491        // Save site icon URL as blogmeta.
     492        bp_blogs_update_blogmeta( get_current_blog_id(), 'site_icon_url_thumb', get_site_icon_url( bp_core_avatar_thumb_width() ) );
     493        bp_blogs_update_blogmeta( get_current_blog_id(), 'site_icon_url_full',  get_site_icon_url( bp_core_avatar_full_width()  ) );
     494    }
     495}
     496add_action( 'update_option_site_icon', 'bp_blogs_update_option_site_icon', 10, 2 );
     497
     498/**
    479499 * Deletes the 'url' blogmeta for a site.
    480500 *
  • 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        /**
  • trunk/src/bp-blogs/classes/class-bp-blogs-component.php

    r11081 r11150  
    3333                'adminbar_myaccount_order' => 30,
    3434                'search_query_arg' => 'sites_search',
     35                'features' => array( 'site-icon' )
    3536            )
    3637        );
Note: See TracChangeset for help on using the changeset viewer.