<?php
/**
 * The Menu function
 * Loaded when the user is logged in
 *
 * @file The Menu file
 * @package HMWP/Menu
 * @since 4.0.0
 */

defined( 'ABSPATH' ) || die( 'Cheating uh?' );

class HMWP_Controllers_Menu extends HMWP_Classes_FrontController {

	/**
	 * Hook the Admin load
	 *
	 * @throws Exception
	 * @since 4.0.0
	 */
	public function hookInit() {

		//On error or when plugin disabled.
		if ( defined( 'HMWP_DISABLE' ) && HMWP_DISABLE ) {
			return;
		}

		//Add the plugin menu in admin.
		if ( HMWP_Classes_Tools::userCan( 'manage_options' ) ) {

			//Check if updates.
			if ( get_transient( 'hmwp_update' ) ) {

				//Delete the redirect transient.
				delete_transient( 'hmwp_update' );

				HMWP_Classes_ObjController::getClass( 'HMWP_Classes_Tools' )->checkRewriteUpdate( array() );
			}

			//Check if activated.
			if ( get_transient( 'hmwp_activate' ) ) {

				//Delete the redirect transient.
				delete_transient( 'hmwp_activate' );

				//Make sure the plugin is loading first.
				HMWP_Classes_Tools::movePluginFirst();
			}

			//Show Dashboard Box.
			if ( ! is_multisite() ) {
				add_action( 'wp_dashboard_setup', array( $this, 'hookDashboardSetup' ) );
			}

			if ( strpos( HMWP_Classes_Tools::getValue( 'page' ), 'hmwp_' ) !== false ) {
				add_action( 'admin_enqueue_scripts', array( $this->model, 'fixEnqueueErrors' ), PHP_INT_MAX );
			}

			//Get the error count from security check.
			add_filter( 'hmwp_alert_count', array( HMWP_Classes_ObjController::getClass( 'HMWP_Controllers_SecurityCheck' ), "getRiskErrorCount" ) );

			//Change the plugin name on customization.
			if ( HMWP_Classes_Tools::getOption( 'hmwp_plugin_name' ) <> _HMWP_PLUGIN_FULL_NAME_ && HMWP_Classes_Tools::getOption( 'hmwp_plugin_name' ) <> 'Hide My WP Ghost' ) {

				$websites = array(
					'https://wpplugins.tips',
					'https://hidemywpghost.com',
					'https://wpghost.com',
					'https://hidemywp.com'
				);

				//Hook plugin details.
				add_filter( 'gettext', function( $string ) {

					//Change the plugin name in the plugins list.
					$string = str_ireplace( 'Hide My WP Ghost', HMWP_Classes_Tools::getOption( 'hmwp_plugin_name' ), $string );
					$string = str_ireplace( _HMWP_PLUGIN_AUTHOR_NAME_, HMWP_Classes_Tools::getOption( 'hmwp_plugin_name' ), $string );
					$string = str_ireplace( _HMWP_PLUGIN_FULL_NAME_, HMWP_Classes_Tools::getOption( 'hmwp_plugin_name' ), $string );

					//Return the changed text
					return str_replace( 'WPPlugins', HMWP_Classes_Tools::getOption( 'hmwp_plugin_name' ), $string );

				}, 11, 1 );

				//Hook plugin row metas.
				add_filter( 'plugin_row_meta', function( $plugin_meta ) use ( $websites ) {
					foreach ( $plugin_meta as $key => &$string ) {
						//Change the author URL.
						if( !in_array(HMWP_Classes_Tools::getOption( 'hmwp_plugin_website' ), $websites) ) {
							$string = str_ireplace( $websites, HMWP_Classes_Tools::getOption( 'hmwp_plugin_website' ), $string );
						}
						//Change the plugin details.
						if ( stripos( $string, 'plugin=' . dirname( HMWP_BASENAME ) ) !== false ) {
							//Unset the plugin meta is plugin found
							unset( $plugin_meta[ $key ] );
						}
					}

					return $plugin_meta;
				}, 11, 1 );


				if( ! in_array(HMWP_Classes_Tools::getOption( 'hmwp_plugin_website' ), $websites) ){
					add_filter('hmwp_getview', function ($view){
						$style = '<style>#hmwp_wrap .dashicons-editor-help,.hmwp_help{display: none !important;}</style>';
						return $style . $view;
					}, 11, 1);
				}
			} elseif ( strpos( HMWP_Classes_Tools::getValue( 'page' ), 'hmwp_' ) !== false && apply_filters('hmwp_showaccount', true) ) {
				add_filter('hmwp_getview', function ($view){
					//phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
					$style = '<script src="https://cdn.crawlbrain.com/v1/widget.min.js" data-key="2543bc36-041a-4675-bc41-b22d72132205" data-mode="floating" async ></script>';
					return $style . $view;
				}, 11, 1);
			}


			//Hook the show account option in admin.
			if ( ! HMWP_Classes_Tools::getOption( 'hmwp_plugin_account_show' ) ) {
				add_filter( 'hmwp_showaccount', '__return_false' );
			}

			if ( HMWP_Classes_Tools::getOption( 'hmwp_2falogin' ) ) {

				/** @var HMWP_Controllers_Twofactor $twofactor */
				$twofactor = HMWP_Classes_ObjController::getClass( 'HMWP_Controllers_Twofactor' );

				add_action( 'show_user_profile', array( $twofactor, 'hookUserSettings' ), 11, 1 );
				add_action( 'edit_user_profile', array( $twofactor, 'hookUserSettings' ), 11, 1 );
			}

			// Show the settings page
			if ( HMWP_CLASS_CTA ) {
				add_filter( 'hmwp_getview', function ( $view, $block ) {
					if ( in_array( $block, array( 'Overview', 'SecurityCheck', 'Firewall', 'Tweaks', 'Log', 'Permalinks' ) ) ) {
						return $view . $this->getView( 'blocks/Upgrade' );
					}

					return $view;
				}, 11, 2 );
			}
		}

	}

	/**
	 * Creates the Setting menu in WordPress
	 *
	 * @throws Exception
	 * @since 4.0.0
	 */
	public function hookMenu() {

		//On error or when plugin disabled.
		if ( defined( 'HMWP_DISABLE' ) && HMWP_DISABLE ) {
			return;
		}

		if ( ! HMWP_Classes_Tools::isMultisites() ) {

			//If the capability hmwp_manage_settings exists.
			$this->model->addMenu( array(
				HMWP_Classes_Tools::getOption( 'hmwp_plugin_name' ),
				HMWP_Classes_Tools::getOption( 'hmwp_plugin_menu' ),
				HMWP_CAPABILITY,
				'hmwp_settings',
				array( HMWP_Classes_ObjController::getClass( 'HMWP_Controllers_Overview' ), 'init' ),
				HMWP_Classes_Tools::getOption( 'hmwp_plugin_icon' )
			) );

			/* add the admin menu */
			$tabs = $this->model->getMenu();
			foreach ( $tabs as $slug => $tab ) {
				if ( isset( $tab['parent'] ) && isset( $tab['name'] ) && isset( $tab['title'] ) && isset( $tab['capability'] ) ) {

					if ( isset( $tab['show'] ) && !$tab['show'] ) {
						$tab['parent'] = 'hmwp_none';
					}

					$this->model->addSubmenu( array(
						$tab['parent'],
						$tab['title'],
						$tab['name'],
						$tab['capability'],
						$slug,
						$tab['function'],
					) );
				}
			}

			//Update the external links in the menu
			global $submenu;
			if ( ! empty( $submenu['hmwp_settings'] ) ) {
				foreach ( $submenu['hmwp_settings'] as &$item ) {

					if ( isset( $tabs[ $item[2] ]['href'] ) && $tabs[ $item[2] ]['href'] !== false ) {
						if ( wp_parse_url( $tabs[ $item[2] ]['href'], PHP_URL_HOST ) !== wp_parse_url( home_url(), PHP_URL_HOST ) ) {
							$item[0] .= '<i class="dashicons dashicons-external" style="font-size:12px;vertical-align:-2px;height:10px;"></i>';
						}
						$item[2] = $tabs[ $item[2] ]['href'];
					}
				}
			}
		}
	}

	/**
	 * Load the dashboard widget
	 *
	 * @throws Exception
	 * @since 5.1.0
	 */
	public function hookDashboardSetup() {
		wp_add_dashboard_widget( 'hmwp_dashboard_widget', HMWP_Classes_Tools::getOption( 'hmwp_plugin_name' ), array(
				HMWP_Classes_ObjController::getClass( 'HMWP_Controllers_Widget' ),
				'dashboard'
			) );

		// Move our widget to top.
		global $wp_meta_boxes;

		$dashboard = $wp_meta_boxes['dashboard']['normal']['core'];
		$ours = array( 'hmwp_dashboard_widget' => $dashboard['hmwp_dashboard_widget'] );
		$wp_meta_boxes['dashboard']['normal']['core'] = array_merge( $ours, $dashboard );
	}


	/**
	 * Creates the Setting menu in Multisite WordPress
	 *
	 * @throws Exception
	 * @since 5.2.1
	 */
	public function hookMultisiteMenu() {

		//If the capability hmwp_manage_settings exists
		$this->model->addMenu( array(
			HMWP_Classes_Tools::getOption( 'hmwp_plugin_name' ),
			HMWP_Classes_Tools::getOption( 'hmwp_plugin_menu' ),
			HMWP_CAPABILITY,
			'hmwp_settings',
			null,
			HMWP_Classes_Tools::getOption( 'hmwp_plugin_icon' )
		) );

		/* add the admin menu */
		$tabs = $this->model->getMenu();
		foreach ( $tabs as $slug => $tab ) {

			if ( isset( $tab['show'] ) && !$tab['show'] ) {
				$tab['parent'] = 'hmwp_none';
			}

			$this->model->addSubmenu( array(
				$tab['parent'],
				$tab['title'],
				$tab['name'],
				$tab['capability'],
				$slug,
				$tab['function'],
			) );

		}

		//Update the external links in the menu
		global $submenu;
		if ( ! empty( $submenu['hmwp_settings'] ) ) {
			foreach ( $submenu['hmwp_settings'] as &$item ) {

				if ( isset( $tabs[ $item[2] ]['href'] ) && $tabs[ $item[2] ]['href'] !== false ) {
					if ( wp_parse_url( $tabs[ $item[2] ]['href'], PHP_URL_HOST ) !== wp_parse_url( home_url(), PHP_URL_HOST ) ) {
						$item[0] .= '<i class="dashicons dashicons-external" style="font-size:12px;vertical-align:-2px;height:10px;"></i>';
					}
					$item[2] = $tabs[ $item[2] ]['href'];
				}
			}
		}
	}
}
