Advertisement
yashmistrey

com_avatar.php

Aug 15th, 2011
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.68 KB | None | 0 0
  1. <?php
  2. /*
  3.     Plugin Name:    Recent Comment Avatars
  4.     Plugin URI:     http://www.sterling-adventures.co.uk/blog/2009/01/01/comments-with-avatars/
  5.     Description:    Add avatars to your recent comments sidebar widget.
  6.     Author:         Peter Sterling
  7.     Version:        3.5
  8.     Changes:        1.0 - Initial version.
  9.                     2.0 - Added option for showing a comment excerpt, thanks to Angelo Milanetti for the idea.
  10.                     2.1 - Convert smilies.
  11.                     3.0 - Indicate country of posting (via IP address) with small flag icon.
  12.                     3.1 - Comment list flags; start or end option.
  13.                     3.2 - Don't allow flag options if no database table.
  14.                     3.3 - Fix printf of comment time.
  15.                     3.4 - Tidy up IP lookup.
  16.                     3.5 - Add option to exclude comments from scheduled posts.
  17.     Author URI:     http://www.sterling-adventures.co.uk/
  18. */
  19.  
  20.  
  21. function sa_get_comment_flag_from_ip($ip)
  22. {
  23.     global $wpdb;
  24.     $sql = "select country_code2 code, country_name name from wp_iptocountry where ip_from <= inet_aton('" . $ip . "') and ip_to >= inet_aton('" . $ip . "')";
  25.     $country = $wpdb->get_row($sql);
  26.     if(!empty($country->code)) {
  27.         $country->ip    = $ip;
  28.         $country->code  = strtolower($country->code);
  29.         $country->name  = ucwords(strtolower($country->name));
  30.         $country->img   = "<img src='" . get_bloginfo('home') . "/wp-content/plugins/recent-comments-with-avatars/mini-flags/" . $country->code . ".gif' alt='" . $country->name . "' />";
  31.     }
  32.     return $country;
  33. }
  34.  
  35.  
  36. function sa_comment_list_with_flag()
  37. {
  38.     $options = get_option('sa_comments');
  39.     switch($options['flags']) {
  40.         case 'S': wp_list_comments('callback=sa_comment_flag_start'); break;
  41.         case 'E': wp_list_comments('end-callback=sa_comment_flag_end'); break;
  42.         default: wp_list_comments(); break;
  43.     }
  44. }
  45.  
  46.  
  47. function sa_comment_flag_start($comment, $args, $depth)
  48. {
  49.     $GLOBALS['comment'] = $comment;
  50.  
  51.     if('div' == $args['style']) {
  52.         $tag = 'div';
  53.         $add_below = 'comment';
  54.     }
  55.     else {
  56.         $tag = 'li';
  57.         $add_below = 'div-comment';
  58.     }
  59.  
  60.     echo '<', $tag, ' ', comment_class(empty($args['has_children']) ? '' : 'parent'), ' id="comment-', comment_ID(), '">';
  61.     if('ul' == $args['style']) echo '<div id="div-comment-', comment_ID(), '">';
  62.     echo '<div class="comment-author vcard">';
  63.  
  64.     if($args['avatar_size'] != 0) echo get_avatar($comment, $args['avatar_size']);
  65.     printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link());
  66.     echo '</div>';
  67.     if($comment->comment_approved == '0') echo '<em>', __('Your comment is awaiting moderation.'), '</em><br />';
  68.  
  69.     echo '<div class="comment-meta commentmetadata"><a href="', htmlspecialchars(get_comment_link($comment->comment_ID)), '">', sprintf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()), '</a>';
  70.     $country = sa_get_comment_flag_from_ip($comment->comment_author_IP);
  71.     if(!empty($country->name)) {
  72.         echo ' in ', $country->img, ' (', $country->name, ')';
  73.     }
  74.     edit_comment_link(__('Edit'), ' | ', '');
  75.     echo '</div>';
  76.  
  77.     comment_text();
  78.  
  79.     echo '<div class="reply">';
  80.     comment_reply_link(array_merge($args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'])));
  81.     echo '</div>';
  82.     if('ul' == $args['style']) echo '</div>';
  83. }
  84.  
  85.  
  86. function sa_comment_flag_end($comment, $args, $depth)
  87. {
  88.     $country = sa_get_comment_flag_from_ip($comment->comment_author_IP);
  89.     if(!empty($country->name)) {
  90.         echo '<p class="commentmetadata">Comment made from: ', $country->img, ' (', $country->name, ')</p>';
  91.     }
  92.  
  93.     if('div' == $args['style']) echo "</div>\n";
  94.     else echo "</li>\n";
  95. }
  96.  
  97.  
  98. function sa_comments_widget_init()
  99. {
  100.     // Check widgets are activated.
  101.     if(!function_exists('register_sidebar_widget')) return;
  102.  
  103.     // Customised recent comments widget.
  104.     function sa_comments($args)
  105.     {
  106.         global $wpdb, $comments, $comment;
  107.         extract($args, EXTR_SKIP);
  108.         $options = get_option('sa_comments');
  109.         $title = empty($options['title']) ? __('Recent Comments') : apply_filters('widget_title', $options['title']);
  110.         if(!$number = (int)$options['number'])
  111.             $number = 5;
  112.         else if($number < 1)
  113.             $number = 1;
  114.         else if($number > 15)
  115.             $number = 15;
  116.  
  117.         if(!$comments = wp_cache_get('recent_comments', 'widget')) {
  118.             if($options['exclude'] == 'on') $sql = "select * from $wpdb->comments, $wpdb->posts where comment_approved = '1' and comment_post_ID = ID and post_status = 'publish' order by comment_date_gmt DESC limit $number";
  119.             else $sql = "select * from $wpdb->comments where comment_approved = '1' order by comment_date_gmt DESC limit $number";
  120.             $comments = $wpdb->get_results($sql);
  121.             wp_cache_add('recent_comments', $comments, 'widget');
  122.         }
  123.  
  124.         echo $before_widget;
  125.             echo $before_title . $title . $after_title;
  126.             echo ($options['avatar'] == 'on' ? '<table' : '<ul') . ' id="recentcomments">';
  127.             if($comments) : foreach((array)$comments as $comment) :
  128.                 echo ($options['avatar'] == 'on' ? '<tr><td>' . get_avatar($comment, (empty($options['avatar-size']) ? 25 : $options['avatar-size'])) . '</td><td>' : '<li class="recentcomments">');
  129.  
  130.                 $url = get_comment_author_url();
  131.                 $author = get_comment_author();
  132.  
  133.                 if(empty( $url ) || 'http://' == $url) echo $author;
  134.                 else printf("<a href='$url' rel='external nofollow' class='url' %s>$author</a>", ((int)$comment->user_id > 0 ? '' : ($options['blank'] == 'on' ? "target='_blank'" : '')));
  135.                 printf(' on %s', '<a href="'. get_comment_link($comment->comment_ID) . '">' . get_the_title($comment->comment_post_ID) . '</a>');
  136.  
  137.                 if($options['date'] == 'on') {
  138.                     echo '<br /><span style="font-size: ', (empty($options['date-size']) ? '0.5' : $options['date-size']), 'em;">', mysql2date((empty($options['date-format']) ? get_option('date_format') : $options['date-format']), $comment->comment_date), '</span>';
  139.                 }
  140.  
  141.                 if($options['excerpt'] == 'on') {
  142.                     $exp = get_comment_excerpt();
  143.                     echo '<br />', ($options['smiles'] == 'on' ? convert_smilies($exp) : $exp);
  144.                 }
  145.  
  146.                 if($options['flags-widget'] == 'on') {
  147.                     echo '<br />';
  148.                     $country = sa_get_comment_flag_from_ip($comment->comment_author_IP);
  149.                     if(!empty($country->name)) echo '<span style="font-size: ', (empty($options['date-size']) ? '0.5' : $options['date-size']), 'em;">From ', $country->img, ' (', $country->name, ')</span>';
  150.                 }
  151.  
  152.                 echo ($options['avatar'] == 'on' ? '</td></tr>' : '</li>');
  153.             endforeach; endif;
  154.             echo ($options['avatar'] == 'on' ? '</table>' : '</ul>');
  155.         echo $after_widget;
  156.     }
  157.  
  158.  
  159.     // Customised recent comments widget control form.
  160.     function sa_comments_control()
  161.     {
  162.         $options = $newoptions = get_option('sa_comments');
  163.         if(isset($_POST["recent-comments-submit"])) {
  164.             $newoptions['title'] = strip_tags(stripslashes($_POST["recent-comments-title"]));
  165.             $newoptions['number'] = (int)$_POST["recent-comments-number"];
  166.             $newoptions['avatar'] = strip_tags(stripslashes($_POST["recent-comments-avatar"]));
  167.             $newoptions['blank'] = strip_tags(stripslashes($_POST["recent-comments-blank"]));
  168.             $newoptions['avatar-size'] = strip_tags(stripslashes($_POST["recent-comments-avatar-size"]));
  169.             $newoptions['date'] = strip_tags(stripslashes($_POST["recent-comments-date"]));
  170.             $newoptions['date-format'] = strip_tags(stripslashes($_POST["recent-comments-date-format"]));
  171.             $newoptions['date-size'] = strip_tags(stripslashes($_POST["recent-comments-date-size"]));
  172.             $newoptions['excerpt'] = strip_tags(stripslashes($_POST["recent-comments-excerpt"]));
  173.             $newoptions['smiles'] = strip_tags(stripslashes($_POST["recent-comments-smiles"]));
  174.             $newoptions['flags'] = strip_tags(stripslashes($_POST["recent-comments-flags"]));
  175.             $newoptions['flags-widget'] = strip_tags(stripslashes($_POST["recent-comments-flags-widget"]));
  176.             $newoptions['exclude'] = strip_tags(stripslashes($_POST["recent-comments-exclude"]));
  177.         }
  178.         if($options != $newoptions) {
  179.             $options = $newoptions;
  180.             update_option('sa_comments', $options);
  181.         }
  182.         $title = attribute_escape($options['title']);
  183.         if(!$number = (int)$options['number'])
  184.             $number = 5;
  185.         ?>
  186.         <p><label for="recent-comments-title">Title: <input class="widefat" id="recent-comments-title" name="recent-comments-title" type="text" value="<?php echo $title; ?>" /></label></p>
  187.         <p>
  188.             <label for="recent-comments-number">Number of comments to show: <input style="width: 40px; text-align: center;" id="recent-comments-number" name="recent-comments-number" type="text" value="<?php echo $number; ?>" /></label>
  189.             <br />
  190.             <small>At most 15</small>
  191.         </p>
  192.         <p><label for="recent-comments-avatar"><input class="checkbox" type="checkbox" id="recent-comments-avatar" name="recent-comments-avatar" <?php echo ($options['avatar'] ? 'checked="checked"' : ''); ?> /> show avatar?</label></p>
  193.         <p><label for="recent-comments-avatar-size">Avatar size: <input style="width: 40px; text-align: center;" id="recent-comments-avatar-size" name="recent-comments-avatar-size" type="text" value="<?php echo empty($options['avatar-size']) ? '25' : $options['avatar-size']; ?>" /> px</label></p>
  194.         <p>
  195.             <label for="recent-comments-blank"><input class="checkbox" type="checkbox" id="recent-comments-blank" name="recent-comments-blank" <?php echo ($options['blank'] ? 'checked="checked"' : ''); ?> /> new window for avatar hyperlinks?</label>
  196.             <br />
  197.             <small>Include <code>target='_blank'</code></small>
  198.         </p>
  199.         <p><label for="recent-comments-date"><input class="checkbox" type="checkbox" id="recent-comments-date" name="recent-comments-date" <?php echo ($options['date'] ? 'checked="checked"' : ''); ?> /> show comment date?</label></p>
  200.         <p>
  201.             <label for="recent-comments-date-format">Date format: <input style="width: 180px; text-align: center;" id="recent-comments-date-format" name="recent-comments-date-format" type="text" value="<?php echo empty($options['date-format']) ? 'jS M y' : $options['date-format']; ?>" /></label>
  202.             <br />
  203.             <small><a href='http://php.net/date' target="_blank">Date format help...</a></small>
  204.         </p>
  205.         <p>
  206.             <label for="recent-comments-date-size">Date text size: <input style="width: 40px; text-align: center;" id="recent-comments-date-size" name="recent-comments-date-size" type="text" value="<?php echo empty($options['date-size']) ? '0.5' : $options['date-size']; ?>" /> em</label>
  207.             <br />
  208.             <small>Factor of normal text size, e.g. 0.5 is half.</small>
  209.         </p>
  210.         <p><label for="recent-comments-excerpt"><input class="checkbox" type="checkbox" id="recent-comments-excerpt" name="recent-comments-excerpt" <?php echo ($options['excerpt'] ? 'checked="checked"' : ''); ?> /> show comment excerpt?</label></p>
  211.         <p>
  212.             <label for="recent-comments-smiles"><input class="checkbox" type="checkbox" id="recent-comments-smiles" name="recent-comments-smiles" <?php echo ($options['smiles'] ? 'checked="checked"' : ''); ?> /> convert emoticons in comment excerpts?</label>
  213.             <br />
  214.             <small>Only works if <i>Convert Emoticons</i> is set on the <i>Settings &raquo; Writing</i>.</small>
  215.         </p>
  216.         <?php
  217.             global $wpdb;
  218.             $found = false;
  219.             foreach($wpdb->get_col("show tables", 0) as $table) {
  220.                 if($table == 'wp_iptocountry') {
  221.                     $found = true;
  222.                     break;
  223.                 }
  224.             }
  225.  
  226.             if($found) { ?>
  227.                 <p><label for="recent-comments-flags-widget"><input class="checkbox" type="checkbox" id="recent-comments-flags-widget" name="recent-comments-flags-widget" <?php echo ($options['flags-widget'] ? 'checked="checked"' : ''); ?> /> show flag for originating country?</label></p>
  228.                 <p>
  229.                     <label for="recent-comments-flags">Show flag for originating country in main comment listings at the </label>
  230.                     <input type='radio' name='recent-comments-flags' value='S' <?php echo ($options['flags'] == 'S' ? 'checked="checked"' : ''); ?> /> start
  231.                     <input type='radio' name='recent-comments-flags' value='E' <?php echo ($options['flags'] == 'E' ? 'checked="checked"' : ''); ?> /> end
  232.                     <input type='radio' name='recent-comments-flags' value='N' <?php echo ($options['flags'] == 'N' ? 'checked="checked"' : ''); ?> /> no flags.
  233.                     <br />
  234.                     <small>Requires theme comment loop modification (see readme).</small>
  235.                 </p>
  236.             <?php }
  237.         ?>
  238.         <p><label for="recent-comments-exclude"><input class="checkbox" type="checkbox" id="recent-comments-exclude" name="recent-comments-exclude" <?php echo ($options['exclude'] ? 'checked="checked"' : ''); ?> /> exclude comments from scheduled posts?</label></p>
  239.         <input type="hidden" id="recent-comments-submit" name="recent-comments-submit" value="1" />
  240.     <?php }
  241.  
  242.  
  243.     // Register widgets and their controls.
  244.     wp_register_sidebar_widget('sa-recent-comments', 'Recent Comments', sa_comments, array('classname' => 'widget_recent_comments', 'description' => 'Recent comments with configurable option to show avatars and more!'));
  245.     wp_register_widget_control('sa-recent-comments', 'Recent Comments', 'sa_comments_control', array('width' => 300));
  246. }
  247.  
  248. add_action('plugins_loaded', 'sa_comments_widget_init');
  249. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement