Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
3 / 3
CRAP
n/a
0 / 0
Automattic\Jetpack\PremiumAnalytics\is_videopress_available
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
Automattic\Jetpack\PremiumAnalytics\configure_videopress_availability
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
Automattic\Jetpack\PremiumAnalytics\inject_videopress_script_data
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * VideoPress availability, shared by the widget layer and the client routes.
4 *
5 * Calypso gates its Videos module on the `videopress` plan feature, which `wpcom_site_has_feature()`
6 * answers on the WPCOM platform but which does not exist off-platform. `Current_Plan::supports()`
7 * grants `videopress` to every plan because of the free tier, and `Product::get_site_features_from_wpcom()`
8 * answers entitlement rather than whether VideoPress is running, over a synchronous WPCOM request.
9 *
10 * The canonical gates (`VideoPress\Status::is_active()`, the WPCOM arm of `Current_Plan::supports()`)
11 * live in packages this one does not depend on, so the detection below is local by necessity. Widen
12 * the dependency graph before writing another detector.
13 *
14 * @package automattic/jetpack-premium-analytics
15 */
16
17namespace Automattic\Jetpack\PremiumAnalytics;
18
19use Automattic\Jetpack\Constants;
20use Automattic\Jetpack\Modules;
21use Automattic\Jetpack\Status\Host;
22
23/**
24 * Filter controlling whether the VideoPress-backed surfaces are available.
25 *
26 * @var string
27 */
28const VIDEOPRESS_AVAILABLE_FILTER = 'jetpack_premium_analytics_videopress_available';
29
30/**
31 * Whether the site can produce VideoPress play data.
32 *
33 * Atomic reads the plan feature alongside Simple, not the module: wpcomsh provides
34 * `wpcom_site_has_feature()` there, and the VideoPress module has no `Auto Activate` header, so a
35 * plan that includes VideoPress routinely comes with the module off. An Atomic site with the module
36 * off therefore keeps the video surfaces, which is intended — hosting is service-level on WPCOM.
37 *
38 * Off-platform the question becomes "is VideoPress running here", not "is it paid for": without the
39 * module there is no new play data. Deliberately narrower than
40 * `Initial_State::has_videopress_access()`, which counts the paid storage tiers instead.
41 *
42 * @return bool Whether Premium Analytics treats VideoPress as available.
43 */
44function is_videopress_available() {
45    if ( ( new Host() )->is_wpcom_platform() ) {
46        $is_available = function_exists( 'wpcom_site_has_feature' ) && \wpcom_site_has_feature( 'videopress' );
47    } else {
48        // `false` skips the module registry the standalone Premium Analytics plugin
49        // does not ship, which would otherwise report every module inactive.
50        $is_available = ( new Modules() )->is_active( 'videopress', false )
51            || Constants::is_defined( 'JETPACK_VIDEOPRESS_ROOT_FILE' );
52    }
53
54    /**
55     * Filters whether Premium Analytics treats VideoPress as available.
56     *
57     * Hides the Videos report and the video detail page when false. The Top videos
58     * widget is the VideoPress package's call: it registers the widget.
59     *
60     * @param bool $is_available Whether VideoPress was detected in the current request.
61     */
62    return (bool) apply_filters( VIDEOPRESS_AVAILABLE_FILTER, $is_available );
63}
64
65/**
66 * Configures the VideoPress script data.
67 *
68 * @return void
69 */
70function configure_videopress_availability() {
71    add_filter( 'jetpack_admin_js_script_data', __NAMESPACE__ . '\\inject_videopress_script_data', 20 );
72}
73
74/**
75 * Injects the VideoPress availability flag into JetpackScriptData.
76 *
77 * The client can't read `site.plan.features` here: that key is only populated when the
78 * Publicize package happens to load, which the standalone Premium Analytics plugin never does.
79 *
80 * @param array $data The script data passed by the assets package.
81 * @return array
82 */
83function inject_videopress_script_data( array $data ): array {
84    if ( ! isset( $data['premium_analytics'] ) || ! is_array( $data['premium_analytics'] ) ) {
85        $data['premium_analytics'] = array();
86    }
87
88    $data['premium_analytics']['has_videopress'] = is_videopress_available();
89
90    return $data;
91}