| 1 |
<?php |
| 2 |
/** |
| 3 |
* Global functions. |
| 4 |
* |
| 5 |
* @package Functions |
| 6 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 7 |
* @since 2.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
if ( ! function_exists('decalog_get_psr_log_version') ) { |
| 11 |
/** |
| 12 |
* Get the needed version of PSR-3. |
| 13 |
* |
| 14 |
* @return int The PSR-3 needed version. |
| 15 |
* @since 4.0.0 |
| 16 |
*/ |
| 17 |
function decalog_get_psr_log_version() { |
| 18 |
$required = 1; |
| 19 |
if ( ! defined( 'DECALOG_PSR_LOG_VERSION') ) { |
| 20 |
define( 'DECALOG_PSR_LOG_VERSION', 'V1' ); |
| 21 |
} |
| 22 |
switch ( strtolower( DECALOG_PSR_LOG_VERSION ) ) { |
| 23 |
case 'v3': |
| 24 |
$required = 3; |
| 25 |
break; |
| 26 |
case 'auto': |
| 27 |
if ( class_exists( '\Psr\Log\NullLogger') ) { |
| 28 |
$reflection = new \ReflectionMethod(\Psr\Log\NullLogger::class, 'log'); |
| 29 |
foreach ( $reflection->getParameters() as $param ) { |
| 30 |
if ( 'message' === $param->getName() ) { |
| 31 |
if ( str_contains($param->getType() ?? '', '|') ) { |
| 32 |
$required = 3; |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
return $required; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Multibyte String Pad |
| 44 |
* |
| 45 |
* Functionally, the equivalent of the standard str_pad function, but is capable of successfully padding multibyte strings. |
| 46 |
* |
| 47 |
* @param string $input The string to be padded. |
| 48 |
* @param int $length The length of the resultant padded string. |
| 49 |
* @param string $padding The string to use as padding. Defaults to space. |
| 50 |
* @param int $padType The type of padding. Defaults to STR_PAD_RIGHT. |
| 51 |
* @param string $encoding The encoding to use, defaults to UTF-8. |
| 52 |
* |
| 53 |
* @return string A padded multibyte string. |
| 54 |
* @since 2.0.0 |
| 55 |
*/ |
| 56 |
function decalog_mb_str_pad( $input, $length, $padding = ' ', $padType = STR_PAD_RIGHT, $encoding = 'UTF-8' ) { |
| 57 |
$result = $input; |
| 58 |
if ( ( $padding_required = $length - mb_strlen( $input, $encoding ) ) > 0 ) { |
| 59 |
switch ( $padType ) { |
| 60 |
case STR_PAD_LEFT: |
| 61 |
$result = |
| 62 |
mb_substr( str_repeat( $padding, $padding_required ), 0, $padding_required, $encoding ) . |
| 63 |
$input; |
| 64 |
break; |
| 65 |
case STR_PAD_RIGHT: |
| 66 |
$result = |
| 67 |
$input . |
| 68 |
mb_substr( str_repeat( $padding, $padding_required ), 0, $padding_required, $encoding ); |
| 69 |
break; |
| 70 |
case STR_PAD_BOTH: |
| 71 |
$left_padding_length = floor( $padding_required / 2 ); |
| 72 |
$right_padding_length = $padding_required - $left_padding_length; |
| 73 |
$result = |
| 74 |
mb_substr( str_repeat( $padding, $left_padding_length ), 0, $left_padding_length, $encoding ) . |
| 75 |
$input . |
| 76 |
mb_substr( str_repeat( $padding, $right_padding_length ), 0, $right_padding_length, $encoding ); |
| 77 |
break; |
| 78 |
} |
| 79 |
} |
| 80 |
return $result; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Multibyte full trim |
| 85 |
* |
| 86 |
* Functionally, the equivalent of the standard str_pad function, but is capable of successfully padding multibyte strings. |
| 87 |
* |
| 88 |
* @param string $input The string to be fully trimed. |
| 89 |
* @param string $replacement Optional. The string replacement. |
| 90 |
* |
| 91 |
* @return string A fully trimed multibyte string. |
| 92 |
* @since 3.6.0 |
| 93 |
*/ |
| 94 |
function decalog_mb_full_trim( $input, $replacement = '' ) { |
| 95 |
return preg_replace( |
| 96 |
"/(\t|\n|\v|\f|\r| |\xC2\x85|\xc2\xa0|\xe1\xa0\x8e|\xe2\x80[\x80-\x8D]|\xe2\x80\xa8|\xe2\x80\xa9|\xe2\x80\xaF|\xe2\x81\x9f|\xe2\x81\xa0|\xe3\x80\x80|\xef\xbb\xbf)+/", |
| 97 |
$replacement, |
| 98 |
$input |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Performs an HTTP request using the PUT method and returns its response. |
| 104 |
* Mimics wp_remote_get or wp_remote_post, but for PUT method. |
| 105 |
* |
| 106 |
* @since 3.0.0 |
| 107 |
* |
| 108 |
* @see wp_remote_request() For more information on the response array format. |
| 109 |
* @see WP_Http::request() For default arguments information. |
| 110 |
* |
| 111 |
* @param string $url URL to retrieve. |
| 112 |
* @param array $args Optional. Request arguments. Default empty array. |
| 113 |
* @return array|WP_Error The response or WP_Error on failure. |
| 114 |
*/ |
| 115 |
function decalog_remote_put( $url, $args = [] ) { |
| 116 |
$http = _wp_http_get_object(); |
| 117 |
if ( isset( $http ) ) { |
| 118 |
$defaults = [ 'method' => 'PUT' ]; |
| 119 |
$parsed_args = wp_parse_args( $args, $defaults ); |
| 120 |
return $http->request( $url, $parsed_args ); |
| 121 |
} |
| 122 |
return new WP_Error( 500 ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Verify if a resource is a shmop resource. |
| 127 |
* |
| 128 |
* @since 3.6.0 |
| 129 |
* |
| 130 |
* @param mixed $value URL to retrieve. |
| 131 |
* @return bool True if it's a shmop resource, false otherwise. |
| 132 |
*/ |
| 133 |
function decalog_is_shmop_resource( $value ) { |
| 134 |
if ( class_exists( 'Shmop' ) ) { |
| 135 |
return $value instanceof Shmop; |
| 136 |
} |
| 137 |
return ( is_resource( $value ) ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Provide a replacement for filter_var() used with FILTER_SANITIZE_STRING flag (was legit with PHP prior to 8.1). |
| 142 |
*/ |
| 143 |
function decalog_filter_string( string $string ) { |
| 144 |
return preg_replace( '/\x00|<[^>]*>?/', '', $string ); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Verify if wp-cli colorization is needed. |
| 149 |
*/ |
| 150 |
function decalog_is_wpcli_colorized() { |
| 151 |
global $argv; |
| 152 |
foreach ( $argv as $arg ) { |
| 153 |
if ( '--NO-COLOR' === strtoupper( $arg ) ) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
if ( '--COLOR' === strtoupper( $arg ) ) { |
| 157 |
return true; |
| 158 |
} |
| 159 |
} |
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Normalize extended fields. |
| 165 |
*/ |
| 166 |
function decalog_normalize_extended_fields( string $extended ) { |
| 167 |
$result = []; |
| 168 |
foreach ( explode ( PHP_EOL, $extended ) as $line ) { |
| 169 |
$pair = explode ( '=', $line ); |
| 170 |
if ( 2 === count( $pair ) ) { |
| 171 |
$result[ trim ( $pair[0] ) ] = is_numeric( $pair[1] ) ? $pair[1] : trim ( $pair[1] ); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
return $result; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Retrieves the translation of $text. |
| 180 |
* |
| 181 |
* This function is a wrapper for the "real" WP function behind this. |
| 182 |
* |
| 183 |
* @since 4.3.0 |
| 184 |
* |
| 185 |
* @param string $text Text to translate. |
| 186 |
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
| 187 |
* Default 'default'. |
| 188 |
* @return string Translated text. |
| 189 |
*/ |
| 190 |
function decalog__( $text, $domain = 'default' ) { |
| 191 |
if ( ! doing_action( 'after_setup_theme' ) && ! did_action( 'after_setup_theme' ) ) { |
| 192 |
return $text; |
| 193 |
} |
| 194 |
return translate( $text, $domain ); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Retrieves the translation of $text and escapes it for safe use in HTML output. |
| 199 |
* |
| 200 |
* This function is a wrapper for the "real" WP function behind this. |
| 201 |
* |
| 202 |
* @since 4.3.0 |
| 203 |
* |
| 204 |
* @param string $text Text to translate. |
| 205 |
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
| 206 |
* Default 'default'. |
| 207 |
* @return string Translated text. |
| 208 |
*/ |
| 209 |
function decalog_esc_html__( $text, $domain = 'default' ) { |
| 210 |
return esc_html( decalog__( $text, $domain ) ); |
| 211 |
} |