Advertisement
Guest User

Rick Beckman

a guest
Mar 2nd, 2008
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.35 KB | None | 0 0
  1. <?php /*
  2. Plugin Name: Commentluv
  3. Plugin URI: http://www.fiddyp.co.uk/commentluv-wordpress-plugin/
  4. Description: Plugin to show a link to the last post from the commenters blog in their comment. Just activate and it's ready. Currently parses with wordpress, blogspot, typepad and blogs that have a feed link in the head section of their page.
  5. Version: 0.999
  6. Author: Andy Bailey
  7. Author URI: http://www.fiddyp.co.uk/
  8.  
  9. *********************************************************************
  10. You can change the message that is displayed under this change log...
  11. *********************************************************************
  12. updates:
  13. 0.999 - try and change the wrong use of ? in post titles.. - http://www.tarheelramblings.com
  14. 0.998 - try to make compatible with Shifter Theme System - http://www.jaynedarcy.us/
  15. 0.997 - add bit to allow user to change message by editing source code
  16. 0.996 - removed [noluv] and replaced with checkbox on form
  17. 0.995 - add option to not get feed if user enters [noluv] (thanks http://www.blogherald.com)
  18. 0.994 - added an option to read the feed output by my own routine to curl the users page
  19. 0.993 - added check for web-log: addition by Edward De Leau of http://edward.de.leau.net/
  20. 0.992 - detect trailing slash on author url and act accordingly
  21. 0.991 - move curl check to higher up the process so the plugin doesn't take longer than necessary
  22. 0.99 - allow for styles to be applied to last blog post text
  23. 0.98 - added ability to allow commenter to switch debug on within comments by putting [debugon] in the content
  24. 0.97 - add support for typepad and blogspt own domain blogs and more header alternate links and raised the priority on action so other plugins can play with the comment afterwards for nofollow
  25. 0.96 - handle the author url more efficiently
  26. 0.95 - rewrite of feed finding and parsing and added a timout increase to the magpie rss parsing function
  27. 0.94 - fix: not parsing some feedburner feeds that have an extra subdomain on the url
  28. 0.93 - use wp internal function to parse feed and improve find feed location
  29. 0.92 - update comments
  30. 0.91 - fix: compatibility with some other comment enhancing plugins so the link isn't repeated
  31. 0.9 - now wont output emptry string if no last post found (blogspot blog with own domain)
  32. 0.8 - now prevents parsing on a trackback, pingback or admin comment
  33. 0.71 - trying to prevent showing last post on trackbacks
  34. 0.7 - prevented admin from having feed parsed when replying to comments
  35. (thanks thesmocklady.com/blog/)
  36. 0.6 - fixed problem where it wouldn't find the feed if the blog was in a subdirectory
  37. (found by http://thesmocklady.com/blog/)
  38. 0.51 - fiddled with timeout. Some feeds were not showing due to it taking too long to load the
  39. commenter's page. Testing done by thedivanetwork.com and lalla-mira.com/. Thanks!
  40. 0.5 - typepad,blogspot,wordpress all working, tries to find a feedburner feed in the authors page
  41. if it's not found in a default location. pretty robust, can now work with script links to
  42. feedburner.
  43. 0.4 - try and find users feed if they don't have a default wordpress/blogger/typepad blog
  44. 0.3 - works with typepad blogs feed, default and feedburner
  45. 0.2 - works with feedburner feed for wordpress and blogger default location
  46. 0.1 - works with wordpress default feed at default location
  47.  
  48.  
  49. */
  50. // *****************************************************************************
  51. //************ you can edit the message that is displayed here ******************
  52. //************ but be careful! only use single (') quotes not (") double ********
  53. // *****************************************************************************
  54.  
  55. $cl_message="Enable <a href='http://www.fiddyp.co.uk/commentluv-wordpress-plugin'>CommentLuv</a> which will try and get your last blog post, please be patient while it finds it for you.";
  56.  
  57.  
  58. //************ you shouldn't edit below this line!*******************************
  59.  
  60. // text between function (to make it easy to parse different types of feeds and streams)
  61. function LL_TextBetween($s1,$s2,$s){
  62.     $s1 = strtolower($s1);
  63.     $s2 = strtolower($s2);
  64.     $L1 = strlen($s1);
  65.     $scheck = strtolower($s);
  66.     if($L1>0){$pos1 = strpos($scheck,$s1);} else {$pos1=0;}
  67.     if($pos1 !== false){
  68.         if($s2 == '') return substr($s,$pos1+$L1);
  69.         $pos2 = strpos(substr($scheck,$pos1+$L1),$s2);
  70.         if($pos2!==false) return substr($s,$pos1+$L1,$pos2);
  71.     }
  72.     return '';
  73. }
  74.  
  75. // find feedburner feed function (parses a users page for a feed link)
  76. function findfeedburner($page_url){
  77.     // can't open default wordpress feed, use curl to parse users page for a relative link feed
  78.     if(function_exists(curl_init)) {
  79.         $ch=curl_init();
  80.         $timeout = 10; // set to zero for no timeout
  81.         curl_setopt ($ch, CURLOPT_URL, $page_url );
  82.         curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  83.         curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  84.         $data=curl_exec($ch);
  85.         curl_close($ch);
  86.         $lines=explode("\n",$data);
  87.         // look for feedburner url
  88.         foreach($lines as $line){
  89.             if(strstr($line,"alternate")&&(strstr($line,"rss")||strstr($line,"xml"))){
  90.                 $pos=strpos($line,"href");
  91.                 $cut=substr($line,$pos+5);
  92.                 $feed_url=LL_TextBetween("\"","\"",$cut);
  93.                 break;
  94.             }
  95.         }
  96.     }
  97.     else // no curl here, borrow mine!
  98.     {
  99.         $rss=fetch_rss("http://www.commentluv.com/commentluvinc/cl_feedfind.php?url=$page_url");
  100.         $items= array_slice($rss->items,0,1);
  101.         foreach($items as $item){
  102.             $feed_post=$item['link'];
  103.         }
  104.         return $feed_post;
  105.     }
  106.     return $feed_url;
  107. }
  108.  
  109. // hooks, call comment_luv function just before comment is posted . gets passed array of comment fields
  110. // hooks, call comment_luv_add_text when comment form is shown, gets passed id of post
  111. add_filter('preprocess_comment','comment_luv',0);
  112. add_action('comment_form','comment_luv_add_text');
  113.  
  114. // function to add text to bottom of form field
  115. function comment_luv_add_text($id='0'){
  116.     global $cl_message, $comment_luv_text_added;
  117.     if (!$comment_luv_text_added) {
  118. ?>
  119.         <p style="clear: both;" class="commentluv">
  120.         <input name="luv" id="luv" value="luv" type="checkbox" style="width: auto;" checked="checked" />
  121.         <label for="luv"><?php echo $cl_message ?></label>
  122.         </p>
  123. <?php
  124.     }
  125.     $comment_luv_text_added = true;
  126.     return $id; // need to return what we got sent
  127. }
  128.  
  129. // this is the magic part.
  130. // function to parse the users feed and add a link to last post after the rest of the comment content
  131. function comment_luv($comment_data){
  132.     $manual_feed=0;
  133.     $debug=0; // for my own debugging, shows a breadcrumb of what is tried for parsing
  134.     $luv = $_POST['luv']; // get checkbox value for commentluv
  135.     // check for debug command
  136.     if(strstr($comment_data['comment_content'],"[debugon]")){
  137.         $debug=1;
  138.     }
  139.     if($luv=='luv' && $debug) {
  140.         $comment_data['comment_content']=substr_replace($comment_data['comment_content'], ' (luv) ',strlen($comment_data['comment_content']),0);
  141.     }
  142.  
  143.     // don't parse for admin posting comment reply,pingback or trackback and checks if last post already added and check for luv box checked
  144.     get_currentuserinfo() ;
  145.     global $user_level;
  146.     if ($luv!='luv' || $user_level > 7 || $comment_data['comment_type'] == 'pingback' || $comment_data['comment_type'] == 'trackback' || strstr($comment_data['comment_content'],"'s last blog post")) {
  147.         return $comment_data;
  148.     }
  149.     // get author url
  150.     $author_url=$comment_data['comment_author_url'];
  151.     // if no author url given, return
  152.     if(!$author_url){
  153.         return $comment_data;
  154.     }
  155.     // clean up author url if it has a trailing forward slash
  156.     if(substr($author_url,-1)=="/") {
  157.         $author_url = substr($author_url, 0, -1);  // remove trailing slash
  158.         if($debug) {
  159.             $comment_data['comment_content']=substr_replace($comment_data['comment_content'], ' (remove slash) ',strlen($comment_data['comment_content']),0);
  160.         }
  161.     }
  162.  
  163.     // ***********************
  164.     // *** fun starts here ***
  165.     // ***********************
  166.     // check for magpie timeout constant
  167.     if(!defined('MAGPIE_FETCH_TIME_OUT')){
  168.         define('MAGPIE_FETCH_TIME_OUT',5);
  169.     }
  170.     // set cache age to 5 minutes so it doesn't show an old last post if a commenter makes a new post and returns to comment again
  171.     if(!defined('MAGPIE_CACHE_AGE')){
  172.         define('MAGPIE_CACHE_AGE',300);
  173.     }
  174.  
  175.     // use wp internal rss.php function (wp 2.1+ only)
  176.     include_once(ABSPATH . WPINC . '/rss.php');
  177.  
  178.     // **************************
  179.     // *** identify blog type ***
  180.     // **************************
  181.     // try and determine blog type and locate default location for feed.
  182.     if(strstr($author_url,"blogspot")){                     // blogspot blog
  183.         $feed_url="$author_url/feeds/posts/default/";
  184.         // debug
  185.         if($debug) {
  186.             $comment_data['comment_content']=substr_replace($comment_data['comment_content'], ' (blogspot) ',strlen($comment_data['comment_content']),0);
  187.         }
  188.  
  189.     } elseif(strstr($author_url,"typepad")){                // typepad blog
  190.         $feed_url="$author_url/atom.xml";
  191.         if($debug) {
  192.             $comment_data['comment_content']=substr_replace($comment_data['comment_content'], ' (typepad) ',strlen($comment_data['comment_content']),0);
  193.         }
  194.     } elseif(strstr($author_url,"livejournal")){            // livejournal
  195.         $feed_url="$author_url/data/rss";
  196.         if($debug) {
  197.             $comment_data['comment_content']=substr_replace($comment_data['comment_content'], ' (livejournal) ',strlen($comment_data['comment_content']),0);
  198.         }
  199.     } elseif(strstr($author_url,"web-log")){ // web-log blog
  200.         // take only the name of the author of http://xxx.web-log.nl
  201.         preg_match('|http://(.*?).web-log.nl|is', $author_url, $authorid);
  202.         $feed_url = $author_url . "/" . $authorid[1] . "/rss.xml";
  203.     } else {
  204.         $feed_url="$author_url/feed/";                      // own domain or wordpress blog
  205.         // debug
  206.         if($debug) {
  207.             $comment_data['comment_content']=substr_replace($comment_data['comment_content'], ' (wp_norm) ',strlen($comment_data['comment_content']),0);
  208.         }
  209.  
  210.     }
  211.  
  212.     // ***************************
  213.     // *** detect manual entry ***
  214.     // ***************************
  215.     // here we see if user manually entered their own feed url
  216.     if(strstr($comment_data['comment_content'],"[feed]")){
  217.         $feed_url=LL_TextBetween("[feed]","[/feed]",$comment_data['comment_content']);
  218.         // now strip feed bit from comment
  219.         $manual_feed_pos_start=strpos($comment_data['comment_content'],"[feed]");
  220.         $comment_data['comment_content']=substr($comment_data['comment_content'],0,$manual_feed_pos_start);
  221.         $manual_feed=1;
  222.         // debug
  223.         if($debug) {
  224.             $comment_data['comment_content']=substr_replace($comment_data['comment_content'], ' (manual feed specified) ',strlen($comment_data['comment_content']),0);
  225.         }
  226.  
  227.     }
  228.  
  229.     // *******************************
  230.     // *** time to do the fetching ***
  231.     // *******************************
  232.     // fetch feed with WP function
  233.     $rss=fetch_rss("$feed_url");
  234.  
  235.     // couldn't find it try to parse users page if curl enabled
  236.     if(!$rss && !$manual_feed){
  237.         // debug
  238.         if($debug) {
  239.             $comment_data['comment_content']=substr_replace($comment_data['comment_content'], ' (try parsing) ',strlen($comment_data['comment_content']),0);
  240.         }
  241.         $feed_url=findfeedburner($author_url);
  242.         $rss=fetch_rss("$feed_url");
  243.     }
  244.  
  245.     // couldn't find it! look in other places
  246.     if(!$rss && !$manual_feed){
  247.         // debug
  248.         if($debug) {
  249.             $comment_data['comment_content']=substr_replace($comment_data['comment_content'], ' (try alternate) ',strlen($comment_data['comment_content']),0);
  250.         }
  251.         $feed_url="$author_url/?feed=rss";
  252.         $rss=fetch_rss("$feed_url");
  253.         // try own domain blogspot
  254.         if(!$rss){
  255.             if($debug) {
  256.                 $comment_data['comment_content']=substr_replace($comment_data['comment_content'], ' (try blogspot location) ',strlen($comment_data['comment_content']),0);
  257.             }
  258.             $feed_url="$author_url/feeds/posts/default";
  259.             $rss=fetch_rss("$feed_url");
  260.         }
  261.         // try typepad own domain
  262.         if(!$rss) {
  263.             if($debug) {
  264.                 $comment_data['comment_content']=substr_replace($comment_data['comment_content'], ' (try typepad) ',strlen($comment_data['comment_content']),0);
  265.             }
  266.             $feed_url="$author_url/atom.xml";
  267.             $rss=fetch_rss("$feed_url");
  268.         }
  269.     }
  270.  
  271.  
  272.     // couldn't find it at all, just return with a sad face
  273.     if(!$rss) {
  274.         // debug
  275.         if($debug) {
  276.             $comment_data['comment_content']=substr_replace($comment_data['comment_content'], ' (failed) ',strlen($comment_data['comment_content']),0);
  277.         }
  278.         return $comment_data;
  279.     }
  280.  
  281.     // for compatibility with other comment plugins remove the wp_rel_nofollow functon call
  282.     remove_filter('pre_comment_content', 'wp_rel_nofollow');
  283.  
  284.     // **************************
  285.     // *** do the parse dance ***
  286.     // **************************
  287.     // now we must have a feed to parse, get last post title and link
  288.     $items= array_slice($rss->items,0,1);
  289.     foreach($items as $item){
  290.         $feed_title=$item['title'];
  291.         $feed_post=$item['link'];
  292.     }
  293.    
  294.     // try and fix any single quotes that got changed to question marks
  295.     $search = array("?s", "?t", "?l", "?v", "?m", "?d");
  296.     $replace = array("'s", "'t", "'l", "'v", "'m", "'d");
  297.     $feed_title=str_replace($search,$replace,$feed_title);
  298.  
  299.     // ****************************
  300.     // *** append the last post ***
  301.     // ****************************
  302.     // insert last post data onto the end of the comment content
  303.     if($feed_title && $feed_post){  // only output if last post found
  304.         $author_excerpt="\n\n<em>".$comment_data['comment_author']."'s last blog post..<a href='$feed_post'>$feed_title</a></em>";
  305.         $comment_data['comment_content']=substr_replace($comment_data['comment_content'], $author_excerpt,strlen($comment_data['comment_content']),0);
  306.     }
  307.  
  308.     // thats it! pass back the new comment data to wordpress
  309.     return $comment_data;
  310.  
  311.  
  312. } // end function
  313. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement