-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore-polyfill.php
More file actions
105 lines (96 loc) · 3.27 KB
/
Copy pathcore-polyfill.php
File metadata and controls
105 lines (96 loc) · 3.27 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
<?php
/**
* Functions from the core unit test library that help with testing.
*
* phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound,
*
* @package Mantle
*/
if ( ! function_exists( 'rand_str' ) ) :
/**
* Provide a random string.
*
* @param int $len String length.
*/
function rand_str( $len = 32 ): string {
return substr( md5( uniqid( (string) wp_rand() ) ), 0, $len );
}
endif;
if ( ! function_exists( 'rand_long_str' ) ) :
/**
* Returns a string of the required length containing random characters.
*
* @param int $length The required length.
* @return string The string.
*/
function rand_long_str( $length ): string {
$chars = 'abcdefghijklmnopqrstuvwxyz';
$string = '';
for ( $i = 0; $i < $length; $i++ ) {
$rand = random_int( 0, strlen( $chars ) - 1 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.rand_rand
$string .= substr( $chars, $rand, 1 );
}
return $string;
}
endif;
if ( ! function_exists( 'strip_ws' ) ) :
/**
* Strips leading and trailing whitespace from each line in the string.
*
* @param string $txt The text.
* @return string Text with line-leading and line-trailing whitespace stripped.
*/
function strip_ws( $txt ): string {
$lines = explode( "\n", $txt );
$result = [];
foreach ( $lines as $line ) {
if ( trim( $line ) !== '' && trim( $line ) !== '0' ) {
$result[] = trim( $line );
}
}
return trim( implode( "\n", $result ) );
}
endif;
if ( ! function_exists( 'ensure_trailingslash' ) ) :
/**
* Appends a trailing slash.
*
* @param string $string String to append a trailing slash to.
*/
function ensure_trailingslash( $string ): string {
return remove_trailingslash( $string ) . '/';
}
endif;
if ( ! function_exists( 'remove_trailingslash' ) ) :
/**
* Removes trailing forward slashes and backslashes if they exist.
*
* @param string $string What to remove the trailing slashes from.
* @return string String without the trailing slashes.
*/
function remove_trailingslash( $string ): string {
return rtrim( $string, '/\\' );
}
endif;
if ( ! function_exists( 'tests_add_filter' ) ) :
/**
* Polyfill for providing a root namespace tests_add_filter() function for
* easier transition to the testing framework.
*
* @see \Mantle\Testing\tests_add_filter()
* @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 {
return \Mantle\Testing\tests_add_filter( $tag, $function_to_add, $priority, $accepted_args );
}
endif;