<?php
/*
Plugin Name: mobileOK Pythia
Plugin URI: http://www.w3.org/2009/11/mobileOKPythia/plugin-wordpress.html
Description: Generates mobileOK content and adapts content based on the properties of the requesting device. 
Version: 1.0
Author: W3C Mobile Web Initiative
Author URI: http://www.w3.org/Mobile
*/

/*  Copyright 2009 W3C (ERCIM, Keio University, MIT CSAIL) (email: public-mobile-dev@w3.org)

	This program is distributed under the W3C Software License [1] 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.
    
    [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
*/

/**
 * This file is the main file of the mobileOK Pythia plug-in for Wordpress.
 * 
 * @author Sylvain Lequeux
 * @author Francois Daoust <fd@w3.org>
 * @package mobileOKPythia
 * @subpackage Wordpress
 * @version $Revision: 1.9 $
 * @link http://www.w3.org/2009/11/mobileOKPythia/plugin-wordpress.html mobileOK Pythia for WordPress
 * @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)
 */


/**
 * Register the functions to execute when the plugin is activated/deactivated.
 */
register_activation_hook('mobileOKPythia/mobileOKPythia.php', 'mobileOKPythiaForWordpress_activate');
register_deactivation_hook('mobileOKPythia/mobileOKPythia.php', 'mobileOKPythiaForWordpress_deactivate');


/**
 * Keep a pointer to the global mobileOKPythia instance used throughout the plugin.
 */
$mobileOKPythiaForWordpress = null;


/**
 * Register the initialization functions that will in turn register
 * the hooks to the actions and filters modified by the mobileOK Pythia
 * plugin.
 * 
 * NB:
 * - "admin_menu" gets executed before "admin_init" and admin options
 * pages must be registered within admin_menu not to end up with
 * access rights problems.
 * 
 * - plugins_loaded gets executed once this file has been included. We can't
 * wait for "init" because template switching relies on the "template" hook
 * that gets called before "init".
 */
add_action('admin_menu', 'mobileOKPythiaForWordpress_admin_init');
add_action('admin_notices', 'mobileOKPythiaForWordpress_admin_notices');
add_action('plugins_loaded', 'mobileOKPythiaForWordpress_init');



/**
 * Checks required pre-requisites before the plugin gets activated.
 * 
 * The method checks that PHP5 is used, that WURFL settings are
 * properly set, and that the code has write access where needed.
 * 
 * @author Francois Daoust <fd@w3.org>
 * @version $Revision: 1.9 $
 * @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)
 */
function mobileOKPythiaForWordpress_activate() {
	update_option('mobileOKPythia_activation_error', '');
	if (version_compare(PHP_VERSION, '5.0.0', '<')) {
	    update_option('mobileOKPythia_activation_error',
	    	 __('This plug-in requires <strong>PHP5</strong>. Earlier versions are not supported, sorry!', 'mobileOKPythia'));
  	}
  	
  	if (!function_exists('imagecreatetruecolor')) {
  		// GD library not detected, no way to resize images
  		update_option('mobileOKPythia_resize_img', 'no');
  	}
}


/**
 * Removes all possible traces of the mobileOK Pythia plugin.
 * 
 * @author Francois Daoust <fd@w3.org>
 * @version $Revision: 1.9 $
 * @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)
 */
function mobileOKPythiaForWordpress_deactivate() {
	// TODO!
}


/**
 * Displays admin notices and in particular plug-in activation errors.
 * 
 * @author Francois Daoust <fd@w3.org>
 * @version $Revision: 1.9 $
 * @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)
 */
function mobileOKPythiaForWordpress_admin_notices() {
	if (get_option('mobileOKPythia_activation_error') != '') {
		echo "<div class='error'><p><strong>"
			. __('mobileOK Pythia activation failed.', 'mobileOKPythia')
			. "</strong></p><p>"
			. get_option('mobileOKPythia_activation_error')
			. "</p><p>"
			. __('De-activate the plug-in to have this message disappear. You may try to re-active the plug-in once the problem is fixed, .', 'mobileOKPythia')
			. "</div>";
	}
}


/**
 * Initializes the admin functionalities of the mobileOK Pythia plugin.
 * 
 * This function is bound to the 'admin_init' action hook and gets
 * called automatically when Wordpress initializes in admin mode.
 * 
 * @author Sylvain Lequeux
 * @author Francois Daoust <fd@w3.org>
 * @version $Revision: 1.9 $
 * @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)
 */
function mobileOKPythiaForWordpress_admin_init() {
	global $mobileOKPythiaForWordpress;
	
	// Include required admin class definition
	require_once(dirname(__FILE__) . '/includes/mobileOKPythiaForWordpressAdmin.php');

	$mobileOKPythiaForWordpress = new mobileOKPythiaForWordpressAdmin();
	$mobileOKPythiaForWordpress->init();
}


/**
 * Initializes the functionalities of the mobileOK Pythia plugin in regular non-admin pages.
 * 
 * This function is bound to the 'init' action hook and gets called automatically when
 * Wordpress initializes in regular non-admin mode.
 * 
 * @author Sylvain Lequeux
 * @author Francois Daoust <fd@w3.org>
 * @version $Revision: 1.9 $
 * @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)
 */
function mobileOKPythiaForWordpress_init() {
	global $mobileOKPythiaForWordpress;
	
	if (!defined('WP_ADMIN')) {
		// Include required class definition
		require_once(dirname(__FILE__) . '/includes/mobileOKPythiaForWordpress.php');
	
		$mobileOKPythiaForWordpress = new mobileOKPythiaForWordpress();
		$mobileOKPythiaForWordpress->init();
	}
}
?>