PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / trunk
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More vtrunk
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 trunk, at inc/functions/restrictions.php

244 lines 6.0 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_excludable() {
42 return \current_user_can( plugin()->get_permission( 'manage_settings' ) );
43 }
44
45 /**
46 * Current user is excluded from restrictions.
47 *
48 * @return bool True if user is excluded, false if not.
49 */
50 function user_is_excluded() {
51 return admins_are_excluded() && user_is_excludable();
52 }
53
54 /**
55 * Check if user meets requirements.
56 *
57 * @param string $user_status logged_in or logged_out.
58 * @param string[]|array<string,string>|string $user_roles array of roles to check.
59 * @param string $role_match any|match|exclude.
60 *
61 * @return bool True if user meets requirements, false if not.
62 */
63 function user_meets_requirements( $user_status, $user_roles = [], $role_match = 'match' ) {
64 if ( empty( $user_status ) ) {
65 // Always default to protecting content.
66 return false;
67 }
68
69 if ( ! in_array( $user_status, [ 'logged_in', 'logged_out' ], true ) ) {
70 // Invalid user status.
71 return false;
72 }
73
74 if ( ! is_array( $user_roles ) ) {
75 // If roles is string, convert to array.
76 $user_roles = explode( ',', $user_roles );
77 $user_roles = array_map( 'trim', $user_roles );
78 $user_roles = array_map( 'strtolower', $user_roles );
79 }
80
81 // If roles is array of keyed roles, convert to array of roles[].
82 if ( is_string( key( $user_roles ) ) ) {
83 $user_roles = array_keys( $user_roles );
84 }
85
86 $logged_in = \is_user_logged_in();
87
88 switch ( $user_status ) {
89 case 'logged_in':
90 // If not logged in, return false.
91 if ( ! $logged_in ) {
92 return false;
93 }
94
95 // If current user is excluded from restrictions, return true.
96 if ( user_is_excluded() ) {
97 return true;
98 }
99
100 // If we got this far, we're logged in.
101 if ( 'any' === $role_match || empty( $user_roles ) ) {
102 return true;
103 }
104
105 if ( ! in_array( $role_match, [ 'match', 'exclude' ], true ) ) {
106 // Invalid role match.
107 return false;
108 }
109
110 // True for match, false for exclude.
111 $match_value = 'match' === $role_match ? true : false;
112
113 // Checks all roles, any match will return.
114 foreach ( $user_roles as $role ) {
115 if ( \current_user_can( $role ) ) {
116 return $match_value;
117 }
118 }
119
120 // If we got this far, we're logged in but don't have the required role.
121 return ! $match_value;
122
123 case 'logged_out':
124 return ! $logged_in;
125 }
126 }
127
128 /**
129 * Check if a given query can be ignored.
130 *
131 * @param \WP_Query|\WP_Term_Query $query Query object.
132 *
133 * @return bool True if query can be ignored, false if not.
134 *
135 * @since 2.2.0 Added support for WP_Term_Query.
136 */
137 function query_can_be_ignored( $query = null ) {
138 // Early bypass.
139 $bypass = \apply_filters( 'content_control/pre_query_can_be_ignored', null, $query );
140 if ( null !== $bypass ) {
141 return $bypass;
142 }
143
144 if ( $query instanceof \WP_Query ) {
145 if ( $query->get( 'ignore_restrictions', false ) ) {
146 return true;
147 }
148
149 // Skip post types that are not public.
150 if ( $query->get( 'post_type' ) ) {
151 $post_types = $query->get( 'post_type' );
152
153 // If post type is a string, convert to array.
154 if ( is_string( $post_types ) ) {
155 $post_types = explode( ',', $post_types );
156 }
157
158 foreach ( $post_types as $post_type ) {
159 // Check if post type exists.
160 if ( ! post_type_exists( $post_type ) ) {
161 continue;
162 }
163
164 $post_type_object = get_post_type_object( $post_type );
165
166 // Check if post type is public.
167 if ( ! $post_type_object->public ) {
168 return true;
169 }
170 }
171 }
172
173 $post_types_to_ignore = \apply_filters( 'content_control/post_types_to_ignore', [
174 'cc_restriction',
175 'wp_template',
176 'wp_template_part',
177 'wp_global_styles',
178 'oembed_cache',
179 ] );
180
181 // Ignore specific core post types.
182 if ( in_array( $query->get( 'post_type' ), $post_types_to_ignore, true ) ) {
183 return true;
184 }
185 }
186
187 if ( $query instanceof \WP_Term_Query ) {
188 // Skip taxonomies that are not public.
189 if ( isset( $query->query_vars['taxonomy'] ) ) {
190 $taxonomies = $query->query_vars['taxonomy'];
191
192 // If taxonomy is a string, convert to array.
193 if ( is_string( $taxonomies ) ) {
194 $taxonomies = explode( ',', $taxonomies );
195 }
196
197 foreach ( $taxonomies as $taxonomy ) {
198 // Check if taxonomy exists.
199 if ( ! taxonomy_exists( $taxonomy ) ) {
200 continue;
201 }
202
203 $taxonomy_object = get_taxonomy( $taxonomy );
204
205 // Check if taxonomy is public.
206 if ( ! $taxonomy_object->public ) {
207 return true;
208 }
209 }
210 }
211
212 // Ignore specific core taxonomies.
213 $taxonomies_to_ignore = \apply_filters( 'content_control/taxonomies_to_ignore', [
214 'nav_menu',
215 'link_category',
216 'post_format',
217 'wp_theme',
218 ] );
219
220 $query_taxonomies = isset( $query->query_vars['taxonomy'] ) ? (array) $query->query_vars['taxonomy'] : [];
221
222 foreach ( (array) $taxonomies_to_ignore as $taxonomy ) {
223 if ( in_array( $taxonomy, $query_taxonomies, true ) ) {
224 return true;
225 }
226 }
227
228 static $last_term_query = null;
229
230 if ( $last_term_query && doing_filter( 'get_terms' ) &&
231 (
232 $query === $last_term_query ||
233 wp_json_encode( $query->query_vars ) === wp_json_encode( $last_term_query->query_vars )
234 )
235 ) {
236 return true;
237 }
238
239 $last_term_query = $query;
240 }
241
242 return false !== \apply_filters( 'content_control/ignoreable_query', false, $query );
243 }
244