PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 10.4
Jetpack – WP Security, Backup, Speed, & Growth v10.4
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 in Jetpack – WP Security, Backup, Speed, & Growth 10.4, at modules/custom-css/custom-css-4.7.php

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