PrivateContent Plugin
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.
| Key | Description |
|---|---|
cont_hide_allow / cont_hide_block | contents hiding system: allowed and blocked users |
comm_hide_allow / comm_hide_block | comments hiding system: allowed and blocked users |
redirect_allow / redirect_block | redirect system: allowed and blocked users |
block_woo_sell | WooCommerce products: users not allowed to purchase |
lb_on_open | lightbox 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
| Parameter | Description |
|---|---|
(int|false) $post_id | post ID. False returns the empty structure |
Return values
| Type | Description |
|---|---|
(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
| Parameter | Description |
|---|---|
(int|false) $term_id | term ID. False returns the empty structure |
Return values
| Type | Description |
|---|---|
(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
| Parameter | Description |
|---|---|
(string) $subj_type | post or term |
(int) $subj_id | post or term ID |
(object|false) $term_data | term object (got through get_term_by()), to manage term parents chaining |
Return values
| Type | Description |
|---|---|
(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
| Parameter | Description |
|---|---|
(array) $restr | restrictions set: array('self' => array('allow' => .., 'block' => ..), 'post_ID' => .., 'term_ID' => ..) |
Return values
| Type | Description |
|---|---|
(int) 1 | user can access |
(int) 2 | user 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
| Parameter | Description |
|---|---|
(array|string) $restrictions | restriction values array or comma-separated string |
(string) $join_with | string joining names |
Return values
| Type | Description |
|---|---|
(string) | restriction names (eg. "Any logged user, Premium") |