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