From 560443e4b8c0c3d148b314d19f5393646eb44d45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Fri, 28 Aug 2026 08:55:25 +0100 Subject: [PATCH 1/9] fix widget record test to use the manifest fixture a local build/ redeclared the stub on first use --- .../tests/php/Widget_Metadata_Test.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php b/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php index 47f6c18706ac..f4c821ccd8ed 100644 --- a/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php +++ b/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php @@ -267,9 +267,19 @@ public function test_widget_modules_record_carries_metadata() { ) ); - $records = get_widget_modules_response()->get_data(); + // A local build/ would otherwise be required on first use and redeclare the fixture stub. + $manifest_path = static function () { + return __DIR__ . '/fixtures/build-entry/widgets.php'; + }; + add_filter( 'jetpack_premium_analytics_widgets_manifest_path', $manifest_path ); - $registry->unregister( 'test/metadata-sentinel' ); + $records = array(); + try { + $records = get_widget_modules_response()->get_data(); + } finally { + remove_filter( 'jetpack_premium_analytics_widgets_manifest_path', $manifest_path ); + $registry->unregister( 'test/metadata-sentinel' ); + } $record = null; foreach ( $records as $candidate ) { From 7607a030b1e4bb2af24d6f0249ef191c0a0dbdfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Fri, 28 Aug 2026 08:56:13 +0100 Subject: [PATCH 2/9] add icon and actions to the widget type registry ports the Gutenberg v23.8.0 sanitizers; widget-local hrefs never resolve --- .../update-pa-widget-dashboard-backend | 4 + .../src/class-widget-type.php | 22 ++ .../premium-analytics/src/widget-i18n.json | 1 + .../premium-analytics/src/widget-types.php | 163 ++++++++++- .../tests/php/Widget_Metadata_Test.php | 268 ++++++++++++++++++ 5 files changed, 452 insertions(+), 6 deletions(-) create mode 100644 projects/packages/premium-analytics/changelog/update-pa-widget-dashboard-backend diff --git a/projects/packages/premium-analytics/changelog/update-pa-widget-dashboard-backend b/projects/packages/premium-analytics/changelog/update-pa-widget-dashboard-backend new file mode 100644 index 000000000000..c73c26c0ff5d --- /dev/null +++ b/projects/packages/premium-analytics/changelog/update-pa-widget-dashboard-backend @@ -0,0 +1,4 @@ +Significance: patch +Type: added + +Dashboard widgets: carry declarative actions and icon references from the widget manifest to the registry and the widget-modules REST record. diff --git a/projects/packages/premium-analytics/src/class-widget-type.php b/projects/packages/premium-analytics/src/class-widget-type.php index 5c98791a8521..f3b9fd591c01 100644 --- a/projects/packages/premium-analytics/src/class-widget-type.php +++ b/projects/packages/premium-analytics/src/class-widget-type.php @@ -112,6 +112,28 @@ class Widget_Type { */ public $help = null; + /** + * Registered icon name (`collection/icon-name`), resolved on the client + * through the application's icon resolver. + * + * Null when the widget did not declare the field. + * + * @var string|null + */ + public $icon = null; + + /** + * Declarative actions the widget exposes. Each entry carries `id`, + * `label`, `href`, and optional `download`/`openInNewTab`/`icon`/ + * `relevance`. Labels are translated at registration time using the + * widget's text domain. + * + * Null when the widget did not declare the field. + * + * @var array|null + */ + public $actions = null; + /** * Alternative terms used to match the widget type when searching, * e.g. "calendar" for an events widget. Translated at registration diff --git a/projects/packages/premium-analytics/src/widget-i18n.json b/projects/packages/premium-analytics/src/widget-i18n.json index 859fa3ef5dc8..a5ef9b522f6a 100644 --- a/projects/packages/premium-analytics/src/widget-i18n.json +++ b/projects/packages/premium-analytics/src/widget-i18n.json @@ -5,5 +5,6 @@ "content": "widget help content", "links": [ { "label": "widget help link label" } ] }, + "actions": [ { "label": "widget action label" } ], "keywords": [ "widget keyword" ] } diff --git a/projects/packages/premium-analytics/src/widget-types.php b/projects/packages/premium-analytics/src/widget-types.php index 84e69bca4b42..d77a72ee5aea 100644 --- a/projects/packages/premium-analytics/src/widget-types.php +++ b/projects/packages/premium-analytics/src/widget-types.php @@ -5,7 +5,8 @@ * Copies the wp-build manifest (`jpa_get_registered_widget_modules()`) into the * in-memory Widget_Type_Registry, so the plugin queries the registry instead * of re-parsing the manifest. On the way in, user-facing metadata strings are - * translated (per the widget-i18n.json schema) and the `help` note sanitized. + * translated (per the widget-i18n.json schema) and the `help`, `icon`, and + * `actions` fields sanitized. * * This is the problem-agnostic "core" layer (a PA-namespaced copy of the * experimental Gutenberg API): it exposes the hooks a consumer uses to scope @@ -58,10 +59,11 @@ function get_widget_metadata_i18n_schema() { /** * Translates a widget's user-facing metadata strings. * - * Runs `title`, `description`, `help`, and `keywords` through the widget - * i18n schema, leaving every other key untouched. Unlike the upstream copy, - * a widget with no `textdomain` falls back to the package text domain - * instead of skipping translation: every bundled widget shares it. + * Runs `title`, `description`, `help`, `actions`, and `keywords` through + * the widget i18n schema, leaving every other key untouched. Unlike the + * upstream copy, a widget with no `textdomain` falls back to the package + * text domain instead of skipping translation: every bundled widget + * shares it. * * @param array $widget Widget data from the build manifest. * @return array Widget data with its translatable strings localized. @@ -70,7 +72,7 @@ function translate_widget_metadata( $widget ) { $textdomain = ! empty( $widget['textdomain'] ) ? $widget['textdomain'] : 'jetpack-premium-analytics-pkg'; $i18n_schema = get_widget_metadata_i18n_schema(); - foreach ( array( 'title', 'description', 'help', 'keywords' ) as $field ) { + foreach ( array( 'title', 'description', 'help', 'actions', 'keywords' ) as $field ) { if ( isset( $widget[ $field ] ) && isset( $i18n_schema->$field ) ) { $widget[ $field ] = translate_settings_using_i18n_schema( $i18n_schema->$field, $widget[ $field ], $textdomain ); } @@ -125,6 +127,153 @@ function sanitize_widget_help( $help ) { return $sanitized; } +/** + * Resolves an action href to the form `esc_url_raw()` can judge. + * + * Absolute, scheme-relative, root-relative, and single-segment admin `.php` + * hrefs pass through unchanged. Returns '' for path traversal and for any + * other relative href, so `esc_url_raw()` cannot invent `http://filename`. + * Unlike upstream, widget-local files are never resolved: `widgets/` does + * not ship with the package, only its build output does. + * + * @param string $href Action href. + * @return string The href to escape, or ''. + */ +function resolve_widget_action_href( $href ) { + if ( ! is_string( $href ) || '' === $href ) { + return ''; + } + + // Absolute, scheme-relative, or schemed, including URLs with `..` in the path. + if ( preg_match( '#^([a-z][a-z0-9+.-]*:)?//#i', $href ) || str_contains( $href, ':' ) ) { + return $href; + } + + // Root-relative paths (e.g. /wp-admin/..., /report.csv). + if ( str_starts_with( $href, '/' ) ) { + return $href; + } + + if ( str_contains( $href, '..' ) ) { + return ''; + } + + $path_only = preg_split( '/[?#]/', $href, 2 )[0]; + if ( str_ends_with( strtolower( $path_only ), '.php' ) ) { + // Single-segment admin entry points stay as-is. Deeper relative + // paths would come out of `esc_url_raw()` as `http://` URLs. + return str_contains( $path_only, '/' ) ? '' : $href; + } + + return ''; +} + +/** + * Sanitizes widget actions to `id` / `label` / `href` (via `esc_url_raw()`), + * plus optional `download` / `openInNewTab` / `icon` / `relevance`. Drops + * incomplete or unsafe entries and reports dropped hrefs through + * `_doing_it_wrong()`; a malformed `icon` or `relevance` drops the key, never + * the action. + * + * @param mixed $actions Actions from the build manifest. + * @return array|null Sanitized actions, or null when none survive. + */ +function sanitize_widget_actions( $actions ) { + if ( ! is_array( $actions ) ) { + return null; + } + + $sanitized = array(); + foreach ( $actions as $action ) { + if ( + ! is_array( $action ) || + ! isset( $action['id'] ) || + ! isset( $action['label'] ) || + ! isset( $action['href'] ) || + ! is_string( $action['id'] ) || + ! is_string( $action['label'] ) || + ! is_string( $action['href'] ) || + '' === $action['id'] || + '' === $action['label'] || + '' === $action['href'] + ) { + continue; + } + + $href = esc_url_raw( resolve_widget_action_href( $action['href'] ) ); + if ( ! $href ) { + _doing_it_wrong( + __FUNCTION__, + esc_html( + sprintf( + /* translators: 1: Widget action id. 2: Declared action href. */ + __( 'Dropped widget action "%1$s": href "%2$s" is not an allowed URL.', 'jetpack-premium-analytics-pkg' ), + $action['id'], + $action['href'] + ) + ), + 'jetpack-premium-analytics-$$next-version$$' + ); + continue; + } + + $entry = array( + 'id' => $action['id'], + 'label' => $action['label'], + 'href' => $href, + ); + + if ( isset( $action['download'] ) ) { + if ( is_bool( $action['download'] ) ) { + $entry['download'] = $action['download']; + } else { + $filename = sanitize_file_name( (string) $action['download'] ); + if ( $filename ) { + $entry['download'] = $filename; + } + } + } + + if ( isset( $action['openInNewTab'] ) ) { + $entry['openInNewTab'] = (bool) $action['openInNewTab']; + } + + if ( isset( $action['icon'] ) ) { + $icon = sanitize_widget_icon( $action['icon'] ); + if ( $icon ) { + $entry['icon'] = $icon; + } + } + + if ( isset( $action['relevance'] ) && in_array( $action['relevance'], array( 'high', 'low' ), true ) ) { + $entry['relevance'] = $action['relevance']; + } + + $sanitized[] = $entry; + } + + return $sanitized ? $sanitized : null; +} + +/** + * Constrains a widget icon reference to a registered icon name + * (`collection/icon-name`); anything else drops silently to no icon. + * + * @param mixed $icon Icon reference from the build manifest. + * @return string|null The icon name, or null when the shape does not match. + */ +function sanitize_widget_icon( $icon ) { + if ( ! is_string( $icon ) || '' === $icon ) { + return null; + } + + if ( ! preg_match( '#^[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?/[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?$#', $icon ) ) { + return null; + } + + return $icon; +} + /** * Hydrates the widget type registry from the build manifest. * @@ -173,6 +322,8 @@ function register_widget_types() { 'title' => $widget['title'] ?? null, 'description' => $widget['description'] ?? null, 'help' => sanitize_widget_help( $widget['help'] ?? null ), + 'icon' => sanitize_widget_icon( $widget['icon'] ?? null ), + 'actions' => sanitize_widget_actions( $widget['actions'] ?? null ), 'keywords' => $widget['keywords'] ?? null, ) ); diff --git a/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php b/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php index f4c821ccd8ed..edb604509a1b 100644 --- a/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php +++ b/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php @@ -19,12 +19,18 @@ * @covers ::Automattic\Jetpack\PremiumAnalytics\register_widget_types * @covers ::Automattic\Jetpack\PremiumAnalytics\translate_widget_metadata * @covers ::Automattic\Jetpack\PremiumAnalytics\sanitize_widget_help + * @covers ::Automattic\Jetpack\PremiumAnalytics\sanitize_widget_icon + * @covers ::Automattic\Jetpack\PremiumAnalytics\sanitize_widget_actions + * @covers ::Automattic\Jetpack\PremiumAnalytics\resolve_widget_action_href * @covers ::Automattic\Jetpack\PremiumAnalytics\get_widget_metadata_i18n_schema * @covers ::Automattic\Jetpack\PremiumAnalytics\get_widget_modules_response */ #[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\register_widget_types' )] #[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\translate_widget_metadata' )] #[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\sanitize_widget_help' )] +#[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\sanitize_widget_icon' )] +#[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\sanitize_widget_actions' )] +#[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\resolve_widget_action_href' )] #[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\get_widget_metadata_i18n_schema' )] #[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\get_widget_modules_response' )] class Widget_Metadata_Test extends BaseTestCase { @@ -81,6 +87,36 @@ public function test_translate_widget_metadata_honors_declared_textdomain() { $this->assertContains( array( 'Hello world', 'widget title', 'my-widget-pack' ), $calls, 'The declared textdomain is used for translation.' ); } + /** + * Action labels are translated with the action label context; the other + * action keys pass through untouched. + */ + public function test_translate_widget_metadata_translates_action_labels() { + $calls = array(); + $callback = static function ( $translation, $text, $context, $domain ) use ( &$calls ) { + $calls[] = array( $text, $context, $domain ); + return $translation; + }; + add_filter( 'gettext_with_context', $callback, 10, 4 ); + + $widget = translate_widget_metadata( + array( + 'actions' => array( + array( + 'id' => 'export', + 'label' => 'Export CSV', + 'href' => 'https://example.com/export.csv', + ), + ), + ) + ); + + remove_filter( 'gettext_with_context', $callback ); + + $this->assertContains( array( 'Export CSV', 'widget action label', 'jetpack-premium-analytics-pkg' ), $calls, 'Each action label is translated under the package domain.' ); + $this->assertSame( 'https://example.com/export.csv', $widget['actions'][0]['href'], 'Non-translatable action keys pass through untouched.' ); + } + /** * Hydration registers manifest candidates with metadata translated, * the help note sanitized, and every field mapped onto the type. @@ -104,6 +140,18 @@ public function test_register_widget_types_hydrates_metadata_from_manifest() { ), ), ), + 'icon' => 'core/chart-bar', + 'actions' => array( + array( + 'id' => 'export', + 'label' => 'Export CSV', + 'href' => 'https://example.com/export.csv', + 'download' => true, + 'icon' => 'core/download', + 'relevance' => 'high', + 'extra' => 'dropped', + ), + ), 'keywords' => array( 'sentinel' ), ), ); @@ -134,6 +182,21 @@ public function test_register_widget_types_hydrates_metadata_from_manifest() { $widget_type->help, 'The help note is sanitized during hydration.' ); + $this->assertSame( 'core/chart-bar', $widget_type->icon, 'The icon reference is mapped.' ); + $this->assertSame( + array( + array( + 'id' => 'export', + 'label' => 'Export CSV', + 'href' => 'https://example.com/export.csv', + 'download' => true, + 'icon' => 'core/download', + 'relevance' => 'high', + ), + ), + $widget_type->actions, + 'The actions are sanitized during hydration.' + ); } finally { Widget_Type_Registry::get_instance()->unregister( 'test/hydration-sentinel' ); unset( $GLOBALS['jpa_test_widget_manifest'] ); @@ -247,6 +310,211 @@ public function test_sanitize_widget_help_omits_links_when_none_survive() { ); } + /** + * Only `collection/icon-name` references survive; anything else drops to null. + */ + public function test_sanitize_widget_icon_accepts_only_registered_icon_names() { + $this->assertSame( 'core/chart-bar', sanitize_widget_icon( 'core/chart-bar' ), 'A registered icon name passes through.' ); + $this->assertSame( 'jpa/site_logo-2', sanitize_widget_icon( 'jpa/site_logo-2' ), 'Digits, hyphens and underscores are allowed inside a segment.' ); + $this->assertNull( sanitize_widget_icon( null ), 'Null stays null.' ); + $this->assertNull( sanitize_widget_icon( '' ), 'An empty string drops.' ); + $this->assertNull( sanitize_widget_icon( 'chart-bar' ), 'A bare name without a collection drops.' ); + $this->assertNull( sanitize_widget_icon( 'Core/Plus' ), 'Uppercase letters drop.' ); + $this->assertNull( sanitize_widget_icon( 'core/-plus' ), 'A segment cannot start with a separator.' ); + $this->assertNull( sanitize_widget_icon( 'core/plus/extra' ), 'Only two segments are allowed.' ); + $this->assertNull( sanitize_widget_icon( array( 'core/plus' ) ), 'A non-string reference drops.' ); + } + + /** + * Entries missing a non-empty string id, label or href are dropped; when + * none survive the result is null. + */ + public function test_sanitize_widget_actions_drops_incomplete_entries() { + $this->assertNull( sanitize_widget_actions( null ), 'Null input stays null.' ); + $this->assertNull( sanitize_widget_actions( 'export' ), 'A non-array input is dropped.' ); + $this->assertNull( + sanitize_widget_actions( + array( + 'not-an-action', + array( + 'label' => 'No id', + 'href' => 'https://example.com', + ), + array( + 'id' => 'no-label', + 'href' => 'https://example.com', + ), + array( + 'id' => 'no-href', + 'label' => 'No href', + ), + array( + 'id' => '', + 'label' => 'Empty id', + 'href' => 'https://example.com', + ), + array( + 'id' => 42, + 'label' => 'Numeric id', + 'href' => 'https://example.com', + ), + ) + ), + 'Incomplete entries are dropped and an empty result becomes null.' + ); + } + + /** + * Hrefs go through resolve_widget_action_href() and esc_url_raw(): absolute, + * scheme-relative, root-relative and single-segment admin `.php` hrefs pass; + * everything else drops the action with a _doing_it_wrong() notice. + */ + public function test_sanitize_widget_actions_keeps_only_allowed_hrefs() { + $notices = array(); + $callback = static function ( $function_name ) use ( &$notices ) { + $notices[] = $function_name; + }; + add_action( 'doing_it_wrong_run', $callback ); + add_filter( 'doing_it_wrong_trigger_error', '__return_false' ); + + $actions = sanitize_widget_actions( + array( + array( + 'id' => 'absolute', + 'label' => 'Absolute', + 'href' => 'https://example.com/report.csv?range=7d', + ), + array( + 'id' => 'scheme-relative', + 'label' => 'Scheme relative', + 'href' => '//example.com/report.csv', + ), + array( + 'id' => 'root', + 'label' => 'Root relative', + 'href' => '/wp-admin/export.php?type=csv', + ), + array( + 'id' => 'admin', + 'label' => 'Admin entry point', + 'href' => 'export.php?type=csv', + ), + array( + 'id' => 'script', + 'label' => 'Script', + 'href' => 'javascript:alert(1)', + ), + array( + 'id' => 'traversal', + 'label' => 'Traversal', + 'href' => '../secret.csv', + ), + array( + 'id' => 'nested-php', + 'label' => 'Nested PHP', + 'href' => 'sub/dir.php', + ), + array( + 'id' => 'local-file', + 'label' => 'Local file', + 'href' => 'report.csv', + ), + ) + ); + + remove_filter( 'doing_it_wrong_trigger_error', '__return_false' ); + remove_action( 'doing_it_wrong_run', $callback ); + + $this->assertSame( + array( + array( + 'id' => 'absolute', + 'label' => 'Absolute', + 'href' => 'https://example.com/report.csv?range=7d', + ), + array( + 'id' => 'scheme-relative', + 'label' => 'Scheme relative', + 'href' => '//example.com/report.csv', + ), + array( + 'id' => 'root', + 'label' => 'Root relative', + 'href' => '/wp-admin/export.php?type=csv', + ), + array( + 'id' => 'admin', + 'label' => 'Admin entry point', + 'href' => 'export.php?type=csv', + ), + ), + $actions, + 'Only absolute, scheme-relative, root-relative and single-segment .php hrefs survive, unchanged.' + ); + $this->assertSame( array_fill( 0, 4, __NAMESPACE__ . '\\sanitize_widget_actions' ), $notices, 'Each dropped href is reported through _doing_it_wrong().' ); + } + + /** + * Optional fields are normalized: `download` keeps booleans and sanitizes + * filenames, `openInNewTab` casts to bool, and a malformed `icon` or + * `relevance` drops the key but keeps the action. Unknown keys never ride along. + */ + public function test_sanitize_widget_actions_normalizes_optional_fields() { + $this->assertSame( + array( + array( + 'id' => 'download-bool', + 'label' => 'Download', + 'href' => 'https://example.com/report.csv', + 'download' => true, + 'openInNewTab' => true, + 'icon' => 'core/download', + 'relevance' => 'high', + ), + array( + 'id' => 'download-name', + 'label' => 'Download as', + 'href' => 'https://example.com/report.csv', + 'download' => 'My-Report.csv', + ), + array( + 'id' => 'download-empty', + 'label' => 'Download nothing', + 'href' => 'https://example.com/report.csv', + ), + ), + sanitize_widget_actions( + array( + array( + 'id' => 'download-bool', + 'label' => 'Download', + 'href' => 'https://example.com/report.csv', + 'download' => true, + 'openInNewTab' => 1, + 'icon' => 'core/download', + 'relevance' => 'high', + 'extra' => 'dropped', + ), + array( + 'id' => 'download-name', + 'label' => 'Download as', + 'href' => 'https://example.com/report.csv', + 'download' => 'My Report.csv', + 'icon' => 'not a reference', + 'relevance' => 'urgent', + ), + array( + 'id' => 'download-empty', + 'label' => 'Download nothing', + 'href' => 'https://example.com/report.csv', + 'download' => '???', + ), + ) + ), + 'Optional fields are normalized and unknown keys dropped.' + ); + } + /** * A registered widget type's metadata reaches the widget-modules REST * record intact. From fa6ae9fd7ffd304ea961c5772c57a69fee21bad3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Fri, 28 Aug 2026 08:56:20 +0100 Subject: [PATCH 3/9] add icon and actions to the widget-modules record --- .../premium-analytics/src/widget-modules.php | 2 ++ .../tests/php/Widget_Metadata_Test.php | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/projects/packages/premium-analytics/src/widget-modules.php b/projects/packages/premium-analytics/src/widget-modules.php index aab1344e9cbf..a384dd5be796 100644 --- a/projects/packages/premium-analytics/src/widget-modules.php +++ b/projects/packages/premium-analytics/src/widget-modules.php @@ -77,6 +77,8 @@ function get_widget_modules_response() { 'title' => $widget_type->title, 'description' => $widget_type->description, 'help' => $widget_type->help, + 'icon' => $widget_type->icon, + 'actions' => $widget_type->actions, 'keywords' => $widget_type->keywords, ); } diff --git a/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php b/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php index edb604509a1b..013f371c1bce 100644 --- a/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php +++ b/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php @@ -531,6 +531,14 @@ public function test_widget_modules_record_carries_metadata() { 'title' => 'Sentinel', 'description' => 'Metadata carrier.', 'help' => array( 'content' => 'Helpful.' ), + 'icon' => 'core/chart-bar', + 'actions' => array( + array( + 'id' => 'export', + 'label' => 'Export', + 'href' => 'https://example.com/export.csv', + ), + ), 'keywords' => array( 'sentinel' ), ) ); @@ -561,6 +569,18 @@ public function test_widget_modules_record_carries_metadata() { $this->assertSame( 'Sentinel', $record['title'], 'The title reaches the record.' ); $this->assertSame( 'Metadata carrier.', $record['description'], 'The description reaches the record.' ); $this->assertSame( array( 'content' => 'Helpful.' ), $record['help'], 'The help note reaches the record.' ); + $this->assertSame( 'core/chart-bar', $record['icon'], 'The icon reference reaches the record.' ); + $this->assertSame( + array( + array( + 'id' => 'export', + 'label' => 'Export', + 'href' => 'https://example.com/export.csv', + ), + ), + $record['actions'], + 'The actions reach the record.' + ); $this->assertSame( array( 'sentinel' ), $record['keywords'], 'The keywords reach the record.' ); } } From b79badbcc91998e146348e345c6c84788965b080 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Fri, 28 Aug 2026 10:19:41 +0100 Subject: [PATCH 4/9] carry widget.json actions into story widget types drops the icon reference the story cannot resolve, as useWidgetTypes does --- .../stories/create-story-widget-type.ts | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/projects/packages/premium-analytics/widgets/stories/create-story-widget-type.ts b/projects/packages/premium-analytics/widgets/stories/create-story-widget-type.ts index 87ea8150d05c..46771562e944 100644 --- a/projects/packages/premium-analytics/widgets/stories/create-story-widget-type.ts +++ b/projects/packages/premium-analytics/widgets/stories/create-story-widget-type.ts @@ -13,11 +13,29 @@ export interface StoryWidgetManifest { title: string; description?: string; help?: WidgetType[ 'help' ]; + actions?: StoryWidgetManifestAction[]; category?: string; presentation?: string; keywords?: string[]; } +/** + * An action as authored in `widget.json`: the wire shape, with `icon` as a + * registered icon name the story cannot resolve and `relevance` widened to + * `string` by the JSON import. + */ +interface StoryWidgetManifestAction { + id: string; + label: string; + href: string; + icon?: string; + relevance?: string; + download?: string | boolean; + openInNewTab?: boolean; +} + +type StoryWidgetAction = NonNullable< WidgetType[ 'actions' ] >[ number ]; + /** * Runtime-only fields a widget declares in `widget.ts` (its default export): * the ones that cannot live in JSON because they hold a React element or @@ -65,6 +83,22 @@ export function createStoryWidgetType( ...( manifest.category ? { category: manifest.category } : {} ), ...( manifest.keywords ? { keywords: manifest.keywords } : {} ), ...( manifest.help ? { help: manifest.help } : {} ), + ...( manifest.actions + ? { + // Mirrors useWidgetTypes: an icon reference only reaches the + // host once resolved, and the story has no resolver. + actions: manifest.actions.map( action => ( { + id: action.id, + label: action.label, + href: action.href, + ...( action.relevance + ? { relevance: action.relevance as StoryWidgetAction[ 'relevance' ] } + : {} ), + ...( action.download !== undefined ? { download: action.download } : {} ), + ...( action.openInNewTab !== undefined ? { openInNewTab: action.openInNewTab } : {} ), + } ) ), + } + : {} ), ...( manifest.presentation ? { presentation: manifest.presentation as WidgetType[ 'presentation' ], From 6a85f39e6398a341b7f40a79cf2e120fe11293bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Thu, 3 Sep 2026 09:02:05 +0100 Subject: [PATCH 5/9] accept the medium relevance tier for widget actions --- .../premium-analytics/src/widget-types.php | 2 +- .../tests/php/Widget_Metadata_Test.php | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/projects/packages/premium-analytics/src/widget-types.php b/projects/packages/premium-analytics/src/widget-types.php index d77a72ee5aea..b9ad91542dca 100644 --- a/projects/packages/premium-analytics/src/widget-types.php +++ b/projects/packages/premium-analytics/src/widget-types.php @@ -245,7 +245,7 @@ function sanitize_widget_actions( $actions ) { } } - if ( isset( $action['relevance'] ) && in_array( $action['relevance'], array( 'high', 'low' ), true ) ) { + if ( isset( $action['relevance'] ) && in_array( $action['relevance'], array( 'high', 'medium', 'low' ), true ) ) { $entry['relevance'] = $action['relevance']; } diff --git a/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php b/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php index 013f371c1bce..249432852e71 100644 --- a/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php +++ b/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php @@ -515,6 +515,46 @@ public function test_sanitize_widget_actions_normalizes_optional_fields() { ); } + /** + * The three relevance tiers survive; any other value drops the key and + * keeps the action. + */ + public function test_sanitize_widget_actions_keeps_every_relevance_tier() { + $href = 'https://example.com/report'; + + $actions = sanitize_widget_actions( + array( + array( + 'id' => 'high', + 'label' => 'High', + 'href' => $href, + 'relevance' => 'high', + ), + array( + 'id' => 'medium', + 'label' => 'Medium', + 'href' => $href, + 'relevance' => 'medium', + ), + array( + 'id' => 'low', + 'label' => 'Low', + 'href' => $href, + 'relevance' => 'low', + ), + array( + 'id' => 'unknown', + 'label' => 'Unknown', + 'href' => $href, + 'relevance' => 'urgent', + ), + ) + ); + + $this->assertSame( array( 'high', 'medium', 'low' ), array_column( $actions, 'relevance' ), 'Every tier survives in order.' ); + $this->assertSame( array( 'high', 'medium', 'low', 'unknown' ), array_column( $actions, 'id' ), 'An unknown tier keeps the action.' ); + } + /** * A registered widget type's metadata reaches the widget-modules REST * record intact. From bb41a6c90bbe478cbd0ad593770799d2892dbd86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Thu, 3 Sep 2026 09:02:57 +0100 Subject: [PATCH 6/9] keep the download intent through the manifest gate a name that sanitizes to nothing becomes true; only false navigates --- .../premium-analytics/src/widget-types.php | 9 ++- .../tests/php/Widget_Metadata_Test.php | 56 +++++++++++++++---- 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/projects/packages/premium-analytics/src/widget-types.php b/projects/packages/premium-analytics/src/widget-types.php index b9ad91542dca..66e67e687ad0 100644 --- a/projects/packages/premium-analytics/src/widget-types.php +++ b/projects/packages/premium-analytics/src/widget-types.php @@ -173,7 +173,7 @@ function resolve_widget_action_href( $href ) { * plus optional `download` / `openInNewTab` / `icon` / `relevance`. Drops * incomplete or unsafe entries and reports dropped hrefs through * `_doing_it_wrong()`; a malformed `icon` or `relevance` drops the key, never - * the action. + * the action, and a `download` name that sanitizes to nothing becomes `true`. * * @param mixed $actions Actions from the build manifest. * @return array|null Sanitized actions, or null when none survive. @@ -227,10 +227,9 @@ function sanitize_widget_actions( $actions ) { if ( is_bool( $action['download'] ) ) { $entry['download'] = $action['download']; } else { - $filename = sanitize_file_name( (string) $action['download'] ); - if ( $filename ) { - $entry['download'] = $filename; - } + // A name that sanitizes to nothing keeps the download under the original name; only `false` navigates. + $filename = sanitize_file_name( (string) $action['download'] ); + $entry['download'] = '' !== $filename ? $filename : true; } } diff --git a/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php b/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php index 249432852e71..a33ff1488953 100644 --- a/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php +++ b/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php @@ -477,11 +477,6 @@ public function test_sanitize_widget_actions_normalizes_optional_fields() { 'href' => 'https://example.com/report.csv', 'download' => 'My-Report.csv', ), - array( - 'id' => 'download-empty', - 'label' => 'Download nothing', - 'href' => 'https://example.com/report.csv', - ), ), sanitize_widget_actions( array( @@ -503,18 +498,57 @@ public function test_sanitize_widget_actions_normalizes_optional_fields() { 'icon' => 'not a reference', 'relevance' => 'urgent', ), - array( - 'id' => 'download-empty', - 'label' => 'Download nothing', - 'href' => 'https://example.com/report.csv', - 'download' => '???', - ), ) ), 'Optional fields are normalized and unknown keys dropped.' ); } + /** + * Every download value but `false` keeps the download: a usable filename + * survives sanitization, anything else falls back to the original name. + */ + public function test_sanitize_widget_actions_keeps_download_intent() { + $href = 'https://example.com/export.csv'; + + $actions = sanitize_widget_actions( + array( + array( + 'id' => 'empty', + 'label' => 'Empty string', + 'href' => $href, + 'download' => '', + ), + array( + 'id' => 'zero', + 'label' => 'Zero', + 'href' => $href, + 'download' => '0', + ), + array( + 'id' => 'unusable', + 'label' => 'Unusable name', + 'href' => $href, + 'download' => '???', + ), + array( + 'id' => 'flag', + 'label' => 'Boolean true', + 'href' => $href, + 'download' => true, + ), + array( + 'id' => 'off', + 'label' => 'Boolean false', + 'href' => $href, + 'download' => false, + ), + ) + ); + + $this->assertSame( array( true, '0', true, true, false ), array_column( $actions, 'download' ), 'Only false means navigation; an unusable name downloads under the original one.' ); + } + /** * The three relevance tiers survive; any other value drops the key and * keeps the action. From caa7bb4e3525262784d8c08fcc445f6226becd7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Thu, 3 Sep 2026 10:17:22 +0100 Subject: [PATCH 7/9] keep the doing_it_wrong call on one line tools/replace-next-version-tag.sh only rewrites the version token in a single-line call, so the build failed on the token --- .../premium-analytics/src/widget-types.php | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/projects/packages/premium-analytics/src/widget-types.php b/projects/packages/premium-analytics/src/widget-types.php index 66e67e687ad0..00463a410e32 100644 --- a/projects/packages/premium-analytics/src/widget-types.php +++ b/projects/packages/premium-analytics/src/widget-types.php @@ -202,18 +202,14 @@ function sanitize_widget_actions( $actions ) { $href = esc_url_raw( resolve_widget_action_href( $action['href'] ) ); if ( ! $href ) { - _doing_it_wrong( - __FUNCTION__, - esc_html( - sprintf( - /* translators: 1: Widget action id. 2: Declared action href. */ - __( 'Dropped widget action "%1$s": href "%2$s" is not an allowed URL.', 'jetpack-premium-analytics-pkg' ), - $action['id'], - $action['href'] - ) - ), - 'jetpack-premium-analytics-$$next-version$$' + $message = sprintf( + /* translators: 1: Widget action id. 2: Declared action href. */ + __( 'Dropped widget action "%1$s": href "%2$s" is not an allowed URL.', 'jetpack-premium-analytics-pkg' ), + $action['id'], + $action['href'] ); + // One line: tools/replace-next-version-tag.sh only rewrites the token in a single-line call. + _doing_it_wrong( __FUNCTION__, esc_html( $message ), 'jetpack-premium-analytics-$$next-version$$' ); continue; } From 68c0dbbb4ffecc118816738d7863474d86991907 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Sat, 5 Sep 2026 09:43:01 +0100 Subject: [PATCH 8/9] share the fixture widget manifest across tests Widget_Modules_Test alone required a local build/ and fataled on the redeclared stub --- .../tests/php/Analytics_Test.php | 21 ++--------- .../tests/php/Widget_Metadata_Test.php | 10 +++--- .../tests/php/Widget_Modules_Test.php | 9 ++++- .../traits/trait-widget-manifest-fixture.php | 35 +++++++++++++++++++ 4 files changed, 50 insertions(+), 25 deletions(-) create mode 100644 projects/packages/premium-analytics/tests/php/traits/trait-widget-manifest-fixture.php diff --git a/projects/packages/premium-analytics/tests/php/Analytics_Test.php b/projects/packages/premium-analytics/tests/php/Analytics_Test.php index 82c6926ba7fd..12590f3ca1ff 100644 --- a/projects/packages/premium-analytics/tests/php/Analytics_Test.php +++ b/projects/packages/premium-analytics/tests/php/Analytics_Test.php @@ -15,6 +15,8 @@ use PHPUnit\Framework\Attributes\RunInSeparateProcess; use PHPUnit\Framework\TestCase; +require_once __DIR__ . '/traits/trait-widget-manifest-fixture.php'; + /** * Tests for the Analytics class. Also covers ensure_widget_registry_ready(): only * test_rest_request_still_serves_the_widget_manifest exercises it, and php-code-coverage @@ -26,6 +28,7 @@ #[CoversClass( Analytics::class )] #[CoversFunction( 'Automattic\\Jetpack\\PremiumAnalytics\\ensure_widget_registry_ready' )] class Analytics_Test extends TestCase { + use Widget_Manifest_Fixture_Trait; const MENU_SLUG = 'jetpack-premium-analytics-wp-admin'; const MENU_HOOKNAME = 'toplevel_page_' . self::MENU_SLUG; @@ -430,24 +433,6 @@ public function test_rest_request_still_serves_the_widget_manifest() { } } - /** - * Point the manifest require at the fixture manifest. - * - * @return string - */ - public function use_fixture_widget_manifest() { - return __DIR__ . '/fixtures/build-entry/widgets.php'; - } - - /** - * Point the manifest at a missing file. - * - * @return string - */ - public function use_absent_widget_manifest() { - return __DIR__ . '/fixtures/build-entry/no-such-widgets.php'; - } - /** * The wp-admin integrated dashboard slug in admin is recognized as a dashboard request. */ diff --git a/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php b/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php index a33ff1488953..955be268223f 100644 --- a/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php +++ b/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php @@ -14,6 +14,7 @@ require_once __DIR__ . '/../../src/widget-types.php'; require_once __DIR__ . '/../../src/widget-modules.php'; require_once __DIR__ . '/fixtures/widget-modules-manifest.php'; +require_once __DIR__ . '/traits/trait-widget-manifest-fixture.php'; /** * @covers ::Automattic\Jetpack\PremiumAnalytics\register_widget_types @@ -34,6 +35,7 @@ #[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\get_widget_metadata_i18n_schema' )] #[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\get_widget_modules_response' )] class Widget_Metadata_Test extends BaseTestCase { + use Widget_Manifest_Fixture_Trait; /** * Without a declared textdomain, strings are translated under the package @@ -617,17 +619,13 @@ public function test_widget_modules_record_carries_metadata() { ) ); - // A local build/ would otherwise be required on first use and redeclare the fixture stub. - $manifest_path = static function () { - return __DIR__ . '/fixtures/build-entry/widgets.php'; - }; - add_filter( 'jetpack_premium_analytics_widgets_manifest_path', $manifest_path ); + add_filter( 'jetpack_premium_analytics_widgets_manifest_path', array( $this, 'use_fixture_widget_manifest' ) ); $records = array(); try { $records = get_widget_modules_response()->get_data(); } finally { - remove_filter( 'jetpack_premium_analytics_widgets_manifest_path', $manifest_path ); + remove_filter( 'jetpack_premium_analytics_widgets_manifest_path', array( $this, 'use_fixture_widget_manifest' ) ); $registry->unregister( 'test/metadata-sentinel' ); } diff --git a/projects/packages/premium-analytics/tests/php/Widget_Modules_Test.php b/projects/packages/premium-analytics/tests/php/Widget_Modules_Test.php index 3c39af7a1aae..4c7516d259f9 100644 --- a/projects/packages/premium-analytics/tests/php/Widget_Modules_Test.php +++ b/projects/packages/premium-analytics/tests/php/Widget_Modules_Test.php @@ -11,11 +11,13 @@ use WP_REST_Server; require_once __DIR__ . '/../../src/widget-modules.php'; +require_once __DIR__ . '/traits/trait-widget-manifest-fixture.php'; /** * Tests for Premium Analytics widget module discovery. */ class Widget_Modules_Test extends TestCase { + use Widget_Manifest_Fixture_Trait; const ROUTE = '/wpcom/v2/widget-modules'; const LEGACY_ROUTE = '/jetpack/v4/widget-modules'; @@ -73,7 +75,12 @@ public function test_widget_modules_route_is_gated_on_the_dashboard_capability() * the process has touched the registry yet. */ public function test_response_hydrates_the_registry_on_first_use() { - $response = get_widget_modules_response(); + add_filter( 'jetpack_premium_analytics_widgets_manifest_path', array( $this, 'use_fixture_widget_manifest' ) ); + try { + $response = get_widget_modules_response(); + } finally { + remove_filter( 'jetpack_premium_analytics_widgets_manifest_path', array( $this, 'use_fixture_widget_manifest' ) ); + } $this->assertInstanceOf( \WP_REST_Response::class, $response ); $this->assertIsArray( $response->get_data() ); diff --git a/projects/packages/premium-analytics/tests/php/traits/trait-widget-manifest-fixture.php b/projects/packages/premium-analytics/tests/php/traits/trait-widget-manifest-fixture.php new file mode 100644 index 000000000000..15c5f1ed178d --- /dev/null +++ b/projects/packages/premium-analytics/tests/php/traits/trait-widget-manifest-fixture.php @@ -0,0 +1,35 @@ + Date: Sat, 5 Sep 2026 09:43:04 +0100 Subject: [PATCH 9/9] reuse WidgetActionRecord in the story helper spread the manifest action instead of re-typing its wire shape --- .../stories/create-story-widget-type.ts | 30 +++++-------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/projects/packages/premium-analytics/widgets/stories/create-story-widget-type.ts b/projects/packages/premium-analytics/widgets/stories/create-story-widget-type.ts index 46771562e944..187acf8ab3e1 100644 --- a/projects/packages/premium-analytics/widgets/stories/create-story-widget-type.ts +++ b/projects/packages/premium-analytics/widgets/stories/create-story-widget-type.ts @@ -1,7 +1,7 @@ /** * WordPress dependencies */ -import type { WidgetType } from '@wordpress/widget-primitives'; +import type { WidgetActionRecord, WidgetRelevance, WidgetType } from '@wordpress/widget-primitives'; /** * Identity and declarative metadata a widget authors in `widget.json` — the @@ -20,21 +20,12 @@ export interface StoryWidgetManifest { } /** - * An action as authored in `widget.json`: the wire shape, with `icon` as a - * registered icon name the story cannot resolve and `relevance` widened to - * `string` by the JSON import. + * An action as authored in `widget.json`: the wire shape, with `relevance` + * widened to `string` by the JSON import. */ -interface StoryWidgetManifestAction { - id: string; - label: string; - href: string; - icon?: string; +type StoryWidgetManifestAction = Omit< WidgetActionRecord, 'relevance' > & { relevance?: string; - download?: string | boolean; - openInNewTab?: boolean; -} - -type StoryWidgetAction = NonNullable< WidgetType[ 'actions' ] >[ number ]; +}; /** * Runtime-only fields a widget declares in `widget.ts` (its default export): @@ -88,14 +79,9 @@ export function createStoryWidgetType( // Mirrors useWidgetTypes: an icon reference only reaches the // host once resolved, and the story has no resolver. actions: manifest.actions.map( action => ( { - id: action.id, - label: action.label, - href: action.href, - ...( action.relevance - ? { relevance: action.relevance as StoryWidgetAction[ 'relevance' ] } - : {} ), - ...( action.download !== undefined ? { download: action.download } : {} ), - ...( action.openInNewTab !== undefined ? { openInNewTab: action.openInNewTab } : {} ), + ...action, + icon: undefined, + relevance: action.relevance as WidgetRelevance | undefined, } ) ), } : {} ),