Jetpack_Custom_CSS

The Jetpack by WordPress.com Jetpack Custom CSS class.

Defined (1)

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

/modules/custom-css/custom-css.php  
  1. class Jetpack_Custom_CSS { 
  2. static function init() { 
  3. add_action( 'switch_theme', array( __CLASS__, 'reset' ) ); 
  4. add_action( 'wp_restore_post_revision', array( __CLASS__, 'restore_revision' ), 10, 2 ); 
  5.  
  6. // Save revisions for posts of type safecss. 
  7. add_action( 'load-revision.php', array( __CLASS__, 'add_revision_redirect' ) ); 
  8.  
  9. // Override the edit link, the default link causes a redirect loop 
  10. add_filter( 'get_edit_post_link', array( __CLASS__, 'revision_post_link' ), 10, 3 ); 
  11.  
  12. // Overwrite the content width global variable if one is set in the custom css 
  13. add_action( 'template_redirect', array( __CLASS__, 'set_content_width' ) ); 
  14. add_action( 'admin_init', array( __CLASS__, 'set_content_width' ) ); 
  15.  
  16. if ( ! is_admin() ) 
  17. add_filter( 'stylesheet_uri', array( __CLASS__, 'style_filter' ) ); 
  18.  
  19. define( 
  20. 'SAFECSS_USE_ACE',  
  21. ! jetpack_is_mobile() && 
  22. ! Jetpack_User_Agent_Info::is_ipad() && 
  23. /** 
  24. * Should the Custom CSS module use ACE to process CSS. 
  25. * @see http://ace.c9.io/ 
  26. * @module custom-css 
  27. * @since 1.7.0 
  28. * @param bool true Use ACE to process the Custom CSS. Default to true. 
  29. */ 
  30. apply_filters( 'safecss_use_ace', true ) 
  31. ); 
  32.  
  33. // Register safecss as a custom post_type 
  34. // Explicit capability definitions are largely unnecessary because the posts are manipulated in code via an options page, managing CSS revisions does check the capabilities, so let's ensure that the proper caps are checked. 
  35. register_post_type( 'safecss', array( 
  36. // These are the defaults 
  37. // 'exclude_from_search' => true,  
  38. // 'public' => false,  
  39. // 'publicly_queryable' => false,  
  40. // 'show_ui' => false,  
  41. 'supports' => array( 'revisions' ),  
  42. 'label' => 'Custom CSS',  
  43. 'can_export' => false,  
  44. 'rewrite' => false,  
  45. 'capabilities' => array( 
  46. 'edit_post' => 'edit_theme_options',  
  47. 'read_post' => 'read',  
  48. 'delete_post' => 'edit_theme_options',  
  49. 'edit_posts' => 'edit_theme_options',  
  50. 'edit_others_posts' => 'edit_theme_options',  
  51. 'publish_posts' => 'edit_theme_options',  
  52. 'read_private_posts' => 'read' 
  53. ) ); 
  54.  
  55. // Short-circuit WP if this is a CSS stylesheet request 
  56. if ( isset( $_GET['custom-css'] ) ) { 
  57. header( 'Content-Type: text/css', true, 200 ); 
  58. header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 31536000) . ' GMT' ); // 1 year 
  59. Jetpack_Custom_CSS::print_css(); 
  60. exit; 
  61.  
  62. add_action( 'admin_enqueue_scripts', array( 'Jetpack_Custom_CSS', 'enqueue_scripts' ) ); 
  63.  
  64. if ( isset( $_GET['page'] ) && 'editcss' == $_GET['page'] && is_admin() ) { 
  65. // Do migration routine if necessary 
  66. Jetpack_Custom_CSS::upgrade(); 
  67.  
  68. /** 
  69. * Allows additional work when migrating safecss from wp_options to wp_post. 
  70. * @module custom-css 
  71. * @since 1.7.0 
  72. */ 
  73. do_action( 'safecss_migrate_post' ); 
  74.  
  75. /** 
  76. * Never embed the style in the head on wpcom. 
  77. * Yes, this filter should be added to an unsynced file on wpcom, but 
  78. * there is no good syntactically-correct location to put it yet. 
  79. * @link https://github.com/Automattic/jetpack/commit/a1be114e9179f64d147124727a58e2cf76c7e5a1#commitcomment-7763921 
  80. */ 
  81. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { 
  82. add_filter( 'safecss_embed_style', '__return_false' ); 
  83. } else { 
  84. add_filter( 'safecss_embed_style', array( 'Jetpack_Custom_CSS', 'should_we_inline_custom_css' ), 10, 2 ); 
  85.  
  86. add_action( 'wp_head', array( 'Jetpack_Custom_CSS', 'link_tag' ), 101 ); 
  87.  
  88. add_filter( 'jetpack_content_width', array( 'Jetpack_Custom_CSS', 'jetpack_content_width' ) ); 
  89. add_filter( 'editor_max_image_size', array( 'Jetpack_Custom_CSS', 'editor_max_image_size' ), 10, 3 ); 
  90.  
  91. if ( !current_user_can( 'switch_themes' ) && !is_super_admin() ) 
  92. return; 
  93.  
  94. add_action( 'admin_menu', array( 'Jetpack_Custom_CSS', 'menu' ) ); 
  95.  
  96. if ( isset( $_POST['safecss'] ) && false == strstr( $_SERVER[ 'REQUEST_URI' ], 'options.php' ) ) { 
  97. check_admin_referer( 'safecss' ); 
  98.  
  99. $save_result = self::save( array( 
  100. 'css' => stripslashes( $_POST['safecss'] ),  
  101. 'is_preview' => isset( $_POST['action'] ) && $_POST['action'] == 'preview',  
  102. 'preprocessor' => isset( $_POST['custom_css_preprocessor'] ) ? $_POST['custom_css_preprocessor'] : '',  
  103. 'add_to_existing' => isset( $_POST['add_to_existing'] ) ? $_POST['add_to_existing'] == 'true' : true,  
  104. 'content_width' => isset( $_POST['custom_content_width'] ) ? $_POST['custom_content_width'] : false,  
  105. ) ); 
  106.  
  107. if ( $_POST['action'] == 'preview' ) { 
  108. wp_safe_redirect( add_query_arg( 'csspreview', 'true', get_option( 'home' ) ) ); 
  109. exit; 
  110.  
  111. if ( $save_result ) 
  112. add_action( 'admin_notices', array( 'Jetpack_Custom_CSS', 'saved_message' ) ); 
  113.  
  114. // Prevent content filters running on CSS when restoring revisions 
  115. if ( isset( $_REQUEST[ 'action' ] ) && 'restore' === $_REQUEST[ 'action' ] && false !== strstr( $_SERVER[ 'REQUEST_URI' ], 'revision.php' ) ) { 
  116. $parent_post = get_post( wp_get_post_parent_id( intval( $_REQUEST[ 'revision' ] ) ) ); 
  117. if ( $parent_post && ! is_wp_error( $parent_post ) && 'safecss' === $parent_post->post_type ) { 
  118. // Remove wp_filter_post_kses, this causes CSS escaping issues 
  119. remove_filter( 'content_save_pre', 'wp_filter_post_kses' ); 
  120. remove_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' ); 
  121. remove_all_filters( 'content_save_pre' ); 
  122.  
  123. // Modify all internal links so that preview state persists 
  124. if ( Jetpack_Custom_CSS::is_preview() ) 
  125. ob_start( array( 'Jetpack_Custom_CSS', 'buffer' ) ); 
  126.  
  127. /** 
  128. * Save new custom CSS. This should be the entry point for any third-party code using Jetpack_Custom_CSS 
  129. * to save CSS. 
  130. * @param array $args Array of arguments: 
  131. * string $css The CSS (or LESS or Sass) 
  132. * bool $is_preview Whether this CSS is preview or published 
  133. * string preprocessor Which CSS preprocessor to use 
  134. * bool $add_to_existing Whether this CSS replaces the theme's CSS or supplements it. 
  135. * int $content_width A custom $content_width to go along with this CSS. 
  136. * @return int The post ID of the saved Custom CSS post. 
  137. */ 
  138. public static function save( $args = array() ) { 
  139. $defaults = array( 
  140. 'css' => '',  
  141. 'is_preview' => false,  
  142. 'preprocessor' => '',  
  143. 'add_to_existing' => true,  
  144. 'content_width' => false,  
  145. ); 
  146.  
  147. $args = wp_parse_args( $args, $defaults ); 
  148.  
  149. if ( $args['content_width'] && intval( $args['content_width']) > 0 && ( ! isset( $GLOBALS['content_width'] ) || $args['content_width'] != $GLOBALS['content_width'] ) ) 
  150. $args['content_width'] = intval( $args['content_width'] ); 
  151. else 
  152. $args['content_width'] = false; 
  153.  
  154. // Remove wp_filter_post_kses, this causes CSS escaping issues 
  155. remove_filter( 'content_save_pre', 'wp_filter_post_kses' ); 
  156. remove_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' ); 
  157. remove_all_filters( 'content_save_pre' ); 
  158.  
  159. /** 
  160. * Fires prior to saving custom css values. Necessitated because the 
  161. * core WordPress save_pre filters were removed: 
  162. * - content_save_pre 
  163. * - content_filtered_save_pre 
  164. * @module custom-css 
  165. * @since 1.7.0 
  166. * @param array $args { 
  167. * Array of custom CSS arguments. 
  168. * @type string $css The CSS (or LESS or Sass). 
  169. * @type bool $is_preview Whether this CSS is preview or published. 
  170. * @type string preprocessor Which CSS preprocessor to use. 
  171. * @type bool $add_to_existing Whether this CSS replaces the theme's CSS or supplements it. 
  172. * @type int $content_width A custom $content_width to go along with this CSS. 
  173. * } 
  174. */ 
  175. do_action( 'safecss_save_pre', $args ); 
  176.  
  177. $warnings = array(); 
  178.  
  179. safecss_class(); 
  180. $csstidy = new csstidy(); 
  181. $csstidy->optimise = new safecss( $csstidy ); 
  182.  
  183. $csstidy->set_cfg( 'remove_bslash', false ); 
  184. $csstidy->set_cfg( 'compress_colors', false ); 
  185. $csstidy->set_cfg( 'compress_font-weight', false ); 
  186. $csstidy->set_cfg( 'optimise_shorthands', 0 ); 
  187. $csstidy->set_cfg( 'remove_last_;', false ); 
  188. $csstidy->set_cfg( 'case_properties', false ); 
  189. $csstidy->set_cfg( 'discard_invalid_properties', true ); 
  190. $csstidy->set_cfg( 'css_level', 'CSS3.0' ); 
  191. $csstidy->set_cfg( 'preserve_css', true ); 
  192. $csstidy->set_cfg( 'template', dirname( __FILE__ ) . '/csstidy/wordpress-standard.tpl' ); 
  193.  
  194. $css = $orig = $args['css']; 
  195.  
  196. $css = preg_replace( '/\\\\([0-9a-fA-F]{4})/', '\\\\\\\\$1', $prev = $css ); 
  197.  
  198. if ( $css != $prev ) 
  199. $warnings[] = 'preg_replace found stuff'; 
  200.  
  201. // Some people put weird stuff in their CSS, KSES tends to be greedy 
  202. $css = str_replace( '<=', '<=', $css ); 
  203. // Why KSES instead of strip_tags? Who knows? 
  204. $css = wp_kses_split( $prev = $css, array(), array() ); 
  205. $css = str_replace( '>', '>', $css ); // kses replaces lone '>' with > 
  206. // Why both KSES and strip_tags? Because we just added some '>'. 
  207. $css = strip_tags( $css ); 
  208.  
  209. if ( $css != $prev ) 
  210. $warnings[] = 'kses found stuff'; 
  211.  
  212. // if we're not using a preprocessor 
  213. if ( ! $args['preprocessor'] ) { 
  214.  
  215. /** 
  216. * Fires before parsing the css with CSSTidy, but only if 
  217. * the preprocessor is not configured for use. 
  218. * @module custom-css 
  219. * @since 1.7.0 
  220. * @param obj $csstidy The csstidy object. 
  221. * @param string $css Custom CSS. 
  222. * @param array $args Array of custom CSS arguments. 
  223. */ 
  224. do_action( 'safecss_parse_pre', $csstidy, $css, $args ); 
  225.  
  226. $csstidy->parse( $css ); 
  227.  
  228. /** 
  229. * Fires after parsing the css with CSSTidy, but only if 
  230. * the preprocessor is not cinfigured for use. 
  231. * @module custom-css 
  232. * @since 1.7.0 
  233. * @param obj $csstidy The csstidy object. 
  234. * @param array $warnings Array of warnings. 
  235. * @param array $args Array of custom CSS arguments. 
  236. */ 
  237. do_action( 'safecss_parse_post', $csstidy, $warnings, $args ); 
  238.  
  239. $css = $csstidy->print->plain(); 
  240.  
  241. if ( $args['add_to_existing'] ) 
  242. $add_to_existing = 'yes'; 
  243. else 
  244. $add_to_existing = 'no'; 
  245.  
  246. if ( $args['is_preview'] || Jetpack_Custom_CSS::is_freetrial() ) { 
  247. // Save the CSS 
  248. $safecss_revision_id = Jetpack_Custom_CSS::save_revision( $css, true, $args['preprocessor'] ); 
  249.  
  250. // Cache Buster 
  251. update_option( 'safecss_preview_rev', intval( get_option( 'safecss_preview_rev' ) ) + 1); 
  252.  
  253. update_metadata( 'post', $safecss_revision_id, 'custom_css_add', $add_to_existing ); 
  254. update_metadata( 'post', $safecss_revision_id, 'content_width', $args['content_width'] ); 
  255. update_metadata( 'post', $safecss_revision_id, 'custom_css_preprocessor', $args['preprocessor'] ); 
  256.  
  257. delete_option( 'safecss_add' ); 
  258. delete_option( 'safecss_content_width' ); 
  259.  
  260. if ( $args['is_preview'] ) { 
  261. return $safecss_revision_id; 
  262.  
  263. /** 
  264. * Fires after saving Custom CSS. 
  265. * @module custom-css 
  266. * @since 1.7.0 
  267. */ 
  268. do_action( 'safecss_save_preview_post' ); 
  269.  
  270. // Save the CSS 
  271. $safecss_post_id = Jetpack_Custom_CSS::save_revision( $css, false, $args['preprocessor'] ); 
  272.  
  273. $safecss_post_revision = Jetpack_Custom_CSS::get_current_revision(); 
  274.  
  275. update_option( 'safecss_rev', intval( get_option( 'safecss_rev' ) ) + 1 ); 
  276.  
  277. update_post_meta( $safecss_post_id, 'custom_css_add', $add_to_existing ); 
  278. update_post_meta( $safecss_post_id, 'content_width', $args['content_width'] ); 
  279. update_post_meta( $safecss_post_id, 'custom_css_preprocessor', $args['preprocessor'] ); 
  280.  
  281. delete_option( 'safecss_add' ); 
  282. delete_option( 'safecss_content_width' ); 
  283.  
  284. update_metadata( 'post', $safecss_post_revision['ID'], 'custom_css_add', $add_to_existing ); 
  285. update_metadata( 'post', $safecss_post_revision['ID'], 'content_width', $args['content_width'] ); 
  286. update_metadata( 'post', $safecss_post_revision['ID'], 'custom_css_preprocessor', $args['preprocessor'] ); 
  287.  
  288. delete_option( 'safecss_preview_add' ); 
  289.  
  290. return $safecss_post_id; 
  291.  
  292. /** 
  293. * Get the published custom CSS post. 
  294. * @return array 
  295. */ 
  296. static function get_post() { 
  297. $custom_css_post_id = Jetpack_Custom_CSS::post_id(); 
  298.  
  299. if ( $custom_css_post_id ) 
  300. return get_post( $custom_css_post_id, ARRAY_A ); 
  301.  
  302. return array(); 
  303.  
  304. /** 
  305. * Get the post ID of the published custom CSS post. 
  306. * @return int|bool The post ID if it exists; false otherwise. 
  307. */ 
  308. static function post_id() { 
  309. /** 
  310. * Filter the ID of the post where Custom CSS is stored, before the ID is retrieved. 
  311. * If the callback function returns a non-null value, then post_id() will immediately 
  312. * return that value, instead of retrieving the normal post ID. 
  313. * @module custom-css 
  314. * @since 3.8.1 
  315. * @param null null The ID to return instead of the normal ID. 
  316. */ 
  317. $custom_css_post_id = apply_filters( 'jetpack_custom_css_pre_post_id', null ); 
  318. if ( ! is_null( $custom_css_post_id ) ) { 
  319. return $custom_css_post_id; 
  320.  
  321. $custom_css_post_id = wp_cache_get( 'custom_css_post_id' ); 
  322.  
  323. if ( false === $custom_css_post_id ) { 
  324. $custom_css_posts = get_posts( array( 
  325. 'posts_per_page' => 1,  
  326. 'post_type' => 'safecss',  
  327. 'post_status' => 'publish',  
  328. 'orderby' => 'date',  
  329. 'order' => 'DESC' 
  330. ) ); 
  331.  
  332. if ( count( $custom_css_posts ) > 0 ) 
  333. $custom_css_post_id = $custom_css_posts[0]->ID; 
  334. else 
  335. $custom_css_post_id = 0; 
  336.  
  337. // Save post_id=0 to note that no safecss post exists. 
  338. wp_cache_set( 'custom_css_post_id', $custom_css_post_id ); 
  339.  
  340. if ( ! $custom_css_post_id ) 
  341. return false; 
  342.  
  343. return $custom_css_post_id; 
  344.  
  345. /** 
  346. * Get the current revision of the original safecss record 
  347. * @return object 
  348. */ 
  349. static function get_current_revision() { 
  350. $safecss_post = Jetpack_Custom_CSS::get_post(); 
  351.  
  352. if ( empty( $safecss_post ) ) { 
  353. return false; 
  354.  
  355. $revisions = wp_get_post_revisions( $safecss_post['ID'], array( 'posts_per_page' => 1, 'orderby' => 'date', 'order' => 'DESC' ) ); 
  356.  
  357. // Empty array if no revisions exist 
  358. if ( empty( $revisions ) ) { 
  359. // Return original post 
  360. return $safecss_post; 
  361. } else { 
  362. // Return the first entry in $revisions, this will be the current revision 
  363. $current_revision = get_object_vars( array_shift( $revisions ) ); 
  364. return $current_revision; 
  365.  
  366. /** 
  367. * Save new revision of CSS 
  368. * Checks to see if content was modified before really saving 
  369. * @param string $css 
  370. * @param bool $is_preview 
  371. * @return bool|int If nothing was saved, returns false. If a post 
  372. * or revision was saved, returns the post ID. 
  373. */ 
  374. static function save_revision( $css, $is_preview = false, $preprocessor = '' ) { 
  375. $safecss_post = Jetpack_Custom_CSS::get_post(); 
  376.  
  377. $compressed_css = Jetpack_Custom_CSS::minify( $css, $preprocessor ); 
  378.  
  379. // If null, there was no original safecss record, so create one 
  380. if ( null == $safecss_post ) { 
  381. if ( ! $css ) 
  382. return false; 
  383.  
  384. $post = array(); 
  385. $post['post_content'] = $css; 
  386. $post['post_title'] = 'safecss'; 
  387. $post['post_status'] = 'publish'; 
  388. $post['post_type'] = 'safecss'; 
  389. $post['post_content_filtered'] = $compressed_css; 
  390.  
  391. // Set excerpt to current theme, for display in revisions list 
  392. if ( function_exists( 'wp_get_theme' ) ) { 
  393. $current_theme = wp_get_theme(); 
  394. $post['post_excerpt'] = $current_theme->Name; 
  395. else { 
  396. $post['post_excerpt'] = get_current_theme(); 
  397.  
  398. // Insert the CSS into wp_posts 
  399. $post_id = wp_insert_post( $post ); 
  400. wp_cache_set( 'custom_css_post_id', $post_id ); 
  401. return $post_id; 
  402.  
  403. // Update CSS in post array with new value passed to this function 
  404. $safecss_post['post_content'] = $css; 
  405. $safecss_post['post_content_filtered'] = $compressed_css; 
  406.  
  407. // Set excerpt to current theme, for display in revisions list 
  408. if ( function_exists( 'wp_get_theme' ) ) { 
  409. $current_theme = wp_get_theme(); 
  410. $safecss_post['post_excerpt'] = $current_theme->Name; 
  411. else { 
  412. $safecss_post['post_excerpt'] = get_current_theme(); 
  413.  
  414. // Don't carry over last revision's timestamps, otherwise revisions all have matching timestamps 
  415. unset( $safecss_post['post_date'] ); 
  416. unset( $safecss_post['post_date_gmt'] ); 
  417. unset( $safecss_post['post_modified'] ); 
  418. unset( $safecss_post['post_modified_gmt'] ); 
  419.  
  420. // Do not update post if we are only saving a preview 
  421. if ( false === $is_preview ) { 
  422. $post_id = wp_update_post( $safecss_post ); 
  423. wp_cache_set( 'custom_css_post_id', $post_id ); 
  424. return $post_id; 
  425. else if ( ! defined( 'DOING_MIGRATE' ) ) { 
  426. return _wp_put_post_revision( $safecss_post ); 
  427.  
  428. static function skip_stylesheet() { 
  429. /** 
  430. * Prevent the Custom CSS stylesheet from being enqueued. 
  431. * @module custom-css 
  432. * @since 2.2.1 
  433. * @param null Should the stylesheet be skipped. Default to null. Anything else will force the stylesheet to be skipped. 
  434. */ 
  435. $skip_stylesheet = apply_filters( 'safecss_skip_stylesheet', null ); 
  436.  
  437. if ( null !== $skip_stylesheet ) { 
  438. return $skip_stylesheet; 
  439. } elseif ( Jetpack_Custom_CSS::is_customizer_preview() ) { 
  440. return false; 
  441. } else { 
  442. if ( Jetpack_Custom_CSS::is_preview() ) { 
  443. $safecss_post = Jetpack_Custom_CSS::get_current_revision(); 
  444.  
  445. if ( $safecss_post ) 
  446. return (bool) ( get_post_meta( $safecss_post['ID'], 'custom_css_add', true ) == 'no' ); 
  447. else 
  448. return (bool) ( get_option( 'safecss_preview_add' ) == 'no' ); 
  449. else { 
  450. $custom_css_post_id = Jetpack_Custom_CSS::post_id(); 
  451.  
  452. if ( $custom_css_post_id ) { 
  453. $custom_css_add = get_post_meta( $custom_css_post_id, 'custom_css_add', true ); 
  454.  
  455. // It is possible for the CSS to be stored in a post but for the safecss_add option 
  456. // to have not been upgraded yet if the user hasn't opened their Custom CSS editor 
  457. // since October 2012. 
  458. if ( ! empty( $custom_css_add ) ) 
  459. return (bool) ( $custom_css_add === 'no' ); 
  460.  
  461. return (bool) ( get_option( 'safecss_add' ) == 'no' ); 
  462.  
  463. static function is_preview() { 
  464. return isset( $_GET['csspreview'] ) && $_GET['csspreview'] === 'true'; 
  465.  
  466. /** 
  467. * Currently this filter function gets called on 
  468. * 'template_redirect' action and 
  469. * 'admin_init' action 
  470. */ 
  471. static function set_content_width() { 
  472. // Don't apply this filter on the Edit CSS page 
  473. if ( isset( $_GET ) && isset( $_GET['page'] ) && 'editcss' == $_GET['page'] && is_admin() ) { 
  474. return; 
  475.  
  476. $GLOBALS['content_width'] = Jetpack::get_content_width(); 
  477.  
  478. /** 
  479. * False when the site has the Custom Design upgrade. 
  480. * Used only on WordPress.com. 
  481. */ 
  482. static function is_freetrial() { 
  483. /** 
  484. * Determine if a WordPress.com site uses a Free trial of the Custom Design Upgrade. 
  485. * Used only on WordPress.com. 
  486. * @module custom-css 
  487. * @since 1.7.0 
  488. * @param bool false Does the site use a Free trial of the Custom Design Upgrade. Default to false. 
  489. */ 
  490. return apply_filters( 'safecss_is_freetrial', false ); 
  491.  
  492. static function get_preprocessor_key() { 
  493. $safecss_post = Jetpack_Custom_CSS::get_current_revision(); 
  494. return get_post_meta( $safecss_post['ID'], 'custom_css_preprocessor', true ); 
  495.  
  496. static function get_preprocessor() { 
  497. /** This filter is documented in modules/custom-css/custom-css.php */ 
  498. $preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() ); 
  499. $selected_preprocessor_key = self::get_preprocessor_key(); 
  500. $selected_preprocessor = isset( $preprocessors[ $selected_preprocessor_key ] ) ? $preprocessors[ $selected_preprocessor_key ] : null; 
  501. return $selected_preprocessor; 
  502.  
  503. static function get_css( $compressed = false ) { 
  504. /** 
  505. * Filter the Custom CSS returned. 
  506. * Can be used to return an error, or no CSS at all. 
  507. * @module custom-css 
  508. * @since 1.7.0 
  509. * @param bool false Should we return an error instead of the Custom CSS. Default to false. 
  510. */ 
  511. $default_css = apply_filters( 'safecss_get_css_error', false ); 
  512.  
  513. if ( $default_css !== false ) 
  514. return $default_css; 
  515.  
  516. $option = ( Jetpack_Custom_CSS::is_preview() || Jetpack_Custom_CSS::is_freetrial() ) ? 'safecss_preview' : 'safecss'; 
  517. $css = ''; 
  518.  
  519. if ( 'safecss' == $option ) { 
  520. // Don't bother checking for a migrated 'safecss' option if it never existed. 
  521. if ( false === get_option( 'safecss' ) || get_option( 'safecss_revision_migrated' ) ) { 
  522. $safecss_post = Jetpack_Custom_CSS::get_post(); 
  523. if ( ! empty( $safecss_post ) ) { 
  524. $css = ( $compressed && $safecss_post['post_content_filtered'] ) ? $safecss_post['post_content_filtered'] : $safecss_post['post_content']; 
  525. } else { 
  526. $current_revision = Jetpack_Custom_CSS::get_current_revision(); 
  527. if ( false === $current_revision ) { 
  528. $css = ''; 
  529. } else { 
  530. $css = ( $compressed && $current_revision['post_content_filtered'] ) ? $current_revision['post_content_filtered'] : $current_revision['post_content']; 
  531.  
  532. // Fix for un-migrated Custom CSS 
  533. if ( empty( $safecss_post ) ) { 
  534. $_css = get_option( 'safecss' ); 
  535. if ( !empty( $_css ) ) { 
  536. $css = $_css; 
  537. else if ( 'safecss_preview' == $option ) { 
  538. $safecss_post = Jetpack_Custom_CSS::get_current_revision(); 
  539. $css = $safecss_post['post_content']; 
  540. $css = stripslashes( $css ); 
  541. $css = Jetpack_Custom_CSS::minify( $css, get_post_meta( $safecss_post['ID'], 'custom_css_preprocessor', true ) ); 
  542.  
  543. $css = str_replace( array( '\\\00BB \\\0020', '\0BB \020', '0BB 020' ), '\00BB \0020', $css ); 
  544.  
  545. if ( empty( $css ) ) { 
  546. $css = "/*\n" 
  547. . wordwrap( 
  548. /** 
  549. * Filter the default message displayed in the Custom CSS editor. 
  550. * @module custom-css 
  551. * @since 1.7.0 
  552. * @param string $str Default Custom CSS editor content. 
  553. */ 
  554. apply_filters( 
  555. 'safecss_default_css',  
  556. __( 
  557. "Welcome to Custom CSS!\n\nTo learn how this works, see http://wp.me/PEmnE-Bt",  
  558. 'jetpack' 
  559. . "\n*/"; 
  560.  
  561. /** 
  562. * Filter the Custom CSS returned from the editor. 
  563. * @module custom-css 
  564. * @since 1.7.0 
  565. * @param string $css Custom CSS. 
  566. */ 
  567. $css = apply_filters( 'safecss_css', $css ); 
  568.  
  569. return $css; 
  570.  
  571. static function replace_insecure_urls( $css ) { 
  572. if ( ! function_exists( '_sa_get_frontend_https_url_replacement_map' ) ) { 
  573. return $css; 
  574. list( $http_urls, $secure_urls ) = _sa_get_frontend_https_url_replacement_map(); 
  575.  
  576. return str_replace( $http_urls, $secure_urls, $css ); 
  577.  
  578. static function print_css() { 
  579.  
  580. /** 
  581. * Fires right before printing the custom CSS inside the <head> element. 
  582. * @module custom-css 
  583. * @since 1.7.0 
  584. */ 
  585. do_action( 'safecss_print_pre' ); 
  586. $css = Jetpack_Custom_CSS::get_css( true ); 
  587. echo self::replace_insecure_urls( $css ); 
  588.  
  589. static function should_we_inline_custom_css( $should_we, $css ) { 
  590. // If the CSS is less than 2, 000 characters, inline it! otherwise return what was passed in. 
  591. return ( strlen( $css ) < 2000 ) ? true : $should_we; 
  592.  
  593. static function link_tag() { 
  594. global $blog_id, $current_blog; 
  595.  
  596. if ( 
  597. /** 
  598. * Do not include any CSS on the page if the CSS includes an error. 
  599. * Setting this filter to true stops any Custom CSS from being enqueued. 
  600. * @module custom-css 
  601. * @since 1.7.0 
  602. * @param bool false Does the CSS include an error. Default to false. 
  603. */ 
  604. apply_filters( 'safecss_style_error', false ) 
  605. ) { 
  606. return; 
  607.  
  608. if ( ! is_super_admin() && isset( $current_blog ) && ( 1 == $current_blog->spam || 1 == $current_blog->deleted ) ) 
  609. return; 
  610.  
  611. if ( Jetpack_Custom_CSS::is_customizer_preview() ) 
  612. return; 
  613.  
  614. $css = ''; 
  615. $option = Jetpack_Custom_CSS::is_preview() ? 'safecss_preview' : 'safecss'; 
  616.  
  617. if ( 'safecss' == $option ) { 
  618. if ( get_option( 'safecss_revision_migrated' ) ) { 
  619. $safecss_post = Jetpack_Custom_CSS::get_post(); 
  620.  
  621. if ( ! empty( $safecss_post['post_content'] ) ) { 
  622. $css = $safecss_post['post_content']; 
  623. } else { 
  624. $current_revision = Jetpack_Custom_CSS::get_current_revision(); 
  625.  
  626. if ( ! empty( $current_revision['post_content'] ) ) { 
  627. $css = $current_revision['post_content']; 
  628.  
  629. // Fix for un-migrated Custom CSS 
  630. if ( empty( $safecss_post ) ) { 
  631. $_css = get_option( 'safecss' ); 
  632. if ( !empty( $_css ) ) { 
  633. $css = $_css; 
  634.  
  635. if ( 'safecss_preview' == $option ) { 
  636. $safecss_post = Jetpack_Custom_CSS::get_current_revision(); 
  637.  
  638. if ( !empty( $safecss_post['post_content'] ) ) { 
  639. $css = $safecss_post['post_content']; 
  640.  
  641. $css = str_replace( array( '\\\00BB \\\0020', '\0BB \020', '0BB 020' ), '\00BB \0020', $css ); 
  642.  
  643. if ( $css == '' ) 
  644. return; 
  645.  
  646. if ( 
  647. /** 
  648. * Allow inserting CSS inline instead of through a separate file. 
  649. * @module custom-css 
  650. * @since 3.4.0 
  651. * @param bool false Should the CSS be added inline instead of through a separate file. Default to false. 
  652. * @param string $css Custom CSS. 
  653. */ 
  654. apply_filters( 'safecss_embed_style', false, $css ) 
  655. ) { 
  656.  
  657. echo "\r\n" . '<style id="custom-css-css">' . Jetpack_Custom_CSS::get_css( true ) . "</style>\r\n"; 
  658.  
  659. } else { 
  660.  
  661. $href = home_url( '/' ); 
  662. $href = add_query_arg( 'custom-css', 1, $href ); 
  663. $href = add_query_arg( 'csblog', $blog_id, $href ); 
  664. $href = add_query_arg( 'cscache', 6, $href ); 
  665. $href = add_query_arg( 'csrev', (int) get_option( $option . '_rev' ), $href ); 
  666.  
  667. /** 
  668. * Filter the Custom CSS link enqueued in the head. 
  669. * @module custom-css 
  670. * @since 1.7.0 
  671. * @param string $href Custom CSS link enqueued in the head. 
  672. * @param string $blog_id Blog ID. 
  673. */ 
  674. $href = apply_filters( 'safecss_href', $href, $blog_id ); 
  675.  
  676. if ( Jetpack_Custom_CSS::is_preview() ) 
  677. $href = add_query_arg( 'csspreview', 'true', $href ); 
  678.  
  679. ?> 
  680. <link rel="stylesheet" id="custom-css-css" type="text/css" href="<?php echo esc_url( $href ); ?>" /> 
  681. <?php 
  682.  
  683.  
  684. /** 
  685. * Fires after creating the <link> in the <head> element for the custom css stylesheet. 
  686. * @module custom-css 
  687. * @since 2.2.2 
  688. */ 
  689. do_action( 'safecss_link_tag_post' ); 
  690.  
  691. static function style_filter( $current ) { 
  692. if ( Jetpack_Custom_CSS::is_freetrial() && ( ! Jetpack_Custom_CSS::is_preview() || ! current_user_can( 'switch_themes' ) ) ) 
  693. return $current; 
  694. else if ( Jetpack_Custom_CSS::skip_stylesheet() ) 
  695. /** 
  696. * Filter the default blank Custom CSS URL. 
  697. * @module custom-css 
  698. * @since 2.2.1 
  699. * @param string $url Default blank Custom CSS URL. 
  700. */ 
  701. return apply_filters( 'safecss_style_filter_url', plugins_url( 'custom-css/css/blank.css', __FILE__ ) ); 
  702.  
  703. return $current; 
  704.  
  705. static function buffer( $html ) { 
  706. $html = str_replace( '</body>', Jetpack_Custom_CSS::preview_flag(), $html ); 
  707. return preg_replace_callback( '!href=([\'"])(.*?)\\1!', array( 'Jetpack_Custom_CSS', 'preview_links' ), $html ); 
  708.  
  709. static function preview_links( $matches ) { 
  710. if ( 0 !== strpos( $matches[2], get_option( 'home' ) ) ) 
  711. return $matches[0]; 
  712.  
  713. $link = wp_specialchars_decode( $matches[2] ); 
  714. $link = add_query_arg( 'csspreview', 'true', $link ); 
  715. $link = esc_url( $link ); 
  716. return "href={$matches[1]}$link{$matches[1]}"; 
  717.  
  718. /** 
  719. * Places a black bar above every preview page 
  720. */ 
  721. static function preview_flag() { 
  722. if ( is_admin() ) 
  723. return; 
  724.  
  725. $message = esc_html__( 'Preview: changes must be saved or they will be lost', 'jetpack' ); 
  726. /** 
  727. * Filter the Preview message displayed on the site when previewing custom CSS, before to save it. 
  728. * @module custom-css 
  729. * @since 1.7.0 
  730. * @param string $message Custom CSS preview message. 
  731. */ 
  732. $message = apply_filters( 'safecss_preview_message', $message ); 
  733.  
  734. $preview_flag_js = "var flag = document.createElement('div'); 
  735. flag.innerHTML = " . json_encode( $message ) . "; 
  736. flag.style.background = '#FF6600'; 
  737. flag.style.color = 'white'; 
  738. flag.style.textAlign = 'center'; 
  739. flag.style.fontSize = '15px'; 
  740. flag.style.padding = '2px'; 
  741. flag.style.fontFamily = 'sans-serif'; 
  742. document.body.style.paddingTop = '0px'; 
  743. document.body.insertBefore(flag, document.body.childNodes[0]); 
  744. "; 
  745.  
  746. /** 
  747. * Filter the Custom CSS preview message JS styling. 
  748. * @module custom-css 
  749. * @since 1.7.0 
  750. * @param string $preview_flag_js Custom CSS preview message JS styling. 
  751. */ 
  752. $preview_flag_js = apply_filters( 'safecss_preview_flag_js', $preview_flag_js ); 
  753. if ( $preview_flag_js ) { 
  754. $preview_flag_js = '<script type="text/javascript"> 
  755. // <![CDATA[ 
  756. ' . $preview_flag_js . ' 
  757. // ]]> 
  758. </script>'; 
  759.  
  760. return $preview_flag_js; 
  761.  
  762. static function menu() { 
  763. $parent = 'themes.php'; 
  764. $title = __( 'Edit CSS', 'jetpack' ); 
  765. $hook = add_theme_page( $title, $title, 'edit_theme_options', 'editcss', array( 'Jetpack_Custom_CSS', 'admin' ) ); 
  766.  
  767. add_action( "load-revision.php", array( 'Jetpack_Custom_CSS', 'prettify_post_revisions' ) ); 
  768. add_action( "load-$hook", array( 'Jetpack_Custom_CSS', 'update_title' ) ); 
  769.  
  770. /** 
  771. * Adds a menu item in the appearance section for this plugin's administration 
  772. * page. Also adds hooks to enqueue the CSS and JS for the admin page. 
  773. */ 
  774. static function update_title() { 
  775. global $title; 
  776. $title = __( 'CSS', 'jetpack' ); 
  777.  
  778. static function prettify_post_revisions() { 
  779. add_filter( 'the_title', array( 'Jetpack_Custom_CSS', 'post_title' ), 10, 2 ); 
  780.  
  781. static function post_title( $title, $post_id ) { 
  782. if ( !$post_id = (int) $post_id ) { 
  783. return $title; 
  784.  
  785. if ( !$post = get_post( $post_id ) ) { 
  786. return $title; 
  787.  
  788. if ( 'safecss' != $post->post_type ) { 
  789. return $title; 
  790.  
  791. return __( 'Custom CSS Stylesheet', 'jetpack' ); 
  792.  
  793. static function enqueue_scripts( $hook ) { 
  794. if ( 'appearance_page_editcss' != $hook ) 
  795. return; 
  796.  
  797. wp_enqueue_script( 'postbox' ); 
  798. wp_enqueue_script( 'custom-css-editor', plugins_url( 'custom-css/js/css-editor.js', __FILE__ ), 'jquery', '20130325', true ); 
  799. wp_enqueue_style( 'custom-css-editor', plugins_url( 'custom-css/css/css-editor.css', __FILE__ ) ); 
  800.  
  801. if ( defined( 'SAFECSS_USE_ACE' ) && SAFECSS_USE_ACE ) { 
  802. wp_register_style( 'jetpack-css-codemirror', plugins_url( 'custom-css/css/codemirror.css', __FILE__ ), array(), '20120905' ); 
  803. wp_enqueue_style( 'jetpack-css-use-codemirror', plugins_url( 'custom-css/css/use-codemirror.css', __FILE__ ), array( 'jetpack-css-codemirror' ), '20120905' ); 
  804.  
  805. wp_register_script( 'jetpack-css-codemirror', plugins_url( 'custom-css/js/codemirror.min.js', __FILE__ ), array(), '3.16', true ); 
  806. wp_enqueue_script( 'jetpack-css-use-codemirror', plugins_url( 'custom-css/js/use-codemirror.js', __FILE__ ), array( 'jquery', 'underscore', 'jetpack-css-codemirror' ), '20131009', true ); 
  807.  
  808. static function saved_message() { 
  809. echo '<div id="message" class="updated fade"><p><strong>' . __( 'Stylesheet saved.', 'jetpack' ) . '</strong></p></div>'; 
  810.  
  811. static function admin() { 
  812. add_meta_box( 'submitdiv', __( 'Publish', 'jetpack' ), array( __CLASS__, 'publish_box' ), 'editcss', 'side' ); 
  813. add_action( 'custom_css_submitbox_misc_actions', array( __CLASS__, 'content_width_settings' ) ); 
  814.  
  815. $safecss_post = Jetpack_Custom_CSS::get_post(); 
  816.  
  817. if ( ! empty( $safecss_post ) && 0 < $safecss_post['ID'] && wp_get_post_revisions( $safecss_post['ID'] ) ) 
  818. add_meta_box( 'revisionsdiv', __( 'CSS Revisions', 'jetpack' ), array( __CLASS__, 'revisions_meta_box' ), 'editcss', 'side' ); 
  819. ?> 
  820. <div class="wrap"> 
  821. <?php 
  822.  
  823. /** 
  824. * Fires right before the custom css page begins. 
  825. * @module custom-css 
  826. * @since 1.7.0 
  827. */ 
  828. do_action( 'custom_design_header' ); 
  829.  
  830. ?> 
  831. <h1><?php _e( 'CSS Stylesheet Editor', 'jetpack' ); ?></h1> 
  832. <form id="safecssform" action="" method="post"> 
  833. <?php wp_nonce_field( 'safecss' ) ?> 
  834. <?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); ?> 
  835. <?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?> 
  836. <input type="hidden" name="action" value="save" /> 
  837. <div id="poststuff"> 
  838. <p class="css-support"> 
  839. <?php 
  840. /** 
  841. * Filter the intro text appearing above the Custom CSS Editor. 
  842. * @module custom-css 
  843. * @since 1.7.0 
  844. * @param string $str Intro text appearing above the Custom CSS editor. 
  845. */ 
  846. echo apply_filters( 'safecss_intro_text', __( 'New to CSS? Start with a <a href="http://www.htmldog.com/guides/cssbeginner/">beginner tutorial</a>. Questions? 
  847. Ask in the <a href="http://wordpress.org/support/forum/themes-and-templates">Themes and Templates forum</a>.', 'jetpack' ) ); 
  848. ?></p> 
  849. <p class="css-support"><?php echo __( 'Note: Custom CSS will be reset when changing themes.', 'jetpack' ); ?></p> 
  850.  
  851. <div id="post-body" class="metabox-holder columns-2"> 
  852. <div id="post-body-content"> 
  853. <div class="postarea"> 
  854. <textarea id="safecss" name="safecss"<?php if ( SAFECSS_USE_ACE ) echo ' class="hide-if-js"'; ?>><?php echo esc_textarea( Jetpack_Custom_CSS::get_css() ); ?></textarea> 
  855. <div class="clear"></div> 
  856. </div> 
  857. </div> 
  858. <div id="postbox-container-1" class="postbox-container"> 
  859. <?php do_meta_boxes( 'editcss', 'side', $safecss_post ); ?> 
  860. </div> 
  861. </div> 
  862. <br class="clear" /> 
  863. </div> 
  864. </form> 
  865. </div> 
  866. <?php 
  867.  
  868. /** 
  869. * Content width setting callback 
  870. */ 
  871. static function content_width_settings() { 
  872. $safecss_post = Jetpack_Custom_CSS::get_current_revision(); 
  873.  
  874. $custom_content_width = get_post_meta( $safecss_post['ID'], 'content_width', true ); 
  875.  
  876. // If custom content width hasn't been overridden and the theme has a content_width value, use that as a default. 
  877. if ( $custom_content_width <= 0 && ! empty( $GLOBALS['content_width'] ) ) 
  878. $custom_content_width = $GLOBALS['content_width']; 
  879.  
  880. if ( ! $custom_content_width || ( isset( $GLOBALS['content_width'] ) && $custom_content_width == $GLOBALS['content_width'] ) ) 
  881. $custom_content_width = ''; 
  882.  
  883. ?> 
  884. <div class="misc-pub-section"> 
  885. <label><?php esc_html_e( 'Media Width:', 'jetpack' ); ?></label> 
  886. <span id="content-width-display" data-default-text="<?php esc_attr_e( 'Default', 'jetpack' ); ?>" data-custom-text="<?php esc_attr_e( '%s px', 'jetpack' ); ?>"><?php echo $custom_content_width ? sprintf( esc_html__( '%s px', 'jetpack' ), $custom_content_width ) : esc_html_e( 'Default', 'jetpack' ); ?></span> 
  887. <a class="edit-content-width hide-if-no-js" href="#content-width"><?php echo esc_html_e( 'Edit', 'jetpack' ); ?></a> 
  888. <div id="content-width-select" class="hide-if-js"> 
  889. <input type="hidden" name="custom_content_width" id="custom_content_width" value="<?php echo esc_attr( $custom_content_width ); ?>" /> 
  890. <p> 
  891. <?php 
  892.  
  893. printf( 
  894. __( 'Limit width to %1$s pixels for full size images. (<a href="%2$s">More info</a>.)', 'jetpack' ),  
  895. '<input type="text" id="custom_content_width_visible" value="' . esc_attr( $custom_content_width ) . '" size="4" />',  
  896. /** 
  897. * Filter the Custom CSS limited width's support doc URL. 
  898. * @module custom-css 
  899. * @since 2.2.3 
  900. * @param string $url Custom CSS limited width's support doc URL. 
  901. */ 
  902. apply_filters( 'safecss_limit_width_link', 'http://jetpack.me/support/custom-css/#limited-width' ) 
  903. ); 
  904.  
  905. ?> 
  906. </p> 
  907. <?php 
  908.  
  909. if ( !empty( $GLOBALS['content_width'] ) && $custom_content_width != $GLOBALS['content_width'] ) { 
  910. if ( function_exists( 'wp_get_theme' ) ) 
  911. $current_theme = wp_get_theme()->Name; 
  912. else 
  913. $current_theme = get_current_theme(); 
  914.  
  915. ?> 
  916. <p><?php printf( __( 'The default content width for the %s theme is %d pixels.', 'jetpack' ), $current_theme, intval( $GLOBALS['content_width'] ) ); ?></p> 
  917. <?php 
  918.  
  919. ?> 
  920. <a class="save-content-width hide-if-no-js button" href="#content-width"><?php esc_html_e( 'OK', 'jetpack' ); ?></a> 
  921. <a class="cancel-content-width hide-if-no-js" href="#content-width"><?php esc_html_e( 'Cancel', 'jetpack' ); ?></a> 
  922. </div> 
  923. <script type="text/javascript"> 
  924. jQuery( function ( $ ) { 
  925. var defaultContentWidth = <?php echo isset( $GLOBALS['content_width'] ) ? json_encode( intval( $GLOBALS['content_width'] ) ) : 0; ?>; 
  926.  
  927. $( '.edit-content-width' ).bind( 'click', function ( e ) { 
  928. e.preventDefault(); 
  929.  
  930. $( '#content-width-select' ).slideDown(); 
  931. $( this ).hide(); 
  932. } ); 
  933.  
  934. $( '.cancel-content-width' ).bind( 'click', function ( e ) { 
  935. e.preventDefault(); 
  936.  
  937. $( '#content-width-select' ).slideUp( function () { 
  938. $( '.edit-content-width' ).show(); 
  939. $( '#custom_content_width_visible' ).val( $( '#custom_content_width' ).val() ); 
  940. } ); 
  941. } ); 
  942.  
  943. $( '.save-content-width' ).bind( 'click', function ( e ) { 
  944. e.preventDefault(); 
  945.  
  946. $( '#content-width-select' ).slideUp(); 
  947.  
  948. var newContentWidth = parseInt( $( '#custom_content_width_visible' ).val(), 10 ); 
  949.  
  950. if ( newContentWidth && newContentWidth != defaultContentWidth ) { 
  951. $( '#content-width-display' ).text( 
  952. $( '#content-width-display' ) 
  953. .data( 'custom-text' ) 
  954. .replace( '%s', $( '#custom_content_width_visible' ).val() ) 
  955. ); 
  956. else { 
  957. $( '#content-width-display' ).text( $( '#content-width-display' ).data( 'default-text' ) ); 
  958.  
  959. $( '#custom_content_width' ).val( $( '#custom_content_width_visible' ).val() ); 
  960. $( '.edit-content-width' ).show(); 
  961. } ); 
  962. } ); 
  963. </script> 
  964. </div> 
  965. <?php 
  966.  
  967. static function publish_box() { 
  968. ?> 
  969. <div id="minor-publishing"> 
  970. <div id="misc-publishing-actions"> 
  971. <?php 
  972.  
  973. /** 
  974. * Filter the array of available Custom CSS preprocessors. 
  975. * @module custom-css 
  976. * @since 2.0.3 
  977. * @param array array() Empty by default. 
  978. */ 
  979. $preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() ); 
  980.  
  981. if ( ! empty( $preprocessors ) ) { 
  982. $safecss_post = Jetpack_Custom_CSS::get_current_revision(); 
  983. $selected_preprocessor_key = get_post_meta( $safecss_post['ID'], 'custom_css_preprocessor', true ); 
  984. $selected_preprocessor = isset( $preprocessors[$selected_preprocessor_key] ) ? $preprocessors[$selected_preprocessor_key] : null; 
  985.  
  986. ?> 
  987. <div class="misc-pub-section"> 
  988. <label><?php esc_html_e( 'Preprocessor:', 'jetpack' ); ?></label> 
  989. <span id="preprocessor-display"><?php echo esc_html( $selected_preprocessor ? $selected_preprocessor['name'] : __( 'None', 'jetpack' ) ); ?></span> 
  990. <a class="edit-preprocessor hide-if-no-js" href="#preprocessor"><?php echo esc_html_e( 'Edit', 'jetpack' ); ?></a> 
  991. <div id="preprocessor-select" class="hide-if-js"> 
  992. <input type="hidden" name="custom_css_preprocessor" id="custom_css_preprocessor" value="<?php echo esc_attr( $selected_preprocessor_key ); ?>" /> 
  993. <select id="preprocessor_choices"> 
  994. <option value=""><?php esc_html_e( 'None', 'jetpack' ); ?></option> 
  995. <?php 
  996.  
  997. foreach ( $preprocessors as $preprocessor_key => $preprocessor ) { 
  998. ?> 
  999. <option value="<?php echo esc_attr( $preprocessor_key ); ?>" <?php selected( $selected_preprocessor_key, $preprocessor_key ); ?>><?php echo esc_html( $preprocessor['name'] ); ?></option> 
  1000. <?php 
  1001.  
  1002. ?> 
  1003. </select> 
  1004. <a class="save-preprocessor hide-if-no-js button" href="#preprocessor"><?php esc_html_e( 'OK', 'jetpack' ); ?></a> 
  1005. <a class="cancel-preprocessor hide-if-no-js" href="#preprocessor"><?php esc_html_e( 'Cancel', 'jetpack' ); ?></a> 
  1006. </div> 
  1007. </div> 
  1008. <?php 
  1009.  
  1010. $safecss_post = Jetpack_Custom_CSS::get_current_revision(); 
  1011.  
  1012. $add_css = ( get_post_meta( $safecss_post['ID'], 'custom_css_add', true ) != 'no' ); 
  1013.  
  1014. ?> 
  1015. <div class="misc-pub-section"> 
  1016. <label><?php esc_html_e( 'Mode:', 'jetpack' ); ?></label> 
  1017. <span id="css-mode-display"><?php echo esc_html( $add_css ? __( 'Add-on', 'jetpack' ) : __( 'Replacement', 'jetpack' ) ); ?></span> 
  1018. <a class="edit-css-mode hide-if-no-js" href="#css-mode"><?php echo esc_html_e( 'Edit', 'jetpack' ); ?></a> 
  1019. <div id="css-mode-select" class="hide-if-js"> 
  1020. <input type="hidden" name="add_to_existing" id="add_to_existing" value="<?php echo $add_css ? 'true' : 'false'; ?>" /> 
  1021. <p> 
  1022. <label> 
  1023. <input type="radio" name="add_to_existing_display" value="true" <?php checked( $add_css ); ?>/> 
  1024. <?php _e( 'Add-on CSS <b>(Recommended)</b>', 'jetpack' ); ?> 
  1025. </label> 
  1026. <br /> 
  1027. <label> 
  1028. <input type="radio" name="add_to_existing_display" value="false" <?php checked( ! $add_css ); ?>/> 
  1029. <?php printf( 
  1030. __( 'Replace <a href="%s">theme\'s CSS</a> <b>(Advanced)</b>', 'jetpack' ),  
  1031. /** 
  1032. * Filter the theme's stylesheet URL. 
  1033. * @module custom-css 
  1034. * @since 1.7.0 
  1035. * @param string $url Active theme's stylesheet URL. Default to get_stylesheet_uri(). 
  1036. */ 
  1037. apply_filters( 'safecss_theme_stylesheet_url', get_stylesheet_uri() ) 
  1038. ); ?> 
  1039. </label> 
  1040. </p> 
  1041. <a class="save-css-mode hide-if-no-js button" href="#css-mode"><?php esc_html_e( 'OK', 'jetpack' ); ?></a> 
  1042. <a class="cancel-css-mode hide-if-no-js" href="#css-mode"><?php esc_html_e( 'Cancel', 'jetpack' ); ?></a> 
  1043. </div> 
  1044. </div> 
  1045. <?php 
  1046.  
  1047. /** 
  1048. * Allows addition of elements to the submit box for custom css on the wp-admin side. 
  1049. * @module custom-css 
  1050. * @since 2.0.3 
  1051. */ 
  1052. do_action( 'custom_css_submitbox_misc_actions' ); 
  1053.  
  1054. ?> 
  1055. </div> 
  1056. </div> 
  1057. <div id="major-publishing-actions"> 
  1058. <input type="button" class="button" id="preview" name="preview" value="<?php esc_attr_e( 'Preview', 'jetpack' ) ?>" /> 
  1059. <div id="publishing-action"> 
  1060. <input type="submit" class="button-primary" id="save" name="save" value="<?php ( Jetpack_Custom_CSS::is_freetrial() ) ? esc_attr_e( 'Save & Buy Upgrade', 'jetpack' ) : esc_attr_e( 'Save Stylesheet', 'jetpack' ); ?>" /> 
  1061. </div> 
  1062. </div> 
  1063. <?php 
  1064.  
  1065. /** 
  1066. * Render metabox listing CSS revisions and the themes that correspond to the revisions. 
  1067. * Called by safecss_admin 
  1068. * @global $post 
  1069. * @param array $safecss_post 
  1070. * @uses wp_revisions_to_keep 
  1071. * @uses WP_Query 
  1072. * @uses wp_post_revision_title 
  1073. * @uses esc_html 
  1074. * @uses add_query_arg 
  1075. * @uses menu_page_url 
  1076. * @uses wp_reset_query 
  1077. * @return string 
  1078. */ 
  1079. static function revisions_meta_box( $safecss_post ) { 
  1080.  
  1081. $show_all_revisions = isset( $_GET['show_all_rev'] ); 
  1082.  
  1083. if ( function_exists( 'wp_revisions_to_keep' ) ) { 
  1084. $max_revisions = wp_revisions_to_keep( (object) $safecss_post ); 
  1085. } else { 
  1086. $max_revisions = defined( 'WP_POST_REVISIONS' ) && is_numeric( WP_POST_REVISIONS ) ? (int) WP_POST_REVISIONS : 25; 
  1087.  
  1088. $posts_per_page = $show_all_revisions ? $max_revisions : 6; 
  1089.  
  1090. $revisions = new WP_Query( array( 
  1091. 'posts_per_page' => $posts_per_page,  
  1092. 'post_type' => 'revision',  
  1093. 'post_status' => 'inherit',  
  1094. 'post_parent' => $safecss_post['ID'],  
  1095. 'orderby' => 'date',  
  1096. 'order' => 'DESC' 
  1097. ) ); 
  1098.  
  1099. if ( $revisions->have_posts() ) { ?> 
  1100. <ul class="post-revisions"><?php 
  1101.  
  1102. global $post; 
  1103.  
  1104. while ( $revisions->have_posts() ) : 
  1105. $revisions->the_post(); 
  1106.  
  1107. ?><li> 
  1108. <?php 
  1109. echo wp_post_revision_title( $post ); 
  1110.  
  1111. if ( ! empty( $post->post_excerpt ) ) 
  1112. echo ' (' . esc_html( $post->post_excerpt ) . ')'; 
  1113. ?> 
  1114. </li><?php 
  1115.  
  1116. endwhile; 
  1117.  
  1118. ?></ul><?php 
  1119.  
  1120. if ( $revisions->found_posts > 6 && !$show_all_revisions ) { 
  1121. ?> 
  1122. <br> 
  1123. <a href="<?php echo add_query_arg( 'show_all_rev', 'true', menu_page_url( 'editcss', false ) ); ?>"><?php esc_html_e( 'Show all', 'jetpack' ); ?></a> 
  1124. <?php 
  1125.  
  1126. wp_reset_query(); 
  1127.  
  1128. /** 
  1129. * Hook in init at priority 11 to disable custom CSS. 
  1130. */ 
  1131. static function disable() { 
  1132. remove_action( 'wp_head', array( 'Jetpack_Custom_CSS', 'link_tag' ), 101 ); 
  1133. remove_filter( 'stylesheet_uri', array( 'Jetpack_Custom_CSS', 'style_filter' ) ); 
  1134.  
  1135. /** 
  1136. * Reset all aspects of Custom CSS on a theme switch so that changing 
  1137. * themes is a sure-fire way to get a clean start. 
  1138. */ 
  1139. static function reset() { 
  1140. $safecss_post_id = Jetpack_Custom_CSS::save_revision( '' ); 
  1141. $safecss_revision = Jetpack_Custom_CSS::get_current_revision(); 
  1142.  
  1143. update_option( 'safecss_rev', intval( get_option( 'safecss_rev' ) ) + 1 ); 
  1144.  
  1145. update_post_meta( $safecss_post_id, 'custom_css_add', 'yes' ); 
  1146. update_post_meta( $safecss_post_id, 'content_width', false ); 
  1147. update_post_meta( $safecss_post_id, 'custom_css_preprocessor', '' ); 
  1148.  
  1149. delete_option( 'safecss_add' ); 
  1150. delete_option( 'safecss_content_width' ); 
  1151.  
  1152. update_metadata( 'post', $safecss_revision['ID'], 'custom_css_add', 'yes' ); 
  1153. update_metadata( 'post', $safecss_revision['ID'], 'content_width', false ); 
  1154. update_metadata( 'post', $safecss_revision['ID'], 'custom_css_preprocessor', '' ); 
  1155.  
  1156. delete_option( 'safecss_preview_add' ); 
  1157.  
  1158. static function is_customizer_preview() { 
  1159. if ( isset ( $GLOBALS['wp_customize'] ) ) 
  1160. return ! $GLOBALS['wp_customize']->is_theme_active(); 
  1161.  
  1162. return false; 
  1163.  
  1164. static function minify( $css, $preprocessor = '' ) { 
  1165. if ( ! $css ) 
  1166. return ''; 
  1167.  
  1168. if ( $preprocessor ) { 
  1169. /** This filter is documented in modules/custom-css/custom-css.php */ 
  1170. $preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() ); 
  1171.  
  1172. if ( isset( $preprocessors[$preprocessor] ) ) { 
  1173. $css = call_user_func( $preprocessors[$preprocessor]['callback'], $css ); 
  1174.  
  1175. safecss_class(); 
  1176. $csstidy = new csstidy(); 
  1177. $csstidy->optimise = new safecss( $csstidy ); 
  1178.  
  1179. $csstidy->set_cfg( 'remove_bslash', false ); 
  1180. $csstidy->set_cfg( 'compress_colors', true ); 
  1181. $csstidy->set_cfg( 'compress_font-weight', true ); 
  1182. $csstidy->set_cfg( 'remove_last_;', true ); 
  1183. $csstidy->set_cfg( 'case_properties', true ); 
  1184. $csstidy->set_cfg( 'discard_invalid_properties', true ); 
  1185. $csstidy->set_cfg( 'css_level', 'CSS3.0' ); 
  1186. $csstidy->set_cfg( 'template', 'highest'); 
  1187. $csstidy->parse( $css ); 
  1188.  
  1189. return $csstidy->print->plain(); 
  1190.  
  1191. /** 
  1192. * When restoring a SafeCSS post revision, also copy over the 
  1193. * content_width and custom_css_add post metadata. 
  1194. */ 
  1195. static function restore_revision( $_post_id, $_revision_id ) { 
  1196. $_post = get_post( $_post_id ); 
  1197.  
  1198. if ( 'safecss' != $_post->post_type ) 
  1199. return; 
  1200.  
  1201. $safecss_revision = Jetpack_Custom_CSS::get_current_revision(); 
  1202.  
  1203. $content_width = get_post_meta( $_revision_id, 'content_width', true ); 
  1204. $custom_css_add = get_post_meta( $_revision_id, 'custom_css_add', true ); 
  1205. $preprocessor = get_post_meta( $_revision_id, 'custom_css_preprocessor', true ); 
  1206.  
  1207. update_metadata( 'post', $safecss_revision['ID'], 'content_width', $content_width ); 
  1208. update_metadata( 'post', $safecss_revision['ID'], 'custom_css_add', $custom_css_add ); 
  1209. update_metadata( 'post', $safecss_revision['ID'], 'custom_css_preprocessor', $preprocessor ); 
  1210.  
  1211. delete_option( 'safecss_add' ); 
  1212. delete_option( 'safecss_content_width' ); 
  1213.  
  1214. update_post_meta( $_post->ID, 'content_width', $content_width ); 
  1215. update_post_meta( $_post->ID, 'custom_css_add', $custom_css_add ); 
  1216. update_post_meta( $_post->ID, 'custom_css_preprocessor', $preprocessor ); 
  1217.  
  1218. delete_option( 'safecss_preview_add' ); 
  1219.  
  1220. /** 
  1221. * Migration routine for moving safecss from wp_options to wp_posts to support revisions 
  1222. * @return void 
  1223. */ 
  1224. static function upgrade() { 
  1225. $css = get_option( 'safecss' ); 
  1226.  
  1227. if ( get_option( 'safecss_revision_migrated' ) ) { 
  1228. return false; 
  1229.  
  1230. // Check if CSS is stored in wp_options 
  1231. if ( $css ) { 
  1232. // Remove the async actions from publish_post 
  1233. remove_action( 'publish_post', 'queue_publish_post' ); 
  1234.  
  1235. $post = array(); 
  1236. $post['post_content'] = $css; 
  1237. $post['post_title'] = 'safecss'; 
  1238. $post['post_status'] = 'publish'; 
  1239. $post['post_type'] = 'safecss'; 
  1240.  
  1241. // Insert the CSS into wp_posts 
  1242. $post_id = wp_insert_post( $post ); 
  1243. // Check for errors 
  1244. if ( !$post_id or is_wp_error( $post_id ) ) 
  1245. die( $post_id->get_error_message() ); 
  1246.  
  1247. // Delete safecss option 
  1248. delete_option( 'safecss' ); 
  1249.  
  1250. unset( $css ); 
  1251.  
  1252. // Check if we have already done this 
  1253. if ( !get_option( 'safecss_revision_migrated' ) ) { 
  1254. define( 'DOING_MIGRATE', true ); 
  1255.  
  1256. // Get hashes of safecss post and current revision 
  1257. $safecss_post = Jetpack_Custom_CSS::get_post(); 
  1258.  
  1259. if ( empty( $safecss_post ) ) 
  1260. return; 
  1261.  
  1262. $safecss_post_hash = md5( $safecss_post['post_content'] ); 
  1263. $current_revision = Jetpack_Custom_CSS::get_current_revision(); 
  1264.  
  1265. if ( null == $current_revision ) 
  1266. return; 
  1267.  
  1268. $current_revision_hash = md5( $current_revision['post_content'] ); 
  1269.  
  1270. // If hashes are not equal, set safecss post with content from current revision 
  1271. if ( $safecss_post_hash !== $current_revision_hash ) { 
  1272. Jetpack_Custom_CSS::save_revision( $current_revision['post_content'] ); 
  1273. // Reset post_content to display the migrated revsion 
  1274. $safecss_post['post_content'] = $current_revision['post_content']; 
  1275.  
  1276. // Set option so that we dont keep doing this 
  1277. update_option( 'safecss_revision_migrated', time() ); 
  1278.  
  1279. $newest_safecss_post = Jetpack_Custom_CSS::get_current_revision(); 
  1280.  
  1281. if ( $newest_safecss_post ) { 
  1282. if ( get_option( 'safecss_content_width' ) ) { 
  1283. // Add the meta to the post and the latest revision. 
  1284. update_post_meta( $newest_safecss_post['ID'], 'content_width', get_option( 'safecss_content_width' ) ); 
  1285. update_metadata( 'post', $newest_safecss_post['ID'], 'content_width', get_option( 'safecss_content_width' ) ); 
  1286.  
  1287. delete_option( 'safecss_content_width' ); 
  1288.  
  1289. if ( get_option( 'safecss_add' ) ) { 
  1290. update_post_meta( $newest_safecss_post['ID'], 'custom_css_add', get_option( 'safecss_add' ) ); 
  1291. update_metadata( 'post', $newest_safecss_post['ID'], 'custom_css_add', get_option( 'safecss_add' ) ); 
  1292.  
  1293. delete_option( 'safecss_add' ); 
  1294.  
  1295. /** 
  1296. * Adds a filter to the redirect location in `wp-admin/revisions.php`. 
  1297. */ 
  1298. static function add_revision_redirect() { 
  1299. add_filter( 'wp_redirect', array( __CLASS__, 'revision_redirect' ) ); 
  1300.  
  1301. /** 
  1302. * Filters the redirect location in `wp-admin/revisions.php`. 
  1303. * @param string $location The path to redirect to. 
  1304. * @return string 
  1305. */ 
  1306. static function revision_redirect( $location ) { 
  1307. $post = get_post(); 
  1308.  
  1309. if ( ! empty( $post->post_type ) && 'safecss' == $post->post_type ) { 
  1310. $location = 'themes.php?page=editcss'; 
  1311.  
  1312. if ( 'edit.php' == $location ) { 
  1313. $location = ''; 
  1314.  
  1315. return $location; 
  1316.  
  1317. static function revision_post_link( $post_link, $post_id, $context ) { 
  1318. if ( !$post_id = (int) $post_id ) { 
  1319. return $post_link; 
  1320.  
  1321. if ( !$post = get_post( $post_id ) ) { 
  1322. return $post_link; 
  1323.  
  1324. if ( 'safecss' != $post->post_type ) { 
  1325. return $post_link; 
  1326.  
  1327. $post_link = admin_url( 'themes.php?page=editcss' ); 
  1328.  
  1329. if ( 'display' == $context ) { 
  1330. return esc_url( $post_link ); 
  1331.  
  1332. return esc_url_raw( $post_link ); 
  1333.  
  1334. /** 
  1335. * When on the edit screen, make sure the custom content width 
  1336. * setting is applied to the large image size. 
  1337. */ 
  1338. static function editor_max_image_size( $dims, $size = 'medium', $context = null ) { 
  1339. list( $width, $height ) = $dims; 
  1340.  
  1341. if ( 'large' == $size && 'edit' == $context ) 
  1342. $width = Jetpack::get_content_width(); 
  1343.  
  1344. return array( $width, $height ); 
  1345.  
  1346. /** 
  1347. * Override the content_width with a custom value if one is set. 
  1348. */ 
  1349. static function jetpack_content_width( $content_width ) { 
  1350. $custom_content_width = 0; 
  1351.  
  1352. if ( Jetpack_Custom_CSS::is_preview() ) { 
  1353. $safecss_post = Jetpack_Custom_CSS::get_current_revision(); 
  1354. $custom_content_width = intval( get_post_meta( $safecss_post['ID'], 'content_width', true ) ); 
  1355. } else if ( ! Jetpack_Custom_CSS::is_freetrial() ) { 
  1356. $custom_css_post_id = Jetpack_Custom_CSS::post_id(); 
  1357. if ( $custom_css_post_id ) 
  1358. $custom_content_width = intval( get_post_meta( $custom_css_post_id, 'content_width', true ) ); 
  1359.  
  1360. if ( $custom_content_width > 0 ) 
  1361. $content_width = $custom_content_width; 
  1362.  
  1363. return $content_width;