Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
19.14% covered (danger)
19.14%
31 / 162
9.52% covered (danger)
9.52%
2 / 21
CRAP
n/a
0 / 0
wpcomsh_is_wpcom_theme
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
wpcomsh_is_wpcom_premium_theme
66.67% covered (warning)
66.67%
6 / 9
0.00% covered (danger)
0.00%
0 / 1
3.33
wpcomsh_is_wpcom_pub_theme
66.67% covered (warning)
66.67%
6 / 9
0.00% covered (danger)
0.00%
0 / 1
3.33
wpcomsh_symlink_theme
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
42
wpcomsh_install_managed_theme
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
42
wpcomsh_delete_theme_cache
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
wpcomsh_is_theme_symlinked
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
wpcomsh_delete_symlinked_theme
41.67% covered (danger)
41.67%
5 / 12
0.00% covered (danger)
0.00%
0 / 1
4.79
wpcomsh_get_wpcom_theme_type
55.56% covered (warning)
55.56%
5 / 9
0.00% covered (danger)
0.00%
0 / 1
13.62
wpcomsh_is_wpcom_child_theme
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
wpcomsh_count_child_themes
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
wpcomsh_symlink_parent_theme
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
wpcomsh_get_atomic_site_id
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
wpcomsh_get_atomic_client_id
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
wpcomsh_get_wpcom_active_subscriptions
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
wpcomsh_get_at_site_info
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
wpcomsh_is_xmlrpc_request_matching
33.33% covered (danger)
33.33%
2 / 6
0.00% covered (danger)
0.00%
0 / 1
16.67
wpcomsh_is_wp_rest_request_matching
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
72
wpcomsh_map_block_map_provider
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
wpcomsh_disable_fatal_error_emails
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
wpcomsh_newsletter_categories_location
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * WPCOMSH functions file.
4 *
5 * @package wpcomsh
6 */
7
8/**
9 * Whether the theme is a wpcom theme.
10 *
11 * @param string $theme_slug Theme slug.
12 * @return bool
13 */
14function wpcomsh_is_wpcom_theme( $theme_slug ) {
15    return wpcomsh_is_wpcom_premium_theme( $theme_slug ) || wpcomsh_is_wpcom_pub_theme( $theme_slug );
16}
17
18/**
19 * Whether the theme is a premium wpcom theme.
20 *
21 * @param string $theme_slug Theme slug.
22 * @return bool
23 */
24function wpcomsh_is_wpcom_premium_theme( $theme_slug ) {
25    if (
26        ! defined( 'WPCOMSH_PREMIUM_THEMES_PATH' ) ||
27        ! file_exists( WPCOMSH_PREMIUM_THEMES_PATH )
28    ) {
29        // phpcs:ignore WordPress.PHP.DevelopmentFunctions
30        error_log(
31            "WPComSH: WPCom premium themes folder couldn't be located. " .
32            'Check whether the ' . WPCOMSH_PREMIUM_THEMES_PATH . ' constant points to the correct directory.'
33        );
34
35        return false;
36    }
37
38    return file_exists(
39        WPCOMSH_PREMIUM_THEMES_PATH . "/{$theme_slug}"
40    );
41}
42
43/**
44 * Whether the theme is a free wpcom theme.
45 *
46 * @param string $theme_slug Theme slug.
47 * @return bool
48 */
49function wpcomsh_is_wpcom_pub_theme( $theme_slug ) {
50    if (
51        ! defined( 'WPCOMSH_PUB_THEMES_PATH' ) ||
52        ! file_exists( WPCOMSH_PUB_THEMES_PATH )
53    ) {
54        // phpcs:ignore WordPress.PHP.DevelopmentFunctions
55        error_log(
56            "WPComSH: WPCom pub themes folder couldn't be located. " .
57            'Check whether the ' . WPCOMSH_PUB_THEMES_PATH . ' constant points to the correct directory.'
58        );
59
60        return false;
61    }
62
63    return file_exists(
64        WPCOMSH_PUB_THEMES_PATH . "/{$theme_slug}"
65    );
66}
67
68/**
69 * Creates a theme's symlink. To install a theme, use wpcomsh_install_managed_theme(), which also handles its parent.
70 *
71 * @param string $theme_slug Theme slug.
72 * @param string $theme_type Type of theme.
73 * @return bool|WP_Error
74 */
75function wpcomsh_symlink_theme( $theme_slug, $theme_type ) {
76    /**
77     * We need to read from the themes_path constants and create the symlink using the themes_symlink constant.
78     *
79     * More context here:
80     * - p1487627624008111-slack-C2PDURDSL
81     * - p1713351585355929-slack-C7YPW6K40
82     */
83    $theme_link_to_path = get_theme_root() . '/' . $theme_slug;
84    $theme_read_path    = '';
85    $theme_symlink_path = '';
86    if ( WPCOMSH_PUB_THEME_TYPE === $theme_type ) {
87        $theme_read_path    = WPCOMSH_PUB_THEMES_PATH . "/$theme_slug";
88        $theme_symlink_path = WPCOMSH_PUB_THEMES_SYMLINK . "/$theme_slug";
89    } elseif ( WPCOMSH_PREMIUM_THEME_TYPE === $theme_type ) {
90        $theme_read_path    = WPCOMSH_PREMIUM_THEMES_PATH . "/$theme_slug";
91        $theme_symlink_path = WPCOMSH_PREMIUM_THEMES_SYMLINK . "/$theme_slug";
92    } elseif ( WPCOMSH_MANAGED_THEME_TYPE === $theme_type ) {
93        $theme_read_path    = THEMES_PATH_BASE . "/$theme_slug/latest";
94        $theme_symlink_path = THEMES_SYMLINK_BASE . "/$theme_slug/latest";
95    }
96
97    if ( file_exists( $theme_read_path ) && symlink( $theme_symlink_path, $theme_link_to_path ) ) {
98        return true;
99    }
100
101    $error_message = "Could not install theme $theme_slug.";
102    $debug_message = "$error_message Read directory: $theme_read_path, symlink directory: $theme_symlink_path, link to: $theme_link_to_path.";
103
104    error_log( 'WPComSH: ' . $debug_message ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
105
106    return new WP_Error( 'error_symlinking_theme', $error_message );
107}
108
109/**
110 * Installs a theme WP Cloud manages by symlinking it, and its parent if it is a child theme.
111 *
112 * @param string $theme_slug Theme slug.
113 * @param string $theme_type Type of theme.
114 *
115 * @return true|\WP_Error
116 */
117function wpcomsh_install_managed_theme( $theme_slug, $theme_type ) {
118    if ( wpcomsh_is_theme_symlinked( $theme_slug ) ) {
119        error_log( "WPComSH: WP.com theme with slug: {$theme_slug} is already installed/symlinked." ); //phpcs:ignore
120
121        return new WP_Error( 'wpcom_theme_already_installed', 'The WP.com theme is already installed/symlinked.' );
122    }
123
124    $was_theme_symlinked = wpcomsh_symlink_theme( $theme_slug, $theme_type );
125
126    if ( is_wp_error( $was_theme_symlinked ) ) {
127        return $was_theme_symlinked;
128    }
129
130    wpcomsh_delete_theme_cache( $theme_slug );
131
132    // If the installed WPCom theme is a child theme, we need to symlink its parent theme as well, unless a working copy is already there.
133    if (
134        wpcomsh_is_wpcom_child_theme( $theme_slug ) &&
135        wp_get_theme( wp_get_theme( $theme_slug )->get_template() )->errors()
136    ) {
137        $was_parent_theme_symlinked = wpcomsh_symlink_parent_theme( $theme_slug );
138
139        if ( true !== $was_parent_theme_symlinked ) {
140            // Without its parent the child is broken, and leaving it would make a retry report it as already installed.
141            wpcomsh_delete_symlinked_theme( $theme_slug );
142            wpcomsh_delete_theme_cache( $theme_slug );
143
144            return new WP_Error( 'wpcom_theme_installation_failed', "Can't install specified WP.com theme. Check error log for more details." );
145        }
146    }
147
148    /**
149     * Fires after a WP.com theme has been installed by symlink.
150     *
151     * @param string $theme_slug Theme slug.
152     */
153    do_action( 'wpcomsh_wpcom_theme_installed', $theme_slug );
154
155    return true;
156}
157
158/**
159 * Deletes cache of the passed theme.
160 *
161 * @param string $theme_slug Optional. Slug of the theme to delete cache for.
162 *                           Default: Current theme.
163 */
164function wpcomsh_delete_theme_cache( $theme_slug = null ) {
165    $theme = wp_get_theme( $theme_slug );
166
167    if ( $theme instanceof WP_Theme ) {
168        $theme->cache_delete();
169    }
170}
171
172/**
173 * Checks whether a theme (by theme slug) is symlinked in the themes' directory.
174 *
175 * @param string $theme_slug The slug of a theme.
176 * @return bool Whether a theme is symlinked in the themes' directory.
177 */
178function wpcomsh_is_theme_symlinked( $theme_slug ) {
179    $theme_root  = get_theme_root();
180    $theme_dir   = "$theme_root/$theme_slug";
181    $site_themes = scandir( $theme_root );
182
183    return in_array( $theme_slug, $site_themes, true ) && is_link( $theme_dir );
184}
185
186/**
187 * Deletes a symlinked theme.
188 *
189 * @param string $theme_slug The slug of a theme.
190 * @return bool|WP_Error True on success, WP_Error on error.
191 */
192function wpcomsh_delete_symlinked_theme( $theme_slug ) {
193    $theme_dir = get_theme_root() . "/$theme_slug";
194
195    if ( file_exists( $theme_dir ) && is_link( $theme_dir ) ) {
196        unlink( $theme_dir ); // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink
197
198        /**
199         * Fires after a symlinked theme has been deleted.
200         *
201         * @param string $theme_slug Theme slug.
202         */
203        do_action( 'wpcomsh_symlinked_theme_deleted', $theme_slug );
204
205        return true;
206    }
207
208    // phpcs:ignore WordPress.PHP.DevelopmentFunctions
209    error_log(
210        "WPComSH: Can't delete the specified symlinked theme: the path or symlink doesn't exist."
211    );
212
213    return new WP_Error(
214        'error_deleting_symlinked_theme',
215        "Can't delete the specified symlinked theme: the path or symlink doesn't exist."
216    );
217}
218
219/**
220 * Returns a theme type.
221 *
222 * @param string $theme_slug The slug of a theme.
223 * @return false|string Theme type or false if not a wpcom theme.
224 */
225function wpcomsh_get_wpcom_theme_type( $theme_slug ) {
226    // The slug becomes a path, so one that leaves its folder would match a WP Cloud directory that isn't a theme.
227    if ( ! is_string( $theme_slug ) || '' === $theme_slug || '.' === $theme_slug[0] || false !== strpos( $theme_slug, '/' ) ) {
228        return false;
229    }
230
231    if ( wpcomsh_is_wpcom_premium_theme( $theme_slug ) ) {
232        return WPCOMSH_PREMIUM_THEME_TYPE;
233    } elseif ( wpcomsh_is_wpcom_pub_theme( $theme_slug ) ) {
234        return WPCOMSH_PUB_THEME_TYPE;
235    } elseif ( file_exists( THEMES_PATH_BASE . "/$theme_slug/latest" ) ) {
236        return WPCOMSH_MANAGED_THEME_TYPE;
237    }
238
239    return false;
240}
241
242/**
243 * Returns whether the theme is a child theme.
244 *
245 * @param string $theme_slug Slug of the theme to check. Default: Active theme.
246 * @return bool
247 */
248function wpcomsh_is_wpcom_child_theme( $theme_slug = null ) {
249    $theme = wp_get_theme( $theme_slug );
250
251    return $theme->get_stylesheet() !== $theme->get_template();
252}
253
254/**
255 * Count the number of child themes with the specified template.
256 *
257 * @param string $template The theme template name.
258 *
259 * @return int
260 */
261function wpcomsh_count_child_themes( $template ) {
262    $child_count = 0;
263
264    foreach ( wp_get_themes() as $theme ) {
265        if (
266            $theme->get_template() === $template &&
267            $theme->get_stylesheet() !== $theme->get_template()
268        ) {
269            ++$child_count;
270        }
271    }
272
273    return $child_count;
274}
275
276/**
277 * Symlinks the theme's parent if it's a child theme.
278 *
279 * @param string $stylesheet Theme slug.
280 * @return bool|WP_Error
281 */
282function wpcomsh_symlink_parent_theme( $stylesheet ) {
283    $theme    = wp_get_theme( $stylesheet );
284    $template = $theme->get_template();
285
286    if ( $template === $stylesheet ) {
287        // phpcs:ignore WordPress.PHP.DevelopmentFunctions
288        error_log( "WPComSH: Can't symlink parent theme. Current theme is not a child theme." );
289
290        return false;
291    }
292
293    // phpcs:ignore WordPress.PHP.DevelopmentFunctions
294    error_log( 'WPComSH: Symlinking parent theme.' );
295
296    return wpcomsh_symlink_theme( $template, wpcomsh_get_wpcom_theme_type( $template ) );
297}
298
299/**
300 * Returns the Atomic site ID.
301 *
302 * @return int
303 */
304function wpcomsh_get_atomic_site_id() {
305    if ( defined( 'ATOMIC_SITE_ID' ) ) {
306        return (int) ATOMIC_SITE_ID;
307    }
308
309    $atomic_site_id = apply_filters( 'wpcomsh_get_atomic_site_id', 0 );
310    if ( ! empty( $atomic_site_id ) ) {
311        return (int) $atomic_site_id;
312    }
313
314    return 0;
315}
316
317/**
318 * Returns the Atomic client ID.
319 *
320 * @return int
321 */
322function wpcomsh_get_atomic_client_id() {
323    if ( defined( 'ATOMIC_CLIENT_ID' ) ) {
324        return (int) ATOMIC_CLIENT_ID;
325    }
326
327    $atomic_client_id = apply_filters( 'wpcomsh_get_atomic_client_id', 0 );
328    if ( ! empty( $atomic_client_id ) ) {
329        return (int) $atomic_client_id;
330    }
331
332    return 0;
333}
334
335/**
336 * Returns an array of active WordPress.com subscriptions. The array keys are the product slugs.
337 *
338 * @return array
339 */
340function wpcomsh_get_wpcom_active_subscriptions() {
341    $persistent_data = wpcomsh_get_atomic_persistent_data();
342
343    if ( ! $persistent_data->WPCOM_PURCHASES ) { // phpcs:ignore WordPress.NamingConventions
344        return array();
345    }
346
347    $wpcom_purchases = json_decode( $persistent_data->WPCOM_PURCHASES ); // phpcs:ignore WordPress.NamingConventions
348
349    // If ( for any reason ) $wpcom_purchases is not an array, return early an empty array so that array_reduce doesn't throw an error.
350    if ( ! is_array( $wpcom_purchases ) ) {
351        return array();
352    }
353
354    return array_reduce(
355        $wpcom_purchases,
356        function ( $assoc_array, $purchase ) {
357            /**
358             * Check if the billing_product_slug exists, if not, revert to parsing the store product slug.
359             * This happens for sites that don't have the APD updated to the new format.
360             */
361            if ( isset( $purchase->billing_product_slug ) ) {
362                $product_slug = $purchase->billing_product_slug;
363            } else {
364                // 1. Remove _monthly or _yearly ( mainly found in marketplace plugins ).
365                // 2. Transform to product slug pattern with dashes (billing product slug).
366                $product_slug = preg_replace( array( '/(_monthly|_yearly)$/', '/_/' ), array( '', '-' ), $purchase->product_slug );
367            }
368
369            $assoc_array[ $product_slug ] = $purchase;
370
371            return $assoc_array;
372        },
373        array()
374    );
375}
376add_filter( 'pre_option_wpcom_active_subscriptions', 'wpcomsh_get_wpcom_active_subscriptions' );
377
378/**
379 * Get Atomic site information.
380 *
381 * @return array
382 */
383function wpcomsh_get_at_site_info() {
384    $at_site_info_file = sys_get_temp_dir() . '/.at-site-info';
385
386    if ( ! is_file( $at_site_info_file ) ) {
387        return array();
388    }
389
390    $site_info_json = file_get_contents( $at_site_info_file ); // phpcs:ignore WordPress.WP.AlternativeFunctions
391
392    if ( empty( $site_info_json ) ) {
393        return array();
394    }
395
396    $site_info = json_decode( $site_info_json, true );
397    if ( empty( $site_info ) ) {
398        return array();
399    }
400
401    return $site_info;
402}
403
404/**
405 * Whether the current request is an XML-RPC request from Calypso to install a theme or plugin.
406 *
407 * @param string $path_regex Regular expression of paths to allow.
408 * @return bool
409 */
410function wpcomsh_is_xmlrpc_request_matching( $path_regex ) {
411    // Return early for all non-API requests.
412    if ( ! defined( 'REST_API_REQUEST' ) || ! REST_API_REQUEST ) {
413        return false;
414    }
415
416    // Return early-ish when it's not a verified XML-RPC request.
417    if (
418        ! method_exists( 'Automattic\Jetpack\Connection\Manager', 'verify_xml_rpc_signature' ) ||
419        ! ( new Automattic\Jetpack\Connection\Manager() )->verify_xml_rpc_signature() ) {
420        return false;
421    }
422
423    return class_exists( 'WPCOM_JSON_API' ) && preg_match( $path_regex, WPCOM_JSON_API::$self->path );
424}
425
426/**
427 * Check if we are handling a WordPress core REST v2 API request where the API path matches
428 * the regular expression in $path_regex and the HTTP method(s) in $request_method.
429 *
430 * @param string          $path_regex      A regular expression for the requested path, which should include the REST prefix available from {@see rest_get_url_prefix()}.
431 * @param string|string[] $request_method  The expected HTTP method(s) of the request, e.g. GET|POST|PUT|DELETE or array( 'PUT', 'POST' ).
432 * @return bool                            Whether the incoming request matches the supplied path and method(s).
433 */
434function wpcomsh_is_wp_rest_request_matching( $path_regex, $request_method = 'GET' ) {
435    if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) {
436        return false;
437    }
438
439    $request_methods = is_array( $request_method ) ? $request_method : array( $request_method );
440
441    if ( ! isset( $_SERVER['REQUEST_METHOD'] ) || ! in_array( $_SERVER['REQUEST_METHOD'], $request_methods, true ) ) {
442        return false;
443    }
444
445    // REQUEST_URI can't be trusted here: in `/wp-json/foo?rest_route=/bar`, WP serves `/bar`.
446    $route = $GLOBALS['wp']->query_vars['rest_route'] ?? '';
447    if ( ! is_string( $route ) || $route === '' ) {
448        return false;
449    }
450
451    $rest_path = '/' . rest_get_url_prefix() . $route;
452
453    return 1 === preg_match( $path_regex, $rest_path );
454}
455
456/**
457 * Returns the map provider the map block should use.
458 *
459 * @return string
460 */
461function wpcomsh_map_block_map_provider() {
462    return 'mapkit';
463}
464
465add_filter( 'wpcom_map_block_map_provider', 'wpcomsh_map_block_map_provider', 10, 0 );
466
467/**
468 * Disabled fatal error emails if the option is set.
469 *
470 * @param array $email The email arguments.
471 *
472 * @return array The email arguments.
473 */
474function wpcomsh_disable_fatal_error_emails( $email ) {
475    if ( get_option( 'wpcomsh_disable_fatal_error_emails', false ) ) {
476        $email['to'] = '';
477    }
478    return $email;
479}
480
481add_filter( 'recovery_mode_email', 'wpcomsh_disable_fatal_error_emails' );
482
483/**
484 * Returns the location where newsletter categories should appear
485 *
486 * @return string
487 */
488function wpcomsh_newsletter_categories_location() {
489    return 'modal';
490}
491
492add_filter( 'wpcom_newsletter_categories_location', 'wpcomsh_newsletter_categories_location', 10, 0 );
493
494/**
495 * Enables new likes layout on Atomic.
496 */
497add_filter( 'likes_new_layout', '__return_true' );
498
499/**
500 * Disable email notification when a user's email address is changed.
501 *
502 * @see https://github.com/Automattic/wp-calypso/issues/100279
503 */
504add_filter( 'send_email_change_email', '__return_false' );