Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
88.00% |
22 / 25 |
|
66.67% |
2 / 3 |
CRAP | n/a |
0 / 0 |
|
| Automattic\Jetpack\PremiumAnalytics\get_widget_support_context | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| Automattic\Jetpack\PremiumAnalytics\get_unsupported_widget_types | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| Automattic\Jetpack\PremiumAnalytics\remove_unsupported_widget_items | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
3.01 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Widget type support shared by the registry and default layouts — hard availability only |
| 4 | * (a feature the site has or doesn't, or a type held back from release); soft/request-dependent |
| 5 | * state belongs in the runtime types filter. Persisted layouts keep missing types as removable |
| 6 | * ghost widgets. |
| 7 | * |
| 8 | * @package automattic/jetpack-premium-analytics |
| 9 | */ |
| 10 | |
| 11 | namespace Automattic\Jetpack\PremiumAnalytics; |
| 12 | |
| 13 | use Automattic\Jetpack\Status\Host; |
| 14 | |
| 15 | // Guarded on a symbol the file declares, so a second copy of the package can't |
| 16 | // redeclare it. See the include block in Analytics::load_dashboard_components(). |
| 17 | if ( ! function_exists( __NAMESPACE__ . '\\is_videopress_available' ) ) { |
| 18 | require_once __DIR__ . '/videopress-availability.php'; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Widget types that only have data on a site running VideoPress. The Top videos widget is not |
| 23 | * listed: the VideoPress package registers it, and decides where. |
| 24 | */ |
| 25 | const VIDEOPRESS_WIDGET_TYPES = array( |
| 26 | 'jpa/video-detail-views-performance', |
| 27 | 'jpa/video-detail-embeds', |
| 28 | ); |
| 29 | |
| 30 | /** |
| 31 | * Period widgets whose chart reads the section's date range. |
| 32 | */ |
| 33 | const PERIOD_WIDGET_TYPES = array( |
| 34 | 'jpa/total-views', |
| 35 | 'jpa/total-visitors', |
| 36 | 'jpa/popular-days', |
| 37 | 'jpa/popular-hours', |
| 38 | ); |
| 39 | |
| 40 | /** |
| 41 | * Returns the current widget support context. |
| 42 | * |
| 43 | * @return array{is_wpcom_simple:bool,has_videopress:bool} Widget support context. |
| 44 | */ |
| 45 | function get_widget_support_context() { |
| 46 | return array( |
| 47 | 'is_wpcom_simple' => ( new Host() )->is_wpcom_simple(), |
| 48 | 'has_videopress' => is_videopress_available(), |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns unsupported widget types for a context. |
| 54 | * |
| 55 | * @param array{is_wpcom_simple?:bool,has_videopress?:bool} $context Widget support context; a missing key reads as false. |
| 56 | * @return string[] Unsupported widget type names. |
| 57 | */ |
| 58 | function get_unsupported_widget_types( $context ) { |
| 59 | // Temporary: the period widgets are held back on every site until product decides |
| 60 | // whether they return or go (WOOA7S-2020). Their code stays. |
| 61 | $unsupported = PERIOD_WIDGET_TYPES; |
| 62 | |
| 63 | // File download tracking is served only on WPCOM Simple. Calypso applies |
| 64 | // the same boundary, which excludes self-hosted Jetpack and Atomic sites. |
| 65 | if ( empty( $context['is_wpcom_simple'] ) ) { |
| 66 | $unsupported[] = 'jpa/file-downloads'; |
| 67 | |
| 68 | // Share counts are recorded only when WPCOM processes the share click; elsewhere the |
| 69 | // click never reaches the counter, so `shares_<service>` stays zero. Calypso excludes it too. |
| 70 | $unsupported[] = 'jpa/shares'; |
| 71 | } |
| 72 | |
| 73 | // Play counts only exist for VideoPress-hosted videos; Calypso gates its Videos module |
| 74 | // the same way (see videopress-availability.php). |
| 75 | if ( empty( $context['has_videopress'] ) ) { |
| 76 | $unsupported = array_merge( $unsupported, VIDEOPRESS_WIDGET_TYPES ); |
| 77 | } |
| 78 | |
| 79 | return $unsupported; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Removes unsupported widget records. |
| 84 | * |
| 85 | * @param array $items Widget records. |
| 86 | * @param string $type_key Record key containing the widget type. |
| 87 | * @param array{is_wpcom_simple?:bool,has_videopress?:bool} $context Widget support context; a missing key reads as false. |
| 88 | * @return array Filtered and re-indexed widget records. |
| 89 | */ |
| 90 | function remove_unsupported_widget_items( $items, $type_key, $context ) { |
| 91 | $unsupported = get_unsupported_widget_types( $context ); |
| 92 | |
| 93 | if ( empty( $unsupported ) ) { |
| 94 | return $items; |
| 95 | } |
| 96 | |
| 97 | return array_values( |
| 98 | array_filter( |
| 99 | $items, |
| 100 | static function ( $item ) use ( $type_key, $unsupported ) { |
| 101 | return ! is_array( $item ) |
| 102 | || ! in_array( $item[ $type_key ] ?? '', $unsupported, true ); |
| 103 | } |
| 104 | ) |
| 105 | ); |
| 106 | } |