<?php
/*
Plugin Name: BPS Pro MU Tools
Description: To turn Off AutoRestore click the "Turn Off AutoRestore" link. When AutoRestore is Off you will see "AutoRestore is turned Off" in green font. To turn On any of these WordPress Automatic Update options/filters click the links. When any of these WordPress Automatic Update options/filters are turned On that means that particular WP Automatic Update option/filter is enabled and the link will be displayed in green font. When any of these WordPress Automatic Update options/filters are turned Off that means that particular WP Automatic Update option/filter is not in use. It does not mean that particular WP Automatic Update filter is disabling or turning Off a particular WP Automatic Update. For additional help info about each of these WordPress Automatic Update options/filters click the "WordPress Automatic Update Help Forum Topic" link below. &bull; Disable all Updates: On = All WordPress Automatic Updates: Core, Plugins, Themes and Translations will be disabled. &bull; Disable all Core Updates: On = All WordPress Core Automatic Updates: Development, Minor and Major versions are disabled. &bull; Enable all Core Updates: On = All WordPress Core Automatic Updates: Development, Minor and Major versions are enabled. &bull; Enable Development Updates: On = WordPress Core Automatic Updates are enabled for Development WP versions. &bull; Enable Minor Updates: On = WordPress Core Automatic Updates are enabled for Minor WP versions. &bull; Enable Major Updates: On = WordPress Core Automatic Updates are enabled for Major WP versions.
Version: 13.0
Author: AITpro
Author URI: https://forum.ait-pro.com/forums/forum/bulletproof-security-pro/
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/

## IMPORTANT!!!!: error_reporting(0); cannot be used generally in this file or all PHP Error Logging will be broken, but can be used safely within certain conditions.
## Important Note: If you would like to add additional customizations to this file it is recommended that you make a copy of this file after you make any customizations. 
## Most likely additional things will be added/created in this BPS Pro must-use file at a later time. 
## If you customize this BPS file then you will lose your customizations if/when this file is updated in the future.
## The MU Tools auto-update function: bpsPro_mu_tools_plugin_copy() is located in general-functions.php at code line: 1427
## 12.9: Added Toggle Action Links and DB option so that BPS Pro MU Tools can be enabled or disabled.
## 13.3: Added CSRF Nonce verification to Toggle GET links.
## 13.3.2: BugFix for Network Plugins blank page and error message: Sorry, you are not allowed to access this page.
## 13.4: BugFix for SSL sites nonce verification failing.
## 13.7: All functions have been disabled. This must-use plugin may be used for something later so keeping it for now.
## 14: Adding a new AutoRestore Deactivate/Turn Off option.
## 14: Changed this function name: bpsPro_mu_plugin_actlinks() to bpsPro_mu_plugin_actlinks_pro() to prevent a PHP fatal error with the BPS free mu plugin file.
## 14: Changed this function name: bpsPro_toggle_links() to bpsPro_toggle_links_pro() to prevent a PHP fatal error with the BPS free mu plugin file.
## 14.6: Changed this function name: bpsPro_plugin_deactivation_check() to bpsPro_plugin_deactivation_check_pro() to prevent a PHP fatal error with the BPS free mu plugin file.
## 14.7: Added WP Automatic Update options/filters. Commented out the MU Tools enable|disable link code.

// Turns ARQ Off by visiting the frontend or backend of the site if the /bulletproof-security/ plugin folder has been renamed or deleted.
// Writes a log entry and sends an email alert once every 5 minutes.
// 12.9: Added ARQ Status condition so that email will not be sent when the BPS Pro plugin is being upgraded.
function bpsPro_plugin_folder_check_ARQ_off() {
	
	$MUTools_Options = get_option('bulletproof_security_options_MU_tools');
	
	if ( @$MUTools_Options['bps_mu_tools_enable_disable'] == 'disable' ) {
		return;
	}

	if ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON === true ) {
		return;
	}

	if ( time() > $MUTools_Options['bps_mu_tools_timestamp'] ) {

		if ( ! is_dir( WP_PLUGIN_DIR . '/bulletproof-security' ) ) {
		
			$ARQoptions = get_option('bulletproof_security_options_ARCM');
		
			$BPS_Options = array(
			'bps_autorestore_cron' 				=> 'Off', 
			'bps_autorestore_cron_frequency' 	=> $ARQoptions['bps_autorestore_cron_frequency'], 
			'bps_autorestore_cron_override' 	=> $ARQoptions['bps_autorestore_cron_override'], 
			'bps_autorestore_cron_filecheck' 	=> $ARQoptions['bps_autorestore_cron_filecheck'], 
			'bps_autorestore_cron_forced' 		=> $ARQoptions['bps_autorestore_cron_forced'], 
			'bps_autorestore_cron_end' 			=> $ARQoptions['bps_autorestore_cron_end'] 
			);	

			foreach ( $BPS_Options as $key => $value ) {
				update_option('bulletproof_security_options_ARCM', $BPS_Options);
			}
	
			global $blog_id;

			if ( is_multisite() && $blog_id != 1 ) {
				return;
			} else {

				$bpsProLog = WP_CONTENT_DIR . '/bps-backup/logs/http_error_log.txt';
				$timeNow = time();
				$gmt_offset = get_option( 'gmt_offset' ) * 3600;
				$timestamp = date_i18n(get_option('date_format'), strtotime("11/15-1976")) . ' - ' . date_i18n(get_option('time_format'), $timeNow + $gmt_offset);
		
				$log_contents = "\r\n" . '[BPS Pro Plugin Folder Renamed or Deleted - ARQ Turned Off: ' . $timestamp . ']' . "\r\n" . 'This Security Log entry is created when the /bulletproof-security/ plugin folder is renamed or deleted. Renaming or deleting the /bulletproof-security/ plugin folder turns AutoRestore Off. An email alert is also sent to you when the /bulletproof-security/ plugin folder is renamed or deleted.'."\r\n";

				if ( is_writable( $bpsProLog ) ) {

				if ( ! $handle = fopen( $bpsProLog, 'a' ) ) {
        			exit;
    			}

    			if ( fwrite( $handle, $log_contents) === FALSE ) {
       				exit;
    			}

    			fclose($handle);
				}	
	
				$ARQ_status = get_option('bulletproof_security_options_ARQ_status');
			
				if ( $ARQ_status['bps_arq_status'] == 'On' ) {
					return;
				
				} else {

					$ARQEmailOptions = get_option('bulletproof_security_options_email');
					$bps_email_to = $ARQEmailOptions['bps_send_email_to'];
					$bps_email_from = $ARQEmailOptions['bps_send_email_from'];
					$bps_email_cc = $ARQEmailOptions['bps_send_email_cc'];
					$bps_email_bcc = $ARQEmailOptions['bps_send_email_bcc'];
					$justUrl = get_site_url();
					$timestamp = date_i18n(get_option('date_format'), strtotime("11/15-1976")) . ' - ' . date_i18n(get_option('time_format'), $timeNow + $gmt_offset);
	
					$headers = 'MIME-Version: 1.0' . "\r\n";
					$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
					$headers .= "From: " . $bps_email_from . "\r\n";
					$headers .= "Cc: " . $bps_email_cc . "\r\n";
					$headers .= "Bcc: " . $bps_email_bcc . "\r\n";		
		
					$subject = " BPS Pro Alert: The /bulletproof-security/ plugin folder has been renamed or deleted - $timestamp ";
					$message = '<p>When the /bulletproof-security/ plugin folder is renamed or deleted, AutoRestore is automatically turned Off on your website. This serves 2 purposes: 1. You are alerted when the /bulletproof-security plugin folder is renamed or deleted. 2. AutoRestore is turned Off for troubleshooting purposes. To stop these email alerts from being sent, go to the WordPress Plugins page, click the Must-Use link, click the BPS Pro MU Tools Disable link.</p>';	
					$message .= '<p>Website: '.$justUrl.'</p>'; 
		
					mail( $bps_email_to, $subject, $message, $headers );

					$MUTools_Option_settings = array( 
					'bps_mu_tools_timestamp' 		=> time() + 300,
					'bps_mu_tools_enable_disable' 	=> $MUTools_Options['bps_mu_tools_enable_disable'] 
					);	

					foreach ( $MUTools_Option_settings as $key => $value ) {
						update_option('bulletproof_security_options_MU_tools', $MUTools_Option_settings);
					}
				}
			}
		}
	}
}

//bpsPro_plugin_folder_check_ARQ_off();

// Writes a log entry and sends an email alert once every 5 minutes when the BPS Pro plugin has been deactivated.
// 12.9: Added ARQ Status condition so that email will not be sent when the BPS Pro plugin is being upgraded.
function bpsPro_plugin_deactivation_check_pro() {
	
	$MUTools_Options = get_option('bulletproof_security_options_MU_tools');
	
	if ( @$MUTools_Options['bps_mu_tools_enable_disable'] == 'disable' ) {
		return;
	}
	
	if ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON === true ) {
		return;
	}

	global $blog_id;

	if ( is_multisite() && $blog_id != 1 ) {
		return;
	}

	if ( time() > $MUTools_Options['bps_mu_tools_timestamp'] ) {

		// The require_once for plugin.php will cause a php warning error: headers already being sent.
		if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
			require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
		}

		$bps_plugin = 'bulletproof-security/bulletproof-security.php';
		@$bps_plugin_active = in_array( $bps_plugin, apply_filters('active_plugins', get_option('active_plugins') ) );

		if ( $bps_plugin_active != 1 && ! is_plugin_active_for_network( $bps_plugin ) ) { 

			$bpsProLog = WP_CONTENT_DIR . '/bps-backup/logs/http_error_log.txt';
			$timeNow = time();
			$gmt_offset = get_option( 'gmt_offset' ) * 3600;
			$timestamp = date_i18n(get_option('date_format'), strtotime("11/15-1976")) . ' - ' . date_i18n(get_option('time_format'), $timeNow + $gmt_offset);
		
			$log_contents = "\r\n" . '[BPS Pro Plugin Deactivated: ' . $timestamp . ']' . "\r\n" . 'This Security Log entry is created when the BPS Pro plugin is deactivated. An email alert is also sent to you when the BPS Pro plugin is deactivated.'."\r\n";

			if ( is_writable( $bpsProLog ) ) {

			if ( ! $handle = fopen( $bpsProLog, 'a' ) ) {
        		exit;
    		}

    		if ( fwrite( $handle, $log_contents) === FALSE ) {
       			exit;
    		}

    		fclose($handle);
			}	
	
			$ARQ_status = get_option('bulletproof_security_options_ARQ_status');
			
			if ( $ARQ_status['bps_arq_status'] == 'On' ) {
				return;
				
			} else {

				$ARQEmailOptions = get_option('bulletproof_security_options_email');
				$bps_email_to = $ARQEmailOptions['bps_send_email_to'];
				$bps_email_from = $ARQEmailOptions['bps_send_email_from'];
				$bps_email_cc = $ARQEmailOptions['bps_send_email_cc'];
				$bps_email_bcc = $ARQEmailOptions['bps_send_email_bcc'];
				$justUrl = get_site_url();
				$timestamp = date_i18n(get_option('date_format'), strtotime("11/15-1976")) . ' - ' . date_i18n(get_option('time_format'), $timeNow + $gmt_offset);
	
				$headers = 'MIME-Version: 1.0' . "\r\n";
				$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
				$headers .= "From: " . $bps_email_from . "\r\n";
				$headers .= "Cc: " . $bps_email_cc . "\r\n";
				$headers .= "Bcc: " . $bps_email_bcc . "\r\n";		
		
				$subject = " BPS Pro Alert: The BPS Pro plugin has been deactivated - $timestamp ";
				$message = '<p>The BPS Pro plugin has been deactivated on website: '.$justUrl.'. To stop these email alerts from being sent while BPS Pro is deactivated, go to the WordPress Plugins page, click the Must-Use link, click the BPS Pro MU Tools Disable link. If you just upgraded BPS Pro you can ignore this email alert unless you continue to receive these email alerts every 5 minutes. If you continue to receive these email alerts, login to your website and run the BPS Pro Setup Wizard.</p>';	
				$message .= '<p>Note: If you are troubleshooting the BPS Pro plugin then click this BPS Pro Troubleshooting link: https://forum.ait-pro.com/forums/topic/read-me-first-pro/#bps-pro-general-troubleshooting. The BPS Pro plugin has built-in troubleshooting capability and should not be deactivated for troubleshooting. Deactivating BPS Pro removes the built-in troubleshooting tools/capabilities. You can turn all BPS Pro security features On or Off for troubleshooting to isolate exactly which BPS Pro security feature is causing an issue/problem or to confirm or eliminate BPS Pro as the cause of an issue/problem.</p>';
		
				mail( $bps_email_to, $subject, $message, $headers );

				$MUTools_Option_settings = array( 
				'bps_mu_tools_timestamp' 		=> time() + 300, 
				'bps_mu_tools_enable_disable' 	=> $MUTools_Options['bps_mu_tools_enable_disable'] 
				);	

				foreach ( $MUTools_Option_settings as $key => $value ) {
					update_option('bulletproof_security_options_MU_tools', $MUTools_Option_settings);
				}
			}
		}
	}
}

//bpsPro_plugin_deactivation_check_pro();

// Displays a Critical Warning message on the WordPress Updates page when the BPS Pro plugin is deactivated or when ARQ is turned Off.
// Hopefully this will prevent folks from deactivating BPS Pro or turning ARQ Off when doing WP Core, Plugin and Theme updates.
function bpsPro_plugin_deactivation_check_display() {
	
	if ( is_admin() ) {
		
	if ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON === true ) {
		return;
	}

		// The require_once for plugin.php will cause a php warning error: headers already being sent.
		// error_reporting(0) can safely be used within the preg_match conditions for the update-core.php condition.
		if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
			require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
		} 

		$bps_plugin = 'bulletproof-security/bulletproof-security.php';
		@$bps_plugin_active = in_array( $bps_plugin, apply_filters('active_plugins', get_option('active_plugins') ) );

		if ( $bps_plugin_active != 1 && ! is_plugin_active_for_network( $bps_plugin ) ) { 

			if ( preg_match( '/\/wp-admin\/update-core\.php/', esc_html($_SERVER['REQUEST_URI']) ) || preg_match( '/\/wp-admin\/network\/update-core\.php/', esc_html($_SERVER['REQUEST_URI']) ) ) {
				error_reporting(0);
				$text = '<div class="update-nag" style="background-color:#dfecf2;border:1px solid #999;font-size:1em;font-weight:600;padding:2px 5px;margin:5px 0px 0px 200px;-moz-border-radius-topleft:3px;-webkit-border-top-left-radius:3px;-khtml-border-top-left-radius:3px;border-top-left-radius:3px;-moz-border-radius-topright:3px;-webkit-border-top-right-radius:3px;-khtml-border-top-right-radius:3px;border-top-right-radius:3px;-webkit-box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);-moz-box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);"><font color="#fb0101">'.__('CRITICAL WARNING! The BPS Pro plugin is currently deactivated', 'bulletproof-security').'</font><br>'.__('The BPS Pro plugin should be activated and AutoRestore should be turned On if/when you are updating WordPress, Plugins or Themes.', 'bulletproof-security').'<br>'.__('BPS Pro AutoRestore Automation will automatically turn ARQ Off, backup all new files and then turn ARQ back On again, but', 'bulletproof-security').'<br>'.__('the BPS Pro plugin must be activated & AutoRestore must be turned On in order for AutoRestore Automation to work.', 'bulletproof-security').'<br>'.__('If you prefer to do things manually do these steps: Turn ARQ Off, update WP, Plugins and/or Themes and then run the BPS Setup Wizards.', 'bulletproof-security').'</div>';
				echo $text;
			}
		}

		$ARQ_status = get_option('bulletproof_security_options_ARQ_status');
		
		if ( $ARQ_status['bps_arq_status'] != 'On' ) {
		
			$ARQoptions = get_option('bulletproof_security_options_ARCM');
			
			if ( $bps_plugin_active == 1 && $ARQoptions['bps_autorestore_cron'] == 'Off' || is_plugin_active_for_network( $bps_plugin ) && $ARQoptions['bps_autorestore_cron'] == 'Off' ) {

				if ( preg_match( '/\/wp-admin\/update-core\.php/', esc_html($_SERVER['REQUEST_URI']) ) || preg_match( '/\/wp-admin\/network\/update-core\.php/', esc_html($_SERVER['REQUEST_URI']) ) ) {
					error_reporting(0);
					$text = '<div class="update-nag" style="background-color:#dfecf2;border:1px solid #999;font-size:1em;font-weight:600;padding:2px 5px;margin:5px 0px 0px 200px;-moz-border-radius-topleft:3px;-webkit-border-top-left-radius:3px;-khtml-border-top-left-radius:3px;border-top-left-radius:3px;-moz-border-radius-topright:3px;-webkit-border-top-right-radius:3px;-khtml-border-top-right-radius:3px;border-top-right-radius:3px;-webkit-box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);-moz-box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);"><font color="#fb0101">'.__('CRITICAL WARNING! BPS Pro AutoRestore is currently turned Off', 'bulletproof-security').'</font><br>'.__('The BPS Pro plugin should be activated and AutoRestore should be turned On if/when you are updating WordPress, Plugins or Themes.', 'bulletproof-security').'<br>'.__('BPS Pro AutoRestore Automation will automatically turn ARQ Off, backup all new files and then turn ARQ back On again, but', 'bulletproof-security').'<br>'.__('the BPS Pro plugin must be activated & AutoRestore must be turned On in order for AutoRestore Automation to work.', 'bulletproof-security').'<br>'.__('If you prefer to do things manually do these steps: Turn ARQ Off, update WP, Plugins and/or Themes and then run the BPS Setup Wizards.', 'bulletproof-security').'</div>';
					echo $text;
				}		
			}
		}
	}
}

//bpsPro_plugin_deactivation_check_display();

// Note: you cannot use current_user_can('manage_options') in a must-use plugin, but the plugins.php page
// cannot be accessed without Admin permissions - Sorry, you are not allowed to access this page.
function bpsPro_toggle_links_pro() {
	
	if ( is_admin() && preg_match( '/\/wp-admin\/plugins\.php/', esc_html($_SERVER['REQUEST_URI']) ) || is_network_admin() && preg_match( '/\/wp-admin\/network\/plugins\.php/', esc_html($_SERVER['REQUEST_URI']) ) ) {

		if ( isset( $_GET['bps_toggle'] ) || isset( $_GET['bps_toggle_arq'] ) || isset( $_GET['bps_toggle_automatic_updater_disabled'] ) || isset( $_GET['bps_toggle_auto_update_core_updates_disabled'] ) || isset( $_GET['bps_toggle_auto_update_core'] ) || isset( $_GET['bps_toggle_allow_dev_auto_core_updates'] ) || isset( $_GET['bps_toggle_allow_minor_auto_core_updates'] ) ||isset( $_GET['bps_toggle_allow_major_auto_core_updates'] ) ) {
		
			if ( ! function_exists( 'wp_verify_nonce' ) ) {
				require_once( ABSPATH . '/wp-includes/pluggable.php' );
			}

			if ( ! defined( 'COOKIEHASH' ) ) {
        		$siteurl = get_site_option( 'siteurl' );
        		
				if ( $siteurl )
            		define( 'COOKIEHASH', md5( $siteurl ) );
        		else
            		define( 'COOKIEHASH', '' );
    		}

			if ( ! defined('AUTH_COOKIE') )
        		define('AUTH_COOKIE', 'wordpress_' . COOKIEHASH);
			
			if ( ! defined('SECURE_AUTH_COOKIE') )
				define('SECURE_AUTH_COOKIE', 'wordpress_sec_' . COOKIEHASH);

    		if ( ! defined('LOGGED_IN_COOKIE') )
				define('LOGGED_IN_COOKIE', 'wordpress_logged_in_' . COOKIEHASH);			

			if ( empty( $_REQUEST['_wpnonce'] ) ) {
				$nonce = '';
			} else {
				$nonce = $_REQUEST['_wpnonce'];
			}
		
			if ( ! wp_verify_nonce( $nonce, 'bps-anti-csrf' ) ) {
				die( 'CSRF Error: Invalid Nonce used in BPS Pro MU Tools must-use plugin GET Request' );
			
			} else {

				$MUTools_Options = get_option('bulletproof_security_options_MU_tools');
				$ARQoptions = get_option('bulletproof_security_options_ARCM');
				$wp_auto_update_options = get_option('bulletproof_security_options_mu_wp_autoupdate');
				
				if ( ! isset( $_GET['bps_toggle'] ) ) {
					$bps_toggle = $MUTools_Options['bps_mu_tools_enable_disable'];
				} elseif ( 'enable' == $_GET['bps_toggle'] ) {
					$bps_toggle = 'enable';
				} elseif ( 'disable' == $_GET['bps_toggle'] ) {
					$bps_toggle = 'disable';
				}

				if ( ! isset( $_GET['bps_toggle_arq'] ) ) {
					$bps_toggle_arq = $ARQoptions['bps_autorestore_cron'];
				} elseif ( 'off' == $_GET['bps_toggle_arq'] ) {
					$bps_toggle_arq = 'Off';
				} elseif ( 'on' == $_GET['bps_toggle_arq'] ) {
					$bps_toggle_arq = 'Off';
				}

				if ( ! isset( $_GET['bps_toggle_automatic_updater_disabled'] ) ) {
					$bps_toggle_automatic_updater_disabled = $wp_auto_update_options['bps_automatic_updater_disabled'];
				} elseif ( 'enable' == $_GET['bps_toggle_automatic_updater_disabled'] ) {
					$bps_toggle_automatic_updater_disabled = 'enabled';
				} elseif ( 'disable' == $_GET['bps_toggle_automatic_updater_disabled'] ) {
					$bps_toggle_automatic_updater_disabled = 'disabled';
				}

				if ( ! isset( $_GET['bps_toggle_auto_update_core_updates_disabled'] ) ) {
					$bps_toggle_auto_update_core_updates_disabled = $wp_auto_update_options['bps_auto_update_core_updates_disabled'];
				} elseif ( 'enable' == $_GET['bps_toggle_auto_update_core_updates_disabled'] ) {
					$bps_toggle_auto_update_core_updates_disabled = 'enabled';
				} elseif ( 'disable' == $_GET['bps_toggle_auto_update_core_updates_disabled'] ) {
					$bps_toggle_auto_update_core_updates_disabled = 'disabled';
				}

				if ( ! isset( $_GET['bps_toggle_auto_update_core'] ) ) {
					$bps_toggle_auto_update_core = $wp_auto_update_options['bps_auto_update_core'];
				} elseif ( 'enable' == $_GET['bps_toggle_auto_update_core'] ) {
					$bps_toggle_auto_update_core = 'enabled';
				} elseif ( 'disable' == $_GET['bps_toggle_auto_update_core'] ) {
					$bps_toggle_auto_update_core = 'disabled';
				}

				if ( ! isset( $_GET['bps_toggle_allow_dev_auto_core_updates'] ) ) {
					$bps_toggle_allow_dev_auto_core_updates = $wp_auto_update_options['bps_allow_dev_auto_core_updates'];
				} elseif ( 'enable' == $_GET['bps_toggle_allow_dev_auto_core_updates'] ) {
					$bps_toggle_allow_dev_auto_core_updates = 'enabled';
				} elseif ( 'disable' == $_GET['bps_toggle_allow_dev_auto_core_updates'] ) {
					$bps_toggle_allow_dev_auto_core_updates = 'disabled';
				}

				if ( ! isset( $_GET['bps_toggle_allow_minor_auto_core_updates'] ) ) {
					$bps_toggle_allow_minor_auto_core_updates = $wp_auto_update_options['bps_allow_minor_auto_core_updates'];
				} elseif ( 'enable' == $_GET['bps_toggle_allow_minor_auto_core_updates'] ) {
					$bps_toggle_allow_minor_auto_core_updates = 'enabled';
				} elseif ( 'disable' == $_GET['bps_toggle_allow_minor_auto_core_updates'] ) {
					$bps_toggle_allow_minor_auto_core_updates = 'disabled';
				}

				if ( ! isset( $_GET['bps_toggle_allow_major_auto_core_updates'] ) ) {
					$bps_toggle_allow_major_auto_core_updates = $wp_auto_update_options['bps_allow_major_auto_core_updates'];
				} elseif ( 'enable' == $_GET['bps_toggle_allow_major_auto_core_updates'] ) {
					$bps_toggle_allow_major_auto_core_updates = 'enabled';
				} elseif ( 'disable' == $_GET['bps_toggle_allow_major_auto_core_updates'] ) {
					$bps_toggle_allow_major_auto_core_updates = 'disabled';
				}

				$MUTools_Option_settings = array( 
				'bps_mu_tools_timestamp' 		=> time() + 300, 
				'bps_mu_tools_enable_disable' 	=> $bps_toggle 
				);	

				foreach ( $MUTools_Option_settings as $key => $value ) {
					update_option('bulletproof_security_options_MU_tools', $MUTools_Option_settings);
				}
			
				$BPS_ARQ_Options = array(
				'bps_autorestore_cron_frequency' 	=> $ARQoptions['bps_autorestore_cron_frequency'], 
				'bps_autorestore_cron' 				=> $bps_toggle_arq, 
				'bps_autorestore_cron_override' 	=> $ARQoptions['bps_autorestore_cron_override'], 
				'bps_autorestore_cron_forced' 		=> $ARQoptions['bps_autorestore_cron_forced'], 
				'bps_autorestore_cron_filecheck' 	=> $ARQoptions['bps_autorestore_cron_filecheck'], 
				'bps_autorestore_cron_end' 			=> $ARQoptions['bps_autorestore_cron_end']
				);	
						
				foreach( $BPS_ARQ_Options as $key => $value ) {
					update_option('bulletproof_security_options_ARCM', $BPS_ARQ_Options);
				}			
			
				$BPS_WP_Autoupdate_Options = array(
				'bps_automatic_updater_disabled' 		=> $bps_toggle_automatic_updater_disabled, 
				'bps_auto_update_core_updates_disabled' => $bps_toggle_auto_update_core_updates_disabled, 
				'bps_auto_update_core' 					=> $bps_toggle_auto_update_core, 
				'bps_allow_dev_auto_core_updates' 		=> $bps_toggle_allow_dev_auto_core_updates, 
				'bps_allow_minor_auto_core_updates' 	=> $bps_toggle_allow_minor_auto_core_updates, 
				'bps_allow_major_auto_core_updates' 	=> $bps_toggle_allow_major_auto_core_updates 
				);	
						
				foreach( $BPS_WP_Autoupdate_Options as $key => $value ) {
					update_option('bulletproof_security_options_mu_wp_autoupdate', $BPS_WP_Autoupdate_Options);
				}			
			}
		}
	}
}

bpsPro_toggle_links_pro();

function bpsPro_mu_plugin_actlinks_pro( $links, $file ) {
	static $this_plugin;
	
	if ( ! $this_plugin ) 
		$this_plugin = plugin_basename(__FILE__);
	if ( $file == $this_plugin ) {

		$MUTools_Options = get_option('bulletproof_security_options_MU_tools');
		$ARQoptions = get_option('bulletproof_security_options_ARCM');
		$wp_auto_update_options = get_option('bulletproof_security_options_mu_wp_autoupdate');
	
		if ( ! function_exists( 'wp_create_nonce' ) ) {
			require_once( ABSPATH . '/wp-includes/pluggable.php' );
		}
		
		$nonce = wp_create_nonce( 'bps-anti-csrf' );

		// Not currently being used, but may be used again later on.
		/*
		if ( ! $MUTools_Options['bps_mu_tools_enable_disable'] || $MUTools_Options['bps_mu_tools_enable_disable'] == 'enable' ) {
		
			if ( is_multisite() ) {
				$links[] = '<a href="'.network_admin_url( "plugins.php?plugin_status=mustuse&bps_toggle=disable&_wpnonce=$nonce" ).'">Disable</a>';
			} else {
				$links[] = '<a href="'.admin_url( "plugins.php?plugin_status=mustuse&bps_toggle=disable&_wpnonce=$nonce" ).'">Disable</a>';
			}		

		} else {
		
			if ( is_multisite() ) {
				$links[] = '<a href="'.network_admin_url( "plugins.php?plugin_status=mustuse&bps_toggle=enable&_wpnonce=$nonce" ).'">Enable</a>';
			} else {
				$links[] = '<a href="'.admin_url( "plugins.php?plugin_status=mustuse&bps_toggle=enable&_wpnonce=$nonce" ).'">Enable</a>';
			}		
		}
		*/		

		if ( $ARQoptions['bps_autorestore_cron'] == 'Off' ) {
		
			if ( is_multisite() ) {
				$links[] = '<a href="'.network_admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_arq=off&_wpnonce=$nonce" ).'" style="color:green;font-weight:600">AutoRestore is turned Off</a>';
			} else {
				$links[] = '<a href="'.admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_arq=off&_wpnonce=$nonce" ).'" style="color:green;font-weight:600">AutoRestore is turned Off</a>';
			}		

		} else {
		
			if ( is_multisite() ) {
				$links[] = '<a href="'.network_admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_arq=on&_wpnonce=$nonce" ).'">Turn Off AutoRestore</a>';
			} else {
				$links[] = '<a href="'.admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_arq=on&_wpnonce=$nonce" ).'">Turn Off AutoRestore</a>';
			}		
		}		

		// Disable all Automatic Updates: Core, Plugins and Themes.
		if ( $wp_auto_update_options['bps_automatic_updater_disabled'] == 'enabled' ) {			

			if ( is_multisite() ) {
				$links[] = '<br><a href="'.network_admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_automatic_updater_disabled=disable&_wpnonce=$nonce" ).'" style="color:green;font-weight:600">Disable all Updates: On</a>';
			} else {
				$links[] = '<br><a href="'.admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_automatic_updater_disabled=disable&_wpnonce=$nonce" ).'" style="color:green;font-weight:600">Disable all Updates: On</a>';
			}		

		} else {
		
			if ( is_multisite() ) {
				$links[] = '<br><a href="'.network_admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_automatic_updater_disabled=enable&_wpnonce=$nonce" ).'">Disable all Updates: Off</a>';
			} else {
				$links[] = '<br><a href="'.admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_automatic_updater_disabled=enable&_wpnonce=$nonce" ).'">Disable all Updates: Off</a>';
			}
		}

		// Disable all WordPress Core Automatic Updates: Development, Minor and Major
		if ( $wp_auto_update_options['bps_auto_update_core_updates_disabled'] == 'enabled' ) {			

			if ( is_multisite() ) {
				$links[] = '<br><a href="'.network_admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_auto_update_core_updates_disabled=disable&_wpnonce=$nonce" ).'" style="color:green;font-weight:600">Disable all Core Updates: On</a>';
			} else {
				$links[] = '<br><a href="'.admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_auto_update_core_updates_disabled=disable&_wpnonce=$nonce" ).'" style="color:green;font-weight:600">Disable all Core Updates: On</a>';
			}		

		} else {
		
			if ( is_multisite() ) {
				$links[] = '<br><a href="'.network_admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_auto_update_core_updates_disabled=enable&_wpnonce=$nonce" ).'">Disable all Core Updates: Off</a>';
			} else {
				$links[] = '<br><a href="'.admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_auto_update_core_updates_disabled=enable&_wpnonce=$nonce" ).'">Disable all Core Updates: Off</a>';
			}
		}

		// Enable all WordPress Core Automatic Updates: Development, Minor and Major
		if ( $wp_auto_update_options['bps_auto_update_core'] == 'enabled' ) {			

			if ( is_multisite() ) {
				$links[] = '<br><a href="'.network_admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_auto_update_core=disable&_wpnonce=$nonce" ).'" style="color:green;font-weight:600">Enable all Core Updates: On</a>';
			} else {
				$links[] = '<br><a href="'.admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_auto_update_core=disable&_wpnonce=$nonce" ).'" style="color:green;font-weight:600">Enable all Core Updates: On</a>';
			}		

		} else {
		
			if ( is_multisite() ) {
				$links[] = '<br><a href="'.network_admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_auto_update_core=enable&_wpnonce=$nonce" ).'">Enable all Core Updates: Off</a>';
			} else {
				$links[] = '<br><a href="'.admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_auto_update_core=enable&_wpnonce=$nonce" ).'">Enable all Core Updates: Off</a>';
			}
		}

		// Enable WordPress Core Development Automatic Updates
		if ( $wp_auto_update_options['bps_allow_dev_auto_core_updates'] == 'enabled' ) {	

			if ( is_multisite() ) {
				$links[] = '<br><a href="'.network_admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_allow_dev_auto_core_updates=disable&_wpnonce=$nonce" ).'" style="color:green;font-weight:600">Enable Development Updates: On</a>';
			} else {
				$links[] = '<br><a href="'.admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_allow_dev_auto_core_updates=disable&_wpnonce=$nonce" ).'" style="color:green;font-weight:600">Enable Development Updates: On</a>';
			}		

		} else {
		
			if ( is_multisite() ) {
				$links[] = '<br><a href="'.network_admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_allow_dev_auto_core_updates=enable&_wpnonce=$nonce" ).'">Enable Development Updates: Off</a>';
			} else {
				$links[] = '<br><a href="'.admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_allow_dev_auto_core_updates=enable&_wpnonce=$nonce" ).'">Enable Development Updates: Off</a>';
			}
		}

		// Enable WordPress Core Minor Automatic Updates
		if ( $wp_auto_update_options['bps_allow_minor_auto_core_updates'] == 'enabled' ) {

			if ( is_multisite() ) {
				$links[] = '<br><a href="'.network_admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_allow_minor_auto_core_updates=disable&_wpnonce=$nonce" ).'" style="color:green;font-weight:600">Enable Minor Updates: On</a>';
			} else {
				$links[] = '<br><a href="'.admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_allow_minor_auto_core_updates=disable&_wpnonce=$nonce" ).'" style="color:green;font-weight:600">Enable Minor Updates: On</a>';
			}		

		} else {
		
			if ( is_multisite() ) {
				$links[] = '<br><a href="'.network_admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_allow_minor_auto_core_updates=enable&_wpnonce=$nonce" ).'">Enable Minor Updates: Off</a>';
			} else {
				$links[] = '<br><a href="'.admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_allow_minor_auto_core_updates=enable&_wpnonce=$nonce" ).'">Enable Minor Updates: Off</a>';
			}
		}
		
		// Enable WordPress Core Major Automatic Updates
		if ( $wp_auto_update_options['bps_allow_major_auto_core_updates'] == 'enabled' ) {

			if ( is_multisite() ) {
				$links[] = '<br><a href="'.network_admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_allow_major_auto_core_updates=disable&_wpnonce=$nonce" ).'" style="color:green;font-weight:600">Enable Major Updates: On</a>';
			} else {
				$links[] = '<br><a href="'.admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_allow_major_auto_core_updates=disable&_wpnonce=$nonce" ).'" style="color:green;font-weight:600">Enable Major Updates: On</a>';
			}		

		} else {
		
			if ( is_multisite() ) {
				$links[] = '<br><a href="'.network_admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_allow_major_auto_core_updates=enable&_wpnonce=$nonce" ).'">Enable Major Updates: Off</a>';
			} else {
				$links[] = '<br><a href="'.admin_url( "plugins.php?plugin_status=mustuse&bps_toggle_allow_major_auto_core_updates=enable&_wpnonce=$nonce" ).'">Enable Major Updates: Off</a>';
			}
		}
	}
	return $links;
}

add_filter( 'plugin_action_links', 'bpsPro_mu_plugin_actlinks_pro', 10, 2 );
add_filter( 'network_admin_plugin_action_links', 'bpsPro_mu_plugin_actlinks_pro', 10, 2 );

function bpsPro_wp_automatic_updates() {
	
	$wp_auto_update_options = get_option('bulletproof_security_options_mu_wp_autoupdate');

	if ( isset($wp_auto_update_options['bps_automatic_updater_disabled']) && $wp_auto_update_options['bps_automatic_updater_disabled'] == 'enabled' ) {
		add_filter( 'automatic_updater_disabled', '__return_true' );
	}

	if ( isset($wp_auto_update_options['bps_auto_update_core_updates_disabled']) && $wp_auto_update_options['bps_auto_update_core_updates_disabled'] == 'enabled' ) {
		add_filter( 'auto_update_core', '__return_false' );
	}

	if ( isset($wp_auto_update_options['bps_auto_update_core']) && $wp_auto_update_options['bps_auto_update_core'] == 'enabled' ) {
		add_filter( 'auto_update_core', '__return_true' );
	}

	if ( isset($wp_auto_update_options['bps_allow_dev_auto_core_updates']) && $wp_auto_update_options['bps_allow_dev_auto_core_updates'] == 'enabled' ) {
		add_filter( 'allow_dev_auto_core_updates', '__return_true' );
	}

	if ( isset($wp_auto_update_options['bps_allow_minor_auto_core_updates']) && $wp_auto_update_options['bps_allow_minor_auto_core_updates'] == 'enabled' ) {
		add_filter( 'allow_minor_auto_core_updates', '__return_true' );
	}

	if ( isset($wp_auto_update_options['bps_allow_major_auto_core_updates']) && $wp_auto_update_options['bps_allow_major_auto_core_updates'] == 'enabled' ) {
		add_filter( 'allow_major_auto_core_updates', '__return_true' );
	}
}

bpsPro_wp_automatic_updates();

// Add additional links on the BPS Must-Use plugins page
function bpsPro_mu_plugin_extra_links($links, $file) {
	static $this_plugin;
	//if ( ! current_user_can('install_plugins') )
		//return $links;
	if ( ! $this_plugin ) $this_plugin = plugin_basename(__FILE__);
	if ( $file == $this_plugin ) {
		$links[] = '<a href="https://forum.ait-pro.com/forums/topic/wordpress-automatic-update-help-forum-topic-bps-must-use-plugin/" target="_blank" title="WordPress Automatic Update Help Forum Topic">' . __('WordPress Automatic Update Help Forum Topic', 'bulleproof-security').'</a>';
	}

	return $links;
}

add_filter( 'plugin_row_meta', 'bpsPro_mu_plugin_extra_links', 10, 2)

?>