free = $free;
$this->other = $other;
}
/**
* Hook the notice and the button's handler.
*/
public function register(): void {
add_action( 'admin_notices', array( $this, 'render' ) );
add_action( 'network_admin_notices', array( $this, 'render' ) );
add_action( 'admin_post_' . self::ACTION, array( $this, 'handle_deactivate' ) );
}
/**
* Print the notice.
*/
public function render(): void {
if ( ! current_user_can( 'activate_plugins' ) || ! $this->both_active() ) {
return;
}
echo '
' . esc_html__( 'Consentfold:', 'consentfold' ) . ' '
. esc_html__( 'The free Consentfold plugin is no longer needed. Both Consentfold packages are active, and the other one includes everything the free one does. Nothing is switched off until you choose to.', 'consentfold' )
. '
';
if ( ! $this->may_deactivate() ) {
echo '
' . esc_html__( 'The free plugin is active for the whole network. Ask a network administrator to deactivate it.', 'consentfold' ) . '
';
} elseif ( ! $this->other_covers_free() ) {
echo '
' . esc_html__( 'The free plugin is active for the whole network, so it can only be switched off network-wide, and the other package is not active on every site. Deactivating it would remove Consentfold from the other sites.', 'consentfold' ) . '
';
} else {
echo '
';
}
echo '
';
}
/**
* Handle the button: check the nonce and the capability, deactivate the free copy, go back.
*/
public function handle_deactivate(): void {
check_admin_referer( self::ACTION );
self::load_plugin_functions();
if ( ! $this->may_deactivate() ) {
wp_die( esc_html__( 'You do not have permission to do this.', 'consentfold' ), '', array( 'response' => 403 ) );
}
if ( ! $this->other_covers_free() ) {
wp_die( esc_html__( 'The other Consentfold package is not active everywhere the free one is, so the free one stays on.', 'consentfold' ), '', array( 'response' => 409 ) );
}
if ( is_plugin_active( $this->free ) ) {
// A network-active copy is switched off for the whole network.
deactivate_plugins( $this->free, false, $this->network_active( $this->free ) );
}
wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url( 'plugins.php' ) );
exit;
}
/**
* Whether both packages are active here: on the network screens, network-active; elsewhere, active on the site or
* network-active.
*/
private function both_active(): bool {
self::load_plugin_functions();
if ( is_network_admin() ) {
return $this->network_active( $this->free ) && $this->network_active( $this->other );
}
return is_plugin_active( $this->free ) && is_plugin_active( $this->other );
}
/**
* Whether the current user may deactivate the free copy: the right to deactivate it and, when it is network-active,
* the right to manage network plugins too.
*/
private function may_deactivate(): bool {
self::load_plugin_functions();
if ( $this->network_active( $this->free ) && ! current_user_can( 'manage_network_plugins' ) ) {
return false;
}
return current_user_can( 'deactivate_plugin', $this->free );
}
/**
* Whether the other package is active wherever the free copy is: a network-active free copy needs the other one
* network-active too, otherwise being active here is enough.
*/
private function other_covers_free(): bool {
self::load_plugin_functions();
if ( $this->network_active( $this->free ) ) {
return $this->network_active( $this->other );
}
return is_plugin_active( $this->other );
}
/**
* Whether a plugin is active for the whole network.
*
* @param string $file Plugin file, relative to the plugins directory.
*/
private function network_active( string $file ): bool {
return is_multisite() && is_plugin_active_for_network( $file );
}
/**
* The plugin functions, which core loads in the admin only.
*/
private static function load_plugin_functions(): void {
if ( ! function_exists( 'is_plugin_active' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
}
}