Advertisement
Guest User

Amer Kawar

a guest
Oct 31st, 2009
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.53 KB | None | 0 0
  1. <?php
  2. /*
  3.  * File: Fixed 'disqus.php' file
  4.  *
  5.  * Article: HOW TO: Build a XHTML Valid Wordpress Blog with DISQUS Plugin
  6.  * URL: http://blog.thoughtpick.com/2009/10/how-to-build-xhtml-valid-wordpress.html
  7.  *
  8.  */
  9. ?><?php
  10. /*
  11. Plugin Name: DISQUS Comment System
  12. Plugin URI: http://disqus.com/
  13. Description: The DISQUS comment system replaces your WordPress comment system with your comments hosted and powered by DISQUS. Head over to the Comments admin page to set up your DISQUS Comment System.
  14. Author: DISQUS.com <team@disqus.com>
  15. Version: 2.12.7121
  16. Author URI: http://disqus.com/
  17.  
  18. */
  19.  
  20. require_once('lib/api.php');
  21.  
  22. define('DISQUS_URL',            'http://disqus.com');
  23. define('DISQUS_API_URL',        DISQUS_URL);
  24. define('DISQUS_DOMAIN',         'disqus.com');
  25. define('DISQUS_IMPORTER_URL',   'http://import.disqus.net');
  26. define('DISQUS_MEDIA_URL',      'http://media.disqus.com');
  27. define('DISQUS_RSS_PATH',       '/latest.rss');
  28.  
  29. function dsq_plugin_basename($file) {
  30.     $file = dirname($file);
  31.  
  32.     // From WP2.5 wp-includes/plugin.php:plugin_basename()
  33.     $file = str_replace('\\','/',$file); // sanitize for Win32 installs
  34.     $file = preg_replace('|/+|','/', $file); // remove any duplicate slash
  35.     $file = preg_replace('|^.*/' . PLUGINDIR . '/|','',$file); // get relative path from plugins dir
  36.  
  37.     if ( strstr($file, '/') === false ) {
  38.         return $file;
  39.     }
  40.  
  41.     $pieces = explode('/', $file);
  42.     return !empty($pieces[count($pieces)-1]) ? $pieces[count($pieces)-1] : $pieces[count($pieces)-2];
  43. }
  44.  
  45. if ( !defined('WP_CONTENT_URL') ) {
  46.     define('WP_CONTENT_URL', get_option('siteurl') . '/wp-content');
  47. }
  48. if ( !defined('PLUGINDIR') ) {
  49.     define('PLUGINDIR', 'wp-content/plugins'); // Relative to ABSPATH.  For back compat.
  50. }
  51.  
  52. define('DSQ_PLUGIN_URL', WP_CONTENT_URL . '/plugins/' . dsq_plugin_basename(__FILE__));
  53.  
  54. /**
  55.  * DISQUS WordPress plugin version.
  56.  *
  57.  * @global  string  $dsq_version
  58.  * @since   1.0
  59.  */
  60. $dsq_version = '2.12';
  61. $mt_dsq_version = '2.01';
  62. /**
  63.  * Response from DISQUS get_thread API call for comments template.
  64.  *
  65.  * @global  string  $dsq_response
  66.  * @since   1.0
  67.  */
  68. $dsq_response = '';
  69. /**
  70.  * Comment sort option.
  71.  *
  72.  * @global  string  $dsq_sort
  73.  * @since   1.0
  74.  */
  75. $dsq_sort = 1;
  76. /**
  77.  * Flag to determine whether or not the comment count script has been embedded.
  78.  *
  79.  * @global  string  $dsq_cc_script_embedded
  80.  * @since   1.0
  81.  */
  82. $dsq_cc_script_embedded = false;
  83. /**
  84.  * DISQUS API instance.
  85.  *
  86.  * @global  string  $dsq_api
  87.  * @since   1.0
  88.  */
  89. $dsq_api = new DisqusAPI(get_option('disqus_forum_url'), get_option('disqus_api_key'));
  90. /**
  91.  * Copy of global wp_query.
  92.  *
  93.  * @global  WP_Query    $dsq_wp_query
  94.  * @since   1.0
  95.  */
  96. $dsq_wp_query = NULL;
  97.  
  98. /**
  99.  * Helper functions.
  100.  */
  101.  
  102. function dsq_legacy_mode() {
  103.     return get_option('disqus_forum_url') && !get_option('disqus_api_key');
  104. }
  105.  
  106. function dsq_is_installed() {
  107.     return get_option('disqus_forum_url') && get_option('disqus_api_key');
  108. }
  109.  
  110. function dsq_can_replace() {
  111.     global $id, $post;
  112.     $replace = get_option('disqus_replace');
  113.  
  114.     if ( 'draft' == $post->post_status )   { return false; }
  115.     if ( !get_option('disqus_forum_url') ) { return false; }
  116.     else if ( 'all' == $replace )          { return true; }
  117.  
  118.     if ( !isset($post->comment_count) ) {
  119.         $num_comments = 0;
  120.     } else {
  121.         if ( 'empty' == $replace ) {
  122.             // Only get count of comments, not including pings.
  123.  
  124.             // If there are comments, make sure there are comments (that are not track/pingbacks)
  125.             if ( $post->comment_count > 0 ) {
  126.                 // Yuck, this causes a DB query for each post.  This can be
  127.                 // replaced with a lighter query, but this is still not optimal.
  128.                 $comments = get_approved_comments($post->ID);
  129.                 foreach ( $comments as $comment ) {
  130.                     if ( $comment->comment_type != 'trackback' && $comment->comment_type != 'pingback' ) {
  131.                         $num_comments++;
  132.                     }
  133.                 }
  134.             } else {
  135.                 $num_comments = 0;
  136.             }
  137.         }
  138.         else {
  139.             $num_comments = $post->comment_count;
  140.         }
  141.     }
  142.  
  143.     return ( ('empty' == $replace && 0 == $num_comments)
  144.         || ('closed' == $replace && 'closed' == $post->comment_status) );
  145. }
  146.  
  147. function dsq_manage_dialog($message, $error = false) {
  148.     global $wp_version;
  149.  
  150.     echo '<div '
  151.         . ( $error ? 'id="disqus_warning" ' : '')
  152.         . 'class="updated fade'
  153.         . ( ($wp_version < 2.5 && $error) ? '-ff0000' : '' )
  154.         . '"><p><strong>'
  155.         . $message
  156.         . '</strong></p></div>';
  157. }
  158.  
  159. function dsq_sync_comments($post, $comments) {
  160.     global $wpdb;
  161.  
  162.     // Get last_comment_date id for $post with Disqus metadata
  163.     // (This is the date that is stored in the Disqus DB.)
  164.     $last_comment_date = $wpdb->get_var('SELECT max(comment_date) FROM ' . $wpdb->prefix . 'comments WHERE comment_post_ID=' . intval($post->ID) . " AND comment_agent LIKE 'Disqus/%';");
  165.     if ( $last_comment_date ) {
  166.         $last_comment_date = strtotime($last_comment_date);
  167.     }
  168.  
  169.     if ( !$last_comment_date ) {
  170.         $last_comment_date = 0;
  171.     }
  172.  
  173.     foreach ( $comments as $comment ) {
  174.         if ( $comment['imported'] ) {
  175.             continue;
  176.         } else if ( $comment['date'] <= $last_comment_date ) {
  177.             // If comment date of comment is <= last_comment_date, skip comment.
  178.             continue;
  179.         } else {
  180.             // Else, insert_comment
  181.             $commentdata = array(
  182.                 'comment_post_ID' => $post->ID,
  183.                 'comment_author' => $comment['user']['display_name'],
  184.                 'comment_author_email' => $comment['user']['email'],
  185.                 'comment_author_url' => $comment['user']['url'],
  186.                 'comment_author_IP' => $comment['user']['ip_address'],
  187.                 'comment_date' => date('Y-m-d H:i:s', $comment['date']),
  188.                 'comment_date_gmt' => date('Y-m-d H:i:s', $comment['date_gmt']),
  189.                 'comment_content' => $comment['message'],
  190.                 'comment_approved' => 1,
  191.                 'comment_agent' => 'Disqus/1.0:' . intval($comment['id']),
  192.                 'comment_type' => '',
  193.             );
  194.             wp_insert_comment($commentdata);
  195.         }
  196.     }
  197.  
  198.     if( isset($_POST['dsq_api_key']) && $_POST['dsq_api_key'] == get_option('disqus_api_key') ) {
  199.         if( isset($_GET['dsq_sync_action']) && isset($_GET['dsq_sync_comment_id']) ) {
  200.             $comment_parts = explode('=', $_GET['dsq_sync_comment_id']);
  201.             if( 'wp_id' == $comment_parts[0] ) {
  202.                 $comment_id = intval($comment_parts[1]);
  203.             } else {
  204.                 $comment_id = $wpdb->get_var('SELECT comment_ID FROM ' . $wpdb->prefix . 'comments WHERE comment_post_ID=' . intval($post->ID) . " AND comment_agent LIKE 'Disqus/1.0:" . intval($comment_parts[1]) . "'");
  205.             }
  206.  
  207.             switch( $_GET['dsq_sync_action'] ) {
  208.                 case 'mark_spam':
  209.                     wp_set_comment_status($comment_id, 'spam');
  210.                     echo "<!-- dsq_sync: wp_set_comment_status($comment_id, 'spam') -->";
  211.                     break;
  212.                 case 'mark_approved':
  213.                     wp_set_comment_status($comment_id, 'approve');
  214.                     echo "<!-- dsq_sync: wp_set_comment_status($comment_id, 'approve') -->";
  215.                     break;
  216.                 case 'mark_killed':
  217.                     wp_set_comment_status($comment_id, 'hold');
  218.                     echo "<!-- dsq_sync: wp_set_comment_status($comment_id, 'hold') -->";
  219.                     break;
  220.             }
  221.         }
  222.     }
  223. }
  224.  
  225. /**
  226.  *  Filters/Actions
  227.  */
  228.  
  229. function dsq_get_style() {
  230.     echo "<link rel=\"stylesheet\" href=\"" . DISQUS_API_URL ."/stylesheets/" .  strtolower(get_option('disqus_forum_url')) . "/disqus.css?v=2.0\" type=\"text/css\" media=\"screen\" />";
  231. }
  232.  
  233. add_action('wp_head','dsq_get_style');
  234.  
  235. function dsq_comments_template($value) {
  236.     global $dsq_response;
  237.     global $dsq_sort;
  238.     global $dsq_api;
  239.     global $post;
  240.  
  241.     if ( ! (is_single() || is_page() || $withcomments) ) {
  242.         return;
  243.     }
  244.  
  245.     if ( !dsq_can_replace() ) {
  246.         return $value;
  247.     }
  248.  
  249.     if ( dsq_legacy_mode() ) {
  250.         return dirname(__FILE__) . '/comments-legacy.php';
  251.     }
  252.  
  253.     $permalink = get_permalink();
  254.     $title = get_the_title();
  255.     $excerpt = get_the_excerpt();
  256.  
  257.     $dsq_sort = get_option('disqus_sort');
  258.     if ( is_numeric($_COOKIE['disqus_sort']) ) {
  259.         $dsq_sort = $_COOKIE['disqus_sort'];
  260.     }
  261.  
  262.     if ( is_numeric($_GET['dsq_sort']) ) {
  263.         setcookie('disqus_sort', $_GET['dsq_sort']);
  264.         $dsq_sort = $_GET['dsq_sort'];
  265.     }
  266.  
  267.     // Call "get_thread" API method.
  268.     $dsq_response = $dsq_api->get_thread($post, $permalink, $title, $excerpt);
  269.     if( $dsq_response < 0 ) {
  270.         return false;
  271.     }
  272.     // Sync comments with database.
  273.     dsq_sync_comments($post, $dsq_response['posts']);
  274.  
  275.     // TODO: If a disqus-comments.php is found in the current template's
  276.     // path, use that instead of the default bundled comments.php
  277.     //return TEMPLATEPATH . '/disqus-comments.php';
  278.  
  279.     return dirname(__FILE__) . '/comments.php';
  280. }
  281.  
  282. function dsq_comment_count() {
  283.     global $dsq_cc_script_embedded, $dsq_wp_query, $wp_query;
  284.  
  285.     if ( $dsq_cc_script_embedded ) {
  286.         return;
  287.     } else if ( (is_single() || is_page() || $withcomments || is_feed()) ) {
  288.         return;
  289.     } else if ( $dsq_wp_query->is_feed ) {
  290.         // Protect ourselves from other plugins which begin their own loop
  291.         // and clobber $wp_query.
  292.         return;
  293.     }
  294.  
  295.     ?>
  296.  
  297.     <script type="text/javascript">
  298.     // <![CDATA[
  299.         (function() {
  300.             var links = document.getElementsByTagName('a');
  301.             var query = '&';
  302.             for(var i = 0; i < links.length; i++) {
  303.                 if(links[i].href.indexOf('#disqus_thread') >= 0) {
  304.                     links[i].innerHTML = 'View Comments';
  305.                                         var id_str = links[i].getAttribute('id');
  306.                     query += 'wpid' + i + '=' + encodeURIComponent(id_str.replace("dsq","")) + '&';
  307.                 }
  308.             }
  309.             document.write('<script charset="utf-8" type="text/javascript" src="http://<?php echo strtolower(get_option('disqus_forum_url')); ?>.<?php echo DISQUS_DOMAIN; ?>/get_num_replies_from_wpid.js?v=2.0' + query + '"><' + '/script>');
  310.  
  311.         })();
  312.     //]]>
  313.     </script>
  314.  
  315.     <?php
  316.  
  317.     $dsq_cc_script_embedded = true;
  318. }
  319.  
  320. // Mark entries in index to replace comments link.
  321. function dsq_comments_number($comment_text) {
  322.     global $post;
  323.  
  324.     if ( dsq_can_replace() ) {
  325.         ob_start();
  326.         the_permalink();
  327.         $the_permalink = ob_get_contents();
  328.         ob_end_clean();
  329.                
  330.         return '</a><noscript><a href="http://' . strtolower(get_option('disqus_forum_url')) . '.' . DISQUS_DOMAIN . '/?url=' . $the_permalink .'">View comments</a></noscript><a class="dsq-comment-count" href="' . $the_permalink . '#disqus_thread" id="dsq' . $post->ID . '">Comments';
  331.     } else {
  332.         return $comment_text;
  333.     }
  334. }
  335.  
  336. function dsq_bloginfo_url($url) {
  337.     if ( get_feed_link('comments_rss2') == $url ) {
  338.         return 'http://' . strtolower(get_option('disqus_forum_url')) . '.' . DISQUS_DOMAIN . DISQUS_RSS_PATH;
  339.     } else {
  340.         return $url;
  341.     }
  342. }
  343.  
  344. // For WordPress 2.0.x
  345. function dsq_loop_start() {
  346.     global $comment_count_cache, $dsq_wp_query, $wp_query;
  347.  
  348.     if ( !isset($dsq_wp_query) || is_null($dsq_wp_query) ) {
  349.         $dsq_wp_query = $wp_query;
  350.     }
  351.  
  352.     if ( isset($comment_count_cache) ) {
  353.         foreach ( $comment_count_cache as $key => $value ) {
  354.             if ( 0 == $value ) {
  355.                 $comment_count_cache[$key] = -1;
  356.             }
  357.         }
  358.     }
  359. }
  360.  
  361. function dsq_add_pages() {
  362.     global $menu, $submenu;
  363.  
  364.     add_submenu_page('edit-comments.php', 'DISQUS', 'DISQUS', 8, 'disqus', dsq_manage);
  365.  
  366.     // TODO: This does not work in WP2.0.
  367.  
  368.     // Replace Comments top-level menu link with link to our page
  369.     foreach ( $menu as $key => $value ) {
  370.         if ( 'edit-comments.php' == $menu[$key][2] ) {
  371.             $menu[$key][2] = 'edit-comments.php?page=disqus';
  372.         }
  373.     }
  374.  
  375.     add_options_page('DISQUS', 'DISQUS', 8, 'disqus', dsq_manage);
  376. }
  377.  
  378. function dsq_manage() {
  379.     require_once('admin-header.php');
  380.     include_once('manage.php');
  381. }
  382.  
  383. // Always add Disqus management page to the admin menu
  384. add_action('admin_menu', 'dsq_add_pages');
  385.  
  386. function dsq_warning() {
  387.     global $wp_version;
  388.  
  389.     if ( !get_option('disqus_forum_url') && !isset($_POST['forum_url']) && $_GET['page'] != 'disqus' ) {
  390.         dsq_manage_dialog('You must <a href="edit-comments.php?page=disqus">configure the plugin</a> to enable Disqus Comments.', true);
  391.     }
  392.  
  393.     if ( dsq_legacy_mode() && $_GET['page'] == 'disqus' ) {
  394.         dsq_manage_dialog('Disqus Comments has not yet been configured. (<a href="edit-comments.php?page=disqus">Click here to configure</a>)');
  395.     }
  396. }
  397.  
  398. function dsq_check_version() {
  399.     global $dsq_api;
  400.  
  401.     $latest_version = $dsq_api->wp_check_version();
  402.     if ( $latest_version ) {
  403.         dsq_manage_dialog('You are running an old version of the Disqus Comments plugin. Please <a href="http://disqus.com/comments/wordpress">check the website</a> for updates.');
  404.     }
  405. }
  406.  
  407. add_action('admin_notices', 'dsq_warning');
  408. add_action('admin_notices', 'dsq_check_version');
  409.  
  410. // Only replace comments if the disqus_forum_url option is set.
  411. add_filter('comments_template', 'dsq_comments_template');
  412. add_filter('comments_number', 'dsq_comments_number');
  413. add_filter('bloginfo_url', 'dsq_bloginfo_url');
  414. add_action('loop_start', 'dsq_loop_start');
  415.  
  416. // For comment count script.
  417. if ( !get_option('disqus_cc_fix') ) {
  418.     add_action('loop_end', 'dsq_comment_count');
  419. }
  420. add_action('wp_footer', 'dsq_comment_count');
  421.  
  422. ?>
  423.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement