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 / developers.php

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

268 lines 6.1 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 use function ContentControl\plugin;
13 use function ContentControl\set_rules_query;
14
15 defined( 'ABSPATH' ) || exit;
16
17 /**
18 * Check if content has restrictions.
19 *
20 * @param int|null $post_id Post ID.
21 *
22 * @return bool
23 *
24 * @since 2.0.0
25 */
26 function content_has_restrictions( $post_id = null ) {
27 $overload_post = setup_post( $post_id );
28
29 $has_restrictions = plugin( 'restrictions' )->has_applicable_restrictions( $post_id );
30
31 // Clear post if we overloaded it.
32 if ( $overload_post ) {
33 clear_post();
34 }
35
36 /**
37 * Filter whether content has restrictions.
38 *
39 * @param bool $has_restrictions Whether content has restrictions.
40 * @param int|null $post_id Post ID.
41 *
42 * @return bool
43 *
44 * @since 2.0.0
45 */
46 return (bool) apply_filters( 'content_control/content_has_restriction', $has_restrictions, $post_id );
47 }
48
49 /**
50 * Check if user can access content.
51 *
52 * @param int|null $post_id Post ID.
53 *
54 * @return bool True if user meets requirements, false if not.
55 *
56 * @since 2.0.0
57 */
58 function user_can_view_content( $post_id = null ) {
59 if ( ! content_has_restrictions( $post_id ) ) {
60 return true;
61 }
62
63 $can_view = true;
64
65 $restrictions = [];
66
67 if ( false === (bool) apply_filters( 'content_control/check_all_restrictions', false, $post_id ) ) {
68 $restriction = get_applicable_restriction( $post_id );
69
70 if ( null !== $restriction ) {
71 $can_view = $restriction->user_meets_requirements();
72 $restrictions[] = $restriction;
73 }
74 } else {
75 $restrictions = get_all_applicable_restrictions( $post_id );
76
77 if ( count( $restrictions ) ) {
78 $checks = [];
79
80 foreach ( $restrictions as $restriction ) {
81 $checks[] = $restriction->user_meets_requirements();
82 }
83
84 // When checking all, we are looking for any true value.
85 $can_view = in_array( true, $checks, true );
86 }
87 }
88
89 /**
90 * Filter whether user can view content.
91 *
92 * @param bool $can_view Whether user can view content.
93 * @param int|null $post_id Post ID.
94 * @param \ContentControl\Models\Restriction[] $restrictions Restrictions.
95 *
96 * @return bool
97 *
98 * @since 2.0.0
99 */
100 return (bool) apply_filters( 'content_control/user_can_view_content', $can_view, $post_id, $restrictions );
101 }
102
103 /**
104 * Helper that checks if given or current post is restricted or not.
105 *
106 * @see \ContentControl\user_can_view_content() to check if user can view content.
107 *
108 * @param int|null $post_id Post ID.
109 *
110 * @return bool
111 *
112 * @since 2.0.0
113 */
114 function content_is_restricted( $post_id = null ) {
115 $is_restricted = ! user_can_view_content( $post_id );
116
117 /**
118 * Filter whether content is restricted.
119 *
120 * @param bool $is_restricted Whether content is restricted.
121 * @param int|null $post_id Post ID.
122 *
123 * @return bool
124 *
125 * @since 2.0.0
126 */
127 return (bool) apply_filters( 'content_control/content_is_restricted', $is_restricted, $post_id );
128 }
129
130 /**
131 * Get applicable restriction.
132 *
133 * @param int|null $post_id Post ID.
134 *
135 * @return \ContentControl\Models\Restriction|false
136 *
137 * @since 2.0.0
138 */
139 function get_applicable_restriction( $post_id = null ) {
140 $overload_post = setup_post( $post_id );
141
142 $restriction = plugin( 'restrictions' )->get_applicable_restriction( $post_id );
143
144 // Clear post if we overloaded it.
145 if ( $overload_post ) {
146 clear_post();
147 }
148
149 return $restriction;
150 }
151
152 /**
153 * Get all applicable restrictions.
154 *
155 * @param int|null $post_id Post ID.
156 *
157 * @return \ContentControl\Models\Restriction[]
158 *
159 * @since 2.0.11
160 */
161 function get_all_applicable_restrictions( $post_id = null ) {
162 $overload_post = setup_post( $post_id );
163
164 $restrictions = plugin( 'restrictions' )->get_all_applicable_restrictions( $post_id );
165
166 // Clear post if we overloaded it.
167 if ( $overload_post ) {
168 clear_post();
169 }
170
171 return $restrictions;
172 }
173
174 /**
175 * Check if query has restrictions.
176 *
177 * @param \WP_Query $query Query object.
178 *
179 * @return array<array{restriction:\ContentControl\Models\Restriction,post_ids:int[]}>|false
180 *
181 * @since 2.0.0
182 */
183 function get_restriction_matches_for_queried_posts( $query ) {
184 // If its the main query, and not an archive-like page, bail.
185 if ( $query->is_main_query() && ( ! $query->is_home() && ! $query->is_archive() && ! $query->is_search() ) ) {
186 return false;
187 }
188
189 if ( empty( $query->posts ) ) {
190 return false;
191 }
192
193 static $restrictions;
194
195 // Generate cache key from hasing $wp_query.
196 $cache_key = md5( (string) wp_json_encode( $query ) );
197
198 if ( isset( $restrictions[ $cache_key ] ) ) {
199 return $restrictions[ $cache_key ];
200 }
201
202 set_rules_query( $query );
203
204 foreach ( $query->posts as $post ) {
205 if ( content_is_restricted( $post->ID ) ) {
206 $restriction = get_applicable_restriction( $post->ID );
207
208 if ( ! isset( $restrictions[ $cache_key ][ $restriction->priority ] ) ) {
209 // Handles deduplication & sorting.
210 $restrictions[ $cache_key ][ $restriction->priority ] = [
211 'restriction' => $restriction,
212 'post_ids' => [],
213 ];
214 }
215
216 // Add post to restriction.
217 $restrictions[ $cache_key ][ $restriction->priority ]['post_ids'][] = $post->ID;
218 }
219 }
220
221 set_rules_query( null );
222
223 if ( empty( $restrictions[ $cache_key ] ) ) {
224 $restrictions[ $cache_key ] = false;
225 } else {
226 // Sort by priority.
227 ksort( $restrictions[ $cache_key ] );
228 // Remove priority keys.
229 $restrictions[ $cache_key ] = array_values( $restrictions[ $cache_key ] );
230 }
231
232 return $restrictions[ $cache_key ];
233 }
234
235 /**
236 * Check if protection methods should be disabled.
237 *
238 * Generally used to bypass protections when using page editors.
239 *
240 * @return bool
241 *
242 * @since 2.0.0
243 */
244 function protection_is_disabled() {
245 $checks = [
246 // Disable protection when not on the frontend.
247 ! \ContentControl\is_frontend(),
248 // Disable protection when user is excluded.
249 user_is_excluded(),
250 // Disable protection when viewing post previews.
251 is_preview() && current_user_can( 'edit_post', get_the_ID() ),
252 ];
253
254 /**
255 * Filter whether protection is disabled.
256 *
257 * @param bool $is_disabled Whether protection is disabled.
258 *
259 * @return bool
260 *
261 * @since 2.0.0
262 */
263 return apply_filters(
264 'content_control/protection_is_disabled',
265 in_array( true, $checks, true )
266 );
267 }
268