Skip to:
Content

BuddyPress.org

Changeset 12779


Ignore:
Timestamp:
11/11/2020 01:33:08 PM (4 years ago)
Author:
imath
Message:

BP Blogs: improve Site Icons / Blavatar synchronization

Make sure BuddyPress tries to synchronize Sites Icons / Blavatars even when it's not network activated.

Add a new BP Repair tool to repair this synchronization. This can be needed when a different site of the network has its site icon updated after the BuddyPress Sites directory already listed this site previously.

Props vapvarun

Fixes #8384

Location:
trunk
Files:
7 edited

Legend:

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

    r12745 r12779  
    550550 */
    551551function bp_blogs_update_option_site_icon( $old_value, $new_value ) {
     552    $blog_id = get_current_blog_id();
     553
    552554    if ( 0 === $new_value ) {
    553         bp_blogs_update_blogmeta( get_current_blog_id(), 'site_icon_url_thumb', 0 );
    554         bp_blogs_update_blogmeta( get_current_blog_id(), 'site_icon_url_full',  0 );
     555        bp_blogs_update_blogmeta( $blog_id, 'site_icon_url_thumb', 0 );
     556        bp_blogs_update_blogmeta( $blog_id, 'site_icon_url_full',  0 );
    555557    } else {
    556558        // Save site icon URL as blogmeta.
    557         bp_blogs_update_blogmeta( get_current_blog_id(), 'site_icon_url_thumb', get_site_icon_url( bp_core_avatar_thumb_width() ) );
    558         bp_blogs_update_blogmeta( get_current_blog_id(), 'site_icon_url_full',  get_site_icon_url( bp_core_avatar_full_width()  ) );
     559        bp_blogs_update_blogmeta( $blog_id, 'site_icon_url_thumb', bp_blogs_get_site_icon_url( $blog_id, bp_core_avatar_thumb_width() ) );
     560        bp_blogs_update_blogmeta( $blog_id, 'site_icon_url_full',  bp_blogs_get_site_icon_url( $blog_id, bp_core_avatar_full_width()  ) );
    559561    }
    560562}
     
    15641566    return wpmu_validate_blog_signup( $blog_name, $blog_title, $user );
    15651567}
     1568
     1569/**
     1570 * Gets the site icon URL even when BuddyPress is not network activated.
     1571 *
     1572 * @since 7.0.0
     1573 *
     1574 * @param integer $blog_id The ID of the blog to get the site icon URL for.
     1575 * @param integer $size    The size of the site icon.
     1576 * @return string          The site icon URL
     1577 */
     1578function bp_blogs_get_site_icon_url( $blog_id = 0, $size = 512 ) {
     1579    if ( is_multisite() && ! bp_is_network_activated() && ! bp_is_root_blog( $blog_id ) ) {
     1580        $switched_blog = false;
     1581        $url           = '';
     1582
     1583        if ( $blog_id && get_current_blog_id() !== (int) $blog_id ) {
     1584            switch_to_blog( $blog_id );
     1585            $switched_blog = true;
     1586        }
     1587
     1588        $site_icon_id = get_option( 'site_icon' );
     1589
     1590        if ( $site_icon_id ) {
     1591            $site_icon_data = wp_get_attachment_metadata( $site_icon_id );
     1592            $sizes          = wp_list_pluck( $site_icon_data['sizes'], 'width' );
     1593
     1594            sort( $sizes );
     1595            $closest = 'full';
     1596
     1597            foreach ( $sizes as $width ) {
     1598                $closest = array( $width, $width );
     1599
     1600                if ( (int) $size < (int) $width ) {
     1601                    break;
     1602                }
     1603            }
     1604
     1605            $url = wp_get_attachment_image_url( $site_icon_id, $closest );
     1606        }
     1607
     1608        if ( $switched_blog ) {
     1609            restore_current_blog();
     1610        }
     1611
     1612        return $url;
     1613    }
     1614
     1615    return get_site_icon_url( $size, '', $blog_id );
     1616}
  • trunk/src/bp-blogs/bp-blogs-template.php

    r12772 r12779  
    381381            // Never attempted to fetch site icon before; do it now!
    382382            if ( '' === $site_icon ) {
    383                 switch_to_blog( $blog_id );
    384 
    385383                // Fetch the other size first.
    386384                if ( 'full' === $r['type'] ) {
     
    392390                }
    393391
    394                 $site_icon = get_site_icon_url( $size );
     392                $site_icon = bp_blogs_get_site_icon_url( $blog_id, $size );
     393
    395394                // Empty site icons get saved as integer 0.
    396395                if ( empty( $site_icon ) ) {
     
    404403                if ( 0 !== $site_icon ) {
    405404                    $size      = 'full' === $r['type'] ? bp_core_avatar_full_width() : bp_core_avatar_thumb_width();
    406                     $site_icon = get_site_icon_url( $size );
     405                    $site_icon = bp_blogs_get_site_icon_url( $blog_id, $size );
    407406                }
    408407
    409408                // Sync site icon to blogmeta.
    410409                bp_blogs_update_blogmeta( $blog_id, "site_icon_url_{$r['type']}", $site_icon );
    411 
    412                 restore_current_blog();
    413410            }
    414411
  • trunk/src/bp-core/admin/bp-core-admin-tools.php

    r12758 r12779  
    144144            'bp_admin_repair_blog_records',
    145145        );
     146
     147        if ( bp_is_active( 'blogs', 'site-icon' ) ) {
     148            $repair_list[91] = array(
     149                'bp-blog-site-icons',
     150                __( 'Repair site tracking site icons/blog avatars synchronization.', 'buddypress' ),
     151                'bp_admin_repair_blog_site_icons',
     152            );
     153        }
    146154    }
    147155
     
    311319    // All done!
    312320    return array( 0, sprintf( $statement, $result ) );
     321}
     322
     323/**
     324 * Repair site icons/blog avatars synchronization.
     325 *
     326 * @since 7.0.0
     327 *
     328 * @return array
     329 */
     330function bp_admin_repair_blog_site_icons() {
     331
     332    /* translators: %s: the result of the action performed by the repair tool */
     333    $statement = __( 'Repairing site icons/blog avatars synchronization&hellip; %s', 'buddypress' );
     334
     335    // Run function if blogs component is active.
     336    if ( bp_is_active( 'blogs', 'site-icon' ) ) {
     337        $blog_ids = get_sites(
     338            array(
     339                'fields'   => 'ids',
     340                'archived' => 0,
     341                'mature'   => 0,
     342                'spam'     => 0,
     343                'deleted'  => 0,
     344            )
     345        );
     346
     347        $sizes = array(
     348            array(
     349                'key'  => 'site_icon_url_full',
     350                'size' => bp_core_avatar_full_width(),
     351            ),
     352            array(
     353                'key'  => 'site_icon_url_thumb',
     354                'size' => bp_core_avatar_thumb_width(),
     355            ),
     356        );
     357
     358        foreach ( $blog_ids as $blog_id ) {
     359            $site_icon = 0;
     360
     361            foreach ( $sizes as $size ) {
     362                $site_icon = bp_blogs_get_site_icon_url( $blog_id, $size['size'] );
     363                if ( ! $site_icon ) {
     364                    $site_icon = 0;
     365                }
     366
     367                bp_blogs_update_blogmeta( $blog_id, $size['key'], $site_icon );
     368            }
     369        }
     370    }
     371
     372    // All done!
     373    return array( 0, sprintf( $statement, __( 'Complete!', 'buddypress' ) ) );
    313374}
    314375
  • trunk/src/bp-templates/bp-nouveau/common-styles/_bp_blogs_loop.scss

    r12082 r12779  
    77
    88        li {
     9
     10            .item-avatar {
     11
     12                img.avatar-150 {
     13                    max-width: 150px;
     14                    max-height: 150px;
     15                }
     16
     17                img.avatar-50 {
     18                    max-width: 50px;
     19                    max-height: 50px;
     20                }
     21            }
    922
    1023            @include medium-up() {
  • trunk/src/bp-templates/bp-nouveau/css/buddypress-rtl.css

    r12773 r12779  
    17751775*----------------------------------------------------------
    17761776*/
     1777.buddypress-wrap .blogs-list li .item-avatar img.avatar-150 {
     1778    max-width: 150px;
     1779    max-height: 150px;
     1780}
     1781
     1782.buddypress-wrap .blogs-list li .item-avatar img.avatar-50 {
     1783    max-width: 50px;
     1784    max-height: 50px;
     1785}
     1786
    17771787@media screen and (min-width: 46.8em) {
    17781788    .buddypress-wrap .blogs-list li .item-block {
  • trunk/src/bp-templates/bp-nouveau/css/buddypress.css

    r12773 r12779  
    17751775*----------------------------------------------------------
    17761776*/
     1777.buddypress-wrap .blogs-list li .item-avatar img.avatar-150 {
     1778    max-width: 150px;
     1779    max-height: 150px;
     1780}
     1781
     1782.buddypress-wrap .blogs-list li .item-avatar img.avatar-50 {
     1783    max-width: 50px;
     1784    max-height: 50px;
     1785}
     1786
    17771787@media screen and (min-width: 46.8em) {
    17781788    .buddypress-wrap .blogs-list li .item-block {
  • trunk/tests/phpunit/testcases/blogs/template.php

    r12772 r12779  
    432432
    433433        add_filter( 'get_site_icon_url', array( $this, 'filter_blog_avatar' ) );
     434        add_filter( 'bp_is_network_activated', '__return_true' );
    434435
    435436        $avatar = bp_get_blog_avatar( array(
     
    442443        ) );
    443444
     445        remove_filter( 'bp_is_network_activated', '__return_true' );
    444446        remove_filter( 'get_site_icon_url', array( $this, 'filter_blog_avatar' ) );
    445447        $blogs_template = $reset_blogs_template;
Note: See TracChangeset for help on using the changeset viewer.