Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.50% covered (warning)
72.50%
29 / 40
33.33% covered (danger)
33.33%
2 / 6
CRAP
n/a
0 / 0
wpcom_has_features_edge_sticker
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
5.93
wpcom_is_rtc_http_polling_rollout
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
4.25
wpcom_is_rtc_websocket_rollout
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
wpcom_rtc_is_desktop_app
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
wpcom_enable_rtc
81.25% covered (warning)
81.25%
13 / 16
0.00% covered (danger)
0.00%
0 / 1
12.95
wpcom_rtc_providers
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
1<?php
2/**
3 * Real-time Collaboration (RTC) integration.
4 *
5 * Enables the RTC package based on the site's features.
6 *
7 * @package automattic/jetpack-mu-wpcom
8 */
9
10/**
11 * Check if the current site has the `wpcom-features-edge` sticker.
12 *
13 * @return bool|mixed
14 */
15function wpcom_has_features_edge_sticker() {
16    $sticker = 'wpcom-features-edge';
17    if ( defined( 'IS_ATOMIC' ) && IS_ATOMIC && function_exists( 'wpcomsh_is_site_sticker_active' ) ) {
18        return wpcomsh_is_site_sticker_active( $sticker );
19    } elseif ( function_exists( 'has_blog_sticker' ) ) {
20        return has_blog_sticker( $sticker, get_wpcom_blog_id() );
21    }
22    return false;
23}
24
25/**
26 * Determine if the site is part of the HTTP-polling gradual rollout.
27 *
28 * @return bool
29 */
30function wpcom_is_rtc_http_polling_rollout() {
31    if (
32        defined( 'IS_ATOMIC' ) && IS_ATOMIC &&
33        ! wpcom_has_features_edge_sticker() // Sites with the sticker should use WS.
34    ) {
35        return true;
36    }
37    return false;
38}
39
40/**
41 * Determine if the site is part of the HTTP-polling gradual rollout.
42 *
43 * @return bool
44 */
45function wpcom_is_rtc_websocket_rollout() {
46    if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
47        return true;
48    }
49    return false;
50}
51
52/**
53 * Determine whether the current request is from the WordPress.com desktop app.
54 *
55 * @return bool
56 */
57function wpcom_rtc_is_desktop_app() {
58    if ( ! isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
59        return false;
60    }
61    $user_agent = sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) );
62    return false !== strpos( $user_agent, 'WordPressDesktop' );
63}
64
65/**
66 * Determine whether RTC should be enabled based on the site's features.
67 *
68 * @return bool
69 */
70function wpcom_enable_rtc() {
71    // Disable RTC on the desktop app due to an incompatibility.
72    if ( wpcom_rtc_is_desktop_app() ) {
73        return false;
74    }
75    $has_rtc_feature = false;
76    if ( function_exists( 'wpcom_site_has_feature' ) && class_exists( 'WPCOM_Features' ) && defined( 'WPCOM_Features::REAL_TIME_COLLABORATION' ) ) {
77        $blog_id         = get_wpcom_blog_id();
78        $has_rtc_feature = wpcom_site_has_feature( \WPCOM_Features::REAL_TIME_COLLABORATION, $blog_id );
79    }
80
81    if ( ! $has_rtc_feature ) {
82        return false;
83    }
84
85    $has_needed_gutenberg_version = defined( 'GUTENBERG_VERSION' ) && is_string( GUTENBERG_VERSION ) && version_compare( (string) GUTENBERG_VERSION, '22.7.0', '>=' );
86
87    if ( ! $has_needed_gutenberg_version ) {
88        return false;
89    }
90
91    if ( wpcom_is_rtc_http_polling_rollout() || wpcom_is_rtc_websocket_rollout() ) {
92        return true;
93    }
94
95    if ( wpcom_has_features_edge_sticker() ) {
96        return true;
97    }
98
99    return false;
100}
101add_filter( 'jetpack_rtc_enabled', 'wpcom_enable_rtc' );
102
103/**
104 * Filters the list of Real-Time Communication (RTC) providers.
105 *
106 * @param array $providers An array of available RTC providers.
107 *
108 * @return array Modified array of RTC providers, enforcing 'http-polling' if necessary.
109 */
110function wpcom_rtc_providers( $providers ) {
111    if ( wpcom_is_rtc_http_polling_rollout() ) {
112        return array( 'http-polling' );
113    }
114    return $providers;
115}
116add_filter( 'jetpack_rtc_providers', 'wpcom_rtc_providers' );
117
118add_filter( 'jetpack_rtc_enable_limit_notices', '__return_false', 99 );
119
120\Automattic\Jetpack\RTC::init();