<?php
/**
 * This file is part of the mobileOKPythia plug-in for Wordpress. It contains the
 * definition of the {@link TranscodingActionDeleteWordpressScripts} class.
 * 
 * @author Sylvain Lequeux
 * @author Francois Daoust <fd@w3.org>
 * @package mobileOKPythia
 * @subpackage Wordpress
 * @version $Revision: 1.4 $
 * @license http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231.html W3C Software Notice and License
 * @copyright Copyright (c) 2009, W3C (MIT, ERCIM, Keio)
 */


/**
 * Include the {@link TranscodingAction} base class definition.
 */
require_once(dirname(__FILE__) . '/../common/transcoding/transcodingaction.php');



/**
 * Transcoding action that removes registered scripts in Wordpress.
 * 
 * The action should be hooked up to the 'wp_headers' Wordpress action.
 * 
 * The DDR property reference that the transcoding action uses to tell whether
 * the requesting device supports scripting or not may be defined in a
 * "script_support" option through a call to {@link TranscodingAction::setOption()}.
 * The action uses the scriptSupport property of the DDR Core Vocabulary by default.
 * 
 * @author Sylvain Lequeux
 * @author Francois Daoust <fd@w3.org>
 * @package mobileOKPythia
 * @subpackage Wordpress
 * @version $Revision: 1.4 $
 * @license http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231.html W3C Software Notice and License
 * @copyright Copyright (c) 2009, W3C (MIT, ERCIM, Keio)
 */
class TranscodingActionDeleteWordpressScripts extends TranscodingAction {
	
	/**
	 * Removes all registered scripts when the requesting device
	 * does not support scripting.
	 * 
	 * @param $content mixed The array of registered scripts
	 * @param $evidence Evidence The evidence that identifies the requesting device.
	 * @return string The transcoded content.
	 * @exception SystemException The evidence is not valid.
	 */
	public function apply($content, $evidence){
		$this->initPropertyValues($evidence);
		
		$this->initProperty('script_support',
			'scriptSupport',
			TranscodingAction::$CORE_VOCABULARY,
			'webBrowser');
			
		$property = $this->getOption('script_support');
		$support_scripting = $this->getPropertyValuePr($property);
		
		if(!isset($support_scripting)
		|| in_array("ecmascript-MP", $support_scripting->getEnumeration())){
		  	return $content;
		}
		
		// Parse and remove registered scripts
		foreach($content as $name=>$script){
			// The scripts are stored in two arrays
			if($name == 'registered'){
				foreach($script as $attribute => $detail){
					wp_deregister_script($detail->handle);
				}
			}
			if($name == 'queue'){
				// TODO: check the following ($detail->handle?)
				foreach($script as $attribute => $detail){
					wp_deregister_script($detail);
				}
			}
		}
		
		return $content;
	}
}

?>