Plugin Directory

Changeset 1127480


Ignore:
Timestamp:
04/04/2015 02:58:31 AM (11 years ago)
Author:
DanielAGW
Message:

Version 1.2.6:

  • PHP 5.4.x or later REQUIRED
  • Added asynchronous Gravatar loading for faster page rendering (needs to be activated in plugin Settings)
  • Added auto-check to see if one or more options in plugin Settings are not empty
  • Fixed standard avatars replacement on Discussion page in Settings
  • Couple of minor fixes
Location:
wp-first-letter-avatar
Files:
153 added
3 edited

Legend:

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

    r1108525 r1127480  
    11=== WP First Letter Avatar ===
    22Plugin Name: WP First Letter Avatar
    3 Version: 1.2.5
     3Version: 1.2.6
    44Plugin URI: https://github.com/DanielAGW/wp-first-letter-avatar
    55Contributors: DanielAGW
     
    3232
    3333= Compatibility with other plugins =
    34 WP First Letter Avatar is fully compatible with [bbPress](https://bbpress.org/). For [BuddyPress](https://buddypress.org/) compatibility please use my other plugin - [BuddyPress First Letter Avatar](https://wordpress.org/plugins/buddypress-first-letter-avatar/).
     34WP First Letter Avatar is fully compatible with [bbPress](https://bbpress.org/) and [wpDiscuz](http://www.gvectors.com/wpdiscuz/). For [BuddyPress](https://buddypress.org/) compatibility please use my other plugin - [BuddyPress First Letter Avatar](https://wordpress.org/plugins/buddypress-first-letter-avatar/).
     35
     36= Requirements =
     37WP First Letter Avatar requires at least PHP 5.4.0. It **does not work properly** on PHP 5.3.x and earlier.
    3538
    3639== Installation ==
     
    5558
    5659= Can I set custom avatars based on last (or any other) character in user's name? =
    57 
    5860Of course! This can be done in plugin settings.
    5961
    6062= I don't want to use Gravatar at all. Can I disable it? =
    6163Yes! By default, WP First Letter Avatar sets custom avatar only to users without Gravatar, but in plugin settings you can disable it and use custom avatar for everybody.
     64
     65= Is WP First Letter Avatar compatible with other plugins? =
     66WP First Letter Avatar has fully tested compatibilty with bbPress and wpDiscuz. For BuddyPress compatibility, please use my other plugin - [BuddyPress First Letter Avatar](https://wordpress.org/plugins/buddypress-first-letter-avatar/).
    6267
    6368= Can avatars be round, like in Google+? =
     
    7277
    7378== Changelog ==
     79
     80= 1.2.6 =
     81* PHP 5.4.x or later REQUIRED: PHP 5.3.x is no longer supported by PHP team, if you are still using it - update immediately
     82* Added asynchronous Gravatar loading for faster page rendering (needs to be activated in plugin Settings)
     83* Added auto-check to see if one or more options in plugin Settings are not empty
     84* Fixed standard avatars replacement on Discussion page in Settings
     85* Couple of minor fixes
    7486
    7587= 1.2.5 =
  • wp-first-letter-avatar/trunk/wp-first-letter-avatar-config.php

    r1106485 r1127480  
    1515    public function __construct(){
    1616
    17         add_action( 'admin_menu', array($this, 'wpfla_add_admin_menu' ));
    18         add_action( 'admin_init', array($this, 'wpfla_settings_init' ));
     17        add_action('admin_menu', array($this, 'wpfla_add_admin_menu'));
     18        add_action('admin_init', array($this, 'wpfla_settings_init'));
    1919
    2020    }
     
    8282
    8383        add_settings_field(
     84            'wpfla_use_js',
     85            'Use JavaScript for Gravatars<br/>Default: uncheck',
     86            array($this, 'wpfla_use_js_render'),
     87            'wpfla_pluginPage',
     88            'wpfla_pluginPage_section'
     89        );
     90
     91        add_settings_field(
    8492            'wpfla_round_avatars',
    8593            'Round avatars<br/>Default: uncheck',
     
    136144
    137145        ?>
    138         <input type='checkbox' name='wpfla_settings[wpfla_use_gravatar]' <?php checked( $this->wpfla_options['wpfla_use_gravatar'], 1 ); ?> value='1' />
     146        <input type='checkbox' name='wpfla_settings[wpfla_use_gravatar]' <?php checked($this->wpfla_options['wpfla_use_gravatar'], 1); ?> value='1' />
     147    <?php
     148
     149    }
     150
     151
     152
     153    public function wpfla_use_js_render(){
     154
     155        ?>
     156        <input type='checkbox' name='wpfla_settings[wpfla_use_js]' <?php checked($this->wpfla_options['wpfla_use_js'], 1); ?> value='1' />
    139157    <?php
    140158
     
    146164
    147165        ?>
    148         <input type='checkbox' name='wpfla_settings[wpfla_round_avatars]' <?php checked( $this->wpfla_options['wpfla_round_avatars'], 1 ); ?> value='1' />
     166        <input type='checkbox' name='wpfla_settings[wpfla_round_avatars]' <?php checked($this->wpfla_options['wpfla_round_avatars'], 1); ?> value='1' />
    149167    <?php
    150168
     
    198216            </p>
    199217            <p>
     218                <strong>Use JavaScript for Gravatars</strong><br />
     219                <span>Works only when option Use Gravatar is active</span><br />
     220                <span style="text-decoration: underline">Check</span>: use JavaScript to check for Gravatars (faster); <span style="text-decoration: underline">Uncheck</span>: use PHP to check for Gravatars (slower).
     221            </p>
     222            <p>
    200223                <strong>Round avatars</strong><br />
    201224                <span style="text-decoration: underline">Check</span>: use rounded avatars; <span style="text-decoration: underline">Uncheck</span>: use standard avatars.
    202225            </p>
    203             <p>In case of any problems, use the default values.</p>
     226            <p>In case of any problems, use default values.</p>
    204227
    205228            <hr />
  • wp-first-letter-avatar/trunk/wp-first-letter-avatar.php

    r1108525 r1127480  
    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.
    7  * Version: 1.2.5
     7 * Version: 1.2.6
    88 * Author: Daniel Wroblewski
    99 * Author URI: https://github.com/DanielAGW
     
    2626    // Default configuration (this is the default configuration only for the first plugin usage):
    2727    const WPFLA_USE_GRAVATAR = TRUE;  // TRUE: if user has Gravatar, use it; FALSE: use custom avatars even when gravatar is set
     28    const WPFLA_USE_JS = FALSE;  // TRUE: use JS to replace avatars to Gravatar; FALSE: generate avatars and gravatars here in PHP
    2829    const WPFLA_AVATAR_SET = 'default'; // directory where avatars are stored
    2930    const WPFLA_LETTER_INDEX = 0;  // 0: first letter; 1: second letter; -1: last letter, etc.
     
    3435    // variables duplicating const values (will be changed in constructor after reading config from DB):
    3536    private $use_gravatar = self::WPFLA_USE_GRAVATAR;
     37    private $use_js = self::WPFLA_USE_JS;
    3638    private $avatar_set = self::WPFLA_AVATAR_SET;
    3739    private $letter_index = self::WPFLA_LETTER_INDEX;
     
    4749        add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'wpfla_add_settings_link'));
    4850
    49         // add stylesheets/scripts:
     51        // add stylesheets/scripts for front-end and admin:
    5052        add_action('wp_enqueue_scripts', array($this, 'wpfla_add_scripts'));
     53        add_action('admin_enqueue_scripts', array($this, 'wpfla_add_scripts'));
     54
     55        // add Ajax action for asynchronous Gravatar verification:
     56        add_action('wp_ajax_gravatar_verify', array($this, 'ajax_gravatar_exists'));
     57        add_action('wp_ajax_nopriv_gravatar_verify', array($this, 'ajax_gravatar_exists'));
    5158
    5259        // add filter to get_avatar:
     
    5461
    5562        // add additional filter for userbar avatar, but only when not in admin:
    56         if (!is_admin()) {
    57             add_action('admin_bar_menu', array($this, 'add_set_userbar_avatar_filter'), 0);
    58         }
    59 
     63        if (!is_admin()){
     64            add_action('admin_bar_menu', function(){
     65                add_filter('get_avatar', array($this, 'set_userbar_avatar'), 10, 5);
     66            }, 0);
     67        } else { // when in admin, make sure first letter avatars are not displayed on discussion settings page
     68            global $pagenow;
     69            if ($pagenow == 'options-discussion.php'){
     70                remove_filter('get_avatar', array($this, 'set_comment_avatar'));
     71            }
     72        }
    6073
    6174        // get plugin configuration from database:
    6275        $options = get_option('wpfla_settings');
    6376        if (empty($options)){
    64 
    6577            // no records in DB, use default (const) values to save plugin config:
    6678            $settings = array(
    6779                'wpfla_use_gravatar' => self::WPFLA_USE_GRAVATAR,
     80                'wpfla_use_js' => self::WPFLA_USE_JS,
    6881                'wpfla_avatar_set' => self::WPFLA_AVATAR_SET,
    6982                'wpfla_letter_index' => self::WPFLA_LETTER_INDEX,
     
    7386            );
    7487            add_option('wpfla_settings', $settings);
    75 
    7688        } else {
    77 
    78             // there are records in DB for our plugin, let's assign them to our variables:
     89            // there are records in DB for our plugin, let's check if some of them are not empty:
     90            $change_values = FALSE; // do not update settings by default...
     91            if ($options['wpfla_avatar_set'] === ''){
     92                $options['wpfla_avatar_set'] = self::WPFLA_AVATAR_SET;
     93                $change_values = TRUE;
     94            }
     95            if ($options['wpfla_letter_index'] === ''){
     96                $options['wpfla_letter_index'] = self::WPFLA_LETTER_INDEX;
     97                $change_values = TRUE;
     98            }
     99            if ($options['wpfla_file_format'] === ''){
     100                $options['wpfla_file_format'] = self::WPFLA_IMAGES_FORMAT;
     101                $change_values = TRUE;
     102            }
     103            if ($options['wpfla_unknown_image'] === ''){
     104                $options['wpfla_unknown_image'] = self::WPFLA_IMAGE_UNKNOWN;
     105                $change_values = TRUE;
     106            }
     107            if ($change_values === TRUE){
     108                $settings['wpfla_use_gravatar'] = $options['wpfla_use_gravatar'];
     109                $settings['wpfla_use_js'] = $options['wpfla_use_js'];
     110                $settings['wpfla_avatar_set'] = $options['wpfla_avatar_set'];
     111                $settings['wpfla_letter_index'] = $options['wpfla_letter_index'];
     112                $settings['wpfla_file_format'] = $options['wpfla_file_format'];
     113                $settings['wpfla_round_avatars'] = $options['wpfla_round_avatars'];
     114                $settings['wpfla_unknown_image'] = $options['wpfla_unknown_image'];
     115                update_option('wpfla_settings', $settings);
     116            }
     117            // and then assign them to our class properties
    79118            $this->use_gravatar = $options['wpfla_use_gravatar'];
     119            $this->use_js = $options['wpfla_use_js'];
    80120            $this->avatar_set = $options['wpfla_avatar_set'];
    81121            $this->letter_index = $options['wpfla_letter_index'];
     
    83123            $this->round_avatars = $options['wpfla_round_avatars'];
    84124            $this->image_unknown = $options['wpfla_unknown_image'];
    85 
    86125        }
    87126
     
    104143
    105144        // add main CSS file:
    106         wp_enqueue_style('prefix-style', plugins_url('css/style.css', __FILE__) );
     145        wp_enqueue_style('prefix-style', plugins_url('css/style.css', __FILE__));
     146
     147        // add main JS file, only when JS is used:
     148        if ($this->use_js == TRUE){
     149            $js_variables = array(
     150                'img_data_attribute' => 'data-wpfla-gravatar',
     151                'ajaxurl' => admin_url('admin-ajax.php')
     152            );
     153            wp_enqueue_script('wpfla-script-handle', plugins_url('js/script.js', __FILE__), array('jquery'));
     154            wp_localize_script('wpfla-script-handle', 'wpfla_vars_data', $js_variables);
     155        }
     156
     157    }
     158
     159
     160
     161    public function ajax_gravatar_exists(){
     162
     163        $gravatar_uri = $_POST['gravatar_uri'];
     164        $gravatar_exists = $this->gravatar_exists_uri($gravatar_uri);
     165
     166        if ($gravatar_exists == TRUE){
     167            echo '1';
     168        } else {
     169            echo '0';
     170        }
     171
     172        exit;
    107173
    108174    }
     
    119185
    120186        // first check whether Gravatar should be used at all:
    121         if ($this->use_gravatar == TRUE){
     187        if ($this->use_gravatar == TRUE && $this->use_js == FALSE){
    122188            // gravatar used as default option, now check whether user's gravatar is set:
    123189            if ($this->gravatar_exists($email)){
     
    128194                $avatar_output = $this->choose_custom_avatar($name, $size, $alt);
    129195            }
     196        } else if ($this->use_gravatar == TRUE && $this->use_js == TRUE){
     197            // gravatar with JS is used as default option, only custom avatars will be used; proceed to choose custom avatar:
     198            $avatar_output = $this->choose_custom_avatar($name, $size, $alt, $email);
    130199        } else {
    131             // gravatar is not used as default option, only custom avatars will be used; proceed to choose custom avatar:
     200            // gravatar is not used:
    132201            $avatar_output = $this->choose_custom_avatar($name, $size, $alt);
    133202        }
     
    198267
    199268
    200     public function add_set_userbar_avatar_filter(){
    201 
    202         add_filter('get_avatar', array($this, 'set_userbar_avatar'), 10, 5);
    203 
    204     }
    205 
    206 
    207 
    208269    public function set_userbar_avatar($avatar, $id_or_email, $size = '96', $default, $alt = ''){ // only size and alt arguments are used
    209270
     
    222283
    223284
    224     private function output_img($avatar_uri, $size, $alt = ''){
     285    private function output_img($avatar_uri, $size, $alt = '', $gravatar_uri = ''){
    225286
    226287        // prepare extra classes for <img> tag depending on plugin settings:
    227288        $extra_img_class = '';
     289        $extra_img_tags = '';
     290        if (!empty($gravatar_uri)){
     291            $extra_img_tags .= "data-wpfla-gravatar='{$gravatar_uri}'";
     292        }
    228293        if ($this->round_avatars == TRUE){
    229294            $extra_img_class .= 'round-avatars';
    230295        }
    231296
    232         $output_data = "<img alt='{$alt}' src='{$avatar_uri}' class='avatar avatar-{$size} photo wpfla {$extra_img_class}' height='{$size}' width='{$size}' />";
     297        $output_data = "<img alt='{$alt}' src='{$avatar_uri}' {$extra_img_tags} class='avatar avatar-{$size} photo wpfla {$extra_img_class}' height='{$size}' width='{$size}' />";
    233298
    234299        // return the complete <img> tag:
     
    239304
    240305
    241     private function choose_custom_avatar($comment_author, $size, $alt = ''){
     306    private function choose_custom_avatar($comment_author, $size, $alt = '', $email = ''){
    242307
    243308        // get picture filename (and lowercase it) from commenter name:
     
    274339            . $this->images_format;
    275340
     341        $gravatar_uri = '';
     342
     343        if (!empty($email)){
     344            $gravatar_uri .= $this->generate_gravatar_uri($email, $size);
     345        }
     346
    276347        // output the final HTML img code:
    277         return $this->output_img($avatar_uri, $size, $alt);
    278 
    279     }
    280 
    281 
    282 
    283     private function output_gravatar_img($comment_email, $size, $alt = ''){
     348        return $this->output_img($avatar_uri, $size, $alt, $gravatar_uri);
     349
     350    }
     351
     352
     353
     354    private function generate_gravatar_uri($email, $size){
    284355
    285356        // email to gravatar url:
    286357        $avatar_uri = self::WPFLA_GRAVATAR_URL;
    287         $avatar_uri .= md5(strtolower(trim($comment_email)));
     358        $avatar_uri .= md5(strtolower(trim($email)));
    288359        $avatar_uri .= "?s={$size}&d=mm&r=g";
    289360
     361        return $avatar_uri;
     362
     363    }
     364
     365
     366
     367    private function output_gravatar_img($comment_email, $size, $alt = ''){
     368
    290369        // output gravatar:
     370        $avatar_uri = $this->generate_gravatar_uri($comment_email, $size);
    291371        return $this->output_img($avatar_uri, $size, $alt);
    292372
     
    319399    }
    320400
     401
     402
     403    private function gravatar_exists_uri($uri){
     404
     405        /*  Check if there is gravatar assigned to this gravatar url
     406            returns bool: true if gravatar is assigned, false if it is not
     407            function partially borrowed from http://codex.wordpress.org/Using_Gravatars - thanks! */
     408
     409
     410        // first check whether it is a gravatar URL; if not, return FALSE
     411        if (stripos($uri, 'gravatar.com/avatar') === FALSE){
     412            return FALSE;
     413        }
     414
     415        // strip all GET parameters:
     416        $uri = strtok($uri, '?');
     417        $uri .=  '?d=404';
     418        $response = wp_remote_head($uri); // response from gravatar server
     419
     420        if (is_wp_error($response)){ // caused error?
     421            $data = 'error';
     422        } else {
     423            $data = $response['response']['code']; // no error, assign response code to data
     424        }
     425
     426        if ($data == '200'){ // response code is 200, gravatar exists, return true
     427            return TRUE;
     428        } else { // response code is not 200, gravatar doesn't exist, return false
     429            return FALSE;
     430        }
     431
     432    }
     433
    321434}
    322435
Note: See TracChangeset for help on using the changeset viewer.