PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.0.2
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.0.2
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.2, at classes/Services/Restrictions.php

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