From 0b1da89ca1f3a1218690a625846bb73967dd1e9c Mon Sep 17 00:00:00 2001 From: Kunal Nagar Date: Sat, 24 Apr 2021 18:55:51 -0400 Subject: [PATCH] feat: Add Multisite Support (#51) --- admin/AdminClass.php | 4 ++-- admin/Helpers.php | 32 +++++++++++++----------------- admin/LogsClass.php | 2 +- custom-404-pro.php | 2 +- includes/ActivateClass.php | 40 ++++++++++++++++++++++++++------------ readme.txt | 5 ++++- 6 files changed, 49 insertions(+), 36 deletions(-) diff --git a/admin/AdminClass.php b/admin/AdminClass.php index d03616a..861f17a 100755 --- a/admin/AdminClass.php +++ b/admin/AdminClass.php @@ -139,7 +139,7 @@ public function custom_404_pro_admin_init() { public function custom_404_pro_redirect() { global $wpdb; if ( is_404() ) { - $sql = 'SELECT * FROM ' . $this->helpers->table_options; + $sql = 'SELECT * FROM ' . $wpdb->prefix . $this->helpers->table_options; $sql_result = $wpdb->get_results( $sql ); $row_mode = $sql_result[0]; $row_mode_page = $sql_result[1]; @@ -181,7 +181,7 @@ private function custom_404_pro_log( $is_email ) { $referer = $_SERVER['HTTP_REFERER']; } $user_agent = $_SERVER['HTTP_USER_AGENT']; - $sql_save = 'INSERT INTO ' . $this->helpers->table_logs . " (ip, path, referer, user_agent) VALUES ('$ip', '$path', '$referer', '$user_agent')"; + $sql_save = 'INSERT INTO ' . $wpdb->prefix . $this->helpers->table_logs . " (ip, path, referer, user_agent) VALUES ('$ip', '$path', '$referer', '$user_agent')"; $wpdb->query( $sql_save ); if ( ! empty( $is_email ) ) { self::custom_404_pro_send_mail( $ip, $path, $referer, $user_agent ); diff --git a/admin/Helpers.php b/admin/Helpers.php index f29c2d2..36969e9 100755 --- a/admin/Helpers.php +++ b/admin/Helpers.php @@ -14,8 +14,8 @@ public static function singleton() { public function __construct() { global $wpdb; - $this->table_options = $wpdb->prefix . 'custom_404_pro_options'; - $this->table_logs = $wpdb->prefix . 'custom_404_pro_logs'; + $this->table_options = 'custom_404_pro_options'; + $this->table_logs = 'custom_404_pro_logs'; $this->options_defaults = array(); $options_defaults_temp = array( 'mode' => '', @@ -34,12 +34,6 @@ public function __construct() { } } - // public function plugin_data() { - // $plugin_main_file = dirname(__FILE__) . '/custom-404-pro/custom-404-pro.php'; - // $plugin_data = get_plugin_data($plugin_main_file); - // return $plugin_data; - // } - public function print_pretty( $result ) { echo '
';
 		var_dump( $result );
@@ -58,7 +52,7 @@ public function initialize_table_options() {
 		global $wpdb;
         if(current_user_can('administrator')) {
             $count = count( $this->options_defaults );
-            $sql   = 'INSERT INTO ' . $this->table_options . ' (name, value) VALUES ';
+            $sql   = 'INSERT INTO ' . $wpdb->prefix . $this->table_options . ' (name, value) VALUES ';
             foreach ( $this->options_defaults as $key => $option ) {
                 if ( $key !== ( $count - 1 ) ) {
                     $sql .= "('" . $option->name . "', '" . $option->value . "'),";
@@ -73,7 +67,7 @@ public function initialize_table_options() {
 	public function is_option( $option_name ) {
 		global $wpdb;
         if(current_user_can('administrator')) {
-    		$query  = 'SELECT * FROM ' . $this->table_options . " WHERE name='" . $option_name . "'";
+    		$query  = 'SELECT * FROM ' . $wpdb->prefix . $this->table_options . " WHERE name='" . $option_name . "'";
     		$result = $wpdb->get_results( $query );
     		if ( empty( $result ) ) {
     			return false;
@@ -86,7 +80,7 @@ public function is_option( $option_name ) {
 	public function get_option( $option_name ) {
 		global $wpdb;
         if(current_user_can('administrator')) {
-    		$query  = 'SELECT value FROM ' . $this->table_options . " WHERE name='" . $option_name . "'";
+    		$query  = 'SELECT value FROM ' . $wpdb->prefix . $this->table_options . " WHERE name='" . $option_name . "'";
     		$result = $wpdb->get_var( $query );
     		return $result;
         }
@@ -96,7 +90,7 @@ public function insert_option( $option_name, $option_value ) {
 		global $wpdb;
         if(current_user_can('administrator')) {
     		$result = $wpdb->insert(
-    			$this->table_options,
+    			$wpdb->prefix . $this->table_options,
     			array(
     				'name'  => $option_name,
     				'value' => $option_value,
@@ -110,7 +104,7 @@ public function update_option( $option_name, $option_value ) {
 		global $wpdb;
         if(current_user_can('administrator')) {
     		$result = $wpdb->update(
-    			$this->table_options,
+    			$wpdb->prefix . $this->table_options,
     			array( 'value' => $option_value ),
     			array( 'name' => $option_name )
     		);
@@ -133,7 +127,7 @@ public function upsert_option( $option_name, $option_value ) {
 	public function get_logs_columns() {
 		global $wpdb;
         if(current_user_can('administrator')) {
-    		$query  = 'SHOW COLUMNS FROM ' . $this->table_logs;
+    		$query  = 'SHOW COLUMNS FROM ' . $wpdb->prefix . $this->table_logs;
     		$result = $wpdb->get_results( $query );
     		return $result;
         }
@@ -162,7 +156,7 @@ public function create_logs( $logsData, $isDeletingOld ) {
         if(current_user_can('administrator')) {
     		$count  = count( $logsData );
     		$logIDs = [];
-    		$query  = 'INSERT INTO ' . $this->table_logs . ' (ip, path, referer, user_agent) VALUES';
+    		$query  = 'INSERT INTO ' . $wpdb->prefix . $this->table_logs . ' (ip, path, referer, user_agent) VALUES';
     		foreach ( $logsData as $key => $log ) {
     			if ( ! empty( $log->id ) ) {
     				array_push( $logIDs, $log->id );
@@ -185,7 +179,7 @@ public function create_logs( $logsData, $isDeletingOld ) {
 	public function get_logs() {
 		global $wpdb;
         if(current_user_can('administrator')) {
-    		$query  = 'SELECT * from ' . $this->table_logs;
+    		$query  = 'SELECT * from ' . $wpdb->prefix . $this->table_logs;
     		$result = $wpdb->get_results( $query, ARRAY_A );
     		return $result;
         }
@@ -195,11 +189,11 @@ public function delete_logs( $path ) {
 		global $wpdb;
         if(current_user_can('administrator')) {
     		if ( $path === 'all' ) {
-    			$query = 'TRUNCATE TABLE ' . $this->table_logs;
+    			$query = 'TRUNCATE TABLE ' . $wpdb->prefix . $this->table_logs;
     		} elseif ( is_array( $path ) ) {
-    			$query = 'DELETE FROM ' . $this->table_logs . ' WHERE id in (' . implode( ',', $path ) . ')';
+    			$query = 'DELETE FROM ' . $wpdb->prefix . $this->table_logs . ' WHERE id in (' . implode( ',', $path ) . ')';
     		} else {
-    			$query = 'DELETE FROM ' . $this->table_logs . ' WHERE id=' . $path . '';
+    			$query = 'DELETE FROM ' . $wpdb->prefix . $this->table_logs . ' WHERE id=' . $path . '';
     		}
     		$result = $wpdb->query( $query );
     		return $result;
diff --git a/admin/LogsClass.php b/admin/LogsClass.php
index 8e6440c..841bcc8 100755
--- a/admin/LogsClass.php
+++ b/admin/LogsClass.php
@@ -37,7 +37,7 @@ public function prepare_items() {
 		$sortable              = self::get_sortable_columns();
 		$this->_column_headers = array( $columns, $hidden, $sortable );
 		$helpers               = Helpers::singleton();
-		$sql                   = 'SELECT * FROM ' . $helpers->table_logs;
+		$sql                   = 'SELECT * FROM ' . $wpdb->prefix . $helpers->table_logs;
 
 		if ( array_key_exists( 'orderby', $_GET ) ) {
 			$order_by = esc_html($_GET['orderby']);
diff --git a/custom-404-pro.php b/custom-404-pro.php
index 6a82fd9..e0cb198 100755
--- a/custom-404-pro.php
+++ b/custom-404-pro.php
@@ -4,7 +4,7 @@
 Plugin Name: Custom 404 Pro
 Plugin URI: https://wordpress.org/plugins/custom-404-pro/
 Description: Override the default 404 page with any page or a custom URL from the Admin Panel.
-Version: 3.2.21
+Version: 3.3.0
 Author: Kunal Nagar
 Author URI: https://www.kunalnagar.in
 License: GPL-2.0+
diff --git a/includes/ActivateClass.php b/includes/ActivateClass.php
index 50d860f..bbad08c 100755
--- a/includes/ActivateClass.php
+++ b/includes/ActivateClass.php
@@ -2,27 +2,43 @@
 
 class ActivateClass {
 
+    static function _activate() {
+        global $wpdb;
+        $helpers                = Helpers::singleton();
+        $is_table_options_query = "SHOW TABLES LIKE '" . $wpdb->prefix . $helpers->table_options . "';";
+        $is_table_logs_query    = "SHOW TABLES LIKE '" . $wpdb->prefix . $helpers->table_logs . "';";
+        $is_table_options       = $wpdb->query( $is_table_options_query );
+        $is_table_logs          = $wpdb->query( $is_table_logs_query );
+        if ( empty( $is_table_options ) && empty( $is_table_logs ) ) {
+            self::create_tables();
+            self::initialize_options();
+        }
+    }
+
 	public static function activate() {
 		global $wpdb;
         if(current_user_can('administrator')) {
-    		$helpers                = Helpers::singleton();
-    		$is_table_options_query = "SHOW TABLES LIKE '" . $helpers->table_options . "';";
-    		$is_table_logs_query    = "SHOW TABLES LIKE '" . $helpers->table_logs . "';";
-    		$is_table_options       = $wpdb->query( $is_table_options_query );
-    		$is_table_logs          = $wpdb->query( $is_table_logs_query );
-    		if ( empty( $is_table_options ) && empty( $is_table_logs ) ) {
-    			self::create_tables();
-    			self::initialize_options();
-    		}
+            if(is_multisite()) {
+                $sites = get_sites(['fields'=>'ids']);
+                foreach ($sites as $blog_id) {
+                    switch_to_blog($blog_id);
+                    self::_activate();
+                    restore_current_blog();
+                }
+            } else {
+                self::_activate();
+            }
         }
 	}
 
 	public static function create_tables() {
 		global $wpdb;
+        $helpers                = Helpers::singleton();
+        $table_options = $wpdb->prefix . $helpers->table_options;
+        $table_logs = $wpdb->prefix . $helpers->table_logs;
         if(current_user_can('administrator')) {
     		$charset_collate = $wpdb->get_charset_collate();
-    		$helpers         = Helpers::singleton();
-    		$sql_logs        = "CREATE TABLE $helpers->table_logs (
+    		$sql_logs        = "CREATE TABLE $table_logs (
     			id mediumint(9) NOT NULL AUTO_INCREMENT,
     			ip text,
     			path text,
@@ -32,7 +48,7 @@ public static function create_tables() {
     			updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
     		  	PRIMARY KEY (id)
     		) $charset_collate;";
-    		$sql_options     = "CREATE TABLE $helpers->table_options (
+    		$sql_options     = "CREATE TABLE $table_options (
     			id mediumint(9) NOT NULL AUTO_INCREMENT,
     			name text,
     			value text,
diff --git a/readme.txt b/readme.txt
index 6ab84ad..9ec0d41 100755
--- a/readme.txt
+++ b/readme.txt
@@ -4,7 +4,7 @@ Donate link: https://www.paypal.me/kunalnagar/10
 Tags: wordpress, 404, 404 error page, 404 link, 404 page, broken link, custom 404, custom 404 error, custom 404 error page, custom 404 page, customize 404, customize 404 error page, customize 404 page, error, error page, missing, page, page not found, page not found error
 Requires at least: 3.0.1
 Tested up to: 5.7
-Stable tag: 3.2.21
+Stable tag: 3.3.0
 License: GPLv2 or later
 License URI: http://www.gnu.org/licenses/gpl-2.0.html
 
@@ -55,6 +55,9 @@ Uninstall the plugin from the Plugins page (important!) and reinstall it. Never
 
 == Changelog ==
 
+= 3.3.0 =
+* Add Multisite Support
+
 = 3.2.21 =
 * Support WordPress 5.7