PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.0.4
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.0.4
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.4, at inc/functions/developers.php

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