Advertisement
Guest User

Untitled

a guest
Sep 1st, 2010
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.97 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Comment Indexer
  4. Plugin URI:
  5. Description:
  6. Author: Andrew Billits (Incsub)
  7. Version: 1.0.4
  8. Author URI:
  9. WDP ID: 28
  10. */
  11.  
  12. /*
  13. Copyright 2007-2009 Incsub (http://incsub.com)
  14.  
  15. This program is free software; you can redistribute it and/or modify
  16. it under the terms of the GNU General Public License (Version 2 - GPLv2) as published by
  17. the Free Software Foundation.
  18.  
  19. This program is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. GNU General Public License for more details.
  23.  
  24. You should have received a copy of the GNU General Public License
  25. along with this program; if not, write to the Free Software
  26. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  27. */
  28.  
  29. $comment_indexer_current_version = '1.0.4';
  30. //------------------------------------------------------------------------//
  31. //---Config---------------------------------------------------------------//
  32. //------------------------------------------------------------------------//
  33.  
  34. //------------------------------------------------------------------------//
  35. //---Hook-----------------------------------------------------------------//
  36. //------------------------------------------------------------------------//
  37. //check for activating
  38. if ($_GET['key'] == '' || $_GET['key'] === ''){
  39.     add_action('admin_head', 'comment_indexer_make_current');
  40. }
  41. //index comments
  42. add_action('comment_post', 'comment_indexer_comment_insert_update');
  43. add_action('edit_comment', 'comment_indexer_comment_insert_update');
  44. add_action('delete_comment', 'comment_indexer_delete');
  45. add_action('wp_set_comment_status', 'comment_indexer_update_comment_status', 5, 2);
  46. //handle blog changes
  47. add_action('make_spam_blog', 'comment_indexer_change_remove');
  48. add_action('archive_blog', 'comment_indexer_change_remove');
  49. add_action('mature_blog', 'comment_indexer_change_remove');
  50. add_action('deactivate_blog', 'comment_indexer_change_remove');
  51. add_action('blog_privacy_selector', 'comment_indexer_public_update');
  52. add_action('delete_blog', 'comment_indexer_change_remove', 10, 1);
  53. //update blog types
  54. add_action('blog_types_update', 'comment_indexer_sort_terms_update');
  55. //------------------------------------------------------------------------//
  56. //---Functions------------------------------------------------------------//
  57. //------------------------------------------------------------------------//
  58. function comment_indexer_make_current() {
  59.     global $wpdb, $comment_indexer_current_version;
  60.     if (get_site_option( "comment_indexer_version" ) == '') {
  61.         add_site_option( 'comment_indexer_version', '0.0.0' );
  62.     }
  63.    
  64.     if (get_site_option( "comment_indexer_version" ) == $comment_indexer_current_version) {
  65.         // do nothing
  66.     } else {
  67.         //update to current version
  68.         update_site_option( "comment_indexer_installed", "no" );
  69.         update_site_option( "comment_indexer_version", $comment_indexer_current_version );
  70.     }
  71.     comment_indexer_global_install();
  72.     //--------------------------------------------------//
  73.     if (get_option( "comment_indexer_version" ) == '') {
  74.         add_option( 'comment_indexer_version', '0.0.0' );
  75.     }
  76.    
  77.     if (get_option( "comment_indexer_version" ) == $comment_indexer_current_version) {
  78.         // do nothing
  79.     } else {
  80.         //update to current version
  81.         update_option( "comment_indexer_version", $comment_indexer_current_version );
  82.         comment_indexer_blog_install();
  83.     }
  84. }
  85.  
  86. function comment_indexer_blog_install() {
  87.     global $wpdb, $comment_indexer_current_version;
  88.     //$comment_indexer_table1 = "";
  89.     //$wpdb->query( $comment_indexer_table1 );
  90. }
  91.  
  92. function comment_indexer_global_install() {
  93.     global $wpdb, $comment_indexer_current_version;
  94.     if (get_site_option( "comment_indexer_installed" ) == '') {
  95.         add_site_option( 'comment_indexer_installed', 'no' );
  96.     }
  97.    
  98.     if (get_site_option( "comment_indexer_installed" ) == "yes") {
  99.         // do nothing
  100.     } else {
  101.    
  102.         $comment_indexer_table1 = "CREATE TABLE IF NOT EXISTS `" . $wpdb->base_prefix . "site_comments` (
  103.  `site_comment_id` bigint(20) unsigned NOT NULL auto_increment,
  104.  `blog_id` bigint(20),
  105.  `site_id` bigint(20),
  106.  `sort_terms` TEXT,
  107.  `blog_public` int(2),
  108.  `comment_approved` VARCHAR(255),
  109.  `comment_id` bigint(20),
  110.  `comment_post_id` bigint(20),
  111.  `comment_post_permalink` TEXT,
  112.  `comment_author` VARCHAR(60),
  113.  `comment_author_email` VARCHAR(255),
  114.  `comment_author_IP` VARCHAR(255),
  115.  `comment_author_url` VARCHAR(50),
  116.  `comment_author_user_id` bigint(20),
  117.  `comment_content` TEXT,
  118.  `comment_content_stripped` TEXT,
  119.  `comment_karma` VARCHAR(255),
  120.  `comment_agent` VARCHAR(255),
  121.  `comment_type` VARCHAR(255),
  122.  `comment_parent` VARCHAR(255),
  123.  `comment_date_gmt` datetime NOT NULL default '0000-00-00 00:00:00',
  124.  `comment_date_stamp` VARCHAR(255),
  125.  PRIMARY KEY  (`site_comment_id`)
  126. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;";
  127.         $comment_indexer_table2 = "";
  128.         $comment_indexer_table3 = "";
  129.         $comment_indexer_table4 = "";
  130.         $comment_indexer_table5 = "";
  131.  
  132.         $wpdb->query( $comment_indexer_table1 );
  133.         $wpdb->query( $comment_indexer_table2 );
  134.         //$wpdb->query( $comment_indexer_table3 );
  135.         //$wpdb->query( $comment_indexer_table4 );
  136.         //$wpdb->query( $comment_indexer_table5 );
  137.         update_site_option( "comment_indexer_installed", "yes" );
  138.     }
  139. }
  140.  
  141. function comment_indexer_update_comment_status($tmp_comment_ID, $tmp_comment_status){
  142.   global $wpdb;
  143.  
  144.   switch ( $tmp_comment_status ) {
  145.   case 'hold':
  146.     $query = "UPDATE " . $wpdb->base_prefix . "site_comments SET comment_approved='0' WHERE comment_id ='" . $tmp_comment_ID . "' and blog_id = '" . $wpdb->blogid . "' LIMIT 1";
  147.     break;
  148.   case 'approve':
  149.     $query = "UPDATE " . $wpdb->base_prefix . "site_comments SET comment_approved='1' WHERE comment_id ='" . $tmp_comment_ID . "' and blog_id = '" . $wpdb->blogid . "' LIMIT 1";
  150.     break;
  151.   case 'spam':
  152.     $query = "UPDATE " . $wpdb->base_prefix . "site_comments SET comment_approved='spam' WHERE comment_id ='" . $tmp_comment_ID . "' and blog_id = '" . $wpdb->blogid . "' LIMIT 1";
  153.     break;
  154.   case 'delete':
  155.     comment_indexer_delete($tmp_comment_ID);
  156.     return true;
  157.     break;
  158.   default:
  159.     return false;
  160.   }
  161.  
  162.   if ( !$wpdb->query($query) )
  163.     return false;
  164. }
  165.  
  166. function comment_indexer_get_sort_terms($tmp_blog_ID){
  167.     $comment_indexer_blog_lang = get_blog_option($tmp_blog_ID,"WPLANG");
  168.     if ($comment_indexer_blog_lang == ''){
  169.         $comment_indexer_blog_lang = 'en_EN';
  170.     }
  171.     $comment_indexer_blog_types = get_blog_option($tmp_blog_ID,"blog_types");
  172.     if ($comment_indexer_blog_types == ''){
  173.         $comment_indexer_blog_types = '||';
  174.     }
  175.     $comment_indexer_class = get_blog_option($tmp_blog_ID,"blog_class");
  176.    
  177.     $tmp_sort_terms = array();
  178.    
  179.     $comment_indexer_blog_types = explode("|", $comment_indexer_blog_types);
  180.     foreach ( $comment_indexer_blog_types as $comment_indexer_blog_type ) {
  181.         if ( $comment_indexer_blog_type != '' ) {
  182.             $tmp_sort_terms[] = 'blog_type_' . $comment_indexer_blog_type;
  183.         }
  184.     }
  185.     if ( $comment_indexer_class != '' ) {
  186.         $tmp_sort_terms[] = 'class_' . $comment_indexer_class;
  187.     }
  188.    
  189.     $tmp_sort_terms[] = 'blog_lang_' . strtolower( $comment_indexer_blog_lang );
  190.    
  191.     return '|' . implode("|", $tmp_sort_terms) . '|all|';
  192.  
  193. }
  194.  
  195. function comment_indexer_comment_insert_update($tmp_comment_ID){
  196.     global $wpdb, $current_site;
  197.    
  198.     $tmp_blog_public = get_blog_status( $wpdb->blogid, 'public');
  199.     $tmp_blog_archived = get_blog_status( $wpdb->blogid, 'archived');
  200.     $tmp_blog_mature = get_blog_status( $wpdb->blogid, 'mature');
  201.     $tmp_blog_spam = get_blog_status( $wpdb->blogid, 'spam');
  202.     $tmp_blog_deleted = get_blog_status( $wpdb->blogid, 'deleted');
  203.    
  204.     $tmp_comment = get_comment($tmp_comment_ID);
  205.     if ($tmp_blog_archived == '1'){
  206.         comment_indexer_delete($tmp_comment_ID);
  207.     } else if ($tmp_blog_mature == '1'){
  208.         comment_indexer_delete($tmp_comment_ID);
  209.     } else if ($tmp_blog_spam == '1'){
  210.         comment_indexer_delete($tmp_comment_ID);
  211.     } else if ($tmp_blog_deleted == '1'){
  212.         comment_indexer_delete($tmp_comment_ID);
  213.     } else if ($tmp_comment->comment_content == ''){
  214.         comment_indexer_delete($tmp_comment_ID);
  215.     } else {
  216.         //delete comment
  217.         comment_indexer_delete($tmp_comment_ID);
  218.        
  219.         //get sort terms
  220.         $tmp_sort_terms = comment_indexer_get_sort_terms($wpdb->blogid);
  221.         //comment does not exist - insert site comment
  222.  
  223.         $wpdb->query("INSERT IGNORE INTO " . $wpdb->base_prefix . "site_comments
  224.         (blog_id, site_id, sort_terms, blog_public, comment_approved, comment_id, comment_post_id, comment_post_permalink, comment_author, comment_author_email, comment_author_IP, comment_author_url, comment_author_user_id, comment_content, comment_content_stripped, comment_karma, comment_agent, comment_type, comment_parent, comment_date_gmt, comment_date_stamp)
  225.         VALUES
  226.         ('" . $wpdb->blogid . "','" . $wpdb->siteid . "','" . $tmp_sort_terms . "','" . $tmp_blog_public . "','" . $tmp_comment->comment_approved . "','" . $tmp_comment_ID . "','" . $tmp_comment->comment_post_ID . "','" . get_permalink($tmp_comment->comment_post_ID) . "','" .  $tmp_comment->comment_author . "','" . $tmp_comment->comment_author_email . "','" . $tmp_comment->comment_author_IP . "','" . $tmp_comment->comment_author_url . "','" . $tmp_comment->user_id . "','" . addslashes($tmp_comment->comment_content) . "','" . addslashes(comment_indexer_strip_content($tmp_comment->comment_content)) . "','" . $tmp_comment->comment_karma . "','" . $tmp_comment->comment_agent . "','" . $tmp_comment->comment_type . "','" . $tmp_comment->comment_parent . "','" . $tmp_comment->comment_date_gmt . "','" . time() . "')");
  227.     }
  228. }
  229.  
  230. function comment_indexer_delete($tmp_comment_ID){
  231.     global $wpdb;
  232.     //delete site comment
  233.     $wpdb->query( "DELETE FROM " . $wpdb->base_prefix . "site_comments WHERE comment_id = '" . $tmp_comment_ID . "' AND blog_id = '" . $wpdb->blogid . "'" );
  234. }
  235.  
  236. function comment_indexer_delete_by_site_comment_id($tmp_site_comment_ID, $tmp_blog_ID) {
  237.     global $wpdb;
  238.     //delete site comment
  239.     $wpdb->query( "DELETE FROM " . $wpdb->base_prefix . "site_comments WHERE site_comment_id = '" . $tmp_site_comment_ID . "'" );
  240. }
  241.  
  242. function comment_indexer_public_update(){
  243.     global $wpdb;
  244.     if ( $_GET['updated'] == 'true' ) {
  245.         $wpdb->query("UPDATE " . $wpdb->base_prefix . "site_comments SET blog_public = '" . get_blog_status( $wpdb->blogid, 'public') . "' WHERE blog_id = '" . $wpdb->blogid . "' AND site_id = '" . $wpdb->siteid . "'");
  246.     }
  247. }
  248. function comment_indexer_sort_terms_update(){
  249.     global $wpdb;
  250.     $wpdb->query("UPDATE " . $wpdb->base_prefix . "site_comments SET sort_terms = '" . comment_indexer_get_sort_terms($wpdb->blogid) . "' WHERE blog_id = '" . $wpdb->blogid . "' AND site_id = '" . $wpdb->siteid . "'");
  251. }
  252.  
  253. function comment_indexer_change_remove($tmp_blog_ID){
  254.     global $wpdb, $current_user, $current_site;
  255.     //delete site posts
  256.     $query = "SELECT * FROM " . $wpdb->base_prefix . "site_comments WHERE blog_id = '" . $tmp_blog_ID . "' AND site_id = '" . $wpdb->siteid . "'";
  257.     $blog_site_comments = $wpdb->get_results( $query, ARRAY_A );
  258.     if (count($blog_site_comments) > 0){
  259.         foreach ($blog_site_comments as $blog_site_comment){
  260.             comment_indexer_delete_by_site_comment_id($blog_site_comment['site_comment_id'], $tmp_blog_ID);
  261.         }
  262.     }
  263. }
  264.  
  265. //------------------------------------------------------------------------//
  266. //---Output Functions-----------------------------------------------------//
  267. //------------------------------------------------------------------------//
  268.  
  269. //------------------------------------------------------------------------//
  270. //---Page Output Functions------------------------------------------------//
  271. //------------------------------------------------------------------------//
  272.  
  273. //------------------------------------------------------------------------//
  274. //---Support Functions----------------------------------------------------//
  275. //------------------------------------------------------------------------//
  276.  
  277. function comment_indexer_strip_content($tmp_content){
  278.     $tmp_content = strip_tags($tmp_content);
  279.     return $tmp_content;
  280. }
  281.  
  282. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement