Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Hide the File downloads widget and report outside WordPress.com Simple sites, matching the Calypso Stats gating.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* External dependencies
*/
import { isSimpleSite } from '@automattic/jetpack-script-data';
/**
* Internal dependencies
*/
import { getReportDefinition } from './registry';

jest.mock( '@automattic/jetpack-script-data', () => ( {
isSimpleSite: jest.fn( () => true ),
} ) );

const mockIsSimpleSite = isSimpleSite as jest.Mock;

describe( 'getReportDefinition', () => {
beforeEach( () => {
mockIsSimpleSite.mockReturnValue( true );
} );

it( 'returns undefined for an unknown report', () => {
expect( getReportDefinition( 'unknown' ) ).toBeUndefined();
expect( getReportDefinition( undefined ) ).toBeUndefined();
} );

it( 'returns the downloads report on Simple sites', () => {
expect( getReportDefinition( 'downloads' )?.id ).toBe( 'downloads' );
} );

it( 'hides the downloads report on non-Simple sites', () => {
// Calypso only shows file downloads on Simple sites ("not yet supported
// in Jetpack environment", which includes Atomic); the route guard
// redirects an unavailable report to the dashboard, same as an unknown
// one.
mockIsSimpleSite.mockReturnValue( false );

expect( getReportDefinition( 'downloads' ) ).toBeUndefined();
} );

it( 'keeps other reports available on non-Simple sites', () => {
mockIsSimpleSite.mockReturnValue( false );

expect( getReportDefinition( 'posts' )?.id ).toBe( 'posts' );
} );
} );
21 changes: 20 additions & 1 deletion projects/packages/premium-analytics/routes/reports/registry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* External dependencies
*/
import { isSimpleSite } from '@automattic/jetpack-script-data';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
Expand Down Expand Up @@ -55,6 +56,13 @@ export type ReportDefinition = {
*/
resolveSection?: ( value: string | undefined ) => string;

/**
* Whether the report is available on this site, resolved at lookup time.
* An unavailable report is treated like an unknown one — the route guard
* redirects it to the dashboard. Omit for reports available everywhere.
*/
isAvailable?: () => boolean;

/**
* Dynamic import of the report's page component (default export). Kept as a
* thunk so React/UI is only pulled in when the report actually renders, and
Expand Down Expand Up @@ -107,6 +115,11 @@ export const REPORTS: Record< string, ReportDefinition > = {
downloads: {
id: 'downloads',
getTitle: () => __( 'File downloads', 'jetpack-premium-analytics' ),
// File download tracking happens on WPCOM infrastructure; Calypso only
// shows the module on Simple sites ("not yet supported in Jetpack
// environment") and we mirror that boundary. Mirrors the widget-level
// gate in `src/widget-type-support.php`.
isAvailable: isSimpleSite,
load: () => import( './downloads/page' ),
},
emails: {
Expand Down Expand Up @@ -161,5 +174,11 @@ export const REPORTS: Record< string, ReportDefinition > = {
* @return The matching definition, or `undefined` when the id is missing or unknown.
*/
export function getReportDefinition( id: string | undefined ): ReportDefinition | undefined {
return id ? REPORTS[ id ] : undefined;
const definition = id ? REPORTS[ id ] : undefined;

if ( definition?.isAvailable && ! definition.isAvailable() ) {
return undefined;
}

return definition;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

require_once __DIR__ . '/dashboard-grammar.php';
require_once __DIR__ . '/rest-namespace.php';
// Availability policy for default layout instances: defaults are read outside
// the widget registry bootstrap, so the policy must be loaded here explicitly.
require_once __DIR__ . '/widget-type-support.php';

/**
* Identifier of the Premium Analytics dashboard, formatted as `<plugin>_<page>`
Expand Down Expand Up @@ -62,7 +65,11 @@ function get_dashboard_default_layout_for( $dashboard_name ) {
*/
$default = apply_filters( DASHBOARD_DEFAULT_LAYOUT_FILTER, array(), $dashboard_name );

return is_array( $default ) ? array_values( $default ) : array();
return remove_unsupported_widget_items(
is_array( $default ) ? array_values( $default ) : array(),
'type',
get_widget_support_context()
);
}

/**
Expand Down
28 changes: 23 additions & 5 deletions projects/packages/premium-analytics/src/widget-availability.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
* Premium Analytics' policy over the neutral filters in widget-types.php:
* availability is the consumer's job, core only offers the hooks.
*
* 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.
* Policies are 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, Simple-only types are only
* registered on WPCOM Simple, and commerce categories require their plugins.
*
* @package automattic/jetpack-premium-analytics
*/

namespace Automattic\Jetpack\PremiumAnalytics;

require_once __DIR__ . '/widget-types.php';
require_once __DIR__ . '/widget-type-support.php';

/**
* Widget categories that are only meaningful with WooCommerce active.
*/
Expand Down Expand Up @@ -69,6 +71,22 @@ function filter_registrable_widget_types_by_environment( $widget_candidates ) {

add_filter( REGISTRABLE_WIDGET_TYPES_FILTER, __NAMESPACE__ . '\\filter_registrable_widget_types_by_environment' );

/**
* Applies shared type-level availability at registry time.
*
* @param array $widget_candidates Manifest candidates.
* @return array Filtered candidates.
*/
function filter_registrable_widget_types_by_availability( $widget_candidates ) {
return remove_unsupported_widget_items(
$widget_candidates,
'name',
get_widget_support_context()
);
}

add_filter( REGISTRABLE_WIDGET_TYPES_FILTER, __NAMESPACE__ . '\\filter_registrable_widget_types_by_availability' );

/**
* Removes candidates whose commerce category lacks its backing plugin.
*
Expand Down
68 changes: 68 additions & 0 deletions projects/packages/premium-analytics/src/widget-type-support.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Widget type support shared by the registry and default layouts.
*
* Persisted layouts are left unchanged so missing types remain as removable
* ghost widgets. Temporary restrictions belong in the runtime types filter.
*
* @package automattic/jetpack-premium-analytics
*/

namespace Automattic\Jetpack\PremiumAnalytics;

use Automattic\Jetpack\Status\Host;

/**
* Returns the current widget support context.
*
* @return array{is_wpcom_simple:bool} Widget support context.
*/
function get_widget_support_context() {
return array(
'is_wpcom_simple' => ( new Host() )->is_wpcom_simple(),
);
}

/**
* Returns unsupported widget types for a context.
*
* @param array{is_wpcom_simple:bool} $context Widget support context.
* @return string[] Unsupported widget type names.
*/
function get_unsupported_widget_types( $context ) {
$unsupported = array();

// File download tracking is served only on WPCOM Simple. Calypso applies
// the same boundary, which excludes self-hosted Jetpack and Atomic sites.
if ( ! $context['is_wpcom_simple'] ) {
$unsupported[] = 'jpa/file-downloads';
}

return $unsupported;
}

/**
* Removes unsupported widget records.
*
* @param array $items Widget records.
* @param string $type_key Record key containing the widget type.
* @param array{is_wpcom_simple:bool} $context Widget support context.
* @return array Filtered and re-indexed widget records.
*/
function remove_unsupported_widget_items( $items, $type_key, $context ) {
$unsupported = get_unsupported_widget_types( $context );

if ( empty( $unsupported ) ) {
return $items;
}

return array_values(
array_filter(
$items,
static function ( $item ) use ( $type_key, $unsupported ) {
return ! is_array( $item )
|| ! in_array( $item[ $type_key ] ?? '', $unsupported, true );
}
)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Automattic\Jetpack\PremiumAnalytics;

use Automattic\Jetpack\Constants;
use PHPUnit\Framework\TestCase;
use WP_REST_Server;

Expand All @@ -21,11 +22,12 @@ class Dashboard_Layout_Test extends TestCase {
const LEGACY_ROUTE = '/jetpack/v4/dashboards/(?P<name>[a-z][a-z0-9-]*(?:_[a-z0-9-]+)*)/default-layout';

/**
* Reset REST globals after route registration tests.
* Reset REST globals and constants between tests.
*/
protected function tearDown(): void {
global $wp_rest_server;
$wp_rest_server = null;
Constants::clear_constants();
parent::tearDown();
}

Expand Down Expand Up @@ -75,6 +77,28 @@ public function test_dashboard_name_resolves_traffic_default() {
$this->assertNotContains( 'jpa/hello-world', $layout_types );
}

/**
* Default layouts pass through the availability policy: on self-hosted
* Jetpack sites (this test env), Simple-only widget instances are dropped.
*/
public function test_traffic_default_excludes_simple_only_widgets_on_self_hosted() {
$layout_types = array_column( get_dashboard_default_layout_for( DASHBOARD_TRAFFIC_SECTION_ID ), 'type' );

$this->assertNotContains( 'jpa/file-downloads', $layout_types, 'Simple-only widget instances must not be part of the default layout on self-hosted sites.' );
$this->assertContains( 'jpa/clicks', $layout_types, 'Regular widget instances remain in the default layout.' );
}

/**
* WPCOM Simple keeps Simple-only widgets in the default layout.
*/
public function test_traffic_default_keeps_simple_only_widgets_on_wpcom_simple() {
Constants::set_constant( 'IS_WPCOM', true );

$layout_types = array_column( get_dashboard_default_layout_for( DASHBOARD_TRAFFIC_SECTION_ID ), 'type' );

$this->assertContains( 'jpa/file-downloads', $layout_types );
}

/**
* Traffic section aliases resolve to the same default layout.
*/
Expand Down
Loading