Skip to content

Instantly share code, notes, and snippets.

@panoslyrakis
Last active July 1, 2017 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panoslyrakis/c80d47ccc43167e51248b5147f7eccbd to your computer and use it in GitHub Desktop.
Save panoslyrakis/c80d47ccc43167e51248b5147f7eccbd to your computer and use it in GitHub Desktop.
Pro Sites - Limit plugins per level
<?php
/*
Plugin Name: Pro Sites - Limit plugins per level
Plugin URI: https://premium.wpmudev.org/
Description: Allow specific number of plugins to be available per level. Requires Pro Sites plugin
Author: Panos Lyrakis @ WPMUDEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_PS_Limit_Plugins' ) ) {
class WPMUDEV_PS_Limit_Plugins {
private static $_instance = null;
public $levels = null,
$active_plugins_count = null,
$blog_level = null,
$allowed_plugins_num = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_PS_Limit_Plugins();
}
return self::$_instance;
}
public function __construct() {
if( ! $this->is_plugins_page() ){
return;
}
$this->levels = self::get_levels();
$this->active_plugins_count = $this->count_blog_active_plugins();
$this->blog_level = $this->get_blog_level();
$this->allowed_plugins_num = $this->get_allowed_plugins_num();
add_action( 'admin_notices', array( $this, 'admin_notice' ) );
add_action( 'activated_plugin', array( $this, 'maybe_deactivate_plugin' ), 10, 2 );
add_filter( 'plugin_action_links', array( $this, 'maybe_remove_activation_links' ), 10, 4 );
}
public function plugins_per_level(){
$plugins_per_level = array(
0 => 1, //0 not a pro site, no level...
1 => 2,
2 => 4,
3 => '-' //'-' has no limit
);
return $plugins_per_level;
}
public function blog_can_have_more_plugins(){
$plugins_by_super = count( get_option( 'wpmudev_plugins_by_super', array() ) );
return $this->allowed_plugins_num == '-' || (int)$this->allowed_plugins_num > ( (int)$this->active_plugins_count - (int)$plugins_by_super );
}
public function admin_notice(){
global $blog_id, $psts;
if( $this->blog_can_have_more_plugins() ){
return;
}
$upgrade_link = '<a href="' . $psts->checkout_url( $blog_id ) . '" target="_parent" class="activatelink nonpsts button-primary" t">' . __( 'upgrade level', 'psts' ) . '</a>';
?>
<div class="notice notice-error is-dismissible">
<p><?php echo sprintf( __( 'You are not allowed to activate any other plugins. If you need to activate a new one you can either deactivate one or %s', 'psts' ), $upgrade_link ); ?></p>
</div>
<?php
}
public function maybe_deactivate_plugin( $plugin, $network_wide ){
if( $network_wide ) {
return;
}
if( is_super_admin( get_current_user_id() ) ){
global $blog_id;
$plugins_by_super = get_option( 'wpmudev_plugins_by_super', array() );
$plugins_by_super[] = $plugin;
update_option( 'wpmudev_plugins_by_super', $plugins_by_super );
return;
}
if( ! $this->blog_can_have_more_plugins() ){
if( ! is_plugin_active_for_network( $plugin ) ){
deactivate_plugins( $plugin );
}
}
}
public function maybe_remove_activation_links( $actions, $plugin_file, $plugin_data, $context ){
if( is_super_admin( get_current_user_id() ) ){
return $actions;
}
if ( ! $this->blog_can_have_more_plugins() && array_key_exists( 'activate', $actions ) ){
unset( $actions['activate'] );
}
return $actions;
}
public function get_allowed_plugins_num(){
$plugins_per_level = $this->plugins_per_level();
return $plugins_per_level[ $this->blog_level ];
}
public function count_blog_active_plugins(){
$blog_plugins = count( $this->get_blog_plugins() );
//$network_plugins = count( $this->get_network_plugins() );
//return $blog_plugins + $network_plugins;
return $blog_plugins;
}
public function get_blog_level( $blog_id = null ){
global $psts;
if( $blog_id == null ){
global $blog_id;
}
return $psts->get_level( $blog_id );
}
public function get_blog_plugins(){
return get_option( 'active_plugins', array() );
}
public function get_network_plugins(){
return get_site_option('active_sitewide_plugins');
}
public static function get_levels(){
$levels = (array) get_site_option( 'psts_levels' );
if ( ! ( defined( 'PSTS_DONT_REVERSE_LEVELS' ) && PSTS_DONT_REVERSE_LEVELS ) ) {
$levels = array_reverse( $levels, true );
}
foreach ( $levels as $level_id => $level ) {
$is_visible = isset( $level['is_visible'] ) ? (bool) $level['is_visible'] : true;
if ( $is_visible ) {
continue;
}
unset( $levels[ $level_id ] );
}
return $levels;
}
public function is_plugins_page(){
if( ! is_admin() || ! class_exists( 'ProSites' ) ){
return false;
}
global $pagenow;
return $pagenow == 'plugins.php';
}
public function get_plugins(){
return get_plugins();
}
}
add_action( 'plugins_loaded', function(){
$GLOBALS['WPMUDEV_PS_Limit_Plugins'] = WPMUDEV_PS_Limit_Plugins::get_instance();
}, 10 );
}
@panoslyrakis
Copy link
Author

panoslyrakis commented Jun 30, 2017

First thing to set is the array with levels and number of plugins:

		```$plugins_per_level = array(
			0 => 1, //0 not a pro site, no level...
			1 => 2,
			2 => 4,
			3 => '-' //'-' has no limit
		);```

Currently it counts the network active plugins too. If you only need to count plugins per blog only comment/delete line

return $blog_plugins + $network_plugins;

and uncomment line

//return $blog_plugins;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment