From 4cfb7af9b2b99b016e475713dbfe54aeb625efce Mon Sep 17 00:00:00 2001
From: Chuck Scott
Date: Tue, 7 Jun 2016 13:05:56 -0400
Subject: [PATCH] Moved checkDatabase to index.php and cleaned up database
checks and what happens if no database tables have been installed.
---
activate.php | 9 +-
classes/glmPluginSupport.php | 276 +----------------
config.php | 29 +-
controllers/admin.php | 3 +-
controllers/front.php | 3 +-
index.php | 514 +++++++++++++++++++++++--------
setup/databaseScripts/readme.txt | 2 +-
7 files changed, 418 insertions(+), 418 deletions(-)
diff --git a/activate.php b/activate.php
index e29c3cc8..b58c0946 100644
--- a/activate.php
+++ b/activate.php
@@ -75,13 +75,8 @@ class glmMembersPluginActivate extends glmPluginSupport
// Save plugin configuration object
$this->config = $config;
-
- if (!$noDatabaseCheck) {
- // Check the database - allow installation of tables for a new add-on
- if (!$this->checkDatabase('install')) {
- die('Database check failure');
- }
+ if (!$noDatabaseCheck) {
// Check for plugin uploads directory
$this->checkUploadsDir();
@@ -93,7 +88,7 @@ class glmMembersPluginActivate extends glmPluginSupport
update_option('glmMembersDatabasePluginVersion', GLM_MEMBERS_PLUGIN_VERSION);
}
-
+
if (get_option(GLM_MEMBERS_PLUGIN_OPTION_FIRST_ACTIVATION, 'None') == 'None') {
update_option(GLM_MEMBERS_PLUGIN_OPTION_FIRST_ACTIVATION, 'true');
} else {
diff --git a/classes/glmPluginSupport.php b/classes/glmPluginSupport.php
index 731086d8..3921db48 100644
--- a/classes/glmPluginSupport.php
+++ b/classes/glmPluginSupport.php
@@ -160,272 +160,6 @@ class GlmPluginSupport
}
- /*
- * Check if database is installed and if it matches the current version
- *
- * @param string $option Set to 'install' to permit installation of the database.
- * @param string $default Whether capability should be on by default
- *
- * @return boolean False if some failure
- * @access private
- */
- public function checkDatabase ()
- {
- global $startupNotices;
-
- $all_db_setup_status = true;
-
- // For each plug-in in the registered add-on array (includes the main plugin)
- foreach ($this->config['addOns'] as $a) {
-
- $db_setup_status = false;
- $db_error = false;
-
- // Has this plug-in registered any database versions
- if ($a['database']) {
-
- if (!isset($a['database']['dbActiveVersionOption'])) {
- $startupNotices .= 'There is missing database information for plugin/add-on '.$a['name']."."
- ."
Incorrect plugin/add-on configuration is suspected. Check add-on registration data array for this add-on.
";
- $db_error = true;
- $dbVersion = false;
-
- } else {
- $dbVersion = get_option($a['database']['dbActiveVersionOption']);
- }
-
-/*
- // Do a sanity check on the database version
- if (!preg_match('^(\d+\.)?(\d+\.)?(\*|\d+)$', $dbVersion)) {
-
- // If there's a current version then try to set that in the WordPress option
- if (isset($a['database']['dbCurrentVersion']) && $a['database']['dbCurrentVersion'] != '') {
- update_option($a['database']['dbActiveVersionOption'], $a['database']['dbCurrentVersion']);
- $dbVersion = $a['database']['dbCurrentVersion'];
-
- // Not much we can do here....
- } else {
- return false;
- }
-
- }
-*/
-
- // Do a quick check for there being a database version but not all the required tables for that version
- if ($dbVersion) {
-
- // Check if the database version set for this plug-in is invalid - not in the dbVersions array
- if (!isset($a['database']['dbVersions'][$dbVersion])) {
- $startupNotices .= "The last database version set for the ".$a['name']." ($dbVersion) isn't valid.
";
- $db_error = true;
- } else {
-
- // Get the number of tables for this plugin database version that should exist.
- $tables = $a['database']['dbVersions'][$dbVersion]['tables'];
-
- // Since MYSQL considers an underscore (_) to be a single character wild-card, we need to escape them.
- $prefixEscaped = str_replace('_', '\_', $a['database']['dbPrefix']);
-
- // Get the number of tables for this plugin that match the
- $existingTables = $this->wpdb->get_var("
- SELECT COUNT(*)
- FROM information_schema.tables
- WHERE table_schema = '".DB_NAME."'
- AND table_name like '$prefixEscaped%';
- ");
-
- // If there's no tables, just assume we need to install all new ones.
- if ($existingTables == 0) {
-
- $dbVersion = false;
-
- // Otherwise check if the number of tables is correct
- } elseif ($tables != $existingTables) {
- $startupNotices .= '
We do not have the correct number of tables for the currently set database version (V'.$dbVersion.') for '.$a['name'].'.'
- ."
There should be $tables but there are currently $existingTables. Please call for support.
";
- $db_error = true;
- }
-
- }
-
- }
-
- // If glmMembersDatabaseDbVersion is not set, install current version
- if (!$dbVersion && !$db_error) {
-
- // Get current database version
- $dbVersion = $a['database']['dbCurrentVersion'];
-
- // Check for a version string
- if (trim($dbVersion == '')) {
-
- $startupNotices .= 'Failure installing database tables for the '.$a['name']
- .'
No database version set:
';
-
- } else {
-
- // Create name for Database creation script
- $sqlFile = $a['database']['dbScriptPath'].'/create_database_V'.$dbVersion.'.sql';
-
- // Make sure it exists
- if (!is_file($sqlFile)) {
-
- $startupNotices .= 'Failure installing database tables for the '.$a['name']
- .'
Database Creation Script not found: '.$sqlFile.'
';
-
- } else {
- $sql = file_get_contents($sqlFile);
-
- // Replace {prefix} with table name prefix
- $sql = str_replace('{prefix}', $a['database']['dbPrefix'], $sql);
-
- // Split script into separate queries by looking for lines with only "----"
- $queries = preg_split('/^----$/m', $sql);
-
- // Try executing all queries to build database
- do {
- $q = current($queries);
- $this->wpdb->query($q);
- $queryError = $this->wpdb->last_error;
- } while ($queryError == '' && next($queries));
-
- // If there were no errors
- if (trim($queryError) == '') {
-
- // Notify the user that the database has been installed
- $startupNotices .= 'New database tables installed for the '.$a['name'].' plugin.
';
-
- // Save the version of the installed database
- update_option($a['database']['dbActiveVersionOption'], $dbVersion);
-
- // Indicate that we were successfull
- $db_setup_status = true;
-
- } else {
- $startupNotices .= 'Failure installing database tables for the '.$a['name']
- .'
Database Installation Error: '.print_r($queryError,1)
- .'
'.$q.'
';
- }
-
- } // db creation file check
-
- } // db version check
-
- // Otherwise, check if we need to update the database
- } elseif ($dbVersion != $a['database']['dbCurrentVersion']) {
-
- // Include an admin message that we're updating the database
- $this->addNotice('The '.$a['name'].' database tables require updating...');
-
- // Check if current glmMembersDatabaseDbVersion is invalid
- if (!in_array($a['database']['dbCurrentVersion'], $a['database']['dbVersions'])) {
- $this->addNotice('The database version currently installed for this plugin is unknown. '
- .'Unable to install/update the '.$a['name'].' plugin.
');
- }
-
- // Traverse version list to find any required updates
- $curVerFound = false;
- $db_setup_status = true;
- foreach($a['database']['dbVersions'] as $version) {
-
- $ver = $version['version'];
-
- // Find the current version of the database
- if ($ver == $dbVersion) {
- // $this->addNotice('The database version installed for the '.GLM_MEMBERS_PLUGIN_NAME
- // .' plugin is current and does not require updating.');
- $db_setup_status = true;
- $curVerFound = true;
-
- // Otherwise if it's already been found and $ver is not the new target version
- } elseif ($curVerFound && $dbVersion != $a['database']['dbCurrentVersion']) {
-
- // Read in Database creation script
- $sqlFile = $a['database']['dbScriptPath'].'/update_database_V'.$ver.'.sql';
- $sql = file_get_contents($sqlFile);
-
- // Replace {prefix} with table name prefix
- $sql = str_replace('{prefix}', $a['database']['dbPrefix'], $sql);
-
- // Split script into separate queries by looking for lines with only "---"
- $queries = preg_split('/^----$/m', $sql);
-
- // Try executing all queries to update database
- do {
- $q = current($queries);
- $this->wpdb->query($q);
- $queryError = $this->wpdb->last_error;
- } while ($queryError == '' && next($queries));
-
- // Check for PHP script to update database
- $phpScript = $a['database']['dbScriptPath'].'/update_database_V'.$ver.'.php';
- if (is_file($phpScript)) {
- require_once ($phpScript);
- }
-
- // If there were no errors
- if ($queryError == '') {
- $this->addNotice('The database for the '.$a['name'].' plugin has been updated '
- .'from V'.$dbVersion.'_V'.$ver.'.');
- } else {
- $this->addNotice('Failure updating the database tables for the '.$a['name'].' plugin '
- .'from V'.$dbVersion.'_V'.$ver.'.');
- $db_setup_status = false;
- $this->addNotice('Database Update Error: '.$queryError, 'Alert');
- }
-
- $dbVersion = $ver;
- $db_setup_status = true;
-
- }
-
- // Save the new version. If we've had a problem updating the database, then stop here.
- if ($db_setup_status) {
-
- // Save the version of the installed database
- update_option($a['database']['dbActiveVersionOption'], $dbVersion);
-
- } else {
- break;
- }
-
- }
-
- if ($db_setup_status) {
-
- // Save the updated version of the installed database
- // update_option('glmMembersDatabaseDbVersion', $dbVersion);
-
- $this->addNotice('Database tables updated.');
- }
-
- } else {
- $db_setup_status = true;
- }
-
- } else { // Plugin doesn't have registered database versions
-
- $db_setup_status = true;
-
- } // Plugin has registered database versions
-
- if (!$db_setup_status) {
- $all_db_setup_status = false;
- }
-
- // If there was any serious error with the database for this add-on
- if ($db_error) {
- // Deactivate this add-on
- deactivate_plugins($a['slug'].'/index.php');
- global $startupNotices;
- $startupNotices .= 'Plugin '.$a['name'].' Deactivated.
';
-
- }
- } // For each plugin
-
- return $all_db_setup_status;
- }
-
/**
* Display Git Branch as an admin warning
*
@@ -524,8 +258,8 @@ return; // Off for now ** Need to make this switchable in management
} else {
$postParent = '0';
}
-
-
+
+
// If the new slug corresponds to a page already existent, then it's
// either the first time this plugin is run on an outdated site or
// somehow the ID was deleted. Either way, replace the option value
@@ -533,17 +267,17 @@ return; // Off for now ** Need to make this switchable in management
// is checked by translating the page title.
$newSlug = sanitize_title($requiredPageInfo['title']);
$existingSlug = $this->verify_post_slug($newSlug);
-
+
// But only run this if it is the first time this plugin is activated
if (get_option(GLM_MEMBERS_PLUGIN_OPTION_FIRST_ACTIVATION) == 'true') {
if ($newSlug == $existingSlug) {
$existingID = $this->get_post_id_by_slug($newSlug);
- update_option($option, $existingID);
+ update_option($option, $existingID);
}
}
-
+
$existingPost = get_post(get_option($option));
// If a post with the ID set in the option does not exist
if ( !$existingPost) {
diff --git a/config.php b/config.php
index f10cd48e..4d58bf57 100644
--- a/config.php
+++ b/config.php
@@ -18,20 +18,21 @@ $config['states'] = $stateData['states'];
$countryData = parse_ini_file(GLM_MEMBERS_PLUGIN_PATH.'/config/countries.ini');
$config['countries'] = $countryData['countries'];
-// Read in Settings and Terms from database
-$settings = $wpdb->get_row("SELECT * FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX . "settings_general WHERE id = 1;", ARRAY_A);
-unset($settings['id']);
-$config['settings'] = $settings;
-
-$terms = $wpdb->get_row("SELECT * FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX . "settings_terms WHERE id = 1;", ARRAY_A);
-unset($terms['id']);
-$config['terms'] = $terms;
-
-// Add Debug defines - These can't go into the defines.php file - Guess why.
-define('GLM_MEMBERS_PLUGIN_ADMIN_DEBUG', $settings['admin_debug']);
-define('GLM_MEMBERS_PLUGIN_ADMIN_DEBUG_VERBOSE', $settings['admin_debug_verbose']);
-define('GLM_MEMBERS_PLUGIN_FRONT_DEBUG', $settings['front_debug']);
-define('GLM_MEMBERS_PLUGIN_FRONT_DEBUG_VERBOSE', $settings['front_debug_verbose']);
+// if we have the database setup already (activated) - Read in Settings and Terms from database
+if ($glmMembersDatabaseDbVersion) {
+ $settings = $wpdb->get_row("SELECT * FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX . "settings_general WHERE id = 1;", ARRAY_A);
+ unset($settings['id']);
+ $config['settings'] = $settings;
+ $terms = $wpdb->get_row("SELECT * FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX . "settings_terms WHERE id = 1;", ARRAY_A);
+ unset($terms['id']);
+ $config['terms'] = $terms;
+
+ // Add Debug defines - These can't go into the defines.php file - Guess why.
+ define('GLM_MEMBERS_PLUGIN_ADMIN_DEBUG', $settings['admin_debug']);
+ define('GLM_MEMBERS_PLUGIN_ADMIN_DEBUG_VERBOSE', $settings['admin_debug_verbose']);
+ define('GLM_MEMBERS_PLUGIN_FRONT_DEBUG', $settings['front_debug']);
+ define('GLM_MEMBERS_PLUGIN_FRONT_DEBUG_VERBOSE', $settings['front_debug_verbose']);
+}
// Check for config value replacements in the current theme
$currentThemeDirectory = get_template_directory();
diff --git a/controllers/admin.php b/controllers/admin.php
index bd1964d5..cb7b9d71 100644
--- a/controllers/admin.php
+++ b/controllers/admin.php
@@ -130,9 +130,10 @@ class glmMembersAdmin extends GlmPluginSupport
$this->config = $config;
// Check the database - allow installation of tables for a new add-on
- if (!$this->checkDatabase('install')) {
+/* if (!$this->checkDatabase('install')) {
die('Database check failure');
}
+*/
/*
* Check if there's a request to bypass the WordPress Dashboard and
diff --git a/controllers/front.php b/controllers/front.php
index 0da60e9a..79cca951 100644
--- a/controllers/front.php
+++ b/controllers/front.php
@@ -47,9 +47,10 @@ class glmMembersFront extends GlmPluginSupport
$this->config = $config;
// Check the database - allow installation of tables for a new add-on
- if (!$this->checkDatabase('install')) {
+/* if (!$this->checkDatabase('install')) {
die('Database check failure');
}
+*/
// Now add any shortcodes from the config 'shortcodes' table
while (list($key, $val) = each($this->config['shortcodes'])) {
diff --git a/index.php b/index.php
index ad2933e2..987f35d6 100644
--- a/index.php
+++ b/index.php
@@ -3,7 +3,7 @@
* Plugin Name: GLM Members Database
* Plugin URI: http://www.gaslightmedia.com/
* Description: Gaslight Media Members Database.
- * Version: 2.0.6
+ * Version: 2.0.3
* Author: Gaslight Media
* Author URI: http://www.gaslightmedia.com/
* License: GPL2
@@ -17,9 +17,9 @@
*
* @category glmWordPressPlugin
* @package glmMembersDatabase
- * @author Gaslight Media
+ * @author Chuck Scott
* @license http://www.gaslightmedia.com Gaslightmedia
- * @version 2.0.6
+ * @version 2.0.2
*/
/*
@@ -38,7 +38,7 @@
*
*/
-define('GLM_MEMBERS_PLUGIN_VERSION', '2.0.5');
+define('GLM_MEMBERS_PLUGIN_VERSION', '2.0.3');
define('GLM_MEMBERS_PLUGIN_DB_VERSION', '1.1.8');
// Check if plugin version is not current in WordPress option and if needed updated it
@@ -204,15 +204,16 @@ if (!defined('ABSPATH')) {
$startupNotices = '';
+// Try to set the DB version option to false (new plugin) - If it's already set this won't do anything.
+add_option('glmMembersDatabaseDbVersion', false);
+$glmMembersDatabaseDbVersion = get_option('glmMembersDatabaseDbVersion');
+
// Get standard defined parameters
require_once('defines.php');
// Get configuration
require_once('config.php');
-// Try to set the DB version option to false (new plugin) - If it's already set this won't do anything.
-add_option('glmMembersDatabaseDbVersion', false);
-
/*
*
* Hook in add-ons and integrate their actions
@@ -274,25 +275,26 @@ require_once(GLM_MEMBERS_PLUGIN_SETUP_PATH.'/validActions.php');
require_once(GLM_MEMBERS_PLUGIN_SETUP_PATH.'/shortcodes.php');
require_once GLM_MEMBERS_PLUGIN_SETUP_PATH.'/requiredPages.php';
require_once(GLM_MEMBERS_PLUGIN_DB_SCRIPTS.'/dbVersions.php');
-$config['addOns'] = array(
- GLM_MEMBERS_PLUGIN_SLUG => array(
- 'dir' => GLM_MEMBERS_PLUGIN_PATH,
- 'name' => GLM_MEMBERS_PLUGIN_NAME,
- 'short_name' => GLM_MEMBERS_PLUGIN_SHORT_NAME,
- 'slug' => GLM_MEMBERS_PLUGIN_SLUG,
- 'underscored_name' => GLM_MEMBERS_PLUGIN_UNDERSCORED_NAME,
- 'actions' => $glmMembersValidActions,
- 'shortcodes' => $glmMembersShortcodes,
- 'shortcodesDescription' => $glmMembersShortcodesDescription,
- 'database' => array(
- 'dbPrefix' => GLM_MEMBERS_PLUGIN_DB_PREFIX,
- 'dbCurrentVersion' => GLM_MEMBERS_PLUGIN_DB_VERSION,
- 'dbActiveVersionOption' => GLM_MEMBERS_PLUGIN_ACTIVE_DB_OPTION,
- 'dbScriptPath' => GLM_MEMBERS_PLUGIN_DB_SCRIPTS,
- 'dbVersions' => $glmMembersDbVersions
- ),
- 'requiredPages' => $glmMembersRequiredPages
- )
+$config['addOns'] = array();
+
+// Register the main member db plugin as an add-on - Will be more normal when members are pulled from main plugin
+$config['addOns'][GLM_MEMBERS_PLUGIN_SLUG] = array(
+ 'dir' => GLM_MEMBERS_PLUGIN_PATH,
+ 'name' => GLM_MEMBERS_PLUGIN_NAME,
+ 'short_name' => GLM_MEMBERS_PLUGIN_SHORT_NAME,
+ 'slug' => GLM_MEMBERS_PLUGIN_SLUG,
+ 'underscored_name' => GLM_MEMBERS_PLUGIN_UNDERSCORED_NAME,
+ 'actions' => $glmMembersValidActions,
+ 'shortcodes' => $glmMembersShortcodes,
+ 'shortcodesDescription' => $glmMembersShortcodesDescription,
+ 'database' => array(
+ 'dbPrefix' => GLM_MEMBERS_PLUGIN_DB_PREFIX,
+ 'dbCurrentVersion' => GLM_MEMBERS_PLUGIN_DB_VERSION,
+ 'dbActiveVersionOption' => GLM_MEMBERS_PLUGIN_ACTIVE_DB_OPTION,
+ 'dbScriptPath' => GLM_MEMBERS_PLUGIN_DB_SCRIPTS,
+ 'dbVersions' => $glmMembersDbVersions
+ ),
+ 'requiredPages' => $glmMembersRequiredPages
);
// Create hook for registering add-on plugins and their features
@@ -301,105 +303,112 @@ $config['addOns'] = apply_filters('glm-member-db-register-addon', $config['addOn
// If any add-ons have registered
$config['validActions'] = array();
$config['shortcodes'] = array();
-if (count($config['addOns']) > 0) {
- // For each add-on that's registered
- foreach ($config['addOns'] as $a) {
+// Check all database tables for all registered add-ons, allow install/update
+if (glmCheckDatabase('install')) {
- // Add their valid actions to the main validActions config array
- if (isset($a['actions'])) {
- $config['validActions'] = array_merge_recursive($config['validActions'], $a['actions']);
- }
+ // If we have addons
+ if (count($config['addOns']) > 0) {
- // Add their shortcodes to the main shortcodes config array
- if (isset($a['shortcodes'])) {
- $config['shortcodes'] = array_merge_recursive($config['shortcodes'], $a['shortcodes']);
- }
+ // For each add-on that's registered
+ foreach ($config['addOns'] as $a) {
- // If the add-on has supplied additional config parameters when registering
- if (isset($a['config'])) {
- $config = array_merge_recursive($config, $a['config']);
- }
+ // Add their valid actions to the main validActions config array
+ if (isset($a['actions'])) {
+ $config['validActions'] = array_merge_recursive($config['validActions'], $a['actions']);
+ }
- // If the add-on has additional config parameters in a plugin.ini file
- $iniFile = GLM_MEMBERS_WORDPRESS_PLUGIN_PATH.'/'.$a['slug'].'/config/plugin.ini';
- if (isset($iniFile)) {
+ // Add their shortcodes to the main shortcodes config array
+ if (isset($a['shortcodes'])) {
+ $config['shortcodes'] = array_merge_recursive($config['shortcodes'], $a['shortcodes']);
+ }
- // Parse the add-on's configuration file
- $addOnIni = parse_ini_file($iniFile);
+ // If the add-on has supplied additional config parameters when registering
+ if (isset($a['config'])) {
+ $config = array_merge_recursive($config, $a['config']);
+ }
- // Replace parameters that are in the theme ini file
- $config = array_replace($config, $addOnIni);
+ // If the add-on has additional config parameters in a plugin.ini file
+ $iniFile = GLM_MEMBERS_WORDPRESS_PLUGIN_PATH.'/'.$a['slug'].'/config/plugin.ini';
+ if (isset($iniFile)) {
- }
- }
-}
+ // Parse the add-on's configuration file
+ $addOnIni = parse_ini_file($iniFile);
-/*
- *
- * Activate and Deactivate hooks
- *
- */
-
-// Activate
-function glmMembersPluginActivate ()
-{
- global $wpdb, $config;
- require_once (GLM_MEMBERS_PLUGIN_PATH . '/activate.php');
- new glmMembersPluginActivate($wpdb, $config);
-}
-register_activation_hook(__FILE__, 'glmMembersPluginActivate');
+ // Replace parameters that are in the theme ini file
+ $config = array_replace($config, $addOnIni);
-// Deactivate
-function glmMembersPluginDeactivate ()
-{
- global $wpdb, $config;
- require_once (GLM_MEMBERS_PLUGIN_PATH . '/deactivate.php');
- $x = new glmMembersPluginDeactivate($wpdb, $config);
- return false;
-}
-register_deactivation_hook(__FILE__, 'glmMembersPluginDeactivate');
-
-/*
- *
- * Load any other common files needed
- *
- */
-
-// Load data abstract
-require_once(GLM_MEMBERS_PLUGIN_LIB_PATH.'/GlmDataAbstract/DataAbstract.php');
-
-// Load glmPluginSupport class
-require_once (GLM_MEMBERS_PLUGIN_PATH . '/classes/glmPluginSupport.php');
-
-// Load Smarty Template Support
-require_once (GLM_MEMBERS_PLUGIN_PATH . '/lib/smartyTemplateSupport.php');
+ }
+ }
+ }
-/*
- * Hook through which an add-on may supply additional logged in user information and
- * have that data stored in the config array. Typically it would be the
- * glm-members-db-contacts add-on supplying the information.
- *
- * This hook provides default data with the current WordPress user 'data' object
- * as a 'wpUser' sub-array if a WordPress user is logged in. If not 'wpUser' will
- * be false. The supplied data is the basic information on the WordPress user
- * provided by the 'data' object from wp_get_current_user().
- *
- * To permit more than one routine to access this filter and therefore to supply
- * additional information on the logged in user, code may merge it's own data or may
- * add another sub-array containing user information specific to an add-on (i.e.
- * contacts add-on might supply a 'contactUser' sub-array).
- *
- */
-if (function_exists('is_user_logged_in')) {
- $config['loggedInUser'] = array(
- 'wpUser' => false
- );
- if (is_user_logged_in()) {
- $config['loggedInUser']['wpUser'] = (array) wp_get_current_user()->data;
+ /*
+ *
+ * Activate and Deactivate hooks
+ *
+ */
+
+ // Activate
+ function glmMembersPluginActivate ()
+ {
+ global $wpdb, $config;
+ require_once (GLM_MEMBERS_PLUGIN_PATH . '/activate.php');
+ new glmMembersPluginActivate($wpdb, $config);
+ }
+ register_activation_hook(__FILE__, 'glmMembersPluginActivate');
+
+ // Deactivate
+ function glmMembersPluginDeactivate ()
+ {
+ global $wpdb, $config;
+ require_once (GLM_MEMBERS_PLUGIN_PATH . '/deactivate.php');
+ $x = new glmMembersPluginDeactivate($wpdb, $config);
+ return false;
+ }
+ register_deactivation_hook(__FILE__, 'glmMembersPluginDeactivate');
+
+ /*
+ *
+ * Load any other common files needed
+ *
+ */
+
+ // Load data abstract
+ require_once(GLM_MEMBERS_PLUGIN_LIB_PATH.'/GlmDataAbstract/DataAbstract.php');
+
+ // Load glmPluginSupport class
+ require_once (GLM_MEMBERS_PLUGIN_PATH . '/classes/glmPluginSupport.php');
+
+ // Load Smarty Template Support
+ require_once (GLM_MEMBERS_PLUGIN_PATH . '/lib/smartyTemplateSupport.php');
+
+ /*
+ * Hook through which an add-on may supply additional logged in user information and
+ * have that data stored in the config array. Typically it would be the
+ * glm-members-db-contacts add-on supplying the information.
+ *
+ * This hook provides default data with the current WordPress user 'data' object
+ * as a 'wpUser' sub-array if a WordPress user is logged in. If not 'wpUser' will
+ * be false. The supplied data is the basic information on the WordPress user
+ * provided by the 'data' object from wp_get_current_user().
+ *
+ * To permit more than one routine to access this filter and therefore to supply
+ * additional information on the logged in user, code may merge it's own data or may
+ * add another sub-array containing user information specific to an add-on (i.e.
+ * contacts add-on might supply a 'contactUser' sub-array).
+ *
+ */
+ if (function_exists('is_user_logged_in')) {
+ $config['loggedInUser'] = array(
+ 'wpUser' => false
+ );
+ if (is_user_logged_in()) {
+ $config['loggedInUser']['wpUser'] = (array) wp_get_current_user()->data;
+ }
+ $config['loggedInUser'] = apply_filters('glm_members_current_logged_in_user', $config['loggedInUser']);
}
- $config['loggedInUser'] = apply_filters('glm_members_current_logged_in_user', $config['loggedInUser']);
-}
+
+} // have databases
/*
*
@@ -411,34 +420,293 @@ if (function_exists('is_user_logged_in')) {
if (isset($_REQUEST['glmDebugWindow']) && $_REQUEST['glmDebugWindow'] == true) {
glmMembersAdminNotices(true);
exit;
+}
+
+// If no database version was set at the top, this was an initial activation, so don't run controllers
+if ($glmMembersDatabaseDbVersion) {
+
+ // Otherwise select appropriate controller
+ if (is_admin()) {
+ require_once (GLM_MEMBERS_PLUGIN_PATH . '/controllers/admin.php');
+ new glmMembersAdmin($wpdb, $config);
+ } else {
+ require_once (GLM_MEMBERS_PLUGIN_PATH . '/controllers/front.php');
+ new glmMembersFront($wpdb, $config);
+ }
-// Otherwise select appropriate controller
-} elseif (is_admin()) {
- require_once (GLM_MEMBERS_PLUGIN_PATH . '/controllers/admin.php');
- new glmMembersAdmin($wpdb, $config);
} else {
- require_once (GLM_MEMBERS_PLUGIN_PATH . '/controllers/front.php');
- new glmMembersFront($wpdb, $config);
+
+ // Inital setup so notify the user that they need to reload to see the admin menus
+ $startupNotices .= 'Initial plugin installation and database setup...
Select "Dashboard" to see new menus.
';
}
/*
* Display any notices?
*/
-
// Display any notices that need to be displayed at the top of the admin content area
function glmMembersWordpressAdminNotices() {
global $startupNotices;
- echo '
'.GLM_MEMBERS_PLUGIN_NAME.' Plugin Notice
'.$startupNotices.'
';
+ echo ''.GLM_MEMBERS_PLUGIN_NAME.' Plugin Notice
'.$startupNotices.'
';
}
if ($startupNotices != '') {
add_action('admin_notices','glmMembersWordpressAdminNotices');
}
-// If not doing debug, then clear all of the debug messages
-if (!GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) {
+// If not doing debug, then clear all of the debug messages that might have been saved
+if (defined('GLM_MEMBERS_PLUGIN_ADMIN_DEBUG') && !GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) {
delete_option('glmMembersAdminNoticeAlerts');
delete_option('glmMembersAdminNotices');
delete_option('glmMembersAdminNoticeProcess');
delete_option('glmMembersAdminNoticeDataBlocks');
}
+/*
+ * Check if database is installed and if it matches the current version
+ *
+ * @param string $option Set to 'install' to permit installation of the database.
+ * @param string $default Whether capability should be on by default
+ *
+ * @return boolean False if some failure
+ * @access private
+ */
+function glmCheckDatabase ()
+{
+ global $startupNotices, $config, $wpdb;
+
+ // Check for add-on array
+ if (!isset($config['addOns']) || count($config['addOns']) == 0) {
+ return true;
+ }
+
+ $all_db_setup_status = true;
+
+ // For each plug-in in the registered add-on array (includes the main plugin)
+ foreach ($config['addOns'] as $a) {
+
+ $db_setup_status = false;
+ $db_error = false;
+
+ // Has this plug-in registered any database versions
+ if ($a['database']) {
+
+ if (!isset($a['database']['dbActiveVersionOption'])) {
+ $startupNotices .= 'There is missing database information for plugin/add-on '.$a['name']."."
+ ."
Incorrect plugin/add-on configuration is suspected. Check add-on registration data array for this add-on.
";
+ $db_error = true;
+ $dbVersion = false;
+
+ } else {
+ $dbVersion = get_option($a['database']['dbActiveVersionOption']);
+ }
+
+ // Do a quick check for there being a database version but not all the required tables for that version
+ if ($dbVersion) {
+
+ // Check if the database version set for this plug-in is invalid - not in the dbVersions array
+ if (!isset($a['database']['dbVersions'][$dbVersion])) {
+ $startupNotices .= "The last database version set for the ".$a['name']." ($dbVersion) isn't valid.
";
+ $db_error = true;
+ } else {
+
+ // Get the number of tables for this plugin database version that should exist.
+ $tables = $a['database']['dbVersions'][$dbVersion]['tables'];
+
+ // Since MYSQL considers an underscore (_) to be a single character wild-card, we need to escape them.
+ $prefixEscaped = str_replace('_', '\_', $a['database']['dbPrefix']);
+
+ // Get the number of tables for this plugin that match the
+ $existingTables = $wpdb->get_var("
+ SELECT COUNT(*)
+ FROM information_schema.tables
+ WHERE table_schema = '".DB_NAME."'
+ AND table_name like '$prefixEscaped%';
+ ");
+
+ // If there's no tables, just assume we need to install all new ones.
+ if ($existingTables == 0) {
+
+ $dbVersion = false;
+
+ // Otherwise check if the number of tables is correct
+ } elseif ($tables != $existingTables) {
+ $startupNotices .= '
We do not have the correct number of tables for the currently set database version (V'.$dbVersion.') for '.$a['name'].'.'
+ ."
There should be $tables but there are currently $existingTables. Please call for support.
";
+ $db_error = true;
+ }
+
+ }
+
+ }
+
+ // If glmMembersDatabaseDbVersion is not set, install current version
+ if (!$dbVersion && !$db_error) {
+
+ // Get current database version
+ $dbVersion = $a['database']['dbCurrentVersion'];
+
+ // Check for a version string
+ if (trim($dbVersion == '')) {
+
+ $startupNotices .= 'Failure installing database tables for the '.$a['name']
+ .'
No database version set:
';
+
+ } else {
+
+ // Create name for Database creation script
+ $sqlFile = $a['database']['dbScriptPath'].'/create_database_V'.$dbVersion.'.sql';
+
+ // Make sure it exists
+ if (!is_file($sqlFile)) {
+
+ $startupNotices .= 'Failure installing database tables for the '.$a['name']
+ .'
Database Creation Script not found: '.$sqlFile.'
';
+
+ } else {
+ $sql = file_get_contents($sqlFile);
+
+ // Replace {prefix} with table name prefix
+ $sql = str_replace('{prefix}', $a['database']['dbPrefix'], $sql);
+
+ // Split script into separate queries by looking for lines with only "----"
+ $queries = preg_split('/^----$/m', $sql);
+
+ // Try executing all queries to build database
+ do {
+ $q = current($queries);
+ $wpdb->query($q);
+ $queryError = $wpdb->last_error;
+ } while ($queryError == '' && next($queries));
+
+ // If there were no errors
+ if (trim($queryError) == '') {
+
+ // Notify the user that the database has been installed
+ $startupNotices .= 'New database tables installed for the '.$a['name'].' plugin.
';
+
+ // Save the version of the installed database
+ update_option($a['database']['dbActiveVersionOption'], $dbVersion);
+
+ // Indicate that we were successfull
+ $db_setup_status = true;
+
+ } else {
+ $startupNotices .= 'Failure installing database tables for the '.$a['name']
+ .'
Database Installation Error: '.print_r($queryError,1)
+ .'
'.$q.'
';
+ }
+
+ } // db creation file check
+
+ } // db version check
+
+ // Otherwise, check if we need to update the database
+ } elseif ($dbVersion != $a['database']['dbCurrentVersion']) {
+
+ // Include an admin message that we're updating the database
+ $startupNotices .= 'The '.$a['name'].' database tables require updating...
';
+
+ // Check if current glmMembersDatabaseDbVersion is invalid
+ if (!in_array($a['database']['dbCurrentVersion'], $a['database']['dbVersions'])) {
+ $startupNotices .= 'The database version currently installed for this plugin is unknown. '
+ .'Unable to install/update the '.$a['name'].' plugin.
';
+ }
+
+ // Traverse version list to find any required updates
+ $curVerFound = false;
+ $db_setup_status = true;
+ foreach($a['database']['dbVersions'] as $version) {
+
+ $ver = $version['version'];
+
+ // Find the current version of the database
+ if ($ver == $dbVersion) {
+ // $startupNotices .= 'The database version installed for the '.GLM_MEMBERS_PLUGIN_NAME
+ // .' plugin is current and does not require updating.
';
+ $db_setup_status = true;
+ $curVerFound = true;
+
+ // Otherwise if it's already been found and $ver is not the new target version
+ } elseif ($curVerFound && $dbVersion != $a['database']['dbCurrentVersion']) {
+
+ // Read in Database creation script
+ $sqlFile = $a['database']['dbScriptPath'].'/update_database_V'.$ver.'.sql';
+ $sql = file_get_contents($sqlFile);
+
+ // Replace {prefix} with table name prefix
+ $sql = str_replace('{prefix}', $a['database']['dbPrefix'], $sql);
+
+ // Split script into separate queries by looking for lines with only "---"
+ $queries = preg_split('/^----$/m', $sql);
+
+ // Try executing all queries to update database
+ do {
+ $q = current($queries);
+ $wpdb->query($q);
+ $queryError = $wpdb->last_error;
+ } while ($queryError == '' && next($queries));
+
+ // Check for PHP script to update database
+ $phpScript = $a['database']['dbScriptPath'].'/update_database_V'.$ver.'.php';
+ if (is_file($phpScript)) {
+ require_once ($phpScript);
+ }
+
+ // If there were no errors
+ if ($queryError == '') {
+ $startupNotices .= 'The database for the '.$a['name'].' plugin has been updated '
+ .'from V'.$dbVersion.'_V'.$ver.'.
';
+ } else {
+ $startupNotices .= 'Failure updating the database tables for the '.$a['name'].' plugin '
+ .'from V'.$dbVersion.'_V'.$ver.'.
';
+ $db_setup_status = false;
+ $startupNotices .= 'Database Update Error: '.$queryError.'
';
+ }
+
+ $dbVersion = $ver;
+ $db_setup_status = true;
+
+ }
+
+ // Save the new version. If we've had a problem updating the database, then stop here.
+ if ($db_setup_status) {
+
+ // Save the version of the installed database
+ update_option($a['database']['dbActiveVersionOption'], $dbVersion);
+
+ } else {
+ break;
+ }
+
+ }
+
+ if ($db_setup_status) {
+
+ $startupNotices .= 'Database tables updated.
';
+ }
+
+ } else {
+ $db_setup_status = true;
+ }
+
+ } else { // Plugin doesn't have registered database versions
+
+ $db_setup_status = true;
+
+ } // Plugin has registered database versions
+
+ if (!$db_setup_status) {
+ $all_db_setup_status = false;
+ }
+
+ // If there was any serious error with the database for this add-on
+ if ($db_error) {
+ // Deactivate this add-on
+ deactivate_plugins($a['slug'].'/index.php');
+ $startupNotices .= 'Plugin '.$a['name'].' Deactivated.
';
+
+ }
+ } // For each plugin
+
+ return $all_db_setup_status;
+}
+
diff --git a/setup/databaseScripts/readme.txt b/setup/databaseScripts/readme.txt
index 141d8b5b..ec232a08 100644
--- a/setup/databaseScripts/readme.txt
+++ b/setup/databaseScripts/readme.txt
@@ -1,6 +1,6 @@
This directory contains database creation and update scripts for this add-on.
-The files in this directory are checked by the checkDatabase() function in the
+The files in this directory are checked by the glmCheckDatabase() function in the
main plugin classes/glmPluginSupport.php file.
This directory is optional. If there are no data tables that need to be created
--
2.17.1