Nessuna descrizione

custom-css.php 60KB

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