Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.00% covered (success)
92.00%
23 / 25
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.
4 *
5 * Persisted layouts are left unchanged so missing types remain as removable
6 * ghost widgets. This is for hard availability — the host, or a feature the site
7 * either has or does not. Request-dependent or soft state (shown locked, say)
8 * belongs in the runtime types filter instead.
9 *
10 * @package automattic/jetpack-premium-analytics
11 */
12
13namespace Automattic\Jetpack\PremiumAnalytics;
14
15use Automattic\Jetpack\Status\Host;
16
17// Guarded on a symbol the file declares, so a second copy of the package can't
18// redeclare it. See the include block in Analytics::load_dashboard_components().
19if ( ! function_exists( __NAMESPACE__ . '\\is_videopress_available' ) ) {
20    require_once __DIR__ . '/videopress-availability.php';
21}
22
23/**
24 * Widget types that only have data on a site running VideoPress.
25 */
26const VIDEOPRESS_WIDGET_TYPES = array(
27    'jpa/videopress',
28    'jpa/video-detail-highlights',
29    'jpa/video-detail-views-performance',
30    'jpa/video-detail-embeds',
31);
32
33/**
34 * Returns the current widget support context.
35 *
36 * @return array{is_wpcom_simple:bool,has_videopress:bool} Widget support context.
37 */
38function get_widget_support_context() {
39    return array(
40        'is_wpcom_simple' => ( new Host() )->is_wpcom_simple(),
41        'has_videopress'  => is_videopress_available(),
42    );
43}
44
45/**
46 * Returns unsupported widget types for a context.
47 *
48 * @param array{is_wpcom_simple?:bool,has_videopress?:bool} $context Widget support context; a missing key reads as false.
49 * @return string[] Unsupported widget type names.
50 */
51function get_unsupported_widget_types( $context ) {
52    $unsupported = array();
53
54    // File download tracking is served only on WPCOM Simple. Calypso applies
55    // the same boundary, which excludes self-hosted Jetpack and Atomic sites.
56    if ( empty( $context['is_wpcom_simple'] ) ) {
57        $unsupported[] = 'jpa/file-downloads';
58
59        // Share counts are recorded when the share request is processed, which
60        // only happens on WPCOM Simple. Elsewhere the button click is handled by
61        // the site itself and never reaches the counter, so the site summary's
62        // `shares_<service>` fields stay at zero however much the content is
63        // shared. Calypso excludes the module on the same grounds.
64        $unsupported[] = 'jpa/shares';
65    }
66
67    // Play counts only exist for VideoPress-hosted videos, so without VideoPress
68    // these widgets have nothing to report. Calypso gates its Videos module the
69    // same way (see videopress-availability.php).
70    if ( empty( $context['has_videopress'] ) ) {
71        $unsupported = array_merge( $unsupported, VIDEOPRESS_WIDGET_TYPES );
72    }
73
74    return $unsupported;
75}
76
77/**
78 * Removes unsupported widget records.
79 *
80 * @param array                                             $items    Widget records.
81 * @param string                                            $type_key Record key containing the widget type.
82 * @param array{is_wpcom_simple?:bool,has_videopress?:bool} $context  Widget support context; a missing key reads as false.
83 * @return array Filtered and re-indexed widget records.
84 */
85function remove_unsupported_widget_items( $items, $type_key, $context ) {
86    $unsupported = get_unsupported_widget_types( $context );
87
88    if ( empty( $unsupported ) ) {
89        return $items;
90    }
91
92    return array_values(
93        array_filter(
94            $items,
95            static function ( $item ) use ( $type_key, $unsupported ) {
96                return ! is_array( $item )
97                    || ! in_array( $item[ $type_key ] ?? '', $unsupported, true );
98            }
99        )
100    );
101}