Setup the cron task and make sure we cleaning up
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 25 Aug 2016 13:25:09 +0000 (09:25 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 25 Aug 2016 13:25:09 +0000 (09:25 -0400)
Clean up by deactivate class.
Deactivating the plugin will clear all cron tasks for this plugin.

deactivate.php
defines.php
index.php

index eef378a..bc8c2da 100644 (file)
@@ -56,6 +56,12 @@ class glmMembersLeadsPluginDeactivate
 
         // Delete our version from WordPress Options
         delete_option('glmMembersDatabaseLeadsPluginVersion');
+        $this->clear_cron_schedules();
     }
+    public function clear_cron_schedules()
+    {
+        wp_clear_scheduled_hook( GLM_MEMBERS_LEADS_PLUGIN_CRON_EVENT );
+    }
+
 
 }
index afd677d..b28b20e 100644 (file)
@@ -31,6 +31,10 @@ if (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443') {
 }
 define('GLM_MEMBERS_LEADS_PLUGIN_HTTP_PROTOCOL', $pageProtocol);
 
+// Cron defines
+define( 'GLM_MEMBERS_LEADS_PLUGIN_CRON_EVENT', 'glm_membersLeads_cron' );
+define( 'GLM_MEMBERS_LEADS_PLUGIN_CRON_RECURRANCE', 'twicedaily' );
+
 // Get various pieces of the URL
 $urlParts = parse_url(get_bloginfo('url'));
 $pageUri = explode('?', $_SERVER['REQUEST_URI']);               // Bust this up to access URL path and script name only
index 11763ba..fbcdd03 100644 (file)
--- a/index.php
+++ b/index.php
@@ -200,7 +200,7 @@ add_filter('glm-member-db-register-addon','glmMembersRegisterLeads', 10, 1);
  function glmMembersLeadsPluginDeactivate ()
  {
      global $wpdb, $config;
-     require_once (GLM_MEMBERS_LEADS_PLUGIN_PATH . '/deactivate.php');
+     require_once GLM_MEMBERS_LEADS_PLUGIN_PATH . '/deactivate.php';
      $x = new glmMembersLeadsPluginDeactivate($wpdb, $config);
      return false;
  }
@@ -232,3 +232,23 @@ class glm_member_db_leads_bootstrap {
     }
 
 }
+
+// Cron task to generate the Leads Stats
+add_action(
+    GLM_MEMBERS_LEADS_PLUGIN_CRON_EVENT,
+    function( $id ) {
+        if ( $id ) {
+            $admin_url                   = admin_url();
+            $ical_feed_import_url        = $admin_url
+                . 'admin-ajax.php?action=glm_members_admin_ajax&glm_action=statCollector';
+            $response                    = wp_remote_get( $ical_feed_import_url );
+            $response_headers            = wp_remote_retrieve_headers( $response );
+            $response_body               = wp_remote_retrieve_body( $response );
+        }
+    },
+    10,
+    1
+);
+if ( !wp_next_scheduled( GLM_MEMBERS_LEADS_PLUGIN_CRON_EVENT ) ) {
+    wp_schedule_event( time(), GLM_MEMBERS_LEADS_PLUGIN_CRON_RECURRANCE, GLM_MEMBERS_LEADS_PLUGIN_CRON_EVENT );
+}