From 53adfe6b80b372ae907e85566907cabd4b3fe478 Mon Sep 17 00:00:00 2001 From: Matt Wiebe Date: Wed, 25 Oct 2023 01:44:04 -0500 Subject: [PATCH] PHP 8.1 compatibility (#533) * PHP 8.1 compatibility * Update compat.php --------- Co-authored-by: Matthias Pfefferle --- includes/compat.php | 12 ++++++++++++ includes/rest/class-collection.php | 4 ++-- includes/rest/class-following.php | 2 +- includes/rest/class-nodeinfo.php | 4 ++-- includes/transformer/class-post.php | 2 ++ 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/includes/compat.php b/includes/compat.php index 4bee640..3dd405c 100644 --- a/includes/compat.php +++ b/includes/compat.php @@ -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; + } +} diff --git a/includes/rest/class-collection.php b/includes/rest/class-collection.php index 2e6522e..365641c 100644 --- a/includes/rest/class-collection.php +++ b/includes/rest/class-collection.php @@ -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(), ); diff --git a/includes/rest/class-following.php b/includes/rest/class-following.php index 22c9d46..58e4375 100644 --- a/includes/rest/class-following.php +++ b/includes/rest/class-following.php @@ -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 diff --git a/includes/rest/class-nodeinfo.php b/includes/rest/class-nodeinfo.php index 1f6277a..4829e75 100644 --- a/includes/rest/class-nodeinfo.php +++ b/includes/rest/class-nodeinfo.php @@ -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; diff --git a/includes/transformer/class-post.php b/includes/transformer/class-post.php index 9250afe..36d357e 100644 --- a/includes/transformer/class-post.php +++ b/includes/transformer/class-post.php @@ -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':