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 / classes / Models / Restriction.php

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

515 lines 11.5 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|null
55 */
56 public $description;
57
58 /**
59 * Restriction Message.
60 *
61 * @var string|null
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{logicalOperator:string,items:array<array<string,mixed>>}
204 */
205 public $conditions;
206
207 /**
208 * Restriction Condition Query.
209 *
210 * @var Query
211 */
212 public $query;
213
214
215 /**
216 * Data version.
217 *
218 * @var int
219 */
220 public $data_version;
221
222 /**
223 * Build a restriction.
224 *
225 * @param \WP_Post|array<string,mixed> $restriction Restriction data.
226 */
227 public function __construct( $restriction ) {
228 if ( ! is_a( $restriction, '\WP_Post' ) ) {
229 $this->setup_v1_restriction( $restriction );
230 } else {
231 $this->post = $restriction;
232
233 /**
234 * Restriction settings.
235 *
236 * @var array<string,mixed>|false $settings
237 */
238 $settings = get_post_meta( $restriction->ID, 'restriction_settings', true );
239
240 if ( ! $settings ) {
241 $settings = [];
242 }
243
244 $settings = wp_parse_args(
245 $settings,
246 get_default_restriction_settings()
247 );
248
249 // Convert keys to snake_case using camel_case_to_snake_case().
250 $settings = array_combine(
251 array_map( 'ContentControl\camel_case_to_snake_case', array_keys( $settings ) ),
252 array_values( $settings )
253 );
254
255 $properties = array_merge(
256 [
257 'id' => $restriction->ID,
258 'slug' => $restriction->post_name,
259 'title' => $restriction->post_title,
260 'status' => $restriction->post_status,
261 'priority' => $restriction->menu_order,
262 // We set this late.. on first use.
263 'description' => null,
264 'message' => null,
265 ],
266 $settings
267 );
268
269 $this->data_version = get_post_meta( $restriction->ID, 'data_version', true );
270
271 if ( ! $this->data_version ) {
272 $this->data_version = 2;
273 update_post_meta( $restriction->ID, 'data_version', 2 );
274 }
275
276 foreach ( $properties as $key => $value ) {
277 $this->$key = $value;
278 }
279
280 $this->query = new Query( $this->conditions );
281 }
282 }
283
284 /**
285 * Map old v1 restriction to new v2 restriction object.
286 *
287 * @param array<string,mixed> $restriction Restriction data.
288 *
289 * @return void
290 */
291 public function setup_v1_restriction( $restriction ) {
292 static $index = 0;
293
294 $restriction = \wp_parse_args( $restriction, [
295 'title' => '',
296 'who' => '',
297 'roles' => [],
298 'protection_method' => 'redirect',
299 'show_excerpts' => false,
300 'override_default_message' => false,
301 'custom_message' => '',
302 'redirect_type' => 'login',
303 'redirect_url' => '',
304 'conditions' => '',
305 ] );
306
307 $this->data_version = 1;
308
309 $this->id = 0;
310 $this->slug = '';
311 $this->title = $restriction['title'];
312 $this->description = '';
313 $this->status = 'publish';
314 $this->priority = $index;
315
316 $user_roles = is_array( $restriction['roles'] ) ? $restriction['roles'] : [];
317
318 $this->user_status = $restriction['who'];
319 $this->role_match = count( $user_roles ) > 0 ? 'match' : 'any';
320 $this->user_roles = $user_roles;
321 $this->protection_method = 'custom_message' === $restriction['protection_method'] ? 'replace' : 'redirect';
322 $this->redirect_type = $restriction['redirect_type'];
323 $this->redirect_url = $restriction['redirect_url'];
324 $this->replacement_type = 'message';
325 $this->replacement_page = 0;
326 $this->archive_handling = 'filter_post_content';
327 $this->archive_replacement_page = 0;
328 $this->archive_redirect_type = $restriction['redirect_type'];
329 $this->archive_redirect_url = $restriction['redirect_url'];
330 $this->additional_query_handling = 'filter_post_content';
331 $this->override_message = $restriction['override_default_message'];
332 $this->custom_message = $restriction['custom_message'];
333 $this->show_excerpts = $restriction['show_excerpts'];
334 $this->conditions = \ContentControl\remap_conditions_to_query( $restriction['conditions'] );
335
336 $this->query = new Query( $this->conditions );
337
338 ++$index;
339 }
340
341 /**
342 * Check if this set has JS based rules.
343 *
344 * @return bool
345 */
346 public function has_js_rules() {
347 return $this->query->has_js_rules();
348 }
349
350 /**
351 * Check this sets rules.
352 *
353 * @return bool
354 */
355 public function check_rules() {
356 if ( ! $this->query->has_rules() ) {
357 // No rules should be treated as no restrictions.
358 return false;
359 }
360
361 return $this->query->check_rules();
362 }
363
364 /**
365 * Check if this restriction applies to the current user.
366 *
367 * @return bool
368 */
369 public function user_meets_requirements() {
370 return \ContentControl\user_meets_requirements( $this->user_status, $this->user_roles, $this->role_match );
371 }
372
373 /**
374 * Get the description for this restriction.
375 *
376 * @return string
377 */
378 public function get_description() {
379 if ( ! isset( $this->description ) ) {
380 $this->description = get_the_excerpt( $this->id );
381
382 if ( empty( $this->description ) ) {
383 $this->description = __( 'This content is restricted.', 'content-control' );
384 }
385 }
386
387 return $this->description;
388 }
389
390 /**
391 * Get the message for this restriction.
392 *
393 * @uses \get_the_content()
394 * @uses \ContentControl\get_default_denial_message()
395 *
396 * @param string $context Context. 'display' or 'raw'.
397 *
398 * @return string
399 */
400 public function get_message( $context = 'display' ) {
401 if ( ! isset( $this->message ) ) {
402 $message = '';
403
404 if ( ! empty( $this->custom_message ) ) {
405 $message = $this->custom_message;
406 } elseif ( ! empty( $this->post->post_content ) ) {
407 $message = 'display' === $context
408 ? \get_the_content( null, false, $this->id )
409 : $this->post->post_content;
410 }
411
412 $this->message = $message;
413 }
414
415 return sanitize_post_field( 'post_content', $this->message, $this->id, $context );
416 }
417
418 /**
419 * Whether to show excerpts for posts that are restricted.
420 *
421 * @return bool
422 */
423 public function show_excerpts() {
424 return (bool) $this->show_excerpts;
425 }
426
427 /**
428 * Check if this uses the redirect method.
429 *
430 * @return bool
431 */
432 public function uses_redirect_method() {
433 return 'redirect' === $this->protection_method;
434 }
435
436 /**
437 * Check if this uses the replace method.
438 *
439 * @return bool
440 */
441 public function uses_replace_method() {
442 return 'replace' === $this->protection_method;
443 }
444
445 /**
446 * Get edit link.
447 *
448 * @return string
449 */
450 public function get_edit_link() {
451 if ( current_user_can( 'edit_post', $this->id ) ) {
452 return admin_url( 'options-general.php?page=content-control-settings&view=restrictions&edit=' . $this->id );
453 }
454
455 return '';
456 }
457
458 /**
459 * Convert this restriction to an array.
460 *
461 * @return array<string,mixed>
462 */
463 public function to_array() {
464 return [
465 'id' => $this->id,
466 'slug' => $this->slug,
467 'title' => $this->title,
468 'description' => $this->get_description(),
469 'message' => $this->get_message(),
470 'status' => $this->status,
471 'priority' => $this->priority,
472 // Options include logged_in, logged_out.
473 'userStatus' => $this->user_status,
474 // Options include any, match, exclude.
475 'roleMatch' => $this->role_match,
476 'userRoles' => $this->user_roles,
477 'protectionMethod' => $this->protection_method,
478 'redirectType' => $this->redirect_type,
479 'redirectUrl' => $this->redirect_url,
480 'replacementType' => $this->replacement_type,
481 'replacementPage' => $this->replacement_page,
482 'archiveHandling' => $this->archive_handling,
483 'archiveReplacementPage' => $this->archive_replacement_page,
484 'archiveRedirectType' => $this->archive_redirect_type,
485 'archiveRedirectUrl' => $this->archive_redirect_url,
486 'additionalQueryHandling' => $this->additional_query_handling,
487 'overrideMessage' => $this->override_message,
488 'customMessage' => $this->custom_message,
489 'showExcerpts' => $this->show_excerpts,
490 'conditions' => $this->conditions,
491 ];
492 }
493
494 /**
495 * Convert this restriction to a v1 array.
496 *
497 * @return array<string,mixed>
498 */
499 public function to_v1_array() {
500 return [
501 'id' => $this->id,
502 'title' => $this->title,
503 'who' => $this->user_status,
504 'roles' => $this->user_roles,
505 'protection_method' => $this->protection_method,
506 'show_excerpts' => $this->show_excerpts,
507 'override_default_message' => $this->override_message,
508 'custom_message' => $this->custom_message,
509 'redirect_type' => $this->redirect_type,
510 'redirect_url' => $this->redirect_url,
511 'conditions' => $this->conditions,
512 ];
513 }
514 }
515