Skip to content

Commit

Permalink
feat: Add Multisite Support (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kunal Nagar authored Apr 24, 2021
1 parent adbf2b8 commit 0b1da89
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 36 deletions.
4 changes: 2 additions & 2 deletions admin/AdminClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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 );
Expand Down
32 changes: 13 additions & 19 deletions admin/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '',
Expand All @@ -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 '<pre>';
var_dump( $result );
Expand All @@ -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 . "'),";
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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,
Expand All @@ -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 )
);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 );
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion admin/LogsClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
2 changes: 1 addition & 1 deletion custom-404-pro.php
Original file line number Diff line number Diff line change
Expand Up @@ -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+
Expand Down
40 changes: 28 additions & 12 deletions includes/ActivateClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 0b1da89

Please sign in to comment.