PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / trunk
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More vtrunk
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 / inc / functions / rules.php

rules.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More trunk, at inc/functions/rules.php

140 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Rule callback functions.
4 *
5 * @package ContentControl
6 */
7
8 namespace ContentControl\Rules;
9
10 use ContentControl\Models\RuleEngine\Rule;
11 use function ContentControl\plugin;
12
13 defined( 'ABSPATH' ) || exit;
14
15 /**
16 * Get or set the current rule (globaly accessible).
17 *
18 * @param Rule|null|false $rule Rule object.
19 * @return Rule|null
20 */
21 function current_rule( $rule = false ) {
22 return plugin( 'rules' )->current_rule( $rule );
23 }
24
25 /**
26 * Get the current rule ID.
27 *
28 * @return string
29 */
30 function get_rule_id() {
31 $rule = current_rule();
32
33 return $rule ? $rule->id : '';
34 }
35
36 /**
37 * Get the current rule name.
38 *
39 * @return string
40 */
41 function get_rule_name() {
42 $rule = current_rule();
43
44 return $rule ? $rule->name : '';
45 }
46
47 /**
48 * Get the current rule options.
49 *
50 * @param array<string,mixed> $defaults Default options.
51 *
52 * @return array<string,mixed>
53 */
54 function get_rule_options( $defaults = [] ) {
55 $rule = current_rule();
56
57 $options = $rule ? $rule->options : [];
58
59 return wp_parse_args( $options, $defaults );
60 }
61
62 /**
63 * Get the current rule extras.
64 *
65 * @return array<string,mixed>
66 */
67 function get_rule_extras() {
68 $rule = current_rule();
69
70 return $rule ? $rule->extras : [];
71 }
72
73 /**
74 * Get the current rule option.
75 *
76 * @param string $key Option key.
77 * @param mixed $default_value Default value.
78 * @return mixed
79 */
80 function get_rule_option( $key, $default_value = false ) {
81 $options = get_rule_options();
82
83 return isset( $options[ $key ] ) ? $options[ $key ] : $default_value;
84 }
85
86 /**
87 * Get the current rule extra.
88 *
89 * @param string $key Extra key.
90 * @param mixed $default_value Default value.
91 * @return mixed
92 */
93 function get_rule_extra( $key, $default_value = false ) {
94 $extras = get_rule_extras();
95
96 return isset( $extras[ $key ] ) ? $extras[ $key ] : $default_value;
97 }
98
99 /**
100 * Gets a filterable array of the allowed user roles.
101 *
102 * @return array|mixed
103 */
104 function allowed_user_roles() {
105 static $roles;
106
107 if ( ! isset( $roles ) ) {
108 /**
109 * Filter the allowed user roles.
110 *
111 * @param array $roles
112 *
113 * @return array
114 */
115 $roles = (array) apply_filters( 'content_control/user_roles', wp_roles()->get_names() );
116
117 if ( empty( $roles ) ) {
118 $roles = [];
119 }
120 }
121
122 return $roles;
123 }
124
125 /**
126 * Checks if the current post is a post type.
127 *
128 * @param string $post_type Post type slug.
129 * @return boolean
130 */
131 function is_post_type( $post_type ) {
132 global $post;
133
134 if ( ! is_a( $post, '\WP_Post' ) ) {
135 return false;
136 }
137
138 return is_singular( $post_type ) || $post->post_type === $post_type;
139 }
140