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 3 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, see . */ require_once ( AK_LIB . '/plugins.php' ); require_once ( AK_CLASSES . '/abstract/module.php' ); /** * Abtract class to be used as a plugin template. * Must be implemented before using this class and it's recommended to prefix the class to prevent collissions. * There are some special functions thay can declared (as protected) in implementations to perform main actions: * - activate: (Protected) Actions to run when activating the plugin. * - _deactivate: (Hook, must be public) Actions to run when deactivating the plugin. * - update: (Protected) Actions to update the plugin to a new version. (Updating version on DB is done after this). * Takes plugin running version as a parameter. * - setDefaults: (Protected) Fills the $defaults class var with the default settings. * - init: (Protected) Actions to run when plugins initialization is performed (In plugins loaded). * - widgetsInit: (Protected) Actions to init plugin widgets (In widgets_init). * - startUp: (Protected) Actions to run at system startup (before plugins are loaded). * - _adminMenus: (Hook, must be public) Set the menus in WordPress Dashboard. * * @uses plugins.php * @author Jordi Canals * @package AOC * @subpackage Library * @link http://alkivia.org */ abstract class akPluginAbstract extends akModuleAbstract { /** * Class constructor. * Calls the implementated method 'startUp' if it exists. This is done at plugins loading time. * Prepares admin menus by seting an action for the implemented method '_adminMenus' if it exists. * * @param string $mod_file Full main plugin's filename (absolute to root). * @param string $ID Plugin short name (known as plugin ID). * @return spostsPlugin|false The plugin object or false if not compatible. */ public function __construct( $mod_file, $ID = '' ) { parent::__construct('plugin', $ID, $mod_file); if ( $this->isCompatible() ) { // Activation and deactivation hooks. register_activation_hook($this->mod_file, array($this, 'activate')); register_deactivation_hook($this->mod_file, array($this, 'deactivate')); add_action('plugins_loaded', array($this, 'update')); } } /** * Fires on plugin activation. * @return void */ abstract protected function pluginActivate (); /** * Fires on plugin deactivation. * @return void */ abstract protected function pluginDeactivate (); /** * Updates the plugin to a new version. * @param string $version Old plugin version. * @return void */ abstract protected function pluginUpdate ( $version ); /** * Fires when plugins have been loaded. * @return void */ abstract protected function pluginsLoaded (); /** * Fires on Widgets init. * @return void */ abstract protected function registerWidgets (); /** * Activates the plugin. Only runs on first activation. * Saves the plugin version in DB, and calls the 'pluginActivate' method. * * @uses do_action() Calls 'ak_activate__plugin' action hook. * @hook register_activation_hook * @access private * @return void */ final function activate() { $this->pluginActivate(); // Save options and version $this->cfg->saveOptions($this->ID); add_option($this->ID . '_version', $this->mod_data['Version']); do_action('ak_activate_' . $this->ID . '_plugin'); } /** * Deactivates the plugin. * * @uses do_action() Calls 'ak_deactivate__plugin' action hook. * @hook register_deactivation_hook * @access private * @return void */ final function deactivate() { $this->pluginDeactivate(); do_action('ak_deactivate_' . $this->ID . '_plugin'); } /** * Init the plugin (In action 'plugins_loaded') * Here whe call the 'update' and 'init' functions. This is done after the plugins are loaded. * Also the plugin version and settings are updated here. * * @hook action plugins_loaded * @access private * @return void */ final function update() { // First, check if the plugin needs to be updated. if ( $this->needs_update ) { $version = get_option($this->ID . '_version'); $this->pluginUpdate($version); $this->cfg->saveOptions($this->ID); update_option($this->ID . '_version', $this->mod_data['Version']); do_action('ak_' . $this->ID . '_updated'); } $this->pluginsLoaded(); } /** * Inits the widgets (In action 'widgets_init') * Before loading the widgets, we check that standard sidebar is present. * * @hook action 'widgets_init' * @return void */ final function widgetsInit() { if ( class_exists('WP_Widget') && function_exists('register_widget') && function_exists('unregister_widget') ) { $this->registerWidgets(); } else { add_action('admin_notices', array($this, 'noSidebarWarning')); } } /** * Checks if the plugin is compatible with the current WordPress version. * If it's not compatible, sets an admin warning. * * @return boolean Plugin is compatible with this WordPress version or not. */ final protected function isCompatible() { global $wp_version; if ( version_compare($wp_version, $this->mod_data['Requires'] , '>=') ) { return true; } elseif ( ! has_action('admin_notices', array($this, 'noCompatibleWarning')) ) { add_action('admin_notices', array($this, 'noCompatibleWarning')); } return false; } /** * Shows a warning message when the plugin is not compatible with current WordPress version. * This is used by calling the action 'admin_notices' in isCompatible() * * @hook action admin_notices * @access private * @return void */ final function noCompatibleWarning() { $this->loadTranslations(); // We have not loaded translations yet. echo '

' . __('Warning:', $this->ID) . ' ' . sprintf(__('The active plugin %s is not compatible with your WordPress version.', $this->ID), '«' . $this->mod_data['Name'] . ' ' . $this->mod_data['Version'] . '»') . '

' . sprintf(__('WordPress %s is required to run this plugin.', $this->ID), $this->mod_data['Requires']) . '

'; } /** * Shows an admin warning when not using the WordPress standard sidebar. * This is done by calling the action 'admin_notices' in isStandardSidebar() * * @hook action admin_notices * @access private * @return void */ final function noSidebarWarning() { $this->loadTranslations(); // We have not loaded translations yet. echo '

' . __('Warning:', $this->ID) . ' ' . __('Standard sidebar functions are not present.', $this->ID) . '

' . sprintf(__('It is required to use the standard sidebar to run %s', $this->ID), '«' . $this->mod_data['Name'] . ' ' . $this->mod_data['Version'] . '»') . '

'; } }