Forked from strangerstudios/pmpro-limit-available-plugins-and-themes.php
Created
May 17, 2018 00:03
-
-
Save robertdevore/82d032ae57498de3f32123974f22381b to your computer and use it in GitHub Desktop.
Limit the available plugins and themes for networks sites in a WordPress multisite install using Paid Memberships Pro.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: PMPro Limit Available Plugins and Themes | |
Plugin URI: http://www.paidmembershipspro.com/wp/pmpro-limit-available-plugins/ | |
Description: Limits Which Plugins are Available for Network Sites | |
Version: .1 | |
Author: Stranger Studios | |
Author URI: http://www.strangerstudios.com | |
1. Place this file in wp-content/mu-plugins. | |
2. Update the pmpro_plugins_per_level and pmpro_themes_per_levels globals in the plugin. | |
3. Make sure that you have "Enable administration menus" checked in the Network Settings page of the network dashboard. | |
*/ | |
/* | |
This is the array to store which plugins are available for which level | |
Level 1 members have access to akisment and hello dolly. | |
Level 2 members have access to just hello dolly. | |
Make sure that the plugin entry in each array is the actual path and file name of the plugin on your server | |
*/ | |
global $pmpro_plugins_per_level; | |
$pmpro_plugins_per_level = array( | |
1 => array( | |
'akismet/akismet.php', | |
'hello.php' | |
), | |
2 => array( | |
'hello.php' | |
) | |
); | |
/* | |
This is the array to store which themes are available for which level | |
Level 1 members have access to twenetytwelve, twentyeleven and twentyten. | |
Level 2 members have access to twentytwelve and twentyten. | |
Make sure that the level entry in each array is the directory name of the theme | |
*/ | |
global $pmpro_themes_per_level; | |
$pmpro_themes_per_level = array( | |
1 => array( | |
'twentyeleven', | |
'twentytwelve' | |
), | |
2 => array( | |
'twentytwelve', | |
'twentyten' | |
) | |
); | |
/* | |
Limit the listed plugins is the admin | |
*/ | |
function pmpro_limit_available_plugins($plugins) | |
{ | |
//don't filter network admins | |
if(!current_user_can("manage_network_plugins")) | |
{ | |
global $pmpro_plugins_per_level; | |
$membership_id = plap_getMembershipId(); | |
//filter by membership level | |
if(!empty($pmpro_plugins_per_level[$membership_id])) | |
{ | |
$available_plugins = $pmpro_plugins_per_level[$membership_id]; | |
$newplugins = array(); | |
foreach($plugins as $plugin => $data) | |
{ | |
if(in_array($plugin, $available_plugins)) | |
$newplugins[$plugin] = $data; //add it | |
} | |
$plugins = $newplugins; | |
return $plugins; | |
} | |
else | |
{ | |
//plugins weren't specified for the users membership (or he has none) so return no plugins | |
return array(); | |
} | |
} | |
return $plugins; | |
} | |
add_filter("all_plugins", "pmpro_limit_available_plugins"); | |
/* | |
Limit the listed themes is the admin | |
*/ | |
function pmpro_limit_available_themes($themes) | |
{ | |
//don't filter network admins | |
if(!current_user_can("manage_network_themes")) | |
{ | |
global $pmpro_themes_per_level; | |
$membership_id = plap_getMembershipId(); | |
//filter by membership level | |
if(!empty($pmpro_themes_per_level[$membership_id])) | |
{ | |
$available_themes = $pmpro_themes_per_level[$membership_id]; | |
$newthemes = array(); | |
foreach($themes as $theme => $data) | |
{ | |
if(in_array($theme, $available_themes)) | |
$newthemes[$theme] = $data; //add it | |
} | |
$themes = $newthemes; | |
return $themes; | |
} | |
else | |
{ | |
//themes weren't specified for the users membership (or he has none) so return no themes | |
return array(); | |
} | |
} | |
return $themes; | |
} | |
add_filter("allowed_themes", "pmpro_limit_available_themes"); | |
/* | |
Get the membership id of the current user at the main site level | |
*/ | |
function plap_getMembershipId() | |
{ | |
global $wpdb, $current_user; | |
$prefix = str_replace($wpdb->blogid . "_", "", $wpdb->prefix); | |
$membership_id = $wpdb->get_var("SELECT membership_id FROM " . $prefix . "pmpro_memberships_users WHERE user_id = '" . $current_user->ID . "' AND status = 'active' ORDER BY id DESC"); | |
return $membership_id; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment