Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.37% covered (warning)
85.37%
70 / 82
40.00% covered (danger)
40.00%
4 / 10
CRAP
n/a
0 / 0
wpcom_is_wordpress_agent_enabled
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
is_fully_managed_agency_site
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
is_wpcom_user
80.00% covered (warning)
80.00%
8 / 10
0.00% covered (danger)
0.00%
0 / 1
3.07
wpcom_get_site_slug
57.14% covered (warning)
57.14%
4 / 7
0.00% covered (danger)
0.00%
0 / 1
8.83
wpcom_get_calypso_origin
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
jetpack_mu_wpcom_enqueue_assets
93.33% covered (success)
93.33%
28 / 30
0.00% covered (danger)
0.00%
0 / 1
9.02
get_wpcom_blog_id
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
7
is_woa_site
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
is_user_connected
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
wpcom_has_blog_sticker
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
5.20
1<?php
2/**
3 * Utility functions for jetpack-mu-wpcom.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8use Automattic\Jetpack\Connection\Manager as Connection_Manager;
9use Automattic\Jetpack\Jetpack_Mu_Wpcom;
10
11/**
12 * Whether the WordPress Agent is enabled on this site.
13 *
14 * Simple combines the platform state with the saved option; Atomic reads the option.
15 *
16 * @return bool
17 */
18function wpcom_is_wordpress_agent_enabled(): bool {
19    if ( function_exists( 'big_sky_is_enabled' ) ) {
20        // @phan-suppress-next-line PhanUndeclaredFunction -- Provided by WPCOM's Big Sky mu-plugin; guarded above.
21        return (bool) get_option( 'big_sky_enable', '1' ) && (bool) big_sky_is_enabled();
22    }
23
24    return (bool) get_option( 'big_sky_enable', '0' );
25}
26
27/**
28 * Whether the site is fully managed agency site.
29 *
30 * @return bool True if the site is fully managed agency site.
31 */
32function is_fully_managed_agency_site() {
33    return ! empty( get_option( 'is_fully_managed_agency_site' ) );
34}
35
36/**
37 * Whether the current user is logged-in via WordPress.com account.
38 *
39 * @return bool True if the user has associated WordPress.com account.
40 */
41function is_wpcom_user() {
42    // If the site is explicitly marked as agency-managed, treat the user as non-wpcom user.
43    if ( is_fully_managed_agency_site() ) {
44        return false;
45    }
46
47    $user_id = get_current_user_id();
48
49    if ( function_exists( '\A8C\Billingdaddy\Users\get_wpcom_user' ) ) {
50        // On Simple sites, use get_wpcom_user function to check if the user has a WordPress.com account.
51        $user        = \A8C\Billingdaddy\Users\get_wpcom_user( $user_id );
52        $has_account = isset( $user->ID );
53    } else {
54        // On Atomic sites, use the Connection Manager to check if the user has a WordPress.com account.
55        $connection_manager = new Connection_Manager();
56        $wpcom_user_data    = $connection_manager->get_connected_user_data( $user_id );
57        $has_account        = isset( $wpcom_user_data['ID'] );
58    }
59
60    return $has_account;
61}
62
63/**
64 * Helper function to return the site slug for Calypso URLs.
65 * The fallback logic here is derived from the following code:
66 *
67 * @see https://github.com/Automattic/wc-calypso-bridge/blob/85664e2c7836b2ddc29e99871ec2c5dc4015bcc8/class-wc-calypso-bridge.php#L227-L251
68 *
69 * @return string
70 */
71function wpcom_get_site_slug() {
72    if ( defined( 'IS_WPCOM' ) && IS_WPCOM && class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) {
73        return WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() );
74    }
75
76    // The Jetpack class should be auto-loaded if Jetpack has been loaded,
77    // but we've seen fatal errors from cases where the class wasn't defined.
78    // So let's make double-sure it exists before calling it.
79    if ( class_exists( '\Automattic\Jetpack\Status' ) ) {
80        $jetpack_status = new \Automattic\Jetpack\Status();
81
82        return $jetpack_status->get_site_suffix();
83    }
84
85    // If the Jetpack Status class doesn't exist, fall back on site_url()
86    // with any trailing '/' characters removed.
87    $site_url = untrailingslashit( site_url( '/', 'https' ) );
88
89    // Remove the leading 'https://' and replace any remaining `/` characters with ::
90    return str_replace( '/', '::', substr( $site_url, 8 ) );
91}
92
93/**
94 * Returns the Calypso domain that originated the current request.
95 *
96 * @return string
97 */
98function wpcom_get_calypso_origin() {
99    $origin  = ! empty( $_GET['calypso_origin'] ) ? wp_unslash( $_GET['calypso_origin'] ) : 'https://wordpress.com'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
100    $allowed = array(
101        'http://calypso.localhost:3000',
102        'http://127.0.0.1:41050', // Desktop App.
103        'https://wpcalypso.wordpress.com',
104        'https://horizon.wordpress.com',
105        'https://wordpress.com',
106        'http://my.localhost:3000',
107        'https://my.wordpress.com',
108    );
109    return in_array( $origin, $allowed, true ) ? $origin : 'https://wordpress.com';
110}
111
112/**
113 * Returns the Calypso domain that originated the current request.
114 *
115 * @param string $asset_name The name of the asset.
116 * @param array  $asset_types The types of the asset.
117 *
118 * @return string
119 */
120function jetpack_mu_wpcom_enqueue_assets( $asset_name, $asset_types = array() ) {
121    $asset_handle = "jetpack-mu-wpcom-$asset_name";
122    $asset_path   = Jetpack_Mu_Wpcom::BASE_DIR . "build/$asset_name/$asset_name.asset.php";
123    $asset_file   = file_exists( $asset_path ) ? include $asset_path : array();
124    if ( ! is_array( $asset_file ) ) {
125        $asset_file = array();
126    }
127
128    if ( in_array( 'js', $asset_types, true ) ) {
129        $js_file      = "build/$asset_name/$asset_name.js";
130        $js_path      = Jetpack_Mu_Wpcom::BASE_DIR . $js_file;
131        $dependencies = $asset_file['dependencies'] ?? array();
132        $version      = $asset_file['version'] ?? ( file_exists( $js_path ) ? filemtime( $js_path ) : null );
133        wp_enqueue_script(
134            "jetpack-mu-wpcom-$asset_name",
135            plugins_url( $js_file, Jetpack_Mu_Wpcom::BASE_FILE ),
136            $dependencies,
137            $version,
138            true
139        );
140        if ( in_array( 'wp-i18n', $dependencies, true ) ) {
141            wp_set_script_translations( $asset_handle, 'jetpack-mu-wpcom' );
142        }
143    }
144
145    if ( in_array( 'css', $asset_types, true ) ) {
146        $css_ext  = is_rtl() ? 'rtl.css' : 'css';
147        $css_file = "build/$asset_name/$asset_name.$css_ext";
148        $css_path = Jetpack_Mu_Wpcom::BASE_DIR . $css_file;
149        wp_enqueue_style(
150            "jetpack-mu-wpcom-$asset_name",
151            plugins_url( $css_file, Jetpack_Mu_Wpcom::BASE_FILE ),
152            array(),
153            file_exists( $css_path ) ? filemtime( $css_path ) : null
154        );
155    }
156
157    return $asset_handle;
158}
159
160/**
161 * Returns the WP.com blog ID for the current site.
162 *
163 * @return int|false The WP.com blog ID, or false if the site does not have a WP.com blog ID.
164 */
165function get_wpcom_blog_id() {
166    if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
167        return get_current_blog_id();
168    }
169
170    if ( defined( 'IS_ATOMIC' ) && IS_ATOMIC ) {
171        /*
172         * Atomic sites have the WP.com blog ID stored as a Jetpack option. This
173         * code deliberately doesn't use `Jetpack_Options::get_option` so it
174         * works even when Jetpack has not been loaded.
175         */
176        $jetpack_options = get_option( 'jetpack_options' );
177        if ( is_array( $jetpack_options ) && isset( $jetpack_options['id'] ) ) {
178            return (int) $jetpack_options['id'];
179        }
180
181        return get_current_blog_id();
182    }
183
184    return false;
185}
186
187/**
188 * Check if the site is a WordPress.com Atomic site.
189 *
190 * @return bool
191 */
192function is_woa_site() {
193    if ( ! class_exists( 'Automattic\Jetpack\Status\Host' ) ) {
194        return false;
195    }
196    $host = new Automattic\Jetpack\Status\Host();
197    return $host->is_woa_site();
198}
199
200/**
201 * Whether the current user is connected to WordPress.com.
202 *
203 * @param int $user_id the user identifier. Default is the current user.
204 * @return bool Boolean is the user connected?
205 */
206function is_user_connected( $user_id ) {
207    if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
208        return true;
209    }
210
211    return ( new Connection_Manager( 'jetpack' ) )->is_user_connected( $user_id );
212}
213
214/**
215 * Wrapper to test a blog sticker on both Simple and Atomic sites at once.
216 *
217 * @param string $blog_sticker The blog sticker.
218 * @param int    $blog_id The WPCOM blog ID.
219 * @return bool Whether the site has the blog sticker.
220 */
221function wpcom_has_blog_sticker( $blog_sticker, $blog_id ) {
222    if ( function_exists( 'has_blog_sticker' ) && has_blog_sticker( $blog_sticker, $blog_id ) ) {
223        return true;
224    }
225    if ( function_exists( 'wpcomsh_is_site_sticker_active' ) && wpcomsh_is_site_sticker_active( $blog_sticker ) ) {
226        return true;
227    }
228    return false;
229}