Skip to:
Content

BuddyPress.org

Ticket #6544: 6544.2.patch

File 6544.2.patch, 8.1 KB (added by imath, 8 years ago)
  • src/bp-blogs/bp-blogs-functions.php

    diff --git src/bp-blogs/bp-blogs-functions.php src/bp-blogs/bp-blogs-functions.php
    index dca5189..836c081 100644
    function bp_blogs_restore_data( $user_id = 0 ) { 
    14971497        }
    14981498}
    14991499add_action( 'bp_make_ham_user', 'bp_blogs_restore_data', 10, 1 );
     1500
     1501/**
     1502 * Generate the avatar upload directory path for a given blog.
     1503 *
     1504 * @since BuddyPress (2.4.0)
     1505 *
     1506 * @param int $blog_id Optional. ID of the blog. Default: ID of the
     1507 *        current blog.
     1508 * @return string
     1509 */
     1510function blogs_avatar_upload_dir( $blog_id = 0 ) {
     1511
     1512        if ( empty( $blog_id ) ) {
     1513                $blog_id = get_current_blog_id();
     1514        }
     1515
     1516        $directory = 'blog-avatars';
     1517        $path      = bp_core_avatar_upload_path() . '/' . $directory . '/' . $blog_id;
     1518        $newbdir   = $path;
     1519        $newurl    = bp_core_avatar_url() . '/' . $directory . '/' . $blog_id;
     1520        $newburl   = $newurl;
     1521        $newsubdir = '/' . $directory . '/' . $blog_id;
     1522
     1523        /**
     1524         * Filters the avatar upload directory path for a given blog.
     1525         *
     1526         * @since BuddyPress (2.4.0)
     1527         *
     1528         * @param array $value Array of parts related to the blog avatar upload directory.
     1529         */
     1530        return apply_filters( 'blogs_avatar_upload_dir', array(
     1531                'path'    => $path,
     1532                'url'     => $newurl,
     1533                'subdir'  => $newsubdir,
     1534                'basedir' => $newbdir,
     1535                'baseurl' => $newburl,
     1536                'error'   => false
     1537        ) );
     1538}
     1539
     1540/**
     1541 * Update the blog's profile photo if the site icon was updated
     1542 *
     1543 * @since BuddyPress (2.4.0)
     1544 *
     1545 * @param string $option name of the option. Passed by do_action() but
     1546 *        unused here.
     1547 * @param string $value Value to save meta to.
     1548 */
     1549function bp_blogs_use_site_icon_as_avatar( $option = 'site_icon', $value = 0 ) {
     1550        /**
     1551         * Filter here to disable blog's avatar / site icon synchronization
     1552         *
     1553         * @since BuddyPress (2.4.0)
     1554         *
     1555         * @param bool True to use the site icon as the blog's profile photo.
     1556         *             False otherwise
     1557         */
     1558        if ( ! is_multisite() || false === (bool) apply_filters( 'bp_blogs_use_site_icon_as_avatar', true ) ) {
     1559                return;
     1560        }
     1561
     1562        $site_icon_id   = (int) $value;
     1563        $site_icon_data = wp_get_attachment_metadata( $site_icon_id );
     1564
     1565        if ( empty( $site_icon_data['file'] ) ) {
     1566                return false;
     1567        }
     1568
     1569        // Get the blog's upload dir
     1570        $upload_dir = wp_upload_dir();
     1571        $blog_id    = get_current_blog_id();
     1572
     1573        // Create a profile photo for the blog
     1574        if ( bp_avatar_create_item_avatar( array(
     1575                'item_id'   => $blog_id,
     1576                'object'    => 'blog',
     1577                'component' => buddypress()->blogs->id,
     1578                'image'     => trailingslashit( $upload_dir['basedir'] ) . $site_icon_data['file'],
     1579                'crop_w'    => $site_icon_data['width'],
     1580                'crop_h'    => $site_icon_data['height'],
     1581        ) ) ) {
     1582                //The blog's profile photo is synchronized with the site icon
     1583                bp_blogs_update_blogmeta( $blog_id, 'blog_avatar_type', 'site_icon' );
     1584        }
     1585}
     1586add_action( 'add_option_site_icon', 'bp_blogs_use_site_icon_as_avatar', 10, 2 );
     1587
     1588/**
     1589 * Delete the blog's profile photo if the site icon was deleted
     1590 * and if the site icon was used as the blog's profile photo
     1591 *
     1592 * @since BuddyPress (2.4.0)
     1593 */
     1594function bp_blogs_delete_blog_profile_photo() {
     1595        if ( ! is_multisite() ) {
     1596                return;
     1597        }
     1598
     1599        $blog_id = get_current_blog_id();
     1600
     1601        // Only delete the blog's avatar if synced with site icon
     1602        if ( 'site_icon' === bp_blogs_get_blogmeta( $blog_id, 'blog_avatar_type' ) ) {
     1603                // Remove the blog's profile photo
     1604                bp_core_delete_existing_avatar( array( 'item_id' => $blog_id, 'object' => 'blog' ) );
     1605
     1606                // Reset the blog avatar type
     1607                bp_blogs_delete_blogmeta( $blog_id, 'blog_avatar_type' );
     1608        }
     1609}
     1610add_action( 'delete_option_site_icon', 'bp_blogs_delete_blog_profile_photo' );
  • src/bp-blogs/bp-blogs-template.php

    diff --git src/bp-blogs/bp-blogs-template.php src/bp-blogs/bp-blogs-template.php
    index 8fd7e9c..2730fd9 100644
    function bp_blog_avatar( $args = '' ) { 
    567567                        'no_grav' => true,
    568568                ) );
    569569
     570                // Defaults to the admin avatar
     571                $item_id    = $blogs_template->blog->admin_user_id;
     572                $object     = 'user';
     573                $avatar_dir = 'avatars';
     574
     575                if ( function_exists( 'has_site_icon' ) ) {
     576                        $object     = 'blog';
     577                        $avatar_dir = 'blog-avatars';
     578                        $item_id    = $blogs_template->blog->blog_id;
     579                        $r['title'] = sprintf( __( 'Site logo of %s', 'buddypress' ), esc_attr( $blogs_template->blog->name ) );
     580                        $r['class'] = 'avatar blog-' . $blogs_template->blog->blog_id . '-avatar';
     581                        $r['alt']   = sprintf( __( 'Site logo of %s', 'buddypress' ), esc_attr( $blogs_template->blog->name ) );
     582                }
     583
    570584                // Fetch the avatar
    571585                $avatar = bp_core_fetch_avatar( array(
    572586                        'item_id'    => $blogs_template->blog->admin_user_id,
    573587                        'title'      => $r['title'],
    574                         //'avatar_dir' => 'blog-avatars',
    575                         //'object'     => 'blog',
     588                        'avatar_dir' => $avatar_dir,
     589                        'object'     => $object,
    576590                        'type'       => $r['type'],
    577591                        'alt'        => $r['alt'],
    578592                        'css_id'     => $r['id'],
  • src/bp-core/bp-core-avatars.php

    diff --git src/bp-core/bp-core-avatars.php src/bp-core/bp-core-avatars.php
    index b3dcb04..786c569 100644
    function bp_avatar_ajax_set() { 
    12551255add_action( 'wp_ajax_bp_avatar_set', 'bp_avatar_ajax_set' );
    12561256
    12571257/**
     1258 * Use the absolute path to an image to set an object's avatar
     1259 *
     1260 * @since BuddyPress (?)
     1261 *
     1262 * @param  array  $args {
     1263 *     @type int    $item_id   The ID of the object
     1264 *     @type string $object    The object type (eg: group, user, blog)
     1265 *     @type string $component The component for the object (eg: groups, xprofile, blogs)
     1266 *     @type string $image     The absolute path to the image
     1267 *     @type int    $crop_w    Crop width. Default: the global 'full' avatar width,
     1268 *                             as retrieved by bp_core_avatar_full_width().
     1269 *     @type int    $crop_h    Crop height. Default: the global 'full' avatar height,
     1270 *                             as retrieved by bp_core_avatar_full_height().
     1271 *     @type int    $crop_x    The horizontal starting point of the crop. Default: 0.
     1272 *     @type int    $crop_y    The vertical starting point of the crop. Default: 0.
     1273 * }
     1274 * @return bool  true on success, false otherwise
     1275 */
     1276function bp_avatar_create_item_avatar( $args = array() ) {
     1277        $r = bp_parse_args( $args, array(
     1278                'item_id'   => 0,
     1279                'object'    => 'user',
     1280                'component' => 'xprofile',
     1281                'image'     => '',
     1282                'crop_w'    => bp_core_avatar_full_width(),
     1283                'crop_h'    => bp_core_avatar_full_height(),
     1284                'crop_x'    => 0,
     1285                'crop_y'    => 0
     1286        ), 'create_item_avatar' );
     1287
     1288        if ( empty( $r['item_id'] ) || ! file_exists( $r['image'] ) || ! @getimagesize( $r['image'] ) ) {
     1289                return false;
     1290        }
     1291
     1292        if ( is_callable( $r['component'] . '_avatar_upload_dir' ) ) {
     1293                $dir_args = array( $r['item_id'] );
     1294
     1295                // In case  of xprofile, we need an extra argument
     1296                if ( 'xprofile' === $r['component'] ) {
     1297                        $dir_args = array( false, $r['item_id'] );
     1298                }
     1299
     1300                $avatar_data = call_user_func_array( $r['component'] . '_avatar_upload_dir', $dir_args );
     1301        }
     1302
     1303        if ( ! isset( $avatar_data['path'] ) || ! isset( $avatar_data['subdir'] ) ) {
     1304                return false;
     1305        }
     1306
     1307        // It's not a regular upload, we may need to create this folder
     1308        if( ! is_dir( $avatar_data['path'] ) ) {
     1309                if ( ! wp_mkdir_p( $avatar_data['path'] ) ) {
     1310                        return false;
     1311                }
     1312        }
     1313
     1314        $image_file_name = wp_unique_filename( $avatar_data['path'], basename( $r['image'] ) );
     1315
     1316        // Copy the image file into the avatar dir
     1317        copy( $r['image'], $avatar_data['path'] . '/' . $image_file_name );
     1318
     1319        // Check the original file is copied
     1320        if ( ! file_exists( $avatar_data['path'] . '/' . $image_file_name ) ) {
     1321                return false;
     1322        }
     1323
     1324        if ( ! bp_core_avatar_handle_crop( array(
     1325                'object'        => $r['object'],
     1326                'avatar_dir'    => trim( dirname( $avatar_data['subdir'] ), '/' ),
     1327                'item_id'       => (int) $r['item_id'],
     1328                'original_file' => trailingslashit( $avatar_data['subdir'] ) . $image_file_name,
     1329                'crop_w'        => $r['crop_w'],
     1330                'crop_h'        => $r['crop_h'],
     1331                'crop_x'        => $r['crop_x'],
     1332                'crop_y'        => $r['crop_y']
     1333        ) ) ) {
     1334                return false;
     1335        } else {
     1336                return true;
     1337        }
     1338}
     1339
     1340/**
    12581341 * Replace default WordPress avatars with BP avatars, if available.
    12591342 *
    12601343 * Filters 'get_avatar'.