Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
66.67% covered (warning)
66.67%
28 / 42
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
66.67% covered (warning)
66.67%
12 / 18
0.00% covered (danger)
0.00%
0 / 1
19.26
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    /**
72     * Filters whether the WordPress.com real-time collaboration rollout is active.
73     *
74     * The rollout is switched off (DOTCOM-18664): RTC has no Product prioritization,
75     * and Gutenberg is removing the `wp.sync` global that the PingHub transport is
76     * built against. The gating below is kept intact so the rollout can be restored
77     * through this filter. P2 and WP for Teams sites are allowed by the wpcom layer
78     * at priority 20 on HTTP polling, which does not use `wp.sync`, so they are
79     * unaffected.
80     *
81     * @since $$next-version$$
82     *
83     * @param bool $enabled Whether the rollout is active. Default false.
84     */
85    if ( ! apply_filters( 'wpcom_rtc_rollout_enabled', false ) ) {
86        return false;
87    }
88
89    // Disable RTC on the desktop app due to an incompatibility.
90    if ( wpcom_rtc_is_desktop_app() ) {
91        return false;
92    }
93    $has_rtc_feature = false;
94    if ( function_exists( 'wpcom_site_has_feature' ) && class_exists( 'WPCOM_Features' ) && defined( 'WPCOM_Features::REAL_TIME_COLLABORATION' ) ) {
95        $blog_id         = get_wpcom_blog_id();
96        $has_rtc_feature = wpcom_site_has_feature( \WPCOM_Features::REAL_TIME_COLLABORATION, $blog_id );
97    }
98
99    if ( ! $has_rtc_feature ) {
100        return false;
101    }
102
103    $has_needed_gutenberg_version = defined( 'GUTENBERG_VERSION' ) && is_string( GUTENBERG_VERSION ) && version_compare( (string) GUTENBERG_VERSION, '22.7.0', '>=' );
104
105    if ( ! $has_needed_gutenberg_version ) {
106        return false;
107    }
108
109    if ( wpcom_is_rtc_http_polling_rollout() || wpcom_is_rtc_websocket_rollout() ) {
110        return true;
111    }
112
113    if ( wpcom_has_features_edge_sticker() ) {
114        return true;
115    }
116
117    return false;
118}
119add_filter( 'jetpack_rtc_enabled', 'wpcom_enable_rtc' );
120
121/**
122 * Filters the list of Real-Time Communication (RTC) providers.
123 *
124 * @param array $providers An array of available RTC providers.
125 *
126 * @return array Modified array of RTC providers, enforcing 'http-polling' if necessary.
127 */
128function wpcom_rtc_providers( $providers ) {
129    if ( wpcom_is_rtc_http_polling_rollout() ) {
130        return array( 'http-polling' );
131    }
132    return $providers;
133}
134add_filter( 'jetpack_rtc_providers', 'wpcom_rtc_providers' );
135
136add_filter( 'jetpack_rtc_enable_limit_notices', '__return_false', 99 );
137
138\Automattic\Jetpack\RTC::init();