| 1 |
<?php |
| 2 |
/** |
| 3 |
* ActivityPub implementation for WordPress/PHP functions either missing from older WordPress/PHP versions or not included by default. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! function_exists( 'str_starts_with' ) ) { |
| 9 |
/** |
| 10 |
* Polyfill for `str_starts_with()` function added in PHP 8.0. |
| 11 |
* |
| 12 |
* Performs a case-sensitive check indicating if |
| 13 |
* the haystack begins with needle. |
| 14 |
* |
| 15 |
* @param string $haystack The string to search in. |
| 16 |
* @param string $needle The substring to search for in the `$haystack`. |
| 17 |
* @return bool True if `$haystack` starts with `$needle`, otherwise false. |
| 18 |
*/ |
| 19 |
function str_starts_with( $haystack, $needle ) { |
| 20 |
if ( '' === $needle ) { |
| 21 |
return true; |
| 22 |
} |
| 23 |
|
| 24 |
return 0 === strpos( $haystack, $needle ); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
if ( ! function_exists( 'str_ends_with' ) ) { |
| 29 |
/** |
| 30 |
* Polyfill for `str_ends_with()` function added in PHP 8.0. |
| 31 |
* |
| 32 |
* Performs a case-sensitive check indicating if |
| 33 |
* the haystack ends with needle. |
| 34 |
* |
| 35 |
* @param string $haystack The string to search in. |
| 36 |
* @param string $needle The substring to search for in the `$haystack`. |
| 37 |
* |
| 38 |
* @return bool True if `$haystack` ends with `$needle`, otherwise false. |
| 39 |
*/ |
| 40 |
function str_ends_with( $haystack, $needle ) { |
| 41 |
return strlen( $needle ) === 0 || substr( $haystack, - strlen( $needle ) ) === $needle; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
if ( ! function_exists( 'is_countable' ) ) { |
| 46 |
/** |
| 47 |
* Polyfill for `is_countable()` function added in PHP 7.3. |
| 48 |
* |
| 49 |
* @param mixed $value The value to check. |
| 50 |
* @return bool True if `$value` is countable, otherwise false. |
| 51 |
*/ |
| 52 |
function is_countable( $value ) { |
| 53 |
return is_array( $value ) || $value instanceof Countable; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Polyfill for `array_is_list()` function added in PHP 7.3. |
| 59 |
* |
| 60 |
* @param array $array The array to check. |
| 61 |
* |
| 62 |
* @return bool True if `$array` is a list, otherwise false. |
| 63 |
*/ |
| 64 |
if ( ! function_exists( 'array_is_list' ) ) { |
| 65 |
/** |
| 66 |
* Check if an array is a list. |
| 67 |
* |
| 68 |
* An array is considered a list if its keys are a range of numbers |
| 69 |
* starting from 0 and ending at count( $array ) - 1. |
| 70 |
* |
| 71 |
* @param array $input The array to check. |
| 72 |
* |
| 73 |
* @return bool True if `$input` is a list, otherwise false. |
| 74 |
*/ |
| 75 |
function array_is_list( $input ) { |
| 76 |
if ( ! is_array( $input ) ) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
if ( array_values( $input ) === $input ) { |
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
$next_key = -1; |
| 85 |
|
| 86 |
foreach ( $input as $k => $v ) { |
| 87 |
if ( ++$next_key !== $k ) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return true; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if ( ! function_exists( 'str_contains' ) ) { |
| 97 |
/** |
| 98 |
* Polyfill for `str_contains()` function added in PHP 8.0. |
| 99 |
* |
| 100 |
* Performs a case-sensitive check indicating if needle is |
| 101 |
* contained in haystack. |
| 102 |
* |
| 103 |
* @param string $haystack The string to search in. |
| 104 |
* @param string $needle The substring to search for in the `$haystack`. |
| 105 |
* |
| 106 |
* @return bool True if `$needle` is in `$haystack`, otherwise false. |
| 107 |
*/ |
| 108 |
function str_contains( $haystack, $needle ) { |
| 109 |
if ( '' === $needle ) { |
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
return false !== strpos( $haystack, $needle ); |
| 114 |
} |
| 115 |
} |
| 116 |
|