From: Chuck Scott Date: Tue, 24 Apr 2018 17:50:28 +0000 (-0400) Subject: Added GLM Associate Cron - See bottom of index.php for details. X-Git-Tag: v2.10.30^2~6 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=ef39bf2e5fa0b8efe4683457a09b37c2e3614544;p=WP-Plugins%2Fglm-member-db.git Added GLM Associate Cron - See bottom of index.php for details. --- diff --git a/activate.php b/activate.php index d554fc7d..86b660b8 100644 --- a/activate.php +++ b/activate.php @@ -95,6 +95,7 @@ class glmMembersPluginActivate extends glmPluginSupport } else { update_option(GLM_MEMBERS_PLUGIN_OPTION_FIRST_ACTIVATION, 'false'); } + } /* diff --git a/controllers/admin.php b/controllers/admin.php index 611838df..16fe3837 100644 --- a/controllers/admin.php +++ b/controllers/admin.php @@ -123,8 +123,8 @@ class glmMembersAdmin extends GlmPluginSupport public function __construct ($wpdb, $config) { - if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG_VERBOSE) { - trigger_error(glmAssociateMemoryUsage(),E_USER_NOTICE); + if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) { + trigger_error(glmAssociateMemoryUsage().' - Start Admin Controller Constructor',E_USER_NOTICE); } // Save WordPress Database object @@ -234,8 +234,8 @@ class glmMembersAdmin extends GlmPluginSupport } - if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG_VERBOSE) { - trigger_error(glmAssociateMemoryUsage(),E_USER_NOTICE); + if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) { + trigger_error(glmAssociateMemoryUsage().' - End Admin Controller Constructor',E_USER_NOTICE); } } @@ -257,8 +257,8 @@ class glmMembersAdmin extends GlmPluginSupport $defaultTimeZone = date_default_timezone_get(); date_default_timezone_set($this->config['settings']['time_zone']); - if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG_VERBOSE) { - trigger_error(glmAssociateMemoryUsage(),E_USER_NOTICE); + if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) { + trigger_error(glmAssociateMemoryUsage().' - Start Admin Controller AJAX',E_USER_NOTICE); } // Get the model action @@ -383,8 +383,8 @@ class glmMembersAdmin extends GlmPluginSupport // Generate output from model data and view $smarty->template->display($viewFile); - if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG_VERBOSE) { - trigger_error(glmAssociateMemoryUsage(),E_USER_NOTICE); + if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) { + trigger_error(glmAssociateMemoryUsage().' - End Admin Controller AJAX',E_USER_NOTICE); } // Restore timezone that was set before our code was called @@ -691,14 +691,17 @@ class glmMembersAdmin extends GlmPluginSupport * @param string $menuItem Name of the menu item that is being processed * @param string $action Specified Action to take or false * @param array $actionData Optional array of data to pass to the selected action + * @param boolean $returnOutput Optionally tell controller to return the output rather than display it + * @param boolean $forceAction Optionally tell controller it must run the supplied action + * This last paramter can prevent a submit paramter from overriding the action. * * @return void * @access public */ - public function controller( $menuItem, $action = false, $actionData = false, $returnOutput = false ) + public function controller( $menuItem, $action = false, $actionData = false, $returnOutput = false, $forceAction = false ) { - if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG_VERBOSE) { - trigger_error(glmAssociateMemoryUsage(),E_USER_NOTICE); + if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) { + trigger_error(glmAssociateMemoryUsage().' - Start Admin Controller',E_USER_NOTICE); } // Enque admin scripts and css here so that only happens when we're doing something @@ -753,9 +756,15 @@ class glmMembersAdmin extends GlmPluginSupport $action = 'index'; } - // Get any requested "action" from a form submission and modify path/name - // accordingly. This modifies the previously set $action. - if (!$actionData && isset($_REQUEST['glm_action']) && $_REQUEST['glm_action'] != '') { + /* + * Get any requested "action" from a form submission and modify path/name + * accordingly. + * + * The 'glm_action' REQUEST parameter used to override the $action supplied to this controller + * but that behavior has been changed so that it doesn't. Not sure why we did that. + * + */ + if (!$forceAction && !$actionData && isset($_REQUEST['glm_action']) && $_REQUEST['glm_action'] != '') { $a = sanitize_text_field($_REQUEST['glm_action']); if ($a != '') { $action = $a; @@ -805,7 +814,7 @@ class glmMembersAdmin extends GlmPluginSupport if (! isset($this->config['validActions']['adminActions'][$menuItem]) || ! isset($this->config['validActions']['adminActions'][$menuItem][$action])) { - trigger_error('Error in Admin Controller: Requested Menu Item is invalid! - '.$menuItem, E_USER_NOTICE); + trigger_error("Error in Admin Controller: Requested Menu Item is invalid! - $menuItem/$action", E_USER_NOTICE); $menuItem = 'error'; $action = 'badAction'; @@ -894,6 +903,11 @@ class glmMembersAdmin extends GlmPluginSupport $modelRedirect = true; } + // If there's no view and no data returned, assume it doesn't want a view processd and just return; + if (!$results['view'] && !$results['data']) { + return false; + } + // Get the specified view file path and name $view = false; if (isset($results['view'])) { @@ -1035,8 +1049,11 @@ class glmMembersAdmin extends GlmPluginSupport // Restore timezone that was set before our code was called date_default_timezone_set($defaultTimeZone); + if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) { + trigger_error(glmAssociateMemoryUsage().' - End Admin Controller',E_USER_NOTICE); + } + if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG_VERBOSE) { - trigger_error(glmAssociateMemoryUsage(),E_USER_NOTICE); $smarty->template->display( 'admin/smartyDebug.html'); } diff --git a/controllers/front.php b/controllers/front.php index ea13c493..5411e814 100644 --- a/controllers/front.php +++ b/controllers/front.php @@ -41,7 +41,7 @@ class glmMembersFront extends GlmPluginSupport { if (GLM_MEMBERS_PLUGIN_FRONT_DEBUG_VERBOSE) { - trigger_error(glmAssociateMemoryUsage(),E_USER_NOTICE); + trigger_error(glmAssociateMemoryUsage()." - Controller Constructor Start",E_USER_NOTICE); } // Save WordPress Database object @@ -103,7 +103,7 @@ class glmMembersFront extends GlmPluginSupport add_action('glmMembersFrontCSSFilter',array($this,'glmMembersFrontCSS')); if (GLM_MEMBERS_PLUGIN_FRONT_DEBUG_VERBOSE) { - trigger_error(glmAssociateMemoryUsage(),E_USER_NOTICE); + trigger_error(glmAssociateMemoryUsage()." - Controller Constructor End",E_USER_NOTICE); } } diff --git a/deactivate.php b/deactivate.php index 8bc76d17..40cd9c5f 100644 --- a/deactivate.php +++ b/deactivate.php @@ -61,7 +61,13 @@ class glmMembersPluginDeactivate extends glmPluginSupport $this->config = $config; */ - // Not doing anything here for now. + // Unschedule GLM Assoicate Cron + $timestamp = wp_next_scheduled( 'glm_associate_cron' ); + if ($timestamp) { + trigger_error("Unscheduling glm_associate_cron @ ".date('r', $timestamp)); + wp_unschedule_event( $timestamp, 'glm_associate_cron' ); + } + } } diff --git a/index.php b/index.php index b31d9e8b..c3b89287 100644 --- a/index.php +++ b/index.php @@ -223,7 +223,7 @@ require_once 'defines.php'; require_once 'config.php'; if (GLM_MEMBERS_PLUGIN_DEBUG_VERBOSE) { - trigger_error(glmAssociateMemoryUsage(),E_USER_NOTICE); + trigger_error(glmAssociateMemoryUsage()." - Start glm-member-db setup",E_USER_NOTICE); } /* @@ -424,7 +424,7 @@ if (function_exists('is_user_logged_in')) { } if (GLM_MEMBERS_PLUGIN_DEBUG_VERBOSE) { - trigger_error(glmAssociateMemoryUsage(),E_USER_NOTICE); + trigger_error(glmAssociateMemoryUsage()." - End glm-member-db setup",E_USER_NOTICE); } /* @@ -733,3 +733,110 @@ function glmAssociateMemoryUsage() { $mu = $m - $GLOBALS['glmAssociateMemTracker']; return 'GLM Associate Memory Usage: '.number_format($mu,0).' of '.number_format($m,0).' Bytes'; } + +/* + * GLM Associate Cron + * + * All cron actions requested by this plugin or add-ons are registerd by using the + * "glm_associate_cron_request" add_filter(). That filter supplies a blank array and anything + * requesting a cron action will add an array element that describes what needs to be + * done as described below. Note that a plugin may provide more than one entry if it + * has multiple actions it wants called. + * + * The action will be executed at the correct time by running apply_filter() with + * the name of {plugin}-{actionID}. That callback that will include an array like + * the second one below. + * + * When an action add_filter is called, it is supplied with a copy of the + * "params" array suplied when the action was requested and the day of week and hour + * being executed. + * + * On completion of the action, the action filter will return a success flag + * and an optional message stating what was completed or what failed. + * + * An action request should be to execute a set of tasks that are related rather + * than for each thing that needs to be processed to keep from cluttering the requests. + * + * NOTE that a plugin only needs to provide an action request that it wants executed. + * via the "glm_associate_cron_request" filter when it needs to be on the schedule. There is + * no need to tell GLM Associate Cron when something is no longer needed. This avoids + * the need for the add-ons to have something in the activation/deactivation scripts + * for this cron. + * + * An appropriate place for the " + * + * Array supplied when requesting an action + * + * array( + * 'menu' => Menu assciated with requested action. + * 'action' => Model to be run - The admin controller will be used to + * run this model. + * 'daysOfWeek'=> Array of days on which the action should take place. + * 1 to 7 starting with Sunday. If false call action on all days. + * 'times' => Array of hours on which the action should take place. + * These are numeric hours using 24 hour clock. + * If false then call this action at the next hour. + * 'params' => Array of parameters that will be passedd to the action + * These parameters allow one callback to perform various + * actions at the requested times. + * ) + * + * Array provided to callback function when executing the action + * + * array( + * 'day' => Day of week on which the action is being called, + * 'time' => Integer hour at which the action is being called, + * 'params' => The 'params' array for this action + * ) + * + * Array returned by the callback function when done performing the action. + * NOTE: Logging of this information is a future feature, so this return array + * is currently optional. + * + * array( + * 'success' => True if successful + * 'Message' => Additional message describing what was accomplished for cron log + * ) + * + * Flow of GLM Associate Cron + * + * 1) activate.php adds a scheduled event for WordPress to run 'glm_associate_cron' (below) + * every hour on the half hour. + * 2) deactivate.php removes the 'glm_asscoiate_cron' scheduled event when deactivating this plugin. + * 3) GLM Associate plugins/add-ons request cron actions using the 'glm_associate_cron_request' + * add_action which should be in their setup/adminHooks.php file. (see sample in main plugin) + * 3) The add_action() below sets up action 'glm_associate_cron' to call do_glm_associate_cron(). + * 4) When called, do_glm_associate_cron() does an AJAX request to models/admin/ajax/glmCron.php. + * 5) When AJAX action glmCron is run it will apply 'glm_associate_cron_request' filters to get + * any requested cron actions from any GLM Associate plugin/add-on. It then calls each model + * requested in sequence that asked to be run at that time. + * + * NOTE: Since the result of the scheduled event is to run an AJAX mode, that model may be run + * at any time by entering the proper URL into a browser. + * + * http://{hostname}/wp-admin/admin-ajax.php?action=glm_members_admin_ajax&glm_action=glmCron + */ + +// Called by WordPress each hour on the half hour. (see activate.php) +add_action('glm_associate_cron', 'do_glm_associate_cron'); +function do_glm_associate_cron() { + + if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) { + trigger_error('Run GLM Associate Cron', E_USER_NOTICE); + } + + // Do AJAX call to glmCron.php + wp_remote_get(admin_url().'admin-ajax.php?action=glm_members_admin_ajax&glm_action=glmCron'); + +} + +// +// If no cron schedue is set, add it now to start at 30 min after the hour during the next hour. +$timestamp = wp_next_scheduled( 'glm_associate_cron' ); +if (!$timestamp) { + $t = strtotime(date('Y-m-d G:00 e')." + 90 minutes"); + trigger_error('No Cron Set: Scheduling glm_cron to start @ '.date('r', $t), E_USER_NOTICE); + wp_schedule_event( $t, 'hourly', 'glm_associate_cron' ); +} + + diff --git a/models/admin/ajax/glmCron.php b/models/admin/ajax/glmCron.php new file mode 100644 index 00000000..52babfb7 --- /dev/null +++ b/models/admin/ajax/glmCron.php @@ -0,0 +1,110 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @version 0.1 + */ + +require_once GLM_MEMBERS_PLUGIN_PATH . '/controllers/admin.php'; + +/* + * This class exports the currently selected members list + * to a printable HTML file, to a CSV file, or otherwise. + */ +class GlmMembersAdmin_ajax_glmCron extends glmMembersAdmin +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + /** + * Plugin Configuration Data + * + * @var $config + * @access public + */ + public $config; + + /* + * Constructor + * + * This contructor sets up this model. At this time that only includes + * storing away the WordPress data object. + * + * @return object Class object + * + */ + public function __construct ($wpdb, $config) + { + + // Save WordPress Database object + $this->wpdb = $wpdb; + + // Save plugin configuration object + $this->config = $config; + + } + + /* + * Perform requested actions for a GLM Cron instance + */ + public function modelAction ($actionData = false) + { + + $run = 0; + + // Get day and hour this is being run - force hours to numeric + $thisDay = date('N'); + $thisHour = (date('G')-0); + + if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) { + trigger_error("glmCron processing started: day = $thisDay, hour = $thisHour", E_USER_NOTICE); + } + + // Get requested cron actions + $cronActions = apply_filters('glm_associate_cron_request', array()); + + + // Loop through these actions and call what's appropriate for the current time + if (is_array($cronActions)) { + if (count($cronActions) > 0) { + + foreach ($cronActions as $act) { + + // Check if this action should be run now + if (in_array($thisDay, $act['daysOfWeek']) && in_array($thisHour, $act['times'])) { + + // Run the action by calling the controller - force controller to use supplied action (no REQUEST override) + $this->controller($act['menu'], $act['action'], $act['params'], false, true); + $run++; + + } + } + + } + } else { + trigger_error('GLM Associate Cron: Bad request array!', E_USER_WARNING); + } + + if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) { + trigger_error("GLM Cron processed $run matching day and hour out of ".count($cronActions).' total requested.', E_USER_NOTICE); + } + + // This AJAX action does not return to anything. + wp_die(); + + } + +} diff --git a/setup/adminHooks.php b/setup/adminHooks.php index ffb8267b..7ad18ad3 100644 --- a/setup/adminHooks.php +++ b/setup/adminHooks.php @@ -26,6 +26,33 @@ * Also note that parameters will be in the context of the main admin controller constructor. */ +/* + * Add an action to GLM Associate Cron + * + * See description for "GLM Associate Cron" in main plugin index.php file. + */ +/* Sample add cron request for testing. +add_filter('glm_associate_cron_request', function($request) { + + // Sanity check - Must be passed an array + if (is_array($request)) { + + $request[] = array( + 'menu' => GLM_MEMBERS_PLUGIN_UNDERSCORED_NAME, // Menu where action exists + 'action' => 'test_action', // Name of action to run + 'daysOfWeek'=> false, // All Days + 'times' => false, // All Hours + 'params' => false // Any parameters needed by the triggered action + ); + + } else { + trigger_error('Cron Request filter recieved bad request array!', E_USER_WARNING); + } + + return $request; + +}, 10, 1); +*/ // Add content to member detail page add_filter('glm-member-db-admin-management-hooksHelp', function($content) { diff --git a/setup/validActions.php b/setup/validActions.php index 28f84bc3..cde1cd42 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -43,7 +43,8 @@ $glmMembersValidActions = array( 'fileLibraryUpload' => 'glm-member-db', 'fileLibraryDownload' => 'glm-member-db', 'fileLibraryDelete' => 'glm-member-db', - 'fileLibraryUpdate' => 'glm-member-db' + 'fileLibraryUpdate' => 'glm-member-db', + 'glmCron' => 'glm-member-db' ), 'dashboard' => array( 'index' => 'glm-member-db',