<?php
/*
Plugin Name: Freesoul Deactivate Plugins
Plugin URI: https://freesoul-deactivate-plugins.com/
Description: Disable plugins on specific pages for performance improvement and support in problem-solving even when you have many plugins.
Author: Jose Mortellaro
Author URI: https://josemortellaro.com/
Text Domain: eos-dp
Domain Path: /languages/
Version: 1.8.9.4
*/
/*  Copyright 2020 Jose Mortellaro (email: info at josemortellaro.com)
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
*/
defined( 'ABSPATH' ) || exit; // Exit if accessed directly

//The plugiin desn't run if constants defined
if( !is_admin() && defined( 'FDP_EXCLUDE_FRONT' ) && FDP_EXCLUDE_FRONT && isset( $_GET[FDP_EXCLUDE_FRONT] ) && 'true' === $_GET[FDP_EXCLUDE_FRONT] ) return;
if( is_admin() && defined( 'FDP_EXCLUDE_BACK' ) && FDP_EXCLUDE_BACK && isset( $_GET[FDP_EXCLUDE_BACK] ) && 'true' === $_GET[FDP_EXCLUDE_BACK] ) return;

//Definitions
define( 'EOS_DP_VERSION','1.8.9.4' );
define( 'EOS_DP_NEED_UPDATE_MU',true );
define( 'EOS_DP_PLUGIN_DIR', untrailingslashit( dirname( __FILE__ ) ) );
define( 'EOS_DP_PLUGIN_URL', untrailingslashit( plugins_url( '', __FILE__ ) ) );
define( 'EOS_DP_PLUGIN_BASE_NAME', untrailingslashit( plugin_basename( __FILE__ ) ) );
define( 'EOS_DP_PLUGINS_DIRNAME',basename( dirname( __DIR__ ) ) );
define( 'EOS_DP_MAIN_STYLESHEET',EOS_DP_PLUGIN_URL.'/admin/css/fdp-admin-3.2' );
define( 'EOS_DP_MAIN_JS',EOS_DP_PLUGIN_URL.'/admin/js/fdp-admin-3.8' );
if( isset( $_REQUEST['eos_dp_preview'] ) || is_admin() || ( defined( 'DOING_AJAX') && DOING_AJAX ) ){
  define( 'EOS_DP_PRO_TESTING_UNIQUE_ID',eos_dp_pro_testing_uniqueid() );
}


if ( defined( 'WP_INSTALLING' ) && WP_INSTALLING ) return; //Return if WordPress is upgrading

//Actions triggered after plugin activation or after a new site of a multisite installation is created
function eos_dp_initialize_plugin( $networkwide ){
    if( is_multisite() && $networkwide ){
		wp_die( sprintf( __( "Freesoul Deactivate Plugins can't be activated networkwide, but only on each single site. %s%s%s","eos-dp" ),'<div><a class="button" href="'.admin_url( 'network/plugins.php' ).'">',__( 'Back to plugins','eos-dp' ),'</a></div>' ) );
	}
	require EOS_DP_PLUGIN_DIR.'/plugin-activation.php';
}
register_activation_hook( __FILE__, 'eos_dp_initialize_plugin' );
//Actions triggered after plugin deaactivation
function eos_dp_deactivate_plugin(){
	if( !is_multisite() && file_exists( WPMU_PLUGIN_DIR.'/eos-deactivate-plugins.php' ) ){
		unlink( WPMU_PLUGIN_DIR.'/eos-deactivate-plugins.php' );
	}
}
register_deactivation_hook( __FILE__, 'eos_dp_deactivate_plugin' );

add_action( 'upgrader_process_complete', 'eos_dp_after_upgrade',10,2 );
//Update mu-plugin after upgrade
function eos_dp_after_upgrade( $upgrader_object, $options ) {
  $update_mu = false;
  if( isset( $options['plugins'] ) && is_array( $options['plugins'] ) && !empty( $options['plugins'] ) && isset( $options['action'] ) && 'update' === $options['action'] && isset( $options['type'] ) && 'plugin' === $options['type'] ) {
     foreach( $options['plugins'] as $plugin ) {
        if( EOS_DP_PLUGIN_BASE_NAME === $plugin  ){
          $update_mu = true;
          break;
        }
     }
  }
  elseif( isset( $upgrader_object->new_plugin_data ) ){
    $new_plugin_data = $upgrader_object->new_plugin_data;
    if( isset( $new_plugin_data['TextDomain'] ) && 'eos-dp' === $new_plugin_data['TextDomain'] ){
      $update_mu = true;
    }
  }
  if( $update_mu ){
    if( file_exists( WPMU_PLUGIN_DIR.'/eos-deactivate-plugins.php' ) ){
      unlink( WPMU_PLUGIN_DIR.'/eos-deactivate-plugins.php' );
    }
    eos_dp_write_file( EOS_DP_PLUGIN_DIR.'/mu-plugins/eos-deactivate-plugins.php',WPMU_PLUGIN_DIR,WPMU_PLUGIN_DIR.'/eos-deactivate-plugins.php',true );
  }
}

//It loads plugin translation files
function eos_load_dp_plugin_textdomain(){
	load_plugin_textdomain( 'eos-dp', FALSE,EOS_DP_PLUGIN_DIR . '/languages/' );
}
add_action( 'admin_init', 'eos_load_dp_plugin_textdomain' );
//Filter function to read plugin translation files
function eos_dp_load_translation_file( $mofile, $domain ) {
	if ( 'eos-dp' === $domain ) {
		$loc = function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
		$mofile = EOS_DP_PLUGIN_DIR . '/languages/freesoul-deactivate-plugins-' . $loc . '.mo';
	}
	return $mofile;
}

if( is_admin() ){
	//Filter translation files
	add_filter( 'load_textdomain_mofile', 'eos_dp_load_translation_file',99,2 ); //loads plugin translation files
	require_once EOS_DP_PLUGIN_DIR.'/admin/eos-dp-helper.php';
	if ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_REQUEST['action'] ) && false !== strpos( $_REQUEST['action'],'eos_dp' ) ){
		require EOS_DP_PLUGIN_DIR. '/admin/eos-dp-ajax.php'; //file including all ajax requests functions
	}
	else{
		require EOS_DP_PLUGIN_DIR . '/inc/eos-dp-metaboxes.php';
		require EOS_DP_PLUGIN_DIR . '/admin/eos-dp-admin.php';
		if( eos_dp_is_fdp_page() ){
			remove_all_actions( 'parse_request' );
			add_action( 'admin_init','eos_dp_remove_other_admin_notices' );
      if( !in_array( sanitize_text_field( $_GET['page'] ),array( 'eos_dp_favorite_plugins' ) ) && !isset( $_GET['load_all_assets'] ) ){
  			add_action( 'admin_enqueue_scripts', 'eos_dp_scripts',999999 );
  			add_action( 'admin_enqueue_scripts', 'eos_dp_dequeue_stylesheets',999999 );
  			add_action( 'admin_head', 'eos_dp_dequeue_stylesheets',999999 );
  			add_action( 'admin_print_scripts', 'eos_dp_dequeue_stylesheets',999999 );
      }
		}
		add_action( 'admin_enqueue_scripts', 'eos_dp_style',10 );
	}
}

//It returns a password to use for page loading testing.
function eos_dp_pro_testing_uniqueid(){
	$pass = get_site_transient( 'eos_dp_pro_unique_id' );
	if( $pass ) return $pass;
	if( is_admin() ){
		$pass = md5( uniqid( microtime().mt_rand(),true ) );
		set_site_transient( 'eos_dp_pro_unique_id',$pass,60*60*24 );
	}
	return $pass;
}

add_action( 'activated_plugin', 'eos_dp_external_plugin_activation', 10, 2 );
//Save when a new plugin is activated
function eos_dp_external_plugin_activation( $plugin,$network_activation ) {
	eos_dp_update_option( 'eos_dp_new_plugin_activated','activated' );
}

//Update options in case of single or multisite installation.
function eos_dp_update_option( $option,$newvalue ){
	if( !is_multisite() ){
    $autoload = in_array( $option,array(
  		'eos_dp_archives',
  		'eos_dp_desktop',
  		'eos_dp_mobile',
  		'eos_dp_by_url',
  		'eos_post_types_plugins',
  		'eos_dp_opts',
  		'eos_dp_pro_main',
      'active_plugins'
  	) );
		return update_option( $option,$newvalue,$autoload );
	}
	else{
		return update_blog_option( get_current_blog_id(),$option,$newvalue );
	}
}

//Helper function to write file
function eos_dp_write_file( $source,$destination_dir,$destination,$update_info = false ){
	$writeAccess = false;
	$access_type = get_filesystem_method();
	if( $access_type === 'direct' ){
		/* you can safely run request_filesystem_credentials() without any issues and don't need to worry about passing in a URL */
		$creds = request_filesystem_credentials( admin_url(), '', false, false, array() );
		/* initialize the API */
		if ( ! WP_Filesystem( $creds ) ) {
			/* any problems and we exit */
			return false;
		}
		global $wp_filesystem;
		$writeAccess = true;
		if( empty( $wp_filesystem ) ){
			require_once ( ABSPATH . '/wp-admin/includes/file.php' );
			WP_Filesystem();
		}
		if( !$wp_filesystem->is_dir( $destination_dir ) ){
			/* directory didn't exist, so let's create it */
			$wp_filesystem->mkdir( $destination_dir );
		}

		$copied = @$wp_filesystem->copy( $source,$destination );
		if ( !$copied ) {
			printf( __( 'Failed to create %s','eos-dp' ),$destination );
		}
		else{
			if( $update_info ){
				set_transient( 'freesoul-dp-notice-succ', true, 5 );
				update_option( 'eos_dp_activation_info',array( 'time' => time(),'version' => EOS_DP_VERSION,'no' ) );
			}
		}
	}
	else{
		if( $update_info ){
			set_transient( 'freesoul-dp-notice-fail', true, 5 ); /* don't have direct write access. Prompt user with our notice */
		}
	}
}
if( isset( $_REQUEST['eos_dp_preview'] ) || isset( $_REQUEST['eos_dp_debug'] ) ){
	add_action( 'after_setup_theme','eos_dp_after_theme_setup_on_preview' );
}
//Actions and filters if page preview
function eos_dp_after_theme_setup_on_preview(){
	//Remove redirectin on checkout page
	add_filter( 'woocommerce_checkout_redirect_empty_cart', '__return_false' );
	add_filter( 'woocommerce_checkout_update_order_review_expired', '__return_false' );
}

add_action( 'plugins_loaded','eos_dp_prevent_missing_functions_errors' );
//It prevents errors due to missing functions when other plugins don't check properly the existence of other plugins
function eos_dp_prevent_missing_functions_errors(){
  if( !is_admin() && defined( 'EOS_WOOCOMMERCE_ACTIVE' ) && EOS_WOOCOMMERCE_ACTIVE ){
    require_once EOS_DP_PLUGIN_DIR.'/inc/fdp-woocommerce.php';
  }
}

do_action( 'fdp_loaded' );
