| 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( 'is_countable' ) ) { |
| 29 |
/** |
| 30 |
* Polyfill for `is_countable()` function added in PHP 7.3. |
| 31 |
* |
| 32 |
* @param mixed $value The value to check. |
| 33 |
* @return bool True if `$value` is countable, otherwise false. |
| 34 |
*/ |
| 35 |
function is_countable( $value ) { |
| 36 |
return is_array( $value ) || $value instanceof Countable; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Polyfill for `array_is_list()` function added in PHP 7.3. |
| 42 |
* |
| 43 |
* @param array $array The array to check. |
| 44 |
* |
| 45 |
* @return bool True if `$array` is a list, otherwise false. |
| 46 |
*/ |
| 47 |
if ( ! function_exists( 'array_is_list' ) ) { |
| 48 |
/** |
| 49 |
* Check if an array is a list. |
| 50 |
* |
| 51 |
* An array is considered a list if its keys are a range of numbers |
| 52 |
* starting from 0 and ending at count( $array ) - 1. |
| 53 |
* |
| 54 |
* @param array $input The array to check. |
| 55 |
* |
| 56 |
* @return bool True if `$input` is a list, otherwise false. |
| 57 |
*/ |
| 58 |
function array_is_list( $input ) { |
| 59 |
if ( ! is_array( $input ) ) { |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
if ( array_values( $input ) === $input ) { |
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
$next_key = -1; |
| 68 |
|
| 69 |
foreach ( $input as $k => $v ) { |
| 70 |
if ( ++$next_key !== $k ) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
return true; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
if ( ! function_exists( 'str_contains' ) ) { |
| 80 |
/** |
| 81 |
* Polyfill for `str_contains()` function added in PHP 8.0. |
| 82 |
* |
| 83 |
* Performs a case-sensitive check indicating if needle is |
| 84 |
* contained in haystack. |
| 85 |
* |
| 86 |
* @param string $haystack The string to search in. |
| 87 |
* @param string $needle The substring to search for in the `$haystack`. |
| 88 |
* |
| 89 |
* @return bool True if `$needle` is in `$haystack`, otherwise false. |
| 90 |
*/ |
| 91 |
function str_contains( $haystack, $needle ) { |
| 92 |
if ( '' === $needle ) { |
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
return false !== strpos( $haystack, $needle ); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
if ( ! function_exists( 'wp_is_serving_rest_request' ) ) { |
| 101 |
/** |
| 102 |
* Polyfill for `wp_is_serving_rest_request()` function added in WordPress 6.5. |
| 103 |
* |
| 104 |
* @see https://developer.wordpress.org/reference/functions/wp_is_serving_rest_request/ |
| 105 |
* |
| 106 |
* @return bool True if it's a WordPress REST API request, false otherwise. |
| 107 |
*/ |
| 108 |
function wp_is_serving_rest_request() { |
| 109 |
return defined( 'REST_REQUEST' ) && REST_REQUEST; |
| 110 |
} |
| 111 |
} |
| 112 |
|