public
Last active

Disable comments query plugin

  • Download Gist
disable-comments-query.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
<?php
/*
Plugin Name: Disable Comment Querying
Description: Disables comment queries
Version: 0.1
License: GPL version 2 or any later version
Author: Mark Jaquith
Author URI: http://coveredwebservices.com/
*/
 
class CWS_Disable_Comments_Query_Plugin {
static $instance;
private $already_ran = false;
 
public function __construct() {
self::$instance = $this;
add_action( 'template_redirect', array( $this, 'template_redirect' ) );
}
 
public function template_redirect() {
add_filter( 'option_require_name_email', array( $this, 'require_name_email' ) );
}
 
/*
This filter is fired in the comments_template() function,
which lacks a suitable way of hooking in early and aborting the comments query
*/
public function require_name_email( $value ) {
if ( !defined( 'DOING_AJAX' ) || !DOING_AJAX )
add_filter( 'query', array( $this, 'comments_query_filter' ) );
return $value;
}
 
/*
Neuter the comments query, to prevent doing double work.
*/
public function comments_query_filter( $query ) {
if ( $this->already_ran )
return $query;
global $wpdb;
$pattern = '#^\s*SELECT\s*\*\s*FROM\s*' . preg_quote( $wpdb->comments, '#' ) .'\s*WHERE\s*comment_post_ID\s*=\s*([0-9]+)\s*#i';
if ( preg_match( $pattern, $query ) ) {
// Neuter the query, while leaving a clue as to what happened
$query = preg_replace( $pattern, 'SELECT * FROM ' . $wpdb->comments . ' WHERE \'neutered\' = \'by CWS Disable Comments Query Plugin\' AND comment_post_ID = $1 ', $query );
$this->already_ran = true;
}
return $query;
}
 
}
 
new CWS_Disable_Comments_Query_Plugin;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.