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