| 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 |
/** |
| 11 |
* Multibyte String Pad |
| 12 |
* |
| 13 |
* Functionally, the equivalent of the standard str_pad function, but is capable of successfully padding multibyte strings. |
| 14 |
* |
| 15 |
* @param string $input The string to be padded. |
| 16 |
* @param int $length The length of the resultant padded string. |
| 17 |
* @param string $padding The string to use as padding. Defaults to space. |
| 18 |
* @param int $padType The type of padding. Defaults to STR_PAD_RIGHT. |
| 19 |
* @param string $encoding The encoding to use, defaults to UTF-8. |
| 20 |
* |
| 21 |
* @return string A padded multibyte string. |
| 22 |
* @since 2.0.0 |
| 23 |
*/ |
| 24 |
function decalog_mb_str_pad( $input, $length, $padding = ' ', $padType = STR_PAD_RIGHT, $encoding = 'UTF-8' ) { |
| 25 |
$result = $input; |
| 26 |
if ( ( $padding_required = $length - mb_strlen( $input, $encoding ) ) > 0 ) { |
| 27 |
switch ( $padType ) { |
| 28 |
case STR_PAD_LEFT: |
| 29 |
$result = |
| 30 |
mb_substr( str_repeat( $padding, $padding_required ), 0, $padding_required, $encoding ) . |
| 31 |
$input; |
| 32 |
break; |
| 33 |
case STR_PAD_RIGHT: |
| 34 |
$result = |
| 35 |
$input . |
| 36 |
mb_substr( str_repeat( $padding, $padding_required ), 0, $padding_required, $encoding ); |
| 37 |
break; |
| 38 |
case STR_PAD_BOTH: |
| 39 |
$left_padding_length = floor( $padding_required / 2 ); |
| 40 |
$right_padding_length = $padding_required - $left_padding_length; |
| 41 |
$result = |
| 42 |
mb_substr( str_repeat( $padding, $left_padding_length ), 0, $left_padding_length, $encoding ) . |
| 43 |
$input . |
| 44 |
mb_substr( str_repeat( $padding, $right_padding_length ), 0, $right_padding_length, $encoding ); |
| 45 |
break; |
| 46 |
} |
| 47 |
} |
| 48 |
return $result; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Performs an HTTP request using the PUT method and returns its response. |
| 53 |
* Mimics wp_remote_get or wp_remote_post, but for PUT method. |
| 54 |
* |
| 55 |
* @since 3.0.0 |
| 56 |
* |
| 57 |
* @see wp_remote_request() For more information on the response array format. |
| 58 |
* @see WP_Http::request() For default arguments information. |
| 59 |
* |
| 60 |
* @param string $url URL to retrieve. |
| 61 |
* @param array $args Optional. Request arguments. Default empty array. |
| 62 |
* @return array|WP_Error The response or WP_Error on failure. |
| 63 |
*/ |
| 64 |
function decalog_remote_put( $url, $args = [] ) { |
| 65 |
$http = _wp_http_get_object(); |
| 66 |
if ( isset( $http ) ) { |
| 67 |
$defaults = [ 'method' => 'PUT' ]; |
| 68 |
$parsed_args = wp_parse_args( $args, $defaults ); |
| 69 |
return $http->request( $url, $parsed_args ); |
| 70 |
} |
| 71 |
return new WP_Error( 500 ); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
/** |
| 76 |
* Provide PHP 7.3 compatibility for array_key_last() function. |
| 77 |
*/ |
| 78 |
if ( ! function_exists( 'array_key_last' ) ) { |
| 79 |
// phpcs:ignore |
| 80 |
function array_key_last( array $array ) { |
| 81 |
if ( ! empty( $array ) ) { |
| 82 |
return key( array_slice( $array, -1, 1, true ) ); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Provide PHP 7.3 compatibility for array_key_first() function. |
| 89 |
*/ |
| 90 |
if ( ! function_exists( 'array_key_first' ) ) { |
| 91 |
// phpcs:ignore |
| 92 |
function array_key_first( array $arr ) { |
| 93 |
foreach ( $arr as $key => $unused ) { |
| 94 |
return $key; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|