php 无法识别的自定义帖子类型的帖子配额。一些旧主题注册Pro Sites无法识别的帖子类型,这可以用于限制

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 无法识别的自定义帖子类型的帖子配额。一些旧主题注册Pro Sites无法识别的帖子类型,这可以用于限制相关的知识,希望对你有一定的参考价值。

<?php
/*
Plugin Name: Post Type Quota
Plugin URI: https://premium.wpmudev.org/
Description: Post type publishing quotas for Pro Sites
Author: Ashok & Lindeni @ WPMUDEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/


/*
*
* You just need to configure the $limits array. The elements are level ID - 1. 
* So, if your level ID 1, the index will be 0.
* Then for product index add the limit
*
*/
class ProSites_Limit_Slides{
	public $post_type;
	public $limits;

	function __construct(){
		$this->post_type = "slide"; //the name of the post type to be limited
		$this->limits =  array(
			'0' => array(
				$post_type => 5 //limit for level 1
			),
			'1' => array(
				$post_type => 5 //limit for level 2
			),
			'2' => array(
				$post_type => 5 //limit for level 3
			),
		);
		$this->hooks();
	}

	public function hooks(){
		add_action( 'admin_notices', array( &$this, 'message_notice' ) );
		add_filter( 'wp_insert_post_data', array( $this, 'check_pro_limit' ), 10, 2 );
		add_action( 'post_submitbox_misc_actions', array( $this, 'remove_publish_option' ) );
	}

	function is_per_level(){
		global $psts;

		$per_level = $psts->get_setting('per_level');
		return  ( $per_level == 1 ) ? true : false;
	}
	
	public function check_pro_limit( $data, $postarr ) {
		global $psts;

		if ( ! isset( $postarr['ID'] ) || $data['post_status'] != 'publish' ) {
			return $data;
		}

		$level = $psts->get_level();
		$limit = $this->limits[ $level ][ $this->post_type ];

		//Return if post does not exists, if there is no limit set for post type or if quota value is unlimited
		if ( ! ( $post = get_post( $postarr['ID'] ) ) || ! isset ( $limit ) || 'unlimited' == $limit ) {
			return $data;
		}
		
		if ( 0 >= $limit - wp_count_posts( $this->post_type )->publish ) {
			$data['post_status'] = $post->post_status != 'auto-draft' ? 'draft' : $post->post_status;
		}

		return $data;
	}

	

	public function message_notice() {		
		global $psts, $current_screen, $post_type, $blog_id;
		$blog_id = ( isset( $blog_id ) && $blog_id != 0 ) ? $blog_id : get_current_blog_id();


		if ( is_numeric( $this->limit ) && wp_count_posts( $post_type )->publish >= $this->limit ) {

			$levels = get_site_option( 'psts_levels' );

			if ( $level < count( $levels ) ){
				$level   = $this->is_per_level ? $psts->get_level( $blog_id ) : $psts->get_setting( 'pq_level' );
				if ( $this->is_per_level() ) $level += 1;
				$name    = $psts->get_level_setting( $level, 'name' );
				$message = "You have reached your publishing limits, please upgrade to LEVEL to publish more " . $post_type . " &raquo;";
				$notice  = str_replace( 'LEVEL', $psts->get_level_setting( $level, 'name' ), $message );
				echo '<div class="error"><p><a href="' . $psts->checkout_url( $blog_id ) . '">' . $notice . '</a></p></div>';

			}elseif ( count ( $levels ) == $level ){ //highest level gets special message if has limits

				$notice = "You have reached your publishing limits, no upgrades for this level. Contact Administrator. &raquo;";
				echo '<div class="error"><p>' . $notice . '</p></div>';
			}
		}
	}

	public function remove_publish_option() {
		global $psts, $typenow, $post_type;

		$level = $psts->get_level();
		$limit = $this->limits[ $level ][ $this->post_type ];

		/**
		 * Return if no limit is set for the post type or if its unlimited
		 */
		if ( ! isset( $limit) || 'unlimited' == $limit ) {
			return;
		}

		$exceeded = false;
		

		if ( is_numeric( $limit ) && wp_count_posts( $typenow )->publish >= $limit ) {
			$exceeded = true;
		}

		if ( $exceeded && get_post()->post_status != 'publish' ) {
			?>
			<style type="text/css">
				#publish {
					display: none;
				}

				#psts-upgrade {
					margin-top: 4px;
					display: block;
					text-align: center;
				}
			</style>

			<div class="misc-pub-section">
			<a id="psts-upgrade" class="button button-primary button-large" href="<?php echo $psts->checkout_url( get_current_blog_id() ); ?>"><?php _e( 'Upgrade Your Account', 'psts' ); ?></a>
			</div><?php
		}
	}
}

global $slidelimit;
$slidelimit = new ProSites_Limit_Slides();

以上是关于php 无法识别的自定义帖子类型的帖子配额。一些旧主题注册Pro Sites无法识别的帖子类型,这可以用于限制的主要内容,如果未能解决你的问题,请参考以下文章

从 Wordpress 取消注册自定义帖子类型

php 在自定义帖子类型存档和一些自定义分类存档上强制命令到`menu_order`

无法获取自定义分类存档页面以显示帖子

尝试将帖子写入 Firebase 实时数据库时遇到无法识别的选择器发送到实例错误”

如何在自定义帖子类型UI菜单图标区域添加字体真棒图标?

在 Wordpress 中使用自定义帖子类型时无法在循环中过滤类别