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