Completed
Push — branch-4.4 ( 3609d2...3dd542 )
by
unknown
14:00 queued 06:34
created

Jetpack_Custom_CSS_Data_Migration::get_post()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 44
Code Lines 19

Duplication

Lines 44
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
eloc 19
nc 7
nop 0
dl 44
loc 44
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
class Jetpack_Custom_CSS_Data_Migration {
4
	public static function add_hooks() {
5
		add_action( 'init', array( __CLASS__, 'register_legacy_post_type' ) );
6
		add_action( 'admin_init', array( __CLASS__, 'do_migration' ) );
7
8
		include_once( dirname( __FILE__ ) . '/custom-css.php' );
9
		if ( ! is_admin() ) {
10
			add_action( 'init', array( 'Jetpack_Custom_CSS', 'init' ) );
11
		}
12
	}
13
14
	public static function do_migration() {
15
		Jetpack_Options::update_option( 'custom_css_4.7_migration', true );
16
		Jetpack::log( 'custom_css_4.7_migration', 'start' );
17
18
		if ( ! post_type_exists( 'safecss' ) ) {
19
			self::register_legacy_post_type();
20
		}
21
22
		/** This filter is documented in modules/custom-css/custom-css.php */
23
		$preprocessors      = apply_filters( 'jetpack_custom_css_preprocessors', array() );
24
		$core_css_post      = wp_get_custom_css_post();
25
		$jetpack_css_post   = self::get_post();
26
		$revisions          = self::get_all_revisions();
27
28
		// Migrate the settings from revision meta to theme mod.
29
		$options = self::get_options( $jetpack_css_post->ID );
30
		set_theme_mod( 'jetpack_custom_css', $options );
31
32
		if ( empty( $revisions ) || ! is_array( $revisions ) ) {
33
			if ( $jetpack_css_post instanceof WP_Post ) {
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
34
				// Feed in the raw, if the current setting is Sass/LESS, it'll filter it inside.
35
				wp_update_custom_css_post( $jetpack_css_post->post_content );
36
				return 1;
37
			}
38
			return null;
39
		}
40
41
		$revisions            = array_reverse( $revisions );
42
		$themes               = Jetpack_Custom_CSS_Enhancements::get_themes();
43
		$migrated             = array();
44
45
		foreach ( $revisions as $post_id => $post ) {
46
			// Jetpack had stored the theme Name, not the stylesheet directory, for ... reasons.
47
			// Get the stylesheet.  If null, the theme is no longer available.  Skip.
48
			$stylesheet = isset( $themes[ $post->post_excerpt ] ) ? $themes[ $post->post_excerpt ] : null;
49
			if ( empty( $stylesheet ) ) {
50
				continue;
51
			}
52
53
			$migrated[] = $post->ID;
54
			$preprocessor = get_post_meta( $post->ID, 'custom_css_preprocessor', true );
55
			$css = $post->post_content;
56
			$pre = '';
57
58
			// Do a revision by revision parsing.
59
			if ( $preprocessor && isset( $preprocessors[ $preprocessor ] ) ) {
60
				$pre = $css;
61
				$css = call_user_func( $preprocessors[ $preprocessor ]['callback'], $pre );
62
			}
63
64
			// Do we need to remove any filters here for users without `unfiltered_html` ?
65
66
			wp_update_custom_css_post( $css, array(
67
				'stylesheet'   => $stylesheet,
68
				'preprocessed' => $pre,
69
			) );
70
		}
71
72
		// If we've migrated some CSS for the current theme and there was already something there in the Core dataset ...
73
		if ( $core_css_post && $jetpack_css_post ) {
74
			$preprocessor = $options['preprocessor'];
75
76
			$css = $core_css_post->post_content;
77
			$pre = $core_css_post->post_content_filtered;
78
			if ( $preprocessor ) {
79
				if ( $pre ) {
80
					$pre .= "\r\n\r\n/*\r\n\t" . esc_js( __( 'CSS Migrated from Jetpack:', 'jetpack' ) ) . "\r\n*/\r\n\r\n";
81
				}
82
				$pre .= $jetpack_css_post->post_content;
83
84
				$css .= "\r\n\r\n/*\r\n\t" . esc_js( __( 'CSS Migrated from Jetpack:', 'jetpack' ) ) . "\r\n*/\r\n\r\n";
85
				$css .= call_user_func( $preprocessors[ $preprocessor ]['callback'], $jetpack_css_post->post_content );
86
			} else {
87
				$css .= "\r\n\r\n/*\r\n\t" . esc_js( __( 'CSS Migrated from Jetpack:', 'jetpack' ) ) . "\r\n*/\r\n\r\n";
88
				$css .= $jetpack_css_post->post_content;
89
			}
90
91
			wp_update_custom_css_post( $css, array(
92
				'preprocessed' => $pre,
93
			) );
94
		}
95
96
		Jetpack::log( 'custom_css_4.7_migration', sizeof( $migrated ) . 'revisions migrated' );
97
		return sizeof( $migrated );
98
	}
99
100
	public static function register_legacy_post_type() {
101
		if ( post_type_exists( 'safecss' ) ) {
102
			return;
103
		}
104
		// Register safecss as a custom post_type
105
		// 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.
106
		register_post_type( 'safecss', array(
107
			'label'        => 'Custom CSS',
108
			'supports'     => array( 'revisions' ),
109
			'can_export'   => false,
110
			'rewrite'      => false,
111
			'capabilities' => array(
112
				'edit_post'          => 'edit_theme_options',
113
				'read_post'          => 'read',
114
				'delete_post'        => 'edit_theme_options',
115
				'edit_posts'         => 'edit_theme_options',
116
				'edit_others_posts'  => 'edit_theme_options',
117
				'publish_posts'      => 'edit_theme_options',
118
				'read_private_posts' => 'read',
119
			),
120
		) );
121
	}
122
123 View Code Duplication
	public static function get_post() {
124
		/**
125
		 * Filter the ID of the post where Custom CSS is stored, before the ID is retrieved.
126
		 *
127
		 * If the callback function returns a non-null value, then post_id() will immediately
128
		 * return that value, instead of retrieving the normal post ID.
129
		 *
130
		 * @module custom-css
131
		 *
132
		 * @since 3.8.1
133
		 *
134
		 * @param null null The ID to return instead of the normal ID.
135
		 */
136
		$custom_css_post_id = apply_filters( 'jetpack_custom_css_pre_post_id', null );
137
		if ( ! is_null( $custom_css_post_id ) ) {
138
			return get_post( $custom_css_post_id );
139
		}
140
141
		$custom_css_post_id = wp_cache_get( 'custom_css_post_id' );
142
143
		if ( false === $custom_css_post_id ) {
144
			$custom_css_posts = get_posts( array(
145
				'posts_per_page' => 1,
146
				'post_type'      => 'safecss',
147
				'post_status'    => 'publish',
148
				'orderby'        => 'date',
149
				'order'          => 'DESC',
150
			) );
151
152
			$custom_css_post_id = 0;
153
			if ( count( $custom_css_posts ) > 0 ) {
154
				$custom_css_post_id = $custom_css_posts[0]->ID;
155
			}
156
157
			// Save post_id=0 to note that no safecss post exists.
158
			wp_cache_set( 'custom_css_post_id', $custom_css_post_id );
159
		}
160
161
		if ( ! $custom_css_post_id ) {
162
			return false;
163
		}
164
165
		return get_post( $custom_css_post_id );
166
	}
167
168
	public static function get_all_revisions() {
169
		$post = self::get_post();
170
		$revisions = wp_get_post_revisions( $post->ID, array(
171
			'posts_per_page' => -1,
172
			'orderby'        => 'date',
173
			'order'          => 'DESC',
174
		) );
175
176
		return $revisions;
177
	}
178
179
	public static function get_options( $post_id = null ) {
180
		if ( empty( $post_id ) ) {
181
			$post = self::get_post();
182
			$post_id = $post->ID;
183
		}
184
185
		$meta = get_post_meta( $post_id );
186
187
		$replace = false;
188
		if ( isset( $meta['custom_css_add'][0] ) && 'no' === $meta['custom_css_add'][0] ) {
189
			$replace = true;
190
		}
191
192
		return array(
193
			'preprocessor'  => isset( $meta['custom_css_preprocessor'][0] ) ? $meta['custom_css_preprocessor'][0] : '',
194
			'replace'       => $replace,
195
			'content_width' => isset( $meta['content_width'][0] )           ? $meta['content_width'][0]           : '',
196
		);
197
	}
198
}
199
200
Jetpack_Custom_CSS_Data_Migration::add_hooks();
201