Created
September 29, 2013 14:19
-
-
Save thefuxia/6752917 to your computer and use it in GitHub Desktop.
T5 Enforce Open Comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php # -*- coding: utf-8 -*- | |
/** | |
* Plugin Name: T5 Enforce Open Comments | |
* Description: Override auto-closing for individual posts | |
* Plugin URI: http://toscho.de | |
* Version: 2013.09.29 | |
* Author: Thomas Scholz | |
* Author URI: http://toscho.de | |
* Licence: MIT | |
* License URI: http://opensource.org/licenses/MIT | |
* Textdomain: plugin_t5_enforce_open_comments | |
* Domain Path: /languages | |
*/ | |
add_action( 'wp_loaded', 't5_efoc_init' ); | |
function t5_efoc_init() | |
{ | |
if ( ! get_option( 'close_comments_for_old_posts' ) ) | |
return; // haha | |
new T5_Enforce_Open_Comments( '_t5_efoc' ); | |
} | |
class T5_Enforce_Open_Comments | |
{ | |
protected $key, $post_types; | |
/** | |
* Constructor. | |
* | |
* @wp-hook wp_loaded | |
* @param string $key Post meta field name | |
*/ | |
public function __construct( $key ) | |
{ | |
$this->key = $key; | |
// this is a core filter. we have to use it to catch custom post types | |
$this->post_types = apply_filters( | |
'close_comments_for_post_types', | |
array( 'post' ) | |
); | |
$this->register_hooks(); | |
} | |
/** | |
* Add a checkbox to post edit screen under Discussion. | |
* | |
* @wp-hook post_comment_status_meta_box-options | |
* @param object $post | |
* @return void | |
*/ | |
public function show_checkbox( $post ) | |
{ | |
if ( ! isset ( $post->post_type ) | |
or ! in_array( $post->post_type, $this->post_types ) | |
) | |
return; | |
printf( | |
'<br /><label for="%1$s"> | |
<input type="checkbox" value="1" id="%1$s" name="%1$s" class="selectit" %2$s/> %3$s | |
</label>', | |
$this->key, | |
checked( 1, $this->get_status( $post->ID ), FALSE ), | |
__( 'Enforce open comments', 'plugin_t5_enforce_open_comments' ) | |
); | |
} | |
/** | |
* Save post meta field. | |
* | |
* @wp-hook save_post | |
* @param int $post_id | |
* @return boolean For tests only, WP ignores any return value here. | |
*/ | |
public function save_status( $post_id ) | |
{ | |
// AJAX autosave | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) | |
return FALSE; | |
// Some other POST request | |
if ( ! isset ( $_POST[ 'post_type' ] ) ) | |
return FALSE; | |
// Missing capability | |
if ( ! current_user_can( 'edit_' . $_POST[ 'post_type' ], $post_id ) ) | |
return FALSE; | |
// Checkbox successfully clicked | |
if ( isset ( $_POST[ $this->key ] ) and '1' === $_POST[ $this->key ] ) | |
return update_post_meta( $post_id, $this->key, 1 ); | |
// Checkbox deselected | |
return delete_post_meta( $post_id, $this->key ); | |
} | |
/** | |
* Change comment and ping status for a post. | |
* | |
* @wp-hook the_posts | |
* @param array $posts | |
* @param WP_Query $query | |
* @return array | |
*/ | |
public function filter_the_posts( $posts, $query ) | |
{ | |
if ( empty ( $posts ) || ! $query->is_singular() ) | |
return $posts; | |
if ( ! in_array( $posts[ 0 ]->post_type, $this->post_types ) ) | |
return $posts; | |
if ( ! $this->get_status( $posts[ 0 ]->ID ) ) | |
return $posts; | |
$posts[0]->comment_status = 'open'; | |
$posts[0]->ping_status = 'open'; | |
return $posts; | |
} | |
/** | |
* Change status for comments and pings. | |
* | |
* @wp-hook comments_open | |
* @wp-hook pings_open | |
* @param boolean $open | |
* @param int $post_id | |
* @return boolean | |
*/ | |
public function filter_comments_and_pings( $open, $post_id ) | |
{ | |
if ( ! $post_id ) // is sometimes NULL | |
$post_id = get_the_ID(); | |
if ( $this->get_status( $post_id ) ) | |
return TRUE; | |
return $open; | |
} | |
/** | |
* Register callbacks for actions and filters. | |
* | |
* @wp-hook wp_loaded | |
* @return void | |
*/ | |
protected function register_hooks() | |
{ | |
add_action( | |
'post_comment_status_meta_box-options', | |
array ( $this, 'show_checkbox' ) | |
); | |
add_action( | |
'save_post', | |
array ( $this, 'save_status' ) | |
); | |
add_filter( | |
'comments_open', | |
array ( $this, 'filter_comments_and_pings' ), | |
11, | |
2 | |
); | |
add_filter( | |
'pings_open', | |
array ( $this, 'filter_comments_and_pings' ), | |
11, | |
2 | |
); | |
add_filter( | |
'the_posts', | |
array ( $this, 'filter_the_posts' ), | |
11, | |
2 | |
); | |
} | |
/** | |
* Get enforced comment status for a post. | |
* | |
* @param int $post_id | |
* @return int | |
*/ | |
protected function get_status( $post_id ) | |
{ | |
return get_post_meta( $post_id, $this->key, TRUE ) ? 1 : 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment