generated from Pierre-Lannoy/wp-plugin-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
109 lines (105 loc) · 3.02 KB
/
functions.php
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
<?php
/**
* Global functions.
*
* @package Functions
* @author Pierre Lannoy <https://pierre.lannoy.fr/>.
* @since 2.0.0
*/
if ( ! function_exists('decalog_get_psr_log_version') ) {
/**
* Get the needed version of PSR-3.
*
* @return int The PSR-3 needed version.
* @since 4.0.0
*/
function decalog_get_psr_log_version() {
$required = 1;
if ( ! defined( 'DECALOG_PSR_LOG_VERSION') ) {
define( 'DECALOG_PSR_LOG_VERSION', 'V1' );
}
switch ( strtolower( DECALOG_PSR_LOG_VERSION ) ) {
case 'v3':
$required = 3;
break;
case 'auto':
if ( class_exists( '\Psr\Log\NullLogger') ) {
$reflection = new \ReflectionMethod(\Psr\Log\NullLogger::class, 'log');
foreach ( $reflection->getParameters() as $param ) {
if ( 'message' === $param->getName() ) {
if ( str_contains($param->getType() ?? '', '|') ) {
$required = 3;
}
}
}
}
}
return $required;
}
}
/**
* Multibyte String Pad
*
* Functionally, the equivalent of the standard str_pad function, but is capable of successfully padding multibyte strings.
*
* @param string $input The string to be padded.
* @param int $length The length of the resultant padded string.
* @param string $padding The string to use as padding. Defaults to space.
* @param int $padType The type of padding. Defaults to STR_PAD_RIGHT.
* @param string $encoding The encoding to use, defaults to UTF-8.
*
* @return string A padded multibyte string.
* @since 2.0.0
*/
function traffic_mb_str_pad( $input, $length, $padding = ' ', $padType = STR_PAD_RIGHT, $encoding = 'UTF-8' ) {
$result = $input;
if ( ( $padding_required = $length - mb_strlen( $input, $encoding ) ) > 0 ) {
switch ( $padType ) {
case STR_PAD_LEFT:
$result =
mb_substr( str_repeat( $padding, $padding_required ), 0, $padding_required, $encoding ) .
$input;
break;
case STR_PAD_RIGHT:
$result =
$input .
mb_substr( str_repeat( $padding, $padding_required ), 0, $padding_required, $encoding );
break;
case STR_PAD_BOTH:
$left_padding_length = floor( $padding_required / 2 );
$right_padding_length = $padding_required - $left_padding_length;
$result =
mb_substr( str_repeat( $padding, $left_padding_length ), 0, $left_padding_length, $encoding ) .
$input .
mb_substr( str_repeat( $padding, $right_padding_length ), 0, $right_padding_length, $encoding );
break;
}
}
return $result;
}
/**
* Close a shmop resource.
*
* @since 2.8.0
*
* @param mixed $shmop The shmop resource to close.
*/
function traffic_shmop_close( $shmop ){
if ( defined( 'PHP_VERSION' ) && version_compare( PHP_VERSION, '8.0.0', '<' ) ) {
shmop_close( $shmop );
}
}
/**
* Verify if a resource is a shmop resource.
*
* @since 2.8.0
*
* @param mixed $value URL to retrieve.
* @return bool True if it's a shmop resource, false otherwise.
*/
function traffic_is_shmop_resource( $value ) {
if ( class_exists( 'Shmop' ) ) {
return $value instanceof Shmop;
}
return ( is_resource( $value ) );
}