PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.0.1
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.0.1
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 / Models / Restriction.php

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

472 lines 10.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Restriction model.
4 *
5 * @package ContentControl\RuleEngine
6 * @subpackage Models
7 */
8
9 namespace ContentControl\Models;
10
11 use ContentControl\Models\RuleEngine\Query;
12 use function ContentControl\get_default_restriction_settings;
13
14 defined( 'ABSPATH' ) || exit;
15
16 /**
17 * Model for restriction sets.
18 *
19 * @package ContentControl\Models
20 */
21 class Restriction {
22
23 /**
24 * Post object.
25 *
26 * @var \WP_Post
27 */
28 private $post;
29
30 /**
31 * Restriction id.
32 *
33 * @var int
34 */
35 public $id = 0;
36
37 /**
38 * Restriction slug.
39 *
40 * @var string
41 */
42 public $slug;
43
44 /**
45 * Restriction label.
46 *
47 * @var string
48 */
49 public $title;
50
51 /**
52 * Restriction description.
53 *
54 * @var string
55 */
56 public $description;
57
58 /**
59 * Restriction Message.
60 *
61 * @var string
62 */
63 public $message;
64
65 /**
66 * Restriction status.
67 *
68 * @var string
69 */
70 public $status;
71
72 /**
73 * Restriction priority.
74 *
75 * @var int
76 */
77 public $priority;
78
79 /**
80 * Restriction Setting: Required user status.
81 *
82 * @var string 'logged_in' | 'logged_out';
83 */
84 public $user_status;
85
86 /**
87 * Restriction Setting: Which roles.
88 *
89 * @deprecated 2.0.0 Use user_roles instead.
90 *
91 * @var string[]
92 */
93 public $roles;
94
95 /**
96 * Restriction Setting: Chosen user roles.
97 *
98 * @var string[]
99 */
100 public $user_roles;
101
102 /**
103 * Restriction Setting: Role match method.
104 *
105 * @var string 'any' | 'match' | 'exclude';
106 */
107 public $role_match;
108
109 /**
110 * Restriction Setting: Protection method.
111 *
112 * @var string 'redirect' | 'replace'
113 */
114 public $protection_method;
115
116 /**
117 * Restriction Setting: Redirect type.
118 *
119 * @var string 'login' | 'home' | 'custom'
120 */
121 public $redirect_type;
122
123 /**
124 * Restriction Setting: Redirect url.
125 *
126 * @var string
127 */
128 public $redirect_url;
129
130 /**
131 * Restriction Setting: Replacement type.
132 *
133 * @var string 'message' | 'page'
134 */
135 public $replacement_type;
136
137 /**
138 * Restriction Setting: Replacement page.
139 *
140 * @var int
141 */
142 public $replacement_page = 0;
143
144 /**
145 * Restriction Setting: Archive handling.
146 *
147 * @var string 'filter_post_content' | 'replace_archive_page' | 'redirect' | 'hide'
148 */
149 public $archive_handling;
150
151 /**
152 * Restriction Setting: Archive replacement page.
153 *
154 * @var int
155 */
156 public $archive_replacement_page = 0;
157
158 /**
159 * Restriction Setting: Redirect type.
160 *
161 * @var string 'login' | 'home' | 'custom'
162 */
163 public $archive_redirect_type;
164
165 /**
166 * Restriction Setting: Redirect url.
167 *
168 * @var string
169 */
170 public $archive_redirect_url;
171
172 /**
173 * Restriction Setting: Additional query handling.
174 *
175 * @var string 'filter_post_content' | 'hide'
176 */
177 public $additional_query_handling;
178
179 /**
180 * Restriction Settings: Show Excerpts.
181 *
182 * @var bool
183 */
184 public $show_excerpts;
185
186 /**
187 * Restriction Settings: Override Default Message.
188 *
189 * @var bool
190 */
191 public $override_message;
192
193 /**
194 * Restriction Settings: Custom Message.
195 *
196 * @var string
197 */
198 public $custom_message;
199
200 /**
201 * Restriction Settings: Conditions.
202 *
203 * @var array
204 */
205 public $conditions;
206
207 /**
208 * Restriction Condition Query.
209 *
210 * @var Query
211 */
212 public $query;
213
214 /**
215 * Build a restriction.
216 *
217 * @param \WP_Post $restriction Restriction data.
218 */
219 public function __construct( $restriction ) {
220 if ( ! is_a( $restriction, '\WP_Post' ) ) {
221 $this->setup_v1_restriction( $restriction );
222 } else {
223 $this->post = $restriction;
224
225 $settings = get_post_meta( $restriction->ID, 'restriction_settings', true );
226
227 $settings = wp_parse_args(
228 is_array( $settings ) ? $settings : [],
229 get_default_restriction_settings()
230 );
231
232 // Convert keys to snake_case using camel_case_to_snake_case().
233 $settings = array_combine(
234 array_map( 'ContentControl\camel_case_to_snake_case', array_keys( $settings ) ),
235 array_values( $settings )
236 );
237
238 $properties = array_merge(
239 [
240 'id' => $restriction->ID,
241 'slug' => $restriction->post_name,
242 'title' => $restriction->post_title,
243 'status' => $restriction->post_status,
244 'priority' => $restriction->menu_order,
245 // We set this late.. on first use.
246 'description' => null,
247 'message' => null,
248 ],
249 $settings
250 );
251
252 foreach ( $properties as $key => $value ) {
253 $this->$key = $value;
254 }
255
256 $this->query = new Query( $this->conditions );
257 }
258 }
259
260 /**
261 * Map old v1 restriction to new v2 restriction object.
262 *
263 * @param array $restriction Restriction data.
264 */
265 public function setup_v1_restriction( $restriction ) {
266 static $index = 0;
267
268 $restriction = \wp_parse_args( $restriction, [
269 'title' => '',
270 'who' => '',
271 'roles' => [],
272 'protection_method' => 'redirect',
273 'show_excerpts' => false,
274 'override_default_message' => false,
275 'custom_message' => '',
276 'redirect_type' => 'login',
277 'redirect_url' => '',
278 'conditions' => '',
279 ] );
280
281 $this->id = 0;
282 $this->slug = '';
283 $this->title = $restriction['title'];
284 $this->description = '';
285 $this->status = 'publish';
286 $this->priority = $index;
287
288 $user_roles = is_array( $restriction['roles'] ) ? $restriction['roles'] : [];
289
290 $this->user_status = $restriction['who'];
291 $this->role_match = count( $user_roles ) > 0 ? 'match' : 'any';
292 $this->user_roles = $user_roles;
293 $this->protection_method = 'custom_message' === $restriction['protection_method'] ? 'replace' : 'redirect';
294 $this->redirect_type = $restriction['redirect_type'];
295 $this->redirect_url = $restriction['redirect_url'];
296 $this->replacement_type = 'message';
297 $this->replacement_page = 0;
298 $this->archive_handling = 'filter_post_content';
299 $this->archive_replacement_page = 0;
300 $this->archive_redirect_type = $restriction['redirect_type'];
301 $this->archive_redirect_url = $restriction['redirect_url'];
302 $this->additional_query_handling = 'filter_post_content';
303 $this->override_message = $restriction['override_default_message'];
304 $this->custom_message = $restriction['custom_message'];
305 $this->show_excerpts = $restriction['show_excerpts'];
306 $this->conditions = \ContentControl\remap_conditions_to_query( $restriction['conditions'] );
307
308 $this->query = new Query( $this->conditions );
309
310 ++$index;
311 }
312
313 /**
314 * Check if this set has JS based rules.
315 *
316 * @return bool
317 */
318 public function has_js_rules() {
319 return $this->query->has_js_rules();
320 }
321
322 /**
323 * Check this sets rules.
324 *
325 * @return bool
326 */
327 public function check_rules() {
328 if ( ! $this->query->has_rules() ) {
329 // No rules should be treated as no restrictions.
330 return false;
331 }
332
333 return $this->query->check_rules();
334 }
335
336 /**
337 * Check if this restriction applies to the current user.
338 *
339 * @return bool
340 */
341 public function user_meets_requirements() {
342 return \ContentControl\user_meets_requirements( $this->user_status, $this->user_roles, $this->role_match );
343 }
344
345 /**
346 * Get the description for this restriction.
347 *
348 * @return string
349 */
350 public function get_description() {
351 if ( ! isset( $this->description ) ) {
352 $this->description = get_the_excerpt( $this->id );
353
354 if ( empty( $this->description ) ) {
355 $this->description = __( 'This content is restricted.', 'content-control' );
356 }
357 }
358
359 return $this->description;
360 }
361
362 /**
363 * Get the message for this restriction.
364 *
365 * @uses \get_the_content()
366 * @uses \ContentControl\get_default_denial_message()
367 *
368 * @return string
369 */
370 public function get_message() {
371 if ( ! isset( $this->message ) ) {
372 if ( ! empty( $this->post->post_content ) ) {
373 $message = \get_the_content( null, false, $this->id );
374 } elseif ( 'message' === $this->replacement_type && $this->override_message && ! empty( $this->custom_message ) ) {
375 $message = $this->custom_message;
376 } else {
377 $message = \ContentControl\get_default_denial_message();
378 }
379
380 $this->message = ! empty( $message ) ?
381 $message :
382 __( 'This content is restricted.', 'content-control' );
383 }
384
385 return $this->message;
386 }
387
388 /**
389 * Whether to show excerpts for posts that are restricted.
390 *
391 * @return bool
392 */
393 public function show_excerpts() {
394 return (bool) $this->show_excerpts;
395 }
396
397 /**
398 * Check if this uses the redirect method.
399 *
400 * @return bool
401 */
402 public function uses_redirect_method() {
403 return 'redirect' === $this->protection_method;
404 }
405
406 /**
407 * Check if this uses the replace method.
408 *
409 * @return bool
410 */
411 public function uses_replace_method() {
412 return 'replace' === $this->protection_method;
413 }
414
415 /**
416 * Convert this restriction to an array.
417 *
418 * @return array
419 */
420 public function to_array() {
421 return [
422 'id' => $this->id,
423 'slug' => $this->slug,
424 'title' => $this->title,
425 'description' => $this->get_description(),
426 'message' => $this->get_message(),
427 'status' => $this->status,
428 'priority' => $this->priority,
429 // Options include logged_in, logged_out.
430 'userStatus' => $this->user_status,
431 // Options include any, match, exclude.
432 'roleMatch' => $this->role_match,
433 'userRoles' => $this->user_roles,
434 'protectionMethod' => $this->protection_method,
435 'redirectType' => $this->redirect_type,
436 'redirectUrl' => $this->redirect_url,
437 'replacementType' => $this->replacement_type,
438 'replacementPage' => $this->replacement_page,
439 'archiveHandling' => $this->archive_handling,
440 'archiveReplacementPage' => $this->archive_replacement_page,
441 'archiveRedirectType' => $this->archive_redirect_type,
442 'archiveRedirectUrl' => $this->archive_redirect_url,
443 'additionalQueryHandling' => $this->additional_query_handling,
444 'overrideMessage' => $this->override_message,
445 'customMessage' => $this->custom_message,
446 'showExcerpts' => $this->show_excerpts,
447 'conditions' => $this->conditions,
448 ];
449 }
450
451 /**
452 * Convert this restriction to a v1 array.
453 *
454 * @return array
455 */
456 public function to_v1_array() {
457 return [
458 'id' => $this->id,
459 'title' => $this->title,
460 'who' => $this->user_status,
461 'roles' => $this->user_roles,
462 'protection_method' => $this->protection_method,
463 'show_excerpts' => $this->show_excerpts,
464 'override_default_message' => $this->override_message,
465 'custom_message' => $this->custom_message,
466 'redirect_type' => $this->redirect_type,
467 'redirect_url' => $this->redirect_url,
468 'conditions' => $this->conditions,
469 ];
470 }
471 }
472