diff --git a/activitypub.php b/activitypub.php index 0528965..47e4167 100644 --- a/activitypub.php +++ b/activitypub.php @@ -29,8 +29,6 @@ function init() { \define( 'ACTIVITYPUB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); \define( 'ACTIVITYPUB_PLUGIN_FILE', plugin_dir_path( __FILE__ ) . '/' . basename( __FILE__ ) ); - \define( 'ACTIVITYPUB_OBJECT', 'ACTIVITYPUB_OBJECT' ); - Migration::init(); Activity_Dispatcher::init(); Activitypub::init(); diff --git a/includes/class-scheduler.php b/includes/class-scheduler.php index 4bfb8b8..a79044e 100644 --- a/includes/class-scheduler.php +++ b/includes/class-scheduler.php @@ -95,7 +95,7 @@ class Scheduler { * @return void */ public static function update_followers() { - $followers = Followers::get_outdated_followers( ACTIVITYPUB_OBJECT ); + $followers = Followers::get_outdated_followers(); foreach ( $followers as $follower ) { $meta = get_remote_metadata_by_actor( $follower->get_actor() ); @@ -116,7 +116,7 @@ class Scheduler { * @return void */ public static function cleanup_followers() { - $followers = Followers::get_faulty_followers( ACTIVITYPUB_OBJECT ); + $followers = Followers::get_faulty_followers(); foreach ( $followers as $follower ) { $meta = get_remote_metadata_by_actor( $follower->get_actor() ); diff --git a/includes/collection/class-followers.php b/includes/collection/class-followers.php index 413af3b..8a271f6 100644 --- a/includes/collection/class-followers.php +++ b/includes/collection/class-followers.php @@ -316,7 +316,7 @@ class Followers { * * @return array The Term list of Followers, the format depends on $output */ - public static function get_followers( $user_id, $output = ARRAY_N, $number = null, $offset = null, $args = array() ) { + public static function get_followers( $user_id, $number = null, $offset = null, $args = array() ) { $defaults = array( 'taxonomy' => self::TAXONOMY, 'hide_empty' => false, @@ -329,25 +329,13 @@ class Followers { $args = wp_parse_args( $args, $defaults ); $terms = new WP_Term_Query( $args ); - $items = array(); - // change output format - switch ( $output ) { - case ACTIVITYPUB_OBJECT: - foreach ( $terms->get_terms() as $follower ) { - $items[] = new Follower( $follower->name ); // phpcs:ignore - } - return $items; - case OBJECT: - return $terms->get_terms(); - case ARRAY_N: - default: - foreach ( $terms->get_terms() as $follower ) { - $items[] = $follower->name; // phpcs:ignore - } - return $items; + foreach ( $terms->get_terms() as $follower ) { + $items[] = new Follower( $follower->name ); // phpcs:ignore } + + return $items; } /** @@ -414,7 +402,7 @@ class Followers { * * @return mixed The Term list of Followers, the format depends on $output. */ - public static function get_outdated_followers( $output = ARRAY_N, $number = 50, $older_than = 604800 ) { + public static function get_outdated_followers( $number = 50, $older_than = 604800 ) { $args = array( 'taxonomy' => self::TAXONOMY, 'number' => $number, @@ -432,25 +420,13 @@ class Followers { ); $terms = new WP_Term_Query( $args ); - $items = array(); - // change output format - switch ( $output ) { - case ACTIVITYPUB_OBJECT: - foreach ( $terms->get_terms() as $follower ) { - $items[] = new Follower( $follower->name ); // phpcs:ignore - } - return $items; - case OBJECT: - return $terms->get_terms(); - case ARRAY_N: - default: - foreach ( $terms->get_terms() as $follower ) { - $items[] = $follower->name; // phpcs:ignore - } - return $items; + foreach ( $terms->get_terms() as $follower ) { + $items[] = new Follower( $follower->name ); // phpcs:ignore } + + return $items; } /** @@ -461,7 +437,7 @@ class Followers { * * @return mixed The Term list of Followers, the format depends on $output. */ - public static function get_faulty_followers( $output = ARRAY_N, $number = 10 ) { + public static function get_faulty_followers( $number = 10 ) { $args = array( 'taxonomy' => self::TAXONOMY, 'number' => $number, @@ -474,24 +450,12 @@ class Followers { ); $terms = new WP_Term_Query( $args ); - $items = array(); - // change output format - switch ( $output ) { - case ACTIVITYPUB_OBJECT: - foreach ( $terms->get_terms() as $follower ) { - $items[] = new Follower( $follower->name ); // phpcs:ignore - } - return $items; - case OBJECT: - return $terms->get_terms(); - case ARRAY_N: - default: - foreach ( $terms->get_terms() as $follower ) { - $items[] = $follower->name; // phpcs:ignore - } - return $items; + foreach ( $terms->get_terms() as $follower ) { + $items[] = new Follower( $follower->name ); // phpcs:ignore } + + return $items; } } diff --git a/includes/model/class-follower.php b/includes/model/class-follower.php index 0668e4d..6c03fd8 100644 --- a/includes/model/class-follower.php +++ b/includes/model/class-follower.php @@ -159,6 +159,15 @@ class Follower { } } + /** + * Magic function to return the Actor-URL when the Object is used as a string + * + * @return string + */ + public function __toString() { + return $this->get_actor(); + } + /** * Prefill the Object with the meta data. * diff --git a/includes/rest/class-followers.php b/includes/rest/class-followers.php index 9a8b9b6..7dd49a8 100644 --- a/includes/rest/class-followers.php +++ b/includes/rest/class-followers.php @@ -81,7 +81,13 @@ class Followers { $json->partOf = \get_rest_url( null, "/activitypub/1.0/users/$user_id/followers" ); // phpcs:ignore $json->first = $json->partOf; // phpcs:ignore $json->totalItems = FollowerCollection::count_followers( $user_id ); // phpcs:ignore - $json->orderedItems = FollowerCollection::get_followers( $user_id, ARRAY_N ); // phpcs:ignore + // phpcs:ignore + $json->orderedItems = array_map( + function( $item ) { + return $item->get_actor(); + }, + FollowerCollection::get_followers( $user_id ) + ); $response = new WP_REST_Response( $json, 200 ); $response->header( 'Content-Type', 'application/activity+json' ); diff --git a/includes/table/class-followers.php b/includes/table/class-followers.php index c9b5948..03fb70f 100644 --- a/includes/table/class-followers.php +++ b/includes/table/class-followers.php @@ -35,7 +35,7 @@ class Followers extends WP_List_Table { $page_num = $this->get_pagenum(); $per_page = 20; - $follower = FollowerCollection::get_followers( \get_current_user_id(), ACTIVITYPUB_OBJECT, $per_page, ( $page_num - 1 ) * $per_page ); + $follower = FollowerCollection::get_followers( \get_current_user_id(), $per_page, ( $page_num - 1 ) * $per_page ); $counter = FollowerCollection::count_followers( \get_current_user_id() ); $this->items = array(); diff --git a/tests/test-class-db-activitypub-followers.php b/tests/test-class-db-activitypub-followers.php index 2467363..a04145a 100644 --- a/tests/test-class-db-activitypub-followers.php +++ b/tests/test-class-db-activitypub-followers.php @@ -58,6 +58,13 @@ class Test_Db_Activitypub_Followers extends WP_UnitTestCase { $this->assertEquals( 3, \count( $db_followers ) ); + $db_followers = array_map( + function( $item ) { + return $item->get_actor(); + }, + $db_followers + ); + $this->assertSame( array( 'https://example.com/author/jon', 'https://example.org/author/doe', 'http://sally.example.org' ), $db_followers ); }