Loading ProSites Early (During MU Plugin Stage)

I am developing a custom must-use plugin and naturally, these plugins load before the normal activated plugins. I was wondering if I could just include the pro-sites.php file when my plugin begins to run so I have access to is_pro_site()? It seems to work ok but not sure if there is anything else I should be aware of in doing this.

  • Vinod Dalvi

    Hi @adam_fisher,

    I hope you are well today and thank you for your question.

    If it's working well for you then it's ok to use it, alternatively instead of including the hole pro-sites.php file you can try using the following custom function custom_is_pro_site in your custom mu plugin which is a copy of the function is_pro_site.

    function custom_is_pro_site($blog_id = false, $level = false) {
    		global $wpdb, $current_site, $psts;
    
    		if ( !$blog_id ) {
    			$blog_id = $wpdb->blogid;
    		}
        $blog_id = intval($blog_id);
    
    		// Allow plugins to short-circuit
    		$pro = apply_filters( 'is_pro_site', null, $blog_id );
    		if ( !is_null($pro) )
    			return $pro;
    
    		//check cache first
    		if ( $level ) { //level is passed, check level
    			if ($level == 0) {
    				return true;
    			} else if ( isset( $psts->pro_sites[$blog_id][$level] ) && is_bool( $psts->pro_sites[$blog_id][$level] ) ) {
    				return $psts->pro_sites[$blog_id][$level];
    			}
    		} else { //any level will do
          if ( isset( $psts->pro_sites[$blog_id] ) && is_array( $psts->pro_sites[$blog_id] ) ) {
    				foreach ($psts->pro_sites[$blog_id] as $key => $value) {
    					if ($value) return true;
    				}
    					}
    				}
    
    		//check if main site
    		if ( is_main_site($blog_id) ) {
    			return true;
    	  } else { //finally go to DB
    			$now = strtotime('-3 hours');	//pad expiration by 3 hours just in case webhooks are delayed, etc
       		$data = $wpdb->get_row( $wpdb->prepare( "SELECT expire, level FROM {$wpdb->base_prefix}pro_sites WHERE blog_ID = %d", $blog_id ) );
    			if (is_object($data)) {
    				if ($level) {
    					if ( $data->expire && $data->expire > $now && $level <= $data->level ) {
    	        	for ($i = 1; $i <= $data->level; $i++)
    			        $psts->pro_sites[$blog_id][$i] = true; //update cache
    						return true;
    					} else {
    					  $levels = (array)get_site_option('psts_levels');
    	        	for ($i = $level; $i <= count($levels); $i++)
    			        $psts->pro_sites[$blog_id][$i] = false; //update cache
    						return false;
    					}
    				} else { //any level will do
    	        if ( $data->expire && $data->expire > $now ) {
    	        	for ($i = 1; $i <= $data->level; $i++)
    			        $psts->pro_sites[$blog_id][$i] = true; //update cache
    						return true;
    					} else {
    	        	for ($i = 1; $i <= $data->level; $i++)
    			        $psts->pro_sites[$blog_id][$i] = false; //update cache
    						return false;
    					}
    				}
    			} else {
    				if ($wpdb->result) { //only cache if there was not a db error
    					for ($i = 1; $i <= $level; $i++)
    						$psts->pro_sites[$blog_id][$i] = false; //update cache
    				}
    				return false;
    			}
    		}
    	}

    Best Regards,
    Vinod Dalvi

  • Jack Kitterhing

    Hi there @adam_fisher,

    Hope you're well today and thanks for your question! :slight_smile:

    I was taking a look around and found this http://wptavern.com/configure-when-wordpress-plugins-load-with-plugin-organizer It seems to work quite well.

    You can selectively choose when the plugins load, so you could change your plugin to a normal plugin, load Pro sites first, then your plugin, if that would work for you?

    Thanks!

    Kind Regards
    Jack.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.