Jetpack_Custom_CSS_Enhancements

Class Jetpack_Custom_CSS_Enhancements.

Defined (1)

The class is defined in the following location(s).

/modules/custom-css/custom-css-4.7.php  
  1. class Jetpack_Custom_CSS_Enhancements { 
  2. /** 
  3. * Set up the actions and filters needed for our compatability layer on top of core's Custom CSS implementation. 
  4. */ 
  5. public static function add_hooks() { 
  6. add_action( 'init', array( __CLASS__, 'init' ) ); 
  7. add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ) ); 
  8. add_action( 'customize_controls_enqueue_scripts', array( __CLASS__, 'customize_controls_enqueue_scripts' ) ); 
  9. add_action( 'customize_register', array( __CLASS__, 'customize_register' ) ); 
  10. add_filter( 'map_meta_cap', array( __CLASS__, 'map_meta_cap' ), 20, 2 ); 
  11. add_action( 'customize_preview_init', array( __CLASS__, 'customize_preview_init' ) ); 
  12. add_filter( '_wp_post_revision_fields', array( __CLASS__, '_wp_post_revision_fields' ), 10, 2 ); 
  13. add_action( 'load-revision.php', array( __CLASS__, 'load_revision_php' ) ); 
  14.  
  15. add_action( 'wp_enqueue_scripts', array( __CLASS__, 'wp_enqueue_scripts' ) ); 
  16.  
  17. // Handle Sass/LESS. 
  18. add_filter( 'customize_value_custom_css', array( __CLASS__, 'customize_value_custom_css' ), 10, 2 ); 
  19. add_filter( 'customize_update_custom_css_post_content_args', array( __CLASS__, 'customize_update_custom_css_post_content_args' ), 10, 3 ); 
  20. add_filter( 'update_custom_css_data', array( __CLASS__, 'update_custom_css_data' ), 10, 2 ); 
  21.  
  22. // Handle Sass/LESS. 
  23. add_filter( 'customize_value_custom_css', array( __CLASS__, 'customize_value_custom_css' ), 10, 2 ); 
  24. add_filter( 'customize_update_custom_css_post_content_args', array( __CLASS__, 'customize_update_custom_css_post_content_args' ), 10, 3 ); 
  25.  
  26. // Stuff for stripping out the theme's default stylesheet... 
  27. add_filter( 'stylesheet_uri', array( __CLASS__, 'style_filter' ) ); 
  28. add_filter( 'safecss_skip_stylesheet', array( __CLASS__, 'preview_skip_stylesheet' ) ); 
  29.  
  30. // Stuff for overriding content width... 
  31. add_action( 'customize_preview_init', array( __CLASS__, 'preview_content_width' ) ); 
  32. add_filter( 'jetpack_content_width', array( __CLASS__, 'jetpack_content_width' ) ); 
  33. add_filter( 'editor_max_image_size', array( __CLASS__, 'editor_max_image_size' ), 10, 3 ); 
  34. add_action( 'template_redirect', array( __CLASS__, 'set_content_width' ) ); 
  35. add_action( 'admin_init', array( __CLASS__, 'set_content_width' ) ); 
  36.  
  37. // Stuff? 
  38.  
  39. /** 
  40. * Things that we do on init. 
  41. */ 
  42. public static function init() { 
  43. $min = '.min'; 
  44. if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { 
  45. $min = ''; 
  46.  
  47. wp_register_style( 'jetpack-codemirror', plugins_url( 'custom-css/css/codemirror.css', __FILE__ ), array(), '20120905' ); 
  48. wp_register_style( 'jetpack-customizer-css', plugins_url( 'custom-css/css/customizer-control.css', __FILE__ ), array( 'jetpack-codemirror' ), '20140728' ); 
  49. wp_register_script( 'jetpack-codemirror', plugins_url( 'custom-css/js/codemirror.min.js', __FILE__ ), array(), '3.16', true ); 
  50. wp_register_script( 'jetpack-customizer-css', plugins_url( 'custom-css/js/core-customizer-css.js', __FILE__ ), array( 'customize-controls', 'underscore', 'jetpack-codemirror' ), JETPACK__VERSION, true ); 
  51.  
  52. wp_register_script( 'jetpack-customizer-css-preview', plugins_url( 'custom-css/js/core-customizer-css-preview.js', __FILE__ ), array( 'customize-selective-refresh' ), JETPACK__VERSION, true ); 
  53.  
  54. remove_action( 'wp_head', 'wp_custom_css_cb', 11 ); // 4.7.0 had it at 11, 4.7.1 moved it to 101. 
  55. remove_action( 'wp_head', 'wp_custom_css_cb', 101 ); 
  56. add_action( 'wp_head', array( __CLASS__, 'wp_custom_css_cb' ), 101 ); 
  57.  
  58. if ( isset( $_GET['custom-css'] ) ) { 
  59. self::print_linked_custom_css(); 
  60.  
  61. /** 
  62. * Things that we do on init when the Customize Preview is loading. 
  63. */ 
  64. public static function customize_preview_init() { 
  65. add_filter( 'wp_get_custom_css', array( __CLASS__, 'customize_preview_wp_get_custom_css' ) ); 
  66.  
  67. /** 
  68. * Print the current Custom CSS. This is for linking instead of printing directly. 
  69. */ 
  70. public static function print_linked_custom_css() { 
  71. header( 'Content-type: text/css' ); 
  72. header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + YEAR_IN_SECONDS ) . ' GMT' ); 
  73. echo wp_get_custom_css(); 
  74. exit; 
  75.  
  76. /** 
  77. * Re-map the Edit CSS capability. 
  78. * Core, by default, restricts this to users that have `unfiltered_html` which 
  79. * would make the feature unusable in multi-site by non-super-admins, due to Core 
  80. * not shipping any solid sanitization. 
  81. * We're expanding who can use it, and then conditionally applying CSSTidy 
  82. * sanitization to users that do not have the `unfiltered_html` capability. 
  83. * @param array $caps Returns the user's actual capabilities. 
  84. * @param string $cap Capability name. 
  85. * @return array $caps 
  86. */ 
  87. public static function map_meta_cap( $caps, $cap ) { 
  88. if ( 'edit_css' === $cap ) { 
  89. $caps = array( 'edit_theme_options' ); 
  90. return $caps; 
  91.  
  92. /** 
  93. * Handle our admin menu item and legacy page declaration. 
  94. */ 
  95. public static function admin_menu() { 
  96. // Add in our legacy page to support old bookmarks and such. 
  97. add_submenu_page( null, __( 'CSS', 'jetpack' ), __( 'Edit CSS', 'jetpack' ), 'edit_theme_options', 'editcss', array( __CLASS__, 'admin_page' ) ); 
  98.  
  99. // Add in our new page slug that will redirect to the customizer. 
  100. $hook = add_theme_page( __( 'CSS', 'jetpack' ), __( 'Edit CSS', 'jetpack' ), 'edit_theme_options', 'editcss-customizer-redirect', array( __CLASS__, 'admin_page' ) ); 
  101. add_action( "load-{$hook}", array( __CLASS__, 'customizer_redirect' ) ); 
  102.  
  103. /** 
  104. * Handle the redirect for the customizer. This is necessary because 
  105. * we can't directly add customizer links to the admin menu. 
  106. * There is a core patch in trac that would make this unnecessary. 
  107. * @link https://core.trac.wordpress.org/ticket/39050 
  108. */ 
  109. public static function customizer_redirect() { 
  110. wp_safe_redirect( self::customizer_link( array( 
  111. 'return_url' => wp_get_referer(),  
  112. ) ) ); 
  113.  
  114. /** 
  115. * Shows Preprocessor code in the Revisions screen, and ensures that post_content_filtered 
  116. * is maintained on revisions 
  117. * @param array $fields Post fields pertinent to revisions. 
  118. * @param array $post A post array being processed for insertion as a post revision. 
  119. * @return array $fields Modified array to include post_content_filtered. 
  120. */ 
  121. public static function _wp_post_revision_fields( $fields, $post ) { 
  122. // None of the fields in $post are required to be passed in this filter. 
  123. if ( ! isset( $post['post_type'], $post['ID'] ) ) { 
  124. return $fields; 
  125.  
  126. // If we're passed in a revision, go get the main post instead. 
  127. if ( 'revision' === $post['post_type'] ) { 
  128. $main_post_id = wp_is_post_revision( $post['ID'] ); 
  129. $post = get_post( $main_post_id, ARRAY_A ); 
  130. if ( 'custom_css' === $post['post_type'] ) { 
  131. $fields['post_content'] = __( 'CSS', 'jetpack' ); 
  132. $fields['post_content_filtered'] = __( 'Preprocessor', 'jetpack' ); 
  133. return $fields; 
  134.  
  135. /** 
  136. * Get the published custom CSS post. 
  137. * @param string $stylesheet Optional. A theme object stylesheet name. Defaults to the current theme. 
  138. * @return WP_Post|null 
  139. */ 
  140. public static function get_css_post( $stylesheet = '' ) { 
  141. return wp_get_custom_css_post( $stylesheet ); 
  142.  
  143. /** 
  144. * Override Core's `wp_custom_css_cb` method to provide linking to custom css. 
  145. */ 
  146. public static function wp_custom_css_cb() { 
  147. $styles = wp_get_custom_css(); 
  148. if ( strlen( $styles ) > 2000 && ! is_customize_preview() ) : 
  149. // Add a cache buster to the url. 
  150. $url = home_url( '/' ); 
  151. $url = add_query_arg( 'custom-css', substr( md5( $styles ), -10 ), $url ); 
  152. ?> 
  153. <link rel="stylesheet" type="text/css" id="wp-custom-css" href="<?php echo esc_url( $url ); ?>" /> 
  154. <?php elseif ( $styles || is_customize_preview() ) : ?> 
  155. <style type="text/css" id="wp-custom-css"> 
  156. <?php echo strip_tags( $styles ); // Note that esc_html() cannot be used because `div > span` is not interpreted properly. ?> 
  157. </style> 
  158. <?php endif; 
  159.  
  160. /** 
  161. * Get the ID of a Custom CSS post tying to a given stylesheet. 
  162. * @param string $stylesheet Stylesheet name. 
  163. * @return int $post_id Post ID. 
  164. */ 
  165. public static function post_id( $stylesheet = '' ) { 
  166. $post = self::get_css_post( $stylesheet ); 
  167. if ( $post instanceof WP_Post ) { 
  168. return $post->ID; 
  169. return 0; 
  170.  
  171. /** 
  172. * Partial for use in the Customizer. 
  173. */ 
  174. public static function echo_custom_css_partial() { 
  175. echo wp_get_custom_css(); 
  176.  
  177. /** 
  178. * Admin page! 
  179. * This currently has two main uses -- firstly to display the css for an inactive 
  180. * theme if there are no revisions attached it to a legacy bug, and secondly to 
  181. * handle folks that have bookmarkes in their browser going to the old page for 
  182. * managing Custom CSS in Jetpack. 
  183. * If we ever add back in a non-Customizer CSS editor, this would be the place. 
  184. */ 
  185. public static function admin_page() { 
  186. $post = null; 
  187. $stylesheet = null; 
  188. if ( isset( $_GET['id'] ) ) { 
  189. $post_id = absint( $_GET['id'] ); 
  190. $post = get_post( $post_id ); 
  191. if ( $post instanceof WP_Post && 'custom_css' === $post->post_type ) { 
  192. $stylesheet = $post->post_title; 
  193. ?> 
  194. <div class="wrap"> 
  195. <?php self::revisions_switcher_box( $stylesheet ); ?> 
  196. <h1> 
  197. <?php 
  198. if ( $post ) { 
  199. printf( 'Custom CSS for “%1$s”', wp_get_theme( $stylesheet )->Name ); 
  200. } else { 
  201. esc_html_e( 'Custom CSS', 'jetpack' ); 
  202. if ( current_user_can( 'customize' ) ) { 
  203. printf( 
  204. ' <a class="page-title-action hide-if-no-customize" href="%1$s">%2$s</a>',  
  205. esc_url( self::customizer_link() ),  
  206. esc_html__( 'Manage with Live Preview', 'jetpack' ) 
  207. ); 
  208. ?> 
  209. </h1> 
  210. <p><?php esc_html_e( 'Custom CSS is now managed in the Customizer.', 'jetpack' ); ?></p> 
  211. <?php if ( $post ) : ?> 
  212. <div class="revisions"> 
  213. <h3><?php esc_html_e( 'CSS', 'jetpack' ); ?></h3> 
  214. <textarea class="widefat" readonly><?php echo esc_textarea( $post->post_content ); ?></textarea> 
  215. <?php if ( $post->post_content_filtered ) : ?> 
  216. <h3><?php esc_html_e( 'Preprocessor', 'jetpack' ); ?></h3> 
  217. <textarea class="widefat" readonly><?php echo esc_textarea( $post->post_content_filtered ); ?></textarea> 
  218. <?php endif; ?> 
  219. </div> 
  220. <?php endif; ?> 
  221. </div> 
  222.  
  223. <style> 
  224. .other-themes-wrap { 
  225. float: right; 
  226. background-color: #fff; 
  227. -webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1); 
  228. box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1); 
  229. padding: 5px 10px; 
  230. margin-bottom: 10px; 
  231. .other-themes-wrap label { 
  232. display: block; 
  233. margin-bottom: 10px; 
  234. .other-themes-wrap select { 
  235. float: left; 
  236. width: 77%; 
  237. .other-themes-wrap button { 
  238. float: right; 
  239. width: 20%; 
  240. .revisions { 
  241. clear: both; 
  242. .revisions textarea { 
  243. min-height: 300px; 
  244. background: #fff; 
  245. </style> 
  246. <script> 
  247. (function($) { 
  248. var $switcher = $('.other-themes-wrap'); 
  249. $switcher.find('button').on('click', function(e) { 
  250. e.preventDefault(); 
  251. if ( $switcher.find('select').val() ) { 
  252. window.location.href = $switcher.find('select').val(); 
  253. }); 
  254. })(jQuery); 
  255. </script> 
  256. <?php 
  257.  
  258. /** 
  259. * Build the URL to deep link to the Customizer. 
  260. * You can modify the return url via $args. 
  261. * @param array $args Array of parameters. 
  262. * @return string 
  263. */ 
  264. public static function customizer_link( $args = array() ) { 
  265. $args = wp_parse_args( $args, array( 
  266. 'return_url' => urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ),  
  267. ) ); 
  268.  
  269. return add_query_arg( 
  270. array( 
  271. array( 
  272. 'autofocus' => array( 
  273. 'section' => 'custom_css',  
  274. ),  
  275. ),  
  276. 'return' => $args['return_url'],  
  277. ),  
  278. admin_url( 'customize.php' ) 
  279. ); 
  280.  
  281. /** 
  282. * Handle the enqueueing and localizing for scripts to be used in the Customizer. 
  283. */ 
  284. public static function customize_controls_enqueue_scripts() { 
  285. wp_enqueue_style( 'jetpack-customizer-css' ); 
  286. wp_enqueue_script( 'jetpack-customizer-css' ); 
  287.  
  288. $content_help = __( 'Set a different content width for full size images.', 'jetpack' ); 
  289. if ( ! empty( $GLOBALS['content_width'] ) ) { 
  290. $content_help .= sprintf( 
  291. _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.', intval( $GLOBALS['content_width'] ), 'jetpack' ),  
  292. wp_get_theme()->Name,  
  293. intval( $GLOBALS['content_width'] ) 
  294. ); 
  295.  
  296. wp_localize_script( 'jetpack-customizer-css', '_jp_css_settings', array( 
  297. /** This filter is documented in modules/custom-css/custom-css.php */ 
  298. 'useRichEditor' => ! jetpack_is_mobile() && apply_filters( 'safecss_use_ace', true ),  
  299. 'areThereCssRevisions' => self::are_there_css_revisions(),  
  300. 'revisionsUrl' => self::get_revisions_url(),  
  301. 'cssHelpUrl' => '//en.support.wordpress.com/custom-design/editing-css/',  
  302. 'l10n' => array( 
  303. 'mode' => __( 'Start Fresh', 'jetpack' ),  
  304. 'mobile' => __( 'On Mobile', 'jetpack' ),  
  305. 'contentWidth' => $content_help,  
  306. 'revisions' => _x( 'See full history', 'Toolbar button to see full CSS revision history', 'jetpack' ),  
  307. 'css_help_title' => _x( 'Help', 'Toolbar button to get help with custom CSS', 'jetpack' ),  
  308. ),  
  309. )); 
  310.  
  311. /** 
  312. * Check whether there are CSS Revisions for a given theme. 
  313. * Going forward, there should always be, but this was necessitated 
  314. * early on by https://core.trac.wordpress.org/ticket/30854 
  315. * @param string $stylesheet Stylesheet name. 
  316. * @return bool|null|WP_Post 
  317. */ 
  318. public static function are_there_css_revisions( $stylesheet = '' ) { 
  319. $post = wp_get_custom_css_post( $stylesheet ); 
  320. if ( empty( $post ) ) { 
  321. return $post; 
  322. return (bool) wp_get_post_revisions( $post ); 
  323.  
  324. /** 
  325. * Core doesn't have a function to get the revisions url for a given post ID. 
  326. * @param string $stylesheet Stylesheet name. 
  327. * @return null|string|void 
  328. */ 
  329. public static function get_revisions_url( $stylesheet = '' ) { 
  330. $post = wp_get_custom_css_post( $stylesheet ); 
  331.  
  332. // If we have any currently saved customizations... 
  333. if ( $post instanceof WP_Post ) { 
  334. $revisions = wp_get_post_revisions( $post->ID, array( 'posts_per_page' => 1 ) ); 
  335. if ( empty( $revisions ) || is_wp_error( $revisions ) ) { 
  336. return admin_url( 'themes.php?page=editcss' ); 
  337. $revision = reset( $revisions ); 
  338. return get_edit_post_link( $revision->ID ); 
  339.  
  340. return admin_url( 'themes.php?page=editcss' ); 
  341.  
  342. /** 
  343. * Get a map of all theme names and theme stylesheets for mapping stuff. 
  344. * @return array 
  345. */ 
  346. public static function get_themes() { 
  347. $themes = wp_get_themes( array( 'errors' => null ) ); 
  348. $all = array(); 
  349. foreach ( $themes as $theme ) { 
  350. $all[ $theme->name ] = $theme->stylesheet; 
  351. return $all; 
  352.  
  353. /** 
  354. * When we need to get all themes that have Custom CSS saved. 
  355. * @return array 
  356. */ 
  357. public static function get_all_themes_with_custom_css() { 
  358. $themes = self::get_themes(); 
  359. $custom_css = get_posts( array( 
  360. 'post_type' => 'custom_css',  
  361. 'post_status' => get_post_stati(),  
  362. 'number' => -1,  
  363. 'order' => 'DESC',  
  364. 'orderby' => 'modified',  
  365. ) ); 
  366. $return = array(); 
  367.  
  368. foreach ( $custom_css as $post ) { 
  369. $stylesheet = $post->post_title; 
  370. $label = array_search( $stylesheet, $themes ); 
  371.  
  372. if ( ! $label ) { 
  373. continue; 
  374.  
  375. $return[ $stylesheet ] = array( 
  376. 'label' => $label,  
  377. 'post' => $post,  
  378. ); 
  379.  
  380. return $return; 
  381.  
  382. /** 
  383. * Handle the enqueueing of scripts for customize previews. 
  384. */ 
  385. public static function wp_enqueue_scripts() { 
  386. if ( is_customize_preview() ) { 
  387. wp_enqueue_script( 'jetpack-customizer-css-preview' ); 
  388. wp_localize_script( 'jetpack-customizer-css-preview', 'jpCustomizerCssPreview', array( 
  389. /** This filter is documented in modules/custom-css/custom-css.php */ 
  390. 'preprocessors' => apply_filters( 'jetpack_custom_css_preprocessors', array() ),  
  391. )); 
  392.  
  393. /** 
  394. * Sanitize the CSS for users without `unfiltered_html`. 
  395. * @param string $css Input CSS. 
  396. * @param array $args Array of CSS options. 
  397. * @return mixed|string 
  398. */ 
  399. public static function sanitize_css( $css, $args = array() ) { 
  400. $args = wp_parse_args( $args, array( 
  401. 'force' => false,  
  402. 'preprocessor' => null,  
  403. ) ); 
  404.  
  405. if ( $args['force'] || ! current_user_can( 'unfiltered_html' ) ) { 
  406.  
  407. $warnings = array(); 
  408.  
  409. safecss_class(); 
  410. $csstidy = new csstidy(); 
  411. $csstidy->optimise = new safecss( $csstidy ); 
  412.  
  413. $csstidy->set_cfg( 'remove_bslash', false ); 
  414. $csstidy->set_cfg( 'compress_colors', false ); 
  415. $csstidy->set_cfg( 'compress_font-weight', false ); 
  416. $csstidy->set_cfg( 'optimise_shorthands', 0 ); 
  417. $csstidy->set_cfg( 'remove_last_;', false ); 
  418. $csstidy->set_cfg( 'case_properties', false ); 
  419. $csstidy->set_cfg( 'discard_invalid_properties', true ); 
  420. $csstidy->set_cfg( 'css_level', 'CSS3.0' ); 
  421. $csstidy->set_cfg( 'preserve_css', true ); 
  422. $csstidy->set_cfg( 'template', dirname( __FILE__ ) . '/csstidy/wordpress-standard.tpl' ); 
  423.  
  424. // Test for some preg_replace stuff. 
  425. $prev = $css; 
  426. $css = preg_replace( '/\\\\([0-9a-fA-F]{4})/', '\\\\\\\\$1', $css ); 
  427. // prevent content: '\3434' from turning into '\\3434'. 
  428. $css = str_replace( array( '\'\\\\', '"\\\\' ), array( '\'\\', '"\\' ), $css ); 
  429. if ( $css !== $prev ) { 
  430. $warnings[] = 'preg_replace found stuff'; 
  431.  
  432. // Some people put weird stuff in their CSS, KSES tends to be greedy. 
  433. $css = str_replace( '<=', '<=', $css ); 
  434.  
  435. // Test for some kses stuff. 
  436. $prev = $css; 
  437. // Why KSES instead of strip_tags? Who knows? 
  438. $css = wp_kses_split( $css, array(), array() ); 
  439. $css = str_replace( '>', '>', $css ); // kses replaces lone '>' with > 
  440. // Why both KSES and strip_tags? Because we just added some '>'. 
  441. $css = strip_tags( $css ); 
  442.  
  443. if ( $css != $prev ) { 
  444. $warnings[] = 'kses found stuff'; 
  445.  
  446. // if we're not using a preprocessor. 
  447. if ( ! $args['preprocessor'] ) { 
  448.  
  449. /** This action is documented in modules/custom-css/custom-css.php */ 
  450. do_action( 'safecss_parse_pre', $csstidy, $css, $args ); 
  451.  
  452. $csstidy->parse( $css ); 
  453.  
  454. /** This action is documented in modules/custom-css/custom-css.php */ 
  455. do_action( 'safecss_parse_post', $csstidy, $warnings, $args ); 
  456.  
  457. $css = $csstidy->print->plain(); 
  458. return $css; 
  459.  
  460. /** 
  461. * Override $content_width in customizer previews. 
  462. */ 
  463. public static function preview_content_width() { 
  464. global $wp_customize; 
  465. if ( ! is_customize_preview() ) { 
  466. return; 
  467.  
  468. $setting = $wp_customize->get_setting( 'jetpack_custom_css[content_width]' ); 
  469. if ( ! $setting ) { 
  470. return; 
  471.  
  472. $customized_content_width = (int) $setting->post_value(); 
  473. if ( ! empty( $customized_content_width ) ) { 
  474. $GLOBALS['content_width'] = $customized_content_width; 
  475.  
  476. /** 
  477. * Filter the current theme's stylesheet for potentially nullifying it. 
  478. * @param string $current Stylesheet URI for the current theme/child theme. 
  479. * @return mixed|void 
  480. */ 
  481. static function style_filter( $current ) { 
  482. if ( is_admin() ) { 
  483. return $current; 
  484. } elseif ( self::is_freetrial() && ( ! self::is_preview() || ! current_user_can( 'switch_themes' ) ) ) { 
  485. return $current; 
  486. } elseif ( self::skip_stylesheet() ) { 
  487. /** This filter is documented in modules/custom-css/custom-css.php */ 
  488. return apply_filters( 'safecss_style_filter_url', plugins_url( 'custom-css/css/blank.css', __FILE__ ) ); 
  489.  
  490. return $current; 
  491.  
  492. /** 
  493. * Determine whether or not we should have the theme skip its main stylesheet. 
  494. * @return mixed The truthiness of this value determines whether the stylesheet should be skipped. 
  495. */ 
  496. static function skip_stylesheet() { 
  497. /** This filter is documented in modules/custom-css/custom-css.php */ 
  498. $skip_stylesheet = apply_filters( 'safecss_skip_stylesheet', null ); 
  499. if ( ! is_null( $skip_stylesheet ) ) { 
  500. return $skip_stylesheet; 
  501.  
  502. $jetpack_custom_css = get_theme_mod( 'jetpack_custom_css', array() ); 
  503. if ( isset( $jetpack_custom_css['replace'] ) ) { 
  504. return $jetpack_custom_css['replace']; 
  505.  
  506. return false; 
  507.  
  508. /** 
  509. * Override $content_width in customizer previews. 
  510. * Runs on `safecss_skip_stylesheet` filter. 
  511. * @param bool $skip_value Should the stylesheet be skipped. 
  512. * @return null|bool 
  513. */ 
  514. public static function preview_skip_stylesheet( $skip_value ) { 
  515. global $wp_customize; 
  516. if ( ! is_customize_preview() ) { 
  517. return $skip_value; 
  518.  
  519. $setting = $wp_customize->get_setting( 'jetpack_custom_css[replace]' ); 
  520. if ( ! $setting ) { 
  521. return $skip_value; 
  522.  
  523. $customized_replace = $setting->post_value(); 
  524. if ( null !== $customized_replace ) { 
  525. return $customized_replace; 
  526.  
  527. return $skip_value; 
  528.  
  529. /** 
  530. * Add Custom CSS section and controls. 
  531. * @param WP_Customize_Manager $wp_customize WP_Customize_Manager instance. 
  532. */ 
  533. public static function customize_register( $wp_customize ) { 
  534.  
  535. /** 
  536. * SETTINGS. 
  537. */ 
  538.  
  539. $wp_customize->add_setting( 'jetpack_custom_css[preprocessor]', array( 
  540. 'default' => '',  
  541. 'transport' => 'postMessage',  
  542. 'sanitize_callback' => array( __CLASS__, 'sanitize_preprocessor' ),  
  543. ) ); 
  544.  
  545. $wp_customize->add_setting( 'jetpack_custom_css[replace]', array( 
  546. 'default' => false,  
  547. 'transport' => 'refresh',  
  548. ) ); 
  549.  
  550. $wp_customize->add_setting( 'jetpack_custom_css[content_width]', array( 
  551. 'default' => '',  
  552. 'transport' => 'refresh',  
  553. 'sanitize_callback' => array( __CLASS__, 'intval_base10' ),  
  554. ) ); 
  555.  
  556. // Add custom sanitization to the core css customizer setting. 
  557. foreach ( $wp_customize->settings() as $setting ) { 
  558. if ( $setting instanceof WP_Customize_Custom_CSS_Setting ) { 
  559. add_filter( "customize_sanitize_{$setting->id}", array( __CLASS__, 'sanitize_css_callback' ), 10, 2 ); 
  560.  
  561. /** 
  562. * CONTROLS. 
  563. */ 
  564.  
  565. // Overwrite the Core Control. 
  566. $core_custom_css = $wp_customize->get_control( 'custom_css' ); 
  567. if ( $core_custom_css ) { 
  568. $wp_customize->remove_control( 'custom_css' ); 
  569. $core_custom_css->type = 'jetpackCss'; 
  570. $wp_customize->add_control( $core_custom_css ); 
  571.  
  572. $wp_customize->selective_refresh->add_partial( 'custom_css', array( 
  573. 'type' => 'custom_css',  
  574. 'selector' => '#wp-custom-css',  
  575. 'container_inclusive' => false,  
  576. 'fallback_refresh' => false,  
  577. 'settings' => array( 
  578. 'custom_css[' . $wp_customize->get_stylesheet() . ']',  
  579. 'jetpack_custom_css[preprocessor]',  
  580. ),  
  581. 'render_callback' => array( __CLASS__, 'echo_custom_css_partial' ),  
  582. ) ); 
  583.  
  584. $wp_customize->add_control( 'wpcom_custom_css_content_width_control', array( 
  585. 'type' => 'text',  
  586. 'label' => __( 'Media Width', 'jetpack' ),  
  587. 'section' => 'custom_css',  
  588. 'settings' => 'jetpack_custom_css[content_width]',  
  589. ) ); 
  590.  
  591. $wp_customize->add_control( 'jetpack_css_mode_control', array( 
  592. 'type' => 'checkbox',  
  593. 'label' => __( 'Don\'t use the theme\'s original CSS.', 'jetpack' ),  
  594. 'section' => 'custom_css',  
  595. 'settings' => 'jetpack_custom_css[replace]',  
  596. ) ); 
  597.  
  598. /** 
  599. * An action to grab on to if another Jetpack Module would like to add its own controls. 
  600. * @module custom-css 
  601. * @since 4.4.2 
  602. * @param $wp_customize The WP_Customize object. 
  603. */ 
  604. do_action( 'jetpack_custom_css_customizer_controls', $wp_customize ); 
  605.  
  606. /** This filter is documented in modules/custom-css/custom-css.php */ 
  607. $preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() ); 
  608. if ( ! empty( $preprocessors ) ) { 
  609. $preprocessor_choices = array( 
  610. '' => __( 'None', 'jetpack' ),  
  611. ); 
  612.  
  613. foreach ( $preprocessors as $preprocessor_key => $processor ) { 
  614. $preprocessor_choices[ $preprocessor_key ] = $processor['name']; 
  615.  
  616. $wp_customize->add_control( 'jetpack_css_preprocessors_control', array( 
  617. 'type' => 'select',  
  618. 'choices' => $preprocessor_choices,  
  619. 'label' => __( 'Preprocessor', 'jetpack' ),  
  620. 'section' => 'custom_css',  
  621. 'settings' => 'jetpack_custom_css[preprocessor]',  
  622. ) ); 
  623.  
  624.  
  625. /** 
  626. * The callback to handle sanitizing the CSS. Takes different arguments, hence the proxy function. 
  627. * @param mixed $css Value of the setting. 
  628. * @param WP_Customize_Setting $setting WP_Customize_Setting instance. 
  629. * @return mixed|string 
  630. */ 
  631. public static function sanitize_css_callback( $css, $setting ) { 
  632. global $wp_customize; 
  633. return self::sanitize_css( $css, array( 
  634. 'preprocessor' => $wp_customize->get_setting( 'jetpack_custom_css[preprocessor]' )->value(),  
  635. ) ); 
  636.  
  637. /** 
  638. * Flesh out for wpcom. 
  639. * @todo 
  640. * @return bool 
  641. */ 
  642. public static function is_freetrial() { 
  643. return false; 
  644.  
  645. /** 
  646. * Flesh out for wpcom. 
  647. * @todo 
  648. * @return bool 
  649. */ 
  650. public static function is_preview() { 
  651. return false; 
  652.  
  653. /** 
  654. * Output the custom css for customize preview. 
  655. * @param string $css Custom CSS content. 
  656. * @return mixed 
  657. */ 
  658. public static function customize_preview_wp_get_custom_css( $css ) { 
  659. global $wp_customize; 
  660.  
  661. $preprocessor = $wp_customize->get_setting( 'jetpack_custom_css[preprocessor]' )->value(); 
  662.  
  663. // If it's empty, just return. 
  664. if ( empty( $preprocessor ) ) { 
  665. return $css; 
  666.  
  667. /** This filter is documented in modules/custom-css/custom-css.php */ 
  668. $preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() ); 
  669. if ( isset( $preprocessors[ $preprocessor ] ) ) { 
  670. return call_user_func( $preprocessors[ $preprocessor ]['callback'], $css ); 
  671.  
  672. return $css; 
  673.  
  674. /** 
  675. * Add CSS preprocessing to our CSS if it is supported. 
  676. * @param mixed $css Value of the setting. 
  677. * @param WP_Customize_Setting $setting WP_Customize_Setting instance. 
  678. * @return string 
  679. */ 
  680. public static function customize_value_custom_css( $css, $setting ) { 
  681. // Find the current preprocessor. 
  682. $jetpack_custom_css = get_theme_mod( 'jetpack_custom_css', array() ); 
  683. if ( isset( $jetpack_custom_css['preprocessor'] ) ) { 
  684. $preprocessor = $jetpack_custom_css['preprocessor']; 
  685.  
  686. // If it's not supported, just return. 
  687. /** This filter is documented in modules/custom-css/custom-css.php */ 
  688. $preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() ); 
  689. if ( ! isset( $preprocessors[ $preprocessor ] ) ) { 
  690. return $css; 
  691.  
  692. // Swap it for the `post_content_filtered` instead. 
  693. $post = wp_get_custom_css_post( $setting->stylesheet ); 
  694. if ( $post && ! empty( $post->post_content_filtered ) ) { 
  695. $css = $post->post_content_filtered; 
  696.  
  697. return $css; 
  698.  
  699. /** 
  700. * Store the original pre-processed CSS in `post_content_filtered` 
  701. * and then store processed CSS in `post_content`. 
  702. * @param array $args Content post args. 
  703. * @param string $css Original CSS being updated. 
  704. * @param WP_Customize_Custom_CSS_Setting $setting Custom CSS Setting. 
  705. * @return mixed 
  706. */ 
  707. public static function customize_update_custom_css_post_content_args( $args, $css, $setting ) { 
  708. // Find the current preprocessor. 
  709. $jetpack_custom_css = get_theme_mod( 'jetpack_custom_css', array() ); 
  710. if ( empty( $jetpack_custom_css['preprocessor'] ) ) { 
  711. return $args; 
  712.  
  713. $preprocessor = $jetpack_custom_css['preprocessor']; 
  714. /** This filter is documented in modules/custom-css/custom-css.php */ 
  715. $preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() ); 
  716.  
  717. // If it's empty, just return. 
  718. if ( empty( $preprocessor ) ) { 
  719. return $args; 
  720.  
  721. if ( isset( $preprocessors[ $preprocessor ] ) ) { 
  722. $args['post_content_filtered'] = $css; 
  723. $args['post_content'] = call_user_func( $preprocessors[ $preprocessor ]['callback'], $css ); 
  724.  
  725. return $args; 
  726.  
  727. /** 
  728. * Filter to handle the processing of preprocessed css on save. 
  729. * @param array $args Custom CSS options. 
  730. * @param string $stylesheet Original CSS to be updated. 
  731. * @return mixed 
  732. */ 
  733. public static function update_custom_css_data( $args, $stylesheet ) { 
  734. // Find the current preprocessor. 
  735. $jetpack_custom_css = get_theme_mod( 'jetpack_custom_css', array() ); 
  736. if ( empty( $jetpack_custom_css['preprocessor'] ) ) { 
  737. return $args; 
  738.  
  739. /** This filter is documented in modules/custom-css/custom-css.php */ 
  740. $preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() ); 
  741. $preprocessor = $jetpack_custom_css['preprocessor']; 
  742.  
  743. // If we have a preprocessor specified ... 
  744. if ( isset( $preprocessors[ $preprocessor ] ) ) { 
  745. // And no other preprocessor has run ... 
  746. if ( empty( $args['preprocessed'] ) ) { 
  747. $args['preprocessed'] = $args['css']; 
  748. $args['css'] = call_user_func( $preprocessors[ $preprocessor ]['callback'], $args['css'] ); 
  749. } else { 
  750. trigger_error( 'Jetpack CSS Preprocessor specified, but something else has already modified the argument.', E_USER_WARNING ); 
  751.  
  752. return $args; 
  753.  
  754. /** 
  755. * When on the edit screen, make sure the custom content width 
  756. * setting is applied to the large image size. 
  757. * @param array $dims Array of image dimensions (width and height). 
  758. * @param string $size Size of the resulting image. 
  759. * @param null $context Context the image is being resized for. `edit` or `display`. 
  760. * @return array 
  761. */ 
  762. static function editor_max_image_size( $dims, $size = 'medium', $context = null ) { 
  763. list( $width, $height ) = $dims; 
  764.  
  765. if ( 'large' === $size && 'edit' === $context ) { 
  766. $width = Jetpack::get_content_width(); 
  767.  
  768. return array( $width, $height ); 
  769.  
  770. /** 
  771. * Override the content_width with a custom value if one is set. 
  772. * @param int $content_width Content Width value to be updated. 
  773. * @return int 
  774. */ 
  775. static function jetpack_content_width( $content_width ) { 
  776. $custom_content_width = 0; 
  777.  
  778. $jetpack_custom_css = get_theme_mod( 'jetpack_custom_css', array() ); 
  779. if ( isset( $jetpack_custom_css['content_width'] ) ) { 
  780. $custom_content_width = $jetpack_custom_css['content_width']; 
  781.  
  782. if ( $custom_content_width > 0 ) { 
  783. return $custom_content_width; 
  784.  
  785. return $content_width; 
  786.  
  787. /** 
  788. * Currently this filter function gets called on 
  789. * 'template_redirect' action and 
  790. * 'admin_init' action 
  791. */ 
  792. static function set_content_width() { 
  793. // Don't apply this filter on the Edit CSS page. 
  794. if ( isset( $_GET['page'] ) && 'editcss' === $_GET['page'] && is_admin() ) { 
  795. return; 
  796.  
  797. $GLOBALS['content_width'] = Jetpack::get_content_width(); 
  798.  
  799. /** 
  800. * Make sure the preprocessor we're saving is one we know about. 
  801. * @param string $preprocessor The preprocessor to sanitize. 
  802. * @return null|string 
  803. */ 
  804. public static function sanitize_preprocessor( $preprocessor ) { 
  805. /** This filter is documented in modules/custom-css/custom-css.php */ 
  806. $preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() ); 
  807. if ( empty( $preprocessor ) || array_key_exists( $preprocessor, $preprocessors ) ) { 
  808. return $preprocessor; 
  809. return null; 
  810.  
  811. /** 
  812. * Get the base10 intval. 
  813. * This is used as a setting's sanitize_callback; we can't use just plain 
  814. * intval because the second argument is not what intval() expects. 
  815. * @access public 
  816. * @param mixed $value Number to convert. 
  817. * @return int Integer. 
  818. */ 
  819. public static function intval_base10( $value ) { 
  820. return intval( $value, 10 ); 
  821.  
  822. /** 
  823. * Add a footer action on revision.php to print some customizations for the theme switcher. 
  824. */ 
  825. public static function load_revision_php() { 
  826. add_action( 'admin_footer', array( __CLASS__, 'revision_admin_footer' ) ); 
  827.  
  828. /** 
  829. * Print the theme switcher on revision.php and move it into place. 
  830. */ 
  831. public static function revision_admin_footer() { 
  832. $post = get_post(); 
  833. if ( 'custom_css' !== $post->post_type ) { 
  834. return; 
  835. $stylesheet = $post->post_title; 
  836. ?> 
  837. <script type="text/html" id="tmpl-other-themes-switcher"> 
  838. <?php self::revisions_switcher_box( $stylesheet ); ?> 
  839. </script> 
  840. <style> 
  841. .other-themes-wrap { 
  842. float: right; 
  843. background-color: #fff; 
  844. -webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1); 
  845. box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1); 
  846. padding: 5px 10px; 
  847. margin-bottom: 10px; 
  848. .other-themes-wrap label { 
  849. display: block; 
  850. margin-bottom: 10px; 
  851. .other-themes-wrap select { 
  852. float: left; 
  853. width: 77%; 
  854. .other-themes-wrap button { 
  855. float: right; 
  856. width: 20%; 
  857. .revisions { 
  858. clear: both; 
  859. /* Hide the back-to-post link */ 
  860. .long-header + a { 
  861. display: none; 
  862. </style> 
  863. <script> 
  864. (function($) { 
  865. var switcher = $('#tmpl-other-themes-switcher').html(),  
  866. qty = $( switcher ).find('select option').length,  
  867. $switcher; 
  868.  
  869. if ( qty >= 3 ) { 
  870. $('h1.long-header').before( switcher ); 
  871. $switcher = $('.other-themes-wrap'); 
  872. $switcher.find('button').on('click', function(e) { 
  873. e.preventDefault(); 
  874. if ( $switcher.find('select').val() ) { 
  875. window.location.href = $switcher.find('select').val(); 
  876. }) 
  877. })(jQuery); 
  878. </script> 
  879. <?php 
  880.  
  881. /** 
  882. * The HTML for the theme revision switcher box. 
  883. * @param string $stylesheet Stylesheet name. 
  884. */ 
  885. public static function revisions_switcher_box( $stylesheet = '' ) { 
  886. $themes = self::get_all_themes_with_custom_css(); 
  887. ?> 
  888. <div class="other-themes-wrap"> 
  889. <label for="other-themes"><?php esc_html_e( 'Select another theme to view its custom CSS.', 'jetpack' ); ?></label> 
  890. <select id="other-themes"> 
  891. <option value=""><?php esc_html_e( 'Select a theme…', 'jetpack' ); ?></option> 
  892. <?php 
  893. foreach ( $themes as $theme_stylesheet => $data ) { 
  894. $revisions = wp_get_post_revisions( $data['post']->ID, array( 'posts_per_page' => 1 ) ); 
  895. if ( ! $revisions ) { 
  896. ?> 
  897. <option value="<?php echo esc_url( add_query_arg( 'id', $data['post']->ID, menu_page_url( 'editcss', 0 ) ) ); ?>" <?php disabled( $stylesheet, $theme_stylesheet ); ?>> 
  898. <?php echo esc_html( $data['label'] ); ?> 
  899. <?php printf( esc_html__( '(modified %s ago)', 'jetpack' ), human_time_diff( strtotime( $data['post']->post_modified_gmt ) ) ); ?></option> 
  900. <?php 
  901. continue; 
  902. $revision = array_shift( $revisions ); 
  903. ?> 
  904. <option value="<?php echo esc_url( get_edit_post_link( $revision->ID ) ); ?>" <?php disabled( $stylesheet, $theme_stylesheet ); ?>> 
  905. <?php echo esc_html( $data['label'] ); ?> 
  906. <?php printf( esc_html__( '(modified %s ago)', 'jetpack' ), human_time_diff( strtotime( $data['post']->post_modified_gmt ) ) ); ?></option> 
  907. <?php 
  908. ?> 
  909. </select> 
  910. <button class="button" id="other_theme_custom_css_switcher"><?php esc_html_e( 'Switch', 'jetpack' ); ?></button> 
  911. </div> 
  912. <?php