JezK
Edit File: banned-plugins.php
<?php if ( ! defined( 'WPINC' ) || ! defined( 'ABSPATH' ) ) { die; } require_once ABSPATH . 'wp-admin/includes/plugin.php'; class RBBannedPluginsPlugin { const NOTICE_TRANSIENT = 'rb_banned_plugins_deactivated'; /** * Plugins that must not run on a Box, mapped to the reason shown to the user. */ protected $banned_plugins = array( 'varnish-http-purge/varnish-http-purge.php' => 'Cache purging is already handled by Raidboxes on this Box.', ); public function __construct() { // LiteSpeed Cache is only banned on Boxes where the platform ships and // configures it as an MU-plugin (RB_LITESPEED_CACHE_PLUGIN), because // loading both copies at once is a fatal class redeclaration. On all // other Boxes the plugin from the WordPress repo may be used normally. if ( defined( 'RB_LITESPEED_CACHE_PLUGIN' ) && RB_LITESPEED_CACHE_PLUGIN ) { $this->banned_plugins['litespeed-cache/litespeed-cache.php'] = 'LiteSpeed Cache is already provided and configured by Raidboxes on this Box, so it cannot be used as a separate plugin.'; } add_action( 'admin_init', array( $this, 'deactivate_banned_plugins' ), 10, 0 ); // Has to be `activated_plugin` and not `activate_plugin`: the latter // fires before WordPress writes the `active_plugins` option, so the // plugin would not be in the list yet and nothing would be deactivated. add_action( 'activated_plugin', array( $this, 'deactivate_banned_plugins' ), 10, 0 ); add_action( 'admin_notices', array( $this, 'render_notice' ), 10, 0 ); add_action( 'network_admin_notices', array( $this, 'render_notice' ), 10, 0 ); } public function deactivate_banned_plugins() { $active_plugins = get_option( 'active_plugins', array() ); $deactivated = array(); foreach ( $active_plugins as $plugin ) { if ( isset( $this->banned_plugins[ $plugin ] ) ) { deactivate_plugins( $plugin ); $deactivated[ $plugin ] = $this->banned_plugins[ $plugin ]; } } if ( ! empty( $deactivated ) ) { set_transient( self::NOTICE_TRANSIENT, $deactivated, MINUTE_IN_SECONDS ); } } public function render_notice() { $deactivated = get_transient( self::NOTICE_TRANSIENT ); // Keep the transient until somebody who can act on it sees the notice. if ( empty( $deactivated ) || ! current_user_can( 'activate_plugins' ) ) { return; } delete_transient( self::NOTICE_TRANSIENT ); foreach ( $deactivated as $plugin => $reason ) { printf( '<div class="notice notice-warning"><p><strong>%s</strong> %s</p></div>', esc_html( sprintf( 'The plugin "%s" was deactivated by Raidboxes.', $this->plugin_name( $plugin ) ) ), esc_html( $reason ) ); } } /** * Human readable plugin name, falling back to the directory name when the * plugin files are not readable (anymore). */ protected function plugin_name( $plugin ) { $data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, false ); return empty( $data['Name'] ) ? dirname( $plugin ) : $data['Name']; } } new RBBannedPluginsPlugin();