/*  ________         ____________      _____ ________                       ________      
#  ___  __ )____  _____  /___  /_____ __  /____  __ \______________ ______ ___  __/      
#  __  __  |_  / / /__  / __  / _  _ \_  __/__  /_/ /__  ___/_  __ \_  __ \__  /_        
#  _  /_/ / / /_/ / _  /  _  /  /  __// /_  _  ____/ _  /    / /_/ // /_/ /_  __/        
#  /_____/  \__,_/  /_/   /_/   \___/ \__/  /_/      /_/     \____/ \____/ /_/           
#  ________                             _____ _____              ________                
#  __  ___/_____ ___________  _____________(_)__  /______  __    ___  __ \______________ 
#  _____ \ _  _ \_  ___/_  / / /__  ___/__  / _  __/__  / / /    __  /_/ /__  ___/_  __ \
#  ____/ / /  __// /__  / /_/ / _  /    _  /  / /_  _  /_/ /     _  ____/ _  /    / /_/ /
#  /____/  \___/ \___/  \__,_/  /_/     /_/   \__/  _\__, /      /_/      /_/     \____/ 
#                                                   /____/                               
# 42756C6C657450726F6F66 5365637572697479 50726F 
*/
// BPS ARQ AJAX Automation
// WP has made it super simple to pass js to php by hooking into pre-built WP AJAX functionality.
// enqueue your custom ajax script, localize it and you have a listener for CSS click events.
// Note: Ran into wierd anamolies when trying to defer with .then and use setTimeout 
// so the simpler/more consistent solution was to use PHP sleep() in the PHP Action/function itself.
// In any case, for this particular usage it makes more sense to process linear vs multiple callbacks.
// This particular usage is simply that I only need to know when a link is clicked and use PHP to process from there.
// There are several different variations on how each individual plugin or theme updating is handled in each WP area.
// 12.6.1: BugFix: pathname was not correct for WP subdirectory sites.
jQuery(document).ready( function($) {
	
	var page = $(location).attr('pathname');
	var path = window.location.pathname;
	
	var regexPlugins = /.*\/wp-admin\/plugins\.php/;
	var regexPluginsNet = /.*\/wp-admin\/network\/plugins\.php/;
	var uriPlugins = path.match(regexPlugins); //console.log(regexPlugins);
	var uriPluginsNet = path.match(regexPluginsNet);

	var regexPluginInstall = /.*\/wp-admin\/plugin-install\.php/;
	var regexPluginInstallNet = /.*\/wp-admin\/network\/plugin-install\.php/;
	var uriPluginInstall = path.match(regexPluginInstall); //console.log(regexPluginInstall);
	var uriPluginInstallNet = path.match(regexPluginInstallNet);

	var regexThemes = /.*\/wp-admin\/themes\.php/;
	var regexThemesNet = /.*\/wp-admin\/network\/themes\.php/;
	var uriThemes = path.match(regexThemes); //console.log(regexThemes);
	var uriThemesNet = path.match(regexThemesNet);

	var regexThemeInstall = /.*\/wp-admin\/theme-install\.php/;
	var regexThemeInstallNet = /.*\/wp-admin\/network\/theme-install\.php/;
	var uriThemeInstall = path.match(regexThemeInstall); //console.log(regexThemeInstall);
	var uriThemeInstallNet = path.match(regexThemeInstallNet);

	var regexUpdateCore = /.*\/wp-admin\/update-core\.php/;
	var regexUpdateCoreNet = /.*\/wp-admin\/network\/update-core\.php/;
	var uriUpdateCore = path.match(regexUpdateCore); //console.log(uriUpdateCore);
	var uriUpdateCoreNet = path.match(regexUpdateCoreNet);
	
	var regexThemeEditor = /.*\/wp-admin\/theme-editor\.php/;
	var regexThemeEditorNet = /.*\/wp-admin\/network\/theme-editor\.php/;
	var uriThemeEditor = path.match(regexThemeEditor); //console.log(regexThemeEditor);
	var uriThemeEditorNet = path.match(regexThemeEditorNet);	

	if ( uriPlugins == page || uriPluginsNet == page ) {
		
		// Plugin update on plugins.php page by clicking the update now link in plugin Description
		// Triggers pre and post upgrader filters		
		// conditional if someone is allowing ARQ to monitor the /plugins/ folder.
		// Seamless ARQ Automation: ARQ Shutdown, ARQ File Backup & ARQ Turn On.
		$( "a.update-link" ).on({ "click": function() { 
	
			var data = {
				action: 'bps_arq_automation_plugin', 
				post_var: 'arq_auto_plugin_shiny_update'
			};

			$.post(bps_arq_ajax.ajaxurl, data, function(response) {
			// Object {action: "bps_arq_automation_plugin", post_var: "arq_auto_plugin"}
			//console.log( data );
			//console.log( page );
	 		});	
			console.log( "clicked!" ); 
		},
		"mouseover": function() { 
			console.log( "hovered!" );
		}
		});

		// Plugin Bulk Update on the plugins.php page by clicking the Apply button
		// Triggers pre and post upgrader filters
		// conditional if someone is allowing ARQ to monitor the /plugins/ folder.
		// Turn ARQ Off and display a message with a clickable link to complete ARQ Automation.
		$( "#doaction" ).on({ "click": function() { 
	
			var data = {
				action: 'bps_arq_automation_bulk_update_plugin', 
				post_var: 'arq_auto_bulk_update_plugin'
			};

			$.post(bps_arq_ajax.ajaxurl, data, function(response) {
			// Object {action: "bps_arq_automation_bulk_update_plugin", post_var: "arq_auto_bulk_update_plugin"}
			//console.log( data );
			//console.log( page );
	 		});	
			console.log( "clicked!" ); 
		},
		"mouseover": function() { 
			console.log( "hovered!" ); 
		}
		});

		// Plugins page: BPS Pro Upgrade Notification: Display an alert when the View version X details link is clicked.
		// As of WP 5.8 my API server code is not being seen correctly.
		$('tr#bulletproof-security-update.plugin-update-tr.active').on( "click", function() {
			
			alert( 'Do not upgrade BPS Pro from the "View version details" window. Click the "update now" link instead to upgrade BPS Pro. To view BPS Pro version details click the "Version Release Dates|Whats New" link under the BPS Pro description.' );
			console.log( "clicked!" );
		});
	}
	
	if ( uriPluginInstall == page || uriPluginInstallNet == page ) {	
	
		// Plugin installation on plugin-install.php page directly by clicking the Install Now button in the plugin Card.
		// Triggers pre and post upgrader filters
		// Note: On Install Now button click > href is > /wp-admin/update.php?action=install-plugin
		// conditional if someone is allowing ARQ to monitor the /plugins/ folder.
		// Seamless ARQ Automation: ARQ Shutdown, ARQ File Backup & ARQ Turn On.
		// Reloads the page 3 seconds after someone finishes typing in the search box 
		// or if they also press a key. The page needs to be reloaded so that the rest of the ajax code will work correctly.
		var delay = (function() {
 		var timer = 0;
 		
			return function(callback, ms) {
  				clearTimeout (timer);
  				timer = setTimeout(callback, ms);
 			};
		})();
		
		$('.wp-filter-search').keyup(function() {
			delay(function() {
				console.log( "User has stopped typing in the Search box" );
				location.reload();
  			}, 3000 );
		});
		
		$( "a.install-now" ).on({ "click": function() { 
	
			var data = {
				action: 'bps_arq_automation_plugin', 
				post_var: 'arq_auto_plugin_shiny_install'
			};

			$.post(bps_arq_ajax.ajaxurl, data, function(response) {
			// Object {action: "bps_arq_automation_plugin", post_var: "arq_auto_plugin"}
			console.log( data );
			console.log( page );
	 		});	
			console.log( "clicked!" ); 
		},
		"mouseover": function() { 
			console.log( "hovered!" ); 
		}
		});

		// Plugin installation from upload: plugins.php > Add New > plugin-install.php > Upload Plugin > Install Now
		// Triggers pre and post upgrader filters
		// Note: On Install Now button click > redirects to > /wp-admin/update.php?action=upload-plugin. mouseover console logging works, others don't.
		// conditional if someone is allowing ARQ to monitor the /plugins/ folder.
		// Seamless ARQ Automation: ARQ Shutdown, ARQ File Backup & ARQ Turn On.
		$( "#install-plugin-submit" ).on({ "click": function() { 
	
			var data = {
				action: 'bps_arq_automation_plugin', 
				post_var: 'arq_auto_plugin_shiny_install'
			};

			$.post(bps_arq_ajax.ajaxurl, data, function(response) {
			// Object {action: "bps_arq_automation_plugin", post_var: "arq_auto_plugin"}
			//console.log( data );
			//console.log( page );
	 		});	
			console.log( "clicked!" ); 
		},
		"mouseover": function() { 
			console.log( "hovered!" ); 
		}
		});
	}

		// iFrame: Plugin update by clicking the View version details link on the plugins.php page & Install Update Now link in the iframe.
		// Triggers pre and post upgrader filters
		// Note: On Install Now button click > href is > /wp-admin/update.php?action=upgrade-plugin
		// conditional if someone is allowing ARQ to monitor the /plugins/ folder.
		// Seamless ARQ Automation: ARQ Shutdown, ARQ File Backup & ARQ Turn On.
		$( "a#plugin_update_from_iframe" ).on({ "click": function() { 
	
			var data = {
				action: 'bps_arq_automation_plugin', 
				post_var: 'arq_auto_plugin_shiny_update'
			};

			$.post(bps_arq_ajax.ajaxurl, data, function(response) {
			// Object {action: "bps_arq_automation_plugin", post_var: "arq_auto_plugin"}
			//console.log( data );
			//console.log( page );
	 		});	
			console.log( "clicked!" ); 
		},
		"mouseover": function() { 
			console.log( "hovered!" ); 
		}
		});

		// iFrame: Plugin installation on plugin-install.php page by clicking the More Details link & Install Now button in the iframe.
		// Triggers pre and post upgrader filters
		// Note: On Install Now button click > href is > /wp-admin/update.php?action=install-plugin
		// conditional if someone is allowing ARQ to monitor the /plugins/ folder.
		// Seamless ARQ Automation: ARQ Shutdown, ARQ File Backup & ARQ Turn On.
		$( "a#plugin_install_from_iframe" ).on({ "click": function() { 
	
			var data = {
				action: 'bps_arq_automation_plugin', 
				post_var: 'arq_auto_plugin_shiny_install'
			};

			$.post(bps_arq_ajax.ajaxurl, data, function(response) {
			// Object {action: "bps_arq_automation_plugin", post_var: "arq_auto_plugin"}
			//console.log( data );
			//console.log( page );
	 		});	
			console.log( "clicked!" ); 
		},
		"mouseover": function() { 
			console.log( "hovered!" ); 
		}
		});

	// Theme updates & installs
	if ( uriThemes == page || uriThemesNet == page ) {
		
		// Theme update on themes.php page by clicking the Update now button above theme screenshots
		// Seamless ARQ Automation: ARQ Shutdown, ARQ File Backup & ARQ Turn On.
		$( ".update-message" ).on({ "click": function() { 
	
			var data = {
				action: 'bps_arq_automation_theme', 
				post_var: 'arq_auto_theme_shiny_update'
			};

			$.post(bps_arq_ajax.ajaxurl, data, function(response) {
			// Object {action: "bps_arq_automation_theme", post_var: "arq_auto_theme"}
			//console.log( data );
			//console.log( page );
	 		});	
			console.log( "clicked!" ); 
		},
		"mouseover": function() { 
			console.log( "hovered!" ); 
		}
		});

		// Theme update on themes.php page by clicking Theme Details > update now link in the Overlay.
		// Notes: The Overlay needs to be reloaded or the CSS properties in the Overlay are not seen/usable/clickable.
		// Seamless ARQ Automation: ARQ Shutdown, ARQ File Backup & ARQ Turn On.
		// 14.2: Commented out the reload code below. This is no longer necessary and causes a problem in newer versions of WP now.
		/*
		$('.more-details').click(function() {
    		location.reload();
		});
		*/

		// See description above.
		// This is still valid as of 9-7-2019 - WP 5.2.3
		// Good: Update Now button/link on the main themes page and the update button/link within the Theme details
		$( "#update-theme" ).on({ "click": function() { 
	
			var data = {
				action: 'bps_arq_automation_theme', 
				post_var: 'arq_auto_theme_shiny_update'
			};

			$.post(bps_arq_ajax.ajaxurl, data, function(response) {
			// Object {action: "bps_arq_automation_theme", post_var: "arq_auto_theme"}
			//console.log( data );
			//console.log( page );
	 		});	
			console.log( "clicked!" ); 
		},
		"mouseover": function() { 
			console.log( "hovered!" ); 
		}
		});
	}

	// Themes Add New|Add Themes page: theme-install.php
	// Seamless ARQ Automation: ARQ Shutdown, ARQ File Backup & ARQ Turn On.
	// This is still valid as of 9-7-2019 - WP 5.2.3
	if ( uriThemeInstall == page || uriThemeInstallNet == page ) {

		var data = {
			action: 'bps_arq_automation_bulk_update_theme', 
			post_var: 'arq_auto_bulk_update_theme_install'
		};

		$.post(bps_arq_ajax.ajaxurl, data, function(response) {
		// Object {action: "bps_arq_automation_bulk_update_theme", post_var: "arq_auto_bulk_update_theme"}
		//console.log( data );
		//console.log( page );
	 	});
	}

	// Backward Compatibility with WP versions below 4.6: bpsPro_wordpress_update_core_manual() in arq-cron.php for Bulk & Core Updates on the WP Updates page.
	// Eventually WP Shiny Updates will probably change the entire WP Updates page: WP Updates page > Bulk Update Plugins, Themes & Core
	// mouseover console logging works. other logging does not work due to redirection on submit: update-core.php?action=do-plugin-upgrade, etc.
	if ( uriUpdateCore == page || uriUpdateCoreNet == page ) {

		// Bulk Update Plugins > Updates page > Update Plugins button
		// Turn ARQ Off and display a message with a clickable link to complete ARQ Automation.
		$( "input#upgrade-plugins" ).on({ "click": function() { 
	
			var data = {
				action: 'bps_arq_automation_bulk_update_plugin', 
				post_var: 'arq_auto_bulk_update_plugin'
			};

			$.post(bps_arq_ajax.ajaxurl, data, function(response) {
			// Object {action: "bps_arq_automation_bulk_update_plugin", post_var: "arq_auto_bulk_update_plugin"}
			//console.log( data );
			//console.log( page );
	 		});	
			console.log( "clicked!" ); 
		},
		"mouseover": function() { 
			console.log( "hovered!" );
		}
		});

		// Bulk Update Themes > Updates page > Update Themes button
		// Turn ARQ Off and display a message with a clickable link to complete ARQ Automation.
		$( "input#upgrade-themes" ).on({ "click": function() { 
	
			var data = {
				action: 'bps_arq_automation_bulk_update_theme', 
				post_var: 'arq_auto_bulk_update_theme'
			};

			$.post(bps_arq_ajax.ajaxurl, data, function(response) {
			// Object {action: "bps_arq_automation_bulk_update_theme", post_var: "arq_auto_bulk_update_theme"}
			//console.log( data );
			//console.log( page );
	 		});	
			console.log( "clicked!" ); 
		},
		"mouseover": function() { 
			console.log( "hovered!" ); 
		}
		});

		// WP Core Update > Update Now button & Re-install Now button on Updates page: update-core.php
		// Also Update Translations button for non-English WP versions.
		// Seamless ARQ Automation: ARQ Shutdown, ARQ File Backup & ARQ Turn On.
		$( "input#upgrade" ).on({ "click": function() { 
	
			var data = {
				action: 'bps_arq_automation_wp_core_update', 
				post_var: 'arq_auto_wp_core'
			};

			$.post(bps_arq_ajax.ajaxurl, data, function(response) {
			// Object {action: "bps_arq_automation_wp_core_update", post_var: "arq_auto_wp_core"}
			//console.log( data );
			//console.log( page );
	 		});	
			console.log( "clicked!" ); 
		},
		"mouseover": function() { 
			console.log( "hovered!" ); 
		}
		});
	}

	// Theme Editor: Themes > Theme Editor > Update File button.
	// Seamless ARQ Automation: ARQ Shutdown, ARQ File Backup & ARQ Turn On.
	if ( uriThemeEditor == page || uriThemeEditorNet == page ) {
		
		$( "input#submit" ).on({ "click": function() { 
	
			var data = {
				action: 'bps_arq_automation_theme_editor', 
				post_var: 'arq_auto_theme_editor'
			};

			$.post(bps_arq_ajax.ajaxurl, data, function(response) {
			// Object {action: "bps_arq_automation_theme_editor", post_var: "arq_auto_theme_editor"}
			//console.log( data );
			//console.log( page );
	 		});	
			console.log( "clicked!" ); 
		},
		"mouseover": function() { 
			console.log( "hovered!" ); 
		}
		});		
	}

	// MScan Malware Scanner: Start. MScan Status: 1
	// MScan Stop is handled in mscan-ajax-functions.php by using a PHP file contents check: /bps-backup/master-backups/mscan-stop.txt
	$( "input#bps-mscan-start-button" ).on({ "click": function() { 
	
		var data = {
			action: 'bps_mscan_scan_processing', 
			post_var: 'bps_mscan'
		};

		$.post(bps_arq_ajax.ajaxurl, data, function(response) {
		// Object {action: "bps_mscan_scan_processing", post_var: "bps_mscan"}
		//console.log( data );
	 	});	
		console.log( "clicked!" ); 
	},
	"mouseover": function() { 
		console.log( "hovered!" );
	}
	});

	// MScan Malware Scanner: Scan Time Estimate Tool. MScan Status: 5
	$( "input#bps-mscan-time-estimate-button" ).on({ "click": function() { 
	
		var data = {
			action: 'bps_mscan_scan_estimate', 
			post_var: 'bps_mscan_estimate'
		};

		$.post(bps_arq_ajax.ajaxurl, data, function(response) {
		// Object {action: "bps_mscan_scan_estimate", post_var: "bps_mscan_estimate"}
		//console.log( data );
	 	});	
		console.log( "clicked!" ); 
	},
	"mouseover": function() { 
		console.log( "hovered!" );
	}
	});
});