-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreload.php
More file actions
79 lines (69 loc) · 2.42 KB
/
Copy pathpreload.php
File metadata and controls
79 lines (69 loc) · 2.42 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
<?php
/**
* Unit tests preload
*
* @package Mantle
*/
namespace Mantle\Testing;
if ( ! function_exists( __NAMESPACE__ . '\\tests_add_filter' ) ) {
/**
* Adds hooks before loading WP.
*
* @see add_filter()
*
* @param string $tag The name of the filter to hook the $function_to_add callback to.
* @param callable $function_to_add The callback to be run when the filter is applied.
* @param int $priority Optional. Used to specify the order in which the functions
* associated with a particular action are executed.
* Lower numbers correspond with earlier execution,
* and functions with the same priority are executed
* in the order in which they were added to the action. Default 10.
* @param int $accepted_args Optional. The number of arguments the function accepts. Default 1.
* @return true
*/
function tests_add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ): bool {
global $wp_filter;
if ( function_exists( 'add_filter' ) ) {
add_filter( $tag, $function_to_add, $priority, $accepted_args );
} else {
$idx = _test_filter_build_unique_id( $tag, $function_to_add );
// phpcs:ignore WordPress.WP.GlobalVariablesOverride
$wp_filter[ $tag ][ $priority ][ $idx ] = [
'function' => $function_to_add,
'accepted_args' => $accepted_args,
];
}
return true;
}
}
if ( ! function_exists( __NAMESPACE__ . '\\_test_filter_build_unique_id' ) ) {
/**
* Generates a unique function ID based on the given arguments.
*
* @see _wp_filter_build_unique_id()
*
* @param string $tag Unused. The name of the filter to build ID for.
* @param callable $function The function to generate ID for.
* @return string Unique function ID for usage as array key.
*/
function _test_filter_build_unique_id( $tag, $function ): ?string {
if ( is_string( $function ) ) {
return $function;
}
if ( is_object( $function ) ) {
// Closures are currently implemented as objects.
$function = [ $function, '' ];
} else {
$function = (array) $function;
}
if ( is_object( $function[0] ) ) {
// Object class calling.
return spl_object_hash( $function[0] ) . $function[1];
}
if ( is_string( $function[0] ) ) {
// Static calling.
return $function[0] . '::' . $function[1];
}
return null;
}
}