Ei kuvausta

custom-css-4.7.php 36KB

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