| 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( 'get_self_link' ) ) { |
| 29 |
/** |
| 30 |
* Returns the link for the currently displayed feed. |
| 31 |
* |
| 32 |
* @return string Correct link for the atom:self element. |
| 33 |
*/ |
| 34 |
function get_self_link() { |
| 35 |
$host = wp_parse_url( home_url() ); |
| 36 |
$path = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; |
| 37 |
|
| 38 |
/** |
| 39 |
* Filters the self link. |
| 40 |
* |
| 41 |
* @param string $link The self link. |
| 42 |
*/ |
| 43 |
return esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . $path ) ) ); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
if ( ! function_exists( 'is_countable' ) ) { |
| 48 |
/** |
| 49 |
* Polyfill for `is_countable()` function added in PHP 7.3. |
| 50 |
* |
| 51 |
* @param mixed $value The value to check. |
| 52 |
* @return bool True if `$value` is countable, otherwise false. |
| 53 |
*/ |
| 54 |
function is_countable( $value ) { |
| 55 |
return is_array( $value ) || $value instanceof \Countable; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Polyfill for `array_is_list()` function added in PHP 7.3. |
| 61 |
* |
| 62 |
* @param array $array The array to check. |
| 63 |
* |
| 64 |
* @return bool True if `$array` is a list, otherwise false. |
| 65 |
*/ |
| 66 |
if ( ! function_exists( 'array_is_list' ) ) { |
| 67 |
/** |
| 68 |
* Check if an array is a list. |
| 69 |
* |
| 70 |
* An array is considered a list if its keys are a range of numbers |
| 71 |
* starting from 0 and ending at count( $array ) - 1. |
| 72 |
* |
| 73 |
* @param array $input The array to check. |
| 74 |
* |
| 75 |
* @return bool True if `$input` is a list, otherwise false. |
| 76 |
*/ |
| 77 |
function array_is_list( $input ) { |
| 78 |
if ( ! is_array( $input ) ) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
if ( array_values( $input ) === $input ) { |
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
$next_key = -1; |
| 87 |
|
| 88 |
foreach ( $input as $k => $v ) { |
| 89 |
if ( ++$next_key !== $k ) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
return true; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
if ( ! function_exists( 'str_contains' ) ) { |
| 99 |
/** |
| 100 |
* Polyfill for `str_contains()` function added in PHP 8.0. |
| 101 |
* |
| 102 |
* Performs a case-sensitive check indicating if needle is |
| 103 |
* contained in haystack. |
| 104 |
* |
| 105 |
* @param string $haystack The string to search in. |
| 106 |
* @param string $needle The substring to search for in the `$haystack`. |
| 107 |
* |
| 108 |
* @return bool True if `$needle` is in `$haystack`, otherwise false. |
| 109 |
*/ |
| 110 |
function str_contains( $haystack, $needle ) { |
| 111 |
if ( '' === $needle ) { |
| 112 |
return true; |
| 113 |
} |
| 114 |
|
| 115 |
return false !== strpos( $haystack, $needle ); |
| 116 |
} |
| 117 |
} |
| 118 |
|