Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 579
0.00% covered (danger)
0.00%
0 / 43
CRAP
0.00% covered (danger)
0.00%
0 / 1
safecss_class
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
Jetpack_Custom_CSS_Enhancements
0.00% covered (danger)
0.00%
0 / 569
0.00% covered (danger)
0.00%
0 / 42
15500
0.00% covered (danger)
0.00%
0 / 1
 add_hooks
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
2
 init
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 customize_preview_init
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 print_linked_custom_css
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 map_meta_cap
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 wp_post_revision_fields
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
30
 get_css_post
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 wp_custom_css_cb
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
20
 post_id
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 echo_custom_css_partial
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 admin_page
0.00% covered (danger)
0.00%
0 / 75
0.00% covered (danger)
0.00%
0 / 1
110
 customizer_link
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
6
 customize_controls_enqueue_scripts
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
12
 are_there_css_revisions
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 get_revisions_url
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 get_themes
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 get_all_themes_with_custom_css
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
12
 wp_admin_enqueue_scripts
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
2
 wp_enqueue_scripts
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 sanitize_css
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 1
42
 preview_content_width
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 style_filter
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
42
 skip_stylesheet
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 preview_skip_stylesheet
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 customize_register
0.00% covered (danger)
0.00%
0 / 88
0.00% covered (danger)
0.00%
0 / 1
72
 sanitize_css_callback
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 is_freetrial
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 is_preview
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 customize_preview_wp_get_custom_css
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 customize_value_custom_css
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
30
 customize_update_custom_css_post_content_args
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
 update_custom_css_data
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
 editor_max_image_size
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
20
 jetpack_content_width
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 set_content_width
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
30
 sanitize_preprocessor
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 intval_base10
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 load_revision_php
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 custom_css_loaded
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 jetpack_custom_css_configuration_url
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 revision_admin_footer
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 1
6
 revisions_switcher_box
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
12
safecss
n/a
0 / 0
n/a
0 / 0
2
n/a
0 / 0
 postparse
n/a
0 / 0
n/a
0 / 0
1
 subvalue
n/a
0 / 0
n/a
0 / 0
1
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
4/**
5 * Alternate Custom CSS source for 4.7 compat.
6 *
7 * @since 4.4.2
8 *
9 * @package automattic/jetpack-mu-wpcom
10 */
11
12use Automattic\Jetpack\Assets;
13
14if ( ! class_exists( 'Jetpack_Custom_CSS_Enhancements' ) ) {
15    /**
16     * Class Jetpack_Custom_CSS_Enhancements
17     */
18    class Jetpack_Custom_CSS_Enhancements {
19
20        /**
21         * Set up the actions and filters needed for our compatability layer on top of core's Custom CSS implementation.
22         */
23        public static function add_hooks() {
24            add_action( 'init', array( __CLASS__, 'init' ) );
25            add_action( 'customize_controls_enqueue_scripts', array( __CLASS__, 'customize_controls_enqueue_scripts' ) );
26            add_action( 'customize_register', array( __CLASS__, 'customize_register' ) );
27            add_filter( 'map_meta_cap', array( __CLASS__, 'map_meta_cap' ), 20, 2 );
28            add_action( 'customize_preview_init', array( __CLASS__, 'customize_preview_init' ) );
29            add_filter( '_wp_post_revision_fields', array( __CLASS__, 'wp_post_revision_fields' ), 10, 2 );
30            add_action( 'load-revision.php', array( __CLASS__, 'load_revision_php' ) );
31
32            add_action( 'admin_enqueue_scripts', array( __CLASS__, 'wp_admin_enqueue_scripts' ) );
33            add_action( 'wp_enqueue_scripts', array( __CLASS__, 'wp_enqueue_scripts' ) );
34
35            // Handle Sass/LESS.
36            add_filter( 'customize_value_custom_css', array( __CLASS__, 'customize_value_custom_css' ), 10, 2 );
37            add_filter( 'customize_update_custom_css_post_content_args', array( __CLASS__, 'customize_update_custom_css_post_content_args' ), 10, 3 );
38            add_filter( 'update_custom_css_data', array( __CLASS__, 'update_custom_css_data' ), 10, 2 );
39
40            // Stuff for stripping out the theme's default stylesheet...
41            add_filter( 'stylesheet_uri', array( __CLASS__, 'style_filter' ) );
42            add_filter( 'safecss_skip_stylesheet', array( __CLASS__, 'preview_skip_stylesheet' ) );
43
44            // Stuff for overriding content width...
45            add_action( 'customize_preview_init', array( __CLASS__, 'preview_content_width' ) );
46            add_filter( 'jetpack_content_width', array( __CLASS__, 'jetpack_content_width' ) );
47            add_filter( 'editor_max_image_size', array( __CLASS__, 'editor_max_image_size' ), 10, 3 );
48            add_action( 'template_redirect', array( __CLASS__, 'set_content_width' ) );
49            add_action( 'admin_init', array( __CLASS__, 'set_content_width' ) );
50            add_action( 'jetpack_modules_loaded', array( __CLASS__, 'custom_css_loaded' ) );
51        }
52
53        /**
54         * Things that we do on init.
55         */
56        public static function init() {
57            remove_action( 'wp_head', 'wp_custom_css_cb', 11 ); // 4.7.0 had it at 11, 4.7.1 moved it to 101.
58            remove_action( 'wp_head', 'wp_custom_css_cb', 101 );
59            add_action( 'wp_head', array( __CLASS__, 'wp_custom_css_cb' ), 101 );
60
61            if ( isset( $_GET['custom-css'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- no changes made to the site.
62                self::print_linked_custom_css();
63            }
64        }
65
66        /**
67         * Things that we do on init when the Customize Preview is loading.
68         */
69        public static function customize_preview_init() {
70            add_filter( 'wp_get_custom_css', array( __CLASS__, 'customize_preview_wp_get_custom_css' ) );
71        }
72
73        /**
74         * Print the current Custom CSS. This is for linking instead of printing directly.
75         *
76         * @return never
77         */
78        public static function print_linked_custom_css() {
79            header( 'Content-type: text/css' );
80            header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + YEAR_IN_SECONDS ) . ' GMT' );
81            echo wp_get_custom_css(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
82            exit( 0 );
83        }
84
85        /**
86         * Re-map the Edit CSS capability.
87         *
88         * Core, by default, restricts this to users that have `unfiltered_html` which
89         * would make the feature unusable in multi-site by non-super-admins, due to Core
90         * not shipping any solid sanitization.
91         *
92         * We're expanding who can use it, and then conditionally applying CSSTidy
93         * sanitization to users that do not have the `unfiltered_html` capability.
94         *
95         * @param array  $caps Returns the user's actual capabilities.
96         * @param string $cap  Capability name.
97         *
98         * @return array $caps
99         */
100        public static function map_meta_cap( $caps, $cap ) {
101            if ( 'edit_css' === $cap ) {
102                $caps = array( 'edit_theme_options' );
103            }
104            return $caps;
105        }
106
107        /**
108         * Shows Preprocessor code in the Revisions screen, and ensures that post_content_filtered
109         * is maintained on revisions
110         *
111         * @param array $fields  Post fields pertinent to revisions.
112         * @param array $post    A post array being processed for insertion as a post revision.
113         *
114         * @return array $fields Modified array to include post_content_filtered.
115         */
116        public static function wp_post_revision_fields( $fields, $post ) {
117            // None of the fields in $post are required to be passed in this filter.
118            if ( ! isset( $post['post_type'] ) || ! isset( $post['ID'] ) ) {
119                return $fields;
120            }
121
122            // If we're passed in a revision, go get the main post instead.
123            if ( 'revision' === $post['post_type'] ) {
124                $main_post_id = wp_is_post_revision( $post['ID'] );
125                $post         = get_post( $main_post_id, ARRAY_A );
126            }
127            // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
128            if ( 'custom_css' === $post['post_type'] ) {
129                $fields['post_content']          = __( 'CSS', 'jetpack-mu-wpcom' );
130                $fields['post_content_filtered'] = __( 'Preprocessor', 'jetpack-mu-wpcom' );
131            }
132            return $fields;
133        }
134
135        /**
136         * Get the published custom CSS post.
137         *
138         * @param string $stylesheet Optional. A theme object stylesheet name. Defaults to the current theme.
139         * @return WP_Post|null
140         */
141        public static function get_css_post( $stylesheet = '' ) {
142            return wp_get_custom_css_post( $stylesheet );
143        }
144
145        /**
146         * Override Core's `wp_custom_css_cb` method to provide linking to custom css.
147         */
148        public static function wp_custom_css_cb() {
149            $styles = wp_get_custom_css();
150            if ( ! $styles ) {
151                return;
152            }
153
154            $should_embed = strlen( $styles ) < 2000;
155            /** This filter is documented in projects/plugins/jetpack/modules/custom-css/custom-css.php */
156            $should_embed = apply_filters( 'safecss_embed_style', $should_embed, $styles );
157
158            if ( $should_embed || is_customize_preview() ) {
159                printf(
160                    '<style type="text/css" id="wp-custom-css">%1$s</style>',
161                    wp_strip_all_tags( $styles ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
162                );
163            } else {
164                // Add a cache buster to the url.
165                $url = home_url( '/' );
166                $url = add_query_arg( 'custom-css', substr( md5( $styles ), -10 ), $url );
167
168                printf(
169                    '<link rel="stylesheet" type="text/css" id="wp-custom-css" href="%1$s" />', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
170                    esc_url( $url )
171                );
172            }
173        }
174
175        /**
176         * Get the ID of a Custom CSS post tying to a given stylesheet.
177         *
178         * @param string $stylesheet Stylesheet name.
179         *
180         * @return int $post_id Post ID.
181         */
182        public static function post_id( $stylesheet = '' ) {
183            $post = self::get_css_post( $stylesheet );
184            if ( $post instanceof WP_Post ) {
185                return $post->ID;
186            }
187            return 0;
188        }
189
190        /**
191         * Partial for use in the Customizer.
192         */
193        public static function echo_custom_css_partial() {
194            echo wp_get_custom_css(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
195        }
196
197        /**
198         * Admin page!
199         *
200         * This currently has two main uses -- firstly to display the css for an inactive
201         * theme if there are no revisions attached it to a legacy bug, and secondly to
202         * handle folks that have bookmarkes in their browser going to the old page for
203         * managing Custom CSS in Jetpack.
204         *
205         * If we ever add back in a non-Customizer CSS editor, this would be the place.
206         */
207        public static function admin_page() {
208            $post       = null;
209            $stylesheet = null;
210            if ( isset( $_GET['id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- no changes made to the site.
211                $post_id = absint( $_GET['id'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- no changes made to the site
212                $post    = get_post( $post_id );
213                if ( $post instanceof WP_Post && 'custom_css' === $post->post_type ) {
214                    $stylesheet = $post->post_title;
215                }
216            }
217            ?>
218            <div class="wrap">
219                <?php
220                if ( is_string( $stylesheet ) ) {
221                    self::revisions_switcher_box( $stylesheet );
222                }
223                ?>
224                <h1>
225                    <?php
226                    if ( $post && is_string( $stylesheet ) ) {
227                        printf( 'Custom CSS for &#8220;%1$s&#8221;', wp_get_theme( $stylesheet )->Name ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
228                    } else {
229                        esc_html_e( 'Custom CSS', 'jetpack-mu-wpcom' );
230                    }
231                    if ( current_user_can( 'customize' ) ) {
232                        printf(
233                            ' <a class="page-title-action hide-if-no-customize" href="%1$s">%2$s</a>',
234                            esc_url( self::customizer_link() ),
235                            esc_html__( 'Manage with Live Preview', 'jetpack-mu-wpcom' )
236                        );
237                    }
238                    ?>
239                </h1>
240                <p><?php esc_html_e( 'Custom CSS is now managed in the Customizer.', 'jetpack-mu-wpcom' ); ?></p>
241                <?php if ( $post ) : ?>
242                    <div class="revisions">
243                        <h3><?php esc_html_e( 'CSS', 'jetpack-mu-wpcom' ); ?></h3>
244                        <textarea class="widefat" readonly><?php echo esc_textarea( $post->post_content ); ?></textarea>
245                        <?php if ( $post->post_content_filtered ) : ?>
246                            <h3><?php esc_html_e( 'Preprocessor', 'jetpack-mu-wpcom' ); ?></h3>
247                            <textarea class="widefat" readonly><?php echo esc_textarea( $post->post_content_filtered ); ?></textarea>
248                        <?php endif; ?>
249                    </div>
250                <?php endif; ?>
251            </div>
252
253            <style>
254                .other-themes-wrap {
255                    float: right;
256                    background-color: #fff;
257                    -webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.1);
258                    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
259                    padding: 5px 10px;
260                    margin-bottom: 10px;
261                }
262                .other-themes-wrap label {
263                    display: block;
264                    margin-bottom: 10px;
265                }
266                .other-themes-wrap select {
267                    float: left;
268                    width: 77%;
269                }
270                .other-themes-wrap button {
271                    float: right;
272                    width: 20%;
273                }
274                .revisions {
275                    clear: both;
276                }
277                .revisions textarea {
278                    min-height: 300px;
279                    background: #fff;
280                }
281            </style>
282            <script>
283                (function($){
284                    var $switcher = $('.other-themes-wrap');
285                    $switcher.find('button').on('click', function(e){
286                        e.preventDefault();
287                        if ( $switcher.find('select').val() ) {
288                            window.location.href = $switcher.find('select').val();
289                        }
290                    });
291                })(jQuery);
292            </script>
293            <?php
294        }
295
296        /**
297         * Build the URL to deep link to the Customizer.
298         *
299         * You can modify the return url via $args.
300         *
301         * @param array $args Array of parameters.
302         * @return string
303         */
304        public static function customizer_link( $args = array() ) {
305            $args = wp_parse_args(
306                $args,
307                array(
308                    'return_url' => isset( $_SERVER['REQUEST_URI'] ) ? rawurlencode( filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) : '',
309                )
310            );
311
312            return add_query_arg(
313                array(
314                    array(
315                        'autofocus' => array(
316                            // @phan-suppress-next-line PhanPluginMixedKeyNoKey
317                            'section' => 'custom_css',
318                        ),
319                    ),
320                    'return' => $args['return_url'],
321                ),
322                admin_url( 'customize.php' )
323            );
324        }
325
326        /**
327         * Handle the enqueueing and localizing for scripts to be used in the Customizer.
328         */
329        public static function customize_controls_enqueue_scripts() {
330            wp_enqueue_style( 'jetpack-customizer-css' );
331            wp_enqueue_script( 'jetpack-customizer-css' );
332
333            $content_help = __( 'Set a different media width for full size images.', 'jetpack-mu-wpcom' );
334            if ( ! empty( $GLOBALS['content_width'] ) ) {
335                $content_help .= sprintf(
336                    // translators: the theme name and then the default width.
337                    _n( ' The default media width for the <strong>%1$s</strong> theme is %2$d pixel.', ' The default media width for the <strong>%1$s</strong> theme is %2$d pixels.', (int) $GLOBALS['content_width'], 'jetpack-mu-wpcom' ),
338                    wp_get_theme()->Name,
339                    (int) $GLOBALS['content_width']
340                );
341            }
342
343            wp_localize_script(
344                'jetpack-customizer-css',
345                '_jp_css_settings',
346                array(
347                    /** This filter is documented in modules/custom-css/custom-css.php */
348                    // @phan-suppress-next-line PhanUndeclaredFunction
349                    'useRichEditor'        => ! jetpack_is_mobile() && apply_filters( 'safecss_use_ace', true ),
350                    'areThereCssRevisions' => self::are_there_css_revisions(),
351                    'revisionsUrl'         => self::get_revisions_url(),
352                    'cssHelpUrl'           => '//en.support.wordpress.com/custom-design/editing-css/',
353                    'l10n'                 => array(
354                        'mode'           => __( 'Start Fresh', 'jetpack-mu-wpcom' ),
355                        'mobile'         => __( 'On Mobile', 'jetpack-mu-wpcom' ),
356                        'contentWidth'   => $content_help,
357                        'revisions'      => _x( 'See full history', 'Toolbar button to see full CSS revision history', 'jetpack-mu-wpcom' ),
358                        'css_help_title' => _x( 'Help', 'Toolbar button to get help with custom CSS', 'jetpack-mu-wpcom' ),
359                    ),
360                )
361            );
362        }
363
364        /**
365         * Check whether there are CSS Revisions for a given theme.
366         *
367         * Going forward, there should always be, but this was necessitated
368         * early on by https://core.trac.wordpress.org/ticket/30854
369         *
370         * @param string $stylesheet Stylesheet name.
371         *
372         * @return bool|null|WP_Post
373         */
374        public static function are_there_css_revisions( $stylesheet = '' ) {
375            $post = wp_get_custom_css_post( $stylesheet );
376            if ( empty( $post ) ) {
377                return $post;
378            }
379            return (bool) wp_get_post_revisions( $post );
380        }
381
382        /**
383         * Core doesn't have a function to get the revisions url for a given post ID.
384         *
385         * @param string $stylesheet Stylesheet name.
386         *
387         * @return null|string|void
388         */
389        public static function get_revisions_url( $stylesheet = '' ) {
390            $post = wp_get_custom_css_post( $stylesheet );
391
392            // If we have any currently saved customizations...
393            if ( $post instanceof WP_Post ) {
394                $revisions = wp_get_post_revisions( $post->ID, array( 'posts_per_page' => 1 ) );
395                if ( empty( $revisions ) || is_wp_error( $revisions ) ) {
396                    return admin_url( 'themes.php?page=editcss' );
397                }
398                $revision = reset( $revisions );
399                return get_edit_post_link( $revision->ID );
400            }
401
402            return admin_url( 'themes.php?page=editcss' );
403        }
404
405        /**
406         * Get a map of all theme names and theme stylesheets for mapping stuff.
407         *
408         * @return array
409         */
410        public static function get_themes() {
411            $themes = wp_get_themes( array( 'errors' => null ) );
412            $all    = array();
413            foreach ( $themes as $theme ) {
414                $all[ $theme->name ] = $theme->stylesheet;
415            }
416            return $all;
417        }
418
419        /**
420         * When we need to get all themes that have Custom CSS saved.
421         *
422         * @return array
423         */
424        public static function get_all_themes_with_custom_css() {
425            $themes     = self::get_themes();
426            $custom_css = get_posts(
427                array(
428                    'post_type'   => 'custom_css',
429                    'post_status' => get_post_stati(),
430                    'number'      => -1,
431                    'order'       => 'DESC',
432                    'orderby'     => 'modified',
433                )
434            );
435            $return     = array();
436
437            foreach ( $custom_css as $post ) {
438                $stylesheet = $post->post_title;
439                $label      = array_search( $stylesheet, $themes, true );
440
441                if ( ! $label ) {
442                    continue;
443                }
444
445                $return[ $stylesheet ] = array(
446                    'label' => $label,
447                    'post'  => $post,
448                );
449            }
450
451            return $return;
452        }
453
454        /**
455         * Handle the registering of admin scripts
456         */
457        public static function wp_admin_enqueue_scripts() {
458            Assets::register_script(
459                'jetpack-customizer-css',
460                '../../build/core-customizer-css/core-customizer-css.js',
461                __FILE__,
462                array(
463                    'dependencies' => array(
464                        'jquery',
465                        'customize-controls',
466                    ),
467                    'in-footer'    => true,
468                    'css_path'     => '../../build/customizer-control/customizer-control.css',
469                )
470            );
471
472            Assets::register_script(
473                'jetpack-customizer-css-preview',
474                '../../build/core-customizer-css-preview/core-customizer-css-preview.js',
475                __FILE__,
476                array(
477                    'dependencies' => array(
478                        'jquery',
479                        'customize-selective-refresh',
480                    ),
481                    'in-footer'    => true,
482                )
483            );
484        }
485
486        /**
487         * Handle the enqueueing of scripts for customize previews.
488         */
489        public static function wp_enqueue_scripts() {
490            if ( is_customize_preview() ) {
491                wp_enqueue_script( 'jetpack-customizer-css-preview' );
492                wp_localize_script(
493                    'jetpack-customizer-css-preview',
494                    'jpCustomizerCssPreview',
495                    array(
496                        /** This filter is documented in modules/custom-css/custom-css.php */
497                        'preprocessors' => apply_filters( 'jetpack_custom_css_preprocessors', array() ),
498                    )
499                );
500            }
501        }
502
503        /**
504         * Sanitize the CSS for users without `unfiltered_html`.
505         *
506         * @param string $css  Input CSS.
507         * @param array  $args Array of CSS options.
508         *
509         * @return mixed|string
510         */
511        public static function sanitize_css( $css, $args = array() ) {
512            $args = wp_parse_args(
513                $args,
514                array(
515                    'force'        => false,
516                    'preprocessor' => null,
517                )
518            );
519
520            if ( $args['force'] || ! current_user_can( 'unfiltered_html' ) ) {
521
522                $warnings = array();
523
524                safecss_class();
525                $csstidy = new csstidy();
526                // @phan-suppress-next-line PhanTypeMismatchArgument
527                $csstidy->optimise = new safecss( $csstidy );
528
529                $csstidy->set_cfg( 'remove_bslash', false );
530                $csstidy->set_cfg( 'compress_colors', false );
531                $csstidy->set_cfg( 'compress_font-weight', false );
532                $csstidy->set_cfg( 'optimise_shorthands', 0 );
533                $csstidy->set_cfg( 'remove_last_;', false );
534                $csstidy->set_cfg( 'case_properties', false );
535                $csstidy->set_cfg( 'discard_invalid_properties', true );
536                $csstidy->set_cfg( 'css_level', 'CSS3.0' );
537                $csstidy->set_cfg( 'preserve_css', true );
538                $csstidy->set_cfg( 'template', __DIR__ . '/csstidy/wordpress-standard.tpl' );
539
540                // Test for some preg_replace stuff.
541                $prev = $css;
542                $css  = preg_replace( '/\\\\([0-9a-fA-F]{4})/', '\\\\\\\\$1', $css );
543                // prevent content: '\3434' from turning into '\\3434'.
544                $css = str_replace( array( '\'\\\\', '"\\\\' ), array( '\'\\', '"\\' ), $css );
545                if ( $css !== $prev ) {
546                    $warnings[] = 'preg_replace found stuff';
547                }
548
549                // Some people put weird stuff in their CSS, KSES tends to be greedy.
550                $css = str_replace( '<=', '&lt;=', $css );
551
552                // Test for some kses stuff.
553                $prev = $css;
554                // Why KSES instead of strip_tags?  Who knows?
555                $css = wp_kses_split( $css, array(), array() );
556                $css = str_replace( '&gt;', '>', $css ); // kses replaces lone '>' with &gt;
557                // Why both KSES and strip_tags?  Because we just added some '>'.
558                $css = strip_tags( $css ); // phpcs:ignore WordPress.WP.AlternativeFunctions.strip_tags_strip_tags -- scared to update this to wp_strip_all_tags since we're building a CSS file here.
559
560                if ( $css !== $prev ) {
561                    $warnings[] = 'kses found stuff';
562                }
563
564                // if we're not using a preprocessor.
565                if ( ! $args['preprocessor'] ) {
566
567                    /** This action is documented in modules/custom-css/custom-css.php */
568                    do_action( 'safecss_parse_pre', $csstidy, $css, $args );
569
570                    $csstidy->parse( $css );
571
572                    /** This action is documented in modules/custom-css/custom-css.php */
573                    do_action( 'safecss_parse_post', $csstidy, $warnings, $args );
574
575                    $css = $csstidy->print->plain();
576                }
577            }
578            return $css;
579        }
580
581        /**
582         * Override $content_width in customizer previews.
583         */
584        public static function preview_content_width() {
585            global $wp_customize;
586            if ( ! is_customize_preview() ) {
587                return;
588            }
589
590            $setting = $wp_customize->get_setting( 'jetpack_custom_css[content_width]' );
591            if ( ! $setting ) {
592                return;
593            }
594
595            $customized_content_width = (int) $setting->post_value();
596            if ( ! empty( $customized_content_width ) ) {
597                $GLOBALS['content_width'] = $customized_content_width;
598            }
599        }
600
601        /**
602         * Filter the current theme's stylesheet for potentially nullifying it.
603         *
604         * @param string $current Stylesheet URI for the current theme/child theme.
605         *
606         * @return mixed|void
607         */
608        public static function style_filter( $current ) {
609            if ( is_admin() ) {
610                return $current;
611            } elseif ( self::is_freetrial() && ( ! self::is_preview() || ! current_user_can( 'switch_themes' ) ) ) {
612                return $current;
613            } elseif ( self::skip_stylesheet() ) {
614                /** This filter is documented in modules/custom-css/custom-css.php */
615                return apply_filters( 'safecss_style_filter_url', plugins_url( 'custom-css/css/blank.css', __FILE__ ) );
616            }
617
618            return $current;
619        }
620
621        /**
622         * Determine whether or not we should have the theme skip its main stylesheet.
623         *
624         * @return mixed The truthiness of this value determines whether the stylesheet should be skipped.
625         */
626        public static function skip_stylesheet() {
627            /** This filter is documented in modules/custom-css/custom-css.php */
628            $skip_stylesheet = apply_filters( 'safecss_skip_stylesheet', null );
629            if ( $skip_stylesheet !== null ) {
630                return $skip_stylesheet;
631            }
632
633            $jetpack_custom_css = get_theme_mod( 'jetpack_custom_css', array() );
634            if ( isset( $jetpack_custom_css['replace'] ) ) {
635                return $jetpack_custom_css['replace'];
636            }
637
638            return false;
639        }
640
641        /**
642         * Override $content_width in customizer previews.
643         *
644         * Runs on `safecss_skip_stylesheet` filter.
645         *
646         * @param bool $skip_value Should the stylesheet be skipped.
647         *
648         * @return null|bool
649         */
650        public static function preview_skip_stylesheet( $skip_value ) {
651            global $wp_customize;
652            if ( ! is_customize_preview() ) {
653                return $skip_value;
654            }
655
656            $setting = $wp_customize->get_setting( 'jetpack_custom_css[replace]' );
657            if ( ! $setting ) {
658                return $skip_value;
659            }
660
661            $customized_replace = $setting->post_value();
662            if ( null !== $customized_replace ) {
663                return $customized_replace;
664            }
665
666            return $skip_value;
667        }
668
669        /**
670         * Add Custom CSS section and controls.
671         *
672         * @param WP_Customize_Manager $wp_customize WP_Customize_Manager instance.
673         */
674        public static function customize_register( $wp_customize ) {
675
676            /**
677             * SETTINGS.
678             */
679
680            $wp_customize->add_setting(
681                'jetpack_custom_css[preprocessor]',
682                array(
683                    'default'           => '',
684                    'transport'         => 'postMessage',
685                    'sanitize_callback' => array( __CLASS__, 'sanitize_preprocessor' ),
686                )
687            );
688
689            $wp_customize->add_setting(
690                'jetpack_custom_css[replace]',
691                array(
692                    'default'   => false,
693                    'transport' => 'refresh',
694                )
695            );
696
697            $wp_customize->add_setting(
698                'jetpack_custom_css[content_width]',
699                array(
700                    'default'           => '',
701                    'transport'         => 'refresh',
702                    'sanitize_callback' => array( __CLASS__, 'intval_base10' ),
703                )
704            );
705
706            // Add custom sanitization to the core css customizer setting.
707            foreach ( $wp_customize->settings() as $setting ) {
708                if ( $setting instanceof WP_Customize_Custom_CSS_Setting ) {
709                    add_filter( "customize_sanitize_{$setting->id}", array( __CLASS__, 'sanitize_css_callback' ), 10, 2 );
710                }
711            }
712
713            /**
714             * CONTROLS.
715             */
716
717            // Overwrite or Tweak the Core Control.
718            $core_custom_css = $wp_customize->get_control( 'custom_css' );
719            if ( $core_custom_css ) {
720                if ( $core_custom_css instanceof WP_Customize_Code_Editor_Control ) {
721                    // In WP 4.9, we let the Core CodeMirror control keep running the show, but hook into it to tweak stuff.
722                    $types        = array(
723                        'default' => 'text/css',
724                        'less'    => 'text/x-less',
725                        'sass'    => 'text/x-scss',
726                    );
727                    $preprocessor = $wp_customize->get_setting( 'jetpack_custom_css[preprocessor]' )->value();
728                    if ( isset( $types[ $preprocessor ] ) ) {
729                        $core_custom_css->code_type = $types[ $preprocessor ];
730                    }
731                } else {
732                    // Core < 4.9 Fallback
733                    $core_custom_css->type = 'jetpackCss';
734                }
735            }
736
737            $wp_customize->selective_refresh->add_partial(
738                'custom_css',
739                array(
740                    'type'                => 'custom_css',
741                    'selector'            => '#wp-custom-css',
742                    'container_inclusive' => false,
743                    'fallback_refresh'    => false,
744                    'settings'            => array(
745                        'custom_css[' . $wp_customize->get_stylesheet() . ']',
746                        'jetpack_custom_css[preprocessor]',
747                    ),
748                    'render_callback'     => array( __CLASS__, 'echo_custom_css_partial' ),
749                )
750            );
751
752            $wp_customize->add_control(
753                'wpcom_custom_css_content_width_control',
754                array(
755                    'type'     => 'text',
756                    'label'    => __( 'Media Width', 'jetpack-mu-wpcom' ),
757                    'section'  => 'custom_css',
758                    'settings' => 'jetpack_custom_css[content_width]',
759                )
760            );
761
762            $wp_customize->add_control(
763                'jetpack_css_mode_control',
764                array(
765                    'type'     => 'checkbox',
766                    'label'    => __( 'Don\'t use the theme\'s original CSS.', 'jetpack-mu-wpcom' ),
767                    'section'  => 'custom_css',
768                    'settings' => 'jetpack_custom_css[replace]',
769                )
770            );
771
772            /**
773             * An action to grab on to if another Jetpack Module would like to add its own controls.
774             *
775             * @module custom-css
776             *
777             * @since 4.4.2
778             *
779             * @param $wp_customize The WP_Customize object.
780             */
781            do_action( 'jetpack_custom_css_customizer_controls', $wp_customize );
782
783            /** This filter is documented in modules/custom-css/custom-css.php */
784            $preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() );
785            if ( ! empty( $preprocessors ) ) {
786                $preprocessor_choices = array(
787                    '' => __( 'None', 'jetpack-mu-wpcom' ),
788                );
789
790                foreach ( $preprocessors as $preprocessor_key => $processor ) {
791                    $preprocessor_choices[ $preprocessor_key ] = $processor['name'];
792                }
793
794                $wp_customize->add_control(
795                    'jetpack_css_preprocessors_control',
796                    array(
797                        'type'     => 'select',
798                        'choices'  => $preprocessor_choices,
799                        'label'    => __( 'Preprocessor', 'jetpack-mu-wpcom' ),
800                        'section'  => 'custom_css',
801                        'settings' => 'jetpack_custom_css[preprocessor]',
802                    )
803                );
804            }
805        }
806
807        /**
808         * The callback to handle sanitizing the CSS. Takes different arguments, hence the proxy function.
809         *
810         * @param mixed $css Value of the setting.
811         *
812         * @return mixed|string
813         */
814        public static function sanitize_css_callback( $css ) {
815            global $wp_customize;
816            return self::sanitize_css(
817                $css,
818                array(
819                    'preprocessor' => $wp_customize->get_setting( 'jetpack_custom_css[preprocessor]' )->value(),
820                )
821            );
822        }
823
824        /**
825         * Flesh out for wpcom.
826         *
827         * @todo
828         *
829         * @return bool
830         */
831        public static function is_freetrial() {
832            return false;
833        }
834
835        /**
836         * Flesh out for wpcom.
837         *
838         * @todo
839         *
840         * @return bool
841         */
842        public static function is_preview() {
843            return false;
844        }
845
846        /**
847         * Output the custom css for customize preview.
848         *
849         * @param string $css Custom CSS content.
850         *
851         * @return mixed
852         */
853        public static function customize_preview_wp_get_custom_css( $css ) {
854            global $wp_customize;
855
856            $preprocessor = $wp_customize->get_setting( 'jetpack_custom_css[preprocessor]' )->value();
857
858            // If it's empty, just return.
859            if ( empty( $preprocessor ) ) {
860                return $css;
861            }
862
863            /** This filter is documented in modules/custom-css/custom-css.php */
864            $preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() );
865            if ( isset( $preprocessors[ $preprocessor ] ) ) {
866                return call_user_func( $preprocessors[ $preprocessor ]['callback'], $css );
867            }
868
869            return $css;
870        }
871
872        /**
873         * Add CSS preprocessing to our CSS if it is supported.
874         *
875         * @param mixed                $css     Value of the setting.
876         * @param WP_Customize_Setting $setting WP_Customize_Setting instance.
877         *
878         * @return string
879         */
880        public static function customize_value_custom_css( $css, $setting ) {
881            // Find the current preprocessor.
882            $preprocessor       = null;
883            $jetpack_custom_css = get_theme_mod( 'jetpack_custom_css', array() );
884            if ( isset( $jetpack_custom_css['preprocessor'] ) ) {
885                $preprocessor = $jetpack_custom_css['preprocessor'];
886            }
887
888            // If it's not supported, just return.
889            /** This filter is documented in modules/custom-css/custom-css.php */
890            $preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() );
891            if ( ! isset( $preprocessors[ $preprocessor ] ) ) {
892                return $css;
893            }
894
895            // Swap it for the `post_content_filtered` instead.
896            $post = wp_get_custom_css_post( $setting->stylesheet );
897            if ( $post && ! empty( $post->post_content_filtered ) ) {
898                $css = $post->post_content_filtered;
899            }
900
901            return $css;
902        }
903
904        /**
905         * Store the original pre-processed CSS in `post_content_filtered`
906         * and then store processed CSS in `post_content`.
907         *
908         * @param array  $args Content post args.
909         * @param string $css  Original CSS being updated.
910         *
911         * @return mixed
912         */
913        public static function customize_update_custom_css_post_content_args( $args, $css ) {
914            // Find the current preprocessor.
915            $jetpack_custom_css = get_theme_mod( 'jetpack_custom_css', array() );
916            if ( empty( $jetpack_custom_css['preprocessor'] ) ) {
917                return $args;
918            }
919
920            $preprocessor = $jetpack_custom_css['preprocessor'];
921            /** This filter is documented in modules/custom-css/custom-css.php */
922            $preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() );
923
924            // If it's empty, just return.
925            if ( empty( $preprocessor ) ) {
926                return $args;
927            }
928
929            if ( isset( $preprocessors[ $preprocessor ] ) ) {
930                $args['post_content_filtered'] = $css;
931                $args['post_content']          = call_user_func( $preprocessors[ $preprocessor ]['callback'], $css );
932            }
933
934            return $args;
935        }
936
937        /**
938         * Filter to handle the processing of preprocessed css on save.
939         *
940         * @param array $args Custom CSS options.
941         *
942         * @return mixed
943         */
944        public static function update_custom_css_data( $args ) {
945            // Find the current preprocessor.
946            $jetpack_custom_css = get_theme_mod( 'jetpack_custom_css', array() );
947            if ( empty( $jetpack_custom_css['preprocessor'] ) ) {
948                return $args;
949            }
950
951            /** This filter is documented in modules/custom-css/custom-css.php */
952            $preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() );
953            $preprocessor  = $jetpack_custom_css['preprocessor'];
954
955            // If we have a preprocessor specified ...
956            if ( isset( $preprocessors[ $preprocessor ] ) ) {
957                // And no other preprocessor has run ...
958                if ( empty( $args['preprocessed'] ) ) {
959                    $args['preprocessed'] = $args['css'];
960                    $args['css']          = call_user_func( $preprocessors[ $preprocessor ]['callback'], $args['css'] );
961                } else {
962                    trigger_error( 'Jetpack CSS Preprocessor specified, but something else has already modified the argument.', E_USER_WARNING ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
963                }
964            }
965
966            return $args;
967        }
968
969        /**
970         * When on the edit screen, make sure the custom content width
971         * setting is applied to the large image size.
972         *
973         * @param array  $dims    Array of image dimensions (width and height).
974         * @param string $size    Size of the resulting image.
975         * @param null   $context Context the image is being resized for. `edit` or `display`.
976         *
977         * @return array
978         */
979        public static function editor_max_image_size( $dims, $size = 'medium', $context = null ) {
980            list( $width, $height ) = $dims;
981
982            if ( class_exists( 'Jetpack' ) && 'large' === $size && 'edit' === $context ) {
983                // @phan-suppress-next-line PhanUndeclaredClassMethod
984                $width = Jetpack::get_content_width();
985            }
986
987            return array( $width, $height );
988        }
989
990        /**
991         * Override the content_width with a custom value if one is set.
992         *
993         * @param int $content_width Content Width value to be updated.
994         *
995         * @return int
996         */
997        public static function jetpack_content_width( $content_width ) {
998            $custom_content_width = 0;
999
1000            $jetpack_custom_css = get_theme_mod( 'jetpack_custom_css', array() );
1001            if ( isset( $jetpack_custom_css['content_width'] ) ) {
1002                $custom_content_width = $jetpack_custom_css['content_width'];
1003            }
1004
1005            if ( $custom_content_width > 0 ) {
1006                return $custom_content_width;
1007            }
1008
1009            return $content_width;
1010        }
1011
1012        /**
1013         * Currently this filter function gets called on
1014         * 'template_redirect' action and
1015         * 'admin_init' action
1016         */
1017        public static function set_content_width() {
1018            // Don't apply this filter on the Edit CSS page.
1019            if ( isset( $_GET['page'] ) && 'editcss' === $_GET['page'] && is_admin() ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- nothing is changed on the site.
1020                return;
1021            }
1022
1023            if ( class_exists( 'Jetpack' ) ) {
1024                // @phan-suppress-next-line PhanUndeclaredClassMethod
1025                $GLOBALS['content_width'] = Jetpack::get_content_width();
1026            }
1027        }
1028
1029        /**
1030         * Make sure the preprocessor we're saving is one we know about.
1031         *
1032         * @param string $preprocessor The preprocessor to sanitize.
1033         *
1034         * @return null|string
1035         */
1036        public static function sanitize_preprocessor( $preprocessor ) {
1037            /** This filter is documented in modules/custom-css/custom-css.php */
1038            $preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() );
1039            if ( empty( $preprocessor ) || array_key_exists( $preprocessor, $preprocessors ) ) {
1040                return $preprocessor;
1041            }
1042            return null;
1043        }
1044
1045        /**
1046         * Get the base10 intval.
1047         *
1048         * This is used as a setting's sanitize_callback; we can't use just plain
1049         * intval because the second argument is not what intval() expects.
1050         *
1051         * @access public
1052         *
1053         * @param mixed $value Number to convert.
1054         * @return int Integer.
1055         */
1056        public static function intval_base10( $value ) {
1057            return (int) $value;
1058        }
1059
1060        /**
1061         * Add a footer action on revision.php to print some customizations for the theme switcher.
1062         */
1063        public static function load_revision_php() {
1064            add_action( 'admin_footer', array( __CLASS__, 'revision_admin_footer' ) );
1065        }
1066
1067        /**
1068         * Enable CSS module.
1069         */
1070        public static function custom_css_loaded() {
1071            if ( class_exists( 'Jetpack' ) ) {
1072                // @phan-suppress-next-line PhanUndeclaredClassMethod
1073                Jetpack::enable_module_configurable( __FILE__ );
1074            }
1075            add_filter( 'jetpack_module_configuration_url_custom-css', array( __CLASS__, 'jetpack_custom_css_configuration_url' ) );
1076        }
1077
1078        /**
1079         * Overrides default configuration url
1080         *
1081         * @uses admin_url
1082         *
1083         * @param string $default_url - the default URL.
1084         * @return string module settings URL
1085         */
1086        public static function jetpack_custom_css_configuration_url( $default_url ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
1087            return self::customizer_link(
1088                array( 'return_url' => wp_get_referer() )
1089            );
1090        }
1091
1092        /**
1093         * Print the theme switcher on revision.php and move it into place.
1094         */
1095        public static function revision_admin_footer() {
1096            $post = get_post();
1097            if ( 'custom_css' !== $post->post_type ) {
1098                return;
1099            }
1100            $stylesheet = $post->post_title;
1101            ?>
1102    <script type="text/html" id="tmpl-other-themes-switcher">
1103            <?php self::revisions_switcher_box( $stylesheet ); ?>
1104    </script>
1105    <style>
1106    .other-themes-wrap {
1107        float: right;
1108        background-color: #fff;
1109        -webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.1);
1110        box-shadow: 0 1px 3px rgba(0,0,0,0.1);
1111        padding: 5px 10px;
1112        margin-bottom: 10px;
1113    }
1114    .other-themes-wrap label {
1115        display: block;
1116        margin-bottom: 10px;
1117    }
1118    .other-themes-wrap select {
1119        float: left;
1120        width: 77%;
1121    }
1122    .other-themes-wrap button {
1123        float: right;
1124        width: 20%;
1125    }
1126    .revisions {
1127        clear: both;
1128    }
1129    /* Hide the back-to-post link */
1130    .long-header + a {
1131        display: none;
1132    }
1133    </style>
1134    <script>
1135    (function($){
1136        var switcher = $('#tmpl-other-themes-switcher').html(),
1137            qty = $( switcher ).find('select option').length,
1138            $switcher;
1139
1140        if ( qty >= 3 ) {
1141            $('h1.long-header').before( switcher );
1142            $switcher = $('.other-themes-wrap');
1143            $switcher.find('button').on('click', function(e){
1144                e.preventDefault();
1145                if ( $switcher.find('select').val() ) {
1146                    window.location.href = $switcher.find('select').val();
1147                }
1148            })
1149        }
1150    })(jQuery);
1151    </script>
1152            <?php
1153        }
1154
1155        /**
1156         * The HTML for the theme revision switcher box.
1157         *
1158         * @param string $stylesheet Stylesheet name.
1159         */
1160        public static function revisions_switcher_box( $stylesheet = '' ) {
1161            $themes = self::get_all_themes_with_custom_css();
1162            ?>
1163            <div class="other-themes-wrap">
1164                <label for="other-themes"><?php esc_html_e( 'Select another theme to view its custom CSS.', 'jetpack-mu-wpcom' ); ?></label>
1165                <select id="other-themes">
1166                    <option value=""><?php esc_html_e( 'Select a theme&hellip;', 'jetpack-mu-wpcom' ); ?></option>
1167                    <?php
1168                    foreach ( $themes as $theme_stylesheet => $data ) {
1169                        $revisions = wp_get_post_revisions( $data['post']->ID, array( 'posts_per_page' => 1 ) );
1170                        if ( ! $revisions ) {
1171                            ?>
1172                            <option value="<?php echo esc_url( add_query_arg( 'id', $data['post']->ID, menu_page_url( 'editcss', false ) ) ); ?><?php disabled( $stylesheet, $theme_stylesheet ); ?>>
1173                                <?php echo esc_html( $data['label'] ); ?>
1174                                <?php
1175                                    // translators: how long ago the stylesheet was modified.
1176                                    printf( esc_html__( '(modified %s ago)', 'jetpack-mu-wpcom' ), human_time_diff( strtotime( $data['post']->post_modified_gmt ) ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1177                                ?>
1178                            </option>
1179                            <?php
1180                            continue;
1181                        }
1182                        $revision = array_shift( $revisions );
1183                        ?>
1184                        <option value="<?php echo esc_url( get_edit_post_link( $revision->ID ) ); ?><?php disabled( $stylesheet, $theme_stylesheet ); ?>>
1185                            <?php echo esc_html( $data['label'] ); ?>
1186                            <?php
1187                                // translators: how long ago the stylesheet was modified.
1188                                printf( esc_html__( '(modified %s ago)', 'jetpack-mu-wpcom' ), human_time_diff( strtotime( $data['post']->post_modified_gmt ) ) );  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1189                            ?>
1190                        </option>
1191                        <?php
1192                    }
1193                    ?>
1194                </select>
1195                <button class="button" id="other_theme_custom_css_switcher"><?php esc_html_e( 'Switch', 'jetpack-mu-wpcom' ); ?></button>
1196            </div>
1197            <?php
1198        }
1199    }
1200}
1201
1202Jetpack_Custom_CSS_Enhancements::add_hooks();
1203
1204if ( ! function_exists( 'safecss_class' ) ) :
1205    /**
1206     * Load in the class only when needed.  Makes lighter load by having one less class in memory.
1207     */
1208    function safecss_class() {
1209        // Wrapped so we don't need the parent class just to load the plugin.
1210        if ( class_exists( 'safecss' ) ) {
1211            return;
1212        }
1213
1214        require_once __DIR__ . '/csstidy/class.csstidy.php';
1215
1216        /**
1217         * Class safecss
1218         */
1219        class safecss extends csstidy_optimise { // phpcs:ignore
1220
1221            /**
1222             * Optimises $css after parsing.
1223             */
1224            public function postparse() {
1225
1226                /** This action is documented in modules/custom-css/custom-css.php */
1227                do_action( 'csstidy_optimize_postparse', $this );
1228
1229                return parent::postparse();
1230            }
1231
1232            /**
1233             * Optimises a sub-value.
1234             */
1235            public function subvalue() {
1236
1237                /** This action is documented in modules/custom-css/custom-css.php */
1238                do_action( 'csstidy_optimize_subvalue', $this );
1239
1240                return parent::subvalue();
1241            }
1242        }
1243    }
1244endif;