| @@ -1,7 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * ActivityPub implementation for WordPress/PHP functions either missing from older WordPress/PHP versions or not included by default. |
| 4 | + * | |
| 5 | + * @package Activitypub | |
| 4 | 6 | */ |
| 5 | 7 | |
| 6 | 8 | if ( ! function_exists( 'str_starts_with' ) ) { |
| 7 | 9 | /** |
| @@ -22,28 +24,80 @@ | ||
| 22 | 24 | return 0 === strpos( $haystack, $needle ); |
| 23 | 25 | } |
| 24 | 26 | } |
| 25 | 27 | |
| 26 | -if ( ! function_exists( 'get_self_link' ) ) { | |
| 28 | +if ( ! function_exists( 'str_ends_with' ) ) { | |
| 27 | 29 | /** |
| 28 | - * Returns the link for the currently displayed feed. | |
| 30 | + * Polyfill for `str_ends_with()` function added in PHP 8.0. | |
| 29 | 31 | * |
| 30 | - * @return string Correct link for the atom:self element. | |
| 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. | |
| 31 | 39 | */ |
| 32 | - function get_self_link() { | |
| 33 | - $host = wp_parse_url( home_url() ); | |
| 34 | - $path = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; | |
| 35 | - return esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . $path ) ) ); | |
| 40 | + function str_ends_with( $haystack, $needle ) { | |
| 41 | + return strlen( $needle ) === 0 || substr( $haystack, - strlen( $needle ) ) === $needle; | |
| 36 | 42 | } |
| 37 | 43 | } |
| 38 | 44 | |
| 39 | -if ( ! function_exists( 'is_countable' ) ) { | |
| 45 | +/** | |
| 46 | + * Polyfill for `array_is_list()` function added in PHP 8.1. | |
| 47 | + * | |
| 48 | + * @param array $array The array to check. | |
| 49 | + * | |
| 50 | + * @return bool True if `$array` is a list, otherwise false. | |
| 51 | + */ | |
| 52 | +if ( ! function_exists( 'array_is_list' ) ) { | |
| 40 | 53 | /** |
| 41 | - * Polyfill for `is_countable()` function added in PHP 7.3. | |
| 54 | + * Check if an array is a list. | |
| 42 | 55 | * |
| 43 | - * @param mixed $value The value to check. | |
| 44 | - * @return bool True if `$value` is countable, otherwise false. | |
| 56 | + * An array is considered a list if its keys are a range of numbers | |
| 57 | + * starting from 0 and ending at count( $array ) - 1. | |
| 58 | + * | |
| 59 | + * @param array $input The array to check. | |
| 60 | + * | |
| 61 | + * @return bool True if `$input` is a list, otherwise false. | |
| 45 | 62 | */ |
| 46 | - function is_countable( $value ) { | |
| 47 | - return is_array( $value ) || $value instanceof \Countable; | |
| 63 | + function array_is_list( $input ) { | |
| 64 | + if ( ! is_array( $input ) ) { | |
| 65 | + return false; | |
| 66 | + } | |
| 67 | + | |
| 68 | + if ( array_values( $input ) === $input ) { | |
| 69 | + return true; | |
| 70 | + } | |
| 71 | + | |
| 72 | + $next_key = -1; | |
| 73 | + | |
| 74 | + foreach ( $input as $k => $v ) { | |
| 75 | + if ( ++$next_key !== $k ) { | |
| 76 | + return false; | |
| 77 | + } | |
| 78 | + } | |
| 79 | + | |
| 80 | + return true; | |
| 81 | + } | |
| 82 | +} | |
| 83 | + | |
| 84 | +if ( ! function_exists( 'str_contains' ) ) { | |
| 85 | + /** | |
| 86 | + * Polyfill for `str_contains()` function added in PHP 8.0. | |
| 87 | + * | |
| 88 | + * Performs a case-sensitive check indicating if needle is | |
| 89 | + * contained in haystack. | |
| 90 | + * | |
| 91 | + * @param string $haystack The string to search in. | |
| 92 | + * @param string $needle The substring to search for in the `$haystack`. | |
| 93 | + * | |
| 94 | + * @return bool True if `$needle` is in `$haystack`, otherwise false. | |
| 95 | + */ | |
| 96 | + function str_contains( $haystack, $needle ) { | |
| 97 | + if ( '' === $needle ) { | |
| 98 | + return true; | |
| 99 | + } | |
| 100 | + | |
| 101 | + return false !== strpos( $haystack, $needle ); | |
| 48 | 102 | } |
| 49 | 103 | } |