Delete old notices.
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 1 Oct 2019 15:54:57 +0000 (11:54 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 1 Oct 2019 15:54:57 +0000 (11:54 -0400)
Notices not found from gaslight site should be deleted.

classes/GlmServerStats.php
index.php
readme.txt

index 694babd..bb7df65 100644 (file)
@@ -155,7 +155,7 @@ class GlmServerStats
             $notices      = array();
             $response     = \wp_remote_get( $fetchUrl );
             $message_data = \wp_remote_retrieve_body( $response );
-            $status       = \wp_remote_retrieve_response_code( $response);
+            $status       = \wp_remote_retrieve_response_code( $response );
 
             if ( $status == 200 ) {
                 $notices = json_decode( $message_data, true );
@@ -163,6 +163,9 @@ class GlmServerStats
 
             set_transient( 'ServerStatCacheUpdate', true, 20 * MINUTE_IN_SECONDS );
 
+            // Delete any old ones no longer existing.
+            $this->deletOldNotices();
+
             if ( !empty( $notices ) ) {
                 if ( GLM_SERVERSTATS_PLUGIN_DEBUG ) {
                     trigger_error( 'updating cache', E_USER_NOTICE );
@@ -175,10 +178,44 @@ class GlmServerStats
                     trigger_error( 'no new ones found', E_USER_NOTICE );
                 }
             }
+
         }
 
     }
 
+    public function deletOldNotices()
+    {
+        $fetchUrl     = GLM_SERVERSTATS_PLUGIN_RELAY_URL . '&option=active';
+        $notices      = array();
+        $response     = \wp_remote_get( $fetchUrl );
+        $message_data = \wp_remote_retrieve_body( $response );
+        $status       = \wp_remote_retrieve_response_code( $response );
+
+        if ( $status == 200 ) {
+            $notices = json_decode( $message_data, true );
+
+            $activeIds = array();
+            if ( $notices ) {
+                foreach ( $notices as $notice ) {
+                    $activeIds[] = $notice['id'];
+                }
+            }
+
+            if ( !empty( $activeIds ) ) {
+                // Delete any not found.
+                $this->wpdb->query( "DELETE FROM {$this->prefix}{$this->cacheTableName} WHERE id NOT IN (" . implode( ',', $activeIds ) . ")" );
+                if ( GLM_SERVERSTATS_PLUGIN_DEBUG ) {
+                    trigger_error( 'Delete where not in ' . implode( ',', $activeIds ), E_USER_NOTICE );
+                }
+            } else {
+                $this->wpdb->query( "DELETE FROM {$this->prefix}{$this->cacheTableName}" );
+                if ( GLM_SERVERSTATS_PLUGIN_DEBUG ) {
+                    trigger_error( 'Delete all', E_USER_NOTICE );
+                }
+            }
+        }
+    }
+
     public function cacheNotice( $notice )
     {
         // Check to see if the notice is already cached.
index ccacc43..caa49e9 100755 (executable)
--- a/index.php
+++ b/index.php
@@ -3,7 +3,7 @@
  * Plugin Name: GLM Bandwidth
  * Plugin URI: http://www.gaslightmedia.com/
  * Description: Gaslight Media Website Bandwidth Display and Reporting
- * Version: 4.1.4
+ * Version: 4.1.5
  * Author: Gaslight Media
  * Author URI: http://www.gaslightmedia.com/
  * License: GPL2
@@ -19,7 +19,7 @@
  * @package glmServerStats
  * @author Chuck Scott <cscott@gaslightmedia.com>
  * @license http://www.gaslightmedia.com Gaslightmedia
- * @version 4.1.4
+ * @version 4.1.5
  */
 
 // Check that we're being called by WordPress.
@@ -28,7 +28,7 @@ if (!defined('ABSPATH')) {
     die();
 }
 
-define('GLM_SERVERSTATS_PLUGIN_VERSION', '4.1.4');
+define('GLM_SERVERSTATS_PLUGIN_VERSION', '4.1.5');
 
 define('GLM_SERVERSTATS_PLUGIN_DB_VERSION', '0.0.1');
 
index a000eaa..efa95c2 100755 (executable)
@@ -29,6 +29,10 @@ e.g.
 1. Activate the plugin through the 'Plugins' menu in WordPress
 
 == Changelog ==
+= 4.1.5 =
+* Delete any old notices not found on main relay plugin when doing updates to
+  cache.
+
 = 4.1.4 =
 * Fixed forced cookie pop-up not showing if cookie pop-up was off for site.