Plugin Directory

Changeset 1127479


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

Version 1.0.2:

  • 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:
buddypress-first-letter-avatar
Files:
153 added
3 edited

Legend:

Unmodified
Added
Removed
  • buddypress-first-letter-avatar/trunk/buddypress-first-letter-avatar-config.php

    r1108538 r1127479  
    8484        add_settings_field(
    8585            'bpfla_use_gravatar',
    86             'Use Gravatar<br/>Default: check',
     86            'Use Gravatars<br/>Default: check',
    8787            array($this, 'bpfla_use_gravatar_render'),
     88            'bpfla_pluginPage',
     89            'bpfla_pluginPage_section'
     90        );
     91
     92
     93        add_settings_field(
     94            'bpfla_use_js',
     95            'Use JavaScript for Gravatars<br/>Default: uncheck',
     96            array($this, 'bpfla_use_js_render'),
    8897            'bpfla_pluginPage',
    8998            'bpfla_pluginPage_section'
     
    145154
    146155        ?>
    147         <input type='checkbox' name='bpfla_settings[bpfla_use_profile_avatar]' <?php checked( $this->bpfla_options['bpfla_use_profile_avatar'], 1 ); ?> value='1' />
     156        <input type='checkbox' name='bpfla_settings[bpfla_use_profile_avatar]' <?php checked($this->bpfla_options['bpfla_use_profile_avatar'], 1); ?> value='1' />
    148157    <?php
    149158
     
    155164
    156165        ?>
    157         <input type='checkbox' name='bpfla_settings[bpfla_use_gravatar]' <?php checked( $this->bpfla_options['bpfla_use_gravatar'], 1 ); ?> value='1' />
     166        <input type='checkbox' name='bpfla_settings[bpfla_use_gravatar]' <?php checked($this->bpfla_options['bpfla_use_gravatar'], 1); ?> value='1' />
     167    <?php
     168
     169    }
     170
     171
     172
     173    public function bpfla_use_js_render(){
     174
     175        ?>
     176        <input type='checkbox' name='bpfla_settings[bpfla_use_js]' <?php checked($this->bpfla_options['bpfla_use_js'], 1); ?> value='1' />
    158177    <?php
    159178
     
    165184
    166185        ?>
    167         <input type='checkbox' name='bpfla_settings[bpfla_round_avatars]' <?php checked( $this->bpfla_options['bpfla_round_avatars'], 1 ); ?> value='1' />
     186        <input type='checkbox' name='bpfla_settings[bpfla_round_avatars]' <?php checked($this->bpfla_options['bpfla_round_avatars'], 1); ?> value='1' />
    168187    <?php
    169188
     
    221240            </p>
    222241            <p>
     242                <strong>Use JavaScript for Gravatars</strong><br />
     243                <span>Works only when option Use Gravatar is active</span><br />
     244                <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).
     245            </p>
     246            <p>
    223247                <strong>Round avatars</strong><br />
    224248                <span style="text-decoration: underline">Check</span>: use rounded avatars; <span style="text-decoration: underline">Uncheck</span>: use standard avatars.
    225249            </p>
    226             <p>In case of any problems, use the default values.</p>
     250            <p>In case of any problems, use default values.</p>
    227251
    228252            <hr />
  • buddypress-first-letter-avatar/trunk/buddypress-first-letter-avatar.php

    r1108538 r1127479  
    55 * Contributors: DanielAGW
    66 * Description: Set custom avatars for BuddyPress users. The avatar will be a first (or any other) letter of the users's name.
    7  * Version: 1.0.1
     7 * Version: 1.0.2
    88 * Author: Daniel Wroblewski
    99 * Author URI: https://github.com/DanielAGW
     
    2727    const BPFLA_USE_PROFILE_AVATAR = TRUE;  // TRUE: if user has his profile avatar, use it; FALSE: use custom avatars or Gravatars
    2828    const BPFLA_USE_GRAVATAR = TRUE;  // TRUE: if user has Gravatar, use it; FALSE: use custom avatars or user's profile avatar
     29    const BPFLA_USE_JS = FALSE;  // TRUE: use JS to replace avatars to Gravatar; FALSE: generate avatars and gravatars here in PHP
    2930    const BPFLA_AVATAR_SET = 'default'; // directory where avatars are stored
    3031    const BPFLA_LETTER_INDEX = 0;  // 0: first letter; 1: second letter; -1: last letter, etc.
     
    3637    private $use_profile_avatar = self::BPFLA_USE_PROFILE_AVATAR;
    3738    private $use_gravatar = self::BPFLA_USE_GRAVATAR;
     39    private $use_js = self::BPFLA_USE_JS;
    3840    private $avatar_set = self::BPFLA_AVATAR_SET;
    3941    private $letter_index = self::BPFLA_LETTER_INDEX;
     
    4951        add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'bpfla_add_settings_link'));
    5052
    51         // add stylesheets/scripts:
     53        // add stylesheets/scripts for front-end and admin:
    5254        add_action('wp_enqueue_scripts', array($this, 'bpfla_add_scripts'));
     55        add_action('admin_enqueue_scripts', array($this, 'bpfla_add_scripts'));
     56
     57        // add Ajax action for asynchronous Gravatar verification:
     58        add_action('wp_ajax_gravatar_verify', array($this, 'ajax_gravatar_exists'));
     59        add_action('wp_ajax_nopriv_gravatar_verify', array($this, 'ajax_gravatar_exists'));
    5360
    5461        // add filter to get_avatar:
     
    5764        // add filter to bp_core_fetch_avatar:
    5865        add_filter('bp_core_fetch_avatar', array($this, 'set_buddypress_avatar'), 10, 1);
     66
     67        // when in admin, make sure first letter avatars are not displayed on discussion settings page:
     68        if (is_admin()){
     69            global $pagenow;
     70            if ($pagenow == 'options-discussion.php'){
     71                remove_filter('get_avatar', array($this, 'set_comment_avatar'));
     72            }
     73        }
    5974
    6075        // get plugin configuration from database:
     
    6580                'bpfla_use_profile_avatar' => self::BPFLA_USE_PROFILE_AVATAR,
    6681                'bpfla_use_gravatar' => self::BPFLA_USE_GRAVATAR,
     82                'bpfla_use_js' => self::BPFLA_USE_JS,
    6783                'bpfla_avatar_set' => self::BPFLA_AVATAR_SET,
    6884                'bpfla_letter_index' => self::BPFLA_LETTER_INDEX,
     
    7389            add_option('bpfla_settings', $settings);
    7490        } else {
    75             // there are records in DB for our plugin, let's assign them to our variables:
     91            // there are records in DB for our plugin, let's check if some of them are not empty:
     92            $change_values = FALSE; // do not update settings by default...
     93            if ($options['bpfla_avatar_set'] === ''){
     94                $options['bpfla_avatar_set'] = self::BPFLA_AVATAR_SET;
     95                $change_values = TRUE;
     96            }
     97            if ($options['bpfla_letter_index'] === ''){
     98                $options['bpfla_letter_index'] = self::BPFLA_LETTER_INDEX;
     99                $change_values = TRUE;
     100            }
     101            if ($options['bpfla_file_format'] === ''){
     102                $options['bpfla_file_format'] = self::BPFLA_IMAGES_FORMAT;
     103                $change_values = TRUE;
     104            }
     105            if ($options['bpfla_unknown_image'] === ''){
     106                $options['bpfla_unknown_image'] = self::BPFLA_IMAGE_UNKNOWN;
     107                $change_values = TRUE;
     108            }
     109            if ($change_values === TRUE){
     110                $settings['bpfla_use_profile_avatar'] = $options['bpfla_use_profile_avatar'];
     111                $settings['bpfla_use_gravatar'] = $options['bpfla_use_gravatar'];
     112                $settings['bpfla_use_js'] = $options['bpfla_use_js'];
     113                $settings['bpfla_avatar_set'] = $options['bpfla_avatar_set'];
     114                $settings['bpfla_letter_index'] = $options['bpfla_letter_index'];
     115                $settings['bpfla_file_format'] = $options['bpfla_file_format'];
     116                $settings['bpfla_round_avatars'] = $options['bpfla_round_avatars'];
     117                $settings['bpfla_unknown_image'] = $options['bpfla_unknown_image'];
     118                update_option('bpfla_settings', $settings);
     119            }
     120            // and then assign them to our class properties
    76121            $this->use_profile_avatar = $options['bpfla_use_profile_avatar'];
    77122            $this->use_gravatar = $options['bpfla_use_gravatar'];
     123            $this->use_js = $options['bpfla_use_js'];
    78124            $this->avatar_set = $options['bpfla_avatar_set'];
    79125            $this->letter_index = $options['bpfla_letter_index'];
     
    101147
    102148        // add main CSS file:
    103         wp_enqueue_style('prefix-style', plugins_url('css/style.css', __FILE__) );
     149        wp_enqueue_style('prefix-style', plugins_url('css/style.css', __FILE__));
     150
     151        // add main JS file, only when JS is used:
     152        if ($this->use_js == TRUE){
     153            $js_variables = array(
     154                'img_data_attribute' => 'data-bpfla-gravatar',
     155                'ajaxurl' => admin_url('admin-ajax.php')
     156            );
     157            wp_enqueue_script('bpfla-script-handle', plugins_url('js/script.js', __FILE__), array('jquery'));
     158            wp_localize_script('bpfla-script-handle', 'bpfla_vars_data', $js_variables);
     159        }
     160
     161    }
     162
     163
     164
     165    public function ajax_gravatar_exists(){
     166
     167        $gravatar_uri = $_POST['gravatar_uri'];
     168        $gravatar_exists = $this->gravatar_exists_uri($gravatar_uri);
     169
     170        if ($gravatar_exists == TRUE){
     171            echo '1';
     172        } else {
     173            echo '0';
     174        }
     175
     176        exit;
    104177
    105178    }
     
    166239
    167240        // first check whether Gravatar should be used at all:
    168         if ($this->use_gravatar == TRUE){
     241        if ($this->use_gravatar == TRUE && $this->use_js == FALSE){
    169242            // gravatar used as default option, now check whether user's gravatar is set:
    170243            if ($this->gravatar_exists($email)){
     
    175248                $avatar_output = $this->choose_custom_avatar($name, $size, $alt);
    176249            }
    177         } else {
    178             // gravatar is not used as default option, only custom avatars will be used; proceed to choose custom avatar:
     250        } else if ($this->use_gravatar == TRUE && $this->use_js == TRUE){
     251            // gravatar with JS is used as default option, only custom avatars will be used; proceed to choose custom avatar:
     252            $avatar_output = $this->choose_custom_avatar($name, $size, $alt, $email);
     253        } else {
     254            // gravatar is not used:
    179255            $avatar_output = $this->choose_custom_avatar($name, $size, $alt);
    180256        }
     
    199275            } else if (stripos($alt, 'Profile photo of ') === 0){ // or profile photo of...
    200276                $name = str_replace('Profile photo of ', '', $alt);
    201             } else { // if there is some problem - just assign alt to name
    202                 $name = $alt;
     277            } else {
     278                if (is_user_logged_in()){
     279                    $user = get_user_by('id', get_current_user_id());
     280                    $name = $user->data->display_name;
     281                } else {
     282                    $name = '';
     283                }
     284
    203285            }
    204286        }
     
    209291        }
    210292
    211         // if there is no gravatar URL it means that user has set his own profila avatar,
     293        // if there is no gravatar URL it means that user has set his own profile avatar,
    212294        // so we're gonna see if we should be using it;
    213295        // if we should, just return the input data and leave the avatar as it was:
     
    219301
    220302        // check whether Gravatar should be used at all:
    221         if ($this->use_gravatar == TRUE){
     303        if ($this->use_gravatar == TRUE && $this->use_js == FALSE){
    222304            // gravatar used as default option, now check whether user's gravatar is set:
    223305            if ($this->gravatar_exists_uri($original_image)){
     
    228310                $avatar_output = $this->choose_custom_avatar($name, $size, $alt);
    229311            }
     312        } else if ($this->use_gravatar == TRUE && $this->use_js == TRUE){
     313            $avatar_output = $this->choose_custom_avatar($name, $size, $alt, $original_image);
    230314        } else {
    231315            // gravatar is not used as default option, only custom avatars will be used; proceed to choose custom avatar:
     
    239323
    240324
    241     private function output_gravatar_img($comment_email, $size, $alt = ''){
     325    private function generate_gravatar_uri($email, $size){
    242326
    243327        // email to gravatar url:
    244328        $avatar_uri = self::BPFLA_GRAVATAR_URL;
    245         $avatar_uri .= md5(strtolower(trim($comment_email)));
     329        $avatar_uri .= md5(strtolower(trim($email)));
    246330        $avatar_uri .= "?s={$size}&d=mm&r=g";
    247331
     332        return $avatar_uri;
     333
     334    }
     335
     336
     337
     338    private function output_gravatar_img($comment_email, $size, $alt = ''){
     339
    248340        // output gravatar:
     341        $avatar_uri = $this->generate_gravatar_uri($comment_email, $size);
    249342        return $this->output_img($avatar_uri, $size, $alt);
    250343
     
    253346
    254347
    255     private function output_img($avatar_uri, $size, $alt = ''){
     348    private function output_img($avatar_uri, $size, $alt = '', $gravatar_uri = ''){
    256349
    257350        // prepare extra classes for <img> tag depending on plugin settings:
    258351        $extra_img_class = '';
     352        $extra_img_tags = '';
     353        if (!empty($gravatar_uri)){
     354            $extra_img_tags .= "data-bpfla-gravatar='{$gravatar_uri}'";
     355        }
    259356        if ($this->round_avatars == TRUE){
    260357            $extra_img_class .= 'round-avatars';
    261358        }
    262359
    263         $output_data = "<img src='{$avatar_uri}' class='avatar avatar-{$size} photo bpfla {$extra_img_class}' width='{$size}' height='{$size}' alt='{$alt}' />";
     360        $output_data = "<img src='{$avatar_uri}' {$extra_img_tags} class='avatar avatar-{$size} photo bpfla {$extra_img_class}' width='{$size}' height='{$size}' alt='{$alt}' />";
    264361
    265362        // return the complete <img> tag:
     
    270367
    271368
    272     private function choose_custom_avatar($username, $size, $alt = ''){
     369    private function choose_custom_avatar($username, $size, $alt = '', $email = ''){
    273370
    274371        // get picture filename (and lowercase it) from commenter name:
    275         //var_dump($username);
    276         $file_name = substr($username, $this->letter_index, 1); // get one letter counting from letter_index
    277         $file_name = strtolower($file_name); // lowercase it...
    278         // if, for some reason, the result is empty, set file_name to default unknown image:
    279         if (empty($file_name)){
     372        if (empty($username)){  // if, for some reason, the result is empty, set file_name to default unknown image
    280373            $file_name = $this->image_unknown;
     374        } else {
     375            $file_name = substr($username, $this->letter_index, 1); // get one letter counting from letter_index
     376            $file_name = strtolower($file_name); // lowercase it...
    281377        }
    282378
     
    306402            . $this->images_format;
    307403
     404        $gravatar_uri = '';
     405
     406        if (!empty($email)){
     407            if (filter_var($email, FILTER_VALIDATE_EMAIL)){
     408                $gravatar_uri .= $this->generate_gravatar_uri($email, $size);
     409            } else {
     410                $gravatar_uri .= $email;
     411            }
     412        }
     413
    308414        // output the final HTML img code:
    309         return $this->output_img($avatar_uri, $size, $alt);
     415        return $this->output_img($avatar_uri, $size, $alt, $gravatar_uri);
    310416
    311417    }
  • buddypress-first-letter-avatar/trunk/readme.txt

    r1108538 r1127479  
    11=== BuddyPress First Letter Avatar ===
    22Plugin Name: BuddyPress First Letter Avatar
    3 Version: 1.0.1
     3Version: 1.0.2
    44Plugin URI: https://github.com/DanielAGW/buddypress-first-letter-avatar
    55Contributors: DanielAGW
     
    1919BuddyPress First Letter Avatar **sets custom avatars for users without profile picture or Gravatar**. The avatar will be a first letter of the users's name, just like in [Discourse](http://www.discourse.org/). You can also configure plugin to use any other letter to set custom avatar.
    2020
    21 BuddyPress First Letter Avatar is based on my other plugin - [WP First Letter Avatar](https://wordpress.org/plugins/wp-first-letter-avatar/). BuddyPress First Letter avatar is basically the same thing, but programmed to work with BuddyPress.
     21BuddyPress First Letter Avatar is based on my other plugin - [WP First Letter Avatar](https://wordpress.org/plugins/wp-first-letter-avatar/). BuddyPress First Letter Avatar is basically the same thing, but programmed to work with BuddyPress.
    2222
    2323BuddyPress First Letter Avatar includes a set of **beautiful, colorful letter avatars** in many sizes. Optimal size will be chosen by the plugin in order to display high quality avatar and not download, for example, big 512px avatars when only 48px is needed... **PSD template** for avatar is also included.
     
    3232
    3333You can [fork the plugin on GitHub](https://github.com/DanielAGW/buddypress-first-letter-avatar).
     34
     35= Requirements =
     36BuddyPress First Letter Avatar requires at least PHP 5.4.0. It **does not work properly** on PHP 5.3.x and earlier.
    3437
    3538== Installation ==
     
    7679== Changelog ==
    7780
     81= 1.0.2 =
     82* 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
     83* Added asynchronous Gravatar loading for faster page rendering (needs to be activated in plugin Settings)
     84* Added auto-check to see if one or more options in plugin Settings are not empty
     85* Fixed standard avatars replacement on Discussion page in Settings
     86* Couple of minor fixes
     87
    7888= 1.0.1 =
    7989* Fixed avatar presentation in WP-Admin
Note: See TracChangeset for help on using the changeset viewer.