class-wp-otp-setup.php 3.02 KB
Newer Older
noplanman's avatar
noplanman committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php
/**
 * Handle all activation, deactivation and uninstallation tasks
 *
 * @package    Wp_Otp
 * @subpackage Setup
 * @since      0.1.0
 */

namespace Wp_Otp;

/**
 * Handle all activation, deactivation and uninstallation tasks.
 *
15
 * @since 0.1.0
noplanman's avatar
noplanman committed
16 17 18 19 20 21 22
 */
class Wp_Otp_Setup {
	/**
	 * Activation on a single or multisite network.
	 *
	 * @since 0.1.0
	 *
noplanman's avatar
noplanman committed
23 24 25
	 * @todo  Type declaration blocked by:
	 *        https://core.trac.wordpress.org/ticket/36406
	 *
noplanman's avatar
noplanman committed
26 27 28
	 * @param bool $network_wide TRUE if multisite/network and superadmin uses the "Network Activate" action.
	 *                           FALSE is no multisite install or plugin gets activated on a single blog.
	 */
29
	public static function activate( $network_wide ): void {
noplanman's avatar
noplanman committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
		if ( $network_wide && is_multisite() ) {
			foreach ( get_sites() as $site ) {
				switch_to_blog( $site->blog_id );
				self::do_activation();
			}

			restore_current_blog();
		} else {
			self::do_activation();
		}
	}

	/**
	 * Deactivation on a single or multisite/network.
	 *
	 * @since 0.1.0
	 *
	 * @param bool $network_wide TRUE if multisite/network and superadmin uses the "Network Deactivate" action.
	 *                           FALSE is no multisite install or plugin gets deactivated on a single blog.
	 */
noplanman's avatar
noplanman committed
50
	public static function deactivate( bool $network_wide ): void {
noplanman's avatar
noplanman committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
		if ( $network_wide && is_multisite() ) {
			foreach ( get_sites() as $site ) {
				switch_to_blog( $site->blog_id );
				self::do_deactivation();
			}

			restore_current_blog();
		} else {
			self::do_deactivation();
		}
	}

	/**
	 * The actual tasks performed during activation of a plugin.
	 *
	 * Should handle only stuff that happens during a single site activation,
67
	 * as the process is repeated for each site on a multisite/network installation
noplanman's avatar
noplanman committed
68 69 70 71
	 * if the plugin is activated network wide.
	 *
	 * @since 0.1.0
	 */
72
	private static function do_activation(): void {
noplanman's avatar
noplanman committed
73 74 75 76
		if ( ! current_user_can( 'activate_plugins' ) ) {
			return;
		}

77
		$plugin = $_REQUEST['plugin'] ?? ''; // phpcs:ignore
noplanman's avatar
noplanman committed
78 79 80 81 82 83 84
		check_admin_referer( "activate-plugin_{$plugin}" );
	}

	/**
	 * The actual tasks performed during deactivation of a plugin.
	 *
	 * Should handle only stuff that happens during a single site deactivation,
85
	 * as the process is repeated for each site on a multisite/network installation
noplanman's avatar
noplanman committed
86 87 88 89
	 * if the plugin is deactivated network wide.
	 *
	 * @since 0.1.0
	 */
90
	private static function do_deactivation(): void {
noplanman's avatar
noplanman committed
91 92 93 94
		if ( ! current_user_can( 'activate_plugins' ) ) {
			return;
		}

95
		$plugin = $_REQUEST['plugin'] ?? ''; // phpcs:ignore
noplanman's avatar
noplanman committed
96 97 98 99 100 101 102 103 104 105
		check_admin_referer( "deactivate-plugin_{$plugin}" );
	}

	/**
	 * Clean up process when this plugin is deleted. (called from uninstall.php)
	 *
	 * @since 0.1.0
	 *
	 * @param string $file Path of uninstall.php.
	 */
noplanman's avatar
noplanman committed
106
	public static function uninstall( string $file ): void {
noplanman's avatar
noplanman committed
107 108 109 110 111 112 113
		if ( ! current_user_can( 'activate_plugins' ) ) {
			return;
		}

		check_admin_referer( 'bulk-plugins' );

		// Important: Check if the file is the one that was registered during the uninstall hook.
114
		if ( WP_UNINSTALL_PLUGIN !== $file ) {
noplanman's avatar
noplanman committed
115 116 117 118
			return;
		}
	}
}