Skip to:
Content

BuddyPress.org

Ticket #5958: 5958.3.patch

File 5958.3.patch, 18.5 KB (added by dcavins, 12 years ago)

Changes bp_core_fetch_avatar(); adds tests.

  • src/bp-core/bp-core-avatars.php

    diff --git src/bp-core/bp-core-avatars.php src/bp-core/bp-core-avatars.php
    index 7992132..bde4bb9 100644
    add_action( 'bp_setup_globals', 'bp_core_set_avatar_globals' );  
    174174 * @return string Formatted HTML <img> element, or raw avatar URL based on $html arg.
    175175 */
    176176function bp_core_fetch_avatar( $args = '' ) {
     177        $bp = buddypress();
    177178
    178179        // If avatars are disabled for the root site, obey that request and bail
    179         if ( ! buddypress()->avatar->show_avatars )
     180        if ( ! $bp->avatar->show_avatars ) {
    180181                return;
     182        }
    181183
    182184        global $current_blog;
    183185
    184         $bp = buddypress();
    185 
    186         // Set a few default variables
    187         $def_object = 'user';
    188         $def_type   = 'thumb';
    189         $def_class  = 'avatar';
    190 
    191         // Set the default variables array
     186        // Set the default variables array and parse it against incoming $args array.
    192187        $params = wp_parse_args( $args, array(
    193188                'item_id'    => false,
    194                 'object'     => $def_object, // user/group/blog/custom type (if you use filters)
    195                 'type'       => $def_type,  // thumb or full
     189                'object'     => 'user',          // user/group/blog/custom type (if you use filters)
     190                'type'       => 'thumb',        // thumb or full
    196191                'avatar_dir' => false,       // Specify a custom avatar directory for your object
    197192                'width'      => false,       // Custom width (int)
    198193                'height'     => false,       // Custom height (int)
    199                 'class'      => $def_class, // Custom <img> class (string)
     194                'class'      => 'avatar',        // Custom <img> class (string)
    200195                'css_id'     => false,       // Custom <img> ID (string)
    201196                'alt'        => '',          // Custom <img> alt (string)
    202197                'email'      => false,       // Pass the user email (for gravatar) to prevent querying the DB for it
    … … function bp_core_fetch_avatar( $args = '' ) {  
    204199                'html'       => true,        // Wrap the return img URL in <img />
    205200                'title'      => ''           // Custom <img> title (string)
    206201        ) );
    207         extract( $params, EXTR_SKIP );
    208202
    209203        /** Set item_id ***********************************************************/
    210204
    211         if ( empty( $item_id ) ) {
     205        if ( empty( $params['item_id'] ) ) {
    212206
    213                 switch ( $object ) {
     207                switch ( $params['object'] ) {
    214208
    215209                        case 'blog'  :
    216                                 $item_id = $current_blog->id;
     210                                $params['item_id'] = $current_blog->id;
    217211                                break;
    218212
    219213                        case 'group' :
    220214                                if ( bp_is_active( 'groups' ) ) {
    221                                         $item_id = $bp->groups->current_group->id;
     215                                        $params['item_id'] = $bp->groups->current_group->id;
    222216                                } else {
    223                                         $item_id = false;
     217                                        $params['item_id'] = false;
    224218                                }
    225219
    226220                                break;
    227221
    228222                        case 'user'  :
    229223                        default      :
    230                                 $item_id = bp_displayed_user_id();
     224                                $params['item_id'] = bp_displayed_user_id();
    231225                                break;
    232226                }
    233227
    234                 $item_id = apply_filters( 'bp_core_avatar_item_id', $item_id, $object, $params );
     228                $params['item_id'] = apply_filters( 'bp_core_avatar_item_id', $params['item_id'], $params['object'], $params );
    235229
    236                 if ( empty( $item_id ) ) {
     230                if ( empty( $params['item_id'] ) ) {
    237231                        return false;
    238232                }
    239233        }
    240234
    241         $class = apply_filters( 'bp_core_avatar_class', $class, $item_id, $object, $params );
    242 
    243235        /** Set avatar_dir ********************************************************/
    244236
    245         if ( empty( $avatar_dir ) ) {
     237        if ( empty( $params['avatar_dir'] ) ) {
    246238
    247                 switch ( $object ) {
     239                switch ( $params['object'] ) {
    248240
    249241                        case 'blog'  :
    250                                 $avatar_dir = 'blog-avatars';
     242                                $params['avatar_dir'] = 'blog-avatars';
    251243                                break;
    252244
    253245                        case 'group' :
    254246                                if ( bp_is_active( 'groups' ) ) {
    255                                         $avatar_dir = 'group-avatars';
     247                                        $params['avatar_dir'] = 'group-avatars';
    256248                                } else {
    257                                         $avatar_dir = false;
     249                                        $params['avatar_dir'] = false;
    258250                                }
    259251
    260252                                break;
    261253
    262254                        case 'user'  :
    263255                        default      :
    264                                 $avatar_dir = 'avatars';
     256                                $params['avatar_dir'] = 'avatars';
    265257                                break;
    266258                }
    267259
    268                 $avatar_dir = apply_filters( 'bp_core_avatar_dir', $avatar_dir, $object, $params );
     260                $params['avatar_dir'] = apply_filters( 'bp_core_avatar_dir', $params['avatar_dir'], $params['object'], $params );
    269261
    270                 if ( empty( $avatar_dir ) ) {
     262                if ( empty( $params['avatar_dir'] ) ) {
    271263                        return false;
    272264                }
    273265        }
    274266
    275267        /** <img> alt *************************************************************/
    276268
    277         if ( false !== strpos( $alt, '%s' ) || false !== strpos( $alt, '%1$s' ) ) {
     269        if ( false !== strpos( $params['alt'], '%s' ) || false !== strpos( $params['alt'], '%1$s' ) ) {
    278270
    279                 switch ( $object ) {
     271                switch ( $params['object'] ) {
    280272
    281273                        case 'blog'  :
    282                                 $item_name = get_blog_option( $item_id, 'blogname' );
     274                                $item_name = get_blog_option( $params['item_id'], 'blogname' );
    283275                                break;
    284276
    285277                        case 'group' :
    286                                 $item_name = bp_get_group_name( groups_get_group( array( 'group_id' => $item_id ) ) );
     278                                $item_name = bp_get_group_name( groups_get_group( array( 'group_id' => $params['item_id'] ) ) );
    287279                                break;
    288280
    289281                        case 'user'  :
    290282                        default :
    291                                 $item_name = bp_core_get_user_displayname( $item_id );
     283                                $item_name = bp_core_get_user_displayname( $params['item_id'] );
    292284                                break;
    293285                }
    294286
    295                 $item_name = apply_filters( 'bp_core_avatar_alt', $item_name, $item_id, $object, $params );
    296                 $alt       = sprintf( $alt, $item_name );
     287                $item_name = apply_filters( 'bp_core_avatar_alt', $item_name, $params['item_id'], $params['object'], $params );
     288                $params['alt'] = sprintf( $params['alt'], $item_name );
    297289        }
    298290
    299291        /** Sanity Checks *********************************************************/
    300292
    301         // Get a fallback for the 'alt' parameter
    302         if ( empty( $alt ) )
    303                 $alt = __( 'Profile Photo', 'buddypress' );
     293        // Get a fallback for the 'alt' parameter, create html output
     294        if ( empty( $params['alt'] ) ) {
     295                $params['alt'] = __( 'Profile Photo', 'buddypress' );
     296        }
     297        $html_alt = ' alt="' . esc_attr( $params['alt'] ) . '"';
    304298
    305         $html_alt = ' alt="' . esc_attr( $alt ) . '"';
     299        // Filter image title and create html string
     300        $html_title = '';
     301        $params['title'] = apply_filters( 'bp_core_avatar_title', $params['title'], $params['item_id'], $params['object'], $params );
    306302
    307         // Set title tag, if it's been provided
    308         if ( !empty( $title ) ) {
    309                 $title = " title='" . esc_attr( apply_filters( 'bp_core_avatar_title', $title, $item_id, $object, $params ) ) . "'";
     303        if ( ! empty( $params['title'] ) ) {
     304                $html_title = ' title="' . esc_attr( $params['title'] ) . '"';
    310305        }
    311306
    312         // Set CSS ID if passed
    313         if ( !empty( $css_id ) ) {
    314                 $css_id = ' id="' . esc_attr( $css_id ) . '"';
     307        // Set CSS ID and create html string
     308        $html_css_id = '';
     309        $params['css_id'] = apply_filters( 'bp_core_css_id', $params['css_id'], $params['item_id'], $params['object'], $params );
     310       
     311        if ( ! empty( $params['css_id'] ) ) {
     312                $html_css_id = ' id="' . esc_attr( $params['css_id'] ) . '"';
    315313        }
    316314
    317315        // Set image width
    318         if ( false !== $width ) {
    319                 $html_width = ' width="' . $width . '"';
    320         } elseif ( 'thumb' == $type ) {
    321                 $html_width = ' width="' . bp_core_avatar_thumb_width() . '"';
     316        if ( false !== $params['width'] ) {
     317                // Width has been specified. No modification necessary.
     318        } else if ( 'thumb' == $params['type'] ) {
     319                $params['width'] = bp_core_avatar_thumb_width();
    322320        } else {
    323                 $html_width = ' width="' . bp_core_avatar_full_width() . '"';
     321                $params['width'] = bp_core_avatar_full_width();
    324322        }
     323        $html_width = ' width="' . $params['width'] . '"';
    325324
    326325        // Set image height
    327         if ( false !== $height ) {
    328                 $html_height = ' height="' . $height . '"';
    329         } elseif ( 'thumb' == $type ) {
    330                 $html_height = ' height="' . bp_core_avatar_thumb_height() . '"';
     326        if ( false !== $params['height'] ) {
     327                // Height has been specified. No modification necessary.
     328        } else if ( 'thumb' == $params['type'] ) {
     329                $params['height'] = bp_core_avatar_thumb_height();
    331330        } else {
    332                 $html_height = ' height="' . bp_core_avatar_full_height() . '"';
     331                $params['height'] = bp_core_avatar_full_height();
    333332        }
     333        $html_height = ' height="' . $params['height'] . '"';
     334
     335        // Create CSS class html string
     336        $params['class'] = apply_filters( 'bp_core_avatar_class', $params['class'], $params['item_id'], $params['object'], $params );
     337        $html_class = ' class="' . sanitize_html_class( $params['class'] ) . ' ' . sanitize_html_class( $params['object'] . '-' . $params['item_id'] . '-avatar' ) . ' ' . sanitize_html_class( 'avatar-' . $params['width'] ) . ' photo"';
    334338
    335339        // Set img URL and DIR based on prepopulated constants
    336340        $avatar_loc        = new stdClass();
    337341        $avatar_loc->path  = trailingslashit( bp_core_avatar_upload_path() );
    338342        $avatar_loc->url   = trailingslashit( bp_core_avatar_url() );
    339343
    340         $avatar_loc->dir   = trailingslashit( $avatar_dir );
    341         $avatar_folder_url = apply_filters( 'bp_core_avatar_folder_url', ( $avatar_loc->url  . $avatar_loc->dir . $item_id ), $item_id, $object, $avatar_dir );
    342         $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', ( $avatar_loc->path . $avatar_loc->dir . $item_id ), $item_id, $object, $avatar_dir );
    343 
    344         // Add an identifying class
    345         $class .= ' ' . $object . '-' . $item_id . '-avatar ' . sanitize_html_class( "avatar-$width" ) . ' photo';
     344        $avatar_loc->dir   = trailingslashit( $params['avatar_dir'] );
     345        $avatar_folder_url = apply_filters( 'bp_core_avatar_folder_url', ( $avatar_loc->url  . $avatar_loc->dir . $params['item_id'] ), $params['item_id'], $params['object'], $params['avatar_dir'] );
     346        $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', ( $avatar_loc->path . $avatar_loc->dir . $params['item_id'] ), $params['item_id'], $params['object'], $params['avatar_dir'] );
    346347
    347348        /**
    348349         * Look for uploaded avatar first. Use it if it exists.
    349350         * Set the file names to search for, to select the full size
    350351         * or thumbnail image.
    351352         */
    352         $avatar_size              = ( 'full' == $type ) ? '-bpfull' : '-bpthumb';
    353         $legacy_user_avatar_name  = ( 'full' == $type ) ? '-avatar2' : '-avatar1';
    354         $legacy_group_avatar_name = ( 'full' == $type ) ? '-groupavatar-full' : '-groupavatar-thumb';
     353        $avatar_size              = ( 'full' == $params['type'] ) ? '-bpfull' : '-bpthumb';
     354        $legacy_user_avatar_name  = ( 'full' == $params['type'] ) ? '-avatar2' : '-avatar1';
     355        $legacy_group_avatar_name = ( 'full' == $params['type'] ) ? '-groupavatar-full' : '-groupavatar-thumb';
    355356
    356357        // Check for directory
    357358        if ( file_exists( $avatar_folder_dir ) ) {
    … … function bp_core_fetch_avatar( $args = '' ) {  
    405406                if ( isset( $avatar_url ) ) {
    406407
    407408                        // Return it wrapped in an <img> element
    408                         if ( true === $html ) {
    409                                 return apply_filters( 'bp_core_fetch_avatar', '<img src="' . $avatar_url . '" class="' . esc_attr( $class ) . '"' . $css_id . $html_width . $html_height . $html_alt . $title . ' />', $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );
     409                        if ( true === $params['html'] ) {
     410                                return apply_filters( 'bp_core_fetch_avatar', '<img src="' . $avatar_url . '"' . $html_class . $html_css_id  . $html_width . $html_height . $html_alt . $html_title . ' />', $params, $params['item_id'], $params['avatar_dir'], $html_css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );
    410411
    411412                        // ...or only the URL
    412413                        } else {
    … … function bp_core_fetch_avatar( $args = '' ) {  
    417418
    418419        // If no avatars could be found, try to display a gravatar
    419420
    420         // Skips gravatar check if $no_grav is passed
    421         if ( ! apply_filters( 'bp_core_fetch_avatar_no_grav', $no_grav ) ) {
    422 
    423                 // Set gravatar size
    424                 if ( false !== $width ) {
    425                         $grav_size = $width;
    426                 } else if ( 'full' == $type ) {
    427                         $grav_size = bp_core_avatar_full_width();
    428                 } else if ( 'thumb' == $type ) {
    429                         $grav_size = bp_core_avatar_thumb_width();
    430                 }
     421        // Skips gravatar check if $params['no_grav'] is passed
     422        if ( ! apply_filters( 'bp_core_fetch_avatar_no_grav', $params['no_grav'], $params ) ) {
    431423
    432424                // Set gravatar type
    433                 if ( empty( $bp->grav_default->{$object} ) ) {
     425                if ( empty( $bp->grav_default->{$params['object']} ) ) {
    434426                        $default_grav = 'wavatar';
    435                 } else if ( 'mystery' == $bp->grav_default->{$object} ) {
    436                         $default_grav = apply_filters( 'bp_core_mysteryman_src', 'mm', $grav_size );
     427                } else if ( 'mystery' == $bp->grav_default->{$params['object']} ) {
     428                        $default_grav = apply_filters( 'bp_core_mysteryman_src', 'mm', $params['width'] );
    437429                } else {
    438                         $default_grav = $bp->grav_default->{$object};
     430                        $default_grav = $bp->grav_default->{$params['object']};
    439431                }
    440432
    441433                // Set gravatar object
    442                 if ( empty( $email ) ) {
    443                         if ( 'user' == $object ) {
    444                                 $email = bp_core_get_user_email( $item_id );
    445                         } else if ( 'group' == $object || 'blog' == $object ) {
    446                                 $email = "{$item_id}-{$object}@{bp_get_root_domain()}";
     434                if ( empty( $params['email'] ) ) {
     435                        if ( 'user' == $params['object'] ) {
     436                                $params['email'] = bp_core_get_user_email( $params['item_id'] );
     437                        } else if ( 'group' == $params['object'] || 'blog' == $params['object'] ) {
     438                                $params['email'] = $params['item_id'] . '-' . $params['object'] . '@' . bp_get_root_domain();
    447439                        }
    448440                }
    449441
    … … function bp_core_fetch_avatar( $args = '' ) {  
    454446                }
    455447
    456448                // Filter gravatar vars
    457                 $email    = apply_filters( 'bp_core_gravatar_email', $email, $item_id, $object );
    458                 $gravatar = apply_filters( 'bp_gravatar_url', $host ) . md5( strtolower( $email ) ) . '?d=' . $default_grav . '&amp;s=' . $grav_size;
     449                $params['email'] = apply_filters( 'bp_core_gravatar_email', $params['email'], $params['item_id'], $params['object'] );
     450                $gravatar = apply_filters( 'bp_gravatar_url', $host ) . md5( strtolower( $params['email'] ) ) . '?d=' . $default_grav . '&amp;s=' . $params['width'];
    459451
    460452                // Gravatar rating; http://bit.ly/89QxZA
    461453                $rating = get_option( 'avatar_rating' );
    … … function bp_core_fetch_avatar( $args = '' ) {  
    465457
    466458        // No avatar was found, and we've been told not to use a gravatar.
    467459        } else {
    468                 $gravatar = apply_filters( "bp_core_default_avatar_$object", bp_core_avatar_default( 'local' ), $params );
     460                $gravatar = apply_filters( 'bp_core_default_avatar_' . $params['object'], bp_core_avatar_default( 'local' ), $params );
    469461        }
    470462
    471         if ( true === $html ) {
    472                 return apply_filters( 'bp_core_fetch_avatar', '<img src="' . $gravatar . '" class="' . esc_attr( $class ) . '"' . $css_id . $html_width . $html_height . $html_alt . $title . ' />', $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );
     463        if ( true === $params['html'] ) {
     464                return apply_filters( 'bp_core_fetch_avatar', '<img src="' . $gravatar . '"' . $html_css_id . $html_class . $html_width . $html_height . $html_alt . $html_title . ' />', $params, $params['item_id'], $params['avatar_dir'], $html_css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );
    473465        } else {
    474466                return apply_filters( 'bp_core_fetch_avatar_url', $gravatar, $params );
    475467        }
  • tests/phpunit/testcases/core/avatars.php

    diff --git tests/phpunit/testcases/core/avatars.php tests/phpunit/testcases/core/avatars.php
    index 8070b7b..316ad26 100644
     
    66class BP_Tests_Avatars extends BP_UnitTestCase {
    77        protected $old_current_user = 0;
    88
     9        private $params = array();
     10
    911        public function setUp() {
    1012                parent::setUp();
    1113
    … … class BP_Tests_Avatars extends BP_UnitTestCase {  
    110112        public function avatar_cb() {
    111113                return 'foo';
    112114        }
     115
     116        /**
     117         * @group bp_core_fetch_avatar
     118         */
     119        public function test_bp_core_fetch_avatar_parameter_conservation() {
     120                // First, run the check with custom parameters, specifying no gravatar.
     121                $this->params = array(
     122                        'item_id'    => 1406,
     123                        'object'     => 'custom_object',        // user/group/blog/custom type (if you use filters)
     124                        'type'       => 'full',                         // thumb or full
     125                        'avatar_dir' => 'custom-dir',       // Specify a custom avatar directory for your object
     126                        'width'      => 48,                             // Custom width (int)
     127                        'height'     => 54,                             // Custom height (int)
     128                        'class'      => 'custom-class',         // Custom <img> class (string)
     129                        'css_id'     => 'custom-css-id',    // Custom <img> ID (string)
     130                        'alt'        => 'custom alt',           // Custom <img> alt (string)
     131                        'email'      => 'avatar@avatar.org',// Pass the user email (for gravatar) to prevent querying the DB for it
     132                        'no_grav'    => true,                   // If there is no avatar found, return false instead of a grav?
     133                        'html'       => true,                   // Wrap the return img URL in <img />
     134                        'title'      => 'custom-title'          // Custom <img> title (string)
     135                );
     136
     137                // Check to make sure the custom parameters survived the function all the way up to output
     138                add_filter( 'bp_core_fetch_avatar', array( $this, 'bp_core_fetch_avatar_filter_check' ), 12, 2 );
     139                $avatar = bp_core_fetch_avatar( $this->params );
     140
     141                // Re-run check, allowing gravatars.
     142                $this->params['no_grav'] = false;
     143                $avatar = bp_core_fetch_avatar( $this->params );
     144
     145                remove_filter( 'bp_core_fetch_avatar', array( $this, 'bp_core_fetch_avatar_filter_check' ), 12, 2 );
     146
     147                unset( $this->params );
     148        }
     149
     150        public function bp_core_fetch_avatar_filter_check( $html, $params ) {
     151                // Check that the passed parameters match the original custom parameters.
     152                $this->assertEmpty( array_merge( array_diff( $params, $this->params ), array_diff( $this->params, $params ) ) );
     153
     154                // Check the returned html to see that it matches an expected value.
     155                // Get the correct default avatar, based on whether gravatars are allowed.
     156                if ( $params['no_grav'] ) {
     157                        $avatar_url = bp_core_avatar_default( 'local' );
     158                } else {
     159                        // This test has the slight odor of hokum since it recreates so much code that could be changed at any time.
     160                        $bp = buddypress();
     161                        // Set host based on if using ssl
     162                        $host = 'http://gravatar.com/avatar/';
     163                        if ( is_ssl() ) {
     164                                $host = 'https://secure.gravatar.com/avatar/';
     165                        }
     166                        // Set expected gravatar type
     167                        if ( empty( $bp->grav_default->{$this->params['object']} ) ) {
     168                                $default_grav = 'wavatar';
     169                        } else if ( 'mystery' == $bp->grav_default->{$this->params['object']} ) {
     170                                $default_grav = apply_filters( 'bp_core_mysteryman_src', 'mm', $this->params['width'] );
     171                        } else {
     172                                $default_grav = $bp->grav_default->{$this->params['object']};
     173                        }
     174
     175                        $avatar_url = $host . md5( strtolower( $this->params['email'] ) ) . '?d=' . $default_grav . '&amp;s=' . $this->params['width'];
     176
     177                        // Gravatar rating; http://bit.ly/89QxZA
     178                        $rating = get_option( 'avatar_rating' );
     179                        if ( ! empty( $rating ) ) {
     180                                $avatar_url .= "&amp;r={$rating}";
     181                        }
     182                }
     183
     184                $expected_html = '<img src="' . $avatar_url . '" id="' . $this->params['css_id'] . '" class="' . $this->params['class'] . ' ' . $this->params['object'] . '-' . $this->params['item_id'] . '-avatar avatar-' . $this->params['width'] . ' photo" width="' . $this->params['width'] . '" height="' . $this->params['height'] . '" alt="' . $this->params['alt'] . '" title="' . $this->params['title'] . '" />';
     185
     186                $this->assertEquals( $html, $expected_html );
     187        }
    113188}