Plugin Directory

Changeset 1100834


Ignore:
Timestamp:
02/27/2015 12:21:35 AM (11 years ago)
Author:
DanielAGW
Message:

Version 1.2.3:

  • Improved avatar appearance on top admin/user bar
  • Added full compatibility with bbPress plugin
Location:
wp-first-letter-avatar
Files:
149 added
2 edited

Legend:

Unmodified
Added
Removed
  • wp-first-letter-avatar/trunk/readme.txt

    r1099472 r1100834  
    11=== WP First Letter Avatar ===
    22Plugin Name: WP First Letter Avatar
    3 Version: 1.2.2
     3Version: 1.2.3
    44Plugin URI: https://github.com/DanielAGW/wp-first-letter-avatar
    55Contributors: DanielAGW
     
    7070== Changelog ==
    7171
     72= 1.2.3 =
     73* Improved avatar appearance on top admin/user bar
     74* Added full compatibility with bbPress plugin
     75
    7276= 1.2.2 =
    7377* Fixed conflicts with some comment systems (such as wpDiscuz)
     
    8892== Upgrade Notice ==
    8993
     94= 1.2.3 =
     95This version introduces full compatibility with bbPress and fixes some issues with avatars on user/admin bar. Update recommended.
     96
    9097= 1.2.2 =
    9198This version fixes conflicts with some comment systems (such as wpDiscuz) and slightly improves plugin performance. Update recommended.
  • wp-first-letter-avatar/trunk/wp-first-letter-avatar.php

    r1099472 r1100834  
    55 * Contributors: DanielAGW
    66 * Description: Set custom avatars for users with no Gravatar. The avatar will be a first (or any other) letter of the users's name, just like in Discourse.
    7  * Version: 1.2.2
     7 * Version: 1.2.3
    88 * Author: Daniel Wroblewski
    99 * Author URI: https://github.com/DanielAGW
     
    5252        // add filter to get_avatar but only when not in admin panel:
    5353        if (!is_admin()){
    54             add_filter('get_avatar', array($this, 'set_avatar'), 10, 4);
    55         }
     54            add_filter('get_avatar', array($this, 'set_comment_avatar'), 10, 5);
     55        }
     56        // use different function for top user/admin bar and remove it after it's been rendered:
     57        add_action('admin_bar_menu', function(){
     58            add_filter('get_avatar', array($this, 'set_userbar_avatar'), 10, 5);
     59        },0);
     60        add_action('wp_after_admin_bar_render', function(){
     61            remove_filter('get_avatar', array($this, 'set_userbar_avatar'), 10);
     62        });
     63
    5664
    5765        // get plugin configuration from database:
     
    106114
    107115
    108     public function set_avatar($avatar, $id_or_email, $size = '96', $default, $alt = ''){ // only size and alt arguments are used
    109 
    110         // get comment information:
    111         $comment_author = get_comment_author();
    112         $comment_email = get_comment_author_email();
    113 
    114         // if, for some reason, there is no comment author, use email instead:
    115         if (empty($comment_author)){
    116             $comment_author = $comment_email;
     116    private function set_avatar($name, $email, $size, $alt){
     117
     118        if (empty($name)){ // if, for some reason, there is no name, use email instead
     119            $name = $email;
     120        } else if (empty($email)){ // and if no email, use user/guest name
     121            $email = $name;
    117122        }
    118123
     
    120125        if ($this->use_gravatar == TRUE){
    121126            // gravatar used as default option, now check whether user's gravatar is set:
    122             if ($this->gravatar_exists($comment_email)){
     127            if ($this->gravatar_exists($email)){
    123128                // gravatar is set, output the gravatar img
    124                 $avatar_output = $this->output_gravatar_img($comment_email, $size, $alt);
     129                $avatar_output = $this->output_gravatar_img($email, $size, $alt);
    125130            } else {
    126131                // gravatar is not set, proceed to choose custom avatar:
    127                 $avatar_output = $this->choose_custom_avatar($comment_author, $size, $alt);
     132                $avatar_output = $this->choose_custom_avatar($name, $size, $alt);
    128133            }
    129134        } else {
    130135            // gravatar is not used as default option, only custom avatars will be used; proceed to choose custom avatar:
    131             $avatar_output = $this->choose_custom_avatar($comment_author, $size, $alt);
    132         }
     136            $avatar_output = $this->choose_custom_avatar($name, $size, $alt);
     137        }
     138
     139        return $avatar_output;
     140
     141    }
     142
     143
     144
     145    public function set_comment_avatar($avatar, $id_or_email, $size = '96', $default, $alt = ''){
     146
     147        // create two main variables:
     148        $name = '';
     149        $email = '';
     150
     151        // check if it's a comment:
     152        $comment_id = get_comment_ID();
     153
     154        if ($comment_id === NULL){ // if it's not a regular comment, use $id_or_email to get more data
     155
     156            if (is_numeric($id_or_email)){ // if id_or_email represents user id, get user by id
     157                $id = (int) $id_or_email;
     158                $user = get_user_by('id', $id);
     159            } else if (is_object($id_or_email)){ // if id_or_email represents an object
     160                if (!empty($id_or_email->user_id)){  // if there we can get user_id from the object, get user by id
     161                    $id = (int) $id_or_email->user_id;
     162                    $user = get_user_by('id', $id);
     163                }
     164            } else { // id_or_email is not user_id and is not an object, then it must be an email
     165                $user = get_user_by('email', $id_or_email);
     166            }
     167
     168            if ($user && is_object($user)){ // if commenter is a registered user...
     169                $name = $user->data->display_name;
     170                $email = $user->data->user_email;
     171            } else { // if commenter is not a registered user, we have to try various fallbacks
     172                $post_id = get_the_ID();
     173                if ($post_id !== NULL){ // if this actually is a post...
     174                    $post_data = array('name' => '', 'email' => '');
     175                    // first we try for bbPress:
     176                    $post_data['name'] = get_post_meta($post_id, '_bbp_anonymous_name', TRUE);
     177                    $post_data['email'] = get_post_meta($post_id, '_bbp_anonymous_email', TRUE);
     178                    if (!empty($post_data)){ // we have some post data...
     179                        $name = $post_data['name'];
     180                        $email = $post_data['email'];
     181                    }
     182                } else { // nothing else to do, assign email from id_or_email to email and later use it as name
     183                    if (!empty($id_or_email)){
     184                        $email = $id_or_email;
     185                    }
     186                }
     187            }
     188
     189        } else { // if it's a standard comment, use basic comment functions to retrive info
     190
     191            $name = get_comment_author();
     192            $email = get_comment_author_email();
     193
     194        }
     195
     196        $avatar_output = $this->set_avatar($name, $email, $size, $alt);
     197
     198        return $avatar_output;
     199
     200    }
     201
     202
     203
     204    public function set_userbar_avatar($avatar, $id_or_email, $size = '96', $default, $alt = ''){ // only size and alt arguments are used
     205
     206        // get user information:
     207        global $current_user;
     208        get_currentuserinfo();
     209        $name = $current_user->display_name;
     210        $email = $current_user->user_email;
     211
     212        $avatar_output = $this->set_avatar($name, $email, $size, $alt);
    133213
    134214        return $avatar_output;
Note: See TracChangeset for help on using the changeset viewer.