class-import-hooks.php 1.96 KB
Newer Older
imac's avatar
imac committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
<?php
/**
 * @package WPSEO\Admin\Import
 */

/**
 * Abstract object for handling the importing and deactivating of the plugin
 */
abstract class WPSEO_Import_Hooks {

	/**
	 * @var string The main plugin file.
	 */
	protected $plugin_file;

	/**
	 * @var string The GET parameter for running the import.
	 */
	protected $import_listener;

	/**
	 * @var string The GET parameter for deactivating the plugin.
	 */
	protected $deactivation_listener;

	/**
	 * Throw a notice to import settings.
	 *
	 * @since 3.0
	 */
	abstract public function show_import_settings_notice();

	/**
	 * Throw a notice to inform the user that the plugin has been deactivated
	 *
	 * @since 3.0
	 */
	abstract public function show_deactivate_notice();

	/**
	 * Adding the hooks to show import/deactivate message when needed.
	 */
	public function __construct() {
		if ( $this->is_active() ) {
			$this->show_import_message();
			$this->show_deactivate_message();
		}
	}

	/**
	 * Handle deactivation & import of the data data
	 *
	 * @since 3.0
	 */
	public function show_import_message() {
		if ( filter_input( INPUT_GET, 'tool' ) !== 'import-export' ) {
			add_action( 'admin_notices', array( $this, 'show_import_settings_notice' ) );
		}
	}

	/**
	 * Handle deactivation of the plugin
	 *
	 * @since 3.0
	 */
	public function show_deactivate_message() {
		if ( filter_input( INPUT_GET, $this->deactivation_listener ) === '1' ) {
			// Deactivate AIO.
			deactivate_plugins( $this->plugin_file );

			// Show notice that aioseo has been deactivated.
			add_action( 'admin_notices', array( $this, 'show_deactivate_notice' ) );

			// Clean up the referrer url for later use.
			if ( isset( $_SERVER['REQUEST_URI'] ) ) {
				$_SERVER['REQUEST_URI'] = remove_query_arg( array( $this->deactivation_listener ), sanitize_text_field( $_SERVER['REQUEST_URI'] ) );
			}
		}
	}

	/**
	 * Check if the plugin is active.
	 *
	 * @return bool
	 */
	protected function is_active() {
		return is_plugin_active( $this->plugin_file );
	}
}