Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 7
CRAP
n/a
0 / 0
Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Legacy_FSE\is_full_site_editing_active
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
30
Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Legacy_FSE\get_theme_slug
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Legacy_FSE\normalize_theme_slug
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Legacy_FSE\is_site_eligible_for_full_site_editing
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Legacy_FSE\is_theme_supported
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Legacy_FSE\get_supported_themes
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Legacy_FSE\did_insert_template_parts
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Helpers for Full Site Editing.
4 *
5 * This file is always loaded, so these functions should always exist if the
6 * plugin is activated on the site. (Not to be confused with whether FSE is
7 * active on the site!)
8 *
9 * @package automattic/jetpack-mu-wpcom
10 */
11
12namespace Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Legacy_FSE;
13
14/**
15 * Whether or not FSE is active.
16 * If false, FSE functionality should be disabled.
17 *
18 * @return bool True if FSE is active, false otherwise.
19 */
20function is_full_site_editing_active() {
21    // We will always return false in admin and REST API contexts as we work towards getting rid of this.
22    if ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) {
23        return false;
24    }
25
26    return is_site_eligible_for_full_site_editing() && is_theme_supported() && did_insert_template_parts();
27}
28
29/**
30 * Returns the slug for the current theme.
31 *
32 * This even works for the WordPress.com API context where the current theme is
33 * not correct. The filter correctly switches to the correct blog context if
34 * that is the case.
35 *
36 * @return string Theme slug.
37 */
38function get_theme_slug() {
39    /**
40     * Used to get the correct theme in certain contexts.
41     *
42     * For example, in the wpcom API context, the theme slug is a8c/public-api,
43     * so we need to grab the correct one with the filter.
44     *
45     * @since 0.7
46     *
47     * @param string current theme slug is the default if nothing overrides it.
48     */
49    return apply_filters( 'a8c_fse_get_theme_slug', get_stylesheet() );
50}
51
52/**
53 * Returns a normalized slug for the current theme.
54 *
55 * In some cases, the theme is located in a subfolder like `pub/maywood`. Use
56 * this function to get the slug without the prefix.
57 *
58 * @param string $theme_slug The raw theme_slug to normalize.
59 * @return string Theme slug.
60 */
61function normalize_theme_slug( $theme_slug ) {
62    // Normalize the theme slug.
63    if ( 'pub/' === substr( $theme_slug, 0, 4 ) ) {
64        $theme_slug = substr( $theme_slug, 4 );
65    }
66
67    if ( '-wpcom' === substr( $theme_slug, -6, 6 ) ) {
68        $theme_slug = substr( $theme_slug, 0, -6 );
69    }
70
71    return $theme_slug;
72}
73
74/**
75 * Whether or not the site is eligible for FSE. This is essentially a feature
76 * gate to disable FSE on some sites which could theoretically otherwise use it.
77 *
78 * By default, sites should not be eligible.
79 *
80 * @return bool True if current site is eligible for FSE, false otherwise.
81 */
82function is_site_eligible_for_full_site_editing() {
83    /**
84     * Can be used to disable Full Site Editing functionality.
85     *
86     * @since 0.2
87     *
88     * @param bool true if Full Site Editing should be disabled, false otherwise.
89     */
90    return ! apply_filters( 'a8c_disable_full_site_editing', true );
91}
92
93/**
94 * Whether or not current theme is enabled for FSE.
95 *
96 * @return bool True if current theme supports FSE, false otherwise.
97 */
98function is_theme_supported() {
99    if ( is_multisite() && 0 === get_current_blog_id() ) {
100        // get_theme_slug will always return false.
101        return false;
102    }
103    $slug = get_theme_slug();
104    // Use un-normalized theme slug because get_theme requires the full string.
105    $theme      = wp_get_theme( $slug );
106    $theme_slug = normalize_theme_slug( $slug );
107    return ! $theme->errors() && in_array( $theme_slug, get_supported_themes(), true );
108}
109
110/**
111 * Hardcoded list of themes we support.
112 * Once upon a time, we relied on the `full-site-editing` tag in themes,
113 * but that conflicted with Core FSE and this project has been deprecated
114 * in favour of Core.
115 *
116 * @return array List of supported themes.
117 */
118function get_supported_themes() {
119    return array(
120        'alves',
121        'exford',
122        'hever',
123        'maywood',
124        'morden',
125        'shawburn',
126        'stow',
127        'varia',
128    );
129}
130
131/**
132 * Determines if the template parts have been inserted for the current theme.
133 *
134 * We want to gate on this check in is_full_site_editing_active so that we don't
135 * load FSE for sites which did not get template parts for some reason or another.
136 *
137 * For example, if a user activates theme A on their site and gets FSE, but then
138 * activates theme B which does not have FSE, they will not get FSE flows. If we
139 * retroactively add FSE support to theme B, the user should not get FSE flows
140 * because their site would be modified. Instead, FSE flows would become active
141 * when they specifically take action to re-activate the theme.
142 *
143 * @return bool True if the template parts have been inserted. False otherwise.
144 */
145function did_insert_template_parts() {
146    require_once dirname( __DIR__ ) . '/templates/class-wp-template-inserter.php';
147
148    $theme_slug = normalize_theme_slug( get_theme_slug() );
149    $inserter   = new WP_Template_Inserter( $theme_slug );
150    return $inserter->is_template_data_inserted();
151}