<?php

class SjObjectCache_PageHandlers
{
	public static function instance()
	{
		static $self = false;
		if (false === $self) {
			$self = new SjObjectCache_PageHandlers();
		}

		return $self;
	}

	private function __construct()
	{
		add_action('load-sj-object-cache/pages/options-object-cache.php', array($this, 'load_generic_options_page'));
		add_action('admin_post_save_sjoc_generic_options',                array($this, 'admin_post_save_sjoc_generic_options'));
		add_action('admin_post_purge_sjoc_cache',                         array($this, 'admin_post_purge_sjoc_cache'));

		$engines = SjCacheFactory::getEngines();
		if (false == empty($engines)) {
			foreach ($engines as $id => $engine) {
				if (0 != $engine[2]) {
					add_action("load-sj-object-cache/pages/options.{$engine[1]}.php", array($this, 'load_ce_options'));
					add_action("admin_post_save_sjoc_options_{$id}",                  array($this, 'save_ce_options'));
				}
			}
		}
	}

	public function load_ce_options()
	{
		global $params;
		if (!is_array($params)) {
			$params = array();
		}

		$params['message'] = null;
		$params['error']   = null;

		if (!empty($_GET['message'])) {
			$params['message'] = __("The settings have been updated.", 'sjobjectcache');
		}

		if (!empty($_GET['error'])) {
			$params['error'] = sprintf(
			__("Unable to write to <code>%1\$s</code>. Please make sure that it is writable by the server.", 'sjobjectcache'),
			ABSPATH . 'wp-content/object-cache.php'
			);
		}

		$options = SjObjectCache::instance()->getOptions();
		$matches = array();

		if (preg_match('!^sj-object-cache/pages/options\\.([^.]+)\\.php$!', stripslashes($_GET['page']), $matches)) {
			$file = $matches[1];
			unset($matches);
			$engines = SjCacheFactory::getEngines();
			foreach ($engines as $id => $engine) {
				if ($engine[1] == $file) {
					$params['engine']  = $id;
					$params['options'] = (isset($options['options'][$id])) ? $options['options'][$id] : array();
					break;
				}
			}
		}
	}

	public function load_generic_options_page()
	{
		global $params;
		if (!is_array($params)) {
			$params = array();
		}

		$options = SjObjectCache::instance()->getOptions();

		$params['modules']       = SjCacheFactory::getEngines();
		$params['enabled']       = $options['enabled'];
		$params['nonpersistent'] = $options['nonpersistent'];
		$params['persist']       = $options['persist'];
		$params['engine']        = $options['engine'];
		$params['maxttl']        = $options['maxttl'];
		$params['message']       = null;
		$params['error']         = null;

		if (!empty($_GET['message'])) {
			switch ((int)$_GET['message']) {
				case 1:
					$params['message'] = __("The settings have been updated.", 'sjobjectcache');
					break;

				case 2:
					$params['message'] = __("The cache has been cleared.", 'sjobjectcache');
					break;
			}
		}

		if (!empty($_GET['error'])) {
			$params['error'] = sprintf(
			__("Unable to write to <code>%1\$s</code>. Please make sure that it is writable by the server.", 'sjobjectcache'),
			ABSPATH . 'wp-content/object-cache.php'
			);
		}
	}

	public function admin_post_save_sjoc_generic_options()
	{
		check_admin_referer('configure-objectcache');

		$options    = SjObjectCache::instance()->getOptions();
		$old_engine = $options['engine'];
		foreach ($options as $key => $value) {
			if ('options' != $key) {
				if (isset($_POST['options'][$key])) {
					$options[$key] = stripslashes($_POST['options'][$key]);
				}
				else {
					$options[$key] = '';
				}
			}
		}

		$options['nonpersistent'] = str_replace(' ', '', $options['nonpersistent']);

		if (SjObjectCache::instance()->writeOptions($options)) {
			$redir = array('message' => 1);
		}
		else {
			$redir = array('error' => 1);
		}

		if ($old_engine != $options['engine']) {
			wp_cache_flush();
		}

		wp_redirect(add_query_arg($redir, $_POST['_wp_http_referer']));
	}

	public function admin_post_purge_sjoc_cache()
	{
		check_admin_referer('purge-objectcache');
		wp_cache_flush();
		wp_redirect(add_query_arg(array('message' => 2), $_POST['_wp_http_referer']));
	}

	public function save_ce_options()
	{
		$data = stripslashes_deep($_POST);
		$engine = $data['engine'];
		check_admin_referer("sjobjectcache-config_{$engine}");

		$options = SjObjectCache::instance()->getOptions();
		$options['options'][$engine] = $data['options'];

		if (SjObjectCache::instance()->writeOptions($options)) {
			$redir = array('message' => 1);
		}
		else {
			$redir = array('error' => 1);
		}

		wp_redirect(add_query_arg($redir, $_POST['_wp_http_referer']));
	}
}

SjObjectCache_PageHandlers::instance();

if (!function_exists('esc_attr_e')) {
	function esc_attr_e($text, $domain = 'default') { echo esc_attr(translate($text, $domain)); }
}

if (!function_exists('esc_attr')) {
	function esc_attr($text) { return attribute_escape($text); }
}
?>