-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFunctions.php
More file actions
589 lines (493 loc) · 17.7 KB
/
Functions.php
File metadata and controls
589 lines (493 loc) · 17.7 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
<?php
declare(strict_types=1);
namespace Inpsyde\ObjectHooksRemover;
/** @internal */
abstract class Functions
{
/**
* @param string $hook
* @param string $targetClassName
* @param string|null $targetMethodName
* @param int|null $targetPriority
* @param bool $removeStaticCallbacks
* @return int Number of removed hooks.
*/
final public static function removeObjectHook(
string $hook,
string $targetClassName,
?string $targetMethodName = null,
?int $targetPriority = null,
bool $removeStaticCallbacks = false
): int {
$callbacks = static::objectCallbacksForHook($hook, $targetPriority);
if ($callbacks === []) {
return 0;
}
$removed = 0;
foreach ($callbacks as [$idx, $targetPriority, $object, $class, $method]) {
$objectMatch = $removeStaticCallbacks || is_object($object);
if (
($objectMatch && static::matchObjectClass($class, $targetClassName))
&& (($targetMethodName === null) || ($method === $targetMethodName))
) {
remove_filter($hook, $idx, $targetPriority);
$removed++;
}
}
return $removed;
}
/**
* @param string $hook
* @param string $targetClassName
* @param string|null $targetMethodName
* @param int|null $targetPriority
* @return int
*/
final public static function removeStaticMethodHook(
string $hook,
string $targetClassName,
?string $targetMethodName = null,
?int $targetPriority = null
): int {
$callbacks = static::objectCallbacksForHook($hook, $targetPriority);
if ($callbacks === []) {
return 0;
}
$removed = 0;
foreach ($callbacks as [$idx, $targetPriority, $object, $class, $method]) {
if (
is_null($object)
&& static::matchObjectClass($class, $targetClassName, true)
&& (($targetMethodName === null) || ($method === $targetMethodName))
) {
remove_filter($hook, $idx, $targetPriority);
$removed++;
}
}
return $removed;
}
/**
* @param string $hook Action or filter name to remove.
* @param object $targetObject Object instance of the hook callbacks to remove.
* @param int|null $targetPriority Priority to target, null to target all of them.
* @return int Number of removed hooks.
*/
final public static function removeInstanceHook(
string $hook,
object $targetObject,
?int $targetPriority = null
): int {
$callbacks = static::objectCallbacksForHook($hook, $targetPriority);
if ($callbacks === []) {
return 0;
}
$removed = 0;
foreach ($callbacks as [$idx, $targetPriority, $object]) {
if ($object === $targetObject) {
remove_filter($hook, $idx, $targetPriority);
$removed++;
}
}
return $removed;
}
/**
* @param string $hook Action or filter name to remove.
* @param string|object|null|false $targetThis Used to filter closures based on `$this` context.
* @param array|null $targetArgs Used to filter closures based on declared arguments.
* @param int|null $targetPriority Priority to target, null to target all of them.
* @return int Number of removed hooks.
*/
final public static function removeClosureHook(
string $hook,
$targetThis = null,
?array $targetArgs = null,
?int $targetPriority = null
): int {
$callbacks = static::objectCallbacksForHook($hook, $targetPriority);
if ($callbacks === []) {
return 0;
}
if (($targetThis === null) && ($targetArgs === null)) {
return static::removeObjectHook($hook, \Closure::class, '__invoke', $targetPriority);
}
$removed = 0;
foreach ($callbacks as [$idx, $targetPriority, $object, $class]) {
/** @psalm-suppress InvalidArgument */
if (
($class === \Closure::class)
&& static::matchClosure($object, $targetThis, $targetArgs)
) {
remove_filter($hook, $idx, $targetPriority);
$removed++;
}
}
return $removed;
}
/**
* @param mixed $object
* @param bool|null $removeStaticCallbacks
* @return int
*/
final public static function removeAllObjectHooks($object, ?bool $removeStaticCallbacks): int
{
global $wp_filter;
if (!is_array($wp_filter)) {
return 0;
}
$isClass = ($object !== 'object') && self::isClassLikeString($object);
if (!is_object($object) && !$isClass) {
return 0;
}
/** @var class-string|object $object */
$removed = 0;
foreach (array_keys($wp_filter) as $hook) {
if (!is_string($hook)) {
continue;
}
$removed += static::removeAllObjectCallbacks(
$hook,
$object,
$isClass,
$removeStaticCallbacks
);
}
return $removed;
}
/**
* @param string $hook
* @param class-string|object $object
* @param bool $isClass
* @param bool|null $removeStaticCallbacks
* @return int
*/
private static function removeAllObjectCallbacks(
string $hook,
$object,
bool $isClass,
?bool $removeStaticCallbacks
): int {
$targetClass = $isClass ? $object : null;
$removeStaticCallbacks ??= $isClass;
$callbacks = static::objectCallbacksForHook($hook);
$removed = 0;
foreach ($callbacks as [$id, $priority, $callbackObject, $callbackClass]) {
$isStatic = $callbackObject === null;
if ($isStatic && !$removeStaticCallbacks) {
continue;
}
/** @psalm-suppress PossiblyInvalidArgument */
$isStatic and $targetClass ??= get_class($object);
$matched = $isStatic
? ($targetClass === $callbackClass)
: ($object === ($isClass ? $callbackClass : $callbackObject));
if ($matched) {
$removed++;
remove_filter($hook, $id, $priority);
}
}
return $removed;
}
/**
* @param string $hook
* @param int|null $targetPriority
* @return list<list{non-empty-string, int, object|null, class-string, non-empty-string}>
*
* phpcs:disable Generic.Metrics.CyclomaticComplexity
*/
private static function objectCallbacksForHook(string $hook, ?int $targetPriority = null): array
{
// phpcs:enable Generic.Metrics.CyclomaticComplexity
if ($hook === '') {
return [];
}
global $wp_filter;
$callbacksGroup = is_array($wp_filter) ? ($wp_filter[$hook] ?? []) : [];
$allCallbacks = [];
// This is not for old WP compat, but in case this is called *very* early, before WP_Hook is
// even loaded.
if ($callbacksGroup instanceof \WP_Hook) {
$allCallbacks = $callbacksGroup->callbacks;
} elseif (is_array($callbacksGroup)) {
$allCallbacks = $callbacksGroup;
}
/** @psalm-suppress DocblockTypeContradiction */
if (($allCallbacks === []) || !is_array($allCallbacks)) {
return [];
}
$targetCallbacks = ($targetPriority === null)
? $allCallbacks
: [$targetPriority => ($allCallbacks[$targetPriority] ?? [])];
/** @psalm-suppress TypeDoesNotContainType */
if (($targetCallbacks === []) || !is_array($targetCallbacks)) {
return [];
}
$all = [];
foreach ($targetCallbacks as $priority => $callbacks) {
if (is_array($callbacks)) {
$all = static::collectObjectCallbacksByPriority($callbacks, (int) $priority, $all);
}
}
return $all;
}
/**
* @param array $callbacks
* @param int $priority
* @param list<list{non-empty-string, int, object|null, class-string, non-empty-string}> $all
* @return list<list{non-empty-string, int, object|null, class-string, non-empty-string}>
*/
private static function collectObjectCallbacksByPriority(
array $callbacks,
int $priority,
array $all = []
): array {
foreach ($callbacks as $callback) {
[$id, $targetThis, $class, $method] = static::parseCallbackData($callback);
if (($class !== '') && ($id !== '') && ($method !== '')) {
$all[] = [$id, $priority, $targetThis, $class, $method];
}
}
return $all;
}
/**
* @param mixed $callbackData
* @return list{string, object|null, class-string|"", string}
*/
private static function parseCallbackData($callbackData): array
{
if (!is_array($callbackData)) {
return ['', null, '', ''];
}
$callback = $callbackData['function'] ?? null;
// 2nd param `true` means syntax-only check
if (!is_callable($callback, true)) {
return ['', null, '', ''];
}
if (is_string($callback)) {
if (substr_count($callback, '::') !== 1) {
// We are not interested in plain string function names
return ['', null, '', ''];
}
// Static method passed as string
$callback = explode('::', $callback);
}
$isInvokable = is_object($callback);
/** @var list{string|object, string}|object $callback */
/**
* `is_callable($cb, true)` returns `true` for empty class or empty method.
* Moreover, we catch here things like "Foo::", "::foo", or "::" passed as callback.
* @psalm-suppress PossiblyInvalidArrayAccess
*/
if (!$isInvokable && (($callback[0] === '') || (($callback[1] === '')))) {
return ['', null, '', ''];
}
/*
* 1st and 3rd params, `$tag` and `$priority, are required by `_wp_filter_build_unique_id()`
* for historical reasons, when WP supported PHP versions where `spl_object_hash()` could
* be unavailable. In modern WP, those params are there for backward compat, but not used.
*/
$callbackId = _wp_filter_build_unique_id('', $callback, 0);
$isInvokable and $callback = [$callback, '__invoke'];
/** @var list{class-string|object, non-empty-string} $callback */
return is_object($callback[0])
? [$callbackId, $callback[0], get_class($callback[0]), $callback[1]]
: [$callbackId, null, $callback[0], $callback[1]];
}
/**
* @param mixed $thing
* @return bool
*
* @psalm-assert-if-true class-string|"object" $thing
*/
private static function isClassLikeString($thing): bool
{
if (($thing === '') || !is_string($thing)) {
return false;
}
if (($thing === 'object') || class_exists($thing) || interface_exists($thing)) {
return true;
}
if (PHP_VERSION_ID < 801000) {
return false;
}
/**
* @psalm-suppress UndefinedFunction
* @var true
* phpcs:disable PHPCompatibility.FunctionUse.NewFunctions.enum_existsFound
*/
return enum_exists($thing);
}
/**
* @param mixed $targetObject
* @param string $targetClass
* @param bool $exactMatch
* @return bool
*/
private static function matchObjectClass(
$targetObject,
string $targetClass,
bool $exactMatch = false
): bool {
$isStringTarget = static::isClassLikeString($targetObject);
if (
(!$isStringTarget && !is_object($targetObject))
|| (($targetClass !== '@anonymous') && !static::isClassLikeString($targetClass))
) {
return false;
}
/**
* @var class-string|"object"|"stdClass" $objectClass
* @var class-string|"object"|"@anonymous" $targetClass
*/
$objectClass = $isStringTarget ? $targetObject : get_class($targetObject);
if ($targetClass === 'object') {
return !$exactMatch || ($objectClass === 'stdClass');
}
if ($targetClass === '@anonymous') {
return strpos($objectClass, 'class@anonymous') === 0;
}
return $exactMatch
? ($targetClass === $objectClass)
: is_a($objectClass, $targetClass, true);
}
/**
* @param \Closure $closure
* @param mixed $targetThis
* @param array|null $targetArgs
* @return bool True when there's a match.
*/
private static function matchClosure(
\Closure $closure,
$targetThis = null,
?array $targetArgs = null
): bool {
if (($targetThis === null) && ($targetArgs === null)) {
return true;
}
// Ensures `$targetThis` is either `null`, `false`, an object, or a class/interface name
$isObjectTarget = is_object($targetThis);
$isClassTarget = !$isObjectTarget && static::isClassLikeString($targetThis);
if (
!$isObjectTarget
&& !$isClassTarget
&& ($targetThis !== null)
&& ($targetThis !== false)
) {
return false;
}
$reflection = new \ReflectionFunction($closure);
$closureThis = $reflection->getClosureThis();
if (($targetThis === false) && ($closureThis !== null)) {
return false;
} elseif ($isObjectTarget && ($closureThis !== $targetThis)) {
// If `$targetThis` was provided as object it must be same instance of closure's $this
return false;
} elseif ($isClassTarget && !static::matchObjectClass($closureThis, $targetThis)) {
// If `$targetThis` was provided as class name in must match closure's $this
return false;
}
// If `$targetThis` check above passed and there's no `$targetArgs`, it matched all
if ($targetArgs === null) {
return true;
}
$closureParams = $reflection->getParameters();
if (($closureParams === []) || ($targetArgs === [])) {
return $closureParams === $targetArgs;
}
return static::matchClosureParams($closureParams, $targetArgs);
}
/**
* @param non-empty-array<int, \ReflectionParameter> $params
* @param array $targetArgs
* @return bool
*/
private static function matchClosureParams(array $params, array $targetArgs): bool
{
[$ignoreType, $targetArgs] = static::normalizeTargetArgsList($targetArgs);
if ($targetArgs === []) {
return false;
}
if (count($params) !== count($targetArgs)) {
return false;
}
$index = 0;
foreach ($targetArgs as $targetArgName => $targetArgType) {
$param = $params[$index];
if ("\${$param->name}" !== $targetArgName) {
return false;
}
if (!$ignoreType && !static::matchParamType($param, $targetArgType)) {
return false;
}
$index++;
}
return true;
}
/**
* @param array $array
* @return list{bool, array<non-empty-string, non-empty-string>}
*
* phpcs:disable SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
*/
private static function normalizeTargetArgsList(array $array): array
{
// phpcs:enable Inpsyde.CodeQuality.NestingLevel
$mode = 'names';
$i = -1;
$normalized = [];
foreach ($array as $key => $value) {
$i++;
$name = $value;
if ($key !== $i) {
if (($i > 0) && ($mode === 'names')) {
return [false, []];
}
$mode = 'types';
$name = $key;
}
if (!is_string($key) && ($mode === 'types')) {
return [false, []];
}
if (
($name === '')
|| ($name === '$')
|| !is_string($name)
|| (strpos($name, '$') !== 0)
) {
return [false, []];
}
if ($mode === 'types') {
($value === null) and $value = 'mixed';
if (($value === '') || !is_string($value)) {
return [false, []];
}
}
/**
* @var non-empty-string $name
* @var non-empty-string $value
*/
$normalized[$name] = ($mode === 'names') ? 'mixed' : $value;
}
return [$mode === 'names', $normalized];
}
/**
* @param \ReflectionParameter $param
* @param non-empty-string $targetArgType
* @return bool
*/
private static function matchParamType(\ReflectionParameter $param, string $targetArgType): bool
{
$paramType = $param->getType();
if ($paramType === null) {
// If declaration parameter has no type, given argument must be "mixed".
return $targetArgType === 'mixed';
}
$targetArgType = ltrim($targetArgType, '\\');
if (PHP_MAJOR_VERSION === 7) {
/** @psalm-suppress UndefinedMethod */
$paramTypeName = $paramType->getName();
$paramType->allowsNull() and $paramTypeName = "?{$paramTypeName}";
return $paramTypeName === $targetArgType;
}
return (string) $paramType === $targetArgType;
}
}