Completed
Pull Request — add/telegram-sharebuttons (#3775)
by
unknown
30:50 queued 21:07
created

Jetpack_Custom_CSS   D

Complexity

Total Complexity 212

Size/Duplication

Total Lines 1565
Duplicated Lines 2.62 %

Coupling/Cohesion

Components 1
Dependencies 4
Metric Value
wmc 212
lcom 1
cbo 4
dl 41
loc 1565
rs 4.4102

42 Methods

Rating   Name   Duplication   Size   Complexity  
A is_preview() 0 3 2
A get_preprocessor_key() 0 4 1
A should_we_inline_custom_css() 0 4 2
A buffer() 0 4 1
A preview_flag() 0 48 3
A update_title() 0 4 1
A prettify_post_revisions() 0 3 1
A post_title() 0 15 4
A enqueue_scripts() 0 16 4
A saved_message() 0 3 1
A disable() 0 4 1
A add_revision_redirect() 0 3 1
A revision_redirect() 0 13 4
D init() 0 138 27
C save() 0 168 12
A get_post() 0 8 2
B post_id() 0 43 5
A get_current_revision() 0 19 3
B save_revision() 14 61 7
C skip_stylesheet() 0 42 7
B set_content_width() 0 8 5
A is_freetrial() 0 13 1
A get_preprocessor() 0 7 2
F get_css() 0 88 17
A replace_insecure_urls() 0 8 2
A print_css() 0 13 1
F link_tag() 14 119 19
B style_filter() 0 17 5
A preview_links() 0 9 2
A menu() 0 8 1
B admin() 0 62 5
C content_width_settings() 0 102 11
C publish_box() 0 108 8
C revisions_meta_box() 0 52 10
A reset() 0 19 1
A is_customizer_preview() 0 6 2
B minify() 0 29 4
B restore_revision() 0 25 2
C upgrade() 13 78 12
B revision_post_link() 0 21 5
A editor_max_image_size() 0 8 3
B jetpack_content_width() 0 17 5

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Jetpack_Custom_CSS often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Custom_CSS, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
class Jetpack_Custom_CSS {
4
	static function init() {
5
		add_action( 'switch_theme', array( __CLASS__, 'reset' ) );
6
		add_action( 'wp_restore_post_revision', array( __CLASS__, 'restore_revision' ), 10, 2 );
7
8
		// Save revisions for posts of type safecss.
9
		add_action( 'load-revision.php', array( __CLASS__, 'add_revision_redirect' ) );
10
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
14
		// Overwrite the content width global variable if one is set in the custom css
15
		add_action( 'template_redirect', array( __CLASS__, 'set_content_width' ) );
16
		add_action( 'admin_init', array( __CLASS__, 'set_content_width' ) );
17
18
		if ( ! is_admin() )
19
			add_filter( 'stylesheet_uri', array( __CLASS__, 'style_filter' ) );
20
21
		define(
22
			'SAFECSS_USE_ACE',
23
			! jetpack_is_mobile() &&
24
			! Jetpack_User_Agent_Info::is_ipad() &&
25
			/**
26
			 * Should the Custom CSS module use ACE to process CSS.
27
			 * @see http://ace.c9.io/
28
			 *
29
			 * @module custom-css
30
			 *
31
			 * @since 1.7.0
32
			 *
33
			 * @param bool true Use ACE to process the Custom CSS. Default to true.
34
			 */
35
			apply_filters( 'safecss_use_ace', true )
36
		);
37
38
		// Register safecss as a custom post_type
39
		// 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.
40
		register_post_type( 'safecss', array(
41
	//		These are the defaults
42
	//		'exclude_from_search' => true,
43
	//		'public' => false,
44
	//		'publicly_queryable' => false,
45
	//		'show_ui' => false,
46
			'supports' => array( 'revisions' ),
47
			'label' => 'Custom CSS',
48
			'can_export' => false,
49
			'rewrite' => false,
50
			'capabilities' => array(
51
				'edit_post' => 'edit_theme_options',
52
				'read_post' => 'read',
53
				'delete_post' => 'edit_theme_options',
54
				'edit_posts' => 'edit_theme_options',
55
				'edit_others_posts' => 'edit_theme_options',
56
				'publish_posts' => 'edit_theme_options',
57
				'read_private_posts' => 'read'
58
			)
59
		) );
60
61
		// Short-circuit WP if this is a CSS stylesheet request
62
		if ( isset( $_GET['custom-css'] ) ) {
63
			header( 'Content-Type: text/css', true, 200 );
64
			header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 31536000) . ' GMT' ); // 1 year
65
			Jetpack_Custom_CSS::print_css();
66
			exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method init() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
67
		}
68
69
		add_action( 'admin_enqueue_scripts', array( 'Jetpack_Custom_CSS', 'enqueue_scripts' ) );
70
71
		if ( isset( $_GET['page'] ) && 'editcss' == $_GET['page'] && is_admin() ) {
72
			// Do migration routine if necessary
73
			Jetpack_Custom_CSS::upgrade();
74
75
			/**
76
			 * Allows additional work when migrating safecss from wp_options to wp_post.
77
			 *
78
			 * @module custom-css
79
			 *
80
			 * @since 1.7.0
81
			 */
82
			do_action( 'safecss_migrate_post' );
83
		}
84
85
		/**
86
		 * Never embed the style in the head on wpcom.
87
		 * Yes, this filter should be added to an unsynced file on wpcom, but
88
		 * there is no good syntactically-correct location to put it yet.
89
		 * @link https://github.com/Automattic/jetpack/commit/a1be114e9179f64d147124727a58e2cf76c7e5a1#commitcomment-7763921
90
		 */
91
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
92
			add_filter( 'safecss_embed_style', '__return_false' );
93
		} else {
94
			add_filter( 'safecss_embed_style', array( 'Jetpack_Custom_CSS', 'should_we_inline_custom_css' ), 10, 2 );
95
		}
96
97
		add_action( 'wp_head', array( 'Jetpack_Custom_CSS', 'link_tag' ), 101 );
98
99
		add_filter( 'jetpack_content_width', array( 'Jetpack_Custom_CSS', 'jetpack_content_width' ) );
100
		add_filter( 'editor_max_image_size', array( 'Jetpack_Custom_CSS', 'editor_max_image_size' ), 10, 3 );
101
102
		if ( !current_user_can( 'switch_themes' ) && !is_super_admin() )
103
			return;
104
105
		add_action( 'admin_menu', array( 'Jetpack_Custom_CSS', 'menu' ) );
106
107
		if ( isset( $_POST['safecss'] ) && false == strstr( $_SERVER[ 'REQUEST_URI' ], 'options.php' ) ) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strstr($_SERVER['REQUEST_URI'], 'options.php') of type string to the boolean false. If you are specifically checking for an empty string, consider using the more explicit === '' instead.
Loading history...
108
			check_admin_referer( 'safecss' );
109
110
			$save_result = self::save( array(
111
				'css' => stripslashes( $_POST['safecss'] ),
112
				'is_preview' => isset( $_POST['action'] ) && $_POST['action'] == 'preview',
113
				'preprocessor' => isset( $_POST['custom_css_preprocessor'] ) ? $_POST['custom_css_preprocessor'] : '',
114
				'add_to_existing' => isset( $_POST['add_to_existing'] ) ? $_POST['add_to_existing'] == 'true' : true,
115
				'content_width' => isset( $_POST['custom_content_width'] ) ? $_POST['custom_content_width'] : false,
116
			) );
117
118
			if ( $_POST['action'] == 'preview' ) {
119
				wp_safe_redirect( add_query_arg( 'csspreview', 'true', get_option( 'home' ) ) );
120
				exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method init() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
121
			}
122
123
			if ( $save_result )
124
				add_action( 'admin_notices', array( 'Jetpack_Custom_CSS', 'saved_message' ) );
125
		}
126
127
		// Prevent content filters running on CSS when restoring revisions
128
		if ( isset( $_REQUEST[ 'action' ] ) && 'restore' === $_REQUEST[ 'action' ] && false !== strstr( $_SERVER[ 'REQUEST_URI' ], 'revision.php' ) ) {
129
			$parent_post = get_post( wp_get_post_parent_id( intval( $_REQUEST[ 'revision' ] ) ) );
130
			if ( $parent_post && ! is_wp_error( $parent_post ) && 'safecss' === $parent_post->post_type ) {
131
				// Remove wp_filter_post_kses, this causes CSS escaping issues
132
				remove_filter( 'content_save_pre', 'wp_filter_post_kses' );
133
				remove_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' );
134
				remove_all_filters( 'content_save_pre' );
135
			}
136
		}
137
138
		// Modify all internal links so that preview state persists
139
		if ( Jetpack_Custom_CSS::is_preview() )
140
			ob_start( array( 'Jetpack_Custom_CSS', 'buffer' ) );
141
	}
142
143
	/**
144
	 * Save new custom CSS. This should be the entry point for any third-party code using Jetpack_Custom_CSS
145
	 * to save CSS.
146
	 *
147
	 * @param array $args Array of arguments:
148
	 *        string $css The CSS (or LESS or Sass)
149
	 *        bool $is_preview Whether this CSS is preview or published
150
	 *        string preprocessor Which CSS preprocessor to use
151
	 *        bool $add_to_existing Whether this CSS replaces the theme's CSS or supplements it.
152
	 *        int $content_width A custom $content_width to go along with this CSS.
153
	 * @return int The post ID of the saved Custom CSS post.
154
	 */
155
	public static function save( $args = array() ) {
156
		$defaults = array(
157
			'css' => '',
158
			'is_preview' => false,
159
			'preprocessor' => '',
160
			'add_to_existing' => true,
161
			'content_width' => false,
162
		);
163
164
		$args = wp_parse_args( $args, $defaults );
165
166
		if ( $args['content_width'] && intval( $args['content_width']) > 0 && ( ! isset( $GLOBALS['content_width'] ) || $args['content_width'] != $GLOBALS['content_width'] ) )
167
			$args['content_width'] = intval( $args['content_width'] );
168
		else
169
			$args['content_width'] = false;
170
171
		// Remove wp_filter_post_kses, this causes CSS escaping issues
172
		remove_filter( 'content_save_pre', 'wp_filter_post_kses' );
173
		remove_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' );
174
		remove_all_filters( 'content_save_pre' );
175
176
		/**
177
		 * Fires prior to saving custom css values. Necessitated because the
178
		 * core WordPress save_pre filters were removed:
179
		 * - content_save_pre
180
		 * - content_filtered_save_pre
181
		 *
182
		 * @module custom-css
183
		 *
184
		 * @since 1.7.0
185
		 *
186
		 * @param array $args {
187
		 * Array of custom CSS arguments.
188
		 * 	@type string $css The CSS (or LESS or Sass).
189
		 * 	@type bool $is_preview Whether this CSS is preview or published.
190
		 * 	@type string preprocessor Which CSS preprocessor to use.
191
		 * 	@type bool $add_to_existing Whether this CSS replaces the theme's CSS or supplements it.
192
		 * 	@type int $content_width A custom $content_width to go along with this CSS.
193
		 * }
194
		 */
195
		do_action( 'safecss_save_pre', $args );
196
197
		$warnings = array();
198
199
		safecss_class();
200
		$csstidy = new csstidy();
201
		$csstidy->optimise = new safecss( $csstidy );
202
203
		$csstidy->set_cfg( 'remove_bslash',              false );
204
		$csstidy->set_cfg( 'compress_colors',            false );
205
		$csstidy->set_cfg( 'compress_font-weight',       false );
206
		$csstidy->set_cfg( 'optimise_shorthands',        0 );
207
		$csstidy->set_cfg( 'remove_last_;',              false );
208
		$csstidy->set_cfg( 'case_properties',            false );
209
		$csstidy->set_cfg( 'discard_invalid_properties', true );
210
		$csstidy->set_cfg( 'css_level',                  'CSS3.0' );
211
		$csstidy->set_cfg( 'preserve_css',               true );
212
		$csstidy->set_cfg( 'template',                   dirname( __FILE__ ) . '/csstidy/wordpress-standard.tpl' );
213
214
		$css = $orig = $args['css'];
0 ignored issues
show
Unused Code introduced by
$orig is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
215
216
		$css = preg_replace( '/\\\\([0-9a-fA-F]{4})/', '\\\\\\\\$1', $prev = $css );
217
218
		if ( $css != $prev )
219
			$warnings[] = 'preg_replace found stuff';
220
221
		// Some people put weird stuff in their CSS, KSES tends to be greedy
222
		$css = str_replace( '<=', '&lt;=', $css );
223
		// Why KSES instead of strip_tags?  Who knows?
224
		$css = wp_kses_split( $prev = $css, array(), array() );
225
		$css = str_replace( '&gt;', '>', $css ); // kses replaces lone '>' with &gt;
226
		// Why both KSES and strip_tags?  Because we just added some '>'.
227
		$css = strip_tags( $css );
228
229
		if ( $css != $prev )
230
			$warnings[] = 'kses found stuff';
231
232
		// if we're not using a preprocessor
233
		if ( ! $args['preprocessor'] ) {
234
235
			/**
236
			 * Fires before parsing the css with CSSTidy, but only if
237
			 * the preprocessor is not configured for use.
238
			 *
239
			 * @module custom-css
240
			 *
241
			 * @since 1.7.0
242
			 *
243
			 * @param obj $csstidy The csstidy object.
244
			 * @param string $css Custom CSS.
245
			 * @param array $args Array of custom CSS arguments.
246
			 */
247
			do_action( 'safecss_parse_pre', $csstidy, $css, $args );
248
249
			$csstidy->parse( $css );
250
251
			/**
252
			 * Fires after parsing the css with CSSTidy, but only if
253
			 * the preprocessor is not cinfigured for use.
254
			 *
255
			 * @module custom-css
256
			 *
257
			 * @since 1.7.0
258
			 *
259
			 * @param obj $csstidy The csstidy object.
260
			 * @param array $warnings Array of warnings.
261
			 * @param array $args Array of custom CSS arguments.
262
			 */
263
			do_action( 'safecss_parse_post', $csstidy, $warnings, $args );
264
265
			$css = $csstidy->print->plain();
266
		}
267
268
		if ( $args['add_to_existing'] )
269
			$add_to_existing = 'yes';
270
		else
271
			$add_to_existing = 'no';
272
273
		if ( $args['is_preview'] || Jetpack_Custom_CSS::is_freetrial() ) {
274
			// Save the CSS
275
			$safecss_revision_id = Jetpack_Custom_CSS::save_revision( $css, true, $args['preprocessor'] );
276
277
			// Cache Buster
278
			update_option( 'safecss_preview_rev', intval( get_option( 'safecss_preview_rev' ) ) + 1);
279
280
			update_metadata( 'post', $safecss_revision_id, 'custom_css_add', $add_to_existing );
281
			update_metadata( 'post', $safecss_revision_id, 'content_width', $args['content_width'] );
282
			update_metadata( 'post', $safecss_revision_id, 'custom_css_preprocessor', $args['preprocessor'] );
283
284
			delete_option( 'safecss_add' );
285
			delete_option( 'safecss_content_width' );
286
287
			if ( $args['is_preview'] ) {
288
				return $safecss_revision_id;
289
			}
290
291
			/**
292
			 * Fires after saving Custom CSS.
293
			 *
294
			 * @module custom-css
295
			 *
296
			 * @since 1.7.0
297
			 */
298
			do_action( 'safecss_save_preview_post' );
299
		}
300
301
		// Save the CSS
302
		$safecss_post_id = Jetpack_Custom_CSS::save_revision( $css, false, $args['preprocessor'] );
303
304
		$safecss_post_revision = Jetpack_Custom_CSS::get_current_revision();
305
306
		update_option( 'safecss_rev', intval( get_option( 'safecss_rev' ) ) + 1 );
307
308
		update_post_meta( $safecss_post_id, 'custom_css_add', $add_to_existing );
309
		update_post_meta( $safecss_post_id, 'content_width', $args['content_width'] );
310
		update_post_meta( $safecss_post_id, 'custom_css_preprocessor', $args['preprocessor'] );
311
312
		delete_option( 'safecss_add' );
313
		delete_option( 'safecss_content_width' );
314
315
		update_metadata( 'post', $safecss_post_revision['ID'], 'custom_css_add', $add_to_existing );
316
		update_metadata( 'post', $safecss_post_revision['ID'], 'content_width', $args['content_width'] );
317
		update_metadata( 'post', $safecss_post_revision['ID'], 'custom_css_preprocessor', $args['preprocessor'] );
318
319
		delete_option( 'safecss_preview_add' );
320
321
		return $safecss_post_id;
322
	}
323
324
	/**
325
	 * Get the published custom CSS post.
326
	 *
327
	 * @return array
328
	 */
329
	static function get_post() {
330
		$custom_css_post_id = Jetpack_Custom_CSS::post_id();
331
332
		if ( $custom_css_post_id )
333
			return get_post( $custom_css_post_id, ARRAY_A );
334
335
		return array();
336
	}
337
338
	/**
339
	 * Get the post ID of the published custom CSS post.
340
	 *
341
	 * @return int|bool The post ID if it exists; false otherwise.
342
	 */
343
	static function post_id() {
344
		/**
345
		 * Filter the ID of the post where Custom CSS is stored, before the ID is retrieved.
346
		 *
347
		 * If the callback function returns a non-null value, then post_id() will immediately
348
		 * return that value, instead of retrieving the normal post ID.
349
		 *
350
		 * @module custom-css
351
		 *
352
		 * @since 3.8.1
353
		 *
354
		 * @param null null The ID to return instead of the normal ID.
355
		 */
356
		$custom_css_post_id = apply_filters( 'jetpack_custom_css_pre_post_id', null );
357
		if ( ! is_null( $custom_css_post_id ) ) {
358
			return $custom_css_post_id;
359
		}
360
361
		$custom_css_post_id = wp_cache_get( 'custom_css_post_id' );
362
363
		if ( false === $custom_css_post_id ) {
364
			$custom_css_posts = get_posts( array(
365
				'posts_per_page' => 1,
366
				'post_type' => 'safecss',
367
				'post_status' => 'publish',
368
				'orderby' => 'date',
369
				'order' => 'DESC'
370
			) );
371
372
			if ( count( $custom_css_posts ) > 0 )
373
				$custom_css_post_id = $custom_css_posts[0]->ID;
374
			else
375
				$custom_css_post_id = 0;
376
377
			// Save post_id=0 to note that no safecss post exists.
378
			wp_cache_set( 'custom_css_post_id', $custom_css_post_id );
379
		}
380
381
		if ( ! $custom_css_post_id )
382
			return false;
383
384
		return $custom_css_post_id;
385
	}
386
387
	/**
388
	 * Get the current revision of the original safecss record
389
	 *
390
	 * @return object
391
	 */
392
	static function get_current_revision() {
393
		$safecss_post = Jetpack_Custom_CSS::get_post();
394
395
		if ( empty( $safecss_post ) ) {
396
			return false;
397
		}
398
399
		$revisions = wp_get_post_revisions( $safecss_post['ID'], array( 'posts_per_page' => 1, 'orderby' => 'date', 'order' => 'DESC' ) );
400
401
		// Empty array if no revisions exist
402
		if ( empty( $revisions ) ) {
403
			// Return original post
404
			return $safecss_post;
405
		} else {
406
			// Return the first entry in $revisions, this will be the current revision
407
			$current_revision = get_object_vars( array_shift( $revisions ) );
408
			return $current_revision;
409
		}
410
	}
411
412
	/**
413
	 * Save new revision of CSS
414
	 * Checks to see if content was modified before really saving
415
	 *
416
	 * @param string $css
417
	 * @param bool $is_preview
418
	 * @return bool|int If nothing was saved, returns false. If a post
419
	 *                  or revision was saved, returns the post ID.
420
	 */
421
	static function save_revision( $css, $is_preview = false, $preprocessor = '' ) {
422
		$safecss_post = Jetpack_Custom_CSS::get_post();
423
424
		$compressed_css = Jetpack_Custom_CSS::minify( $css, $preprocessor );
425
426
		// If null, there was no original safecss record, so create one
427
		if ( null == $safecss_post ) {
428
			if ( ! $css )
429
				return false;
430
431
			$post = array();
432
			$post['post_content'] = $css;
433
			$post['post_title'] = 'safecss';
434
			$post['post_status'] = 'publish';
435
			$post['post_type'] = 'safecss';
436
			$post['post_content_filtered'] = $compressed_css;
437
438
			// Set excerpt to current theme, for display in revisions list
439 View Code Duplication
			if ( function_exists( 'wp_get_theme' ) ) {
440
				$current_theme = wp_get_theme();
441
				$post['post_excerpt'] = $current_theme->Name;
442
			}
443
			else {
444
				$post['post_excerpt'] = get_current_theme();
445
			}
446
447
			// Insert the CSS into wp_posts
448
			$post_id = wp_insert_post( $post );
449
			wp_cache_set( 'custom_css_post_id', $post_id );
450
			return $post_id;
451
		}
452
453
		// Update CSS in post array with new value passed to this function
454
		$safecss_post['post_content'] = $css;
455
		$safecss_post['post_content_filtered'] = $compressed_css;
456
457
		// Set excerpt to current theme, for display in revisions list
458 View Code Duplication
		if ( function_exists( 'wp_get_theme' ) ) {
459
			$current_theme = wp_get_theme();
460
			$safecss_post['post_excerpt'] = $current_theme->Name;
461
		}
462
		else {
463
			$safecss_post['post_excerpt'] = get_current_theme();
464
		}
465
466
		// Don't carry over last revision's timestamps, otherwise revisions all have matching timestamps
467
		unset( $safecss_post['post_date'] );
468
		unset( $safecss_post['post_date_gmt'] );
469
		unset( $safecss_post['post_modified'] );
470
		unset( $safecss_post['post_modified_gmt'] );
471
472
		// Do not update post if we are only saving a preview
473
		if ( false === $is_preview ) {
474
			$post_id = wp_update_post( $safecss_post );
475
			wp_cache_set( 'custom_css_post_id', $post_id );
476
			return $post_id;
477
		}
478
		else if ( ! defined( 'DOING_MIGRATE' ) ) {
479
			return _wp_put_post_revision( $safecss_post );
480
		}
481
	}
482
483
	static function skip_stylesheet() {
484
		/**
485
		 * Prevent the Custom CSS stylesheet from being enqueued.
486
		 *
487
		 * @module custom-css
488
		 *
489
		 * @since 2.2.1
490
		 *
491
		 * @param null Should the stylesheet be skipped. Default to null. Anything else will force the stylesheet to be skipped.
492
		 */
493
		$skip_stylesheet = apply_filters( 'safecss_skip_stylesheet', null );
494
495
		if ( null !== $skip_stylesheet ) {
496
			return $skip_stylesheet;
497
		} elseif ( Jetpack_Custom_CSS::is_customizer_preview() ) {
498
			return false;
499
		} else {
500
			if ( Jetpack_Custom_CSS::is_preview() ) {
501
				$safecss_post = Jetpack_Custom_CSS::get_current_revision();
502
503
				if ( $safecss_post )
504
					return (bool) ( get_post_meta( $safecss_post['ID'], 'custom_css_add', true ) == 'no' );
505
				else
506
					return (bool) ( get_option( 'safecss_preview_add' ) == 'no' );
507
			}
508
			else {
509
				$custom_css_post_id = Jetpack_Custom_CSS::post_id();
510
511
				if ( $custom_css_post_id ) {
512
					$custom_css_add = get_post_meta( $custom_css_post_id, 'custom_css_add', true );
513
514
					// It is possible for the CSS to be stored in a post but for the safecss_add option
515
					// to have not been upgraded yet if the user hasn't opened their Custom CSS editor
516
					// since October 2012.
517
					if ( ! empty( $custom_css_add ) )
518
						return (bool) ( $custom_css_add === 'no' );
519
				}
520
521
				return (bool) ( get_option( 'safecss_add' ) == 'no' );
522
			}
523
		}
524
	}
525
526
	static function is_preview() {
527
		return isset( $_GET['csspreview'] ) && $_GET['csspreview'] === 'true';
528
	}
529
530
	/**
531
	 * Currently this filter function gets called on
532
	 * 'template_redirect' action and
533
	 * 'admin_init' action
534
	 */
535
	static function set_content_width(){
536
		// Don't apply this filter on the Edit CSS page
537
		if ( isset( $_GET ) && isset( $_GET['page'] ) &&  'editcss' == $_GET['page'] && is_admin() ) {
538
			return;
539
		}
540
541
		$GLOBALS['content_width'] = Jetpack::get_content_width();
542
	}
543
544
	/*
545
	 * False when the site has the Custom Design upgrade.
546
	 * Used only on WordPress.com.
547
	 */
548
	static function is_freetrial() {
549
		/**
550
		 * Determine if a WordPress.com site uses a Free trial of the Custom Design Upgrade.
551
		 * Used only on WordPress.com.
552
		 *
553
		 * @module custom-css
554
		 *
555
		 * @since 1.7.0
556
		 *
557
		 * @param bool false Does the site use a Free trial of the Custom Design Upgrade. Default to false.
558
		 */
559
		return apply_filters( 'safecss_is_freetrial', false );
560
	}
561
562
	static function get_preprocessor_key() {
563
		$safecss_post = Jetpack_Custom_CSS::get_current_revision();
564
		return get_post_meta( $safecss_post['ID'], 'custom_css_preprocessor', true );
565
	}
566
567
	static function get_preprocessor() {
568
		/** This filter is documented in modules/custom-css/custom-css.php */
569
		$preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() );
570
		$selected_preprocessor_key = self::get_preprocessor_key();
571
		$selected_preprocessor = isset( $preprocessors[ $selected_preprocessor_key ] ) ? $preprocessors[ $selected_preprocessor_key ] : null;
572
		return $selected_preprocessor;
573
	}
574
575
	static function get_css( $compressed = false ) {
576
		/**
577
		 * Filter the Custom CSS returned.
578
		 * Can be used to return an error, or no CSS at all.
579
		 *
580
		 * @module custom-css
581
		 *
582
		 * @since 1.7.0
583
		 *
584
		 * @param bool false Should we return an error instead of the Custom CSS. Default to false.
585
		 */
586
		$default_css = apply_filters( 'safecss_get_css_error', false );
587
588
		if ( $default_css !== false )
589
			return $default_css;
590
591
		$option = ( Jetpack_Custom_CSS::is_preview() || Jetpack_Custom_CSS::is_freetrial() ) ? 'safecss_preview' : 'safecss';
592
		$css = '';
593
594
		if ( 'safecss' == $option ) {
595
			// Don't bother checking for a migrated 'safecss' option if it never existed.
596
			if ( false === get_option( 'safecss' ) || get_option( 'safecss_revision_migrated' ) ) {
597
				$safecss_post = Jetpack_Custom_CSS::get_post();
598
				if ( ! empty( $safecss_post ) ) {
599
					$css = ( $compressed && $safecss_post['post_content_filtered'] ) ? $safecss_post['post_content_filtered'] : $safecss_post['post_content'];
600
				}
601
			} else {
602
				$current_revision = Jetpack_Custom_CSS::get_current_revision();
603
				if ( false === $current_revision ) {
604
					$css = '';
605
				} else {
606
					$css = ( $compressed && $current_revision['post_content_filtered'] ) ? $current_revision['post_content_filtered'] : $current_revision['post_content'];
607
				}
608
			}
609
610
			// Fix for un-migrated Custom CSS
611
			if ( empty( $safecss_post ) ) {
612
				$_css = get_option( 'safecss' );
613
				if ( !empty( $_css ) ) {
614
					$css = $_css;
615
				}
616
			}
617
		}
618
		else if ( 'safecss_preview' == $option ) {
619
			$safecss_post = Jetpack_Custom_CSS::get_current_revision();
620
			$css = $safecss_post['post_content'];
621
			$css = stripslashes( $css );
622
			$css = Jetpack_Custom_CSS::minify( $css, get_post_meta( $safecss_post['ID'], 'custom_css_preprocessor', true ) );
623
		}
624
625
		$css = str_replace( array( '\\\00BB \\\0020', '\0BB \020', '0BB 020' ), '\00BB \0020', $css );
626
627
		if ( empty( $css ) ) {
628
			$css = "/*\n"
629
				. wordwrap(
630
					/**
631
					 * Filter the default message displayed in the Custom CSS editor.
632
					 *
633
					 * @module custom-css
634
					 *
635
					 * @since 1.7.0
636
					 *
637
					 * @param string $str Default Custom CSS editor content.
638
					 */
639
					apply_filters(
640
						'safecss_default_css',
641
						__(
642
							"Welcome to Custom CSS!\n\nTo learn how this works, see http://wp.me/PEmnE-Bt",
643
							'jetpack'
644
						)
645
					)
646
				)
647
				. "\n*/";
648
		}
649
650
		/**
651
		 * Filter the Custom CSS returned from the editor.
652
		 *
653
		 * @module custom-css
654
		 *
655
		 * @since 1.7.0
656
		 *
657
		 * @param string $css Custom CSS.
658
		 */
659
		$css = apply_filters( 'safecss_css', $css );
660
661
		return $css;
662
	}
663
664
	static function replace_insecure_urls( $css ) {
665
		if ( ! function_exists( '_sa_get_frontend_https_url_replacement_map' ) ) {
666
			return $css;
667
		}
668
		list( $http_urls, $secure_urls ) = _sa_get_frontend_https_url_replacement_map();
669
670
		return str_replace( $http_urls, $secure_urls, $css );
671
	}
672
673
	static function print_css() {
674
675
		/**
676
		 * Fires right before printing the custom CSS inside the <head> element.
677
		 *
678
		 * @module custom-css
679
		 *
680
		 * @since 1.7.0
681
		 */
682
		do_action( 'safecss_print_pre' );
683
		$css = Jetpack_Custom_CSS::get_css( true );
684
		echo self::replace_insecure_urls( $css );
685
	}
686
687
	static function should_we_inline_custom_css( $should_we, $css ) {
688
		// If the CSS is less than 2,000 characters, inline it! otherwise return what was passed in.
689
		return ( strlen( $css ) < 2000 ) ? true : $should_we;
690
	}
691
692
	static function link_tag() {
693
		global $blog_id, $current_blog;
694
695
		if (
696
			/**
697
			 * Do not include any CSS on the page if the CSS includes an error.
698
			 * Setting this filter to true stops any Custom CSS from being enqueued.
699
			 *
700
			 * @module custom-css
701
			 *
702
			 * @since 1.7.0
703
			 *
704
			 * @param bool false Does the CSS include an error. Default to false.
705
			 */
706
			apply_filters( 'safecss_style_error', false )
707
		) {
708
			return;
709
		}
710
711
		if ( ! is_super_admin() && isset( $current_blog ) && ( 1 == $current_blog->spam || 1 == $current_blog->deleted ) )
712
			return;
713
714
		if ( Jetpack_Custom_CSS::is_customizer_preview() )
715
			return;
716
717
		$css    = '';
718
		$option = Jetpack_Custom_CSS::is_preview() ? 'safecss_preview' : 'safecss';
719
720
		if ( 'safecss' == $option ) {
721
			if ( get_option( 'safecss_revision_migrated' ) ) {
722
				$safecss_post = Jetpack_Custom_CSS::get_post();
723
724
				if ( ! empty( $safecss_post['post_content'] ) ) {
725
					$css = $safecss_post['post_content'];
726
				}
727 View Code Duplication
			} else {
728
				$current_revision = Jetpack_Custom_CSS::get_current_revision();
729
730
				if ( ! empty( $current_revision['post_content'] ) ) {
731
					$css = $current_revision['post_content'];
732
				}
733
			}
734
735
			// Fix for un-migrated Custom CSS
736
			if ( empty( $safecss_post ) ) {
737
				$_css = get_option( 'safecss' );
738
				if ( !empty( $_css ) ) {
739
					$css = $_css;
740
				}
741
			}
742
		}
743
744 View Code Duplication
		if ( 'safecss_preview' == $option ) {
745
			$safecss_post = Jetpack_Custom_CSS::get_current_revision();
746
747
			if ( !empty( $safecss_post['post_content'] ) ) {
748
				$css = $safecss_post['post_content'];
749
			}
750
		}
751
752
		$css = str_replace( array( '\\\00BB \\\0020', '\0BB \020', '0BB 020' ), '\00BB \0020', $css );
753
754
		if ( $css == '' )
755
			return;
756
757
		if (
758
			/**
759
			 * Allow inserting CSS inline instead of through a separate file.
760
			 *
761
			 * @module custom-css
762
			 *
763
			 * @since 3.4.0
764
			 *
765
			 * @param bool false Should the CSS be added inline instead of through a separate file. Default to false.
766
			 * @param string $css Custom CSS.
767
			 */
768
			apply_filters( 'safecss_embed_style', false, $css )
769
		) {
770
771
			echo "\r\n" . '<style id="custom-css-css">' . Jetpack_Custom_CSS::get_css( true ) . "</style>\r\n";
772
773
		} else {
774
775
			$href = home_url( '/' );
776
			$href = add_query_arg( 'custom-css', 1, $href );
777
			$href = add_query_arg( 'csblog', $blog_id, $href );
778
			$href = add_query_arg( 'cscache', 6, $href );
779
			$href = add_query_arg( 'csrev', (int) get_option( $option . '_rev' ), $href );
780
781
			/**
782
			 * Filter the Custom CSS link enqueued in the head.
783
			 *
784
			 * @module custom-css
785
			 *
786
			 * @since 1.7.0
787
			 *
788
			 * @param string $href Custom CSS link enqueued in the head.
789
			 * @param string $blog_id Blog ID.
790
			 */
791
			$href = apply_filters( 'safecss_href', $href, $blog_id );
792
793
			if ( Jetpack_Custom_CSS::is_preview() )
794
				$href = add_query_arg( 'csspreview', 'true', $href );
795
796
			?>
797
			<link rel="stylesheet" id="custom-css-css" type="text/css" href="<?php echo esc_url( $href ); ?>" />
798
			<?php
799
800
		}
801
802
		/**
803
		 * Fires after creating the <link> in the <head> element for the custom css stylesheet.
804
		 *
805
		 * @module custom-css
806
		 *
807
		 * @since 2.2.2
808
		 */
809
		do_action( 'safecss_link_tag_post' );
810
	}
811
812
	static function style_filter( $current ) {
813
		if ( Jetpack_Custom_CSS::is_freetrial() && ( ! Jetpack_Custom_CSS::is_preview() || ! current_user_can( 'switch_themes' ) ) )
814
			return $current;
815
		else if ( Jetpack_Custom_CSS::skip_stylesheet() )
816
			/**
817
			 * Filter the default blank Custom CSS URL.
818
			 *
819
			 * @module custom-css
820
			 *
821
			 * @since 2.2.1
822
			 *
823
			 * @param string $url Default blank Custom CSS URL.
824
			 */
825
			return apply_filters( 'safecss_style_filter_url', plugins_url( 'custom-css/css/blank.css', __FILE__ ) );
826
827
		return $current;
828
	}
829
830
	static function buffer( $html ) {
831
		$html = str_replace( '</body>', Jetpack_Custom_CSS::preview_flag(), $html );
832
		return preg_replace_callback( '!href=([\'"])(.*?)\\1!', array( 'Jetpack_Custom_CSS', 'preview_links' ), $html );
833
	}
834
835
	static function preview_links( $matches ) {
836
		if ( 0 !== strpos( $matches[2], get_option( 'home' ) ) )
837
			return $matches[0];
838
839
		$link = wp_specialchars_decode( $matches[2] );
840
		$link = add_query_arg( 'csspreview', 'true', $link );
841
		$link = esc_url( $link );
842
		return "href={$matches[1]}$link{$matches[1]}";
843
	}
844
845
	/**
846
	 * Places a black bar above every preview page
847
	 */
848
	static function preview_flag() {
849
		if ( is_admin() )
850
			return;
851
852
		$message = esc_html__( 'Preview: changes must be saved or they will be lost', 'jetpack' );
853
		/**
854
		 * Filter the Preview message displayed on the site when previewing custom CSS, before to save it.
855
		 *
856
		 * @module custom-css
857
		 *
858
		 * @since 1.7.0
859
		 *
860
		 * @param string $message Custom CSS preview message.
861
		 */
862
		$message = apply_filters( 'safecss_preview_message', $message );
863
864
		$preview_flag_js = "var flag = document.createElement('div');
865
		flag.innerHTML = " . json_encode( $message ) . ";
866
		flag.style.background = '#FF6600';
867
		flag.style.color = 'white';
868
		flag.style.textAlign = 'center';
869
		flag.style.fontSize = '15px';
870
		flag.style.padding = '2px';
871
		flag.style.fontFamily = 'sans-serif';
872
		document.body.style.paddingTop = '0px';
873
		document.body.insertBefore(flag, document.body.childNodes[0]);
874
		";
875
876
		/**
877
		 * Filter the Custom CSS preview message JS styling.
878
		 *
879
		 * @module custom-css
880
		 *
881
		 * @since 1.7.0
882
		 *
883
		 * @param string $preview_flag_js Custom CSS preview message JS styling.
884
		 */
885
		$preview_flag_js = apply_filters( 'safecss_preview_flag_js', $preview_flag_js );
886
		if ( $preview_flag_js ) {
887
			$preview_flag_js = '<script type="text/javascript">
888
	// <![CDATA[
889
	' . $preview_flag_js . '
890
	// ]]>
891
	</script>';
892
		}
893
894
		return $preview_flag_js;
895
	}
896
897
	static function menu() {
898
		$parent = 'themes.php';
0 ignored issues
show
Unused Code introduced by
$parent is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
899
		$title = __( 'Edit CSS', 'jetpack' );
900
		$hook = add_theme_page( $title, $title, 'edit_theme_options', 'editcss', array( 'Jetpack_Custom_CSS', 'admin' ) );
901
902
		add_action( "load-revision.php", array( 'Jetpack_Custom_CSS', 'prettify_post_revisions' ) );
903
		add_action( "load-$hook", array( 'Jetpack_Custom_CSS', 'update_title' ) );
904
	}
905
906
	/**
907
	 * Adds a menu item in the appearance section for this plugin's administration
908
	 * page. Also adds hooks to enqueue the CSS and JS for the admin page.
909
	 */
910
	static function update_title() {
911
		global $title;
912
		$title = __( 'CSS', 'jetpack' );
913
	}
914
915
	static function prettify_post_revisions() {
916
		add_filter( 'the_title', array( 'Jetpack_Custom_CSS', 'post_title' ), 10, 2 );
917
	}
918
919
	static function post_title( $title, $post_id ) {
920
		if ( !$post_id = (int) $post_id ) {
921
			return $title;
922
		}
923
924
		if ( !$post = get_post( $post_id ) ) {
925
			return $title;
926
		}
927
928
		if ( 'safecss' != $post->post_type ) {
929
			return $title;
930
		}
931
932
		return __( 'Custom CSS Stylesheet', 'jetpack' );
933
	}
934
935
	static function enqueue_scripts( $hook ) {
936
		if ( 'appearance_page_editcss' != $hook )
937
			return;
938
939
		wp_enqueue_script( 'postbox' );
940
		wp_enqueue_script( 'custom-css-editor', plugins_url( 'custom-css/js/css-editor.js', __FILE__ ), 'jquery', '20130325', true );
941
		wp_enqueue_style( 'custom-css-editor', plugins_url( 'custom-css/css/css-editor.css', __FILE__ ) );
942
943
		if ( defined( 'SAFECSS_USE_ACE' ) && SAFECSS_USE_ACE ) {
944
			wp_register_style( 'jetpack-css-codemirror', plugins_url( 'custom-css/css/codemirror.css', __FILE__ ), array(), '20120905' );
945
			wp_enqueue_style( 'jetpack-css-use-codemirror', plugins_url( 'custom-css/css/use-codemirror.css', __FILE__ ), array( 'jetpack-css-codemirror' ), '20120905' );
946
947
			wp_register_script( 'jetpack-css-codemirror', plugins_url( 'custom-css/js/codemirror.min.js', __FILE__ ), array(), '3.16', true );
948
			wp_enqueue_script( 'jetpack-css-use-codemirror', plugins_url( 'custom-css/js/use-codemirror.js', __FILE__ ), array( 'jquery', 'underscore', 'jetpack-css-codemirror' ), '20131009', true );
949
		}
950
	}
951
952
	static function saved_message() {
953
		echo '<div id="message" class="updated fade"><p><strong>' . __( 'Stylesheet saved.', 'jetpack' ) . '</strong></p></div>';
954
	}
955
956
	static function admin() {
957
		add_meta_box( 'submitdiv', __( 'Publish', 'jetpack' ), array( __CLASS__, 'publish_box' ), 'editcss', 'side' );
958
		add_action( 'custom_css_submitbox_misc_actions', array( __CLASS__, 'content_width_settings' ) );
959
960
		$safecss_post = Jetpack_Custom_CSS::get_post();
961
962
		if ( ! empty( $safecss_post ) && 0 < $safecss_post['ID'] && wp_get_post_revisions( $safecss_post['ID'] ) )
963
			add_meta_box( 'revisionsdiv', __( 'CSS Revisions', 'jetpack' ), array( __CLASS__, 'revisions_meta_box' ), 'editcss', 'side' );
964
		?>
965
		<div class="wrap">
966
			<?php
967
968
			/**
969
			 * Fires right before the custom css page begins.
970
			 *
971
			 * @module custom-css
972
			 *
973
			 * @since 1.7.0
974
			 */
975
			do_action( 'custom_design_header' );
976
977
			?>
978
			<h1><?php _e( 'CSS Stylesheet Editor', 'jetpack' ); ?></h1>
979
			<form id="safecssform" action="" method="post">
980
				<?php wp_nonce_field( 'safecss' ) ?>
981
				<?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); ?>
982
				<?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
983
				<input type="hidden" name="action" value="save" />
984
				<div id="poststuff">
985
					<p class="css-support">
986
					<?php
987
						/**
988
						 * Filter the intro text appearing above the Custom CSS Editor.
989
						 *
990
						 * @module custom-css
991
						 *
992
						 * @since 1.7.0
993
						 *
994
						 * @param string $str Intro text appearing above the Custom CSS editor.
995
						 */
996
						echo apply_filters( 'safecss_intro_text', __( 'New to CSS? Start with a <a href="http://www.htmldog.com/guides/cssbeginner/">beginner tutorial</a>. Questions?
997
		Ask in the <a href="http://wordpress.org/support/forum/themes-and-templates">Themes and Templates forum</a>.', 'jetpack' ) );
998
					?></p>
999
					<p class="css-support"><?php echo __( 'Note: Custom CSS will be reset when changing themes.', 'jetpack' ); ?></p>
1000
1001
					<div id="post-body" class="metabox-holder columns-2">
1002
						<div id="post-body-content">
1003
							<div class="postarea">
1004
								<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>
1005
								<div class="clear"></div>
1006
							</div>
1007
						</div>
1008
						<div id="postbox-container-1" class="postbox-container">
1009
						<?php do_meta_boxes( 'editcss', 'side', $safecss_post ); ?>
1010
					</div>
1011
					</div>
1012
					<br class="clear" />
1013
				</div>
1014
			</form>
1015
		</div>
1016
		<?php
1017
	}
1018
1019
	/**
1020
	 * Content width setting callback
1021
	 */
1022
	static function content_width_settings() {
1023
		$safecss_post = Jetpack_Custom_CSS::get_current_revision();
1024
1025
		$custom_content_width = get_post_meta( $safecss_post['ID'], 'content_width', true );
1026
1027
		// If custom content width hasn't been overridden and the theme has a content_width value, use that as a default.
1028
		if ( $custom_content_width <= 0 && ! empty( $GLOBALS['content_width'] ) )
1029
			$custom_content_width = $GLOBALS['content_width'];
1030
1031
		if ( ! $custom_content_width || ( isset( $GLOBALS['content_width'] ) && $custom_content_width == $GLOBALS['content_width'] ) )
1032
			$custom_content_width = '';
1033
1034
		?>
1035
		<div class="misc-pub-section">
1036
			<label><?php esc_html_e( 'Media Width:', 'jetpack' ); ?></label>
1037
			<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>
1038
			<a class="edit-content-width hide-if-no-js" href="#content-width"><?php echo esc_html_e( 'Edit', 'jetpack' ); ?></a>
1039
			<div id="content-width-select" class="hide-if-js">
1040
				<input type="hidden" name="custom_content_width" id="custom_content_width" value="<?php echo esc_attr( $custom_content_width ); ?>" />
1041
				<p>
1042
					<?php
1043
1044
					printf(
1045
						__( 'Limit width to %1$s pixels for full size images. (<a href="%2$s">More info</a>.)', 'jetpack' ),
1046
						'<input type="text" id="custom_content_width_visible" value="' . esc_attr( $custom_content_width ) . '" size="4" />',
1047
						/**
1048
						 * Filter the Custom CSS limited width's support doc URL.
1049
						 *
1050
						 * @module custom-css
1051
						 *
1052
						 * @since 2.2.3
1053
						 *
1054
						 * @param string $url Custom CSS limited width's support doc URL.
1055
						 */
1056
						apply_filters( 'safecss_limit_width_link', 'http://jetpack.me/support/custom-css/#limited-width' )
1057
					);
1058
1059
					?>
1060
				</p>
1061
				<?php
1062
1063
				if ( !empty( $GLOBALS['content_width'] ) && $custom_content_width != $GLOBALS['content_width'] ) {
1064
					if ( function_exists( 'wp_get_theme' ) )
1065
						$current_theme = wp_get_theme()->Name;
1066
					else
1067
						$current_theme = get_current_theme();
1068
1069
					?>
1070
					<p><?php printf( __( 'The default content width for the %s theme is %d pixels.', 'jetpack' ), $current_theme, intval( $GLOBALS['content_width'] ) ); ?></p>
1071
					<?php
1072
				}
1073
1074
				?>
1075
				<a class="save-content-width hide-if-no-js button" href="#content-width"><?php esc_html_e( 'OK', 'jetpack' ); ?></a>
1076
				<a class="cancel-content-width hide-if-no-js" href="#content-width"><?php esc_html_e( 'Cancel', 'jetpack' ); ?></a>
1077
			</div>
1078
			<script type="text/javascript">
1079
				jQuery( function ( $ ) {
1080
					var defaultContentWidth = <?php echo isset( $GLOBALS['content_width'] ) ? json_encode( intval( $GLOBALS['content_width'] ) ) : 0; ?>;
1081
1082
					$( '.edit-content-width' ).bind( 'click', function ( e ) {
1083
						e.preventDefault();
1084
1085
						$( '#content-width-select' ).slideDown();
1086
						$( this ).hide();
1087
					} );
1088
1089
					$( '.cancel-content-width' ).bind( 'click', function ( e ) {
1090
						e.preventDefault();
1091
1092
						$( '#content-width-select' ).slideUp( function () {
1093
							$( '.edit-content-width' ).show();
1094
							$( '#custom_content_width_visible' ).val( $( '#custom_content_width' ).val() );
1095
						} );
1096
					} );
1097
1098
					$( '.save-content-width' ).bind( 'click', function ( e ) {
1099
						e.preventDefault();
1100
1101
						$( '#content-width-select' ).slideUp();
1102
1103
						var newContentWidth = parseInt( $( '#custom_content_width_visible' ).val(), 10 );
1104
1105
						if ( newContentWidth && newContentWidth != defaultContentWidth ) {
1106
							$( '#content-width-display' ).text(
1107
								$( '#content-width-display' )
1108
									.data( 'custom-text' )
1109
										.replace( '%s', $( '#custom_content_width_visible' ).val() )
1110
							);
1111
						}
1112
						else {
1113
							$( '#content-width-display' ).text( $( '#content-width-display' ).data( 'default-text' ) );
1114
						}
1115
1116
						$( '#custom_content_width' ).val( $( '#custom_content_width_visible' ).val() );
1117
						$( '.edit-content-width' ).show();
1118
					} );
1119
				} );
1120
			</script>
1121
		</div>
1122
		<?php
1123
	}
1124
1125
	static function publish_box() {
1126
		?>
1127
		<div id="minor-publishing">
1128
			<div id="misc-publishing-actions">
1129
				<?php
1130
1131
				/**
1132
				 * Filter the array of available Custom CSS preprocessors.
1133
				 *
1134
				 * @module custom-css
1135
				 *
1136
				 * @since 2.0.3
1137
				 *
1138
				 * @param array array() Empty by default.
1139
				 */
1140
				$preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() );
1141
1142
				if ( ! empty( $preprocessors ) ) {
1143
					$safecss_post = Jetpack_Custom_CSS::get_current_revision();
1144
					$selected_preprocessor_key = get_post_meta( $safecss_post['ID'], 'custom_css_preprocessor', true );
1145
					$selected_preprocessor = isset( $preprocessors[$selected_preprocessor_key] ) ? $preprocessors[$selected_preprocessor_key] : null;
1146
1147
					?>
1148
					<div class="misc-pub-section">
1149
						<label><?php esc_html_e( 'Preprocessor:', 'jetpack' ); ?></label>
1150
						<span id="preprocessor-display"><?php echo esc_html( $selected_preprocessor ? $selected_preprocessor['name'] : __( 'None', 'jetpack' ) ); ?></span>
1151
						<a class="edit-preprocessor hide-if-no-js" href="#preprocessor"><?php echo esc_html_e( 'Edit', 'jetpack' ); ?></a>
1152
						<div id="preprocessor-select" class="hide-if-js">
1153
							<input type="hidden" name="custom_css_preprocessor" id="custom_css_preprocessor" value="<?php echo esc_attr( $selected_preprocessor_key ); ?>" />
1154
							<select id="preprocessor_choices">
1155
								<option value=""><?php esc_html_e( 'None', 'jetpack' ); ?></option>
1156
								<?php
1157
1158
								foreach ( $preprocessors as $preprocessor_key => $preprocessor ) {
1159
								?>
1160
									<option value="<?php echo esc_attr( $preprocessor_key ); ?>" <?php selected( $selected_preprocessor_key, $preprocessor_key ); ?>><?php echo esc_html( $preprocessor['name'] ); ?></option>
1161
									<?php
1162
								}
1163
1164
								?>
1165
							</select>
1166
							<a class="save-preprocessor hide-if-no-js button" href="#preprocessor"><?php esc_html_e( 'OK', 'jetpack' ); ?></a>
1167
							<a class="cancel-preprocessor hide-if-no-js" href="#preprocessor"><?php esc_html_e( 'Cancel', 'jetpack' ); ?></a>
1168
						</div>
1169
					</div>
1170
					<?php
1171
				}
1172
1173
				$safecss_post = Jetpack_Custom_CSS::get_current_revision();
1174
1175
				$add_css = ( get_post_meta( $safecss_post['ID'], 'custom_css_add', true ) != 'no' );
1176
1177
				?>
1178
				<div class="misc-pub-section">
1179
					<label><?php esc_html_e( 'Mode:', 'jetpack' ); ?></label>
1180
					<span id="css-mode-display"><?php echo esc_html( $add_css ? __( 'Add-on', 'jetpack' ) : __( 'Replacement', 'jetpack' ) ); ?></span>
1181
					<a class="edit-css-mode hide-if-no-js" href="#css-mode"><?php echo esc_html_e( 'Edit', 'jetpack' ); ?></a>
1182
					<div id="css-mode-select" class="hide-if-js">
1183
						<input type="hidden" name="add_to_existing" id="add_to_existing" value="<?php echo $add_css ? 'true' : 'false'; ?>" />
1184
						<p>
1185
							<label>
1186
								<input type="radio" name="add_to_existing_display" value="true" <?php checked( $add_css ); ?>/>
1187
								<?php _e( 'Add-on CSS <b>(Recommended)</b>', 'jetpack' ); ?>
1188
							</label>
1189
							<br />
1190
							<label>
1191
								<input type="radio" name="add_to_existing_display" value="false" <?php checked( ! $add_css ); ?>/>
1192
								<?php printf(
1193
									__( 'Replace <a href="%s">theme\'s CSS</a> <b>(Advanced)</b>', 'jetpack' ),
1194
									/**
1195
									 * Filter the theme's stylesheet URL.
1196
									 *
1197
									 * @module custom-css
1198
									 *
1199
									 * @since 1.7.0
1200
									 *
1201
									 * @param string $url Active theme's stylesheet URL. Default to get_stylesheet_uri().
1202
									 */
1203
									apply_filters( 'safecss_theme_stylesheet_url', get_stylesheet_uri() )
1204
								); ?>
1205
							</label>
1206
						</p>
1207
						<a class="save-css-mode hide-if-no-js button" href="#css-mode"><?php esc_html_e( 'OK', 'jetpack' ); ?></a>
1208
						<a class="cancel-css-mode hide-if-no-js" href="#css-mode"><?php esc_html_e( 'Cancel', 'jetpack' ); ?></a>
1209
					</div>
1210
				</div>
1211
				<?php
1212
1213
				/**
1214
				 * Allows addition of elements to the submit box for custom css on the wp-admin side.
1215
				 *
1216
				 * @module custom-css
1217
				 *
1218
				 * @since 2.0.3
1219
				 */
1220
				do_action( 'custom_css_submitbox_misc_actions' );
1221
1222
				?>
1223
			</div>
1224
		</div>
1225
		<div id="major-publishing-actions">
1226
			<input type="button" class="button" id="preview" name="preview" value="<?php esc_attr_e( 'Preview', 'jetpack' ) ?>" />
1227
			<div id="publishing-action">
1228
				<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' ); ?>" />
1229
			</div>
1230
		</div>
1231
		<?php
1232
	}
1233
1234
	/**
1235
	 * Render metabox listing CSS revisions and the themes that correspond to the revisions.
1236
	 * Called by safecss_admin
1237
	 *
1238
	 * @global $post
1239
	 * @param array $safecss_post
1240
	 * @uses wp_revisions_to_keep
1241
	 * @uses WP_Query
1242
	 * @uses wp_post_revision_title
1243
	 * @uses esc_html
1244
	 * @uses add_query_arg
1245
	 * @uses menu_page_url
1246
	 * @uses wp_reset_query
1247
	 * @return string
1248
	 */
1249
	static function revisions_meta_box( $safecss_post ) {
1250
1251
		$show_all_revisions = isset( $_GET['show_all_rev'] );
1252
1253
		if ( function_exists( 'wp_revisions_to_keep' ) ) {
1254
			$max_revisions = wp_revisions_to_keep( (object) $safecss_post );
1255
		} else {
1256
			$max_revisions = defined( 'WP_POST_REVISIONS' ) && is_numeric( WP_POST_REVISIONS ) ? (int) WP_POST_REVISIONS : 25;
1257
		}
1258
1259
		$posts_per_page = $show_all_revisions ? $max_revisions : 6;
1260
1261
		$revisions = new WP_Query( array(
1262
			'posts_per_page' => $posts_per_page,
1263
			'post_type' => 'revision',
1264
			'post_status' => 'inherit',
1265
			'post_parent' => $safecss_post['ID'],
1266
			'orderby' => 'date',
1267
			'order' => 'DESC'
1268
		) );
1269
1270
		if ( $revisions->have_posts() ) { ?>
1271
			<ul class="post-revisions"><?php
1272
1273
			global $post;
1274
1275
			while ( $revisions->have_posts() ) :
1276
				$revisions->the_post();
1277
1278
				?><li>
1279
					<?php
1280
						echo wp_post_revision_title( $post );
1281
1282
						if ( ! empty( $post->post_excerpt ) )
1283
							echo ' (' . esc_html( $post->post_excerpt ) . ')';
1284
					?>
1285
				</li><?php
1286
1287
			endwhile;
1288
1289
			?></ul><?php
1290
1291
			if ( $revisions->found_posts > 6 && !$show_all_revisions ) {
1292
				?>
1293
				<br>
1294
				<a href="<?php echo add_query_arg( 'show_all_rev', 'true', menu_page_url( 'editcss', false ) ); ?>"><?php esc_html_e( 'Show all', 'jetpack' ); ?></a>
1295
				<?php
1296
			}
1297
		}
1298
1299
		wp_reset_query();
1300
	}
1301
1302
	/**
1303
	 * Hook in init at priority 11 to disable custom CSS.
1304
	 */
1305
	static function disable() {
1306
		remove_action( 'wp_head', array( 'Jetpack_Custom_CSS', 'link_tag' ), 101 );
1307
	    remove_filter( 'stylesheet_uri', array( 'Jetpack_Custom_CSS', 'style_filter' ) );
1308
	}
1309
1310
	/**
1311
	 * Reset all aspects of Custom CSS on a theme switch so that changing
1312
	 * themes is a sure-fire way to get a clean start.
1313
	 */
1314
	static function reset() {
1315
		$safecss_post_id = Jetpack_Custom_CSS::save_revision( '' );
1316
		$safecss_revision = Jetpack_Custom_CSS::get_current_revision();
1317
1318
		update_option( 'safecss_rev', intval( get_option( 'safecss_rev' ) ) + 1 );
1319
1320
		update_post_meta( $safecss_post_id, 'custom_css_add', 'yes' );
1321
		update_post_meta( $safecss_post_id, 'content_width', false );
1322
		update_post_meta( $safecss_post_id, 'custom_css_preprocessor', '' );
1323
1324
		delete_option( 'safecss_add' );
1325
		delete_option( 'safecss_content_width' );
1326
1327
		update_metadata( 'post', $safecss_revision['ID'], 'custom_css_add', 'yes' );
1328
		update_metadata( 'post', $safecss_revision['ID'], 'content_width', false );
1329
		update_metadata( 'post', $safecss_revision['ID'], 'custom_css_preprocessor', '' );
1330
1331
		delete_option( 'safecss_preview_add' );
1332
	}
1333
1334
	static function is_customizer_preview() {
1335
		if ( isset ( $GLOBALS['wp_customize'] ) )
1336
			return ! $GLOBALS['wp_customize']->is_theme_active();
1337
1338
		return false;
1339
	}
1340
1341
	static function minify( $css, $preprocessor = '' ) {
1342
		if ( ! $css )
1343
			return '';
1344
1345
		if ( $preprocessor ) {
1346
			/** This filter is documented in modules/custom-css/custom-css.php */
1347
			$preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() );
1348
1349
			if ( isset( $preprocessors[$preprocessor] ) ) {
1350
				$css = call_user_func( $preprocessors[$preprocessor]['callback'], $css );
1351
			}
1352
		}
1353
1354
		safecss_class();
1355
		$csstidy = new csstidy();
1356
		$csstidy->optimise = new safecss( $csstidy );
1357
1358
		$csstidy->set_cfg( 'remove_bslash',              false );
1359
		$csstidy->set_cfg( 'compress_colors',            true );
1360
		$csstidy->set_cfg( 'compress_font-weight',       true );
1361
		$csstidy->set_cfg( 'remove_last_;',              true );
1362
		$csstidy->set_cfg( 'case_properties',            true );
1363
		$csstidy->set_cfg( 'discard_invalid_properties', true );
1364
		$csstidy->set_cfg( 'css_level',                  'CSS3.0' );
1365
		$csstidy->set_cfg( 'template', 'highest');
1366
		$csstidy->parse( $css );
1367
1368
		return $csstidy->print->plain();
1369
	}
1370
1371
	/**
1372
	 * When restoring a SafeCSS post revision, also copy over the
1373
	 * content_width and custom_css_add post metadata.
1374
	 */
1375
	static function restore_revision( $_post_id, $_revision_id ) {
1376
		$_post = get_post( $_post_id );
1377
1378
		if ( 'safecss' != $_post->post_type )
1379
			return;
1380
1381
		$safecss_revision = Jetpack_Custom_CSS::get_current_revision();
1382
1383
		$content_width = get_post_meta( $_revision_id, 'content_width', true );
1384
		$custom_css_add = get_post_meta( $_revision_id, 'custom_css_add', true );
1385
		$preprocessor = get_post_meta( $_revision_id, 'custom_css_preprocessor', true );
1386
1387
		update_metadata( 'post', $safecss_revision['ID'], 'content_width', $content_width );
1388
		update_metadata( 'post', $safecss_revision['ID'], 'custom_css_add', $custom_css_add );
1389
		update_metadata( 'post', $safecss_revision['ID'], 'custom_css_preprocessor', $preprocessor );
1390
1391
		delete_option( 'safecss_add' );
1392
		delete_option( 'safecss_content_width' );
1393
1394
		update_post_meta( $_post->ID, 'content_width', $content_width );
1395
		update_post_meta( $_post->ID, 'custom_css_add', $custom_css_add );
1396
		update_post_meta( $_post->ID, 'custom_css_preprocessor', $preprocessor );
1397
1398
		delete_option( 'safecss_preview_add' );
1399
	}
1400
1401
	/**
1402
	 * Migration routine for moving safecss from wp_options to wp_posts to support revisions
1403
	 *
1404
	 * @return void
1405
	 */
1406
	static function upgrade() {
1407
		$css = get_option( 'safecss' );
1408
1409
		if ( get_option( 'safecss_revision_migrated' ) ) {
1410
			return false;
1411
		}
1412
1413
		// Check if CSS is stored in wp_options
1414
		if ( $css ) {
1415
			// Remove the async actions from publish_post
1416
			remove_action( 'publish_post', 'queue_publish_post' );
1417
1418
			$post = array();
1419
			$post['post_content'] = $css;
1420
			$post['post_title'] = 'safecss';
1421
			$post['post_status'] = 'publish';
1422
			$post['post_type'] = 'safecss';
1423
1424
			// Insert the CSS into wp_posts
1425
			$post_id = wp_insert_post( $post );
1426
			// Check for errors
1427
			if ( !$post_id or is_wp_error( $post_id ) )
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
1428
				die( $post_id->get_error_message() );
0 ignored issues
show
Coding Style Compatibility introduced by
The method upgrade() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
1429
1430
			// Delete safecss option
1431
			delete_option( 'safecss' );
1432
		}
1433
1434
		unset( $css );
1435
1436
		// Check if we have already done this
1437
		if ( !get_option( 'safecss_revision_migrated' ) ) {
1438
			define( 'DOING_MIGRATE', true );
1439
1440
			// Get hashes of safecss post and current revision
1441
			$safecss_post = Jetpack_Custom_CSS::get_post();
1442
1443
			if ( empty( $safecss_post ) )
1444
				return;
1445
1446
			$safecss_post_hash = md5( $safecss_post['post_content'] );
1447
			$current_revision = Jetpack_Custom_CSS::get_current_revision();
1448
1449
			if ( null == $current_revision )
1450
				return;
1451
1452
			$current_revision_hash = md5( $current_revision['post_content'] );
1453
1454
			// If hashes are not equal, set safecss post with content from current revision
1455
			if ( $safecss_post_hash !== $current_revision_hash ) {
1456
				Jetpack_Custom_CSS::save_revision( $current_revision['post_content'] );
1457
				// Reset post_content to display the migrated revsion
1458
				$safecss_post['post_content'] = $current_revision['post_content'];
1459
			}
1460
1461
			// Set option so that we dont keep doing this
1462
			update_option( 'safecss_revision_migrated', time() );
1463
		}
1464
1465
		$newest_safecss_post = Jetpack_Custom_CSS::get_current_revision();
1466
1467
		if ( $newest_safecss_post ) {
1468 View Code Duplication
			if ( get_option( 'safecss_content_width' ) ) {
1469
				// Add the meta to the post and the latest revision.
1470
				update_post_meta( $newest_safecss_post['ID'], 'content_width', get_option( 'safecss_content_width' ) );
1471
				update_metadata( 'post', $newest_safecss_post['ID'], 'content_width', get_option( 'safecss_content_width' ) );
1472
1473
				delete_option( 'safecss_content_width' );
1474
			}
1475
1476 View Code Duplication
			if ( get_option( 'safecss_add' ) ) {
1477
				update_post_meta( $newest_safecss_post['ID'], 'custom_css_add', get_option( 'safecss_add' ) );
1478
				update_metadata( 'post', $newest_safecss_post['ID'], 'custom_css_add', get_option( 'safecss_add' ) );
1479
1480
				delete_option( 'safecss_add' );
1481
			}
1482
		}
1483
	}
1484
1485
	/**
1486
	 * Adds a filter to the redirect location in `wp-admin/revisions.php`.
1487
	 */
1488
	static function add_revision_redirect() {
1489
		add_filter( 'wp_redirect', array( __CLASS__, 'revision_redirect' ) );
1490
	}
1491
1492
	/**
1493
	 * Filters the redirect location in `wp-admin/revisions.php`.
1494
	 *
1495
	 * @param string $location The path to redirect to.
1496
	 * @return string
1497
	 */
1498
	static function revision_redirect( $location ) {
1499
		$post = get_post();
1500
1501
		if ( ! empty( $post->post_type ) && 'safecss' == $post->post_type ) {
1502
			$location = 'themes.php?page=editcss';
1503
1504
			if ( 'edit.php' == $location ) {
1505
				$location = '';
1506
			}
1507
		}
1508
1509
		return $location;
1510
	}
1511
1512
	static function revision_post_link( $post_link, $post_id, $context ) {
1513
		if ( !$post_id = (int) $post_id ) {
1514
			return $post_link;
1515
		}
1516
1517
		if ( !$post = get_post( $post_id ) ) {
1518
			return $post_link;
1519
		}
1520
1521
		if ( 'safecss' != $post->post_type ) {
1522
			return $post_link;
1523
		}
1524
1525
		$post_link = admin_url( 'themes.php?page=editcss' );
1526
1527
		if ( 'display' == $context ) {
1528
			return esc_url( $post_link );
1529
		}
1530
1531
		return esc_url_raw( $post_link );
1532
	}
1533
1534
	/**
1535
	 * When on the edit screen, make sure the custom content width
1536
	 * setting is applied to the large image size.
1537
	 */
1538
	static function editor_max_image_size( $dims, $size = 'medium', $context = null ) {
1539
		list( $width, $height ) = $dims;
1540
1541
		if ( 'large' == $size && 'edit' == $context )
1542
			$width = Jetpack::get_content_width();
1543
1544
		return array( $width, $height );
1545
	}
1546
1547
	/**
1548
	 * Override the content_width with a custom value if one is set.
1549
	 */
1550
	static function jetpack_content_width( $content_width ) {
1551
		$custom_content_width = 0;
1552
1553
		if ( Jetpack_Custom_CSS::is_preview() ) {
1554
			$safecss_post = Jetpack_Custom_CSS::get_current_revision();
1555
			$custom_content_width = intval( get_post_meta( $safecss_post['ID'], 'content_width', true ) );
1556
		} else if ( ! Jetpack_Custom_CSS::is_freetrial() ) {
1557
			$custom_css_post_id = Jetpack_Custom_CSS::post_id();
1558
			if ( $custom_css_post_id )
1559
				$custom_content_width = intval( get_post_meta( $custom_css_post_id, 'content_width', true ) );
1560
		}
1561
1562
		if ( $custom_content_width > 0 )
1563
			$content_width = $custom_content_width;
1564
1565
		return $content_width;
1566
	}
1567
}
1568
1569
class Jetpack_Safe_CSS {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
1570
	static function filter_attr( $css, $element = 'div' ) {
1571
		safecss_class();
1572
1573
		$css = $element . ' {' . $css . '}';
1574
1575
		$csstidy = new csstidy();
1576
		$csstidy->optimise = new safecss( $csstidy );
1577
		$csstidy->set_cfg( 'remove_bslash', false );
1578
		$csstidy->set_cfg( 'compress_colors', false );
1579
		$csstidy->set_cfg( 'compress_font-weight', false );
1580
		$csstidy->set_cfg( 'discard_invalid_properties', true );
1581
		$csstidy->set_cfg( 'merge_selectors', false );
1582
		$csstidy->set_cfg( 'remove_last_;', false );
1583
		$csstidy->set_cfg( 'css_level', 'CSS3.0' );
1584
1585
		$css = preg_replace( '/\\\\([0-9a-fA-F]{4})/', '\\\\\\\\$1', $css );
1586
		$css = wp_kses_split( $css, array(), array() );
1587
		$csstidy->parse( $css );
1588
1589
		$css = $csstidy->print->plain();
1590
1591
		$css = str_replace( array( "\n","\r","\t" ), '', $css );
1592
1593
		preg_match( "/^{$element}\s*{(.*)}\s*$/", $css, $matches );
1594
1595
		if ( empty( $matches[1] ) )
1596
			return '';
1597
1598
		return $matches[1];
1599
	}
1600
}
1601
1602
function migrate() {
1603
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::upgrade()' );
1604
1605
	return Jetpack_Custom_CSS::upgrade();
1606
}
1607
1608
function safecss_revision_redirect( $redirect ) {
1609
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::revision_redirect()' );
1610
1611
	return Jetpack_Custom_CSS::revision_redirect( $redirect );
1612
}
1613
1614
function safecss_revision_post_link( $post_link, $post_id, $context ) {
1615
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::revision_post_link()' );
1616
1617
	return Jetpack_Custom_CSS::revision_post_link( $post_link, $post_id, $context );
1618
}
1619
1620
function get_safecss_post() {
1621
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::get_post()' );
1622
1623
	return Jetpack_Custom_CSS::get_post();
1624
}
1625
1626
function custom_css_post_id() {
1627
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::post_id()' );
1628
1629
	return Jetpack_Custom_CSS::post_id();
1630
}
1631
1632
function get_current_revision() {
1633
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::get_current_revision()' );
1634
1635
	return Jetpack_Custom_CSS::get_current_revision();
1636
}
1637
1638
function save_revision( $css, $is_preview = false, $preprocessor = '' ) {
1639
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::save_revision()' );
1640
1641
	return Jetpack_Custom_CSS::save_revision( $css, $is_preview, $preprocessor );
1642
}
1643
1644
function safecss_skip_stylesheet() {
1645
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::skip_stylesheet()' );
1646
1647
	return Jetpack_Custom_CSS::skip_stylesheet();
1648
}
1649
1650
function safecss_init() {
1651
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::init()' );
1652
1653
	return Jetpack_Custom_CSS::init();
1654
}
1655
1656
function safecss_is_preview() {
1657
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::is_preview()' );
1658
1659
	return Jetpack_Custom_CSS::is_preview();
1660
}
1661
1662
function safecss_is_freetrial() {
1663
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::is_freetrial()' );
1664
1665
	return Jetpack_Custom_CSS::is_freetrial();
1666
}
1667
1668
function safecss( $compressed = false ) {
1669
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::get_css()' );
1670
1671
	return Jetpack_Custom_CSS::get_css( $compressed );
1672
}
1673
1674
function safecss_print() {
1675
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::print_css()' );
1676
1677
	return Jetpack_Custom_CSS::print_css();
1678
}
1679
1680
function safecss_style() {
1681
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::link_tag()' );
1682
1683
	return Jetpack_Custom_CSS::link_tag();
1684
}
1685
1686
function safecss_style_filter( $current ) {
1687
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::style_filter()' );
1688
1689
	return Jetpack_Custom_CSS::style_filter( $current );
1690
}
1691
1692
function safecss_buffer( $html ) {
1693
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::buffer()' );
1694
1695
	return Jetpack_Custom_CSS::buffer( $html );
1696
}
1697
1698
function safecss_preview_links( $matches ) {
1699
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::preview_links()' );
1700
1701
	return Jetpack_Custom_CSS::preview_links( $matches );
1702
}
1703
1704
function safecss_preview_flag() {
1705
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::preview_flag()' );
1706
1707
	return Jetpack_Custom_CSS::preview_flag();
1708
}
1709
1710
function safecss_menu() {
1711
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::menu()' );
1712
1713
	return Jetpack_Custom_CSS::menu();
1714
}
1715
1716
function update_title() {
1717
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::update_title()' );
1718
1719
	return Jetpack_Custom_CSS::update_title();
1720
}
1721
1722
function safecss_prettify_post_revisions() {
1723
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::prettify_post_revisions()' );
1724
1725
	return Jetpack_Custom_CSS::prettify_post_revisions();
1726
}
1727
1728
function safecss_remove_title_excerpt_from_revisions() {
1729
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::remove_title_excerpt_from_revisions()' );
1730
1731
	return Jetpack_Custom_CSS::remove_title_excerpt_from_revisions();
0 ignored issues
show
Bug introduced by
The method remove_title_excerpt_from_revisions() does not seem to exist on object<Jetpack_Custom_CSS>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1732
}
1733
1734
function safecss_post_title( $title, $post_id ) {
1735
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::post_title()' );
1736
1737
	return Jetpack_Custom_CSS::post_title( $title, $post_id );
1738
}
1739
1740
function safe_css_enqueue_scripts() {
1741
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::enqueue_scripts()' );
1742
1743
	return Jetpack_Custom_CSS::enqueue_scripts();
0 ignored issues
show
Bug introduced by
The call to enqueue_scripts() misses a required argument $hook.

This check looks for function calls that miss required arguments.

Loading history...
1744
}
1745
1746
function safecss_admin_head() {
1747
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::admin_head()' );
1748
1749
	return Jetpack_Custom_CSS::admin_head();
0 ignored issues
show
Bug introduced by
The method admin_head() does not seem to exist on object<Jetpack_Custom_CSS>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1750
}
1751
1752
function safecss_saved() {
1753
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::saved_message()' );
1754
1755
	return Jetpack_Custom_CSS::saved_message();
1756
}
1757
1758
function safecss_admin() {
1759
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::admin()' );
1760
1761
	return Jetpack_Custom_CSS::admin();
1762
}
1763
1764
function custom_css_meta_box() {
1765
	_deprecated_function( __FUNCTION__, '2.1', 'add_meta_box( $id, $title, $callback, \'editcss\', \'side\' )' );
1766
}
1767
1768
function custom_css_post_revisions_meta_box( $safecss_post ) {
1769
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::revisions_meta_box()' );
1770
1771
	return Jetpack_Custom_CSS::revisions_meta_box( $safecss_post );
1772
}
1773
1774
function disable_safecss_style() {
1775
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::disable()' );
1776
1777
	return Jetpack_Custom_CSS::disable();
1778
}
1779
1780
function custom_css_reset() {
1781
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::reset()' );
1782
1783
	return Jetpack_Custom_CSS::reset();
1784
}
1785
1786
function custom_css_is_customizer_preview() {
1787
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::is_customizer_preview()' );
1788
1789
	return Jetpack_Custom_CSS::is_customizer_preview();
1790
}
1791
1792
function custom_css_minify( $css, $preprocessor = '' ) {
1793
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::minify()' );
1794
1795
	return Jetpack_Custom_CSS::minify( $css, $preprocessor );
1796
}
1797
1798
function custom_css_restore_revision( $_post_id, $_revision_id ) {
1799
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::restore_revision()' );
1800
1801
	return Jetpack_Custom_CSS::restore_revision( $_post_id, $_revision_id );
1802
}
1803
1804
function safecss_class() {
1805
	// Wrapped so we don't need the parent class just to load the plugin
1806
	if ( class_exists('safecss') )
1807
		return;
1808
1809
	require_once( dirname( __FILE__ ) . '/csstidy/class.csstidy.php' );
1810
1811
	class safecss extends csstidy_optimise {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
1812
		function __construct( &$css ) {
1813
			return $this->csstidy_optimise( $css );
1814
		}
1815
1816
		function postparse() {
1817
1818
			/**
1819
			 * Fires after parsing the css.
1820
			 *
1821
			 * @module custom-css
1822
			 *
1823
			 * @since 1.8.0
1824
			 *
1825
			 * @param obj $this CSSTidy object.
1826
			 */
1827
			do_action( 'csstidy_optimize_postparse', $this );
1828
1829
			return parent::postparse();
1830
		}
1831
1832
		function subvalue() {
1833
1834
			/**
1835
			 * Fires before optimizing the Custom CSS subvalue.
1836
			 *
1837
			 * @module custom-css
1838
			 *
1839
			 * @since 1.8.0
1840
			 *
1841
			 * @param obj $this CSSTidy object.
1842
			 **/
1843
			do_action( 'csstidy_optimize_subvalue', $this );
1844
1845
			return parent::subvalue();
1846
		}
1847
	}
1848
}
1849
1850
if ( ! function_exists( 'safecss_filter_attr' ) ) {
1851
	function safecss_filter_attr( $css, $element = 'div' ) {
1852
		return Jetpack_Safe_CSS::filter_attr( $css, $element );
1853
	}
1854
}
1855
1856
add_action( 'init', array( 'Jetpack_Custom_CSS', 'init' ) );
1857
1858
include dirname( __FILE__ ) . '/custom-css/preprocessors.php';
1859