public static function do_migration() { Jetpack_Options::update_option('custom_css_4.7_migration', true); Jetpack::log('custom_css_4.7_migration', 'start'); if (!post_type_exists('safecss')) { self::register_legacy_post_type(); } /** This filter is documented in modules/custom-css/custom-css.php */ $preprocessors = apply_filters('jetpack_custom_css_preprocessors', array()); $core_css_post = wp_get_custom_css_post(); $jetpack_css_post = self::get_post(); $revisions = self::get_all_revisions(); // Migrate the settings from revision meta to theme mod. $options = self::get_options($jetpack_css_post->ID); set_theme_mod('jetpack_custom_css', $options); if (empty($revisions) || !is_array($revisions)) { if ($jetpack_css_post instanceof WP_Post) { // Feed in the raw, if the current setting is Sass/LESS, it'll filter it inside. wp_update_custom_css_post($jetpack_css_post->post_content); return 1; } return null; } $revisions = array_reverse($revisions); $themes = Jetpack_Custom_CSS_Enhancements::get_themes(); $migrated = array(); foreach ($revisions as $post_id => $post) { // Jetpack had stored the theme Name, not the stylesheet directory, for ... reasons. // Get the stylesheet. If null, the theme is no longer available. Skip. $stylesheet = isset($themes[$post->post_excerpt]) ? $themes[$post->post_excerpt] : null; if (empty($stylesheet)) { continue; } $migrated[] = $post->ID; $preprocessor = get_post_meta($post->ID, 'custom_css_preprocessor', true); $css = $post->post_content; $pre = ''; // Do a revision by revision parsing. if ($preprocessor && isset($preprocessors[$preprocessor])) { $pre = $css; $css = call_user_func($preprocessors[$preprocessor]['callback'], $pre); } // Do we need to remove any filters here for users without `unfiltered_html` ? wp_update_custom_css_post($css, array('stylesheet' => $stylesheet, 'preprocessed' => $pre)); } // If we've migrated some CSS for the current theme and there was already something there in the Core dataset ... if ($core_css_post && $jetpack_css_post) { $preprocessor = $options['preprocessor']; $css = $core_css_post->post_content; $pre = $core_css_post->post_content_filtered; if ($preprocessor) { if ($pre) { $pre .= "\r\n\r\n/*\r\n\t" . esc_js(__('CSS Migrated from Jetpack:', 'jetpack')) . "\r\n*/\r\n\r\n"; } $pre .= $jetpack_css_post->post_content; $css .= "\r\n\r\n/*\r\n\t" . esc_js(__('CSS Migrated from Jetpack:', 'jetpack')) . "\r\n*/\r\n\r\n"; $css .= call_user_func($preprocessors[$preprocessor]['callback'], $jetpack_css_post->post_content); } else { $css .= "\r\n\r\n/*\r\n\t" . esc_js(__('CSS Migrated from Jetpack:', 'jetpack')) . "\r\n*/\r\n\r\n"; $css .= $jetpack_css_post->post_content; } wp_update_custom_css_post($css, array('preprocessed' => $pre)); } Jetpack::log('custom_css_4.7_migration', sizeof($migrated) . 'revisions migrated'); return sizeof($migrated); }
/** * Store the CSS setting value in the custom_css custom post type for the stylesheet. * * @since 4.7.0 * @access public * * @param string $css The input value. * @return int|false The post ID or false if the value could not be saved. */ public function update($css) { if (empty($css)) { $css = ''; } $r = wp_update_custom_css_post($css, array('stylesheet' => $this->stylesheet)); if ($r instanceof WP_Error) { return false; } $post_id = $r->ID; // Cache post ID in theme mod for performance to avoid additional DB query. if ($this->manager->get_stylesheet() === $this->stylesheet) { set_theme_mod('custom_css_post_id', $post_id); } return $post_id; }
/** * Test revision saving on initial save of Custom CSS. * * @ticket 39032 */ function test_custom_css_revision_saved() { $inserted_css = 'body { background: black; }'; $updated_css = 'body { background: red; }'; $post = wp_update_custom_css_post($inserted_css, array('stylesheet' => 'testtheme')); $this->assertSame($inserted_css, $post->post_content); $revisions = array_values(wp_get_post_revisions($post)); $this->assertCount(1, $revisions); $this->assertSame($inserted_css, $revisions[0]->post_content); wp_update_custom_css_post($updated_css, array('stylesheet' => 'testtheme')); $revisions = array_values(wp_get_post_revisions($post)); $this->assertCount(2, $revisions); $this->assertSame($updated_css, $revisions[0]->post_content); $this->assertSame($inserted_css, $revisions[1]->post_content); }
/** * Import Custom CSS. * * @since 3.5.0 */ function blue_planet_import_custom_css() { // Bail if not WP 4.7. if (!function_exists('wp_get_custom_css_post')) { return; } $custom_css = blue_planet_get_option('custom_css'); // Bail if there is no Custom CSS. if (empty($custom_css)) { return; } $core_css = wp_get_custom_css(); $return = wp_update_custom_css_post($core_css . $custom_css); if (!is_wp_error($return)) { // Remove from theme. $options = blue_planet_get_option_all(); $options['custom_css'] = ''; set_theme_mod('blueplanet_options', $options); } }