custom-css-4.7.php 36 KB

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