Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 5
CRAP
n/a
0 / 0
wpcomsh_maybe_disable_custom_css
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
wpcomsh_custom_css_admin_menu
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
wpcomsh_custom_css_customizer_redirect
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
wpcomsh_custom_css_remove_available_module
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
wpcomsh_custom_css_remove_active_module
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Manages Additional CSS feature for Atomic sites.
4 *
5 * @package wpcomsh
6 */
7
8/**
9 * Disable the Core custom CSS and the Jetpack custom CSS module.
10 */
11function wpcomsh_maybe_disable_custom_css() {
12    // Do not run if Jetpack is not enabled.
13    if ( ! defined( 'JETPACK__VERSION' ) ) {
14        return;
15    }
16
17    if ( wpcom_site_has_feature( WPCOM_Features::CUSTOM_DESIGN ) ) {
18        return;
19    }
20
21    // Global Styles are available on the Personal plan, so all Atomic sites have custom CSS enabled.
22    if ( defined( 'IS_ATOMIC' ) && IS_ATOMIC ) {
23        return;
24    }
25
26    add_action( 'admin_menu', 'wpcomsh_custom_css_admin_menu' );
27    add_filter( 'jetpack_get_available_modules', 'wpcomsh_custom_css_remove_available_module' );
28    add_filter( 'jetpack_active_modules', 'wpcomsh_custom_css_remove_active_module' );
29    add_filter( 'jetpack_customize_enable_additional_css_nudge', '__return_true' );
30}
31add_action( 'jetpack_loaded', 'wpcomsh_maybe_disable_custom_css' );
32
33/**
34 * Handle our admin menu item and legacy page declaration.
35 */
36function wpcomsh_custom_css_admin_menu() {
37    if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) {
38        $styles = wp_get_custom_css();
39        if ( ! $styles ) {
40            return;
41        }
42    }
43
44    // Add in our legacy page to support old bookmarks and such.
45    add_submenu_page( '', __( 'CSS', 'wpcomsh' ), __( 'Additional CSS', 'wpcomsh' ), 'edit_theme_options', 'editcss', '__return_false' );
46
47    // Add in our new page slug that will redirect to the Customizer.
48    $hook = add_theme_page( __( 'CSS', 'wpcomsh' ), __( 'Additional CSS', 'wpcomsh' ), 'edit_theme_options', 'editcss-customizer-redirect', '__return_false' );
49    add_action( 'load-' . $hook, 'wpcomsh_custom_css_customizer_redirect' );
50}
51
52/**
53 * Handle the redirect for the customizer.
54 *
55 * This is necessary because we can't directly add customizer links to the admin menu. There is a core patch in trac
56 * that would make this unnecessary.
57 *
58 * @link https://core.trac.wordpress.org/ticket/39050
59 * @return never
60 */
61function wpcomsh_custom_css_customizer_redirect() {
62    $redirect_to = add_query_arg(
63        array(
64            array(
65                'autofocus' => array( 'section' => 'custom_css' ),
66            ),
67        ),
68        admin_url( 'customize.php' )
69    );
70
71    wp_safe_redirect( $redirect_to );
72    exit( 0 );
73}
74
75/**
76 * Remove the custom-css module from the active modules in order to disable it entirely.
77 *
78 * @param array $modules A list of active modules.
79 *
80 * @return array
81 */
82function wpcomsh_custom_css_remove_available_module( $modules ) {
83    unset( $modules['custom-css'] );
84
85    return $modules;
86}
87
88/**
89 * Remove Jetpack's custom-css module from active modules.
90 *
91 * @param array $active_modules The current Jetpack active modules.
92 *
93 * @return array
94 */
95function wpcomsh_custom_css_remove_active_module( $active_modules ) {
96    return array_filter(
97        $active_modules,
98        static function ( $module ) {
99            return 'custom-css' !== $module;
100        }
101    );
102}