From 5ddb5d986c870b9f5598f108306f10e117c4bcc2 Mon Sep 17 00:00:00 2001
From: Erick Hitter <ehitter@gmail.com>
Date: Tue, 25 Feb 2014 22:19:08 -0800
Subject: [PATCH] Overhaul plugin's settings UI, adding validation to options
 and generally cleaning up the code.

---
 wp-redis-cache/wp-redis-cache.php | 139 ++++++++++++++++++++----------
 1 file changed, 93 insertions(+), 46 deletions(-)

diff --git a/wp-redis-cache/wp-redis-cache.php b/wp-redis-cache/wp-redis-cache.php
index 7aef7b8..cd2bdef 100644
--- a/wp-redis-cache/wp-redis-cache.php
+++ b/wp-redis-cache/wp-redis-cache.php
@@ -114,61 +114,108 @@ We do this because WordPress is no longer in charge of displaying our posts.  Re
 
 */
 
-/**
- * Register plugin options page
- */
-function wp_redis_cache_add_ui() {
-	add_options_page( 'WP Redis Cache', 'WP Redis Cache', 'manage_options', 'wp-redis-cache', 'wp_redis_cache_render_options_screen' );
-}
-add_action( 'admin_menu', 'wp_redis_cache_add_ui' );
-
-function wp_redis_cache_render_options_screen() {
-	?>
-	<div class='wrap'>
-	<h2>WP-Redis Options</h2>
-	<form method="post" action="options.php">
-	<?php wp_nonce_field('update-options') ?>
+class WP_Redis_Cache {
+	// Hold singleton instance
+	private static $__instance = null;
+
+	// Regular class variables
+	private $ns = 'wp-redis-cache';
+
+	/**
+	 *
+	 */
+	public static function get_instance() {
+		if ( ! is_a( self::$__instance, __CLASS__ ) ) {
+			self::$__instance = new self;
+		}
+
+		return self::$__instance;
+	}
 
-	<p>This plugin does not work out of the box and requires additional steps.<br />
-	Please follow these install instructions: <a target='_blank' href='https://github.com/BenjaminAdams/wp-redis-cache'>https://github.com/BenjaminAdams/wp-redis-cache</a></p>
+	/**
+	 * Register necessary actions
+	 *
+	 * @return null
+	 */
+	private function __construct() {
+		add_action( 'admin_init', array( $this, 'register_options' ) );
+		add_action( 'admin_menu', array( $this, 'register_ui' ) );
+		add_action( 'transition_post_status', array( $this, 'flush_cache' ), 10, 3 );
+	}
 
-	<p>If you do not have Redis installed on your machine this will NOT work! </p>
+	/**
+	 * Register plugin's settings for proper sanitization
+	 *
+	 * @return null
+	 */
+	public function register_options() {
+		register_setting( $this->ns, 'wp-redis-cache-seconds', 'absint' );
+		register_setting( $this->ns, 'wp-redis-cache-unlimited', 'is_bool' );
+	}
 
-	<p><strong>Seconds of Caching:</strong><br />
-	How many seconds would you like to cache?  *Recommended 12 hours or 43200 seconds <br />
-	<input type="text" name="wp-redis-cache-seconds" size="45" value="<?php echo get_option('wp-redis-cache-seconds'); ?>" /></p>
+	/**
+	 * Register plugin options page
+	 *
+	 * @action admin_menu
+	 * @return null
+	 */
+	public function register_ui() {
+		add_options_page( 'WP Redis Cache', 'WP Redis Cache', 'manage_options', $this->ns, array( $this, 'render_ui' ) );
+	}
 
-	<p><strong>Cache unlimeted:</strong><br />
-		If this options set the cache never expire. This option overiedes the setting "Seconds of Caching"<br />
-	<input type="checkbox" name="wp-redis-cache-unlimited" size="45" value="true" <?php checked('true', get_option('wp-redis-cache-unlimited')); ?>/></p>
+	/**
+	 * Render plugin settings screen
+	 *
+	 * @return string
+	 */
+	public function render_ui() {
+		?>
+		<div class='wrap'>
+		<h2>WP Redis Options</h2>
+		<form method="post" action="options.php">
+		<?php settings_fields( $this->ns ); ?>
 
-	<p><input type="submit" name="Submit" value="Update Options" /></p>
-	<p><input type="button" id="WPRedisClearCache" name="WPRedisClearCache" value="Clear Cache"></p>
-	<input type="hidden" name="action" value="update" />
-	<input type="hidden" name="page_options" value="wp-redis-cache-seconds,wp-redis-cache-unlimited" />
+			<p>This plugin does not work out of the box and requires additional steps.<br />Please follow these install instructions: <a target='_blank' href='https://github.com/BenjaminAdams/wp-redis-cache'>https://github.com/BenjaminAdams/wp-redis-cache</a></p>
 
-	</form>
-	</div>
-	<?php
-}
+			<p>If you do not have Redis installed on your machine this will NOT work!</p>
 
-/**
- *
- */
-function wp_redis_cache_refresh_on_publish( $new, $old, $post ) {
-	if ( in_array( 'publish', array( $new, $old ) ) ) {
-		$permalink = get_permalink( $post->ID );
+			<p><strong>Duration of Caching in Seconds:</strong><br />How many seconds would you like to cache individual pages?  *Recommended 12 hours or 43200 seconds <br />
+			<input type="text" name="wp-redis-cache-seconds" size="45" value="<?php echo (int) get_option( 'wp-redis-cache-seconds' ); ?>" /></p>
 
-		include_once dirname( __FILE__ ) . '/predis5.2.php';  //we need this to use Redis inside of PHP
-		$redis = new Predis_Client();
+			<p><strong>Cache without expiration:</strong><br />If this option is set, the cache never expire. This option overides the setting <em>Duration of Caching in Seconds</em>.<br />
+			<input type="checkbox" name="wp-redis-cache-unlimited" size="45" value="true" <?php checked( 'true', (bool) get_option( 'wp-redis-cache-unlimited' ) ); ?>/></p>
 
-		$redis_key = md5( $permalink );
-		$redis->del( $redis_key );
+			<p><?php submit_button(); ?></p>
+		</form>
+		</div>
+		<?php
+	}
 
-		//refresh the front page
-		$front_page = get_home_url( '/' );
-		$redis_key = md5( $front_page );
-		$redis->del( $redis_key );
+	/**
+	 * On publish, purge cache for individual entry and the homepage
+	 *
+	 * @param string $new_status
+	 * @param string $old_status
+	 * @param object $post
+	 * @action transition_post_status
+	 * @return null
+	 */
+	public function flush_cache( $new_status, $old_status, $post ) {
+		if ( in_array( 'publish', array( $new_status, $old_status ) ) ) {
+			$permalink = get_permalink( $post->ID );
+
+			include_once dirname( __FILE__ ) . '/predis5.2.php'; // we need this to use Redis inside of PHP
+			$redis = new Predis_Client();
+
+			$redis_key = md5( $permalink );
+			$redis->del( $redis_key );
+
+			//refresh the front page
+			$front_page = get_home_url( '/' );
+			$redis_key = md5( $front_page );
+			$redis->del( $redis_key );
+		}
 	}
 }
-add_action('transition_post_status', 'wp_redis_cache_refresh_on_publish',10,3);
+
+WP_Redis_Cache::get_instance();
-- 
GitLab