Ends in
00Days
00Hours
00Mins
00Secs
Save up to 50%

PrivateContent Plugin

API Documentation

Restrictions Class

Introduction

#

Defined in classes/restrictions_wizard.php

PHP class managing posts and terms restrictions: the restrictions wizard, their storage and the checks against the current user.

It is reachable through the $pc_restr_wizard PHP global, available from plugins_loaded (priority 100).

Restrictions are inherited: a post is affected also by its parents restrictions and by the ones of its terms (and their parents). Use get_entity_full_restr() to get the complete tree, then user_passes_restr() to check it.

Example

// can the current user see post #10 contents?
global $pc_restr_wizard;

$restr = $pc_restr_wizard->get_entity_full_restr('post', 10);
$can_see = (empty($restr['cont_hide']) || $pc_restr_wizard->user_passes_restr($restr['cont_hide']) === 1);

Restrictions structure

#

Restrictions of a single post or term are stored in an associative array. The structure can be extended through pc_restr_wizard_structure.

KeyDescription
cont_hide_allow / cont_hide_blockcontents hiding system: allowed and blocked users
comm_hide_allow / comm_hide_blockcomments hiding system: allowed and blocked users
redirect_allow / redirect_blockredirect system: allowed and blocked users
block_woo_sellWooCommerce products: users not allowed to purchase
lb_on_openlightbox shown on page opening: lightbox instance ID, empty or inherit

Allowed and blocked users are arrays containing user category IDs, all (any logged user), unlogged or custom values added by add-ons through pc_user_cat_dd_opts.

get_post_restr()

method#

Gets restrictions set for a post (not considering inherited ones).

Usage

get_post_restr($post_id = false)

Parameters

ParameterDescription
(int|false) $post_idpost ID. False returns the empty structure

Return values

TypeDescription
(array)restrictions structure

get_term_restr()

method#

Gets restrictions set for a term (not considering inherited ones).

Usage

get_term_restr($term_id = false)

Parameters

ParameterDescription
(int|false) $term_idterm ID. False returns the empty structure

Return values

TypeDescription
(array)restrictions structure

get_entity_full_restr()

method#

Gets the complete restrictions tree of a post or term: its own restrictions plus the inherited ones. Results are cached for the current request and can be altered through pc_entity_full_restr.

Usage

get_entity_full_restr($subj_type, $subj_id, $term_data = false)

Parameters

ParameterDescription
(string) $subj_typepost or term
(int) $subj_idpost or term ID
(object|false) $term_dataterm object (got through get_term_by()), to manage term parents chaining

Return values

TypeDescription
(array)restrictions tree (empty array if there are no restrictions) - check the example below

Example

array(
	'cont_hide' => array(
		'self' 		=> array('allow' => array(2, 3), 'block' => array(5)), // own restrictions
		'post_12'	=> array('allow' => array('all')), // inherited from parent post #12
		'term_7'	=> array('allow' => array(3)), // inherited from term #7
	),
	'comm_hide' => array( ... ),
	'redirect' 	=> array( ... ),

	'block_woo_sell' => array('self' => array(4)),
	'lb_on_open' 	 => array('self' => 'inherit', 'term_7' => 125),
)

user_passes_restr()

method#

Checks the current user against a restrictions set (cont_hide, comm_hide or redirect branch of the full restrictions tree), using pc_user_check() for each level. Restrictions blocking the user are stored in the public $last_matched_restr property.

Usage

user_passes_restr($restr)

Parameters

ParameterDescription
(array) $restrrestrictions set: array('self' => array('allow' => .., 'block' => ..), 'post_ID' => .., 'term_ID' => ..)

Return values

TypeDescription
(int) 1user can access
(int) 2user is logged but doesn't have the right permissions
(false)user is not logged

restr_arr_to_string()

method#

Converts a restriction values array into a string of human readable names.

Usage

restr_arr_to_string($restrictions, $join_with = ', ')

Parameters

ParameterDescription
(array|string) $restrictionsrestriction values array or comma-separated string
(string) $join_withstring joining names

Return values

TypeDescription
(string)restriction names (eg. "Any logged user, Premium")

Let's get in touch!

Features list