From: Chuck Scott Date: Tue, 31 Mar 2015 20:43:30 +0000 (-0400) Subject: Added general structure for plugin X-Git-Tag: v1.0.1~10 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=1c74c52b35ffba65c2ba53a67082c605fe4efa45;p=WP-Plugins%2Fglm-woocommerce-merchant-e-solutions-gateway.git Added general structure for plugin --- diff --git a/activate.php b/activate.php new file mode 100644 index 0000000..0bc007b --- /dev/null +++ b/activate.php @@ -0,0 +1,104 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @release activate.php,v 1.0 2014/10/31 19:31:47 cscott Exp $ + * @link http://dev.gaslightmedia.com/ + */ + +// Load glmPluginSupport class +require_once (GLM_MES_PLUGIN_PATH . '/classes/glmPluginSupport.php'); + +/* + * This class performs all necessary additional work when this + * plugin is activated. + * + * Currently the only actions are to add role capability to display and modify + * prototypes. + */ +class glmMesPluginActivate extends glmPluginSupport +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + /** + * Plugin Configuration Data + * + * @var $config + * @access public + */ + public $config; + + /* + * Constructor + * + * Note that the $noDatabaseCheck is used to access the database versions + * without triggering a database check. + * + * Performs all the work for this model + */ + public function __construct ($wpdb, $config) + { + + // Make sure the current user has this capability + if (! current_user_can('activate_plugins')) { + $this->addNotice("Interesting, you don't have permission to activate plugins."); + die(); + } + + // Save WordPress Database object + $this->wpdb = $wpdb; + + // Save plugin configuration object + $this->config = $config; + + } + + /* + * Add a role capability to all current roles + * + * @param string $capability Name of capability to add + * @param string $default Whether capability should be on by default + * + * @return void + * @access private + */ + private function addRoleCapability ($capability, $default) + { + // Get list of role objects + $roleObjects = $GLOBALS['wp_roles']->role_objects; + + // Get list of roles we can edit + $roles = get_editable_roles(); + + // For each role object + foreach ($roleObjects as $key => $role) { + + // Check if the role exists in list of editable roles and capability + // does not exist + if (isset($roles[$key]) && ! isset($role->capabilities[$capability])) { + + // Add the role + $role->add_cap($capability, $default); + } + } + } + + + +} + +?> \ No newline at end of file diff --git a/classes/glmPluginSupport.php b/classes/glmPluginSupport.php new file mode 100644 index 0000000..7be41a5 --- /dev/null +++ b/classes/glmPluginSupport.php @@ -0,0 +1,118 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @release glmPluginSupport.php,v 1.0 2014/10/31 19:31:47 cscott Exp $ + * @link http://dev.gaslightmedia.com/ + */ + +/* + * This class provides some standard methods used by this plugin + */ +class GlmPluginSupport +{ + + /* + * Add a message to the 'glmMembersAdminNotices' option for output later. + * + * Messages that don't have a $type specified are displayed in + * the admin WordPress message area. + * + * Messages that have a $type are for the debug window and are categorized + * as follows and in this order in the debug window + * Alert Placed sequentially in the Alerts list + * Process Placed sequentially in the process list + * DataBlock Placed in a colored data block. Requires a $title + * + * @param string $message + * @param string $type + * @param string $title + * + * @return void + * @access public + */ + public static function addNotice ($message, $type = false, $title = false) + { + + switch($type) { + + case 'Alert'; + $alerts = get_option('glmMembersAdminNoticeAlerts'); + $alerts[] = $message; + update_option('glmMembersAdminNoticeAlerts', $alerts); + break; + + case 'Process'; + $process = get_option('glmMembersAdminNoticeProcess'); + $process[] = $message; + update_option('glmMembersAdminNoticeProcess', $process); + break; + + case 'DataBlock'; + $dataBlocks = get_option('glmMembersAdminNoticeDataBlocks'); + $dataBlocks[] = array('data' => $message, 'title' => $title); + update_option('glmMembersAdminNoticeDataBlocks', $dataBlocks); + break; + + default; + $notices = get_option('glmMembersAdminNotices'); + $notices[] = $message; + update_option('glmMembersAdminNotices', $notices); + break; + + } + + } + + /* + * Check if this is a second call to the activation hook by WordPress to activate this plugin. + * + * (Yea, I know that's stupid behavior, but there's nothing I can do about it.) + * It tells us this is happening by setting the 'action' GET parameter to 'error_scrape', + * which is also stupid. + * + * In this case, we don't want to try again, so output any saved notices from the first pass + * and then exit to tell WordPress the plugin didn't install. + * + * @return void + * @access public + */ + public function checkErrorScrape() + { + + // Check for 'action' = 'error_scrape', which indicates a second call from WordPress due to an error. + if (isset($_GET['action']) && $_GET['action'] == 'error_scrape') { + + // Sleep for a bit to make sure the glmMembersAdminNotices option is set. (seems to need this) + sleep(1); + + // if we have pending messages, display those + if (get_option('glmMembersAdminNotices')) { + + glmMembersAdminNotices(); + + // Otherwise, there must have been some other error. + } else { + + echo 'There has been an unknown error installing the Gaslight Media Members Database plugin.'; + + } + + // Quit here so Wordpress doesn't mark plugin as activated or deactivated + exit; + + } + + } + +} + +?> \ No newline at end of file diff --git a/config/plugin.ini b/config/plugin.ini new file mode 100644 index 0000000..e6be830 --- /dev/null +++ b/config/plugin.ini @@ -0,0 +1,41 @@ +; +; Main Configuration File +; GLM WooCommerce Merchant e-Solutions Gateway +; +; Custom configurations for development and developer configuration at bottom of file. +; + +[common] + +; +; Site Configuration Options +; + +; Debug Options +debug = false +debug_verbose = false + +; Site Time Zone +timezone = America/Detroit + +; +; End of site configuration options +; + +; Phrases +phrase['phrase_test'] = 'test' + +; +; Override sections for various servers by GLM_HOST_ID +; + +[production:common] + + +[development:common] + + +[chuck:common] +debug = true +debug_verbose = false + diff --git a/controllers/controller.php b/controllers/controller.php new file mode 100644 index 0000000..fb2aeef --- /dev/null +++ b/controllers/controller.php @@ -0,0 +1,65 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @release controllers/controller.php,v 1.0 2014/10/31 19:31:47 cscott Exp $ + * @link http://dev.gaslightmedia.com/ + */ + +// Load glmPluginSupport class +require_once (GLM_MEMBERS_PLUGIN_PATH . '/classes/glmPluginSupport.php'); + +/* + * This class controls which models are used. + */ +class glmMesController extends GlmPluginSupport +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + + /** + * Plugin Configuration Data + * + * @var $config + * @access public + */ + public $config; + + public function __construct ($wpdb, $config) + { + + // Save WordPress Database object + $this->wpdb = $wpdb; + + // Save plugin configuration object + $this->config = $config; + + + } + + /** + * Controller + * + * @return void + * @access public + */ + public function controller () + { + + } +} +?> \ No newline at end of file diff --git a/deactivate.php b/deactivate.php new file mode 100644 index 0000000..f2de196 --- /dev/null +++ b/deactivate.php @@ -0,0 +1,92 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @release deactivate.php,v 1.0 2014/10/31 19:31:47 cscott Exp $ + * @link http://dev.gaslightmedia.com/ + */ + +// Load glmPluginSupport class +require_once (GLM_MES_PLUGIN_PATH . '/classes/glmPluginSupport.php'); + +/* + * This class performs all necessary additional work when this + * plugin is deactivated. + */ +class glmMesPluginDeactivate extends glmPluginSupport +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + /** + * Plugin Configuration Data + * + * @var $config + * @access public + */ + public $config; + + /* + * Constructor + * + * Performs all the work for this model + */ + public function __construct ($wpdb, $config) + { + + // Make sure the current user has this capability + if (!current_user_can('activate_plugins')) { + die(); + } + + // Save WordPress Database object + $this->wpdb = $wpdb; + + // Save plugin configuration object + $this->config = $config; + + } + + /* + * Remove a role capability from all current roles + * + * @param string $capability + * + * @return void + * @access public + */ + public function removeRoleCapability ($capability) + { + // Get list of role objects + $roleObjects = $GLOBALS['wp_roles']->role_objects; + + // Get list of roles we can edit + $roles = get_editable_roles(); + + // For each role object + foreach ($roleObjects as $key => $role) { + // Check if the role exists in list of editable roles and capability + // does not exist + if (isset($roles[$key]) && isset($role->capabilities[$capability])) { + + // Remove role + $role->remove_cap($capability); + } + } + } +} + +?> \ No newline at end of file diff --git a/defines.php b/defines.php new file mode 100644 index 0000000..0f7233c --- /dev/null +++ b/defines.php @@ -0,0 +1,49 @@ +prefix.'glm_MES_'); + +?> \ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..0a4580d --- /dev/null +++ b/index.php @@ -0,0 +1,270 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @version 1.0 + */ + +/* + * Copyright 2014-2015 Charles Scott (email : cscott@gaslightmedia.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2, as + * published by the Free Software Foundation. + * + * 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 + */ + +// Get standard defined parameters +require_once('defines.php'); + +// Get plugin configuration +$configData = parse_ini_file(GLM_MES_PLUGIN_PATH.'/config/plugin.ini', true); +$config = $configData['common']; + +// Override parameters according to GLM_HOST_ID +$hostSection = strtolower(GLM_MES_PLUGIN_HOST).':common'; +if (isset($configData[$hostSection])) { + $config = array_replace($config, $configData[strtolower(GLM_MES_PLUGIN_HOST).':common']); +} else { + $startupNotices .= + '

Bad configuration file section name or section not found: '. $hostSection + .'
See plugin '.GLM_MES_PLUGIN_PATH.'config/plugin.ini file.' + .'
Also check that the server "GLM_HOST_ID" environment parameter exists and matches a section in the above ini file.

' + ; +} + +// Add Debug defines - These can't go into the defines.php file - Guess why. +define('GLM_MES_PLUGIN_ADMIN_DEBUG', $config['admin_debug']); +define('GLM_MES_PLUGIN_ADMIN_DEBUG_VERBOSE', $config['admin_debug_verbose']); +define('GLM_MES_PLUGIN_FRONT_DEBUG', $config['front_debug']); +define('GLM_MES_PLUGIN_FRONT_DEBUG_VERBOSE', $config['front_debug_verbose']); + +// Also get image sizes array from the plugin.ini - uses separate ini section. +$config['imageSizes'] = $configData['imageSizes']; + +// Get additional configuration data +$stateData = parse_ini_file(GLM_MES_PLUGIN_PATH.'/config/states.ini'); +$config['states'] = $stateData['states']; +$countryData = parse_ini_file(GLM_MES_PLUGIN_PATH.'/config/countries.ini'); +$config['countries'] = $countryData['countries']; + + +/** + * ******************************************************************************* + * + * *** Directory and File Structure *** + * + * index.php + * + * Index file for this plugin. All plugin processing starts here. (See + * "Process Flow" below.) + * + * ******************************************************************************** + */ + +/* + * + * Activate, Deactivate, Uninstall hooks + * + */ + +// Activate +function glmMesPluginActivate () +{ + global $wpdb, $config; + require_once (GLM_MES_PLUGIN_PATH . '/activate.php'); + new glmMesPluginActivate($wpdb, $config); +} +register_activation_hook(__FILE__, 'glmMesPluginActivate'); + +// Deactivate +function glmMesPluginDeactivate () +{ + global $wpdb, $config; + require_once (GLM_MES_PLUGIN_PATH . '/deactivate.php'); + $x = new glmMesPluginDeactivate($wpdb, $config); + return false; +} +register_deactivation_hook(__FILE__, 'glmMesPluginDeactivate'); + + +/* + * + * Load Controller + * + */ + + require_once (GLM_MES_PLUGIN_PATH . '/controllers/controller.php'); + new glmMesController($wpdb, $config); + + +/* + * Function to display admin notices. + * + * This function is only called using the add_action('admin_notices','...') function + * in the code below this function. + * + * @return void + * @access public + */ +function glmMesAdminNotices($windowed = false) +{ + + $output = ''; + + // If windowed for debug, also include HTML header and stylesheet + if ($windowed) { + + $output .= ' + + + + + + +
'.date('m/d/Y G:i:s A').'
+
'.GLM_MES_PLUGIN_NAME.' - Debug Data
+ '; + + // Display alerts + $alerts = get_option('glmMesAdminNoticeAlerts'); + if(is_array($alerts)) { + $output .= '

Alerts

'; + foreach($alerts as $a) { + $output .= $a.'
'; + } + } else { + $output .= '

Alerts

'; + } + $output .= '

'; + delete_option('glmMesAdminNoticeAlerts'); + + // Display process messages + $process = get_option('glmMesAdminNoticeProcess'); + $output .= '

Processing

'; + if(is_array($process)) { + foreach($process as $p) { + $output .= $p.'
'; + } + } + $output .= '

'; + delete_option('glmMesAdminNoticeProcess'); + + // Display data blocks table of contents then the data blocks + $dataBlocks = get_option('glmMesAdminNoticeDataBlocks'); + $output .= '

Data Blocks

    '; + $n = 0; + if (is_array($dataBlocks)) { + foreach($dataBlocks as $d) { + $output .= '
  • '.$d['title'].'
  • '; + } + } + $output .= '

'; + if (is_array($dataBlocks)) { + reset($dataBlocks); + $n = 0; + foreach($dataBlocks as $d) { + $output .= ' +
+
[Top]
+ +
'.print_r($d['data'],1).'
+
+ '; + } + } + delete_option('glmMesAdminNoticeDataBlocks'); + + echo $output.' + + + '; + + // Otherwise we're outputting data to the message block in the WordPress admin area + } else { + + // Start with div class to output in standard admin notice block + $output .= '

'; + + // Get the notice texts + $notices = get_option('glmMesAdminNotices'); + + if (is_array($notices) && count($notices) > 0) { + + // For each notice retrieved + $br = ''; + foreach($notices as $n) { + + // Add the notice to the output + $output .= $br.$n; + $br = '
'; + } + + } + + echo $output.'

'; + + // Delete the option containing the notices to indicate they have been sent + delete_option('glmMesAdminNotices'); + + } + +} + +/* + * Check if there's any startup notices in this file. (stuff that happens before we get all setup) + * + * If there is, have the message displayed at the top of the wp-admin content area. + */ +function glmMesStartupNotices() { + global $startupNotices; + echo '

'.GLM_MES_PLUGIN_NAME.' Plugin Warning

'.$startupNotices.'

'; +} +if ($startupNotices != '') { + add_action('admin_notices','glmMesStartupNotices'); +} + +/* + * Check if there's any debug information or other notices that need to be displayed + * + * If there is, display as a separate window. + * + * NOTE: Need to break out notices that should be displayed in the wp-admin content area. To + * do that I need to modify the glmMesAdminNotices function above and the + * addNotice() function in the classes/glmPluginSupport.php file. Should have addNotice() + * function be able to add to another option specifically for doing this. + */ +$notices = get_option('glmMesAdminNotices'); +if (is_admin() && $notices && !GLM_MES_PLUGIN_ADMIN_DEBUG) { + + // Add action to output the notices + add_action('admin_notices','glmMesAdminNotices'); + +} + +?> \ No newline at end of file diff --git a/uninstall.php b/uninstall.php new file mode 100644 index 0000000..168bbdd --- /dev/null +++ b/uninstall.php @@ -0,0 +1,51 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @release admin.php,v 1.0 2014/10/31 19:31:47 cscott Exp $ + * @link http://dev.gaslightmedia.com/ + */ + +// Get setup and configuration +require_once('defines.php'); + +//if uninstall not called from WordPress exit +if ( !defined( 'WP_UNINSTALL_PLUGIN' ) ) + exit(); + +// Delete options +delete_option('glmMembersDatbasePluginVersion'); +delete_option('glmMembersDatabaseDbVersion'); +delete_option('glmMembersAdminNotices'); + +// Same for Multi-site +delete_site_option('glmMembersDatbasePluginVersion'); +delete_site_option('glmMembersDatabaseDbVersion'); +delete_site_option('glmMembersAdminNotices'); + +/* + * Drop database tables + */ +// Set current database version +$dbVersion = GLM_MEMBERS_PLUGIN_DB_VERSION; + +// Read in Database deletion script - assumes the current db version. +$sqlFile = GLM_MEMBERS_PLUGIN_DB_SCRIPTS.'/drop_database_V'.$dbVersion.'.sql'; +$sql = file_get_contents($sqlFile); + +// Replace {prefix} with table name prefix +$sql = str_replace('{prefix}', GLM_MEMBERS_PLUGIN_DB_PREFIX, $sql); + +// Removing the tables using the script +global $wpdb; +$wpdb->query($sql); + +?> \ No newline at end of file