Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.83% covered (success)
95.83%
23 / 24
100.00% covered (success)
100.00%
3 / 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
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
3
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 * Returns the current widget support context.
28 *
29 * @return array{is_wpcom_simple:bool,has_videopress:bool} Widget support context.
30 */
31function get_widget_support_context() {
32    return array(
33        'is_wpcom_simple' => ( new Host() )->is_wpcom_simple(),
34        'has_videopress'  => is_videopress_available(),
35    );
36}
37
38/**
39 * Returns unsupported widget types for a context.
40 *
41 * @param array{is_wpcom_simple?:bool,has_videopress?:bool} $context Widget support context; a missing key reads as false.
42 * @return string[] Unsupported widget type names.
43 */
44function get_unsupported_widget_types( $context ) {
45    $unsupported = array();
46
47    // File download tracking is served only on WPCOM Simple. Calypso applies
48    // the same boundary, which excludes self-hosted Jetpack and Atomic sites.
49    if ( empty( $context['is_wpcom_simple'] ) ) {
50        $unsupported[] = 'jpa/file-downloads';
51
52        // Share counts are recorded only when WPCOM processes the share click; elsewhere the
53        // click never reaches the counter, so `shares_<service>` stays zero. Calypso excludes it too.
54        $unsupported[] = 'jpa/shares';
55    }
56
57    // Play counts only exist for VideoPress-hosted videos; Calypso gates its Videos module
58    // the same way (see videopress-availability.php).
59    if ( empty( $context['has_videopress'] ) ) {
60        $unsupported = array_merge( $unsupported, VIDEOPRESS_WIDGET_TYPES );
61    }
62
63    return $unsupported;
64}
65
66/**
67 * Removes unsupported widget records.
68 *
69 * @param array                                             $items    Widget records.
70 * @param string                                            $type_key Record key containing the widget type.
71 * @param array{is_wpcom_simple?:bool,has_videopress?:bool} $context  Widget support context; a missing key reads as false.
72 * @return array Filtered and re-indexed widget records.
73 */
74function remove_unsupported_widget_items( $items, $type_key, $context ) {
75    $unsupported = get_unsupported_widget_types( $context );
76
77    if ( empty( $unsupported ) ) {
78        return $items;
79    }
80
81    return array_values(
82        array_filter(
83            $items,
84            static function ( $item ) use ( $type_key, $unsupported ) {
85                return ! is_array( $item )
86                    || ! in_array( $item[ $type_key ] ?? '', $unsupported, true );
87            }
88        )
89    );
90}