<?php
/*
Plugin Name: table optimizer
Plugin URI: 
Description: optimize table
Version: 0.0.2
Author: wokamoto
Author URI: http://dogmap.jp/
Text Domain: optimizer
Domain Path: lang

 Released under the GPL license
  http://www.gnu.org/copyleft/gpl.html

  Copyright (c) 2009 - wokamoto - wokamoto1973@gmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

class OptimizeTable {
	const OPTIMIZER_INTERVAL = 24 * 60;
	const OPTIMIZER_SCHEDULE_HANDLER = 'optimize_table';

	// Constructor
	public function __construct() {
		if ( !$this->schedule_enabled() )
			$this->schedule_single_event(0);
		add_action(self::OPTIMIZER_SCHEDULE_HANDLER, array(&$this, 'optimize_table'));
	}

	public function optimize_table(){
		global $wpdb, $table_prefix;

		$tables = $wpdb->get_col('SHOW TABLES');
		$pattern = '/^'. preg_quote($wpdb->prefix) . '/i';
		foreach ( $tables as $table ) {
			if ( preg_match( $pattern, $table ) ) {
				$wpdb->query("OPTIMIZE TABLE $table");
			}
		}

		$time_interval = self::OPTIMIZER_INTERVAL;
		$this->schedule_single_event($time_interval);
	}

	private function _upgrade_cron_array($cron) {
		if ( function_exists('_upgrade_cron_array') ) {
			return _upgrade_cron_array($cron);
		} else {
			if ( isset($cron['version']) && 2 == $cron['version'])
				return $cron;

			$new_cron = array();

			foreach ( (array) $cron as $timestamp => $hooks) {
				foreach ( (array) $hooks as $hook => $args ) {
					$key = md5(serialize($args['args']));
					$new_cron[$timestamp][$hook][$key] = $args;
				}
			}

			$new_cron['version'] = 2;
			update_option( 'cron', $new_cron );
			return $new_cron;
		}
	}

	private function _get_cron_array() {
		if ( function_exists('_get_cron_array') ) {
			return _get_cron_array();
		} else {
			$cron = get_option('cron');
			if ( ! is_array($cron) )
				return false;

			if ( !isset($cron['version']) )
				$cron = $this->_upgrade_cron_array($cron);

			unset($cron['version']);

			return $cron;
		}
	}

	// get wp-cron schedule
	private function _get_schedule($schedule_procname = self::OPTIMIZER_SCHEDULE_HANDLER) {
		$schedule = array(
			'procname' => '' ,
			'enabled' => FALSE ,
			'time' => '' ,
		);

		$crons = $this->_get_cron_array();
		if ( !empty($crons) ) {
			foreach ( $crons as $time => $tasks ) {
				foreach ( $tasks as $procname => $task ) {
					if ($procname === $schedule_procname) {
						$schedule['procname'] = $procname;
						$schedule['time'] = $time;
						$schedule['enabled'] = true;
						break;
					}
				}
				if ($schedule['enabled']) break;
			}
			unset($procname); unset($task);
			unset($time); unset ($tasks);
		}
		unset($crons);

		return ($schedule);
	}

	public function schedule_single_event($time_interval = self::OPTIMIZER_INTERVAL) {
		return (wp_schedule_single_event(time() + $time_interval * 60, self::OPTIMIZER_SCHEDULE_HANDLER));
	}

	public function schedule_enabled($schedule_procname = self::OPTIMIZER_SCHEDULE_HANDLER) {
		$schedule = $this->_get_schedule($schedule_procname);
		return ($schedule['enabled']);
	}
}

new OptimizeTable();
?>
