PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.0.12
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.0.12
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / inc / functions / restrictions.php

restrictions.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More 2.0.12, at inc/functions/restrictions.php

146 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Restriction utility & helper functions.
4 *
5 * @package ContentControl
6 * @subpackage Functions
7 * @copyright (c) 2023 Code Atlantic LLC
8 */
9
10 namespace ContentControl;
11
12 defined( 'ABSPATH' ) || exit;
13
14 use function ContentControl\plugin;
15
16 /**
17 * Get restriction, by ID, slug or object.
18 *
19 * @param int|string|\ContentControl\Models\Restriction $restriction Restriction ID, slug or object.
20 *
21 * @return \ContentControl\Models\Restriction|null
22 */
23 function get_restriction( $restriction ) {
24 return plugin( 'restrictions' )->get_restriction( $restriction );
25 }
26
27 /**
28 * Check if admins are excluded from restrictions.
29 *
30 * @return bool True if admins are excluded, false if not.
31 */
32 function admins_are_excluded() {
33 return get_data_version( 'settings' ) > 1 && plugin()->get_option( 'excludeAdmins' );
34 }
35
36 /**
37 * Current user is excluded from restrictions.
38 *
39 * @return bool True if user is excluded, false if not.
40 */
41 function user_is_excluded() {
42 return admins_are_excluded() && \current_user_can( plugin()->get_permission( 'manage_settings' ) );
43 }
44
45 /**
46 * Check if user meets requirements.
47 *
48 * @param string $user_status logged_in or logged_out.
49 * @param string[]|array<string,string>|string $user_roles array of roles to check.
50 * @param string $role_match any|match|exclude.
51 *
52 * @return bool True if user meets requirements, false if not.
53 */
54 function user_meets_requirements( $user_status, $user_roles = [], $role_match = 'match' ) {
55 if ( empty( $user_status ) ) {
56 // Always default to protecting content.
57 return false;
58 }
59
60 if ( ! in_array( $user_status, [ 'logged_in', 'logged_out' ], true ) ) {
61 // Invalid user status.
62 return false;
63 }
64
65 if ( ! is_array( $user_roles ) ) {
66 // If roles is string, convert to array.
67 $user_roles = explode( ',', $user_roles );
68 $user_roles = array_map( 'trim', $user_roles );
69 $user_roles = array_map( 'strtolower', $user_roles );
70 }
71
72 // If roles is array of keyed roles, convert to array of roles[].
73 if ( is_string( key( $user_roles ) ) ) {
74 $user_roles = array_keys( $user_roles );
75 }
76
77 $logged_in = \is_user_logged_in();
78
79 switch ( $user_status ) {
80 case 'logged_in':
81 // If not logged in, return false.
82 if ( ! $logged_in ) {
83 return false;
84 }
85
86 // If current user is excluded from restrictions, return true.
87 if ( user_is_excluded() ) {
88 return true;
89 }
90
91 // If we got this far, we're logged in.
92 if ( 'any' === $role_match || empty( $user_roles ) ) {
93 return true;
94 }
95
96 if ( ! in_array( $role_match, [ 'match', 'exclude' ], true ) ) {
97 // Invalid role match.
98 return false;
99 }
100
101 // True for match, false for exclude.
102 $match_value = 'match' === $role_match ? true : false;
103
104 // Checks all roles, any match will return.
105 foreach ( $user_roles as $role ) {
106 if ( \current_user_can( $role ) ) {
107 return $match_value;
108 }
109 }
110
111 // If we got this far, we're logged in but don't have the required role.
112 return ! $match_value;
113
114 case 'logged_out':
115 return ! $logged_in;
116 }
117 }
118
119 /**
120 * Check if a given query can be ignored.
121 *
122 * @param \WP_Query $query Query object.
123 *
124 * @return bool True if query can be ignored, false if not.
125 */
126 function query_can_be_ignored( $query = null ) {
127 if ( $query->get( 'ignore_restrictions', false ) ) {
128 return true;
129 }
130
131 $post_types_to_ignore = \apply_filters( 'content_control/post_types_to_ignore', [
132 'cc_restriction',
133 'wp_template',
134 'wp_template_part',
135 'wp_global_styles',
136 'oembed_cache',
137 ] );
138
139 // Ignore specific core post types.
140 if ( in_array( $query->get( 'post_type' ), $post_types_to_ignore, true ) ) {
141 return true;
142 }
143
144 return false !== \apply_filters( 'content_control/ignoreable_query', false, $query );
145 }
146