Changeset 39565

Show
Ignore:
Timestamp:
04/10/08 11:34:50 (3 months ago)
Author:
peterwsterling
Message:

avatars in posts

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • add-local-avatar/trunk/avatars.php

    r39491 r39565  
    55      Description:      A plugin to manage public and private avatars. 
    66      Author:                 Peter Sterling 
    7       Version:          2.1 
     7      Version:          3.0 
    88      Changes:          0.1 - Initial version. 
    99                              1.0 - Added pagination of users list. 
    1010                              2.0 - Added pagination of the commentors list too. 
    1111                              2.1 - Added example formatting information. 
     12                              3.0 - Added ability to place avatars in written post content (plus other tweaks). 
    1213      Author URI:       http://www.sterling-adventures.co.uk/ 
    1314*/ 
     
    2021            'size' => '30', 
    2122            'snapshots' => 'off', 
     23            'in_posts' => 'on', 
    2224            'default' => '' 
    2325      ); 
     
    2830// Main template tag - outputs the avatar (returns false if avatars are switched off)... 
    2931if(!function_exists('get_avatar')) : 
    30 function get_avatar($id_or_email, $size = '', $default = ''
     32function get_avatar($id_or_email, $size = '', $default = '', $post = false
    3133{ 
    3234      global $avatar_options; 
     
    8385      } 
    8486 
     87      // What class to apply to avatar images? 
     88      $class = ($post ? 'post_avatar no-rate' : 'avatar'); 
     89 
    8590      // Try to use local avatar. 
    8691      if($id) { 
    8792            $local = get_usermeta($id, 'avatar'); 
    8893            if(!empty($local)) { 
    89                   $avatar = "<img alt='' src='{$local}' class='avatar avatar-{$size}' height='{$size}' width='{$size}' />"; 
     94                  $avatar = "<img alt='' src='{$local}' class='{$class} avatar-{$size}' height='{$size}' width='{$size}' />"; 
    9095            } 
    9196      } 
     
    102107                  if(!empty($rating)) $out .= "&amp;r={$rating}"; 
    103108       
    104                   $avatar = "<img alt='' src='{$out}' class='avatar avatar-{$size}' height='{$size}' width='{$size}' />"; 
     109                  $avatar = "<img alt='' src='{$out}' class='{$class} avatar-{$size}' height='{$size}' width='{$size}' />"; 
    105110            } else { 
    106                   $avatar = "<img alt='' src='{$default}' class='avatar avatar-{$size} avatar-default' height='{$size}' width='{$size}'>"; 
     111                  $avatar = "<img alt='' src='{$default}' class='{$class} avatar-{$size} avatar-default' height='{$size}' width='{$size}'>"; 
    107112            } 
    108113      } 
     
    138143                  'size' => $_POST['size'], 
    139144                  'snapshots' => $_POST['snapshots'], 
     145                  'in_posts' => $_POST['in_posts'], 
    140146                  'default' => $_POST['default'] 
    141147            ); 
    142148            update_option('plugin_avatars', $options_update); 
     149            update_option('show_avatars', $_POST['show_avatars']); 
     150            update_option('avatar_rating', $_POST['avatar_rating']); 
    143151            $msg = 'Options saved.'; 
    144152      } 
     
    288296                  <table class='form-table'> 
    289297                        <tr> 
     298                              <td><label for="show_avatars">Show avatars:</label><br /><small>Repeated from Settings &raquo; Discussion</small></td> 
     299                              <td><input type="checkbox" name="show_avatars" <?php echo (get_option('show_avatars') ? 'checked' : ''); ?> /></td> 
     300                        </tr> 
     301                        <tr> 
     302                              <td>Avatar rating:<br /><small>Repeated from Settings &raquo; Discussion</small></td> 
     303                              <td> 
     304                                    <label><input type='radio' name='avatar_rating' value='G'  <?php echo (get_option('avatar_rating') == 'G'  ? 'checked="checked"' : ''); ?>> G  &#8212; Suitable for all audiences</label><br /> 
     305                                    <label><input type='radio' name='avatar_rating' value='PG' <?php echo (get_option('avatar_rating') == 'PG' ? 'checked="checked"' : ''); ?>> PG &#8212; Possibly offensive, usually for audiences 13 and above</label><br /> 
     306                                    <label><input type='radio' name='avatar_rating' value='R'  <?php echo (get_option('avatar_rating') == 'R'  ? 'checked="checked"' : ''); ?>> R  &#8212; Intended for adult audiences above 17</label><br /> 
     307                                    <label><input type='radio' name='avatar_rating' value='X'  <?php echo (get_option('avatar_rating') == 'X'  ? 'checked="checked"' : ''); ?>> X  &#8212; Even more mature than above</label><br /> 
     308                              </td> 
     309                        </tr> 
     310                        <tr> 
    290311                              <td><label for='size'>Size:</label></td> 
    291312                              <td> 
     
    305326                        <tr> 
    306327                              <td><label for="snapshots">Use Snapshots:</label></td> 
    307                               <td><input type="checkbox" name="snapshots" <?php echo $avatar_options['snapshots'] == 'on' ? 'checked' : ''; ?> /></td> 
     328                              <td><input type="checkbox" name="snapshots" <?php echo $avatar_options['snapshots'] == 'on' ? 'checked' : ''; ?> /> <small>if you have enabled <a href="http://www.snap.com">snapshots</a>, clearing this will disable them for avatar links</small></td> 
     329                        </tr> 
     330                        <tr> 
     331                              <td><label for="in_posts">Replace <code>&lt;!-- avatar <b>e-mail</b> --&gt;</code> with avatar in posts:</label></td> 
     332                              <td><input type="checkbox" name="in_posts" <?php echo $avatar_options['in_posts'] == 'on' ? 'checked' : ''; ?> /></td> 
    308333                        </tr> 
    309334                  </table> 
     
    316341            <p>The function takes the following parameters:<br /> 
    317342            <code><ol> 
    318                   <li>Identifier; NULL (the currently logged in user), user ID (a blog user), or e-mail address (for comments etc.).</li> 
     343                  <li>Identifier; <code>NULL</code> (the currently logged in user), user ID (a blog user), e-mail address, or a comment object from WordPress comment loop (for comments).</li> 
    319344                  <li>Size (pixels); defaulted to value set above.</li> 
    320345                  <li>Default image if no public (global) or private (local) avatar found; defaulted to value set above.</li> 
    321346            </ol></code></p> 
    322             <p>Apply format to the avatars with something like the following in in you <code>style.css</code> theme file:<br /> 
    323             <ul><code>.avatar { float:left; padding: 2px; margin: 0; border: 1px solid #ddd; background: white; }</code></ul></p> 
     347            <p>Apply format to the avatars with something like the following in your <code>style.css</code> theme file:<br /><ul> 
     348                  <li>For comment avatars, <code>.avatar { float: left; padding: 2px; margin: 0; border: 1px solid #ddd; background: white; }</code></li> 
     349                  <li>For avatars in post content, <code>.post_avatar { padding: 2px; margin: 0; border: 1px solid #ddd; background: white; }</code></li> 
     350            </ul></p> 
    324351            <p>Examples for your theme's template files:<br /> 
    325352            <ul> 
     
    337364 
    338365 
     366// Replace <!-- avatar e-mail --> in post content with an avatar. 
     367function generate_avatar_in_posts($content) 
     368{ 
     369      global $avatar_options; 
     370 
     371      // Is there content to work with? 
     372      if(!empty($content)) { 
     373            $matches = array(); 
     374            $replacement = array(); 
     375            $counter = 0; 
     376 
     377            // Look for all instances of <!-- avatar ??? --> in the content... 
     378            preg_match_all("/<!-- avatar ([^>]+) -->/", $content, $matches); 
     379 
     380            // For each instance, let's try to parse it... 
     381            foreach($matches['1'] as $email) { 
     382                  // Check if we should replace with an avatar or with 'nothing' (to protect email addresses from prying eyes/robots. 
     383                  if(!get_option('show_avatars') || $avatar_options['in_posts'] != 'on') $replacement[$counter] = ''; 
     384                  else $replacement[$counter] = get_avatar($email, $avatar_options['size'], $avatar_options['default'], true); 
     385                  $counter++; 
     386            } 
     387 
     388            // Replace... 
     389            for($i = 0; $i <= $counter; $i++) { 
     390                  $content = str_replace($matches[0][$i], $replacement[$i], $content); 
     391            } 
     392      } 
     393 
     394      return $content; 
     395} 
     396 
     397 
    339398// Hooks... 
    340 add_action ('admin_menu', 'avatar_menu'); 
    341  
     399add_action('admin_menu', 'avatar_menu'); 
     400add_filter('the_content', 'generate_avatar_in_posts'); 
    342401?>