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