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 / classes / Services / Restrictions.php

Restrictions.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More 2.0.4, at classes/Services/Restrictions.php

287 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Frontend feed setup.
4 *
5 * @copyright (c) 2021, Code Atlantic LLC.
6 * @package ContentControl
7 */
8
9 namespace ContentControl\Services;
10
11 use ContentControl\Models\Restriction;
12
13 use function ContentControl\get_query;
14 use function ContentControl\current_query_context;
15 use function ContentControl\deep_clean_array;
16 use function ContentControlPro\plugin;
17
18 defined( 'ABSPATH' ) || exit;
19
20 /**
21 * Restrictions service.
22 */
23 class Restrictions {
24
25 /**
26 * Cache
27 *
28 * @var array
29 */
30 protected $cache = [];
31
32 /**
33 * Get a list of all restrictions.
34 *
35 * @return Restriction[]
36 */
37 public function get_restrictions() {
38 static $all_restrictions;
39
40 if ( ! isset( $all_restrictions ) ) {
41 $all_restrictions = [];
42
43 if ( \ContentControl\get_data_version( 'restrictions' ) === 1 ) {
44 $restrictions = \ContentControl\get_v1_restrictions();
45
46 foreach ( $restrictions as $key => $restriction ) {
47 $restriction['id'] = $key;
48 $all_restrictions[ $key ] = new Restriction( $restriction );
49 }
50 } else {
51 // Query restriction post type.
52 $restrictions = get_posts(
53 [
54 'post_type' => 'cc_restriction',
55 'posts_per_page' => -1,
56 'post_status' => 'publish',
57 // Order by menu order.
58 'orderby' => [
59 'menu_order' => 'ASC',
60 'post_modified' => 'DESC',
61 ],
62 ]
63 );
64
65 foreach ( $restrictions as $restriction ) {
66 $all_restrictions[ $restriction->ID ] = new Restriction( $restriction );
67 }
68 }
69 }
70
71 return $all_restrictions;
72 }
73
74 /**
75 * Get restriction, by ID, slug or object.
76 *
77 * @param int|string|Restriction $restriction Restriction ID, slug or object.
78 *
79 * @return Restriction|false
80 */
81 public function get_restriction( $restriction ) {
82 if ( $restriction instanceof Restriction ) {
83 return $restriction;
84 }
85
86 $restrictions = $this->get_restrictions();
87
88 // If restriction is an ID, get the object.
89 if ( is_numeric( $restriction ) && isset( $restrictions[ $restriction ] ) ) {
90 return $restrictions[ $restriction ];
91 }
92
93 // If restriction is a slug, get the object.
94 if ( is_string( $restriction ) ) {
95 // Check if string is a slug or title.
96 $slug = sanitize_title( $restriction );
97
98 if ( $slug === $restriction ) {
99 foreach ( $restrictions as $restriction_obj ) {
100 if ( $restriction_obj->slug === $restriction ) {
101 return $restriction_obj;
102 }
103 }
104 }
105
106 // Check if string is a title.
107 foreach ( $restrictions as $restriction_obj ) {
108 if ( $restriction_obj->title === $restriction ) {
109 return $restriction_obj;
110 }
111 }
112 }
113
114 return false;
115 }
116
117 /**
118 * Get cache key for restrictions.
119 *
120 * @param int|null $post_id Post ID.
121 *
122 * @return string
123 */
124 public function get_cache_key( $post_id = null ) {
125 $query = get_query();
126 $context = current_query_context();
127
128 try {
129 $hash_vars = deep_clean_array( $query->query_vars );
130
131 $query_hash = md5( maybe_serialize( $hash_vars ) );
132 } catch ( \Exception $e ) {
133 $query_hash = md5( wp_rand( 0, 100000 ) );
134 plugin( 'logging' )->log( 'ERROR: ' . $e->getMessage() );
135 }
136
137 if ( is_null( $post_id ) ) {
138 $post_id = \get_the_ID();
139 }
140
141 switch ( $context ) {
142 case 'main':
143 $cache_key = 'main';
144 break;
145
146 case 'main/posts':
147 case 'posts':
148 $cache_key = 'post-' . $post_id;
149 break;
150
151 default:
152 $cache_key = $context . '_' . $query_hash . ( $post_id ? ( '_post-' . $post_id ) : '' );
153 break;
154 }
155
156 return $cache_key;
157 }
158
159 /**
160 * Get all applicable restrictions for the current post.
161 *
162 * Careful, this could be very unperformant if you have a lot of restrictions.
163 *
164 * @param int|null $post_id Post ID.
165 *
166 * @return Restriction[]
167 */
168 public function get_all_applicable_restrictions( $post_id = null ) {
169 $cache_name = 'all_applicable_restrictions';
170 $cache_key = $this->get_cache_key( $post_id );
171
172 if ( isset( $this->cache[ $cache_name ][ $cache_key ] ) ) {
173 return $this->cache[ $cache_name ][ $cache_key ];
174 }
175
176 $restrictions = $this->get_restrictions();
177
178 if ( ! empty( $restrictions ) ) {
179 foreach ( $restrictions as $key => $restriction ) {
180 if ( ! $restriction->check_rules() ) {
181 unset( $restrictions[ $key ] );
182 }
183 }
184 }
185
186 $this->cache[ $cache_name ][ $cache_key ] = $restrictions;
187
188 return $restrictions;
189 }
190
191 /**
192 * Get the first applicable restriction for the current post.
193 *
194 * Performant version that breaks on first applicable restriction. Sorted by priority.
195 * cached internally.
196 *
197 * @param int|null $post_id Post ID.
198 *
199 * @return Restriction|false
200 */
201 public function get_applicable_restriction( $post_id = null ) {
202 $cache_name = 'applicable_restriction';
203 $cache_key = $this->get_cache_key( $post_id );
204
205 if ( isset( $this->cache[ $cache_name ][ $cache_key ] ) ) {
206 return $this->cache[ $cache_name ][ $cache_key ];
207 }
208
209 $this->cache[ $cache_name ][ $cache_key ] = false;
210
211 $restrictions = $this->get_restrictions();
212
213 $restrictions = $this->sort_restrictions_by_priority( $restrictions );
214
215 if ( ! empty( $restrictions ) ) {
216 foreach ( $restrictions as $restriction ) {
217 if ( $restriction->check_rules() ) {
218 $this->cache[ $cache_name ][ $cache_key ] = $restriction;
219 break;
220 }
221 }
222 }
223
224 return $this->cache[ $cache_name ][ $cache_key ];
225 }
226
227 /**
228 * Check if post has applicable restrictions.
229 *
230 * Cached via get_applicable_restriction().
231 *
232 * @param int|null $post_id Post ID.
233 *
234 * @return boolean
235 */
236 public function has_applicable_restrictions( $post_id = null ) {
237 return false !== $this->get_applicable_restriction( $post_id );
238 }
239
240 /**
241 * Check if user meets requirements for given restriction.
242 *
243 * @param int|string|Restriction $restriction Restriction ID, slug or object.
244 *
245 * @return boolean
246 */
247 public function user_meets_requirements( $restriction ) {
248 $restriction = $this->get_restriction( $restriction );
249
250 if ( false === $restriction ) {
251 return false;
252 }
253
254 $cache_name = 'user_meets_requirements';
255 $cache_key = $restriction->id;
256
257 // Check cache.
258 if ( isset( $this->cache[ $cache_name ][ $cache_key ] ) ) {
259 return $this->cache[ $cache_name ][ $cache_key ];
260 }
261
262 $user_meets_requirements = $restriction->user_meets_requirements();
263
264 // Cache result.
265 $this->cache[ $cache_name ][ $cache_key ] = $user_meets_requirements;
266
267 return $user_meets_requirements;
268 }
269
270 /**
271 * Sort restrictions based on post sort order.
272 *
273 * MOVE to restrictions file.
274 *
275 * @param \ContentControl\Models\Restriction[] $restrictions Restrictions.
276 *
277 * @return \ContentControl\Models\Restriction[]
278 */
279 public function sort_restrictions_by_priority( $restrictions ) {
280 usort( $restrictions, function ( $a, $b ) {
281 return $a->priority - $b->priority;
282 });
283
284 return $restrictions;
285 }
286 }
287