Completed
Push — kraftbj-patch-2 ( 82c983...ae9d16 )
by
unknown
513:58 queued 503:20
created

Jetpack_Custom_CSS::content_width_settings()   D

Complexity

Conditions 10
Paths 8

Size

Total Lines 102
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 33
nc 8
nop 0
dl 0
loc 102
rs 4.8196
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 );
0 ignored issues
show
Documentation introduced by
$csstidy is of type object<csstidy>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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
		// prevent content: '\3434' from turning into '\\3434'
218
		$css = str_replace( array( '\'\\\\', '"\\\\' ), array( '\'\\', '"\\' ), $css );
219
220
		if ( $css != $prev )
221
			$warnings[] = 'preg_replace found stuff';
222
223
		// Some people put weird stuff in their CSS, KSES tends to be greedy
224
		$css = str_replace( '<=', '&lt;=', $css );
225
		// Why KSES instead of strip_tags?  Who knows?
226
		$css = wp_kses_split( $prev = $css, array(), array() );
227
		$css = str_replace( '&gt;', '>', $css ); // kses replaces lone '>' with &gt;
228
		// Why both KSES and strip_tags?  Because we just added some '>'.
229
		$css = strip_tags( $css );
230
231
		if ( $css != $prev )
232
			$warnings[] = 'kses found stuff';
233
234
		// if we're not using a preprocessor
235 View Code Duplication
		if ( ! $args['preprocessor'] ) {
236
237
			/**
238
			 * Fires before parsing the css with CSSTidy, but only if
239
			 * the preprocessor is not configured for use.
240
			 *
241
			 * @module custom-css
242
			 *
243
			 * @since 1.7.0
244
			 *
245
			 * @param obj $csstidy The csstidy object.
246
			 * @param string $css Custom CSS.
247
			 * @param array $args Array of custom CSS arguments.
248
			 */
249
			do_action( 'safecss_parse_pre', $csstidy, $css, $args );
250
251
			$csstidy->parse( $css );
252
253
			/**
254
			 * Fires after parsing the css with CSSTidy, but only if
255
			 * the preprocessor is not cinfigured for use.
256
			 *
257
			 * @module custom-css
258
			 *
259
			 * @since 1.7.0
260
			 *
261
			 * @param obj $csstidy The csstidy object.
262
			 * @param array $warnings Array of warnings.
263
			 * @param array $args Array of custom CSS arguments.
264
			 */
265
			do_action( 'safecss_parse_post', $csstidy, $warnings, $args );
266
267
			$css = $csstidy->print->plain();
268
		}
269
270
		if ( $args['add_to_existing'] )
271
			$add_to_existing = 'yes';
272
		else
273
			$add_to_existing = 'no';
274
275
		if ( $args['is_preview'] || Jetpack_Custom_CSS::is_freetrial() ) {
276
			// Save the CSS
277
			$safecss_revision_id = Jetpack_Custom_CSS::save_revision( $css, true, $args['preprocessor'] );
278
279
			// Cache Buster
280
			update_option( 'safecss_preview_rev', intval( get_option( 'safecss_preview_rev' ) ) + 1);
281
282
			update_metadata( 'post', $safecss_revision_id, 'custom_css_add', $add_to_existing );
283
			update_metadata( 'post', $safecss_revision_id, 'content_width', $args['content_width'] );
284
			update_metadata( 'post', $safecss_revision_id, 'custom_css_preprocessor', $args['preprocessor'] );
285
286
			delete_option( 'safecss_add' );
287
			delete_option( 'safecss_content_width' );
288
289
			if ( $args['is_preview'] ) {
290
				return $safecss_revision_id;
291
			}
292
293
			/**
294
			 * Fires after saving Custom CSS.
295
			 *
296
			 * @module custom-css
297
			 *
298
			 * @since 1.7.0
299
			 */
300
			do_action( 'safecss_save_preview_post' );
301
		}
302
303
		// Save the CSS
304
		$safecss_post_id = Jetpack_Custom_CSS::save_revision( $css, false, $args['preprocessor'] );
305
306
		$safecss_post_revision = Jetpack_Custom_CSS::get_current_revision();
307
308
		update_option( 'safecss_rev', intval( get_option( 'safecss_rev' ) ) + 1 );
309
310
		update_post_meta( $safecss_post_id, 'custom_css_add', $add_to_existing );
311
		update_post_meta( $safecss_post_id, 'content_width', $args['content_width'] );
312
		update_post_meta( $safecss_post_id, 'custom_css_preprocessor', $args['preprocessor'] );
313
314
		delete_option( 'safecss_add' );
315
		delete_option( 'safecss_content_width' );
316
317
		update_metadata( 'post', $safecss_post_revision['ID'], 'custom_css_add', $add_to_existing );
318
		update_metadata( 'post', $safecss_post_revision['ID'], 'content_width', $args['content_width'] );
319
		update_metadata( 'post', $safecss_post_revision['ID'], 'custom_css_preprocessor', $args['preprocessor'] );
320
321
		delete_option( 'safecss_preview_add' );
322
323
		return $safecss_post_id;
324
	}
325
326
	/**
327
	 * Get the published custom CSS post.
328
	 *
329
	 * @return array
330
	 */
331
	static function get_post() {
332
		$custom_css_post_id = Jetpack_Custom_CSS::post_id();
333
334
		if ( $custom_css_post_id )
335
			return get_post( $custom_css_post_id, ARRAY_A );
336
337
		return array();
338
	}
339
340
	/**
341
	 * Get the post ID of the published custom CSS post.
342
	 *
343
	 * @return int|bool The post ID if it exists; false otherwise.
344
	 */
345 View Code Duplication
	static function post_id() {
346
		/**
347
		 * Filter the ID of the post where Custom CSS is stored, before the ID is retrieved.
348
		 *
349
		 * If the callback function returns a non-null value, then post_id() will immediately
350
		 * return that value, instead of retrieving the normal post ID.
351
		 *
352
		 * @module custom-css
353
		 *
354
		 * @since 3.8.1
355
		 *
356
		 * @param null null The ID to return instead of the normal ID.
357
		 */
358
		$custom_css_post_id = apply_filters( 'jetpack_custom_css_pre_post_id', null );
359
		if ( ! is_null( $custom_css_post_id ) ) {
360
			return $custom_css_post_id;
361
		}
362
363
		$custom_css_post_id = wp_cache_get( 'custom_css_post_id' );
364
365
		if ( false === $custom_css_post_id ) {
366
			$custom_css_posts = get_posts( array(
367
				'posts_per_page' => 1,
368
				'post_type' => 'safecss',
369
				'post_status' => 'publish',
370
				'orderby' => 'date',
371
				'order' => 'DESC'
372
			) );
373
374
			if ( count( $custom_css_posts ) > 0 )
375
				$custom_css_post_id = $custom_css_posts[0]->ID;
376
			else
377
				$custom_css_post_id = 0;
378
379
			// Save post_id=0 to note that no safecss post exists.
380
			wp_cache_set( 'custom_css_post_id', $custom_css_post_id );
381
		}
382
383
		if ( ! $custom_css_post_id )
384
			return false;
385
386
		return $custom_css_post_id;
387
	}
388
389
	/**
390
	 * Get the current revision of the original safecss record
391
	 *
392
	 * @return object
393
	 */
394
	static function get_current_revision() {
395
		$safecss_post = Jetpack_Custom_CSS::get_post();
396
397
		if ( empty( $safecss_post ) ) {
398
			return false;
399
		}
400
401
		$revisions = wp_get_post_revisions( $safecss_post['ID'], array( 'posts_per_page' => 1, 'orderby' => 'date', 'order' => 'DESC' ) );
402
403
		// Empty array if no revisions exist
404
		if ( empty( $revisions ) ) {
405
			// Return original post
406
			return $safecss_post;
407
		} else {
408
			// Return the first entry in $revisions, this will be the current revision
409
			$current_revision = get_object_vars( array_shift( $revisions ) );
410
			return $current_revision;
411
		}
412
	}
413
414
	/**
415
	 * Save new revision of CSS
416
	 * Checks to see if content was modified before really saving
417
	 *
418
	 * @param string $css
419
	 * @param bool $is_preview
420
	 * @return bool|int If nothing was saved, returns false. If a post
421
	 *                  or revision was saved, returns the post ID.
422
	 */
423
	static function save_revision( $css, $is_preview = false, $preprocessor = '' ) {
424
		$safecss_post = Jetpack_Custom_CSS::get_post();
425
426
		$compressed_css = Jetpack_Custom_CSS::minify( $css, $preprocessor );
427
428
		// If null, there was no original safecss record, so create one
429
		if ( null == $safecss_post ) {
430
			if ( ! $css )
431
				return false;
432
433
			$post = array();
434
			$post['post_content'] = wp_slash( $css );
435
			$post['post_title'] = 'safecss';
436
			$post['post_status'] = 'publish';
437
			$post['post_type'] = 'safecss';
438
			$post['post_content_filtered'] = wp_slash( $compressed_css );
439
440
			// Set excerpt to current theme, for display in revisions list
441
			$current_theme = wp_get_theme();
442
			$post['post_excerpt'] = $current_theme->Name;
443
444
			// Insert the CSS into wp_posts
445
			$post_id = wp_insert_post( $post );
446
			wp_cache_set( 'custom_css_post_id', $post_id );
447
			return $post_id;
448
		}
449
450
		// Update CSS in post array with new value passed to this function
451
		$safecss_post['post_content'] = $css;
452
		$safecss_post['post_content_filtered'] = $compressed_css;
453
454
		// Set excerpt to current theme, for display in revisions list
455
		$current_theme = wp_get_theme();
456
		$safecss_post['post_excerpt'] = $current_theme->Name;
457
458
		// Don't carry over last revision's timestamps, otherwise revisions all have matching timestamps
459
		unset( $safecss_post['post_date'] );
460
		unset( $safecss_post['post_date_gmt'] );
461
		unset( $safecss_post['post_modified'] );
462
		unset( $safecss_post['post_modified_gmt'] );
463
464
		// Do not update post if we are only saving a preview
465
		if ( false === $is_preview ) {
466
			$safecss_post['post_content'] = wp_slash( $safecss_post['post_content'] );
467
			$safecss_post['post_content_filtered'] = wp_slash( $safecss_post['post_content_filtered'] );
468
			$post_id = wp_update_post( $safecss_post );
469
			wp_cache_set( 'custom_css_post_id', $post_id );
470
			return $post_id;
471
		}
472
		else if ( ! defined( 'DOING_MIGRATE' ) ) {
473
			return _wp_put_post_revision( $safecss_post );
474
		}
475
	}
476
477
	static function skip_stylesheet() {
478
		/**
479
		 * Prevent the Custom CSS stylesheet from being enqueued.
480
		 *
481
		 * @module custom-css
482
		 *
483
		 * @since 2.2.1
484
		 *
485
		 * @param null Should the stylesheet be skipped. Default to null. Anything else will force the stylesheet to be skipped.
486
		 */
487
		$skip_stylesheet = apply_filters( 'safecss_skip_stylesheet', null );
488
489
		if ( null !== $skip_stylesheet ) {
490
			return $skip_stylesheet;
491
		} elseif ( Jetpack_Custom_CSS::is_customizer_preview() ) {
492
			return false;
493
		} else {
494
			if ( Jetpack_Custom_CSS::is_preview() ) {
495
				$safecss_post = Jetpack_Custom_CSS::get_current_revision();
496
497
				if ( $safecss_post )
498
					return (bool) ( get_post_meta( $safecss_post['ID'], 'custom_css_add', true ) == 'no' );
499
				else
500
					return (bool) ( get_option( 'safecss_preview_add' ) == 'no' );
501
			}
502
			else {
503
				$custom_css_post_id = Jetpack_Custom_CSS::post_id();
504
505
				if ( $custom_css_post_id ) {
506
					$custom_css_add = get_post_meta( $custom_css_post_id, 'custom_css_add', true );
507
508
					// It is possible for the CSS to be stored in a post but for the safecss_add option
509
					// to have not been upgraded yet if the user hasn't opened their Custom CSS editor
510
					// since October 2012.
511
					if ( ! empty( $custom_css_add ) )
512
						return (bool) ( $custom_css_add === 'no' );
513
				}
514
515
				return (bool) ( Jetpack_Options::get_option_and_ensure_autoload( 'safecss_add', '' ) == 'no' );
516
			}
517
		}
518
	}
519
520
	static function is_preview() {
521
		return isset( $_GET['csspreview'] ) && $_GET['csspreview'] === 'true';
522
	}
523
524
	/**
525
	 * Currently this filter function gets called on
526
	 * 'template_redirect' action and
527
	 * 'admin_init' action
528
	 */
529 View Code Duplication
	static function set_content_width(){
530
		// Don't apply this filter on the Edit CSS page
531
		if ( isset( $_GET ) && isset( $_GET['page'] ) &&  'editcss' == $_GET['page'] && is_admin() ) {
532
			return;
533
		}
534
535
		$GLOBALS['content_width'] = Jetpack::get_content_width();
536
	}
537
538
	/*
539
	 * False when the site has the Custom Design upgrade.
540
	 * Used only on WordPress.com.
541
	 */
542
	static function is_freetrial() {
543
		/**
544
		 * Determine if a WordPress.com site uses a Free trial of the Custom Design Upgrade.
545
		 * Used only on WordPress.com.
546
		 *
547
		 * @module custom-css
548
		 *
549
		 * @since 1.7.0
550
		 *
551
		 * @param bool false Does the site use a Free trial of the Custom Design Upgrade. Default to false.
552
		 */
553
		return apply_filters( 'safecss_is_freetrial', false );
554
	}
555
556
	static function get_preprocessor_key() {
557
		$safecss_post = Jetpack_Custom_CSS::get_current_revision();
558
		return get_post_meta( $safecss_post['ID'], 'custom_css_preprocessor', true );
559
	}
560
561
	static function get_preprocessor() {
562
		/** This filter is documented in modules/custom-css/custom-css.php */
563
		$preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() );
564
		$selected_preprocessor_key = self::get_preprocessor_key();
565
		$selected_preprocessor = isset( $preprocessors[ $selected_preprocessor_key ] ) ? $preprocessors[ $selected_preprocessor_key ] : null;
566
		return $selected_preprocessor;
567
	}
568
569
	static function get_css( $compressed = false ) {
570
		/**
571
		 * Filter the Custom CSS returned.
572
		 * Can be used to return an error, or no CSS at all.
573
		 *
574
		 * @module custom-css
575
		 *
576
		 * @since 1.7.0
577
		 *
578
		 * @param bool false Should we return an error instead of the Custom CSS. Default to false.
579
		 */
580
		$default_css = apply_filters( 'safecss_get_css_error', false );
581
582
		if ( $default_css !== false )
583
			return $default_css;
584
585
		$option = ( Jetpack_Custom_CSS::is_preview() || Jetpack_Custom_CSS::is_freetrial() ) ? 'safecss_preview' : 'safecss';
586
		$css = '';
587
588
		if ( 'safecss' == $option ) {
589
			// Don't bother checking for a migrated 'safecss' option if it never existed.
590
			if ( false === get_option( 'safecss' ) || get_option( 'safecss_revision_migrated' ) ) {
591
				$safecss_post = Jetpack_Custom_CSS::get_post();
592
				if ( ! empty( $safecss_post ) ) {
593
					$css = ( $compressed && $safecss_post['post_content_filtered'] ) ? $safecss_post['post_content_filtered'] : $safecss_post['post_content'];
594
				}
595
			} else {
596
				$current_revision = Jetpack_Custom_CSS::get_current_revision();
597
				if ( false === $current_revision ) {
598
					$css = '';
599
				} else {
600
					$css = ( $compressed && $current_revision['post_content_filtered'] ) ? $current_revision['post_content_filtered'] : $current_revision['post_content'];
601
				}
602
			}
603
604
			// Fix for un-migrated Custom CSS
605
			if ( empty( $safecss_post ) ) {
606
				$_css = get_option( 'safecss' );
607
				if ( !empty( $_css ) ) {
608
					$css = $_css;
609
				}
610
			}
611
		}
612
		else if ( 'safecss_preview' == $option ) {
613
			$safecss_post = Jetpack_Custom_CSS::get_current_revision();
614
			$css = $safecss_post['post_content'];
615
			$css = Jetpack_Custom_CSS::minify( $css, get_post_meta( $safecss_post['ID'], 'custom_css_preprocessor', true ) );
616
		}
617
618
		$css = str_replace( array( '\\\00BB \\\0020', '\0BB \020', '0BB 020' ), '\00BB \0020', $css );
619
620
		if ( empty( $css ) ) {
621
			$css = "/*\n"
622
				. wordwrap(
623
					/**
624
					 * Filter the default message displayed in the Custom CSS editor.
625
					 *
626
					 * @module custom-css
627
					 *
628
					 * @since 1.7.0
629
					 *
630
					 * @param string $str Default Custom CSS editor content.
631
					 */
632
					apply_filters(
633
						'safecss_default_css',
634
						__(
635
							"Welcome to Custom CSS!\n\nTo learn how this works, see http://wp.me/PEmnE-Bt",
636
							'jetpack'
637
						)
638
					)
639
				)
640
				. "\n*/";
641
		}
642
643
		/**
644
		 * Filter the Custom CSS returned from the editor.
645
		 *
646
		 * @module custom-css
647
		 *
648
		 * @since 1.7.0
649
		 *
650
		 * @param string $css Custom CSS.
651
		 */
652
		$css = apply_filters( 'safecss_css', $css );
653
654
		return $css;
655
	}
656
657
	static function replace_insecure_urls( $css ) {
658
		if ( ! function_exists( '_sa_get_frontend_https_url_replacement_map' ) ) {
659
			return $css;
660
		}
661
		list( $http_urls, $secure_urls ) = _sa_get_frontend_https_url_replacement_map();
662
663
		return str_replace( $http_urls, $secure_urls, $css );
664
	}
665
666
	static function print_css() {
667
668
		/**
669
		 * Fires right before printing the custom CSS inside the <head> element.
670
		 *
671
		 * @module custom-css
672
		 *
673
		 * @since 1.7.0
674
		 */
675
		do_action( 'safecss_print_pre' );
676
		$css = Jetpack_Custom_CSS::get_css( true );
677
		echo self::replace_insecure_urls( $css );
678
	}
679
680
	static function should_we_inline_custom_css( $should_we, $css ) {
681
		// If the CSS is less than 2,000 characters, inline it! otherwise return what was passed in.
682
		return ( strlen( $css ) < 2000 ) ? true : $should_we;
683
	}
684
685
	static function link_tag() {
686
		global $blog_id, $current_blog;
687
688
		if (
689
			/**
690
			 * Do not include any CSS on the page if the CSS includes an error.
691
			 * Setting this filter to true stops any Custom CSS from being enqueued.
692
			 *
693
			 * @module custom-css
694
			 *
695
			 * @since 1.7.0
696
			 *
697
			 * @param bool false Does the CSS include an error. Default to false.
698
			 */
699
			apply_filters( 'safecss_style_error', false )
700
		) {
701
			return;
702
		}
703
704
		if ( ! is_super_admin() && isset( $current_blog ) && ( 1 == $current_blog->spam || 1 == $current_blog->deleted ) )
705
			return;
706
707
		if ( Jetpack_Custom_CSS::is_customizer_preview() )
708
			return;
709
710
		$css    = '';
711
		$option = Jetpack_Custom_CSS::is_preview() ? 'safecss_preview' : 'safecss';
712
713
		if ( 'safecss' == $option ) {
714
			if ( Jetpack_Options::get_option_and_ensure_autoload( 'safecss_revision_migrated', '0' ) ) {
715
				$safecss_post = Jetpack_Custom_CSS::get_post();
716
717
				if ( ! empty( $safecss_post['post_content'] ) ) {
718
					$css = $safecss_post['post_content'];
719
				}
720 View Code Duplication
			} else {
721
				$current_revision = Jetpack_Custom_CSS::get_current_revision();
722
723
				if ( ! empty( $current_revision['post_content'] ) ) {
724
					$css = $current_revision['post_content'];
725
				}
726
			}
727
728
			// Fix for un-migrated Custom CSS
729
			if ( empty( $safecss_post ) ) {
730
				$_css = Jetpack_Options::get_option_and_ensure_autoload( 'safecss', '' );
731
				if ( !empty( $_css ) ) {
732
					$css = $_css;
733
				}
734
			}
735
		}
736
737 View Code Duplication
		if ( 'safecss_preview' == $option ) {
738
			$safecss_post = Jetpack_Custom_CSS::get_current_revision();
739
740
			if ( !empty( $safecss_post['post_content'] ) ) {
741
				$css = $safecss_post['post_content'];
742
			}
743
		}
744
745
		$css = str_replace( array( '\\\00BB \\\0020', '\0BB \020', '0BB 020' ), '\00BB \0020', $css );
746
747
		if ( $css == '' )
748
			return;
749
750
		if (
751
			/**
752
			 * Allow inserting CSS inline instead of through a separate file.
753
			 *
754
			 * @module custom-css
755
			 *
756
			 * @since 3.4.0
757
			 *
758
			 * @param bool false Should the CSS be added inline instead of through a separate file. Default to false.
759
			 * @param string $css Custom CSS.
760
			 */
761
			apply_filters( 'safecss_embed_style', false, $css )
762
		) {
763
764
			echo "\r\n" . '<style id="custom-css-css">' . Jetpack_Custom_CSS::get_css( true ) . "</style>\r\n";
765
766
		} else {
767
768
			$href = home_url( '/' );
769
			$href = add_query_arg( 'custom-css', 1, $href );
770
			$href = add_query_arg( 'csblog', $blog_id, $href );
771
			$href = add_query_arg( 'cscache', 6, $href );
772
			$href = add_query_arg( 'csrev', (int) get_option( $option . '_rev' ), $href );
773
774
			/**
775
			 * Filter the Custom CSS link enqueued in the head.
776
			 *
777
			 * @module custom-css
778
			 *
779
			 * @since 1.7.0
780
			 *
781
			 * @param string $href Custom CSS link enqueued in the head.
782
			 * @param string $blog_id Blog ID.
783
			 */
784
			$href = apply_filters( 'safecss_href', $href, $blog_id );
785
786
			if ( Jetpack_Custom_CSS::is_preview() )
787
				$href = add_query_arg( 'csspreview', 'true', $href );
788
789
			?>
790
			<link rel="stylesheet" id="custom-css-css" type="text/css" href="<?php echo esc_url( $href ); ?>" />
791
			<?php
792
793
		}
794
795
		/**
796
		 * Fires after creating the <link> in the <head> element for the custom css stylesheet.
797
		 *
798
		 * @module custom-css
799
		 *
800
		 * @since 2.2.2
801
		 */
802
		do_action( 'safecss_link_tag_post' );
803
	}
804
805
	static function style_filter( $current ) {
806
		if ( Jetpack_Custom_CSS::is_freetrial() && ( ! Jetpack_Custom_CSS::is_preview() || ! current_user_can( 'switch_themes' ) ) )
807
			return $current;
808
		else if ( Jetpack_Custom_CSS::skip_stylesheet() )
809
			/**
810
			 * Filter the default blank Custom CSS URL.
811
			 *
812
			 * @module custom-css
813
			 *
814
			 * @since 2.2.1
815
			 *
816
			 * @param string $url Default blank Custom CSS URL.
817
			 */
818
			return apply_filters( 'safecss_style_filter_url', plugins_url( 'custom-css/css/blank.css', __FILE__ ) );
819
820
		return $current;
821
	}
822
823
	static function buffer( $html ) {
824
		$html = str_replace( '</body>', Jetpack_Custom_CSS::preview_flag(), $html );
825
		return preg_replace_callback( '!href=([\'"])(.*?)\\1!', array( 'Jetpack_Custom_CSS', 'preview_links' ), $html );
826
	}
827
828
	static function preview_links( $matches ) {
829
		if ( 0 !== strpos( $matches[2], get_option( 'home' ) ) )
830
			return $matches[0];
831
832
		$link = wp_specialchars_decode( $matches[2] );
833
		$link = add_query_arg( 'csspreview', 'true', $link );
834
		$link = esc_url( $link );
835
		return "href={$matches[1]}$link{$matches[1]}";
836
	}
837
838
	/**
839
	 * Places a black bar above every preview page
840
	 */
841
	static function preview_flag() {
842
		if ( is_admin() )
843
			return;
844
845
		$message = esc_html__( 'Preview: changes must be saved or they will be lost', 'jetpack' );
846
		/**
847
		 * Filter the Preview message displayed on the site when previewing custom CSS, before to save it.
848
		 *
849
		 * @module custom-css
850
		 *
851
		 * @since 1.7.0
852
		 *
853
		 * @param string $message Custom CSS preview message.
854
		 */
855
		$message = apply_filters( 'safecss_preview_message', $message );
856
857
		$preview_flag_js = "var flag = document.createElement('div');
858
		flag.innerHTML = " . json_encode( $message ) . ";
859
		flag.style.background = '#FF6600';
860
		flag.style.color = 'white';
861
		flag.style.textAlign = 'center';
862
		flag.style.fontSize = '15px';
863
		flag.style.padding = '2px';
864
		flag.style.fontFamily = 'sans-serif';
865
		document.body.style.paddingTop = '0px';
866
		document.body.insertBefore(flag, document.body.childNodes[0]);
867
		";
868
869
		/**
870
		 * Filter the Custom CSS preview message JS styling.
871
		 *
872
		 * @module custom-css
873
		 *
874
		 * @since 1.7.0
875
		 *
876
		 * @param string $preview_flag_js Custom CSS preview message JS styling.
877
		 */
878
		$preview_flag_js = apply_filters( 'safecss_preview_flag_js', $preview_flag_js );
879
		if ( $preview_flag_js ) {
880
			$preview_flag_js = '<script type="text/javascript">
881
	// <![CDATA[
882
	' . $preview_flag_js . '
883
	// ]]>
884
	</script>';
885
		}
886
887
		return $preview_flag_js;
888
	}
889
890
	static function menu() {
891
		$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...
892
		$title = __( 'Edit CSS', 'jetpack' );
893
		$hook = add_theme_page( $title, $title, 'edit_theme_options', 'editcss', array( 'Jetpack_Custom_CSS', 'admin' ) );
894
895
		add_action( "load-revision.php", array( 'Jetpack_Custom_CSS', 'prettify_post_revisions' ) );
896
		add_action( "load-$hook", array( 'Jetpack_Custom_CSS', 'update_title' ) );
897
	}
898
899
	/**
900
	 * Adds a menu item in the appearance section for this plugin's administration
901
	 * page. Also adds hooks to enqueue the CSS and JS for the admin page.
902
	 */
903
	static function update_title() {
904
		global $title;
905
		$title = __( 'CSS', 'jetpack' );
906
	}
907
908
	static function prettify_post_revisions() {
909
		add_filter( 'the_title', array( 'Jetpack_Custom_CSS', 'post_title' ), 10, 2 );
910
	}
911
912
	static function post_title( $title, $post_id ) {
913
		if ( !$post_id = (int) $post_id ) {
914
			return $title;
915
		}
916
917
		if ( !$post = get_post( $post_id ) ) {
918
			return $title;
919
		}
920
921
		if ( 'safecss' != $post->post_type ) {
922
			return $title;
923
		}
924
925
		return __( 'Custom CSS Stylesheet', 'jetpack' );
926
	}
927
928
	static function enqueue_scripts( $hook ) {
929
		if ( 'appearance_page_editcss' != $hook )
930
			return;
931
932
		wp_enqueue_script( 'postbox' );
933
		wp_enqueue_script( 'custom-css-editor', plugins_url( 'custom-css/js/css-editor.js', __FILE__ ), 'jquery', '20130325', true );
934
		wp_enqueue_style( 'custom-css-editor', plugins_url( 'custom-css/css/css-editor.css', __FILE__ ) );
935
936
		if ( defined( 'SAFECSS_USE_ACE' ) && SAFECSS_USE_ACE ) {
937
			wp_register_style( 'jetpack-css-codemirror', plugins_url( 'custom-css/css/codemirror.css', __FILE__ ), array(), '20120905' );
938
			wp_enqueue_style( 'jetpack-css-use-codemirror', plugins_url( 'custom-css/css/use-codemirror.css', __FILE__ ), array( 'jetpack-css-codemirror' ), '20120905' );
939
940
			wp_register_script( 'jetpack-css-codemirror', plugins_url( 'custom-css/js/codemirror.min.js', __FILE__ ), array(), '3.16', true );
941
			wp_enqueue_script( 'jetpack-css-use-codemirror', plugins_url( 'custom-css/js/use-codemirror.js', __FILE__ ), array( 'jquery', 'underscore', 'jetpack-css-codemirror' ), '20131009', true );
942
		}
943
	}
944
945
	static function saved_message() {
946
		echo '<div id="message" class="updated fade"><p><strong>' . __( 'Stylesheet saved.', 'jetpack' ) . '</strong></p></div>';
947
	}
948
949
	static function admin() {
950
		add_meta_box( 'submitdiv', __( 'Publish', 'jetpack' ), array( __CLASS__, 'publish_box' ), 'editcss', 'side' );
951
		add_action( 'custom_css_submitbox_misc_actions', array( __CLASS__, 'content_width_settings' ) );
952
953
		$safecss_post = Jetpack_Custom_CSS::get_post();
954
955
		if ( ! empty( $safecss_post ) && 0 < $safecss_post['ID'] && wp_get_post_revisions( $safecss_post['ID'], array( 'posts_per_page' => 1 ) ) )
956
			add_meta_box( 'revisionsdiv', __( 'CSS Revisions', 'jetpack' ), array( __CLASS__, 'revisions_meta_box' ), 'editcss', 'side' );
957
		?>
958
		<div class="wrap">
959
			<?php
960
961
			/**
962
			 * Fires right before the custom css page begins.
963
			 *
964
			 * @module custom-css
965
			 *
966
			 * @since 1.7.0
967
			 */
968
			do_action( 'custom_design_header' );
969
970
			?>
971
			<h1><?php _e( 'CSS Stylesheet Editor', 'jetpack' ); ?></h1>
972
			<form id="safecssform" action="" method="post">
973
				<?php wp_nonce_field( 'safecss' ) ?>
974
				<?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); ?>
975
				<?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
976
				<input type="hidden" name="action" value="save" />
977
				<div id="poststuff">
978
					<p class="css-support">
979
					<?php
980
						/**
981
						 * Filter the intro text appearing above the Custom CSS Editor.
982
						 *
983
						 * @module custom-css
984
						 *
985
						 * @since 1.7.0
986
						 *
987
						 * @param string $str Intro text appearing above the Custom CSS editor.
988
						 */
989
						echo apply_filters( 'safecss_intro_text', __( 'New to CSS? Start with a <a href="http://www.htmldog.com/guides/cssbeginner/" target="_blank">beginner tutorial</a>. Questions?
990
		Ask in the <a href="https://wordpress.org/support/forum/themes-and-templates" target="_blank">Themes and Templates forum</a>.', 'jetpack' ) );
991
					?></p>
992
					<p class="css-support"><?php echo __( 'Note: Custom CSS will be reset when changing themes.', 'jetpack' ); ?></p>
993
994
					<div id="post-body" class="metabox-holder columns-2">
995
						<div id="post-body-content">
996
							<div class="postarea">
997
								<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>
998
								<div class="clear"></div>
999
							</div>
1000
						</div>
1001
						<div id="postbox-container-1" class="postbox-container">
1002
						<?php do_meta_boxes( 'editcss', 'side', $safecss_post ); ?>
1003
					</div>
1004
					</div>
1005
					<br class="clear" />
1006
				</div>
1007
			</form>
1008
		</div>
1009
		<?php
1010
	}
1011
1012
	/**
1013
	 * Content width setting callback
1014
	 */
1015
	static function content_width_settings() {
1016
		$safecss_post = Jetpack_Custom_CSS::get_current_revision();
1017
1018
		$custom_content_width = get_post_meta( $safecss_post['ID'], 'content_width', true );
1019
1020
		// If custom content width hasn't been overridden and the theme has a content_width value, use that as a default.
1021
		if ( $custom_content_width <= 0 && ! empty( $GLOBALS['content_width'] ) )
1022
			$custom_content_width = $GLOBALS['content_width'];
1023
1024
		if ( ! $custom_content_width || ( isset( $GLOBALS['content_width'] ) && $custom_content_width == $GLOBALS['content_width'] ) )
1025
			$custom_content_width = '';
1026
1027
		?>
1028
		<div class="misc-pub-section">
1029
			<label><?php esc_html_e( 'Media Width:', 'jetpack' ); ?></label>
1030
			<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>
1031
			<a class="edit-content-width hide-if-no-js" href="#content-width"><?php echo esc_html_e( 'Edit', 'jetpack' ); ?></a>
1032
			<div id="content-width-select" class="hide-if-js">
1033
				<input type="hidden" name="custom_content_width" id="custom_content_width" value="<?php echo esc_attr( $custom_content_width ); ?>" />
1034
				<p>
1035
					<?php
1036
1037
					printf( /* translators: %1$s is replaced with an input field for numbers. */
1038
						__( 'Limit width to %1$s pixels for full size images. (<a href="%2$s" target="_blank">More info</a>.)', 'jetpack' ),
1039
						'<input type="text" id="custom_content_width_visible" value="' . esc_attr( $custom_content_width ) . '" size="4" />',
1040
						/**
1041
						 * Filter the Custom CSS limited width's support doc URL.
1042
						 *
1043
						 * @module custom-css
1044
						 *
1045
						 * @since 2.2.3
1046
						 *
1047
						 * @param string $url Custom CSS limited width's support doc URL.
1048
						 */
1049
						apply_filters( 'safecss_limit_width_link', 'http://jetpack.com/support/custom-css/#limited-width' )
1050
					);
1051
1052
					?>
1053
				</p>
1054
				<?php
1055
1056
				if (
1057
					! empty( $GLOBALS['content_width'] )
1058
					&& $custom_content_width != $GLOBALS['content_width']
1059
				) {
1060
					$current_theme = wp_get_theme()->Name;
1061
1062
					?>
1063
					<p><?php printf( _n( 'The default content width for the %s theme is %d pixel.', 'The default content width for the %s theme is %d pixels.', intval( $GLOBALS['content_width'] ), 'jetpack' ), $current_theme, intval( $GLOBALS['content_width'] ) ); ?></p>
1064
					<?php
1065
				}
1066
1067
				?>
1068
				<a class="save-content-width hide-if-no-js button" href="#content-width"><?php esc_html_e( 'OK', 'jetpack' ); ?></a>
1069
				<a class="cancel-content-width hide-if-no-js" href="#content-width"><?php esc_html_e( 'Cancel', 'jetpack' ); ?></a>
1070
			</div>
1071
			<script type="text/javascript">
1072
				jQuery( function ( $ ) {
1073
					var defaultContentWidth = <?php echo isset( $GLOBALS['content_width'] ) ? json_encode( intval( $GLOBALS['content_width'] ) ) : 0; ?>;
1074
1075
					$( '.edit-content-width' ).bind( 'click', function ( e ) {
1076
						e.preventDefault();
1077
1078
						$( '#content-width-select' ).slideDown();
1079
						$( this ).hide();
1080
					} );
1081
1082
					$( '.cancel-content-width' ).bind( 'click', function ( e ) {
1083
						e.preventDefault();
1084
1085
						$( '#content-width-select' ).slideUp( function () {
1086
							$( '.edit-content-width' ).show();
1087
							$( '#custom_content_width_visible' ).val( $( '#custom_content_width' ).val() );
1088
						} );
1089
					} );
1090
1091
					$( '.save-content-width' ).bind( 'click', function ( e ) {
1092
						e.preventDefault();
1093
1094
						$( '#content-width-select' ).slideUp();
1095
1096
						var newContentWidth = parseInt( $( '#custom_content_width_visible' ).val(), 10 );
1097
1098
						if ( newContentWidth && newContentWidth != defaultContentWidth ) {
1099
							$( '#content-width-display' ).text(
1100
								$( '#content-width-display' )
1101
									.data( 'custom-text' )
1102
										.replace( '%s', $( '#custom_content_width_visible' ).val() )
1103
							);
1104
						}
1105
						else {
1106
							$( '#content-width-display' ).text( $( '#content-width-display' ).data( 'default-text' ) );
1107
						}
1108
1109
						$( '#custom_content_width' ).val( $( '#custom_content_width_visible' ).val() );
1110
						$( '.edit-content-width' ).show();
1111
					} );
1112
				} );
1113
			</script>
1114
		</div>
1115
		<?php
1116
	}
1117
1118
	static function publish_box() {
1119
		?>
1120
		<div id="minor-publishing">
1121
			<div id="misc-publishing-actions">
1122
				<?php
1123
1124
				/**
1125
				 * Filter the array of available Custom CSS preprocessors.
1126
				 *
1127
				 * @module custom-css
1128
				 *
1129
				 * @since 2.0.3
1130
				 *
1131
				 * @param array array() Empty by default.
1132
				 */
1133
				$preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() );
1134
1135
				if ( ! empty( $preprocessors ) ) {
1136
					$safecss_post = Jetpack_Custom_CSS::get_current_revision();
1137
					$selected_preprocessor_key = get_post_meta( $safecss_post['ID'], 'custom_css_preprocessor', true );
1138
					$selected_preprocessor = isset( $preprocessors[$selected_preprocessor_key] ) ? $preprocessors[$selected_preprocessor_key] : null;
1139
1140
					?>
1141
					<div class="misc-pub-section">
1142
						<label><?php esc_html_e( 'Preprocessor:', 'jetpack' ); ?></label>
1143
						<span id="preprocessor-display"><?php echo esc_html( $selected_preprocessor ? $selected_preprocessor['name'] : __( 'None', 'jetpack' ) ); ?></span>
1144
						<a class="edit-preprocessor hide-if-no-js" href="#preprocessor"><?php echo esc_html_e( 'Edit', 'jetpack' ); ?></a>
1145
						<div id="preprocessor-select" class="hide-if-js">
1146
							<input type="hidden" name="custom_css_preprocessor" id="custom_css_preprocessor" value="<?php echo esc_attr( $selected_preprocessor_key ); ?>" />
1147
							<select id="preprocessor_choices">
1148
								<option value=""><?php esc_html_e( 'None', 'jetpack' ); ?></option>
1149
								<?php
1150
1151
								foreach ( $preprocessors as $preprocessor_key => $preprocessor ) {
1152
								?>
1153
									<option value="<?php echo esc_attr( $preprocessor_key ); ?>" <?php selected( $selected_preprocessor_key, $preprocessor_key ); ?>><?php echo esc_html( $preprocessor['name'] ); ?></option>
1154
									<?php
1155
								}
1156
1157
								?>
1158
							</select>
1159
							<a class="save-preprocessor hide-if-no-js button" href="#preprocessor"><?php esc_html_e( 'OK', 'jetpack' ); ?></a>
1160
							<a class="cancel-preprocessor hide-if-no-js" href="#preprocessor"><?php esc_html_e( 'Cancel', 'jetpack' ); ?></a>
1161
						</div>
1162
					</div>
1163
					<?php
1164
				}
1165
1166
				$safecss_post = Jetpack_Custom_CSS::get_current_revision();
1167
1168
				$add_css = ( get_post_meta( $safecss_post['ID'], 'custom_css_add', true ) != 'no' );
1169
1170
				?>
1171
				<div class="misc-pub-section">
1172
					<label><?php esc_html_e( 'Mode:', 'jetpack' ); ?></label>
1173
					<span id="css-mode-display"><?php echo esc_html( $add_css ? __( 'Add-on', 'jetpack' ) : __( 'Replacement', 'jetpack' ) ); ?></span>
1174
					<a class="edit-css-mode hide-if-no-js" href="#css-mode"><?php echo esc_html_e( 'Edit', 'jetpack' ); ?></a>
1175
					<div id="css-mode-select" class="hide-if-js">
1176
						<input type="hidden" name="add_to_existing" id="add_to_existing" value="<?php echo $add_css ? 'true' : 'false'; ?>" />
1177
						<p>
1178
							<label>
1179
								<input type="radio" name="add_to_existing_display" value="true" <?php checked( $add_css ); ?>/>
1180
								<?php _e( 'Add-on CSS <b>(Recommended)</b>', 'jetpack' ); ?>
1181
							</label>
1182
							<br />
1183
							<label>
1184
								<input type="radio" name="add_to_existing_display" value="false" <?php checked( ! $add_css ); ?>/>
1185
								<?php printf(
1186
									__( 'Replace <a href="%s">theme\'s CSS</a> <b>(Advanced)</b>', 'jetpack' ),
1187
									/**
1188
									 * Filter the theme's stylesheet URL.
1189
									 *
1190
									 * @module custom-css
1191
									 *
1192
									 * @since 1.7.0
1193
									 *
1194
									 * @param string $url Active theme's stylesheet URL. Default to get_stylesheet_uri().
1195
									 */
1196
									apply_filters( 'safecss_theme_stylesheet_url', get_stylesheet_uri() )
1197
								); ?>
1198
							</label>
1199
						</p>
1200
						<a class="save-css-mode hide-if-no-js button" href="#css-mode"><?php esc_html_e( 'OK', 'jetpack' ); ?></a>
1201
						<a class="cancel-css-mode hide-if-no-js" href="#css-mode"><?php esc_html_e( 'Cancel', 'jetpack' ); ?></a>
1202
					</div>
1203
				</div>
1204
				<?php
1205
1206
				/**
1207
				 * Allows addition of elements to the submit box for custom css on the wp-admin side.
1208
				 *
1209
				 * @module custom-css
1210
				 *
1211
				 * @since 2.0.3
1212
				 */
1213
				do_action( 'custom_css_submitbox_misc_actions' );
1214
1215
				?>
1216
			</div>
1217
		</div>
1218
		<div id="major-publishing-actions">
1219
			<input type="button" class="button" id="preview" name="preview" value="<?php esc_attr_e( 'Preview', 'jetpack' ) ?>" />
1220
			<div id="publishing-action">
1221
				<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' ); ?>" />
1222
			</div>
1223
		</div>
1224
		<?php
1225
	}
1226
1227
	/**
1228
	 * Render metabox listing CSS revisions and the themes that correspond to the revisions.
1229
	 * Called by safecss_admin
1230
	 *
1231
	 * @global $post
1232
	 * @param array $safecss_post
1233
	 * @uses wp_revisions_to_keep
1234
	 * @uses WP_Query
1235
	 * @uses wp_post_revision_title
1236
	 * @uses esc_html
1237
	 * @uses add_query_arg
1238
	 * @uses menu_page_url
1239
	 * @uses wp_reset_query
1240
	 * @return string
1241
	 */
1242
	static function revisions_meta_box( $safecss_post ) {
1243
1244
		$show_all_revisions = isset( $_GET['show_all_rev'] );
1245
1246
		if ( function_exists( 'wp_revisions_to_keep' ) ) {
1247
			$max_revisions = wp_revisions_to_keep( (object) $safecss_post );
1248
		} else {
1249
			$max_revisions = defined( 'WP_POST_REVISIONS' ) && is_numeric( WP_POST_REVISIONS ) ? (int) WP_POST_REVISIONS : 25;
1250
		}
1251
1252
		$posts_per_page = $show_all_revisions ? $max_revisions : 6;
1253
1254
		$revisions = new WP_Query( array(
1255
			'posts_per_page' => $posts_per_page,
1256
			'post_type' => 'revision',
1257
			'post_status' => 'inherit',
1258
			'post_parent' => $safecss_post['ID'],
1259
			'orderby' => 'date',
1260
			'order' => 'DESC'
1261
		) );
1262
1263
		if ( $revisions->have_posts() ) { ?>
1264
			<ul class="post-revisions"><?php
1265
1266
			global $post;
1267
1268
			while ( $revisions->have_posts() ) :
1269
				$revisions->the_post();
1270
1271
				?><li>
1272
					<?php
1273
						echo wp_post_revision_title( $post );
1274
1275
						if ( ! empty( $post->post_excerpt ) )
1276
							echo ' (' . esc_html( $post->post_excerpt ) . ')';
1277
					?>
1278
				</li><?php
1279
1280
			endwhile;
1281
1282
			?></ul><?php
1283
1284
			if ( $revisions->found_posts > 6 && !$show_all_revisions ) {
1285
				?>
1286
				<br>
1287
				<a href="<?php echo add_query_arg( 'show_all_rev', 'true', menu_page_url( 'editcss', false ) ); ?>"><?php esc_html_e( 'Show all', 'jetpack' ); ?></a>
1288
				<?php
1289
			}
1290
		}
1291
1292
		wp_reset_query();
1293
	}
1294
1295
	/**
1296
	 * Hook in init at priority 11 to disable custom CSS.
1297
	 */
1298
	static function disable() {
1299
		remove_action( 'wp_head', array( 'Jetpack_Custom_CSS', 'link_tag' ), 101 );
1300
	    remove_filter( 'stylesheet_uri', array( 'Jetpack_Custom_CSS', 'style_filter' ) );
1301
	}
1302
1303
	/**
1304
	 * Reset all aspects of Custom CSS on a theme switch so that changing
1305
	 * themes is a sure-fire way to get a clean start.
1306
	 */
1307
	static function reset() {
1308
		$safecss_post_id = Jetpack_Custom_CSS::save_revision( '' );
1309
		$safecss_revision = Jetpack_Custom_CSS::get_current_revision();
1310
1311
		update_option( 'safecss_rev', intval( get_option( 'safecss_rev' ) ) + 1 );
1312
1313
		update_post_meta( $safecss_post_id, 'custom_css_add', 'yes' );
1314
		update_post_meta( $safecss_post_id, 'content_width', false );
1315
		update_post_meta( $safecss_post_id, 'custom_css_preprocessor', '' );
1316
1317
		delete_option( 'safecss_add' );
1318
		delete_option( 'safecss_content_width' );
1319
1320
		update_metadata( 'post', $safecss_revision['ID'], 'custom_css_add', 'yes' );
1321
		update_metadata( 'post', $safecss_revision['ID'], 'content_width', false );
1322
		update_metadata( 'post', $safecss_revision['ID'], 'custom_css_preprocessor', '' );
1323
1324
		delete_option( 'safecss_preview_add' );
1325
	}
1326
1327
	static function is_customizer_preview() {
1328
		if ( isset ( $GLOBALS['wp_customize'] ) )
1329
			return ! $GLOBALS['wp_customize']->is_theme_active();
1330
1331
		return false;
1332
	}
1333
1334
	static function minify( $css, $preprocessor = '' ) {
1335
		if ( ! $css )
1336
			return '';
1337
1338 View Code Duplication
		if ( $preprocessor ) {
1339
			/** This filter is documented in modules/custom-css/custom-css.php */
1340
			$preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() );
1341
1342
			if ( isset( $preprocessors[$preprocessor] ) ) {
1343
				$css = call_user_func( $preprocessors[$preprocessor]['callback'], $css );
1344
			}
1345
		}
1346
1347
		safecss_class();
1348
		$csstidy = new csstidy();
1349
		$csstidy->optimise = new safecss( $csstidy );
0 ignored issues
show
Documentation introduced by
$csstidy is of type object<csstidy>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1350
1351
		$csstidy->set_cfg( 'remove_bslash',              false );
1352
		$csstidy->set_cfg( 'compress_colors',            true );
1353
		$csstidy->set_cfg( 'compress_font-weight',       true );
1354
		$csstidy->set_cfg( 'remove_last_;',              true );
1355
		$csstidy->set_cfg( 'case_properties',            true );
1356
		$csstidy->set_cfg( 'discard_invalid_properties', true );
1357
		$csstidy->set_cfg( 'css_level',                  'CSS3.0' );
1358
		$csstidy->set_cfg( 'template', 'highest');
1359
		$csstidy->parse( $css );
1360
1361
		return $csstidy->print->plain();
1362
	}
1363
1364
	/**
1365
	 * When restoring a SafeCSS post revision, also copy over the
1366
	 * content_width and custom_css_add post metadata.
1367
	 */
1368
	static function restore_revision( $_post_id, $_revision_id ) {
1369
		$_post = get_post( $_post_id );
1370
1371
		if ( 'safecss' != $_post->post_type )
1372
			return;
1373
1374
		$safecss_revision = Jetpack_Custom_CSS::get_current_revision();
1375
1376
		$content_width = get_post_meta( $_revision_id, 'content_width', true );
1377
		$custom_css_add = get_post_meta( $_revision_id, 'custom_css_add', true );
1378
		$preprocessor = get_post_meta( $_revision_id, 'custom_css_preprocessor', true );
1379
1380
		update_metadata( 'post', $safecss_revision['ID'], 'content_width', $content_width );
1381
		update_metadata( 'post', $safecss_revision['ID'], 'custom_css_add', $custom_css_add );
1382
		update_metadata( 'post', $safecss_revision['ID'], 'custom_css_preprocessor', $preprocessor );
1383
1384
		delete_option( 'safecss_add' );
1385
		delete_option( 'safecss_content_width' );
1386
1387
		update_post_meta( $_post->ID, 'content_width', $content_width );
1388
		update_post_meta( $_post->ID, 'custom_css_add', $custom_css_add );
1389
		update_post_meta( $_post->ID, 'custom_css_preprocessor', $preprocessor );
1390
1391
		delete_option( 'safecss_preview_add' );
1392
	}
1393
1394
	/**
1395
	 * Migration routine for moving safecss from wp_options to wp_posts to support revisions
1396
	 *
1397
	 * @return void
1398
	 */
1399
	static function upgrade() {
1400
		$css = get_option( 'safecss' );
1401
1402
		if ( get_option( 'safecss_revision_migrated' ) ) {
1403
			return false;
1404
		}
1405
1406
		// Check if CSS is stored in wp_options
1407
		if ( $css ) {
1408
			// Remove the async actions from publish_post
1409
			remove_action( 'publish_post', 'queue_publish_post' );
1410
1411
			$post = array();
1412
			$post['post_content'] = $css;
1413
			$post['post_title'] = 'safecss';
1414
			$post['post_status'] = 'publish';
1415
			$post['post_type'] = 'safecss';
1416
1417
			// Insert the CSS into wp_posts
1418
			$post_id = wp_insert_post( $post );
1419
			// Check for errors
1420
			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...
1421
				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...
1422
1423
			// Delete safecss option
1424
			delete_option( 'safecss' );
1425
		}
1426
1427
		unset( $css );
1428
1429
		// Check if we have already done this
1430
		if ( !get_option( 'safecss_revision_migrated' ) ) {
1431
			define( 'DOING_MIGRATE', true );
1432
1433
			// Get hashes of safecss post and current revision
1434
			$safecss_post = Jetpack_Custom_CSS::get_post();
1435
1436
			if ( empty( $safecss_post ) )
1437
				return;
1438
1439
			$safecss_post_hash = md5( $safecss_post['post_content'] );
1440
			$current_revision = Jetpack_Custom_CSS::get_current_revision();
1441
1442
			if ( null == $current_revision )
1443
				return;
1444
1445
			$current_revision_hash = md5( $current_revision['post_content'] );
1446
1447
			// If hashes are not equal, set safecss post with content from current revision
1448
			if ( $safecss_post_hash !== $current_revision_hash ) {
1449
				Jetpack_Custom_CSS::save_revision( $current_revision['post_content'] );
1450
				// Reset post_content to display the migrated revsion
1451
				$safecss_post['post_content'] = $current_revision['post_content'];
1452
			}
1453
1454
			// Set option so that we dont keep doing this
1455
			update_option( 'safecss_revision_migrated', time() );
1456
		}
1457
1458
		$newest_safecss_post = Jetpack_Custom_CSS::get_current_revision();
1459
1460
		if ( $newest_safecss_post ) {
1461 View Code Duplication
			if ( get_option( 'safecss_content_width' ) ) {
1462
				// Add the meta to the post and the latest revision.
1463
				update_post_meta( $newest_safecss_post['ID'], 'content_width', get_option( 'safecss_content_width' ) );
1464
				update_metadata( 'post', $newest_safecss_post['ID'], 'content_width', get_option( 'safecss_content_width' ) );
1465
1466
				delete_option( 'safecss_content_width' );
1467
			}
1468
1469 View Code Duplication
			if ( get_option( 'safecss_add' ) ) {
1470
				update_post_meta( $newest_safecss_post['ID'], 'custom_css_add', get_option( 'safecss_add' ) );
1471
				update_metadata( 'post', $newest_safecss_post['ID'], 'custom_css_add', get_option( 'safecss_add' ) );
1472
1473
				delete_option( 'safecss_add' );
1474
			}
1475
		}
1476
	}
1477
1478
	/**
1479
	 * Adds a filter to the redirect location in `wp-admin/revisions.php`.
1480
	 */
1481
	static function add_revision_redirect() {
1482
		add_filter( 'wp_redirect', array( __CLASS__, 'revision_redirect' ) );
1483
	}
1484
1485
	/**
1486
	 * Filters the redirect location in `wp-admin/revisions.php`.
1487
	 *
1488
	 * @param string $location The path to redirect to.
1489
	 * @return string
1490
	 */
1491
	static function revision_redirect( $location ) {
1492
		$post = get_post();
1493
1494
		if ( ! empty( $post->post_type ) && 'safecss' == $post->post_type ) {
1495
			$location = 'themes.php?page=editcss';
1496
1497
			if ( 'edit.php' == $location ) {
1498
				$location = '';
1499
			}
1500
		}
1501
1502
		return $location;
1503
	}
1504
1505
	static function revision_post_link( $post_link, $post_id, $context ) {
1506
		if ( !$post_id = (int) $post_id ) {
1507
			return $post_link;
1508
		}
1509
1510
		if ( !$post = get_post( $post_id ) ) {
1511
			return $post_link;
1512
		}
1513
1514
		if ( 'safecss' != $post->post_type ) {
1515
			return $post_link;
1516
		}
1517
1518
		$post_link = admin_url( 'themes.php?page=editcss' );
1519
1520
		if ( 'display' == $context ) {
1521
			return esc_url( $post_link );
1522
		}
1523
1524
		return esc_url_raw( $post_link );
1525
	}
1526
1527
	/**
1528
	 * When on the edit screen, make sure the custom content width
1529
	 * setting is applied to the large image size.
1530
	 */
1531 View Code Duplication
	static function editor_max_image_size( $dims, $size = 'medium', $context = null ) {
1532
		list( $width, $height ) = $dims;
1533
1534
		if ( 'large' == $size && 'edit' == $context )
1535
			$width = Jetpack::get_content_width();
1536
1537
		return array( $width, $height );
1538
	}
1539
1540
	/**
1541
	 * Override the content_width with a custom value if one is set.
1542
	 */
1543
	static function jetpack_content_width( $content_width ) {
1544
		$custom_content_width = 0;
1545
1546
		if ( Jetpack_Custom_CSS::is_preview() ) {
1547
			$safecss_post = Jetpack_Custom_CSS::get_current_revision();
1548
			$custom_content_width = intval( get_post_meta( $safecss_post['ID'], 'content_width', true ) );
1549
		} else if ( ! Jetpack_Custom_CSS::is_freetrial() ) {
1550
			$custom_css_post_id = Jetpack_Custom_CSS::post_id();
1551
			if ( $custom_css_post_id )
1552
				$custom_content_width = intval( get_post_meta( $custom_css_post_id, 'content_width', true ) );
1553
		}
1554
1555
		if ( $custom_content_width > 0 )
1556
			$content_width = $custom_content_width;
1557
1558
		return $content_width;
1559
	}
1560
}
1561
1562
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...
1563
	static function filter_attr( $css, $element = 'div' ) {
1564
		safecss_class();
1565
1566
		$css = $element . ' {' . $css . '}';
1567
1568
		$csstidy = new csstidy();
1569
		$csstidy->optimise = new safecss( $csstidy );
0 ignored issues
show
Documentation introduced by
$csstidy is of type object<csstidy>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1570
		$csstidy->set_cfg( 'remove_bslash', false );
1571
		$csstidy->set_cfg( 'compress_colors', false );
1572
		$csstidy->set_cfg( 'compress_font-weight', false );
1573
		$csstidy->set_cfg( 'discard_invalid_properties', true );
1574
		$csstidy->set_cfg( 'merge_selectors', false );
1575
		$csstidy->set_cfg( 'remove_last_;', false );
1576
		$csstidy->set_cfg( 'css_level', 'CSS3.0' );
1577
1578
		$css = preg_replace( '/\\\\([0-9a-fA-F]{4})/', '\\\\\\\\$1', $css );
1579
		$css = wp_kses_split( $css, array(), array() );
1580
		$csstidy->parse( $css );
1581
1582
		$css = $csstidy->print->plain();
1583
1584
		$css = str_replace( array( "\n","\r","\t" ), '', $css );
1585
1586
		preg_match( "/^{$element}\s*{(.*)}\s*$/", $css, $matches );
1587
1588
		if ( empty( $matches[1] ) )
1589
			return '';
1590
1591
		return $matches[1];
1592
	}
1593
}
1594
1595
function migrate() {
1596
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::upgrade()' );
1597
1598
	return Jetpack_Custom_CSS::upgrade();
1599
}
1600
1601
function safecss_revision_redirect( $redirect ) {
1602
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::revision_redirect()' );
1603
1604
	return Jetpack_Custom_CSS::revision_redirect( $redirect );
1605
}
1606
1607
function safecss_revision_post_link( $post_link, $post_id, $context ) {
1608
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::revision_post_link()' );
1609
1610
	return Jetpack_Custom_CSS::revision_post_link( $post_link, $post_id, $context );
1611
}
1612
1613
function get_safecss_post() {
1614
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::get_post()' );
1615
1616
	return Jetpack_Custom_CSS::get_post();
1617
}
1618
1619
function custom_css_post_id() {
1620
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::post_id()' );
1621
1622
	return Jetpack_Custom_CSS::post_id();
1623
}
1624
1625
function get_current_revision() {
1626
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::get_current_revision()' );
1627
1628
	return Jetpack_Custom_CSS::get_current_revision();
1629
}
1630
1631
function save_revision( $css, $is_preview = false, $preprocessor = '' ) {
1632
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::save_revision()' );
1633
1634
	return Jetpack_Custom_CSS::save_revision( $css, $is_preview, $preprocessor );
1635
}
1636
1637
function safecss_skip_stylesheet() {
1638
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::skip_stylesheet()' );
1639
1640
	return Jetpack_Custom_CSS::skip_stylesheet();
1641
}
1642
1643
function safecss_init() {
1644
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::init()' );
1645
1646
	return Jetpack_Custom_CSS::init();
1647
}
1648
1649
function safecss_is_preview() {
1650
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::is_preview()' );
1651
1652
	return Jetpack_Custom_CSS::is_preview();
1653
}
1654
1655
function safecss_is_freetrial() {
1656
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::is_freetrial()' );
1657
1658
	return Jetpack_Custom_CSS::is_freetrial();
1659
}
1660
1661
function safecss( $compressed = false ) {
1662
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::get_css()' );
1663
1664
	return Jetpack_Custom_CSS::get_css( $compressed );
1665
}
1666
1667
function safecss_print() {
1668
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::print_css()' );
1669
1670
	return Jetpack_Custom_CSS::print_css();
1671
}
1672
1673
function safecss_style() {
1674
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::link_tag()' );
1675
1676
	return Jetpack_Custom_CSS::link_tag();
1677
}
1678
1679
function safecss_style_filter( $current ) {
1680
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::style_filter()' );
1681
1682
	return Jetpack_Custom_CSS::style_filter( $current );
1683
}
1684
1685
function safecss_buffer( $html ) {
1686
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::buffer()' );
1687
1688
	return Jetpack_Custom_CSS::buffer( $html );
1689
}
1690
1691
function safecss_preview_links( $matches ) {
1692
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::preview_links()' );
1693
1694
	return Jetpack_Custom_CSS::preview_links( $matches );
1695
}
1696
1697
function safecss_preview_flag() {
1698
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::preview_flag()' );
1699
1700
	return Jetpack_Custom_CSS::preview_flag();
1701
}
1702
1703
function safecss_menu() {
1704
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::menu()' );
1705
1706
	return Jetpack_Custom_CSS::menu();
1707
}
1708
1709
function update_title() {
1710
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::update_title()' );
1711
1712
	return Jetpack_Custom_CSS::update_title();
1713
}
1714
1715
function safecss_prettify_post_revisions() {
1716
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::prettify_post_revisions()' );
1717
1718
	return Jetpack_Custom_CSS::prettify_post_revisions();
1719
}
1720
1721
function safecss_remove_title_excerpt_from_revisions() {
1722
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::remove_title_excerpt_from_revisions()' );
1723
1724
	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...
1725
}
1726
1727
function safecss_post_title( $title, $post_id ) {
1728
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::post_title()' );
1729
1730
	return Jetpack_Custom_CSS::post_title( $title, $post_id );
1731
}
1732
1733
function safe_css_enqueue_scripts() {
1734
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::enqueue_scripts()' );
1735
1736
	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...
1737
}
1738
1739
function safecss_admin_head() {
1740
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::admin_head()' );
1741
1742
	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...
1743
}
1744
1745
function safecss_saved() {
1746
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::saved_message()' );
1747
1748
	return Jetpack_Custom_CSS::saved_message();
1749
}
1750
1751
function safecss_admin() {
1752
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::admin()' );
1753
1754
	return Jetpack_Custom_CSS::admin();
1755
}
1756
1757
function custom_css_meta_box() {
1758
	_deprecated_function( __FUNCTION__, '2.1', 'add_meta_box( $id, $title, $callback, \'editcss\', \'side\' )' );
1759
}
1760
1761
function custom_css_post_revisions_meta_box( $safecss_post ) {
1762
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::revisions_meta_box()' );
1763
1764
	return Jetpack_Custom_CSS::revisions_meta_box( $safecss_post );
1765
}
1766
1767
function disable_safecss_style() {
1768
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::disable()' );
1769
1770
	return Jetpack_Custom_CSS::disable();
1771
}
1772
1773
function custom_css_reset() {
1774
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::reset()' );
1775
1776
	return Jetpack_Custom_CSS::reset();
1777
}
1778
1779
function custom_css_is_customizer_preview() {
1780
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::is_customizer_preview()' );
1781
1782
	return Jetpack_Custom_CSS::is_customizer_preview();
1783
}
1784
1785
function custom_css_minify( $css, $preprocessor = '' ) {
1786
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::minify()' );
1787
1788
	return Jetpack_Custom_CSS::minify( $css, $preprocessor );
1789
}
1790
1791
function custom_css_restore_revision( $_post_id, $_revision_id ) {
1792
	_deprecated_function( __FUNCTION__, '2.1', 'Jetpack_Custom_CSS::restore_revision()' );
1793
1794
	return Jetpack_Custom_CSS::restore_revision( $_post_id, $_revision_id );
1795
}
1796
1797 View Code Duplication
if ( ! function_exists( 'safecss_class' ) ) :
1798
function safecss_class() {
0 ignored issues
show
Best Practice introduced by
The function safecss_class() has been defined more than once; this definition is ignored, only the first definition in modules/custom-css/custom-css-4.7.php (L1107-1142) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
1799
	// Wrapped so we don't need the parent class just to load the plugin
1800
	if ( class_exists('safecss') )
1801
		return;
1802
1803
	require_once( dirname( __FILE__ ) . '/csstidy/class.csstidy.php' );
1804
1805
	class safecss extends csstidy_optimise {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type safecss has been defined more than once; this definition is ignored, only the first definition in modules/custom-css/custom-css-4.7.php (L1118-1141) is considered.

This check looks for classes that have been defined more than once.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
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...
1806
1807
		function postparse() {
1808
1809
			/**
1810
			 * Fires after parsing the css.
1811
			 *
1812
			 * @module custom-css
1813
			 *
1814
			 * @since 1.8.0
1815
			 *
1816
			 * @param obj $this CSSTidy object.
1817
			 */
1818
			do_action( 'csstidy_optimize_postparse', $this );
1819
1820
			return parent::postparse();
1821
		}
1822
1823
		function subvalue() {
1824
1825
			/**
1826
			 * Fires before optimizing the Custom CSS subvalue.
1827
			 *
1828
			 * @module custom-css
1829
			 *
1830
			 * @since 1.8.0
1831
			 *
1832
			 * @param obj $this CSSTidy object.
1833
			 **/
1834
			do_action( 'csstidy_optimize_subvalue', $this );
1835
1836
			return parent::subvalue();
1837
		}
1838
	}
1839
}
1840
endif;
1841
1842
if ( ! function_exists( 'safecss_filter_attr' ) ) {
1843
	function safecss_filter_attr( $css, $element = 'div' ) {
1844
		return Jetpack_Safe_CSS::filter_attr( $css, $element );
1845
	}
1846
}
1847
1848
include_once dirname( __FILE__ ) . '/custom-css/preprocessors.php';
1849