From 9d2a7651691bc252d2810911ffa9bc8a0e9a8abc Mon Sep 17 00:00:00 2001 From: Allison Levine Date: Wed, 4 Nov 2026 16:08:43 -0500 Subject: [PATCH 1/9] Add a stats dashboard link to the Fediverse stats emails The monthly and annual stats emails had no link to the stats themselves. They now include a "View your stats" button pointing at the Fediverse Stats dashboard widget, and the plain text alternative carries the URL. The link is only added when the recipient would actually see the widget: user_can_activitypub() mirrors the widget's own gate for both the blog actor (whose report goes to admin_email) and individual users. Fixes #4014 --- .../changelog/add-stats-email-dashboard-link | 4 + includes/scheduler/class-statistics.php | 34 ++++ templates/emails/stats-report.php | 10 + .../scheduler/class-test-statistics.php | 182 +++++++++++++++++- 4 files changed, 226 insertions(+), 4 deletions(-) create mode 100644 .github/changelog/add-stats-email-dashboard-link diff --git a/.github/changelog/add-stats-email-dashboard-link b/.github/changelog/add-stats-email-dashboard-link new file mode 100644 index 0000000000..de37b7b7a3 --- /dev/null +++ b/.github/changelog/add-stats-email-dashboard-link @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Your monthly and annual Fediverse stats emails now include a link that takes you straight to the Fediverse Stats widget on your dashboard. diff --git a/includes/scheduler/class-statistics.php b/includes/scheduler/class-statistics.php index 078f6dbff6..0a74e2ece9 100644 --- a/includes/scheduler/class-statistics.php +++ b/includes/scheduler/class-statistics.php @@ -13,6 +13,8 @@ use Activitypub\Mailer; use Activitypub\Statistics as Statistics_Collector; +use function Activitypub\user_can_activitypub; + /** * Statistics scheduler class. */ @@ -166,6 +168,7 @@ public static function send_annual_email( $user_id, $year, $summary, $force = fa 'most_active_month_name' => $most_active_month_name, 'followers_text' => $followers_text, 'supporter_text' => $supporter_text, + 'stats_url' => self::get_stats_url( $user_id ), 'user_id' => $user_id, ) ); @@ -196,6 +199,11 @@ public static function send_annual_email( $user_id, $year, $summary, $force = fa $alt_body .= \sprintf( \__( "Most active month: %s\n", 'activitypub' ), $most_active_month_name ); } + if ( ! empty( $args['stats_url'] ) ) { + /* translators: %s: URL of the Fediverse Stats dashboard widget */ + $alt_body .= \sprintf( \__( "\nView your stats: %s\n", 'activitypub' ), $args['stats_url'] ); + } + Mailer::send( $user_id, $subject, 'stats-report', $args, $alt_body ); } @@ -268,6 +276,7 @@ public static function send_monthly_email( $user_id, $year, $month, $force = fal 'closing' => \__( 'Keep sharing great content on the Fediverse!', 'activitypub' ), 'followers_text' => $followers_text, 'supporter_text' => $supporter_text, + 'stats_url' => self::get_stats_url( $user_id ), 'user_id' => $user_id, ) ); @@ -293,9 +302,34 @@ public static function send_monthly_email( $user_id, $year, $month, $force = fal $alt_body .= \sprintf( \__( "New followers: %+d\n", 'activitypub' ), $stats['followers_count'] ); } + if ( ! empty( $args['stats_url'] ) ) { + /* translators: %s: URL of the Fediverse Stats dashboard widget */ + $alt_body .= \sprintf( \__( "\nView your stats: %s\n", 'activitypub' ), $args['stats_url'] ); + } + Mailer::send( $user_id, $subject, 'stats-report', $args, $alt_body ); } + /** + * Get the URL of the Fediverse Stats dashboard widget for a report recipient. + * + * Mirrors the gate that registers the widget in `Activitypub\WP_Admin\Dashboard`, so the + * link is only added when the recipient would actually see the widget on their dashboard. + * For the blog actor the report goes to `admin_email`, which satisfies the widget's + * `manage_options` requirement. + * + * @param int $user_id The user ID, or `Actors::BLOG_USER_ID` for the blog actor. + * + * @return string The dashboard URL, or an empty string if the recipient cannot see the widget. + */ + private static function get_stats_url( $user_id ) { + if ( ! user_can_activitypub( $user_id ) ) { + return ''; + } + + return \admin_url( 'index.php#activitypub_stats' ); + } + /** * Build the option name used to record that a stats email has been sent for a given period. * diff --git a/templates/emails/stats-report.php b/templates/emails/stats-report.php index bdc1203153..dc1e9801c7 100644 --- a/templates/emails/stats-report.php +++ b/templates/emails/stats-report.php @@ -18,6 +18,7 @@ * - followers_start (int) Followers at period start (annual only). * - followers_end (int) Followers at period end (annual only). * - followers_text (string) The follower detail text (HTML allowed). + * - stats_url (string) URL of the Fediverse Stats dashboard widget, empty to omit the link. * - most_active_month_name (string) Most active month (annual only). * - {type}_count (int) Per-engagement-type counts. * @@ -42,6 +43,7 @@ 'top_posts' => array(), 'top_multiplicator' => null, 'supporter_text' => '', + 'stats_url' => '', 'user_id' => 0, ) ); @@ -207,6 +209,14 @@

+ +

+ + + +

+ + sent_count = 0; - \add_filter( 'pre_wp_mail', array( $this, 'short_circuit_wp_mail' ) ); + $this->sent_count = 0; + $this->sent_message = ''; + $this->sent_alt_body = ''; + \add_filter( 'pre_wp_mail', array( $this, 'short_circuit_wp_mail' ), 10, 2 ); } /** * Remove the test mail filter so it doesn't leak across tests. */ public function tear_down() { - \remove_filter( 'pre_wp_mail', array( $this, 'short_circuit_wp_mail' ) ); + \remove_filter( 'pre_wp_mail', array( $this, 'short_circuit_wp_mail' ), 10 ); + \remove_action( 'phpmailer_init', array( $this, 'capture_alt_body' ), 11 ); parent::tear_down(); } /** * Count sent emails and short-circuit wp_mail during tests. * + * @param null|bool $short_circuit Short-circuit return value. + * @param array $atts The wp_mail() arguments. + * * @return bool Always true to short-circuit wp_mail. */ - public function short_circuit_wp_mail() { + public function short_circuit_wp_mail( $short_circuit, $atts = array() ) { ++$this->sent_count; + $this->sent_message = $atts['message'] ?? ''; + return true; } + /** + * Capture the plain text alternative body set by the mailer. + * + * Runs after `Mailer::send()`'s own `phpmailer_init` callback, which assigns AltBody. + * + * @param \PHPMailer\PHPMailer\PHPMailer $mailer The mailer instance. + */ + public function capture_alt_body( $mailer ) { + $this->sent_alt_body = $mailer->AltBody; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.NotSnakeCase + } + + /** + * Seed a month of stats with meaningful activity. + * + * @param int $user_id The user ID. + * @param int $year The year. + * @param int $month The month. + */ + protected function seed_monthly_stats( $user_id, $year, $month ) { + \update_option( + Statistics_Collector::get_monthly_option_name( $user_id, $year, $month ), + array( + 'posts_count' => 3, + 'followers_count' => 5, + 'followers_total' => 10, + ) + ); + } + /** * Test that send_monthly_email only sends once per (user, year, month). * @@ -191,4 +243,126 @@ public function test_send_annual_email_force_records_email_sent() { $this->assertSame( 2, $this->sent_count, 'Forced send should bypass the email-sent check and still send.' ); } + + /** + * Test that the monthly report links to the Fediverse Stats dashboard widget. + * + * @covers ::send_monthly_email + */ + public function test_send_monthly_email_links_to_stats_widget() { + $year = 2026; + $month = 5; + $this->seed_monthly_stats( self::$user_id, $year, $month ); + + Statistics_Scheduler::send_monthly_email( self::$user_id, $year, $month ); + + $this->assertSame( 1, $this->sent_count, 'Monthly email should be sent.' ); + $this->assertStringContainsString( + \esc_url( \admin_url( 'index.php#activitypub_stats' ) ), + $this->sent_message, + 'Monthly email should link to the stats dashboard widget.' + ); + $this->assertStringContainsString( 'View your stats', $this->sent_message, 'Monthly email should show the stats link label.' ); + } + + /** + * Test that the annual report links to the Fediverse Stats dashboard widget. + * + * @covers ::send_annual_email + */ + public function test_send_annual_email_links_to_stats_widget() { + \update_user_option( self::$user_id, 'activitypub_mailer_annual_report', '1' ); + + Statistics_Scheduler::send_annual_email( self::$user_id, 2024, array( 'posts_count' => 8 ) ); + + $this->assertSame( 1, $this->sent_count, 'Annual email should be sent.' ); + $this->assertStringContainsString( + \esc_url( \admin_url( 'index.php#activitypub_stats' ) ), + $this->sent_message, + 'Annual email should link to the stats dashboard widget.' + ); + } + + /** + * Test that the blog actor's report links to the stats widget, since its report + * goes to admin_email. + * + * @covers ::send_monthly_email + */ + public function test_send_monthly_email_links_to_stats_widget_for_blog_actor() { + $year = 2026; + $month = 6; + + // The widget's blog branch requires the blog actor to be enabled. + \update_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_AND_BLOG_MODE ); + $this->seed_monthly_stats( Actors::BLOG_USER_ID, $year, $month ); + + Statistics_Scheduler::send_monthly_email( Actors::BLOG_USER_ID, $year, $month ); + + $this->assertSame( 1, $this->sent_count, 'Blog actor monthly email should be sent.' ); + $this->assertStringContainsString( + \esc_url( \admin_url( 'index.php#activitypub_stats' ) ), + $this->sent_message, + 'Blog actor email should link to the stats dashboard widget.' + ); + } + + /** + * Test that no stats link is added when the recipient cannot see the dashboard widget. + * + * @covers ::send_monthly_email + */ + public function test_send_monthly_email_omits_stats_link_without_access() { + $year = 2026; + $month = 7; + $this->seed_monthly_stats( self::$user_id, $year, $month ); + + \add_filter( 'activitypub_user_can_activitypub', '__return_false' ); + Statistics_Scheduler::send_monthly_email( self::$user_id, $year, $month ); + \remove_filter( 'activitypub_user_can_activitypub', '__return_false' ); + + $this->assertSame( 1, $this->sent_count, 'Monthly email should still be sent.' ); + $this->assertStringNotContainsString( + 'activitypub_stats', + $this->sent_message, + 'Email should not link to a dashboard widget the recipient cannot see.' + ); + $this->assertStringNotContainsString( 'View your stats', $this->sent_message, 'Email should not show the stats link label.' ); + } + + /** + * Test that the plain text alternative also carries the stats URL. + * + * @covers ::send_monthly_email + */ + public function test_send_monthly_email_adds_stats_url_to_alt_body() { + $year = 2026; + $month = 8; + $this->seed_monthly_stats( self::$user_id, $year, $month ); + + // Let wp_mail run so Mailer's phpmailer_init callback assigns AltBody. The default + // From address (wordpress@localhost) has no TLD, which PHPMailer rejects. + \remove_filter( 'pre_wp_mail', array( $this, 'short_circuit_wp_mail' ), 10 ); + \add_filter( 'wp_mail_from', array( $this, 'valid_mail_from' ) ); + \add_action( 'phpmailer_init', array( $this, 'capture_alt_body' ), 11 ); + + Statistics_Scheduler::send_monthly_email( self::$user_id, $year, $month ); + + \remove_filter( 'wp_mail_from', array( $this, 'valid_mail_from' ) ); + + $this->assertStringContainsString( + \admin_url( 'index.php#activitypub_stats' ), + $this->sent_alt_body, + 'Plain text body should include the stats dashboard URL.' + ); + } + + /** + * Provide a From address PHPMailer accepts. + * + * @return string The From address. + */ + public function valid_mail_from() { + return 'wordpress@example.org'; + } } From d835b1212940e869eb76bffc61121fe50f4a20e5 Mon Sep 17 00:00:00 2001 From: Allison Levine Date: Wed, 4 Nov 2026 16:37:16 -0500 Subject: [PATCH 2/9] Use variable property syntax for PHPMailer's AltBody Matches Mailer::send() and satisfies the snake_case property sniff without an ignore comment. --- .../phpunit/tests/includes/scheduler/class-test-statistics.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/includes/scheduler/class-test-statistics.php b/tests/phpunit/tests/includes/scheduler/class-test-statistics.php index 6cd1c0c62c..c597749a3d 100644 --- a/tests/phpunit/tests/includes/scheduler/class-test-statistics.php +++ b/tests/phpunit/tests/includes/scheduler/class-test-statistics.php @@ -100,7 +100,7 @@ public function short_circuit_wp_mail( $short_circuit, $atts = array() ) { * @param \PHPMailer\PHPMailer\PHPMailer $mailer The mailer instance. */ public function capture_alt_body( $mailer ) { - $this->sent_alt_body = $mailer->AltBody; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.NotSnakeCase + $this->sent_alt_body = $mailer->{'AltBody'}; } /** From ae7e8da7cd1f4914c0f5ed058a0f9095873c9527 Mon Sep 17 00:00:00 2001 From: Allison Levine Date: Wed, 4 Nov 2026 16:45:34 -0500 Subject: [PATCH 3/9] Gate the stats email link on the widget's own check get_stats_url() mirrored the widget gate with user_can_activitypub(), which the activitypub_user_can_activitypub filter can force true. The widget re-checks the actor type after that filter, so a forced capability in blog-only mode produced a link to a dashboard with no widget. Defer to Dashboard::user_can_see_stats() instead, now public and accepting a user ID, so there is one source of truth. This also fixes the inverse: an administrator on a blog-only site sees the widget through its manage_options branch and now keeps the link. --- includes/scheduler/class-statistics.php | 19 ++- includes/wp-admin/class-dashboard.php | 16 ++- .../scheduler/class-test-statistics.php | 54 +++++++++ .../wp-admin/class-test-dashboard.php | 109 ++++++++++++++++++ 4 files changed, 188 insertions(+), 10 deletions(-) create mode 100644 tests/phpunit/tests/includes/wp-admin/class-test-dashboard.php diff --git a/includes/scheduler/class-statistics.php b/includes/scheduler/class-statistics.php index 0a74e2ece9..1115733fe3 100644 --- a/includes/scheduler/class-statistics.php +++ b/includes/scheduler/class-statistics.php @@ -12,8 +12,9 @@ use Activitypub\Collection\Actors; use Activitypub\Mailer; use Activitypub\Statistics as Statistics_Collector; +use Activitypub\WP_Admin\Dashboard; -use function Activitypub\user_can_activitypub; +use function Activitypub\is_user_type_disabled; /** * Statistics scheduler class. @@ -313,17 +314,23 @@ public static function send_monthly_email( $user_id, $year, $month, $force = fal /** * Get the URL of the Fediverse Stats dashboard widget for a report recipient. * - * Mirrors the gate that registers the widget in `Activitypub\WP_Admin\Dashboard`, so the - * link is only added when the recipient would actually see the widget on their dashboard. - * For the blog actor the report goes to `admin_email`, which satisfies the widget's - * `manage_options` requirement. + * Defers to the gate that registers the widget, so the link is only added when the + * recipient would actually see the widget on their dashboard. The blog actor is not a + * WordPress user, so it cannot be passed to that gate: its report goes to `admin_email`, + * which stands in for the `manage_options` branch. * * @param int $user_id The user ID, or `Actors::BLOG_USER_ID` for the blog actor. * * @return string The dashboard URL, or an empty string if the recipient cannot see the widget. */ private static function get_stats_url( $user_id ) { - if ( ! user_can_activitypub( $user_id ) ) { + if ( Actors::BLOG_USER_ID === $user_id ) { + $can_see_stats = ! is_user_type_disabled( 'blog' ); + } else { + $can_see_stats = Dashboard::user_can_see_stats( $user_id ); + } + + if ( ! $can_see_stats ) { return ''; } diff --git a/includes/wp-admin/class-dashboard.php b/includes/wp-admin/class-dashboard.php index f10017f936..4135f08e84 100644 --- a/includes/wp-admin/class-dashboard.php +++ b/includes/wp-admin/class-dashboard.php @@ -141,13 +141,21 @@ public static function enqueue_scripts( $hook ) { } /** - * Check if the current user can see the stats widget. + * Check if a user can see the stats widget. + * + * @since unreleased Accepts a user ID, so callers outside the admin request can ask about a given user. + * + * @param int|null $user_id Optional. The user to check. Default null, the current user. * * @return bool True if user has access. */ - private static function user_can_see_stats() { - $has_user_access = user_can_activitypub( \get_current_user_id() ) && ! is_user_type_disabled( 'user' ); - $has_blog_access = ! is_user_type_disabled( 'blog' ) && \current_user_can( 'manage_options' ); + public static function user_can_see_stats( $user_id = null ) { + if ( null === $user_id ) { + $user_id = \get_current_user_id(); + } + + $has_user_access = user_can_activitypub( $user_id ) && ! is_user_type_disabled( 'user' ); + $has_blog_access = ! is_user_type_disabled( 'blog' ) && \user_can( $user_id, 'manage_options' ); return $has_user_access || $has_blog_access; } diff --git a/tests/phpunit/tests/includes/scheduler/class-test-statistics.php b/tests/phpunit/tests/includes/scheduler/class-test-statistics.php index c597749a3d..fa54124f15 100644 --- a/tests/phpunit/tests/includes/scheduler/class-test-statistics.php +++ b/tests/phpunit/tests/includes/scheduler/class-test-statistics.php @@ -330,6 +330,60 @@ public function test_send_monthly_email_omits_stats_link_without_access() { $this->assertStringNotContainsString( 'View your stats', $this->sent_message, 'Email should not show the stats link label.' ); } + /** + * Test that no stats link is added when user mode is disabled, even if a filter forces + * the ActivityPub capability on. The widget's own gate re-checks the actor type after + * that filter, so the link would otherwise point at a dashboard without the widget. + * + * @covers ::send_monthly_email + */ + public function test_send_monthly_email_omits_stats_link_when_user_mode_disabled() { + $year = 2026; + $month = 9; + $this->seed_monthly_stats( self::$user_id, $year, $month ); + + // Blog-only mode disables the user actor type. + \update_option( 'activitypub_actor_mode', ACTIVITYPUB_BLOG_MODE ); + \add_filter( 'activitypub_user_can_activitypub', '__return_true' ); + + Statistics_Scheduler::send_monthly_email( self::$user_id, $year, $month ); + + \remove_filter( 'activitypub_user_can_activitypub', '__return_true' ); + + $this->assertSame( 1, $this->sent_count, 'Monthly email should still be sent.' ); + $this->assertStringNotContainsString( + 'activitypub_stats', + $this->sent_message, + 'A forced capability should not add a link to a widget the actor type disables.' + ); + } + + /** + * Test that an administrator still gets the link when only the blog actor is enabled, + * since the widget's blog branch shows it to anyone who can manage options. + * + * @covers ::send_monthly_email + */ + public function test_send_monthly_email_links_to_stats_widget_for_admin_in_blog_mode() { + $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + \update_user_option( $admin_id, 'activitypub_mailer_monthly_report', '1' ); + + $year = 2026; + $month = 10; + $this->seed_monthly_stats( $admin_id, $year, $month ); + + \update_option( 'activitypub_actor_mode', ACTIVITYPUB_BLOG_MODE ); + + Statistics_Scheduler::send_monthly_email( $admin_id, $year, $month ); + + $this->assertSame( 1, $this->sent_count, 'Monthly email should be sent.' ); + $this->assertStringContainsString( + \esc_url( \admin_url( 'index.php#activitypub_stats' ) ), + $this->sent_message, + 'An administrator sees the widget through its blog branch, so the link belongs in the email.' + ); + } + /** * Test that the plain text alternative also carries the stats URL. * diff --git a/tests/phpunit/tests/includes/wp-admin/class-test-dashboard.php b/tests/phpunit/tests/includes/wp-admin/class-test-dashboard.php new file mode 100644 index 0000000000..0a49f835c8 --- /dev/null +++ b/tests/phpunit/tests/includes/wp-admin/class-test-dashboard.php @@ -0,0 +1,109 @@ +user->create( array( 'role' => 'author' ) ); + self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) ); + } + + /** + * Test that the check defaults to the current user when no ID is given. + * + * @covers ::user_can_see_stats + */ + public function test_user_can_see_stats_defaults_to_current_user() { + \wp_set_current_user( self::$author_id ); + $this->assertTrue( Dashboard::user_can_see_stats(), 'An ActivityPub-enabled author sees the widget.' ); + + \wp_set_current_user( 0 ); + $this->assertFalse( Dashboard::user_can_see_stats(), 'A logged out visitor does not see the widget.' ); + } + + /** + * Test that the check answers for the given user rather than the current one. + * + * @covers ::user_can_see_stats + */ + public function test_user_can_see_stats_for_a_given_user() { + \wp_set_current_user( 0 ); + + $this->assertTrue( Dashboard::user_can_see_stats( self::$author_id ), 'The author sees the widget regardless of who is logged in.' ); + $this->assertFalse( Dashboard::user_can_see_stats( 0 ), 'User 0 is nobody and sees nothing.' ); + } + + /** + * Test that an administrator sees the widget through the blog branch when only the + * blog actor is enabled. + * + * @covers ::user_can_see_stats + */ + public function test_user_can_see_stats_in_blog_mode() { + \update_option( 'activitypub_actor_mode', ACTIVITYPUB_BLOG_MODE ); + + $this->assertTrue( Dashboard::user_can_see_stats( self::$admin_id ), 'An administrator sees the widget through the blog branch.' ); + $this->assertFalse( Dashboard::user_can_see_stats( self::$author_id ), 'An author has no blog access and no user actor in blog mode.' ); + } + + /** + * Test that a forced ActivityPub capability does not grant access while the user actor + * type is disabled. + * + * @covers ::user_can_see_stats + */ + public function test_user_can_see_stats_ignores_forced_capability_in_blog_mode() { + \update_option( 'activitypub_actor_mode', ACTIVITYPUB_BLOG_MODE ); + \add_filter( 'activitypub_user_can_activitypub', '__return_true' ); + + $can_see = Dashboard::user_can_see_stats( self::$author_id ); + + \remove_filter( 'activitypub_user_can_activitypub', '__return_true' ); + + $this->assertFalse( $can_see, 'The actor type check runs after the capability filter.' ); + } + + /** + * Test that only the blog actor being disabled does not remove user access. + * + * @covers ::user_can_see_stats + */ + public function test_user_can_see_stats_in_actor_mode() { + \update_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE ); + + $this->assertTrue( Dashboard::user_can_see_stats( self::$author_id ), 'The author still sees the widget through the user branch.' ); + } +} From 2cff8237a5ff255713cab51bcd92058a8fd8da97 Mon Sep 17 00:00:00 2001 From: Allison Levine Date: Wed, 4 Nov 2026 16:55:49 -0500 Subject: [PATCH 4/9] Move the stats widget gate out of the admin layer The scheduler reached into Activitypub\WP_Admin\Dashboard from a cron path, the only reference to that namespace from includes/ outside includes/wp-admin/. The gate now lives in functions-user.php beside user_can_activitypub() and is_user_type_disabled(), and both the widget and the scheduler call it. It also rejects non-user IDs. BLOG_USER_ID is 0, so user_can_activitypub( 0 ) answers for the blog actor, which made user_can_see_stats( 0 ) - including the current-user default on a logged out request - return true whenever the blog actor was enabled. Adds the missing annual alt-body test. --- includes/functions-user.php | 31 +++++ includes/scheduler/class-statistics.php | 4 +- includes/wp-admin/class-dashboard.php | 25 +--- .../includes/class-test-functions-user.php | 81 +++++++++++++ .../scheduler/class-test-statistics.php | 24 ++++ .../wp-admin/class-test-dashboard.php | 109 ------------------ 6 files changed, 141 insertions(+), 133 deletions(-) delete mode 100644 tests/phpunit/tests/includes/wp-admin/class-test-dashboard.php diff --git a/includes/functions-user.php b/includes/functions-user.php index 615e5648d0..f4ee986f22 100644 --- a/includes/functions-user.php +++ b/includes/functions-user.php @@ -151,6 +151,37 @@ function user_can_act_as_blog() { return (bool) \apply_filters( 'activitypub_user_can_act_as_blog', \current_user_can( 'manage_options' ) ); } +/** + * Checks whether a user can see the Fediverse Stats dashboard widget. + * + * A user reaches the widget either through their own actor, or through the blog + * actor if they can manage the site. The blog actor itself is not a user and has + * no dashboard, so `BLOG_USER_ID` and any other non-user ID are rejected: callers + * acting for the blog actor decide for themselves who its report reaches. + * + * @since unreleased + * + * @param int|null $user_id Optional. The user to check. Default null, the current user. + * + * @return bool True if the user can see the stats widget. + */ +function user_can_see_stats( $user_id = null ) { + if ( null === $user_id ) { + $user_id = \get_current_user_id(); + } + + $user_id = (int) $user_id; + + if ( $user_id <= Actors::BLOG_USER_ID ) { + return false; + } + + $has_user_access = user_can_activitypub( $user_id ) && ! is_user_type_disabled( 'user' ); + $has_blog_access = ! is_user_type_disabled( 'blog' ) && \user_can( $user_id, 'manage_options' ); + + return $has_user_access || $has_blog_access; +} + /** * Checks if a User-Type is disabled for ActivityPub. * diff --git a/includes/scheduler/class-statistics.php b/includes/scheduler/class-statistics.php index 1115733fe3..eeedf5c8ea 100644 --- a/includes/scheduler/class-statistics.php +++ b/includes/scheduler/class-statistics.php @@ -12,9 +12,9 @@ use Activitypub\Collection\Actors; use Activitypub\Mailer; use Activitypub\Statistics as Statistics_Collector; -use Activitypub\WP_Admin\Dashboard; use function Activitypub\is_user_type_disabled; +use function Activitypub\user_can_see_stats; /** * Statistics scheduler class. @@ -327,7 +327,7 @@ private static function get_stats_url( $user_id ) { if ( Actors::BLOG_USER_ID === $user_id ) { $can_see_stats = ! is_user_type_disabled( 'blog' ); } else { - $can_see_stats = Dashboard::user_can_see_stats( $user_id ); + $can_see_stats = user_can_see_stats( $user_id ); } if ( ! $can_see_stats ) { diff --git a/includes/wp-admin/class-dashboard.php b/includes/wp-admin/class-dashboard.php index 4135f08e84..d31e00c5a5 100644 --- a/includes/wp-admin/class-dashboard.php +++ b/includes/wp-admin/class-dashboard.php @@ -12,6 +12,7 @@ use function Activitypub\is_user_type_disabled; use function Activitypub\user_can_activitypub; +use function Activitypub\user_can_see_stats; /** * Dashboard Widgets Class. @@ -62,7 +63,7 @@ public static function add_dashboard_widgets() { } // Stats widget. - if ( self::user_can_see_stats() ) { + if ( user_can_see_stats() ) { \wp_add_dashboard_widget( 'activitypub_stats', \__( 'Fediverse Stats', 'activitypub' ), @@ -88,7 +89,7 @@ public static function enqueue_scripts( $hook ) { } // Only enqueue if user has access to stats. - if ( ! self::user_can_see_stats() ) { + if ( ! user_can_see_stats() ) { return; } @@ -140,26 +141,6 @@ public static function enqueue_scripts( $hook ) { ); } - /** - * Check if a user can see the stats widget. - * - * @since unreleased Accepts a user ID, so callers outside the admin request can ask about a given user. - * - * @param int|null $user_id Optional. The user to check. Default null, the current user. - * - * @return bool True if user has access. - */ - public static function user_can_see_stats( $user_id = null ) { - if ( null === $user_id ) { - $user_id = \get_current_user_id(); - } - - $has_user_access = user_can_activitypub( $user_id ) && ! is_user_type_disabled( 'user' ); - $has_blog_access = ! is_user_type_disabled( 'blog' ) && \user_can( $user_id, 'manage_options' ); - - return $has_user_access || $has_blog_access; - } - /** * Render the ActivityPub.blog news feed widget. * diff --git a/tests/phpunit/tests/includes/class-test-functions-user.php b/tests/phpunit/tests/includes/class-test-functions-user.php index 2716a82983..efbabb3807 100644 --- a/tests/phpunit/tests/includes/class-test-functions-user.php +++ b/tests/phpunit/tests/includes/class-test-functions-user.php @@ -181,4 +181,85 @@ public function test_get_active_users_with_custom_post_types() { $cleanup(); } } + + /** + * Test that the stats widget check defaults to the current user. + * + * @covers ::user_can_see_stats + */ + public function test_user_can_see_stats_defaults_to_current_user() { + $author_id = self::factory()->user->create( array( 'role' => 'author' ) ); + + \wp_set_current_user( $author_id ); + $this->assertTrue( \Activitypub\user_can_see_stats(), 'An ActivityPub-enabled author sees the widget.' ); + + \wp_set_current_user( 0 ); + $this->assertFalse( \Activitypub\user_can_see_stats(), 'A logged out visitor does not see the widget.' ); + } + + /** + * Test that the stats widget check answers for the given user, not the current one. + * + * @covers ::user_can_see_stats + */ + public function test_user_can_see_stats_for_a_given_user() { + $author_id = self::factory()->user->create( array( 'role' => 'author' ) ); + + \wp_set_current_user( 0 ); + + $this->assertTrue( \Activitypub\user_can_see_stats( $author_id ), 'The author sees the widget regardless of who is logged in.' ); + } + + /** + * Test that no user ID is rejected in every actor mode. + * + * The blog actor's own ID is zero, so `user_can_activitypub( 0 )` answers for the blog + * actor rather than for a user. The stats widget belongs to a user's dashboard, so a + * missing or non-user ID must not inherit that answer. + * + * @covers ::user_can_see_stats + */ + public function test_user_can_see_stats_rejects_non_users() { + foreach ( array( ACTIVITYPUB_ACTOR_MODE, ACTIVITYPUB_BLOG_MODE, ACTIVITYPUB_ACTOR_AND_BLOG_MODE ) as $mode ) { + \update_option( 'activitypub_actor_mode', $mode ); + + $this->assertFalse( \Activitypub\user_can_see_stats( 0 ), "User 0 is nobody and sees nothing in $mode." ); + $this->assertFalse( \Activitypub\user_can_see_stats( \Activitypub\Collection\Actors::APPLICATION_USER_ID ), "The application actor has no dashboard in $mode." ); + } + } + + /** + * Test that an administrator sees the widget through the blog branch in blog-only mode, + * while an author does not. + * + * @covers ::user_can_see_stats + */ + public function test_user_can_see_stats_in_blog_mode() { + $author_id = self::factory()->user->create( array( 'role' => 'author' ) ); + $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + + \update_option( 'activitypub_actor_mode', ACTIVITYPUB_BLOG_MODE ); + + $this->assertTrue( \Activitypub\user_can_see_stats( $admin_id ), 'An administrator sees the widget through the blog branch.' ); + $this->assertFalse( \Activitypub\user_can_see_stats( $author_id ), 'An author has no blog access and no user actor in blog mode.' ); + } + + /** + * Test that a forced ActivityPub capability does not grant access while the user actor + * type is disabled. + * + * @covers ::user_can_see_stats + */ + public function test_user_can_see_stats_ignores_forced_capability_in_blog_mode() { + $author_id = self::factory()->user->create( array( 'role' => 'author' ) ); + + \update_option( 'activitypub_actor_mode', ACTIVITYPUB_BLOG_MODE ); + \add_filter( 'activitypub_user_can_activitypub', '__return_true' ); + + $can_see = \Activitypub\user_can_see_stats( $author_id ); + + \remove_filter( 'activitypub_user_can_activitypub', '__return_true' ); + + $this->assertFalse( $can_see, 'The actor type check runs after the capability filter.' ); + } } diff --git a/tests/phpunit/tests/includes/scheduler/class-test-statistics.php b/tests/phpunit/tests/includes/scheduler/class-test-statistics.php index fa54124f15..10bd0eaa68 100644 --- a/tests/phpunit/tests/includes/scheduler/class-test-statistics.php +++ b/tests/phpunit/tests/includes/scheduler/class-test-statistics.php @@ -411,6 +411,30 @@ public function test_send_monthly_email_adds_stats_url_to_alt_body() { ); } + /** + * Test that the annual report's plain text alternative also carries the stats URL. + * + * @covers ::send_annual_email + */ + public function test_send_annual_email_adds_stats_url_to_alt_body() { + \update_user_option( self::$user_id, 'activitypub_mailer_annual_report', '1' ); + + // Let wp_mail run so Mailer's phpmailer_init callback assigns AltBody. + \remove_filter( 'pre_wp_mail', array( $this, 'short_circuit_wp_mail' ), 10 ); + \add_filter( 'wp_mail_from', array( $this, 'valid_mail_from' ) ); + \add_action( 'phpmailer_init', array( $this, 'capture_alt_body' ), 11 ); + + Statistics_Scheduler::send_annual_email( self::$user_id, 2023, array( 'posts_count' => 8 ) ); + + \remove_filter( 'wp_mail_from', array( $this, 'valid_mail_from' ) ); + + $this->assertStringContainsString( + \admin_url( 'index.php#activitypub_stats' ), + $this->sent_alt_body, + 'Annual plain text body should include the stats dashboard URL.' + ); + } + /** * Provide a From address PHPMailer accepts. * diff --git a/tests/phpunit/tests/includes/wp-admin/class-test-dashboard.php b/tests/phpunit/tests/includes/wp-admin/class-test-dashboard.php deleted file mode 100644 index 0a49f835c8..0000000000 --- a/tests/phpunit/tests/includes/wp-admin/class-test-dashboard.php +++ /dev/null @@ -1,109 +0,0 @@ -user->create( array( 'role' => 'author' ) ); - self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) ); - } - - /** - * Test that the check defaults to the current user when no ID is given. - * - * @covers ::user_can_see_stats - */ - public function test_user_can_see_stats_defaults_to_current_user() { - \wp_set_current_user( self::$author_id ); - $this->assertTrue( Dashboard::user_can_see_stats(), 'An ActivityPub-enabled author sees the widget.' ); - - \wp_set_current_user( 0 ); - $this->assertFalse( Dashboard::user_can_see_stats(), 'A logged out visitor does not see the widget.' ); - } - - /** - * Test that the check answers for the given user rather than the current one. - * - * @covers ::user_can_see_stats - */ - public function test_user_can_see_stats_for_a_given_user() { - \wp_set_current_user( 0 ); - - $this->assertTrue( Dashboard::user_can_see_stats( self::$author_id ), 'The author sees the widget regardless of who is logged in.' ); - $this->assertFalse( Dashboard::user_can_see_stats( 0 ), 'User 0 is nobody and sees nothing.' ); - } - - /** - * Test that an administrator sees the widget through the blog branch when only the - * blog actor is enabled. - * - * @covers ::user_can_see_stats - */ - public function test_user_can_see_stats_in_blog_mode() { - \update_option( 'activitypub_actor_mode', ACTIVITYPUB_BLOG_MODE ); - - $this->assertTrue( Dashboard::user_can_see_stats( self::$admin_id ), 'An administrator sees the widget through the blog branch.' ); - $this->assertFalse( Dashboard::user_can_see_stats( self::$author_id ), 'An author has no blog access and no user actor in blog mode.' ); - } - - /** - * Test that a forced ActivityPub capability does not grant access while the user actor - * type is disabled. - * - * @covers ::user_can_see_stats - */ - public function test_user_can_see_stats_ignores_forced_capability_in_blog_mode() { - \update_option( 'activitypub_actor_mode', ACTIVITYPUB_BLOG_MODE ); - \add_filter( 'activitypub_user_can_activitypub', '__return_true' ); - - $can_see = Dashboard::user_can_see_stats( self::$author_id ); - - \remove_filter( 'activitypub_user_can_activitypub', '__return_true' ); - - $this->assertFalse( $can_see, 'The actor type check runs after the capability filter.' ); - } - - /** - * Test that only the blog actor being disabled does not remove user access. - * - * @covers ::user_can_see_stats - */ - public function test_user_can_see_stats_in_actor_mode() { - \update_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE ); - - $this->assertTrue( Dashboard::user_can_see_stats( self::$author_id ), 'The author still sees the widget through the user branch.' ); - } -} From c5d44852508befec4041dc5c04cf86a9e0cf3077 Mon Sep 17 00:00:00 2001 From: Allison Levine Date: Thu, 5 Nov 2026 11:01:21 -0500 Subject: [PATCH 5/9] Fix invalid @covers annotations on the stats gate tests Test_Functions_User has no @coversDefaultClass, so "::user_can_see_stats" resolved to a global symbol that does not exist and PHPUnit warned on each test. Use the fully qualified function name, as the other tests in the file already do. --- .../tests/includes/class-test-functions-user.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/includes/class-test-functions-user.php b/tests/phpunit/tests/includes/class-test-functions-user.php index efbabb3807..038bee5bd9 100644 --- a/tests/phpunit/tests/includes/class-test-functions-user.php +++ b/tests/phpunit/tests/includes/class-test-functions-user.php @@ -185,7 +185,7 @@ public function test_get_active_users_with_custom_post_types() { /** * Test that the stats widget check defaults to the current user. * - * @covers ::user_can_see_stats + * @covers \Activitypub\user_can_see_stats */ public function test_user_can_see_stats_defaults_to_current_user() { $author_id = self::factory()->user->create( array( 'role' => 'author' ) ); @@ -200,7 +200,7 @@ public function test_user_can_see_stats_defaults_to_current_user() { /** * Test that the stats widget check answers for the given user, not the current one. * - * @covers ::user_can_see_stats + * @covers \Activitypub\user_can_see_stats */ public function test_user_can_see_stats_for_a_given_user() { $author_id = self::factory()->user->create( array( 'role' => 'author' ) ); @@ -217,7 +217,7 @@ public function test_user_can_see_stats_for_a_given_user() { * actor rather than for a user. The stats widget belongs to a user's dashboard, so a * missing or non-user ID must not inherit that answer. * - * @covers ::user_can_see_stats + * @covers \Activitypub\user_can_see_stats */ public function test_user_can_see_stats_rejects_non_users() { foreach ( array( ACTIVITYPUB_ACTOR_MODE, ACTIVITYPUB_BLOG_MODE, ACTIVITYPUB_ACTOR_AND_BLOG_MODE ) as $mode ) { @@ -232,7 +232,7 @@ public function test_user_can_see_stats_rejects_non_users() { * Test that an administrator sees the widget through the blog branch in blog-only mode, * while an author does not. * - * @covers ::user_can_see_stats + * @covers \Activitypub\user_can_see_stats */ public function test_user_can_see_stats_in_blog_mode() { $author_id = self::factory()->user->create( array( 'role' => 'author' ) ); @@ -248,7 +248,7 @@ public function test_user_can_see_stats_in_blog_mode() { * Test that a forced ActivityPub capability does not grant access while the user actor * type is disabled. * - * @covers ::user_can_see_stats + * @covers \Activitypub\user_can_see_stats */ public function test_user_can_see_stats_ignores_forced_capability_in_blog_mode() { $author_id = self::factory()->user->create( array( 'role' => 'author' ) ); From 7a6ad48e843d1cb88ba7be10636cd96e1c070990 Mon Sep 17 00:00:00 2001 From: Allison Levine Date: Thu, 5 Nov 2026 11:32:48 -0500 Subject: [PATCH 6/9] Nudge GitHub to resync the PR head The previous push moved the branch ref but GitHub did not update the PR or run CI against it. Empty commit; safe to drop. From bed9c5d14167d76b7ba7c58d083b7310acc57ed1 Mon Sep 17 00:00:00 2001 From: Allison Levine Date: Wed, 11 Nov 2026 09:31:53 -0500 Subject: [PATCH 7/9] Always add the stats link, drop the gate Who receives a report is already decided before the link is built: reports only go to the actors in Actors::get_all_ids(), should_send_report() checks the opt-in, and Mailer::send() sends each one to that actor's own address. get_stats_url() checked the same thing a second time. Dropping it also drops the move of user_can_see_stats() to functions-user.php, so the gate stays private in Dashboard as before, and the two ID 0 special cases that only cancelled each other out are gone. The URL and the plain text line are now built once in send_report() rather than in both senders. --- includes/functions-user.php | 31 ------- includes/scheduler/class-statistics.php | 51 ++++-------- includes/wp-admin/class-dashboard.php | 17 +++- .../includes/class-test-functions-user.php | 81 ------------------ .../scheduler/class-test-statistics.php | 83 +------------------ 5 files changed, 32 insertions(+), 231 deletions(-) diff --git a/includes/functions-user.php b/includes/functions-user.php index f4ee986f22..615e5648d0 100644 --- a/includes/functions-user.php +++ b/includes/functions-user.php @@ -151,37 +151,6 @@ function user_can_act_as_blog() { return (bool) \apply_filters( 'activitypub_user_can_act_as_blog', \current_user_can( 'manage_options' ) ); } -/** - * Checks whether a user can see the Fediverse Stats dashboard widget. - * - * A user reaches the widget either through their own actor, or through the blog - * actor if they can manage the site. The blog actor itself is not a user and has - * no dashboard, so `BLOG_USER_ID` and any other non-user ID are rejected: callers - * acting for the blog actor decide for themselves who its report reaches. - * - * @since unreleased - * - * @param int|null $user_id Optional. The user to check. Default null, the current user. - * - * @return bool True if the user can see the stats widget. - */ -function user_can_see_stats( $user_id = null ) { - if ( null === $user_id ) { - $user_id = \get_current_user_id(); - } - - $user_id = (int) $user_id; - - if ( $user_id <= Actors::BLOG_USER_ID ) { - return false; - } - - $has_user_access = user_can_activitypub( $user_id ) && ! is_user_type_disabled( 'user' ); - $has_blog_access = ! is_user_type_disabled( 'blog' ) && \user_can( $user_id, 'manage_options' ); - - return $has_user_access || $has_blog_access; -} - /** * Checks if a User-Type is disabled for ActivityPub. * diff --git a/includes/scheduler/class-statistics.php b/includes/scheduler/class-statistics.php index eeedf5c8ea..3346f449be 100644 --- a/includes/scheduler/class-statistics.php +++ b/includes/scheduler/class-statistics.php @@ -13,9 +13,6 @@ use Activitypub\Mailer; use Activitypub\Statistics as Statistics_Collector; -use function Activitypub\is_user_type_disabled; -use function Activitypub\user_can_see_stats; - /** * Statistics scheduler class. */ @@ -169,7 +166,6 @@ public static function send_annual_email( $user_id, $year, $summary, $force = fa 'most_active_month_name' => $most_active_month_name, 'followers_text' => $followers_text, 'supporter_text' => $supporter_text, - 'stats_url' => self::get_stats_url( $user_id ), 'user_id' => $user_id, ) ); @@ -200,12 +196,7 @@ public static function send_annual_email( $user_id, $year, $summary, $force = fa $alt_body .= \sprintf( \__( "Most active month: %s\n", 'activitypub' ), $most_active_month_name ); } - if ( ! empty( $args['stats_url'] ) ) { - /* translators: %s: URL of the Fediverse Stats dashboard widget */ - $alt_body .= \sprintf( \__( "\nView your stats: %s\n", 'activitypub' ), $args['stats_url'] ); - } - - Mailer::send( $user_id, $subject, 'stats-report', $args, $alt_body ); + self::send_report( $user_id, $subject, $args, $alt_body ); } /** @@ -277,7 +268,6 @@ public static function send_monthly_email( $user_id, $year, $month, $force = fal 'closing' => \__( 'Keep sharing great content on the Fediverse!', 'activitypub' ), 'followers_text' => $followers_text, 'supporter_text' => $supporter_text, - 'stats_url' => self::get_stats_url( $user_id ), 'user_id' => $user_id, ) ); @@ -303,38 +293,31 @@ public static function send_monthly_email( $user_id, $year, $month, $force = fal $alt_body .= \sprintf( \__( "New followers: %+d\n", 'activitypub' ), $stats['followers_count'] ); } - if ( ! empty( $args['stats_url'] ) ) { - /* translators: %s: URL of the Fediverse Stats dashboard widget */ - $alt_body .= \sprintf( \__( "\nView your stats: %s\n", 'activitypub' ), $args['stats_url'] ); - } - - Mailer::send( $user_id, $subject, 'stats-report', $args, $alt_body ); + self::send_report( $user_id, $subject, $args, $alt_body ); } /** - * Get the URL of the Fediverse Stats dashboard widget for a report recipient. + * Send a stats report, adding the link to the Fediverse Stats dashboard widget. * - * Defers to the gate that registers the widget, so the link is only added when the - * recipient would actually see the widget on their dashboard. The blog actor is not a - * WordPress user, so it cannot be passed to that gate: its report goes to `admin_email`, - * which stands in for the `manage_options` branch. + * Both reports carry the same link, so it is built here rather than in each sender. + * Who receives a report is already decided before this point: reports only go to the + * actors in `Actors::get_all_ids()`, and `Mailer::send()` sends each one to that + * actor's own address. * - * @param int $user_id The user ID, or `Actors::BLOG_USER_ID` for the blog actor. + * @param int $user_id The user ID. + * @param string $subject The email subject. + * @param array $args Template arguments. + * @param string $alt_body The plain text alternative. * - * @return string The dashboard URL, or an empty string if the recipient cannot see the widget. + * @return bool Whether the email was sent successfully. */ - private static function get_stats_url( $user_id ) { - if ( Actors::BLOG_USER_ID === $user_id ) { - $can_see_stats = ! is_user_type_disabled( 'blog' ); - } else { - $can_see_stats = user_can_see_stats( $user_id ); - } + private static function send_report( $user_id, $subject, $args, $alt_body ) { + $args['stats_url'] = \admin_url( 'index.php#activitypub_stats' ); - if ( ! $can_see_stats ) { - return ''; - } + /* translators: %s: URL of the Fediverse Stats dashboard widget */ + $alt_body .= \sprintf( \__( "\nView your stats: %s\n", 'activitypub' ), $args['stats_url'] ); - return \admin_url( 'index.php#activitypub_stats' ); + return Mailer::send( $user_id, $subject, 'stats-report', $args, $alt_body ); } /** diff --git a/includes/wp-admin/class-dashboard.php b/includes/wp-admin/class-dashboard.php index d31e00c5a5..f10017f936 100644 --- a/includes/wp-admin/class-dashboard.php +++ b/includes/wp-admin/class-dashboard.php @@ -12,7 +12,6 @@ use function Activitypub\is_user_type_disabled; use function Activitypub\user_can_activitypub; -use function Activitypub\user_can_see_stats; /** * Dashboard Widgets Class. @@ -63,7 +62,7 @@ public static function add_dashboard_widgets() { } // Stats widget. - if ( user_can_see_stats() ) { + if ( self::user_can_see_stats() ) { \wp_add_dashboard_widget( 'activitypub_stats', \__( 'Fediverse Stats', 'activitypub' ), @@ -89,7 +88,7 @@ public static function enqueue_scripts( $hook ) { } // Only enqueue if user has access to stats. - if ( ! user_can_see_stats() ) { + if ( ! self::user_can_see_stats() ) { return; } @@ -141,6 +140,18 @@ public static function enqueue_scripts( $hook ) { ); } + /** + * Check if the current user can see the stats widget. + * + * @return bool True if user has access. + */ + private static function user_can_see_stats() { + $has_user_access = user_can_activitypub( \get_current_user_id() ) && ! is_user_type_disabled( 'user' ); + $has_blog_access = ! is_user_type_disabled( 'blog' ) && \current_user_can( 'manage_options' ); + + return $has_user_access || $has_blog_access; + } + /** * Render the ActivityPub.blog news feed widget. * diff --git a/tests/phpunit/tests/includes/class-test-functions-user.php b/tests/phpunit/tests/includes/class-test-functions-user.php index 038bee5bd9..2716a82983 100644 --- a/tests/phpunit/tests/includes/class-test-functions-user.php +++ b/tests/phpunit/tests/includes/class-test-functions-user.php @@ -181,85 +181,4 @@ public function test_get_active_users_with_custom_post_types() { $cleanup(); } } - - /** - * Test that the stats widget check defaults to the current user. - * - * @covers \Activitypub\user_can_see_stats - */ - public function test_user_can_see_stats_defaults_to_current_user() { - $author_id = self::factory()->user->create( array( 'role' => 'author' ) ); - - \wp_set_current_user( $author_id ); - $this->assertTrue( \Activitypub\user_can_see_stats(), 'An ActivityPub-enabled author sees the widget.' ); - - \wp_set_current_user( 0 ); - $this->assertFalse( \Activitypub\user_can_see_stats(), 'A logged out visitor does not see the widget.' ); - } - - /** - * Test that the stats widget check answers for the given user, not the current one. - * - * @covers \Activitypub\user_can_see_stats - */ - public function test_user_can_see_stats_for_a_given_user() { - $author_id = self::factory()->user->create( array( 'role' => 'author' ) ); - - \wp_set_current_user( 0 ); - - $this->assertTrue( \Activitypub\user_can_see_stats( $author_id ), 'The author sees the widget regardless of who is logged in.' ); - } - - /** - * Test that no user ID is rejected in every actor mode. - * - * The blog actor's own ID is zero, so `user_can_activitypub( 0 )` answers for the blog - * actor rather than for a user. The stats widget belongs to a user's dashboard, so a - * missing or non-user ID must not inherit that answer. - * - * @covers \Activitypub\user_can_see_stats - */ - public function test_user_can_see_stats_rejects_non_users() { - foreach ( array( ACTIVITYPUB_ACTOR_MODE, ACTIVITYPUB_BLOG_MODE, ACTIVITYPUB_ACTOR_AND_BLOG_MODE ) as $mode ) { - \update_option( 'activitypub_actor_mode', $mode ); - - $this->assertFalse( \Activitypub\user_can_see_stats( 0 ), "User 0 is nobody and sees nothing in $mode." ); - $this->assertFalse( \Activitypub\user_can_see_stats( \Activitypub\Collection\Actors::APPLICATION_USER_ID ), "The application actor has no dashboard in $mode." ); - } - } - - /** - * Test that an administrator sees the widget through the blog branch in blog-only mode, - * while an author does not. - * - * @covers \Activitypub\user_can_see_stats - */ - public function test_user_can_see_stats_in_blog_mode() { - $author_id = self::factory()->user->create( array( 'role' => 'author' ) ); - $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); - - \update_option( 'activitypub_actor_mode', ACTIVITYPUB_BLOG_MODE ); - - $this->assertTrue( \Activitypub\user_can_see_stats( $admin_id ), 'An administrator sees the widget through the blog branch.' ); - $this->assertFalse( \Activitypub\user_can_see_stats( $author_id ), 'An author has no blog access and no user actor in blog mode.' ); - } - - /** - * Test that a forced ActivityPub capability does not grant access while the user actor - * type is disabled. - * - * @covers \Activitypub\user_can_see_stats - */ - public function test_user_can_see_stats_ignores_forced_capability_in_blog_mode() { - $author_id = self::factory()->user->create( array( 'role' => 'author' ) ); - - \update_option( 'activitypub_actor_mode', ACTIVITYPUB_BLOG_MODE ); - \add_filter( 'activitypub_user_can_activitypub', '__return_true' ); - - $can_see = \Activitypub\user_can_see_stats( $author_id ); - - \remove_filter( 'activitypub_user_can_activitypub', '__return_true' ); - - $this->assertFalse( $can_see, 'The actor type check runs after the capability filter.' ); - } } diff --git a/tests/phpunit/tests/includes/scheduler/class-test-statistics.php b/tests/phpunit/tests/includes/scheduler/class-test-statistics.php index 10bd0eaa68..cdb6ab84c5 100644 --- a/tests/phpunit/tests/includes/scheduler/class-test-statistics.php +++ b/tests/phpunit/tests/includes/scheduler/class-test-statistics.php @@ -284,17 +284,13 @@ public function test_send_annual_email_links_to_stats_widget() { } /** - * Test that the blog actor's report links to the stats widget, since its report - * goes to admin_email. + * Test that the blog actor's report links to the stats widget. * * @covers ::send_monthly_email */ public function test_send_monthly_email_links_to_stats_widget_for_blog_actor() { $year = 2026; $month = 6; - - // The widget's blog branch requires the blog actor to be enabled. - \update_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_AND_BLOG_MODE ); $this->seed_monthly_stats( Actors::BLOG_USER_ID, $year, $month ); Statistics_Scheduler::send_monthly_email( Actors::BLOG_USER_ID, $year, $month ); @@ -307,83 +303,6 @@ public function test_send_monthly_email_links_to_stats_widget_for_blog_actor() { ); } - /** - * Test that no stats link is added when the recipient cannot see the dashboard widget. - * - * @covers ::send_monthly_email - */ - public function test_send_monthly_email_omits_stats_link_without_access() { - $year = 2026; - $month = 7; - $this->seed_monthly_stats( self::$user_id, $year, $month ); - - \add_filter( 'activitypub_user_can_activitypub', '__return_false' ); - Statistics_Scheduler::send_monthly_email( self::$user_id, $year, $month ); - \remove_filter( 'activitypub_user_can_activitypub', '__return_false' ); - - $this->assertSame( 1, $this->sent_count, 'Monthly email should still be sent.' ); - $this->assertStringNotContainsString( - 'activitypub_stats', - $this->sent_message, - 'Email should not link to a dashboard widget the recipient cannot see.' - ); - $this->assertStringNotContainsString( 'View your stats', $this->sent_message, 'Email should not show the stats link label.' ); - } - - /** - * Test that no stats link is added when user mode is disabled, even if a filter forces - * the ActivityPub capability on. The widget's own gate re-checks the actor type after - * that filter, so the link would otherwise point at a dashboard without the widget. - * - * @covers ::send_monthly_email - */ - public function test_send_monthly_email_omits_stats_link_when_user_mode_disabled() { - $year = 2026; - $month = 9; - $this->seed_monthly_stats( self::$user_id, $year, $month ); - - // Blog-only mode disables the user actor type. - \update_option( 'activitypub_actor_mode', ACTIVITYPUB_BLOG_MODE ); - \add_filter( 'activitypub_user_can_activitypub', '__return_true' ); - - Statistics_Scheduler::send_monthly_email( self::$user_id, $year, $month ); - - \remove_filter( 'activitypub_user_can_activitypub', '__return_true' ); - - $this->assertSame( 1, $this->sent_count, 'Monthly email should still be sent.' ); - $this->assertStringNotContainsString( - 'activitypub_stats', - $this->sent_message, - 'A forced capability should not add a link to a widget the actor type disables.' - ); - } - - /** - * Test that an administrator still gets the link when only the blog actor is enabled, - * since the widget's blog branch shows it to anyone who can manage options. - * - * @covers ::send_monthly_email - */ - public function test_send_monthly_email_links_to_stats_widget_for_admin_in_blog_mode() { - $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); - \update_user_option( $admin_id, 'activitypub_mailer_monthly_report', '1' ); - - $year = 2026; - $month = 10; - $this->seed_monthly_stats( $admin_id, $year, $month ); - - \update_option( 'activitypub_actor_mode', ACTIVITYPUB_BLOG_MODE ); - - Statistics_Scheduler::send_monthly_email( $admin_id, $year, $month ); - - $this->assertSame( 1, $this->sent_count, 'Monthly email should be sent.' ); - $this->assertStringContainsString( - \esc_url( \admin_url( 'index.php#activitypub_stats' ) ), - $this->sent_message, - 'An administrator sees the widget through its blog branch, so the link belongs in the email.' - ); - } - /** * Test that the plain text alternative also carries the stats URL. * From 031022018c0250ff2243823ac3904f5eb63a7d70 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 11 Nov 2026 16:59:22 +0100 Subject: [PATCH 8/9] keep the newline out of the translatable string --- includes/scheduler/class-statistics.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/scheduler/class-statistics.php b/includes/scheduler/class-statistics.php index 3346f449be..9665c747a6 100644 --- a/includes/scheduler/class-statistics.php +++ b/includes/scheduler/class-statistics.php @@ -314,8 +314,9 @@ public static function send_monthly_email( $user_id, $year, $month, $force = fal private static function send_report( $user_id, $subject, $args, $alt_body ) { $args['stats_url'] = \admin_url( 'index.php#activitypub_stats' ); + $alt_body .= "\n"; /* translators: %s: URL of the Fediverse Stats dashboard widget */ - $alt_body .= \sprintf( \__( "\nView your stats: %s\n", 'activitypub' ), $args['stats_url'] ); + $alt_body .= \sprintf( \__( "View your stats: %s\n", 'activitypub' ), $args['stats_url'] ); return Mailer::send( $user_id, $subject, 'stats-report', $args, $alt_body ); } From 9a491e6ec831d6ab73edee8c116300e8a26f1e17 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 11 Nov 2026 17:16:39 +0100 Subject: [PATCH 9/9] build the stats link where the rest of the email is built --- includes/scheduler/class-statistics.php | 31 +++++++------------------ 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/includes/scheduler/class-statistics.php b/includes/scheduler/class-statistics.php index 9665c747a6..c8a07a4d1d 100644 --- a/includes/scheduler/class-statistics.php +++ b/includes/scheduler/class-statistics.php @@ -166,6 +166,7 @@ public static function send_annual_email( $user_id, $year, $summary, $force = fa 'most_active_month_name' => $most_active_month_name, 'followers_text' => $followers_text, 'supporter_text' => $supporter_text, + 'stats_url' => \admin_url( 'index.php#activitypub_stats' ), 'user_id' => $user_id, ) ); @@ -196,7 +197,11 @@ public static function send_annual_email( $user_id, $year, $summary, $force = fa $alt_body .= \sprintf( \__( "Most active month: %s\n", 'activitypub' ), $most_active_month_name ); } - self::send_report( $user_id, $subject, $args, $alt_body ); + $alt_body .= "\n"; + /* translators: %s: URL of the Fediverse Stats dashboard widget */ + $alt_body .= \sprintf( \__( "View your stats: %s\n", 'activitypub' ), $args['stats_url'] ); + + Mailer::send( $user_id, $subject, 'stats-report', $args, $alt_body ); } /** @@ -268,6 +273,7 @@ public static function send_monthly_email( $user_id, $year, $month, $force = fal 'closing' => \__( 'Keep sharing great content on the Fediverse!', 'activitypub' ), 'followers_text' => $followers_text, 'supporter_text' => $supporter_text, + 'stats_url' => \admin_url( 'index.php#activitypub_stats' ), 'user_id' => $user_id, ) ); @@ -293,32 +299,11 @@ public static function send_monthly_email( $user_id, $year, $month, $force = fal $alt_body .= \sprintf( \__( "New followers: %+d\n", 'activitypub' ), $stats['followers_count'] ); } - self::send_report( $user_id, $subject, $args, $alt_body ); - } - - /** - * Send a stats report, adding the link to the Fediverse Stats dashboard widget. - * - * Both reports carry the same link, so it is built here rather than in each sender. - * Who receives a report is already decided before this point: reports only go to the - * actors in `Actors::get_all_ids()`, and `Mailer::send()` sends each one to that - * actor's own address. - * - * @param int $user_id The user ID. - * @param string $subject The email subject. - * @param array $args Template arguments. - * @param string $alt_body The plain text alternative. - * - * @return bool Whether the email was sent successfully. - */ - private static function send_report( $user_id, $subject, $args, $alt_body ) { - $args['stats_url'] = \admin_url( 'index.php#activitypub_stats' ); - $alt_body .= "\n"; /* translators: %s: URL of the Fediverse Stats dashboard widget */ $alt_body .= \sprintf( \__( "View your stats: %s\n", 'activitypub' ), $args['stats_url'] ); - return Mailer::send( $user_id, $subject, 'stats-report', $args, $alt_body ); + Mailer::send( $user_id, $subject, 'stats-report', $args, $alt_body ); } /**