2023-10-22 22:20:53 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ActivityPub implementation for WordPress/PHP functions either missing from older WordPress/PHP versions or not included by default.
|
2024-10-09 12:44:17 +00:00
|
|
|
*
|
|
|
|
* @package Activitypub
|
2023-10-22 22:20:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
if ( ! function_exists( 'str_starts_with' ) ) {
|
|
|
|
/**
|
|
|
|
* Polyfill for `str_starts_with()` function added in PHP 8.0.
|
|
|
|
*
|
|
|
|
* Performs a case-sensitive check indicating if
|
|
|
|
* the haystack begins with needle.
|
|
|
|
*
|
|
|
|
* @param string $haystack The string to search in.
|
|
|
|
* @param string $needle The substring to search for in the `$haystack`.
|
|
|
|
* @return bool True if `$haystack` starts with `$needle`, otherwise false.
|
|
|
|
*/
|
|
|
|
function str_starts_with( $haystack, $needle ) {
|
|
|
|
if ( '' === $needle ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0 === strpos( $haystack, $needle );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! function_exists( 'get_self_link' ) ) {
|
|
|
|
/**
|
|
|
|
* Returns the link for the currently displayed feed.
|
|
|
|
*
|
|
|
|
* @return string Correct link for the atom:self element.
|
|
|
|
*/
|
|
|
|
function get_self_link() {
|
|
|
|
$host = wp_parse_url( home_url() );
|
|
|
|
$path = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
|
2024-10-09 12:44:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters the self link.
|
|
|
|
*
|
|
|
|
* @param string $link The self link.
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
return esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . $path ) ) );
|
|
|
|
}
|
|
|
|
}
|
2023-12-08 23:23:11 +00:00
|
|
|
|
|
|
|
if ( ! function_exists( 'is_countable' ) ) {
|
|
|
|
/**
|
|
|
|
* Polyfill for `is_countable()` function added in PHP 7.3.
|
|
|
|
*
|
|
|
|
* @param mixed $value The value to check.
|
|
|
|
* @return bool True if `$value` is countable, otherwise false.
|
|
|
|
*/
|
|
|
|
function is_countable( $value ) {
|
|
|
|
return is_array( $value ) || $value instanceof \Countable;
|
|
|
|
}
|
|
|
|
}
|
2024-02-08 12:31:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Polyfill for `array_is_list()` function added in PHP 7.3.
|
|
|
|
*
|
|
|
|
* @param array $array The array to check.
|
|
|
|
*
|
|
|
|
* @return bool True if `$array` is a list, otherwise false.
|
|
|
|
*/
|
|
|
|
if ( ! function_exists( 'array_is_list' ) ) {
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Check if an array is a list.
|
|
|
|
*
|
|
|
|
* An array is considered a list if its keys are a range of numbers
|
|
|
|
* starting from 0 and ending at count( $array ) - 1.
|
|
|
|
*
|
|
|
|
* @param array $input The array to check.
|
|
|
|
*
|
|
|
|
* @return bool True if `$input` is a list, otherwise false.
|
|
|
|
*/
|
|
|
|
function array_is_list( $input ) {
|
|
|
|
if ( ! is_array( $input ) ) {
|
2024-02-08 12:31:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
if ( array_values( $input ) === $input ) {
|
2024-02-08 12:31:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$next_key = -1;
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
foreach ( $input as $k => $v ) {
|
2024-02-08 12:31:25 +00:00
|
|
|
if ( ++$next_key !== $k ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! function_exists( 'str_contains' ) ) {
|
|
|
|
/**
|
|
|
|
* Polyfill for `str_contains()` function added in PHP 8.0.
|
|
|
|
*
|
|
|
|
* Performs a case-sensitive check indicating if needle is
|
|
|
|
* contained in haystack.
|
|
|
|
*
|
|
|
|
* @param string $haystack The string to search in.
|
|
|
|
* @param string $needle The substring to search for in the `$haystack`.
|
|
|
|
*
|
|
|
|
* @return bool True if `$needle` is in `$haystack`, otherwise false.
|
|
|
|
*/
|
|
|
|
function str_contains( $haystack, $needle ) {
|
|
|
|
if ( '' === $needle ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false !== strpos( $haystack, $needle );
|
|
|
|
}
|
|
|
|
}
|