Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.31% |
12 / 13 |
|
100.00% |
2 / 2 |
CRAP | n/a |
0 / 0 |
|
| Automattic\Jetpack\PremiumAnalytics\remove_dev_only_widget_types | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
| Automattic\Jetpack\PremiumAnalytics\filter_registrable_widget_types_by_environment | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Widget availability policy (consumer layer). |
| 4 | * |
| 5 | * Premium Analytics' policy over the neutral filters in widget-types.php: |
| 6 | * availability is the consumer's job, core only offers the hooks. |
| 7 | * |
| 8 | * Ships one policy: the developer-only React Query Devtools widget is never |
| 9 | * registered in production. Hooking the registry-time filter (a hard hide) |
| 10 | * keeps every registry consumer correct without a filtered accessor. |
| 11 | * |
| 12 | * @package automattic/jetpack-premium-analytics |
| 13 | */ |
| 14 | |
| 15 | namespace Automattic\Jetpack\PremiumAnalytics; |
| 16 | |
| 17 | /** |
| 18 | * Removes developer-only candidates in production. |
| 19 | * |
| 20 | * Split from the hook callback so both branches are testable without touching |
| 21 | * the global environment. |
| 22 | * |
| 23 | * @param array $widget_candidates Manifest candidates, each with a `name`. |
| 24 | * @param string $environment Site environment type. |
| 25 | * @return array The candidates, minus developer-only types in production. |
| 26 | */ |
| 27 | function remove_dev_only_widget_types( $widget_candidates, $environment ) { |
| 28 | if ( 'production' !== $environment ) { |
| 29 | return $widget_candidates; |
| 30 | } |
| 31 | |
| 32 | // Types that must never reach a production dashboard. Matched by name, not |
| 33 | // by `category: developer`: wp-build does not copy `category` into the PHP |
| 34 | // manifest yet, so it is not queryable here. Switch to a category check |
| 35 | // once the manifest carries it. |
| 36 | $dev_only = array( 'jpa/react-query-dev-tool' ); |
| 37 | |
| 38 | return array_values( |
| 39 | array_filter( |
| 40 | $widget_candidates, |
| 41 | static function ( $widget ) use ( $dev_only ) { |
| 42 | return ! in_array( $widget['name'] ?? '', $dev_only, true ); |
| 43 | } |
| 44 | ) |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Registry-time callback: hides developer-only types in production. |
| 50 | * |
| 51 | * Defaults to `production`; a site opts in via `WP_ENVIRONMENT_TYPE` |
| 52 | * (`local`, `development`, `staging`). |
| 53 | * |
| 54 | * @param array $widget_candidates Manifest candidates. |
| 55 | * @return array The candidates, minus developer-only types in production. |
| 56 | */ |
| 57 | function filter_registrable_widget_types_by_environment( $widget_candidates ) { |
| 58 | return remove_dev_only_widget_types( $widget_candidates, wp_get_environment_type() ); |
| 59 | } |
| 60 | |
| 61 | add_filter( REGISTRABLE_WIDGET_TYPES_FILTER, __NAMESPACE__ . '\\filter_registrable_widget_types_by_environment' ); |