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

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

759 lines 21.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Rule registery
4 *
5 * @package ContentControl
6 */
7
8 namespace ContentControl\RuleEngine;
9
10 defined( 'ABSPATH' ) || exit;
11
12 use ContentControl\Models\RuleEngine\Rule;
13
14 use function ContentControl\plugin;
15
16 /**
17 * Rules registry
18 */
19 class Rules {
20
21 /**
22 * Array of rules.
23 *
24 * @var array<string,array<string,mixed>>
25 */
26 private $data = [];
27
28 /**
29 * Current global rule instance.
30 *
31 * @var Rule
32 */
33 public $current_rule = null;
34
35 /**
36 * Get the current global rule instance.
37 *
38 * @param Rule|null|false $rule Rule instance to set.
39 * @return Rule|null
40 */
41 public function current_rule( $rule = false ) {
42 if ( false === $rule ) {
43 return $this->current_rule;
44 }
45
46 $this->current_rule = $rule;
47 return $rule;
48 }
49
50 /**
51 * Set up rules list.
52 *
53 * @return void
54 */
55 public function init() {
56 // Register rules.
57 $this->register_built_in_rules();
58
59 // Register deprecated rules (using old filter).
60 $this->register_deprecated_rules();
61 }
62
63 /**
64 * Register new rule type.
65 *
66 * @param array<string,mixed> $rule New rule to register.
67 * @return void
68 */
69 public function register_rule( $rule ) {
70 if ( $this->is_rule_valid( $rule ) ) {
71 $rule = wp_parse_args( $rule, $this->get_rule_defaults() );
72
73 /**
74 * Rule index.
75 *
76 * @var string $index
77 */
78 $index = $rule['name'];
79 /**
80 * In the case multiple conditions are registered with the same
81 * identifier, we append an integer. This do/while quickly increases
82 * the integer by one until a valid new key is found.
83 */
84 if ( array_key_exists( $index, $this->data ) ) {
85 $i = 0;
86 do {
87 ++$i;
88 $index = $rule['name'] . '-' . $i;
89 } while ( array_key_exists( $index, $this->data ) );
90 }
91
92 $indexs[] = $index;
93 $this->data[ $index ] = $rule;
94 }
95 }
96
97 /**
98 * Check if rule is valid.
99 *
100 * @param array<string,mixed> $rule Rule to test.
101 * @return boolean
102 */
103 public function is_rule_valid( $rule ) {
104 return is_array( $rule ) && ! empty( $rule );
105 }
106
107 /**
108 * Get array of all registered rules.
109 *
110 * @return array<string,array<string,mixed>>
111 */
112 public function get_rules() {
113 if ( ! did_action( 'content_control/rule_engine/register_rules' ) ) {
114 $this->init();
115 }
116
117 return $this->data;
118 }
119
120 /**
121 * Get a rule definition by name.
122 *
123 * @param string $rule_name Rule definition or null.
124 * @return array<string,mixed>|null
125 */
126 public function get_rule( $rule_name ) {
127 $rules = $this->get_rules();
128 return isset( $rules[ $rule_name ] ) ? $rules[ $rule_name ] : null;
129 }
130
131 /**
132 * Get array of registered rules filtered for the block-editor.
133 *
134 * @return array<string,array<string,mixed>>
135 */
136 public function get_block_editor_rules() {
137 $rules = $this->get_rules();
138
139 /**
140 * Filter the rules.
141 *
142 * @param array $rules Rules.
143 *
144 * @return array
145 */
146 return apply_filters( 'content_control/rule_engine/get_block_editor_rules', $rules );
147 }
148
149 /**
150 * Get list of verbs.
151 *
152 * @return array<string,string> List of verbs with translatable text.
153 */
154 public function get_verbs() {
155 static $verbs;
156
157 if ( isset( $verbs ) ) {
158 return $verbs;
159 }
160
161 $verbs = [
162 'are' => __( 'Are', 'content-control' ),
163 'arenot' => __( 'Are Not', 'content-control' ),
164 'is' => __( 'Is', 'content-control' ),
165 'isnot' => __( 'Is Not', 'content-control' ),
166 'has' => __( 'Has', 'content-control' ),
167 'hasnot' => __( 'Has Not', 'content-control' ),
168 'can' => __( 'Can', 'content-control' ),
169 'cannot' => __( 'Can Not', 'content-control' ),
170 'doesnothave' => __( 'Does Not Have', 'content-control' ),
171 'does' => __( 'Does', 'content-control' ),
172 'doesnot' => __( 'Does Not', 'content-control' ),
173 'was' => __( 'Was', 'content-control' ),
174 'wasnot' => __( 'Was Not', 'content-control' ),
175 'were' => __( 'Were', 'content-control' ),
176 'werenot' => __( 'Were Not', 'content-control' ),
177 ];
178
179 return $verbs;
180 }
181
182 /**
183 * Get a list of common text strings.
184 *
185 * @return array<string,string>
186 */
187 protected function get_common_text_strings() {
188 static $text_strings;
189
190 if ( ! isset( $text_strings ) ) {
191 $text_strings = [
192 'User' => __( 'User', 'content-control' ),
193 'Role(s)' => __( 'Role(s)', 'content-control' ),
194 'Content' => __( 'Content', 'content-control' ),
195 /* translators: %s: Post type plural name */
196 'Select %s' => __( 'Select %s', 'content-control' ),
197 /* translators: %s: Post type singular name */
198 'A Selected %s' => __( 'A Selected %s', 'content-control' ),
199 ];
200 }
201
202 return $text_strings;
203 }
204
205 /**
206 * Get a list of built in rules.
207 *
208 * @return void
209 */
210 protected function register_built_in_rules() {
211 $rules = array_merge(
212 $this->get_user_rules(),
213 $this->get_general_content_rules(),
214 $this->get_post_type_rules(),
215 $this->get_taxonomy_rules()
216 );
217
218 /**
219 * Allow registering additional rules quickly.
220 *
221 * @param array<string,array<string,mixed>> $rules Rules.
222 *
223 * @return array<string,array<string,mixed>>
224 */
225 $rules = apply_filters( 'content_control/rule_engine/rules', $rules );
226
227 foreach ( $rules as $rule ) {
228 $this->register_rule( $rule );
229 }
230
231 /**
232 * Allow manipulating registered rules.
233 *
234 * @param Rules $rules Rules instance.
235 *
236 * @return void
237 */
238 do_action( 'content_control/rule_engine/register_rules', $this );
239 }
240
241 /**
242 * Get a list of user rules.
243 *
244 * @return array<string,array<string,mixed>>
245 */
246 protected function get_user_rules() {
247 $verbs = $this->get_verbs();
248 $strings = $this->get_common_text_strings();
249 return [
250 'user_is_logged_in' => [
251 'name' => 'user_is_logged_in',
252 'label' => __( 'Logged In', 'content-control' ),
253 'context' => [ 'user' ],
254 'category' => $strings['User'],
255 'format' => '{category} {verb} {label}',
256 'verbs' => [ $verbs['is'], $verbs['isnot'] ],
257 'callback' => '\is_user_logged_in',
258 ],
259 'user_has_role' => [
260 'name' => 'user_has_role',
261 'label' => $strings['Role(s)'],
262 'context' => [ 'user' ],
263 'category' => $strings['User'],
264 'format' => '{category} {verb} {label}',
265 'verbs' => [ $verbs['has'], $verbs['doesnothave'] ],
266 'fields' => [
267 'roles' => [
268 'label' => $strings['Role(s)'],
269 'type' => 'tokenselect',
270 'multiple' => true,
271 'options' => wp_roles()->get_names(),
272 ],
273 ],
274 'callback' => '\ContentControl\Rules\user_has_role',
275 ],
276 ];
277 }
278
279 /**
280 * Get a list of general content rules.
281 *
282 * @return array<string,array<string,mixed>>
283 */
284 protected function get_general_content_rules() {
285 $rules = [];
286 $verbs = $this->get_verbs();
287 $strings = $this->get_common_text_strings();
288
289 $rules['entire_site'] = [
290 'name' => 'entire_site',
291 'label' => __( 'Entire Site (Any Page, Post, or Archive)', 'content-control' ),
292 'context' => [ 'content' ],
293 'category' => $strings['Content'],
294 'format' => '{category} {verb} {label}',
295 'verbs' => [ $verbs['is'], $verbs['isnot'] ],
296 'callback' => '__return_true',
297 ];
298
299 $rules['content_is_front_page'] = [
300 'name' => 'content_is_front_page',
301 'label' => __( 'The Home Page', 'content-control' ),
302 'context' => [ 'content' ],
303 'category' => $strings['Content'],
304 'format' => '{category} {verb} {label}',
305 'verbs' => [ $verbs['is'], $verbs['isnot'] ],
306 'callback' => '\ContentControl\Rules\content_is_home_page',
307 ];
308
309 $rules['content_is_blog_index'] = [
310 'name' => 'content_is_blog_index',
311 'label' => __( 'The Blog Index', 'content-control' ),
312 'context' => [ 'content', 'posttype:post' ],
313 'category' => $strings['Content'],
314 'format' => '{category} {verb} {label}',
315 'verbs' => [ $verbs['is'], $verbs['isnot'] ],
316 'callback' => '\ContentControl\Rules\content_is_blog_index',
317 ];
318
319 $rules['content_is_search_results'] = [
320 'name' => 'content_is_search_results',
321 'label' => __( 'A Search Result Page', 'content-control' ),
322 'context' => [ 'content', 'search' ],
323 'category' => $strings['Content'],
324 'format' => '{category} {verb} {label}',
325 'verbs' => [ $verbs['is'], $verbs['isnot'] ],
326 'callback' => '\is_search',
327 ];
328
329 $rules['content_is_404_page'] = [
330 'name' => 'content_is_404_page',
331 'label' => __( 'A 404 Error Page', 'content-control' ),
332 'context' => [ 'content', '404' ],
333 'category' => $strings['Content'],
334 'format' => '{category} {verb} {label}',
335 'verbs' => [ $verbs['is'], $verbs['isnot'] ],
336 'callback' => '\is_404',
337 ];
338
339 return $rules;
340 }
341
342 /**
343 * Get a list of WP post type rules.
344 *
345 * @return array<string,array<string,mixed>>
346 */
347 protected function get_post_type_rules() {
348 $verbs = $this->get_verbs();
349 $strings = $this->get_common_text_strings();
350
351 // Skip post types that are not public.
352 $should_skip_private_pt = ! plugin()->get_option( 'includePrivatePostTypes', false );
353
354 $args = $should_skip_private_pt ? [ 'public' => true ] : [];
355
356 $rules = [];
357 $post_types = get_post_types( $args, 'objects' );
358
359 foreach ( $post_types as $post_type ) {
360 $type_rules = [];
361 $name = $post_type->name;
362
363 if ( $post_type->has_archive || 'post' === $name ) {
364 $type_rules[ "content_is_{$name}_archive" ] = [
365 'name' => "content_is_{$name}_archive",
366 /* translators: %s: Post type singular name */
367 'label' => sprintf( __( 'A %s Archive', 'content-control' ), $post_type->labels->singular_name ),
368 'callback' => '\ContentControl\Rules\content_is_post_type_archive',
369 ];
370 }
371
372 $type_rules[ "content_is_{$name}" ] = [
373 'name' => "content_is_{$name}",
374 /* translators: %s: Post type singular name */
375 'label' => sprintf( __( 'A %s', 'content-control' ), $post_type->labels->singular_name ),
376 'callback' => '\ContentControl\Rules\content_is_post_type',
377 ];
378
379 $type_rules[ "content_is_selected_{$name}" ] = [
380 'name' => "content_is_selected_{$name}",
381 /* translators: %s: Post type singular name */
382 'label' => sprintf( $strings['A Selected %s'], $post_type->labels->singular_name ),
383 'fields' => [
384 'selected' => [
385 /* translators: %s: Post type plurals name */
386 'placeholder' => sprintf( $strings['Select %s'], strtolower( $post_type->labels->name ) ),
387 'type' => 'postselect',
388 'post_type' => $name,
389 'multiple' => true,
390 ],
391 ],
392 'callback' => '\ContentControl\Rules\content_is_selected_post',
393 ];
394
395 $type_rules[ "content_is_{$name}_with_id" ] = [
396 'name' => "content_is_{$name}_with_id",
397 /* translators: %s: Post type singular name */
398 'label' => sprintf( __( 'A %s with ID', 'content-control' ), $post_type->labels->singular_name ),
399 'fields' => [
400 'selected' => [
401 /* translators: %s: Post type singular name */
402 'placeholder' => sprintf( __( '%s IDs: 128, 129', 'content-control' ), strtolower( $post_type->labels->name ) ),
403 'type' => 'text',
404 ],
405 ],
406 'callback' => '\ContentControl\Rules\content_is_selected_post',
407 ];
408
409 if ( is_post_type_hierarchical( $name ) ) {
410 $type_rules[ "content_is_child_of_{$name}" ] = [
411 'name' => "content_is_child_of_{$name}",
412 /* translators: %s: Post type plural name */
413 'label' => sprintf( __( 'A Child of Selected %s', 'content-control' ), $post_type->labels->name ),
414 'fields' => [
415 'selected' => [
416
417 'placeholder' => sprintf( $strings['Select %s'], strtolower( $post_type->labels->name ) ),
418 'type' => 'postselect',
419 'post_type' => $name,
420 'multiple' => true,
421 ],
422 ],
423 'callback' => '\ContentControl\Rules\content_is_child_of_post',
424 ];
425
426 $type_rules[ "content_is_ancestor_of_{$name}" ] = [
427 'name' => "content_is_ancestor_of_{$name}",
428 /* translators: %s: Post type plural name */
429 'label' => sprintf( __( 'An Ancestor of Selected %s', 'content-control' ), $post_type->labels->name ),
430 'fields' => [
431 'selected' => [
432 /* translators: %s: Post type plural name */
433 'placeholder' => sprintf( $strings['Select %s'], strtolower( $post_type->labels->name ) ),
434 'type' => 'postselect',
435 'post_type' => $name,
436 'multiple' => true,
437 ],
438 ],
439 'callback' => '\ContentControl\Rules\content_is_ancestor_of_post',
440 ];
441 }
442
443 if ( 'page' === $name ) {
444 $templates = is_admin() ? wp_get_theme()->get_page_templates() : [];
445
446 if ( ! empty( $templates ) ) {
447 $type_rules[ "content_is_{$name}_with_template" ] = [
448 'name' => "content_is_{$name}_with_template",
449 /* translators: %s: Post type singular name */
450 'label' => sprintf( __( 'A %s With Template', 'content-control' ), $post_type->labels->singular_name ),
451 'fields' => [
452 'selected' => [
453 'type' => 'tokenselect',
454 'multiple' => true,
455 'options' => array_merge( [ 'default' => __( 'Default', 'content-control' ) ], $templates ),
456 ],
457 ],
458 'callback' => '\ContentControl\Rules\content_is_post_with_template',
459 ];
460 }
461 }
462
463 foreach ( $type_rules as $rule ) {
464 // Merge defaults.
465 $type_rules[ $rule['name'] ] = wp_parse_args( $rule, [
466 'category' => $strings['Content'],
467 'context' => [ 'content', "posttype:{$name}" ],
468 'format' => '{category} {verb} {label}',
469 'verbs' => [ $verbs['is'], $verbs['isnot'] ],
470 'fields' => [],
471 'extras' => [
472 'post_type' => $name,
473 ],
474 ] );
475 }
476
477 /**
478 * Allow filtering post type rules.
479 *
480 * @param array<string,array<string,mixed>> $type_rules Post type rules.
481 * @param string $name Post type name.
482 *
483 * @return array<string,array<string,mixed>>
484 */
485 $type_rules = apply_filters( 'content_control/rule_engine/post_type_rules', $type_rules, $name, $post_type );
486
487 // Merge type rules & type tax rules.
488 $rules = array_merge( $rules, $type_rules, $this->get_post_type_tax_rules( $name ) );
489 }
490
491 return $rules;
492 }
493
494 /**
495 * Generate post type taxonomy rules.
496 *
497 * @param string $name Post type name.
498 *
499 * @return array<string,array<string,mixed>>
500 */
501 protected function get_post_type_tax_rules( $name ) {
502 $verbs = $this->get_verbs();
503 $strings = $this->get_common_text_strings();
504
505 $post_type = get_post_type_object( $name );
506 /**
507 * Taxnomies array.
508 *
509 * @var \WP_Taxonomy[]
510 */
511 $taxonomies = get_object_taxonomies( $name, 'objects' );
512 $rules = [];
513
514 // Skip taxonomies that are not public.
515 $should_skip_private_tax = ! plugin()->get_option( 'includePrivateTaxonomies', false );
516
517 foreach ( $taxonomies as $tax_name => $taxonomy ) {
518 if ( $should_skip_private_tax && ! $taxonomy->public ) {
519 continue;
520 }
521
522 $rules[ "content_is_{$name}_with_{$tax_name}" ] = [
523 'name' => "content_is_{$name}_with_{$tax_name}",
524 'label' => sprintf(
525 /* translators: %1$s: Post type singular name, %2$s: Taxonomy singular name */
526 _x( 'A %1$s with %2$s', 'condition: post type plural and taxonomy singular label ie. A Post With Category', 'content-control' ),
527 $post_type->labels->singular_name,
528 $taxonomy->labels->singular_name
529 ),
530 'context' => [ 'content', "posttype:{$name}", "taxonomy:{$tax_name}" ],
531 'category' => $strings['Content'],
532 'format' => '{category} {verb} {label}',
533 'verbs' => [ $verbs['is'], $verbs['isnot'] ],
534 'fields' => [
535 'selected' => [
536 'placeholder' => sprintf(
537 /* translators: %s: Taxonomy singular name */
538 $strings['Select %s'],
539 strtolower( $taxonomy->labels->name )
540 ),
541 'type' => 'taxonomyselect',
542 'taxonomy' => $tax_name,
543 'multiple' => true,
544 ],
545 ],
546 'extras' => [
547 'post_type' => $name,
548 'taxonomy' => $tax_name,
549 ],
550 'callback' => '\ContentControl\Rules\content_is_post_with_tax_term',
551 ];
552 }
553
554 /**
555 * Allow filtering post type taxonomy rules.
556 *
557 * @param array<string,array<string,mixed>> $rules Post type taxonomy rules.
558 * @param string $name Post type name.
559 *
560 * @return array<string,array<string,mixed>>
561 */
562 $rules = apply_filters( 'content_control/rule_engine/post_type_tax_rules', $rules, $name );
563
564 return $rules;
565 }
566
567 /**
568 * Generates conditions for all public taxonomies.
569 *
570 * @return array<string,array<string,mixed>>
571 */
572 protected function get_taxonomy_rules() {
573 // Skip taxonomies that are not public.
574 $should_skip_private_tax = ! plugin()->get_option( 'includePrivateTaxonomies', false );
575
576 $args = $should_skip_private_tax ? [ 'public' => true ] : [];
577
578 $rules = [];
579 $taxonomies = get_taxonomies( $args, 'objects' );
580 $verbs = $this->get_verbs();
581 $strings = $this->get_common_text_strings();
582
583 foreach ( $taxonomies as $tax_name => $taxonomy ) {
584 $tax_defaults = [
585 'category' => $strings['Content'],
586 'context' => [ 'content', "taxonomy:{$tax_name}" ],
587 'format' => '{category} {verb} {label}',
588 'verbs' => [ $verbs['is'], $verbs['isnot'] ],
589 'fields' => [],
590 'extras' => [
591 'taxonomy' => $tax_name,
592 ],
593 ];
594
595 $rules[ "content_is_{$tax_name}_archive" ] = wp_parse_args( [
596 'name' => "content_is_{$tax_name}_archive",
597 /* translators: %s: Taxonomy plural name */
598 'label' => sprintf( _x( 'A %s Archive', 'condition: taxonomy plural label ie. A Category Archive', 'content-control' ), $taxonomy->labels->singular_name ),
599 'callback' => '\ContentControl\Rules\content_is_taxonomy_archive',
600 ], $tax_defaults );
601
602 $rules[ "content_is_selected_tax_{$tax_name}" ] = wp_parse_args( [
603 'name' => "content_is_selected_tax_{$tax_name}",
604 /* translators: %s: Taxonomy plural name */
605 'label' => sprintf( $strings['A Selected %s'], $taxonomy->labels->singular_name ),
606 'fields' => [
607 'selected' => [
608 'placeholder' => sprintf( $strings['Select %s'], strtolower( $taxonomy->labels->name ) ),
609 'type' => 'taxonomyselect',
610 'taxonomy' => $tax_name,
611 'multiple' => true,
612 ],
613 ],
614 'callback' => '\ContentControl\Rules\content_is_selected_term',
615 ], $tax_defaults );
616
617 $rules[ "content_is_tax_{$tax_name}_with_id" ] = wp_parse_args( [
618 'name' => "content_is_tax_{$tax_name}_with_id",
619 /* translators: %s: Taxonomy plural name */
620 'label' => sprintf( _x( 'A %s with ID', 'condition: taxonomy plural label ie. A Category with ID: Selected', 'content-control' ), $taxonomy->labels->name ),
621 'fields' => [
622 'selected' => [
623 /* translators: %s: Taxonomy plural name */
624 'placeholder' => sprintf( _x( '%s IDs: 128, 129', 'condition: taxonomy plural label ie. Category IDs', 'content-control' ), strtolower( $taxonomy->labels->singular_name ) ),
625 'type' => 'text',
626 ],
627 ],
628 'callback' => '\ContentControl\Rules\content_is_selected_term',
629 ], $tax_defaults );
630 }
631
632 return $rules;
633 }
634
635 /**
636 * Get an array of rule default values.
637 *
638 * @return array<string,mixed> Array of rule default values.
639 */
640 public function get_rule_defaults() {
641 static $rule_defaults;
642
643 if ( isset( $rule_defaults ) ) {
644 return $rule_defaults;
645 }
646
647 $verbs = $this->get_verbs();
648 $strings = $this->get_common_text_strings();
649
650 $rule_defaults = [
651 'name' => '',
652 'label' => '',
653 'context' => [],
654 'category' => $strings['Content'],
655 'format' => '{category} {verb} {label}',
656 'verbs' => [ $verbs['is'], $verbs['isnot'] ],
657 'fields' => [],
658 'callback' => null,
659 'frontend' => false,
660 ];
661
662 return $rule_defaults;
663 }
664
665 /**
666 * Register & remap deprecated conditions to rules.
667 *
668 * @return void
669 */
670 protected function register_deprecated_rules() {
671 /**
672 * Filters the old conditions to be registered as rules.
673 *
674 * @deprecated 2.0.0
675 *
676 * @param array $old_rules Array of old rules to manipulate.
677 *
678 * @return array
679 */
680 $old_rules = apply_filters( 'content_control/rule_engine/deprecated_rules', [] );
681
682 if ( ! empty( $old_rules ) ) {
683 $old_rules = $this->parse_old_rules( $old_rules );
684
685 foreach ( $old_rules as $rule ) {
686 $rule['deprecated'] = true;
687 $this->register_rule( $rule );
688 }
689 }
690 }
691
692 /**
693 * Parse rules that are still registered using the older deprecated methods.
694 *
695 * @param array<string,mixed> $old_rules Array of old rules to manipulate.
696 * @return array<string,mixed>
697 */
698 public function parse_old_rules( $old_rules ) {
699 $new_rules = [];
700
701 foreach ( $old_rules as $key => $old_rule ) {
702 $new_rules[ $key ] = $this->remap_old_rule( $old_rule );
703 }
704
705 return $new_rules;
706 }
707
708 /**
709 * Remaps keys & values from an old `condition` into a new `rule`.
710 *
711 * @param array<string,mixed> $old_rule Old rule definition.
712 * @return array<string,mixed> New rule definition.
713 */
714 public function remap_old_rule( $old_rule ) {
715 $old_rule = wp_parse_args( $old_rule, $this->get_old_rule_defaults() );
716
717 $new_rule = [
718 'format' => '{label}',
719 ];
720
721 $remaped_keys = [
722 'id' => 'name',
723 'name' => 'label',
724 'group' => 'category',
725 'fields' => 'fields',
726 'callback' => 'callback',
727 'advanced' => 'frontend',
728 'priority' => 'priority',
729 ];
730
731 foreach ( $remaped_keys as $old_key => $new_key ) {
732 if ( isset( $old_rule[ $old_key ] ) ) {
733 $new_rule[ $new_key ] = $old_rule[ $old_key ];
734 unset( $old_rule[ $old_key ] );
735 }
736 }
737
738 // Merge any leftover 'unknonw' keys, with new stuff second.
739 return array_merge( $new_rule, $old_rule );
740 }
741
742 /**
743 * Get an array of old rule default values.
744 *
745 * @return array<string,mixed> Array of old rule default values.
746 */
747 private function get_old_rule_defaults() {
748 return [
749 'id' => '',
750 'callback' => null,
751 'group' => '',
752 'name' => '',
753 'priority' => 10,
754 'fields' => [],
755 'advanced' => false,
756 ];
757 }
758 }
759