-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBetterWPAPI.php
More file actions
175 lines (151 loc) · 4.46 KB
/
BetterWPAPI.php
File metadata and controls
175 lines (151 loc) · 4.46 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
declare(strict_types=1);
namespace Snicco\Component\BetterWPAPI;
use InvalidArgumentException;
use WP_User;
use function add_action;
use function add_filter;
use function apply_filters_ref_array;
use function array_unshift;
use function current_user_can;
use function do_action;
use function get_class;
use function get_current_user_id;
use function gettype;
use function is_user_logged_in;
use function remove_filter;
use function wp_cache_delete;
use function wp_cache_get;
use function wp_cache_set;
use function wp_create_nonce;
use function wp_get_current_user;
use function wp_verify_nonce;
/**
* @note No new (public|protected) methods will be added to this class until a next major version.
*/
class BetterWPAPI
{
/**
* @return true
*/
public function addFilter(string $hook_name, callable $callback, int $priority = 10, int $accepted_args = 1): bool
{
return add_filter($hook_name, $callback, $priority, $accepted_args);
}
/**
* @return true
*/
public function addAction(string $hook_name, callable $callback, int $priority = 10, int $accepted_args = 1): bool
{
return add_action($hook_name, $callback, $priority, $accepted_args);
}
/**
* @param mixed ...$args
*/
public function doAction(string $hook_name, ...$args): void
{
do_action($hook_name, ...$args);
}
/**
* @template T
*
* @param T $filterable_value
* @param mixed ...$args
*
* @psalm-return T
*/
public function applyFiltersStrict(string $hook_name, $filterable_value, ...$args)
{
/** @psalm-suppress MixedAssignment */
$return_value = $this->applyFilters($hook_name, $filterable_value, ...$args);
$expected = gettype($filterable_value);
$actual = gettype($return_value);
if ($actual !== $expected) {
throw new InvalidArgumentException(
sprintf('Initial value for filter [%s] is %s. Returned [%s].', $hook_name, $expected, $actual)
);
}
if ('object' === $expected) {
/**
* @var object $filterable_value
* @var object $return_value
*/
$value_class = get_class($filterable_value);
$returned_class = get_class($return_value);
if ($value_class !== $returned_class) {
throw new InvalidArgumentException(
sprintf(
'Initial value for filter [%s] is an instance of [%s]. Returned [%s].',
$hook_name,
$value_class,
$returned_class
)
);
}
}
/**
* @var T $return_value
*/
return $return_value;
}
/**
* @param mixed $value
* @param mixed ...$args
*
* @return mixed
*/
public function applyFilters(string $hook_name, $value, ...$args)
{
array_unshift($args, $value);
return apply_filters_ref_array($hook_name, $args);
}
public function removeFilter(string $hook_name, callable $callback, int $priority = 10): bool
{
return remove_filter($hook_name, $callback, $priority);
}
public function isUserLoggedIn(): bool
{
return is_user_logged_in();
}
public function currentUser(): WP_User
{
return wp_get_current_user();
}
public function currentUserId(): int
{
return get_current_user_id();
}
/**
* @param mixed $args
*/
public function currentUserCan(string $capability, ...$args): bool
{
return current_user_can($capability, ...$args);
}
/**
* @return false|mixed
*/
public function cacheGet(string $key, string $group = '', bool $force = false, bool &$found = null)
{
return wp_cache_get($key, $group, $force, $found);
}
/**
* @param mixed $data
*/
public function cacheSet(string $key, $data, string $group = '', int $expire = 0): bool
{
return wp_cache_set($key, $data, $group, $expire);
}
public function cacheDelete(string $key, string $group = ''): bool
{
return wp_cache_delete($key, $group);
}
public function verifyNonce(string $nonce, string $action): bool
{
return false !== wp_verify_nonce($nonce, $action);
}
public function createNonce(string $form_action): string
{
return wp_create_nonce($form_action);
}
}