From 748e6c44d8354a8acba4184f5058c9b0b081558d Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Tue, 23 May 2017 11:40:04 -0400 Subject: [PATCH] Initial Test of Shortcode Caching Added cache control logig to front controller Moved enqueueing of scropts from the members list.php script to the controller Added a cache table to the database Added cache control options to shortcodes list. --- controllers/front.php | 81 ++++++++++++++++++- index.php | 7 +- models/front/members/list.php | 15 ---- ...1.1.30.sql => create_database_V1.1.31.sql} | 12 +++ setup/databaseScripts/dbVersions.php | 3 +- ..._V1.1.30.sql => drop_database_V1.1.31.sql} | 3 +- .../update_database_V1.1.31.sql | 17 ++++ setup/shortcodes.php | 8 ++ 8 files changed, 127 insertions(+), 19 deletions(-) rename setup/databaseScripts/{create_database_V1.1.30.sql => create_database_V1.1.31.sql} (99%) rename setup/databaseScripts/{drop_database_V1.1.30.sql => drop_database_V1.1.31.sql} (93%) create mode 100644 setup/databaseScripts/update_database_V1.1.31.sql diff --git a/controllers/front.php b/controllers/front.php index c3b3b740..276d555d 100644 --- a/controllers/front.php +++ b/controllers/front.php @@ -170,6 +170,21 @@ class glmMembersFront extends GlmPluginSupport ); wp_enqueue_style('glm-members-admin-fullcalendar-css'); + // enque the script and css for asmselect + wp_enqueue_script( + 'asmselect', + GLM_MEMBERS_PLUGIN_BASE_URL . '/js/jquery.asmselect.js', + 'jquery', + '1.0.4a', + true + ); + wp_enqueue_style( + 'asmselect', + GLM_MEMBERS_PLUGIN_BASE_URL . '/css/jquery.asmselect.css', + null, + '1.0.4a' + ); + } public function glmMembersFrontCSS () { @@ -261,12 +276,61 @@ class glmMembersFront extends GlmPluginSupport public function controller ($atts, $content = null, $shortcode) { + // Start with no cache_code - This prevents caching for subordinate controller hits + $cache_code = false; + // Enque all needed scripts and css here so that only happens when we're doing something $this->glmMembersFrontScripts(); // Enqueue all css here so that only happens when we're doing something $this->glmMembersFrontCSS(); + // If cache control not active, this must be the top-level page content request + if (!$GLOBALS['cache_control']['active']) { + + + // Check shortcode for cache configuration + $scCacheControl = false; + foreach ($this->config['addOns'] as $addon) { + foreach ($addon['shortcodes'] as $sc) { + if (isset($sc['cacheControl'])) { + $scCacheControl = $sc['cacheControl']; + } + } + } + + if ($scCacheControl && $scCacheControl['active']) { + + // Create cache_code for this request + $cache_code = md5(GLM_MEMBERS_SITE_BASE_URL.print_r($atts,1).print_r($shortcode,1).print_r($_REQUEST,1)); + + // Remove all old cache entries from the database + $sql = "DELETE from ".GLM_MEMBERS_PLUGIN_DB_PREFIX."cache WHERE DATE_ADD(created, INTERVAL ".$scCacheControl['cacheTime'].") < NOW();"; + $this->wpdb->query($sql); + + // Check if this request is currently cached + $sql = "SELECT * FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX."cache WHERE cache_code = '$cache_code';"; + $cached = $this->wpdb->get_results($sql, ARRAY_A); + if (count($cached) == 1) { + + // Currently cached - Return cached content + return $cached[0]['html']; + + } + + // Setup cache control array for this request + $GLOBALS['cache_control'] = array( + 'active' => true, + 'shortcode' => $shortcode, + 'cache_code' => $cache_code, + 'complete' => false + ); + + $cache_this_request = true; + + } + } + /* * Because WordPress insists on forcing the timezone to UTC * we need to save the current timezone setting, set the one @@ -500,7 +564,6 @@ class glmMembersFront extends GlmPluginSupport // Get suggested view $view = $results['view']; - // Check for modified settings to save in conf if (isset($results['settings'])) { while (list($key, $val) = each($results['settings'])) { @@ -591,6 +654,22 @@ Request Data: ".print_r($_REQUEST,1)." // Restore timezone that was set before our code was called date_default_timezone_set($defaultTimeZone); + +// If we have a cache code, then this is a top-level caching request +if ($cache_code) { + + // Cache this request page content + $sql = $this->wpdb->prepare("INSERT INTO ".GLM_MEMBERS_PLUGIN_DB_PREFIX."cache (shortcode, cache_code, created, html) VALUES (%s, %s, now(), %s);", $shortcode, $cache_code, $out); + $this->wpdb->query($sql); + + // Mark cache control as complete + $GLOBALS['cache_control']['complete'] = true; + +} + + + + return $out; } diff --git a/index.php b/index.php index e71592c0..64468f36 100644 --- a/index.php +++ b/index.php @@ -39,7 +39,7 @@ */ define('GLM_MEMBERS_PLUGIN_VERSION', '2.9.15'); -define('GLM_MEMBERS_PLUGIN_DB_VERSION', '1.1.30'); +define('GLM_MEMBERS_PLUGIN_DB_VERSION', '1.1.31'); // Check if plugin version is not current in WordPress option and if needed updated it if (GLM_MEMBERS_PLUGIN_VERSION != get_option('glmMembersDatabasePluginVersion')) { @@ -202,6 +202,11 @@ if (!defined('ABSPATH')) { * */ +// Global Cache Control array +$cache_control = array( + 'active' => false +); + $startupNotices = ''; // Try to set the DB version option to false (new plugin) - If it's already set this won't do anything. diff --git a/models/front/members/list.php b/models/front/members/list.php index df83478f..fedb5836 100644 --- a/models/front/members/list.php +++ b/models/front/members/list.php @@ -270,21 +270,6 @@ class GlmMembersFront_members_list extends GlmDataMemberInfo } } - // enque the script and css for asmselect - wp_enqueue_script( - 'asmselect', - GLM_MEMBERS_PLUGIN_BASE_URL . '/js/jquery.asmselect.js', - 'jquery', - '1.0.4a', - true - ); - wp_enqueue_style( - 'asmselect', - GLM_MEMBERS_PLUGIN_BASE_URL . '/css/jquery.asmselect.css', - null, - '1.0.4a' - ); - // Apply any provided text search to name, description, short description, and street address if (trim($actionData['request']['text-search']) != '') { $textSearch = addslashes(filter_var($actionData['request']['text-search'], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES)); diff --git a/setup/databaseScripts/create_database_V1.1.30.sql b/setup/databaseScripts/create_database_V1.1.31.sql similarity index 99% rename from setup/databaseScripts/create_database_V1.1.30.sql rename to setup/databaseScripts/create_database_V1.1.31.sql index a217000e..807a9032 100644 --- a/setup/databaseScripts/create_database_V1.1.30.sql +++ b/setup/databaseScripts/create_database_V1.1.31.sql @@ -588,6 +588,18 @@ INSERT INTO {prefix}settings_terms ---- +-- Shortcode Output Cache +CREATE TABLE {prefix}cache ( + shortcode TINYTEXT NULL, + cache_code TINYTEXT NULL, + created DATETIME NULL, + html MEDIUMTEXT NULL, + PRIMARY KEY (cache_code(20)), + INDEX (created) +); + +---- + -- Theme Settings - Only 1 entry in this table CREATE TABLE {prefix}settings_theme ( id INT NOT NULL AUTO_INCREMENT, diff --git a/setup/databaseScripts/dbVersions.php b/setup/databaseScripts/dbVersions.php index de82af3b..152063f7 100644 --- a/setup/databaseScripts/dbVersions.php +++ b/setup/databaseScripts/dbVersions.php @@ -61,7 +61,8 @@ $glmMembersDbVersions = array( '1.1.27' => array('version' => '1.1.27', 'tables' => 19, 'date' => '03/29/17'), '1.1.28' => array('version' => '1.1.28', 'tables' => 19, 'date' => '04/04/17'), '1.1.29' => array('version' => '1.1.29', 'tables' => 19, 'date' => '04/10/17'), - '1.1.30' => array('version' => '1.1.30', 'tables' => 19, 'date' => '04/12/17') + '1.1.30' => array('version' => '1.1.30', 'tables' => 19, 'date' => '04/12/17'), + '1.1.31' => array('version' => '1.1.31', 'tables' => 20, 'date' => '04/23/17') ); diff --git a/setup/databaseScripts/drop_database_V1.1.30.sql b/setup/databaseScripts/drop_database_V1.1.31.sql similarity index 93% rename from setup/databaseScripts/drop_database_V1.1.30.sql rename to setup/databaseScripts/drop_database_V1.1.31.sql index 4977c6c9..ef327d88 100644 --- a/setup/databaseScripts/drop_database_V1.1.30.sql +++ b/setup/databaseScripts/drop_database_V1.1.31.sql @@ -23,6 +23,7 @@ DROP TABLE IF EXISTS {prefix}settings_theme, {prefix}categories, {prefix}amenity_groups, - {prefix}grouped_amenities + {prefix}grouped_amenities, + {prefix}cache ; diff --git a/setup/databaseScripts/update_database_V1.1.31.sql b/setup/databaseScripts/update_database_V1.1.31.sql new file mode 100644 index 00000000..6465d46c --- /dev/null +++ b/setup/databaseScripts/update_database_V1.1.31.sql @@ -0,0 +1,17 @@ +-- Gaslight Media Members Database +-- File Created: 04/12/17 +-- Database Version: 1.1.31 +-- Database Update From Previous Version Script +-- +-- To permit each query below to be executed separately, +-- all queries must be separated by a line with four dashes +-- Sets which character to use to separate phone numbers + +CREATE TABLE {prefix}cache ( + shortcode TINYTEXT NULL, + cache_code TINYTEXT NULL, + created DATETIME NULL, + html MEDIUMTEXT NULL, + PRIMARY KEY (cache_code(20)), + INDEX (created) +); diff --git a/setup/shortcodes.php b/setup/shortcodes.php index 9d146f50..31822ecf 100644 --- a/setup/shortcodes.php +++ b/setup/shortcodes.php @@ -53,6 +53,10 @@ * 'menu' => '{menu name}', // Menu name in this context is simply where to find the action * 'action' => '{shortcode action name}, // Action used to execute this shortcode * 'table' => '{table prefix}{table name}', // Database table where default attribute values are stored + * 'cacheControl' => array( // Short code output cache control + * 'active' => true, // Enable cache for this shortcode + * 'cacheTime' => '1 HOUR' // Cache refresh time - MySQL "DATE_ADD" time interval - see https://www.w3schools.com/sql/func_date_add.asp + * ), * 'attributes' => array( // An array of all shortcode attributes (options) * '{attr name}' => '{field name}', // Available attribute names and the database field names with the default value * .... @@ -69,6 +73,10 @@ if ( isset( $config['settings'] ) && $config['settings']['enable_members'] ) { 'menu' => 'members', 'action' => 'list', 'table' => GLM_MEMBERS_PLUGIN_DB_PREFIX.'settings_general', + 'cacheControl' => array( + 'active' => true, + 'cacheTime' => '1 HOUR' // MySQL "DATE_ADD" time interval - see https://www.w3schools.com/sql/func_date_add.asp + ), 'attributes' => array( 'type' => false, 'category' => false, -- 2.17.1