diff --git a/projects/packages/premium-analytics/changelog/update-wooa7s-1775-hide-woo-bookings-widgets b/projects/packages/premium-analytics/changelog/update-wooa7s-1775-hide-woo-bookings-widgets new file mode 100644 index 000000000000..cbbf03d30da9 --- /dev/null +++ b/projects/packages/premium-analytics/changelog/update-wooa7s-1775-hide-woo-bookings-widgets @@ -0,0 +1,4 @@ +Significance: minor +Type: changed + +Hide WooCommerce widget categories (store, orders, coupons, bookings) when WooCommerce is inactive, and bookings widgets when the WooCommerce Bookings extension is inactive. diff --git a/projects/packages/premium-analytics/src/widget-availability.php b/projects/packages/premium-analytics/src/widget-availability.php index faa7dd68fda3..f29aa94225b7 100644 --- a/projects/packages/premium-analytics/src/widget-availability.php +++ b/projects/packages/premium-analytics/src/widget-availability.php @@ -5,22 +5,37 @@ * Premium Analytics' policy over the neutral filters in widget-types.php: * availability is the consumer's job, core only offers the hooks. * - * Ships one policy: the developer-only React Query Devtools widget is never - * registered in production. Hooking the registry-time filter (a hard hide) - * keeps every registry consumer correct without a filtered accessor. + * Ships two policies, both hooked on the registry-time filter (a hard hide, + * which keeps every registry consumer correct without a filtered accessor): + * developer-only widget types are never registered in production, and the + * commerce widget categories are only registered while the plugin backing + * them (WooCommerce, plus WooCommerce Bookings for `bookings`) is active. * * @package automattic/jetpack-premium-analytics */ namespace Automattic\Jetpack\PremiumAnalytics; +/** + * Widget categories that are only meaningful with WooCommerce active. + */ +const WOOCOMMERCE_WIDGET_CATEGORIES = array( 'store', 'orders', 'coupons' ); + +/** + * Widget categories that are only meaningful with WooCommerce Bookings active. + * + * Checked independently of WOOCOMMERCE_WIDGET_CATEGORIES: the Bookings + * extension cannot run without WooCommerce, so its presence implies both. + */ +const WOOCOMMERCE_BOOKINGS_WIDGET_CATEGORIES = array( 'bookings' ); + /** * Removes developer-only candidates in production. * * Split from the hook callback so both branches are testable without touching * the global environment. * - * @param array $widget_candidates Manifest candidates, each with a `name`. + * @param array $widget_candidates Manifest candidates, each with a `name` and `category`. * @param string $environment Site environment type. * @return array The candidates, minus developer-only types in production. */ @@ -29,17 +44,11 @@ function remove_dev_only_widget_types( $widget_candidates, $environment ) { return $widget_candidates; } - // Types that must never reach a production dashboard. Matched by name, not - // by `category: developer`: wp-build does not copy `category` into the PHP - // manifest yet, so it is not queryable here. Switch to a category check - // once the manifest carries it. - $dev_only = array( 'jpa/react-query-dev-tool' ); - return array_values( array_filter( $widget_candidates, - static function ( $widget ) use ( $dev_only ) { - return ! in_array( $widget['name'] ?? '', $dev_only, true ); + static function ( $widget ) { + return 'developer' !== ( $widget['category'] ?? '' ); } ) ); @@ -59,3 +68,71 @@ function filter_registrable_widget_types_by_environment( $widget_candidates ) { } add_filter( REGISTRABLE_WIDGET_TYPES_FILTER, __NAMESPACE__ . '\\filter_registrable_widget_types_by_environment' ); + +/** + * Removes candidates whose commerce category lacks its backing plugin. + * + * Split from the hook callback so the branches are testable without touching + * global plugin state. + * + * @param array $widget_candidates Manifest candidates, each with a `category`. + * @param bool $woocommerce_available Whether WooCommerce is available. + * @param bool $bookings_available Whether WooCommerce Bookings is available. + * @return array The candidates, minus commerce categories missing their plugin. + */ +function remove_plugin_gated_widget_types( $widget_candidates, $woocommerce_available, $bookings_available ) { + return array_values( + array_filter( + $widget_candidates, + static function ( $widget ) use ( $woocommerce_available, $bookings_available ) { + $category = $widget['category'] ?? ''; + + if ( ! $woocommerce_available && in_array( $category, WOOCOMMERCE_WIDGET_CATEGORIES, true ) ) { + return false; + } + + if ( ! $bookings_available && in_array( $category, WOOCOMMERCE_BOOKINGS_WIDGET_CATEGORIES, true ) ) { + return false; + } + + return true; + } + ) + ); +} + +/** + * Whether the WooCommerce Bookings extension is active. + * + * Mirrors the detection in woocommerce-analytics' Bookings sync module; + * `is_plugin_active()` only exists in admin contexts, hence the guard. + * + * @return bool Whether WooCommerce Bookings was detected in the current request. + */ +function is_bookings_plugin_active() { + return class_exists( 'WC_Bookings' ) + || ( function_exists( 'is_plugin_active' ) && \is_plugin_active( 'woocommerce-bookings/woocommerce-bookings.php' ) ); +} + +/** + * Registry-time callback: hides commerce widget categories without their plugin. + * + * WooCommerce availability is read through the store section's signal + * (dashboard-sections.php, including its availability filter) so section and + * widgets can't disagree — a consumer overriding the section filter gets + * matching widget types. Both dashboard entry points load + * dashboard-sections.php before the widget registry can hydrate, so the + * function is always defined by the time this callback runs. + * + * @param array $widget_candidates Manifest candidates. + * @return array The candidates, minus commerce categories missing their plugin. + */ +function filter_registrable_widget_types_by_plugin( $widget_candidates ) { + return remove_plugin_gated_widget_types( + $widget_candidates, + is_woocommerce_dashboard_section_available(), + is_bookings_plugin_active() + ); +} + +add_filter( REGISTRABLE_WIDGET_TYPES_FILTER, __NAMESPACE__ . '\\filter_registrable_widget_types_by_plugin' ); diff --git a/projects/packages/premium-analytics/tests/php/Widget_Availability_Test.php b/projects/packages/premium-analytics/tests/php/Widget_Availability_Test.php index 199920f44556..326f02724871 100644 --- a/projects/packages/premium-analytics/tests/php/Widget_Availability_Test.php +++ b/projects/packages/premium-analytics/tests/php/Widget_Availability_Test.php @@ -11,18 +11,32 @@ use WorDBless\BaseTestCase; require_once __DIR__ . '/../../src/widget-types.php'; +require_once __DIR__ . '/../../src/dashboard-sections.php'; require_once __DIR__ . '/../../src/widget-availability.php'; /** * @covers ::Automattic\Jetpack\PremiumAnalytics\get_available_widget_types * @covers ::Automattic\Jetpack\PremiumAnalytics\filter_registrable_widget_types_by_environment * @covers ::Automattic\Jetpack\PremiumAnalytics\remove_dev_only_widget_types + * @covers ::Automattic\Jetpack\PremiumAnalytics\filter_registrable_widget_types_by_plugin + * @covers ::Automattic\Jetpack\PremiumAnalytics\remove_plugin_gated_widget_types */ #[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\get_available_widget_types' )] #[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\filter_registrable_widget_types_by_environment' )] #[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\remove_dev_only_widget_types' )] +#[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\filter_registrable_widget_types_by_plugin' )] +#[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\remove_plugin_gated_widget_types' )] class Widget_Availability_Test extends BaseTestCase { + /** + * Reset availability filters between tests. + */ + public function tear_down() { + remove_all_filters( WOOCOMMERCE_DASHBOARD_SECTION_AVAILABLE_FILTER ); + + parent::tear_down(); + } + /** * Candidate set shaped like the build manifest entries. * @@ -30,8 +44,44 @@ class Widget_Availability_Test extends BaseTestCase { */ private function widget_candidates() { return array( - array( 'name' => 'jpa/react-query-dev-tool' ), - array( 'name' => 'jpa/hello-world' ), + array( + 'name' => 'jpa/react-query-dev-tool', + 'category' => 'developer', + ), + array( + 'name' => 'jpa/hello-world', + 'category' => 'demo', + ), + ); + } + + /** + * Candidate set spanning the commerce categories and an ungated one. + * + * @return array[] List of widget candidates. + */ + private function commerce_widget_candidates() { + return array( + array( + 'name' => 'jpa/traffic-chart', + 'category' => 'traffic', + ), + array( + 'name' => 'jpa/store-performance', + 'category' => 'store', + ), + array( + 'name' => 'jpa/orders-over-time', + 'category' => 'orders', + ), + array( + 'name' => 'jpa/sales-by-coupon-usage', + 'category' => 'coupons', + ), + array( + 'name' => 'jpa/bookings-over-time', + 'category' => 'bookings', + ), ); } @@ -70,6 +120,69 @@ public function test_registry_filter_callback_drops_dev_widget_by_default() { $this->assertContains( 'jpa/hello-world', $names, 'Regular widgets remain available.' ); } + /** + * Without WooCommerce (and thus without its Bookings extension), every + * commerce category is dropped; the rest pass through. + */ + public function test_commerce_widgets_removed_without_woocommerce() { + $names = array_column( remove_plugin_gated_widget_types( $this->commerce_widget_candidates(), false, false ), 'name' ); + + $this->assertSame( array( 'jpa/traffic-chart' ), $names, 'Without WooCommerce only ungated categories remain.' ); + } + + /** + * With WooCommerce but no Bookings extension, only `bookings` is dropped. + */ + public function test_bookings_widgets_removed_without_bookings_plugin() { + $names = array_column( remove_plugin_gated_widget_types( $this->commerce_widget_candidates(), true, false ), 'name' ); + + $this->assertNotContains( 'jpa/bookings-over-time', $names, 'Bookings widgets must be hidden without the Bookings extension.' ); + $this->assertContains( 'jpa/store-performance', $names, 'Store widgets only need WooCommerce.' ); + $this->assertContains( 'jpa/orders-over-time', $names, 'Orders widgets only need WooCommerce.' ); + $this->assertContains( 'jpa/sales-by-coupon-usage', $names, 'Coupons widgets only need WooCommerce.' ); + } + + /** + * With both plugins available, everything passes through. + */ + public function test_commerce_widgets_kept_with_both_plugins() { + $this->assertSame( + $this->commerce_widget_candidates(), + remove_plugin_gated_widget_types( $this->commerce_widget_candidates(), true, true ), + 'With WooCommerce and Bookings available no candidate is dropped.' + ); + } + + /** + * Candidates without a category are never plugin-gated. + */ + public function test_uncategorized_widgets_pass_through() { + $candidates = array( array( 'name' => 'jpa/no-category' ) ); + + $this->assertSame( + $candidates, + remove_plugin_gated_widget_types( $candidates, false, false ), + 'A candidate without a category must not be plugin-gated.' + ); + } + + /** + * The registry-time callback follows the store section's availability + * signal, so forcing the section visible also surfaces its widgets. + */ + public function test_registry_filter_callback_follows_section_availability() { + $this->assertFalse( is_woocommerce_dashboard_section_available(), 'The test environment must not have WooCommerce loaded.' ); + + $names = array_column( filter_registrable_widget_types_by_plugin( $this->commerce_widget_candidates() ), 'name' ); + $this->assertSame( array( 'jpa/traffic-chart' ), $names, 'Without WooCommerce the callback drops every commerce category.' ); + + add_filter( WOOCOMMERCE_DASHBOARD_SECTION_AVAILABLE_FILTER, '__return_true' ); + + $names = array_column( filter_registrable_widget_types_by_plugin( $this->commerce_widget_candidates() ), 'name' ); + $this->assertContains( 'jpa/store-performance', $names, 'Forcing the section available must surface the store widgets.' ); + $this->assertNotContains( 'jpa/bookings-over-time', $names, 'Bookings widgets still require the Bookings extension.' ); + } + /** * Reading the available set runs the registry through WIDGET_TYPES_FILTER. */ diff --git a/projects/packages/premium-analytics/widgets/top-performing-bookings/widget.json b/projects/packages/premium-analytics/widgets/top-performing-bookings/widget.json index 4d05356cbf0a..16277327560a 100644 --- a/projects/packages/premium-analytics/widgets/top-performing-bookings/widget.json +++ b/projects/packages/premium-analytics/widgets/top-performing-bookings/widget.json @@ -5,6 +5,6 @@ "help": { "content": "Your best-selling bookings by revenue over the selected time period." }, - "category": "store", + "category": "bookings", "presentation": "framed" }