custom-css.php 59 KB

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