<?php
/**
 * This is the API for dependency checking for plugins and themes. This way plugins/themes can use this API
 * to check to see if something is loaded or not. If its loaded and the right version
 * passes muster as well, the API will return true.
 * 
 * By default all plugins that are in the plugins list (active or inactive) are regiestered and
 * this API will help plugins/themes detirmine if they can run if it meets the requirements.
 *
 * @package WordPress
 * @subpackage Plugin Dependency
 * @since 2.9
 */

class WP_Plugin_Dependency {
	
	var $item_array = array();
	
	var $item_defaults = array('required' => true, 'oper' => '>=' );
		
	// Public Functions
	/**
	 * 
	 * @return 
	 * @param object $item_id
	 */
	function is_activated($item_id) {
		if ($this->item_array[$item_id]['settings']['activated'])
			return true;
			
		return false;
	}

	/**
	 * 
	 * @return 
	 * @param object $item_id
	 */
	function activate($item_id) {
		global $wp_version;
		
		if (!$this->check($item_id))
			return false;		
		
		// Add "Activate" to the plugins "setting" array.
		$item_settings = $this->item_array[$item_id]['settings'];
		$act = array('activated' => true);
		
		$this->item_array[$item_id]['settings'] = array_merge($item_settings, $act);
		
		return true;
	}

	/**
	 * 
	 * @return 
	 * @param object $item_id
	 */
	function check($item_id) {
		global $wp_version;
		
		$item = $this->_get_item_data($item_id);	//	the name just very sanitized
		
		if (!$item)
			return false;	//	plugin does not exist. FAIL!
		
		$setting = array();
		$wordpress = array();
		$item_dep = array();
		foreach ($item as $item_data => $data) {
			
			if ($item_data == 'settings')
				$setting = $data;
			if ($item_data == 'wordpress')
				$wordpress = $data;
			if ($item_data == 'plugins')
				$item_dep = $data;
		}

		// version needed to run this plugin is passed through this function
		// @todo: Merge WordPress Defaults in-case there are none in the plugin check.
		if (!$this->_wordpress_version_check($wordpress['version'], $wordpress['oper']))
			return false;	//	plugin can not run since the WordPress version does not work.

		//	no plugin dependinces. As long as the WordPress 
		//	version is up-to-date, we can use this plugin/theme!
		if (!is_array($item_dep))
			return true;
		
		foreach ($item_dep as $plugin => $data) {
			
			// @todo: Merge Plugin Defaults in-case there are none in the plugin check.
			if ($data['required'] && !$this->_plugin_version_check($data['version'], $this->item_array[$plugin]['settings']['version'], $data['oper'])) {
				if ($this->check($plugin)) 	// is it even installed?
					return false;
			}
		}
		
		//	if everything passes.. tell it ok!
		return true;	
	}
	
	function register() {
		/**
		 * Major Code Upgrade
		 */
		$temp = array(
			'contact_form_7' => array (
				'settings' => array (
					'type' => 'plugin',
					'version' => '1.10.0.1'
				),
				'wordpress' => array (
					'version' => '2.5',
					'oper' => '>='
					),
				'plugins' => array (
					'really_simple_captcha' => array (
						'required' => false,
						'version' => '1.0',
						'oper' => '>='
					),
					'really' => array (
						'required' => true,
						'recommended' => '0.5',
						'version' => '0.1',
						'oper' => '>='
					),
				)
			),
			'really' => array (
				'settings' => array (
					'type' => 'plugin',
					'version' => '0.3'
				),
				'wordpress' => array (
					'version' => '2.8',
					'oper' => '>='
				)
			),
		);
		
		foreach ($temp as $item_name => $data) {
			$this->item_array[$item_name] = $data;
			// tell the plugin that their checks are register'ed correctly
			do_action('dependency_registered_' . $item_name);
			do_action('dependency_registered', $item_name);	//	catch all action tigger.
		}
		
	}
	
	// Private Functions
	/** 
	 * 
	 * Get the plugin requirments.
	 *
	 * @param string $item_id The plugin sanitized name.
	 * 
	 */
	function _get_item_data($item_id) {
		if (is_array($this->item_array[$item_id]))
			return $this->item_array[$item_id];
		
		return false;
	}
	
	/** 
	 * 
	 * Checks to see if the item we are checking has the 
	 * minuium requirments for this WordPress installation.
	 *
	 * @param string $version The version of WordPress needed for this theme/plugin to work.
	 * @param string $oper How we should compare the version numberes.
	 * @return boolen True if it's allowed, false if it doesn't pass muster.
	 * 
	 */
	function _wordpress_version_check($version, $oper) {
		global $wp_version;
		
		if (version_compare($wp_version, $version, $oper))
			return true;
		else
			return false;
	}

	/** 
	 * 
	 * Checks to see if the item we are checking has the 
	 * minuium requirments for this WordPress installation.
	 *
	 * @param string $version The version of plugins needed for this theme/plugin to work.
	 * @param string $plugin_version The version of Plugins needed for this theme/plugin to work.
	 * @return boolen True if it's allowed, false if it doesn't pass muster.
	 * 
	 */
	function _plugin_version_check($version, $plugin_version, $oper) {
		if (version_compare($plugin_version, $version, $oper))
			return true;
		else
			return false;		
	}

}
	
?>