Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
21.21% covered (danger)
21.21%
21 / 99
18.18% covered (danger)
18.18%
2 / 11
CRAP
n/a
0 / 0
wpcomsh_is_admin_menu_api_request
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
wpcomsh_mimic_admin_page_load
83.33% covered (warning)
83.33%
15 / 18
0.00% covered (danger)
0.00%
0 / 1
2.02
wpcomsh_admin_color_scheme_picker_disabled
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
wpcomsh_set_connected_user_data_as_user_options
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
56
wpcomsh_activate_nav_unification
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
56
wpcomsh_is_site_sticker_active
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
wpcomsh_get_plugin_install_calypso_url
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
42
wpcomsh_maybe_redirect_plugin_install_to_calypso
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
wpcomsh_get_wpcom_admin_interface_option
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
wpcomsh_get_wpcom_admin_interface_default
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
wpcomsh_unsync_color_schemes_on_save
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Customizations to the Masterbar module available in Jetpack.
4 * We want that feature to always be available on Atomic sites.
5 *
6 * @package wpcomsh
7 */
8
9use Automattic\Jetpack\Connection\Manager as Connection_Manager;
10
11/**
12 * Check if the current request is an API request to the `wpcom/v2/admin-menu` endpoint.
13 *
14 * @return bool
15 */
16function wpcomsh_is_admin_menu_api_request() {
17    $route = wp_unslash( $_GET['rest_route'] ?? '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
18    return str_starts_with( wp_unslash( $_SERVER['REQUEST_URI'] ), '/?rest_route=%2Fwpcom%2Fv2%2Fadmin-menu' ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
19        && is_string( $route ) && str_starts_with( $route, '/wpcom/v2/admin-menu' );
20}
21
22/**
23 * Makes `is_admin()` return true on API requests for admin menus.
24 *
25 * Attempt to increase our chances that third-party plugins will
26 * register their menu items based on `is_admin()` returning true.
27 *
28 * This has to run before plugins are loaded.
29 */
30function wpcomsh_mimic_admin_page_load() {
31    if ( wpcomsh_is_admin_menu_api_request() ) {
32        // Display errors can cause the API request to fail due to the PHP notice
33        // triggered by `$pagenow` not being correctly determined when `is_admin()`
34        // is forced on a non-WP Admin page.
35        @ini_set( 'display_errors', false ); // phpcs:ignore
36
37        // Not WP_ADMIN: rest_api_loaded() refuses to serve any request that defines it.
38        require_once ABSPATH . 'wp-admin/includes/class-wp-screen.php';
39        $GLOBALS['current_screen'] = WP_Screen::get( 'dashboard' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
40
41        add_action(
42            'wp_loaded',
43            function () {
44                require_once ABSPATH . 'wp-admin/includes/admin.php';
45            }
46        );
47
48        // Valid requests will exit before here, but ensure custom code doesn't handle things poorly.
49        add_action(
50            'wp',
51            /**
52             * Rejects the request.
53             *
54             * @return never
55             */
56            function () {
57                status_header( 400 );
58                exit;
59            },
60            PHP_INT_MIN
61        );
62    }
63}
64add_action( 'muplugins_loaded', 'wpcomsh_mimic_admin_page_load' );
65
66/**
67 * Prints the calypso page link for changing a color scheme.
68 **/
69function wpcomsh_admin_color_scheme_picker_disabled() {
70    printf(
71        '<a target="_blank" href="%1$s">%2$s</a>',
72        esc_url( 'https://wordpress.com/me/account' ),
73        esc_html( __( 'Set your color scheme on WordPress.com.', 'wpcomsh' ) )
74    );
75}
76
77/**
78 * Gets data from the `wpcom.getUser` XMLRPC response and set it as user options. This is hooked
79 * into the `set_transient` action that is triggered everytime the XMLRPC response is read.
80 *
81 * @see https://github.com/Automattic/jetpack/blob/57ca1d524a6f6e446c5a3891d3024c71a6b0684b/projects/packages/connection/src/class-manager.php#L676
82 *
83 * @param string $transient  The name of the transient.
84 * @param mixed  $value      Transient value.
85 */
86function wpcomsh_set_connected_user_data_as_user_options( $transient, $value ) {
87    if ( 0 !== strpos( (string) $transient, 'jetpack_connected_user_data_' . get_current_user_id() ) ) {
88        return;
89    }
90
91    if ( ! $value || ! is_array( $value ) ) {
92        return;
93    }
94
95    if ( isset( $value['site_count'] ) ) {
96        update_user_option( get_current_user_id(), 'wpcom_site_count', $value['site_count'] );
97    }
98
99    if ( isset( $value['sidebar_collapsed'] ) ) {
100        set_user_setting( 'mfold', $value['sidebar_collapsed'] ? 'f' : 'o' );
101    }
102}
103
104add_action( 'set_transient', 'wpcomsh_set_connected_user_data_as_user_options', 10, 2 );
105
106/**
107 * Determines whether Nav Unification should be enabled (pbAPfg-Ou-p2).
108 *
109 * This function is hooked into the `jetpack_load_admin_menu_class` filter that lives in Jetpack.
110 * See https://github.com/Automattic/jetpack/blob/507142b09bae12b58e84c0c2b7d20024563f170d/modules%2Fmasterbar.php#L29.
111 *
112 * @return bool Whether Nav Unification should be enabled.
113 */
114function wpcomsh_activate_nav_unification() {
115    remove_filter( 'pre_option_wpcom_admin_interface', 'wpcom_admin_interface_pre_get_option' );
116    $is_wp_admin_menu = get_option( 'wpcom_admin_interface' ) === 'wp-admin';
117
118    if ( function_exists( 'wpcom_admin_interface_pre_get_option' ) ) {
119        add_filter( 'pre_option_wpcom_admin_interface', 'wpcom_admin_interface_pre_get_option', 10 );
120    }
121
122    // Disable when in the redesigned nav.
123    if ( $is_wp_admin_menu ) {
124        return false;
125    }
126
127    $user_id = get_current_user_id();
128
129    // Loads for all API requests to the admin-menu endpoint (i.e. Calypso).
130    if ( wpcomsh_is_admin_menu_api_request() ) {
131        return true;
132    }
133
134    // Disable when explicitly requested. This is an escape hatch for HEs. See paYJgx-1p8-p2.
135    if ( isset( $_GET['disable-nav-unification'] ) ) { // phpcs:ignore WordPress.Security
136        return false;
137    }
138
139    // Disable for users not connected to WP.com.
140    if ( ! class_exists( 'Automattic\Jetpack\Connection\Manager' ) ) {
141        return false;
142    }
143    $connection_manager = new Connection_Manager( 'jetpack' );
144    if ( ! $connection_manager->is_user_connected( $user_id ) ) {
145        return false;
146    }
147
148    // Enabled by default.
149    return true;
150}
151add_filter( 'jetpack_load_admin_menu_class', 'wpcomsh_activate_nav_unification' );
152
153/**
154 * Checks if site sticker is toggled on/off.
155 * For further information/context on Atomic_Persistent_Data and site_stickers please also see this diff: D66496-code
156 *
157 * @param string $sticker_name Name of the site sticker to check.
158 * @return boolean
159 */
160function wpcomsh_is_site_sticker_active( $sticker_name ) {
161    if ( ! class_exists( '\Atomic_Persistent_Data' ) ) {
162        return false;
163    }
164
165    return (bool) wpcomsh_get_atomic_persistent_data()->{"site_sticker_{$sticker_name}"};
166}
167
168/**
169 * Gets the Calypso Plugins marketplace URL to send a plugin-install.php request to.
170 *
171 * Only the bare page moves: themes open plugin-install.php?tab=plugin-information in a popup, which Calypso cannot load.
172 *
173 * @param bool $has_query Whether the request has a query string.
174 *
175 * @return string|null The Calypso URL, or null to stay in wp-admin.
176 */
177function wpcomsh_get_plugin_install_calypso_url( $has_query ) {
178    if ( $has_query ) {
179        return null;
180    }
181
182    if ( ! class_exists( 'Automattic\Jetpack\Status' ) || get_option( 'wpcom_admin_interface' ) === 'wp-admin' ) {
183        return null;
184    }
185
186    if ( ! class_exists( 'Jetpack' ) || ! Jetpack::is_module_active( 'sso' ) ) {
187        return null;
188    }
189
190    return 'https://wordpress.com/plugins/' . ( new Automattic\Jetpack\Status() )->get_site_suffix();
191}
192
193/**
194 * Redirects the Add New plugin screen to the Calypso Plugins marketplace.
195 *
196 * @return void
197 */
198function wpcomsh_maybe_redirect_plugin_install_to_calypso() {
199    // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only checks whether the request has a query.
200    $calypso_url = wpcomsh_get_plugin_install_calypso_url( ! empty( $_GET ) );
201
202    if ( null === $calypso_url ) {
203        return;
204    }
205
206    wp_safe_redirect( $calypso_url );
207    exit( 0 );
208}
209add_action( 'load-plugin-install.php', 'wpcomsh_maybe_redirect_plugin_install_to_calypso' );
210
211/**
212 * Enable the mailbox in WPCOMSH sites.
213 */
214add_filter( 'jetpack_show_wpcom_inbox_menu', '__return_true' );
215
216/**
217 * Ensure that when getting the wpcom_admin_interface option, we return a value from the persistent data store if
218 * available, otherwise fall back to the option value.
219 *
220 * @return false|string|null
221 */
222function wpcomsh_get_wpcom_admin_interface_option() {
223
224    if ( class_exists( '\Atomic_Persistent_Data' ) ) {
225        $persistent_data = wpcomsh_get_atomic_persistent_data();
226
227        // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
228        if ( isset( $persistent_data->WPCOM_ADMIN_INTERFACE ) ) {
229            // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
230            return $persistent_data->WPCOM_ADMIN_INTERFACE;
231        }
232    }
233
234    // Fallback to the current option value.
235    return false;
236}
237// This pre_option_{$option} hook runs before the option is retrieved from the database with get_option().
238add_filter( 'pre_option_wpcom_admin_interface', 'wpcomsh_get_wpcom_admin_interface_option' );
239
240/**
241 * Use the Classic style (WP Admin) on a site that never chose an admin interface style, as WordPress.com does.
242 *
243 * It replaces a default the caller passes too, such as the one Settings > General passes to check a radio.
244 *
245 * @return string
246 */
247function wpcomsh_get_wpcom_admin_interface_default() {
248    return 'wp-admin';
249}
250add_filter( 'default_option_wpcom_admin_interface', 'wpcomsh_get_wpcom_admin_interface_default' );
251
252/**
253 * Color scheme from Calypso (/me/account) is stored as user option,
254 * while color scheme from /wp-admin/profile.php is stored as user meta.
255 *
256 * When saving the color scheme from /wp-admin/profile.php, we want to unsync them:
257 *
258 * 1. Read the current color scheme from Calypso (if there's still any).
259 * 2. Delete the color scheme from Calypso, effectively only using color scheme
260 *    from wp-admin going forward.
261 */
262function wpcomsh_unsync_color_schemes_on_save() {
263    // Returns Calypso color scheme (if still exists),
264    // or wp-admin color scheme otherwise.
265    $maybe_synced_color_scheme = get_user_option( 'admin_color' );
266
267    // Always return the (maybe) synced color scheme above.
268    add_filter(
269        'get_user_metadata',
270        function ( $value, $user_id, $meta_key ) use ( $maybe_synced_color_scheme ) {
271            if ( $meta_key === 'admin_color' ) {
272                return $maybe_synced_color_scheme;
273            }
274        },
275        999999,
276        3
277    );
278
279    // Delete the synced color scheme.
280    add_filter(
281        'update_user_metadata',
282        function ( $check, $user_id, $meta_key ) {
283            if ( $meta_key === 'admin_color' ) {
284                delete_user_option( $user_id, 'admin_color' );
285            }
286        },
287        10,
288        3
289    );
290}
291add_action( 'wp_ajax_save-user-color-scheme', 'wpcomsh_unsync_color_schemes_on_save', 1 );