Can i set different levels of posts allowed for each prosite level purchased

I want to offer up to x posts /pages for level 1 and then up to y posts/pages for level 2 and up to z posts/pages with level 3

Can this be done with pro sites

The description on the plug in seems to imply this...
"Post/Page Quotas – Allows you to limit the number of post types that can be published for selected Pro Site levels."

But so far I only see how to set one limit, and then unlimited. Am i missing something?

  • PC

    Hiya,

    Greetings and thanks for posting on the forums.

    Currently the plugin only allows you to set one limit and then its unlimited.

    As the text suggests

    Select the minimum level required to remove quotas.

    So, if the user is at that minimum prosite level, the quotas are removed.

    So you are not missing anything and doing it right. I have moved this post to the Features and Feedback section so if other members are also looking for this functionality, they can vote on it to be included in future releases of the plugin.

    I hope that helps. Please feel free to ask if you have more questions on the same.

    Cheers
    PC

  • Andy42

    Ok, as a partial solution I am thinking I could edit the PHP to make unlimited have a limit... in other words, basic level you get 20 and unlimited means 200 products

    Can you ask a coder is this where I would change it in pro-sites.php?

    if ( isset($_POST['psts_extend']) ) {
    check_admin_referer('psts_extend'); //check nonce

    if ( isset($_POST['extend_permanent']) ) {
    $extend = 9999999999;
    } else {

  • Eugene

    I also would like (even really need) this feature. I vote FOR! :slight_smile:

    There are not many line of code in posting-quota.php - for a professional it should be easy to improve the feature. Could you ask the plugin developer to do this pls?

    If no...could you ask him to give me a hack pls? I am afraid that if I do this myself some how and get lot's of clients with these quotas, and then the feature will be improved officially - I will have to start from the very beginning due to different code (tables to store data, titles or so on). Help pls.

  • aristath

    Hello there @Eugene, I hope you're well today!

    Please avoid re-opening old threads like this one in the future... Instead you should create a new thread. This way we'll be able to better support you and it causes less confusion in the forums.
    To answer your question though, you'd have to custom-code this.
    I have written some example code for you:

    <?php
    
    function my_custom_write_filter( $allcaps, $caps, $args ) {
    
    	global $psts;
    
    	if ( is_pro_site( false, 1 ) ) {
    
    		if ( wp_count_posts( 'post' ) >= 10 ) {
    
    			$pt_obj = get_post_type_object( 'post' );
    			unset( $allcaps[$pt_obj->cap->publish_posts] );
    
    		}
    
    	}
    
    	return $allcaps;
    }
    
    function my_custom_replace_write_filter() {
    
    	$posting_quota = new ProSites_Module_PostingQuota();
    	remove_filter( 'user_has_cap', array( $posting_quota, 'write_filter' ), 10 );
    
    	add_filter( 'user_has_cap', 'my_custom_write_filter', 15, 3 );
    
    }
    add_action( 'user_has_cap', 'my_custom_replace_write_filter', 12 );

    You can tkae this and modify it to suit your needs, adding more conditions to the the if ( ! is_pro_site( false, 1 ) ) { line.

    I hope that helps!

    Cheers,
    Ari.

  • Eugene

    Oh, thank you very much!

    I will definitely look through your code but first one question pls: am I right thinking that ProSies doesn't keep anywhere the data how many posts do each user has - ProSites take this data from WP each time when it is needed, right?

    (I am asking this to understand and to be sure that when this feature will be added in ProSites in future - I can just replace my custom file via the new one and nothing become broken as it only asks WP but don't keep own data - all is correct?)

  • aristath

    am I right thinking that ProSies doesn't keep anywhere the data how many posts do each user has - ProSites take this data from WP each time when it is needed, right?

    Correct.
    Actually you can see that in my example code above...
    I see I failed to provide any explanations about that code though, so let me take it line-by line a little bit and explain everything:

    Line 7: if ( ! is_pro_site( false, 1 ) ) {
    Checks if the current site is on level 1. If yes, then it proceeds.

    Line 9: if ( wp_count_posts( 'post' ) >= 10 ) {
    Counts the number of posts in the current site and if they are more or equal to 10 then it proceeds.

    Lines 11-12:
    $pt_obj = get_post_type_object( 'post' );
    unset( $allcaps[$pt_obj->cap->publish_posts] );

    This is where the "magic" happens and the capability to post is removed from this user.

    Lines 22-30 simply remove the default behavior for the plugin and replace it with our custom one.

    So in conclusion, the above code does the following:
    IF the current site is on level 1 (line 7) AND the number of posts from the post type 'post' are greater or equal to 10 (line 9), THEN the user is no longer able to post any more posts from the post type 'post' (lines 11-12).

    You could expand it and say for example this:
    IF the site is on level 1 and the number of posts is >= 10 then disallow posting, OTHERWISE if the site is on level 2 and the number of posts is >= 20 then disallow posting, OTHERWISE if the site is on level 3 and the number of posts is >= 50 then disallow posting.
    This would look like this: (I'm using a slightly shorter method of writing here just to make this slightly more compact. If you read the code it makes the same sense. :slight_smile:

    <?php
    
    function my_custom_write_filter( $allcaps, $caps, $args ) {
    
    	global $psts;
    
    	if ( is_pro_site( false, 1 ) && wp_count_posts( 'post' ) >= 10 ) {
    
    		$pt_obj = get_post_type_object( 'post' );
    		unset( $allcaps[$pt_obj->cap->publish_posts] );
    
    	} elseif ( is_pro_site( false, 2 ) && wp_count_posts( 'post' ) >= 20 ) {
    
    		$pt_obj = get_post_type_object( 'post' );
    		unset( $allcaps[$pt_obj->cap->publish_posts] );
    
    	} elseif ( is_pro_site( false, 3 ) && wp_count_posts( 'post' ) >= 50 ) {
    
    		$pt_obj = get_post_type_object( 'post' );
    		unset( $allcaps[$pt_obj->cap->publish_posts] );
    
    	}
    
    	return $allcaps;
    }
    
    function my_custom_replace_write_filter() {
    
    	$posting_quota = new ProSites_Module_PostingQuota();
    	remove_filter( 'user_has_cap', array( $posting_quota, 'write_filter' ), 10 );
    
    	add_filter( 'user_has_cap', 'my_custom_write_filter', 15, 3 );
    
    }
    add_action( 'user_has_cap', 'my_custom_replace_write_filter', 12 );

    I hope that helps you understand a little the way this should work...

    Cheers,
    Ari.

  • Eugene

    You are the brilliant php teacher :slight_smile:

    I understand everything now. But just one moment:

    Where exactly I should put this code? in posting-quota.php?
    I would like to keep the opportunity to auto update the Pro Sites plugin (if it is possible) and think, that may be it is better to put your code in a separate plugin? (I have no experience in separate plugin developing)

    Any way, I am also afraid of default settings of Pro Sites (/wp-admin/network/admin.php?page=psts-settings) - there are settings for a level and phrases that should be shown to the used that get his limit. I would like to use them. Will there be a conflict with this modification or how?

    P.S.- your code above seems to be only for posts. I am going to add some lines/conditions there where "wp_count_posts( 'post' )" will be changed to "wp_count_posts( 'page' )" - hope this is a good way. For example:
    change this:
    ( is_pro_site( false, 3 ) && wp_count_posts( 'post' ) >= 50
    to this:
    emm.... just understand that there seems to be nothing about the phrases I mention above (from ProSites: "Choose the quota and message that is displayed on the add post screen for sites that have used up their quota. "LEVEL" will be replaced with the needed level name."). So, I don't know what to do, sorry :slight_smile:

  • Michael Bissett

    Hey @Eugene,

    Where exactly I should put this code?

    You can create a mu-plugin, and insert the code into that. It'll always be active, and you won't have to worry about it being overwritten. :slight_smile:

    You can learn how to create one here:

    http://www.newnine.com/learn/how-to-create-a-wordpress-mu-plugin

    Any way, I am also afraid of default settings of Pro Sites (/wp-admin/network/admin.php?page=psts-settings) - there are settings for a level and phrases that should be shown to the used that get his limit. I would like to use them. Will there be a conflict with this modification or how?

    The code that my colleague @aristath suggested looks to be overriding the settings inside of Pro Sites for this, you wouldn't be able to use that settings area to show a message for each level in the way you're wanting. That would require some extra customization.

    (you can look at 107 - 125 of pro-sites\pro-sites-files\modules\posting-quota.php for some inspiration on this)

    However, as you said that plugin development isn't something you've done a lot of, you may wish to post a job on our Jobs Board about this:

    https://premium.wpmudev.org/wordpress-development/

    Hope this helps! :slight_smile:

    Regards,
    Michael

  • Eugene

    Ok, I looked all the information you gave, thank you! And now I have only 2 questions:

    1) Should I switch the module off (called "Post/Page Quotas" in ProSites) as it is no longer needed?

    2) I am about to finish creating the mu-plugin - here is the code (based on the one above) which is for posts and for pages:

    // For Posts (this part is a copy of the code above)
    function my_custom_write_filter_pro_sites_1_posts( $allcaps, $caps, $args ) {
    
    	global $psts;
    
    	if ( is_pro_site( false, 1 ) && wp_count_posts( 'post' ) > 10 ) {
    
    		$pt_obj = get_post_type_object( 'post' );
    		unset( $allcaps[$pt_obj->cap->publish_posts] );
    
    	} elseif ( is_pro_site( false, 2 ) && wp_count_posts( 'post' ) > 20 ) {
    
    		$pt_obj = get_post_type_object( 'post' );
    		unset( $allcaps[$pt_obj->cap->publish_posts] );
    
    	} elseif ( is_pro_site( false, 3 ) && wp_count_posts( 'post' ) > 50 ) {
    
    		$pt_obj = get_post_type_object( 'post' );
    		unset( $allcaps[$pt_obj->cap->publish_posts] );
    
    	}
    
    	return $allcaps;
    }
    
    function my_custom_replace_write_filter_pro_sites_1_posts() {
    
    	$posting_quota = new ProSites_Module_PostingQuota();
    	remove_filter( 'user_has_cap', array( $posting_quota, 'write_filter' ), 10 );
    
    	add_filter( 'user_has_cap', 'my_custom_write_filter_pro_sites_1_posts', 15, 3 );
    
    }
    add_action( 'user_has_cap', 'my_custom_replace_write_filter_pro_sites_1_posts', 12 );
    
    // For Pages
    function my_custom_write_filter_pro_sites_1_pages( $allcaps, $caps, $args ) {
    
    	global $psts;
    
    	if ( is_pro_site( false, 1 ) && wp_count_posts( 'page' ) > 10 ) {
    
    		$pt_obj = get_post_type_object( 'page' );
    		unset( $allcaps[$pt_obj->cap->publish_page] );
    
    	} elseif ( is_pro_site( false, 2 ) && wp_count_posts( 'page' ) > 20 ) {
    
    		$pt_obj = get_post_type_object( 'page' );
    		unset( $allcaps[$pt_obj->cap->publish_page] );
    
    	} elseif ( is_pro_site( false, 3 ) && wp_count_posts( 'page' ) > 50 ) {
    
    		$pt_obj = get_post_type_object( 'page' );
    		unset( $allcaps[$pt_obj->cap->publish_page] );
    
    	}
    
    	return $allcaps;
    }
    
    function my_custom_replace_write_filter_pro_sites_1_pages() {
    
    	$posting_quota = new ProSites_Module_PostingQuota();
    	remove_filter( 'user_has_cap', array( $posting_quota, 'write_filter' ), 10 );
    
    	add_filter( 'user_has_cap', 'my_custom_write_filter_pro_sites_1_pages', 15, 3 );
    
    }
    add_action( 'user_has_cap', 'my_custom_replace_write_filter_pro_sites_1_pages', 12 );

    BUT I do not know how to cross it with the code you mentioned (lines "107 - 125") - could you help me with this pls and it will be fully done!

    function message() {
    		global $psts, $current_screen, $post_type, $blog_id;
    
        if ( is_pro_site(false, $psts->get_setting('pq_level', 1)) )
          return;
    
    		if ( in_array( $current_screen->id, array('edit-post', 'post', 'edit-page', 'page') ) ) {
    			$quota_settings = $psts->get_setting("pq_quotas");
    			if (is_array($quota_settings)) {
    				if ( isset($quota_settings[$post_type]) ) {
    					if ( is_numeric(@$quota_settings[$post_type]['quota']) && wp_count_posts($post_type)->publish >= @$quota_settings[$post_type]['quota'] ) {
    						$notice = str_replace( 'LEVEL', $psts->get_level_setting($psts->get_setting('pq_level', 1), 'name'), @$quota_settings[$post_type]['message'] );
    						echo '<div class="error"><p><a href="'.$psts->checkout_url($blog_id).'">' . $notice . '</a></p></div>';
    					}
    				}
    			}
    		}
    	}
    }
  • aristath

    Hello again @Eugene,

    1) Should I switch the module off (called "Post/Page Quotas" in ProSites) as it is no longer needed?

    No, you should leave it on so that it gets loaded.
    If you disable it, your custom code will also stop functioning. We're overriding the default values of that class but we still need it.

    I further optimized and kinda simplified the above code, combining some functions you had there and writing some other "helper functions that makes things a little easier and more "modular" and a lot more elegant.

    Using the below code, you can specify your limits in a simple array (see the my_custom_define_qota_limits function) and then that is parsed to the other functions. Here's the resulting code:

    <?php
    
    /**
     * Define an array with our limits for post quotas.
     */
    function my_custom_define_qota_limits() {
    
    	$limits = array(
    		array( 'level' => 1, 'posts' => 10, 'pages' => 10 ),
    		array( 'level' => 2, 'posts' => 20, 'pages' => 20 ),
    		array( 'level' => 1, 'posts' => 50, 'pages' => 50 ),
    	);
    
    	return $limits;
    }
    
    /**
     * Helper function to remove posting capabilities from a post type
     */
    function my_custom_remove_posting_cap( $allcaps, $caps, $args, $post_type = 'post') {
    
    	if ( $post_type = 'post' ) {
    
    		$pt_obj = get_post_type_object( 'post' );
    		unset( $allcaps[$pt_obj->cap->publish_posts] );
    
    	} else if ( $post_type = 'page' ) {
    
    		$pt_obj = get_post_type_object( 'page' );
    		unset( $allcaps[$pt_obj->cap->publish_page] );
    
    	}
    
    	return $allcaps;
    
    }
    
    /**
     * define our limits for pro-sites quotas
     */
    function my_custom_write_filter_pro_sites( $allcaps, $caps, $args ) {
    
    	global $psts;
    
    	$limits = my_custom_define_qota_limits();
    
    	foreach ( $limits as $limit ) {
    
    		if ( is_pro_site( false, $limit['level'] ) ) {
    
    			// Posts
    			if ( wp_count_posts( 'post' ) > $limit['posts'] ) {
    				$allcaps = my_custom_remove_posting_cap( $allcaps, $caps, $args, 'post' );
    			}
    
    			// Pages
    			if ( wp_count_posts( 'page' ) > $limit['pages'] ) {
    				$allcaps = my_custom_remove_posting_cap( $allcaps, $caps, $args, 'page' );
    			}
    
    		}
    
    	}
    
    	return $allcaps;
    
    }
    
    /**
     * Replace the default behavior with our custom one
     */
    function my_custom_replace_write_filter_pro_sites() {
    
    	$posting_quota = new ProSites_Module_PostingQuota();
    
    	remove_filter( 'user_has_cap', array( $posting_quota, 'write_filter' ), 10 );
    	add_filter( 'user_has_cap', 'my_custom_write_filter_pro_sites', 15, 3 );
    
    }
    add_action( 'user_has_cap', 'my_custom_replace_write_filter_pro_sites', 12 );

    As for the "message" function, you will have to do something similar for that too.

    function my_custom_pro_sites_quota_message() {
    
    	global $psts, $current_screen, $post_type, $blog_id;
    
    	$limits = my_custom_define_qota_limits();
    
    	foreach ( $limits as $limit ) {
    
    		if ( is_pro_site( false, $limit['level'] ) ) {
    
    			$quota_settings = $psts->get_setting("pq_quotas");
    
    			if ( in_array( $current_screen->id, array('edit-post', 'post', 'edit-page', 'page') ) ) {
    
    				if ( wp_count_posts( $post_type )->publish >= $limit[$post_type] ) {
    
    					$notice = str_replace( 'LEVEL', $psts->get_level_setting($psts->get_setting('pq_level', 1), 'name'), @$quota_settings[$post_type]['message'] );
    					echo '<div class="error"><p><a href="' . $psts->checkout_url($blog_id) . '">' . $notice . '</a></p></div>';
    
    				}
    
    			}
    
    		}
    
    	}
    
    }
    
    /**
     * Replace the default behavior with our custom one
     */
    function my_custom_replace_quota_message_pro_sites() {
    
    	$posting_quota = new ProSites_Module_PostingQuota();
    
    	remove_action( 'admin_notices', array( $posting_quota, 'message') );
    	add_action( 'admin_notices', 'my_custom_pro_sites_quota_message' );
    
    }
    add_action( 'admin_notices', 'my_custom_replace_quota_message_pro_sites', 12 );

    In that code we're also taking advantage of the above "modular" structure and so we avoid code duplication.

    Once again, I can't stress enough that the above code is untested and I'm just writing it 'cause it makes sense to me... it may crash your site (I might for example have missed closing a parenthesis, made a spelling mistake or add a semicolon) so you might want to do your tests on a non-live site.

    I hope that helps!

    Cheers,
    Ari.

  • Eugene

    Thank you!

    But you are right - there must be a spelling mistake or smth like that :slight_smile: Hope you can fix it.

    When I go to /wp-admin/ of a sub site to see if there is a message about getting quota limit - I get only this:

    Fatal error: Class 'ProSites_Module_PostingQuota' not found in /var/www/mysite.ru/wp-content/mu-plugins/custom-define-qota-limits-pro-sites.php on line 83

    The 83 line is:
    $posting_quota = new ProSites_Module_PostingQuota();

    P.S.- about the message which client will see - I still will be able to change it from ProSites settings page? Or how?

  • aristath

    Hello again @Eugene!

    I think these little tweaks will take care of your issues...
    I'm posting the complete file here:

    <?php
    
    /**
     * Define an array with our limits for post quotas.
     */
    function my_custom_define_qota_limits() {
    
    	$limits = array(
    		array( 'level' => 1, 'posts' => 10, 'pages' => 10 ),
    		array( 'level' => 2, 'posts' => 20, 'pages' => 20 ),
    		array( 'level' => 1, 'posts' => 50, 'pages' => 50 ),
    	);
    
    	return $limits;
    }
    
    /**
     * Helper function to remove posting capabilities from a post type
     */
    function my_custom_remove_posting_cap( $allcaps, $caps, $args, $post_type = 'post') {
    
    	if ( $post_type = 'post' ) {
    
    		$pt_obj = get_post_type_object( 'post' );
    		unset( $allcaps[$pt_obj->cap->publish_posts] );
    
    	} else if ( $post_type = 'page' ) {
    
    		$pt_obj = get_post_type_object( 'page' );
    		unset( $allcaps[$pt_obj->cap->publish_page] );
    
    	}
    
    	return $allcaps;
    
    }
    
    /**
     * define our limits for pro-sites quotas
     */
    function my_custom_write_filter_pro_sites( $allcaps, $caps, $args ) {
    
    	global $psts;
    
    	$limits = my_custom_define_qota_limits();
    
    	foreach ( $limits as $limit ) {
    
    		if ( is_pro_site( false, $limit['level'] ) ) {
    
    			// Posts
    			if ( wp_count_posts( 'post' ) > $limit['posts'] ) {
    				$allcaps = my_custom_remove_posting_cap( $allcaps, $caps, $args, 'post' );
    			}
    
    			// Pages
    			if ( wp_count_posts( 'page' ) > $limit['pages'] ) {
    				$allcaps = my_custom_remove_posting_cap( $allcaps, $caps, $args, 'page' );
    			}
    
    		}
    
    	}
    
    	return $allcaps;
    
    }
    add_filter( 'user_has_cap', 'my_custom_write_filter_pro_sites', 15, 3 );
    
    /**
     * Remove the default behavior
     */
    function my_custom_replace_write_filter_pro_sites() {
    
    	if ( class_exists( 'ProSites_Module_PostingQuota' ) {
    
    		$posting_quota = new ProSites_Module_PostingQuota();
    		remove_filter( 'user_has_cap', array( $posting_quota, 'write_filter' ), 10 );
    
    	}
    
    }
    add_action( 'wp', 'my_custom_replace_write_filter_pro_sites', 12 );
    
    /**
     * Our custom admin messages
     */
    function my_custom_pro_sites_quota_message() {
    
    	global $psts, $current_screen, $post_type, $blog_id;
    
    	$limits = my_custom_define_qota_limits();
    
    	foreach ( $limits as $limit ) {
    
    		if ( is_pro_site( false, $limit['level'] ) ) {
    
    			$quota_settings = $psts->get_setting("pq_quotas");
    
    			if ( in_array( $current_screen->id, array('edit-post', 'post', 'edit-page', 'page') ) ) {
    
    				if ( wp_count_posts( $post_type )->publish >= $limit[$post_type] ) {
    
    					$notice = str_replace( 'LEVEL', $psts->get_level_setting($psts->get_setting('pq_level', 1), 'name'), @$quota_settings[$post_type]['message'] );
    					echo '<div class="error"><p><a href="' . $psts->checkout_url($blog_id) . '">' . $notice . '</a></p></div>';
    
    				}
    
    			}
    
    		}
    
    	}
    
    }
    add_action( 'admin_notices', 'my_custom_pro_sites_quota_message' );
    
    /**
     * Remove the default behavior
     */
    function my_custom_replace_quota_message_pro_sites() {
    
    	if ( class_exists( 'ProSites_Module_PostingQuota' ) {
    
    		$posting_quota = new ProSites_Module_PostingQuota();
    		remove_action( 'admin_notices', array( $posting_quota, 'message') );
    
    	}
    
    }
    add_action( 'wp', 'my_custom_replace_quota_message_pro_sites', 12 );

    Let me know if that works now!

    Cheers,
    Ari.

  • Eugene

    Hello Aristath!

    Thank you for your help. But I just tried the new version and it gives the error even at the main page of the main site:

    Parse error: syntax error, unexpected '{' in /var/www/mysite.ru/wp-content/mu-plugins/custom-define-qota-limits-pro-sites.php on line 84

    Line 84:
    if ( class_exists( 'ProSites_Module_PostingQuota' ) {

    Could you pls test it at your test WP MU installation to see the problem by your own pls - I think this could help debugging.

    P.S.- what about the "message" which client will see - I still will be able to change it from ProSites settings page? or how?

  • Eugene

    So, my results:

    After adding ")" in two places we now son't have fatal error - it's good :slight_smile:

    But it seems to be working incorrectly - now I have a sub-site (for tests) with just 1 post (1 published, 0 drafts, 0 in trash). Level 1 = trial (free level). But I sett it's (the sub-site) level at 2 (Bronze). In the custom plugin I have:

    $limits = array(
    		array( 'level' => 1, 'posts' => 10, 'pages' => 10 ),
    		array( 'level' => 2, 'posts' => 20, 'pages' => 20 ),
    		array( 'level' => 3, 'posts' => 50, 'pages' => 50 ),
    		array( 'level' => 4, 'posts' => 100, 'pages' => 100 ),
    	);

    But as Super Admin I see the message (that I got the limit for posts) and do need the level I set in ProSites (even if I set there level 1 or level 3)...

    Also I see two same messages, not one...

    So, the logic of the plugin/code is incorrect I think.
    Could you pls test it at your test installation? :slight_smile:

  • Ash

    Hello @Eugene

    Would you please try the following code as mu-plugins to check if it works?

    <?php
    $limits = array(
    		'0' => array(
    				'posts' => 5, 'pages' => 10
    			),
    		'1' => array(
    				'posts' => 6, 'pages' => 30
    			),
    		'2' => array(
    				'posts' => 25, 'pages' => 30
    			),
    		);
    
    add_action( 'admin_head', 'check_pro_limit' );
    function check_pro_limit( $post_id ) {
    	$id = get_current_blog_id();
    	global $psts, $limits, $post;
    	if( $id != 1 ){
    		$level = $psts->get_level($id);
    		$screen = get_current_screen();
    		switch( $screen->post_type ){
    
    			case 'post':
    				$published_posts = wp_count_posts( 'post' )->publish;
    				if( $published_posts >= $limits[$level - 1]['posts'] ){
    					wp_die( 'Please upgrade the level to add more posts.' );
    				}
    				break;
    
    			case 'page':
    				$published_posts = wp_count_posts( 'page' )->publish;
    				if( $published_posts >= $limits[$level - 1]['pages'] ){
    					wp_die( 'Please upgrade the level to add more pages.' );
    				}
    				break;
    
    		}
    
    	}
    }

    Note that, in $limits array 0 it for level 1, 1 is for level 2, 2 is for level 3 and so on...

    Hope it helps :slight_smile: Please feel free to ask more question if you have.

    Cheers
    Ash

  • Eugene

    Thank you!

    Just tried - seems that it works :slight_smile:

    But it doesn't use the message feature of ProSites - I mean that there is a message shown by your variant but it blocks the whole admin panel at the edit page and it blocks even the possibility to change or delete a post/page already published.

    So, I would like to ask you to improve your code to include the ProSites message functionality.

    P.S.- this seems to be the thing which should be included in the ProSites plugin I think, but first of all I would like to get it at least in this form. :slight_smile:

    Regards, Eugene.

  • Ash

    Please try this code:

    $limits = array(
    		'0' => array(
    				'posts' => 5, 'pages' => 10
    			),
    		'1' => array(
    				'posts' => 6, 'pages' => 30
    			),
    		'2' => array(
    				'posts' => 25, 'pages' => 30
    			),
    		);
    
    add_action( 'save_post', 'check_pro_limit' );
    function check_pro_limit( $post_id ) {
    	$id = get_current_blog_id();
    	global $psts, $limits, $post;
    	if( $id != 1 ){
    		$level = $psts->get_level($id);
    		$screen = get_current_screen();
    		switch( $screen->post_type ){
    
    			case 'post':
    				$published_posts = wp_count_posts( 'post' )->publish;
    				if( $published_posts >= $limits[$level - 1]['posts'] ){
    					wp_redirect( admin_url() . '?quota=post' );
    				}
    				break;
    
    			case 'page':
    				$published_posts = wp_count_posts( 'page' )->publish;
    				if( $published_posts >= $limits[$level - 1]['pages'] ){
    					wp_redirect( admin_url() . '?quota=page' );
    				}
    				break;
    
    		}
    
    	}
    }
    
    add_action( 'admin_notices', 'quota_admin_notice' );
    function quota_admin_notice() {
    	if( isset( $_REQUEST['quota'] ) ){
    		switch( $_REQUEST['quota'] ) {
    			case 'post':
    				?>
    				<div class="error">
    					<p>Please upgrade to publish more posts.</p>
    				</div>
    				<?php
    				break;
    
    			case 'page':
    				case 'posts':
    				?>
    				<div class="error">
    					<p>Please upgrade to publish more pages.</p>
    				</div>
    				<?php
    				break;
    		}
    	}
    }

    Hope it helps :slight_smile: Please feel free to ask more question if you have.

    Cheers
    Ash

  • Eugene

    Hi Ash!

    Thank you for your answer!

    But... it seems to me that you are testing on me - not at your test installation. :slight_frown:
    There is no any message - only inappropriate behavior when SuperAdmin press the button to create new post - he just redirects to the dashboard, that's all.... why SuperAdmin? why no message? why dashboard?

    I do need absolutely the same functional as the ProSites plugin do. Could you guys help me pls? This is your plugin - I hope you can :slight_smile:
    May be you should better ask the ProSites developer to take a look at this issue with his experience?

  • Ash

    Hello @Eugene

    @aristath is right, you need to remove line 56, and it will show as a notice, screenshot attached.

    What do you mean by "messages from ProSites"? Would you please post a screenshot of such message? I will try my best to provide solution for this.

    Corrected code:

    $limits = array(
    		'0' => array(
    				'posts' => 5, 'pages' => 10
    			),
    		'1' => array(
    				'posts' => 6, 'pages' => 30
    			),
    		'2' => array(
    				'posts' => 25, 'pages' => 30
    			),
    		);
    
    add_action( 'save_post', 'check_pro_limit' );
    function check_pro_limit( $post_id ) {
    	$id = get_current_blog_id();
    	global $psts, $limits, $post;
    	if( $id != 1 ){
    		$level = $psts->get_level($id);
    		$screen = get_current_screen();
    		switch( $screen->post_type ){
    
    			case 'post':
    				$published_posts = wp_count_posts( 'post' )->publish;
    				if( $published_posts >= $limits[$level - 1]['posts'] ){
    					wp_redirect( admin_url() . '?quota=post' );
    				}
    				break;
    
    			case 'page':
    				$published_posts = wp_count_posts( 'page' )->publish;
    				if( $published_posts >= $limits[$level - 1]['pages'] ){
    					wp_redirect( admin_url() . '?quota=page' );
    				}
    				break;
    
    		}
    
    	}
    }
    
    add_action( 'admin_notices', 'quota_admin_notice' );
    function quota_admin_notice() {
    	if( isset( $_REQUEST['quota'] ) ){
    		switch( $_REQUEST['quota'] ) {
    			case 'post':
    				?>
    				<div class=&quot;error&quot;>
    					<p>Please upgrade to publish more posts.</p>
    				</div>
    				<?php
    				break;
    
    			case 'page':
    				?>
    				<div class=&quot;error&quot;>
    					<p>Please upgrade to publish more pages.</p>
    				</div>
    				<?php
    				break;
    		}
    	}
    }

    Cheers
    Ash

  • Eugene

    Hello,

    I just tried new version and it works the same way...

    Could you pls try the ProSites post quota limitation functionality at your sites and see how exactly it works - this will show you that there is no any redirection to dashboard, it will show you the message and ect...

    I wish I could describe everything but I do really need exactly what and how ProSites does but for several levels (not just one). :slight_smile:

    Regards, Eugene.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.