<?php
/**
 * Plugin Name: Maintainn Tools
 * Version: 2.3.4
 * Description: A plugin to Maintainn your WordPress website
 * Author: Maintainn
 * Author URI: https://maintainn.com/
 * Plugin URI: https://pluginize.com/
 * Text Domain: maintainn-tools
 * Domain Path: /languages
 *
 * @package Maintainn Tools
 */

/**
 * Copyright (c) 2017 WebDevStudios / Maintainn (email : info@maintainn.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2 or, at
 * your discretion, any later version, as published by the Free
 * Software Foundation.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

define( 'TOOLS_VERSION', '2.3.4' ); // Left for legacy purposes.
define( 'MAINTAINN_TOOLS_VERSION', '2.3.4' );

/**
 * Autoloader for the Maintainn Tools plugin.
 *
 * @param  string $class_name Classname to attempt to autoload.
 * @return void
 */
function maintainn_customer_autoload_classes( $class_name ) {
	// Bail early if the class isn't prefixed correctly.
	if ( 0 !== strpos( $class_name, 'Maintainn_Tools' ) ) {
		return;
	}

	$filename = strtolower( str_ireplace(
		[ 'Maintainn_Tools_', '_' ],
		[ '', '-' ],
		$class_name
	) );

	Maintainn_Tools::include_file( $filename );
}
spl_autoload_register( 'maintainn_customer_autoload_classes' );


/**
 * Main initiation class
 *
 * @since  0.1.0
 * @var  string $version  Plugin version
 * @var  string $basename Plugin basename
 * @var  string $url      Plugin URL
 * @var  string $path     Plugin Path
 */
class Maintainn_Tools {

	/**
	 * Current version
	 *
	 * @var  string
	 * @since  0.1.0
	 */
	const VERSION = '2.3.4';

	/**
	 * URL of plugin directory
	 *
	 * @var string
	 * @since  0.1.0
	 */
	protected $url = '';

	/**
	 * Path of plugin directory
	 *
	 * @var string
	 * @since  0.1.0
	 */
	protected $path = '';

	/**
	 * Plugin basename
	 *
	 * @var string
	 * @since  0.1.0
	 */
	protected $basename = '';

	/**
	 * Utilities
	 *
	 * @var string
	 */
	public $utilities = '';

	/**
	 * Helpers
	 *
	 * @var string
	 */
	public $helpers = '';

	/**
	 * Log
	 *
	 * @var string
	 */
	public $log = '';

	/**
	 * AJAX
	 *
	 * @var string
	 */
	public $ajax = '';
	/**
	 * Checksums
	 *
	 * @var string
	 */
	public $verify_checksums = '';

	/**
	 * WP-CLI
	 *
	 * @var string
	 */
	public $cli = '';

	/**
	 * Dashboard
	 *
	 * @var Maintainn_Tools_Dashboard
	 * @since 2.0.0
	 */
	public $dashboard = '';

	/**
	 * Dashboard
	 *
	 * @var Maintainn_Tools_Extensions
	 * @since 2.0.0
	 */
	public $extensions = '';

	/**
	 * Singleton instance of plugin
	 *
	 * @var object
	 * @since  0.1.0
	 */
	protected static $single_instance;

	/**
	 * Creates or returns an instance of this class.
	 *
	 * @since  0.1.0
	 * @return object Maintainn_Scanner A single instance of this class.
	 */
	public static function get_instance() {
		if ( null === self::$single_instance ) {
			self::$single_instance = new self();
		}

		return self::$single_instance;
	}

	/**
	 * Sets up our plugin
	 *
	 * @since  0.1.0
	 */
	protected function __construct() {
		$cli = defined( 'WP_CLI' ) && WP_CLI;

		// Bail early if not admin.
		if ( ! $cli && ! is_admin() ) {
			return;
		}

		$this->basename = plugin_basename( __FILE__ );
		$this->url      = plugin_dir_url( __FILE__ );
		$this->path     = plugin_dir_path( __FILE__ );

		$this->plugin_classes();
		$this->hooks();
	}

	/**
	 * Attach other plugin classes to the base plugin class.
	 *
	 * @since 0.1.0
	 */
	protected function plugin_classes() {
		// Include helpers.
		$this->helpers = new Maintainn_Tools_Helpers( $this );

		$this->log = new Maintainn_Tools_Log( $this );

		$this->ajax = new Maintainn_Tools_Ajax( $this );

		$this->verify_checksums = new Maintainn_Tools_Verify_Checksums( $this );

		// Only add wp cli class if running via cli.
		if ( defined( 'WP_CLI' ) && WP_CLI ) {
			$this->cli = new Maintainn_Tools_Cli( $this );
		}

		// Load only on admin side.
		if ( is_admin() && ( ! wp_doing_ajax() ) ) {
			$this->dashboard = new Maintainn_Tools_Dashboard( $this );
		}

		// Our available extensions|addons.
		$this->extensions = new Maintainn_Tools_Extensions( $this );

		// Only run this if it is active.
		if ( $this->are_any_extensions_active() ) {
			self::include_vendor( 'plugin-police' );
			self::include_vendor( 'tools-view' );
			self::include_vendor( 'cpt-core' );
			self::include_vendor( 'plugin-notes' );
			self::include_vendor( 'class-tools-form' );
		}
	}

	/**
	 * Check all extension slugs.
	 *
	 * @return bool
	 */
	private function are_any_extensions_active() {
		$extensions = [ 'notes', 'lock', 'update' ];
		foreach ( $extensions as $extension ) {
			if ( $this->extensions->is_active( $extension ) ) {
				return true;
			}
		}

		return false;
	}

	/**
	 * Add hooks and filters
	 *
	 * @since 0.1.0
	 */
	public function hooks() {
		register_activation_hook( __FILE__, [ $this, 'activate' ] );

		add_action( 'init', [ $this, 'init' ] );
	}

	/**
	 * Activate the plugin
	 *
	 * @since 0.1.0
	 */
	public function activate() {
		// Make sure any rewrite functionality has been loaded.
		flush_rewrite_rules();
	}

	/**
	 * Init hooks
	 *
	 * @since 0.1.0
	 */
	public function init() {
		if ( $this->check_requirements() ) {
			load_plugin_textdomain( 'maintainn-tools', false, dirname( $this->basename ) . '/languages/' );
		}
	}

	/**
	 * Check that all plugin requirements are met
	 *
	 * @since 0.1.0
	 *
	 * @return boolean
	 */
	public static function meets_requirements() {
		// We have met all requirements.
		return true;
	}

	/**
	 * Check if the plugin meets requirements and
	 * disable it if they are not present.
	 *
	 * @since 0.1.0
	 *
	 * @return boolean result of meets_requirements
	 */
	public function check_requirements() {
		if ( ! self::meets_requirements() ) {
			// Display our error.
			echo '<div id="message" class="error">';
			echo '<p>' . sprintf( __( 'Maintainn Scanner is missing requirements and has been <a href="%s">deactivated</a>. Please make sure all requirements are available.', 'maintainn-tools' ), esc_url( admin_url( 'plugins.php' ) ) ) . '</p>';
			echo '</div>';

			// Deactivate our plugin.
			deactivate_plugins( $this->basename );

			return false;
		}

		return true;
	}

	/**
	 * Magic getter for our object.
	 *
	 * @since 0.1.0
	 *
	 * @param string $field Field to fetch.
	 * @throws Exception Throws an exception if the field is invalid.
	 * @return mixed
	 */
	public function __get( $field ) {
		switch ( $field ) {
			case 'version':
				return self::VERSION;
			case 'basename':
			case 'url':
			case 'path':
				return $this->$field;
			default:
				throw new Exception( 'Invalid ' . __CLASS__ . ' property: ' . $field );
		}
	}

	/**
	 * Include a file from the includes directory.
	 *
	 * @since 0.1.0
	 *
	 * @param string $filename Name of the file to be included.
	 * @return bool Result of include call.
	 */
	public static function include_file( $filename ) {
		$file = self::dir( 'includes/' . $filename . '.php' );

		if ( file_exists( $file ) ) {
			return include_once $file;
		}
		return false;
	}

	/**
	 * Include a file from the includes directory.
	 *
	 * @since 0.1.0
	 * @param string $filename Name of the file to be included.
	 * @return bool Result of include call.
	 */
	public static function include_vendor( $filename ) {
		$file = self::dir( 'vendor/' . $filename . '.php' );

		if ( file_exists( $file ) ) {
			return include_once $file;
		}

		return false;
	}

	/**
	 * Include a file from the includes directory.
	 *
	 * @since 0.1.0
	 *
	 * @param string $filename Name of the file to be included.
	 * @return bool Result of include call.
	 */
	public static function include_view( $filename ) {
		$file = self::dir( 'views/' . $filename . '.php' );

		if ( file_exists( $file ) ) {
			return include $file;
		}

		return false;
	}

	/**
	 * This plugin's directory.
	 *
	 * @since 0.1.0
	 *
	 * @param string $path (optional) appended path.
	 * @return string       Directory and path
	 */
	public static function dir( $path = '' ) {
		static $dir;
		$dir = $dir ? $dir : trailingslashit( __DIR__ );
		return $dir . $path;
	}

	/**
	 * This plugin's url.
	 *
	 * @since 0.1.0
	 *
	 * @param string $path (optional) appended path.
	 * @return string URL and path
	 */
	public static function url( $path = '' ) {
		static $url;
		$url = $url ? $url : trailingslashit( plugin_dir_url( __FILE__ ) );
		return $url . $path;
	}

	/**
	 * Return plugin version.
	 */
	public function version() {
		return self::VERSION;
	}

	/**
	 * Suffix for scripts/styles
	 *
	 * @since 0.1.0
	 * @return  string
	 */
	public function get_script_suffix() {
		// If WP is in script debug, or we pass ?script_debug in a URL - set debug to true.
		return ( defined( 'SCRIPT_DEBUG' ) && ( true === SCRIPT_DEBUG ) )
			|| ( isset( $_GET['script_debug'] ) )
			? ''
			: '.min';
	}

}

/**
 * Grab the Maintainn_Tools object and return it.
 * Wrapper for Maintainn_Tools::get_instance()
 *
 * @since 0.1.0
 *
 * @return object Singleton instance of plugin class.
 */
function maintainn_customer() {
	return Maintainn_Tools::get_instance();
}

/**
 * Set the Maintainn Tools Setting Link
 *
 * @since 0.1.0
 *
 * @param array $links Action links for the plugin.
 * @return array  Maintainn Tools Link
 */
function maintainn_tools_link( $links ) {
	$settings_link = '<a href="' . admin_url( 'admin.php?page=maintainn' ) . '">' . __( 'Settings', 'maintainn-tools' ) . '</a>';

	$links[] = $settings_link;
	return $links;
}
$plugin = plugin_basename( __FILE__ );
add_filter( "plugin_action_links_$plugin", 'maintainn_tools_link' );

// Kick it off.
maintainn_customer();


function myplugin_add_debug_info( $debug_info ) {

	if ( ! class_exists( 'Browser' ) ) {
		Maintainn_Tools::get_instance()->include_vendor( 'browser' );
	}

	global $wpdb;
	$browser     = new Browser();
	$system_info = new Maintainn_Tools_System_Info( $browser, $wpdb );
	$info        = $system_info->get_system_info();

	$maintainn_fields               = [];
	$maintainn_fields['post_types'] = [
		'label' => __( 'Registered Post Types', 'maintainn-tools' ),
		'value' => $info['site_info']['post_types'],
	];

	$maintainn_fields['post_stati'] = [
		'label' => __( 'Registered Post Stati', 'maintainn-tools' ),
		'value' => $info['site_info']['post_stati'],
	];

	$maintainn_fields['image_sizes'] = [
		'label' => __( 'Registered Images Sizes', 'maintainn-tools' ),
		'value' => $info['image_sizes'],
	];

	// @todo TEST ON A MULTISITE
	if ( is_multisite() && isset( $info['plugins']['network_plugins'] ) ) {
		$network_plugins = [];
		foreach ( $info['plugins']['network_active_plugins'] as $plugin_path ) {
			$plugin_base  = plugin_basename( $plugin_path );
			$the_plugin   = [];
			$plugin       = get_plugin_data( $plugin_path );
			$the_plugin[] = "{$plugin['Name']} {$plugin['Version']}";
		}

		$debug_info['maintainn-tools-network-plugins'] = [
			'label'  => __( 'Network Activated Plugins', 'maintainn-tools' ),
			'fields' => $network_plugins,
		];
	}

	$maintainn_fields['site_url'] = [
		'label' => __( 'Site URL', 'maintainn-tools' ),
		'value' => $info['site_info']['site_url'],
	];

	$maintainn_fields['home_url'] = [
		'label' => __( 'Home URL', 'maintainn-tools' ),
		'value' => $info['site_info']['home_url'],
	];

	$maintainn_fields['page_for_posts'] = [
		'label' => __( 'Posts Page', 'maintainn-tools' ),
		'value' => $info['site_info']['page_for_posts'],
	];

	$maintainn_fields['page_on_front'] = [
		'label' => __( 'Frontpage Page', 'maintainn-tools' ),
		'value' => $info['site_info']['page_on_front'],
	];

	$maintainn_fields['show_on_front'] = [
		'label' => __( 'Show on Front', 'maintainn-tools' ),
		'value' => $info['site_info']['show_on_front'],
	];

	foreach ( $info['wp_extra'] as $extrakey => $extravalue ) {
		$maintainn_fields[ $extrakey ] = [
			'label' => ucfirst( str_replace( '_', ' ', $extrakey ) ),
			'value' => $extravalue,
		];
	}

	$debug_info['maintainn-tools'] = [
		'label'  => __( 'Maintainn Extra Data', 'maintainn-tools' ),
		'fields' => $maintainn_fields,
	];

	$maintainn_db_fields = [];
	foreach ( $info['db_info'] as $dbk => $db ) {
		$maintainn_db_fields[ $dbk ] = [
			'label' => ucfirst( str_replace( '_', ' ', $dbk ) ),
			'value' => $db,
		];
	}

	$debug_info['maintainn-tools-database'] = [
		'label'  => __( 'Maintainn Database', 'maintainn-tools' ),
		'fields' => $maintainn_db_fields,
	];

	$maintainn_browser_fields = [];
	foreach ( $info['browser'] as $browserkey => $browser ) {
		$maintainn_browser_fields[ $browserkey ] = [
			'label' => ucfirst( str_replace( '_', ' ', $browserkey ) ),
			'value' => $browser,
		];
	}

	$debug_info['maintainn-tools-browser'] = [
		'label'  => esc_html__( 'Maintainn Browser', 'maintainn-tools' ),
		'fields' => $maintainn_browser_fields,
	];

	$maintainn_php_info_fields = [];
	foreach ( $info['php_info'] as $phpkey => $phpvalue ) {
		$maintainn_php_info_fields[ $phpkey ] = [
			'label' => ucfirst( str_replace( '_', ' ', $phpkey ) ),
			'value' => $phpvalue,
		];
	}

	$debug_info['maintainn-tools-php-info'] = [
		'label'  => esc_html__( 'Maintainn PHP Info', 'maintainn-tools' ),
		'fields' => $maintainn_php_info_fields,
	];

	$maintainn_server_info_fields = [];
	foreach ( $info['server_info'] as $serverkey => $servervalue ) {
		$maintainn_server_info_fields[ $serverkey ] = [
			'label' => ucfirst( str_replace( '_', ' ', $serverkey ) ),
			'value' => $servervalue,
		];
	}

	$debug_info['maintainn-tools-server-info'] = [
		'label'  => esc_html__( 'Maintainn Server Info', 'maintainn-tools' ),
		'fields' => $maintainn_server_info_fields,
	];

	$maintainn_system_overview_fields = [];
	foreach ( $info['system_overview'] as $systemkey => $systemvalue ) {
		$maintainn_system_overview_fields[ $systemkey ] = [
			'label' => ucfirst( str_replace( '_', ' ', $systemkey ) ),
			'value' => $systemvalue,
		];
	}

	$debug_info['maintainn-tools-system-overview'] = [
		'label'  => esc_html__( 'Maintainn System Overview', 'maintainn-tools' ),
		'fields' => $maintainn_system_overview_fields,
	];

	$maintainn_security_fields = [];
	foreach ( $info['sec_info'] as $seckey => $secvalue ) {
		$maintainn_security_fields[ $seckey ] = [
			'label' => ucfirst( str_replace( '_', ' ', $seckey ) ),
			'value' => $secvalue,
		];
	}

	$debug_info['maintainn-tools-security-overview'] = [
		'label'  => esc_html__( 'Maintainn Security Overview', 'maintainn-tools' ),
		'fields' => $maintainn_security_fields,
	];

	return $debug_info;
}
add_filter( 'debug_information', 'myplugin_add_debug_info' );
