Created
November 22, 2015 11:44
-
-
Save sjaved87/f434fdae300091cc3bd1 to your computer and use it in GitHub Desktop.
Membership 2 : Its a custom add-on to limit the number of blog a member can create within a membersihp. Dependent on membership attributes add-on.
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: Membership 2 Site Limitation | |
* Version: 1.0 | |
* Description: Limit number of blogs a member can create on multisite. | |
* Author: Sajid Javed From Incsub | |
* Author URI: http://premium.wpmudev.org/ | |
*/ | |
class MS_Custom_Addon_Sitelimit { | |
/** | |
* A unique Add-on ID, prefix with "custom_" to avoid collissions | |
*/ | |
const ID = 'custom_addon_sitelimit'; | |
/** | |
* This is the slug of the custom attribtue which defines the site-limit. | |
*/ | |
const ATTR_SLUG = 'max-sites'; | |
/** | |
* This is the slug of the custom attribtue message to display when member reached the limit | |
*/ | |
const MAX_LIMIT_REACH_MESSAGE = 'max-limit-message'; | |
static public function init() { | |
// Those actions are only called by the M2 plugin, so we do not check | |
// if M2 is active or not: | |
// If M2 is not active, then those actions are never triggered. | |
//add_action( 'ms_model_addon_initialize', array( __CLASS__, 'sl_init' ) ); | |
if( class_exists( 'MS_Plugin' ) ) | |
add_action( 'init', array( __CLASS__, 'sl_init' ) ); | |
add_filter( 'ms_model_addon_register', array( __CLASS__, 'register' ) ); | |
} | |
/** | |
* Checks if the current Add-on is enabled | |
* | |
* @return bool | |
*/ | |
static public function is_active() { | |
return MS_Model_Addon::is_enabled( self::ID ); | |
} | |
/** | |
* Initializes the Add-on. ALWAYS EXECUTED, EVEN WHEN DISABLED. | |
*/ | |
public function sl_init() { | |
if ( self::is_active() ) { | |
if ( ! MS_Model_Addon::is_enabled( MS_Addon_Attributes::ID ) ) { | |
echo 'This Add-on requires the Membership Attributes Add-on'; | |
return; | |
} | |
global $pagenow; | |
if($pagenow != 'wp-signup.php' and !isset($_GET['action']) and $_GET['action'] != 'new_blog'){ | |
return; | |
} | |
$api = ms_api(); | |
$real_limit = 0; | |
// Find the max limit for current user. | |
$member = $api->get_current_member(); | |
$user_id = $member->id; | |
if($member->subscriptions){ | |
foreach ( $member->subscriptions as $subscription ) { | |
// Read the attribute value from a membership. | |
$cur_limit = ms_membership_attr( self::ATTR_SLUG, $subscription->membership_id ); | |
$max_limit_message = ms_membership_attr( self::MAX_LIMIT_REACH_MESSAGE, $subscription->membership_id ); | |
// Use the highest limit that the user has access to. | |
$cur_limit = intval( $cur_limit ); | |
if ( $cur_limit > $real_limit ) { | |
$real_limit = $cur_limit; | |
} | |
} | |
} | |
$user_blogs = get_blogs_of_user( $user_id ); | |
$blog_count = 0; | |
if($user_blogs){ | |
foreach ($user_blogs as $blog){ | |
$blog_admins = self::site_admins($blog->userblog_id); | |
if( in_array($user_id, $blog_admins ) ) | |
$blog_count++; | |
} | |
} | |
if( $blog_count >= $real_limit and ( !empty($real_limit) || $real_limit != 0) ){ | |
if(empty($max_limit_message)) | |
$max_limit_message = 'You reached the maximum blog limit. Please upgrade to create more.'; | |
wp_die( $max_limit_message ); | |
} | |
// Now you have the $real_limit, rest is up to you ;) | |
// Add your custom hooks and add-on logic here. | |
} | |
} | |
/** | |
* Get blog admins | |
* param: $blog_id | |
* return: array of admin ids | |
*/ | |
static public function site_admins($blog_id) { | |
$site_admins = ''; | |
switch_to_blog( $blog_id ); | |
$users_query = new WP_User_Query( array( | |
'role' => 'administrator', | |
'orderby' => 'display_name' | |
) ); | |
$results = $users_query->get_results(); | |
foreach($results as $user) | |
{ | |
$site_admins[] = $user->ID ; | |
} | |
restore_current_blog(); | |
return $site_admins; | |
} | |
/** | |
* Make the Add-On appear in the Add-on list. | |
* | |
* @param array $list The Add-Ons list. | |
* @return array The updated Add-Ons list. | |
*/ | |
public function register( $list ) { | |
$list[ self::ID ] = (object) array( | |
'name' => __( 'Site Limitation', 'text-domain' ), | |
'description' => __( 'Custom Addon description. Required Membership Attributes', 'text-domain' ), | |
); | |
return $list; | |
} | |
}; // End of the custom Add-on class. | |
add_action( 'plugins_loaded', array( 'MS_Custom_Addon_Sitelimit', 'init' ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment