PHP 8.1 compatibility (#533)

* PHP 8.1 compatibility

* Update compat.php

---------

Co-authored-by: Matthias Pfefferle <pfefferle@users.noreply.github.com>
This commit is contained in:
Matt Wiebe 2023-10-25 01:44:04 -05:00 committed by GitHub
parent 8078512b8c
commit 53adfe6b80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 19 additions and 5 deletions

View file

@ -35,3 +35,15 @@ if ( ! function_exists( 'get_self_link' ) ) {
return esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . $path ) ) );
}
}
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;
}
}

View file

@ -105,7 +105,7 @@ class Collection {
'@context' => Activity::CONTEXT,
'id' => get_rest_url_by_path( sprintf( 'users/%d/collections/tags', $user->get__id() ) ),
'type' => 'Collection',
'totalItems' => count( $tags ),
'totalItems' => is_countable( $tags ) ? count( $tags ) : 0,
'items' => array(),
);
@ -163,7 +163,7 @@ class Collection {
'@context' => Activity::CONTEXT,
'id' => get_rest_url_by_path( sprintf( 'users/%d/collections/featured', $user_id ) ),
'type' => 'OrderedCollection',
'totalItems' => count( $posts ),
'totalItems' => is_countable( $posts ) ? count( $posts ) : 0,
'orderedItems' => array(),
);

View file

@ -75,7 +75,7 @@ class Following {
$items = apply_filters( 'activitypub_rest_following', array(), $user ); // phpcs:ignore
$json->totalItems = count( $items ); // phpcs:ignore
$json->totalItems = is_countable( $items ) ? count( $items ) : 0; // phpcs:ignore
$json->orderedItems = $items; // phpcs:ignore
$json->first = $json->partOf; // phpcs:ignore

View file

@ -88,7 +88,7 @@ class Nodeinfo {
)
);
if ( is_array( $users ) ) {
if ( is_countable( $users ) ) {
$users = count( $users );
} else {
$users = 1;
@ -145,7 +145,7 @@ class Nodeinfo {
)
);
if ( is_array( $users ) ) {
if ( is_countable( $users ) ) {
$users = count( $users );
} else {
$users = 1;

View file

@ -335,6 +335,8 @@ class Post {
return \ucfirst( \get_option( 'activitypub_object_type', 'note' ) );
}
// Default to Article.
$object_type = 'Article';
$post_type = \get_post_type( $this->wp_post );
switch ( $post_type ) {
case 'post':