<?php
/*
Plugin Name: SJ Object Cache
Plugin URI: http://blog.sjinks.pro/wp-file-cache/
Description: Object Cache for WordPress - replacement for the standard WP_Object_Cache
Author: Vladimir Kolesnikov
Version: 1.0
Author URI: http://blog.sjinks.pro/
*/

	require_once(WP_PLUGIN_DIR . '/sj-object-cache/lib/class.CacheFactory.php');

	class SjObjectCache
	{
		protected $options;

		public static function instance()
		{
			static $self = false;

			if (!$self) {
				$self = new SjObjectCache();
			}

			return $self;
		}

		private function __construct()
		{
			add_action('init', array($this, 'init'));
			if (is_admin()) {
				add_action('activate_sj-object-cache/core.php',   array($this, 'activate'));
				add_action('deactivate_sj-object-cache/core.php', array($this, 'deactivate'));

				add_action('admin_menu', array($this, 'admin_menu'));
				add_action('admin_init', array($this, 'admin_init'));
			}
		}

		public function init()
		{
			$this->loadOptions();
			if (is_admin()) {
				load_plugin_textdomain('sjobjectcache', PLUGINDIR . '/sj-object-cache/lang');
			}
		}

		public function admin_init()
		{
			require_once(WP_PLUGIN_DIR . "/sj-object-cache/lib/class.PageHandlers.php");
		}

		public function loadOptions()
		{
			static $done = false;

			if (false === $done) {
				$done     = true;
				$defaults = array(
					'enabled'       => 1,
					'persist'       => 1,
					'nonpersistent' => '',
					'engine'        => 'basecache',
					'maxttl'        => 3600,
					'options'       => array(),
				);

				global $__sjfc_options;

				$options = @unserialize($__sjfc_options);
				if (!is_array($options)) {
					$options = array();
				}

				$update = false;
				foreach ($defaults as $k => $v) {
					if (!isset($options[$k])) {
						$options[$k] = $v;
						$update = true;
					}
				}

				foreach ($options as $k => $v) {
					if (!isset($defaults[$k])) {
						unset($options[$k]);
						$update = true;
					}
				}

				if ($update) {
					$this->writeOptions($options);
				}

				$this->options = $options;
			}
		}

		public function writeOptions($options)
		{
			$data    = file_get_contents(WP_PLUGIN_DIR . '/sj-object-cache/object-cache.php');
			$options = var_export(serialize($options), 1);

			$matches = array();
			$data = preg_replace('/\\$GLOBALS\\[\'__sjfc_options\'\\]\\s*=\\s*\'a:.*$/m', "\$GLOBALS['__sjfc_options'] = {$options};", $data);
			$f    = fopen(ABSPATH . 'wp-content/object-cache.php', 'w');

			if (is_resource($f)) {
				fwrite($f, $data);
				fclose($f);
				return true;
			}

			return false;
		}

		public function activate()
		{
			$this->loadOptions();
		}

		public function deactivate()
		{
			if (!unlink(ABSPATH . 'wp-content/object-cache.php')) {
				die("WARNING: failed to delete <code>" . ABSPATH . "wp-content/object-cache.php</code><br/>Please delete this file ASAP.");
			}
		}

		public function admin_menu()
		{
			add_menu_page(__('SJ Object Cache', 'sjobjectcache'), __('SJ Object Cache', 'sjobjectcache'), 'manage_options', 'sj-object-cache/pages/options-object-cache.php');
			add_submenu_page('sj-object-cache/pages/options-object-cache.php', __('Generic Options', 'sjobjectcache'), __('Generic Options', 'sjobjectcache'), 'manage_options', 'sj-object-cache/pages/options-object-cache.php');

			$engines = SjCacheFactory::getEngines();
			if (!empty($engines)) {
				foreach ($engines as $engine) {
					if (0 != $engine[2]) {
						add_submenu_page('sj-object-cache/pages/options-object-cache.php', sprintf(__('%1$s Options', 'sjobjectcache'), $engine[3]), $engine[3], 'manage_options', "sj-object-cache/pages/options.{$engine[1]}.php");
					}
				}
			}
		}

		public function getOptions()
		{
			$this->loadOptions();
			return $this->options;
		}

	}

	SjObjectCache::instance();
?>