Jetpack_Custom_CSS_Data_Migration

Class Jetpack_Custom_CSS_Data_Migration.

Defined (1)

The class is defined in the following location(s).

/modules/custom-css/migrate-to-core.php  
  1. class Jetpack_Custom_CSS_Data_Migration { 
  2. /** 
  3. * Set up assorted actions and filters used by this class. 
  4. */ 
  5. public static function add_hooks() { 
  6. add_action( 'init', array( __CLASS__, 'register_legacy_post_type' ) ); 
  7. add_action( 'admin_init', array( __CLASS__, 'do_migration' ) ); 
  8.  
  9. include_once( dirname( __FILE__ ) . '/custom-css.php' ); 
  10. if ( ! is_admin() ) { 
  11. add_action( 'init', array( 'Jetpack_Custom_CSS', 'init' ) ); 
  12.  
  13. /** 
  14. * Do the bulk of the migration. 
  15. * @return int|null 
  16. */ 
  17. public static function do_migration() { 
  18. Jetpack_Options::update_option( 'custom_css_4.7_migration', true ); 
  19. Jetpack::log( 'custom_css_4.7_migration', 'start' ); 
  20.  
  21. if ( ! post_type_exists( 'safecss' ) ) { 
  22. self::register_legacy_post_type(); 
  23.  
  24. /** This filter is documented in modules/custom-css/custom-css.php */ 
  25. $preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() ); 
  26. $core_css_post = wp_get_custom_css_post(); 
  27. $jetpack_css_post = self::get_post(); 
  28.  
  29. if ( ! $jetpack_css_post ) { 
  30. return; 
  31.  
  32. $revisions = self::get_all_revisions(); 
  33.  
  34. // Migrate the settings from revision meta to theme mod. 
  35. $options = self::get_options( $jetpack_css_post->ID ); 
  36. set_theme_mod( 'jetpack_custom_css', $options ); 
  37.  
  38. if ( empty( $revisions ) || ! is_array( $revisions ) ) { 
  39. if ( $jetpack_css_post instanceof WP_Post ) { 
  40. // Feed in the raw, if the current setting is Sass/LESS, it'll filter it inside. 
  41. kses_remove_filters(); 
  42. wp_update_custom_css_post( $jetpack_css_post->post_content ); 
  43. kses_init(); 
  44. return 1; 
  45. return null; 
  46.  
  47. $revisions = array_reverse( $revisions ); 
  48. $themes = Jetpack_Custom_CSS_Enhancements::get_themes(); 
  49. $migrated = array(); 
  50.  
  51. foreach ( $revisions as $post_id => $post ) { 
  52. // Jetpack had stored the theme Name, not the stylesheet directory, for ... reasons. 
  53. // Get the stylesheet. If null, the theme is no longer available. Skip. 
  54. $stylesheet = isset( $themes[ $post->post_excerpt ] ) ? $themes[ $post->post_excerpt ] : null; 
  55. if ( empty( $stylesheet ) ) { 
  56. continue; 
  57.  
  58. $migrated[] = $post->ID; 
  59. $preprocessor = get_post_meta( $post->ID, 'custom_css_preprocessor', true ); 
  60. $css = $post->post_content; 
  61. $pre = ''; 
  62.  
  63. // Do a revision by revision parsing. 
  64. if ( $preprocessor && isset( $preprocessors[ $preprocessor ] ) ) { 
  65. $pre = $css; 
  66. $css = call_user_func( $preprocessors[ $preprocessor ]['callback'], $pre ); 
  67.  
  68. kses_remove_filters(); 
  69. wp_update_custom_css_post( $css, array( 
  70. 'stylesheet' => $stylesheet,  
  71. 'preprocessed' => $pre,  
  72. ) ); 
  73. kses_init(); 
  74.  
  75. // If we've migrated some CSS for the current theme and there was already something there in the Core dataset ... 
  76. if ( $core_css_post && $jetpack_css_post ) { 
  77. $preprocessor = $options['preprocessor']; 
  78.  
  79. $css = $core_css_post->post_content; 
  80. $pre = $core_css_post->post_content_filtered; 
  81. if ( $preprocessor ) { 
  82. if ( $pre ) { 
  83. $pre .= "\r\n\r\n/*\r\n\t" . esc_js( __( 'CSS Migrated from Jetpack:', 'jetpack' ) ) . "\r\n*/\r\n\r\n"; 
  84. $pre .= $jetpack_css_post->post_content; 
  85.  
  86. $css .= "\r\n\r\n/*\r\n\t" . esc_js( __( 'CSS Migrated from Jetpack:', 'jetpack' ) ) . "\r\n*/\r\n\r\n"; 
  87. $css .= call_user_func( $preprocessors[ $preprocessor ]['callback'], $jetpack_css_post->post_content ); 
  88. } else { 
  89. $css .= "\r\n\r\n/*\r\n\t" . esc_js( __( 'CSS Migrated from Jetpack:', 'jetpack' ) ) . "\r\n*/\r\n\r\n"; 
  90. $css .= $jetpack_css_post->post_content; 
  91.  
  92. wp_update_custom_css_post( $css, array( 
  93. 'preprocessed' => $pre,  
  94. ) ); 
  95.  
  96. Jetpack::log( 'custom_css_4.7_migration', count( $migrated ) . 'revisions migrated' ); 
  97. return count( $migrated ); 
  98.  
  99. /** 
  100. * Re-register the legacy CPT so we can play with the content already in the database. 
  101. */ 
  102. public static function register_legacy_post_type() { 
  103. if ( post_type_exists( 'safecss' ) ) { 
  104. return; 
  105. // Register safecss as a custom post_type 
  106. // 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. 
  107. register_post_type( 'safecss', array( 
  108. 'label' => 'Custom CSS',  
  109. 'supports' => array( 'revisions' ),  
  110. 'can_export' => false,  
  111. 'rewrite' => false,  
  112. 'capabilities' => array( 
  113. 'edit_post' => 'edit_theme_options',  
  114. 'read_post' => 'read',  
  115. 'delete_post' => 'edit_theme_options',  
  116. 'edit_posts' => 'edit_theme_options',  
  117. 'edit_others_posts' => 'edit_theme_options',  
  118. 'publish_posts' => 'edit_theme_options',  
  119. 'read_private_posts' => 'read',  
  120. ),  
  121. ) ); 
  122.  
  123. /** 
  124. * Get the post used for legacy storage. 
  125. * Jetpack used to use a single post for all themes, just blanking it on theme switch. This gets that post. 
  126. * @return array|bool|null|WP_Post 
  127. */ 
  128. public static function get_post() { 
  129. /** This filter is documented in modules/custom-css/custom-css.php */ 
  130. $custom_css_post_id = apply_filters( 'jetpack_custom_css_pre_post_id', null ); 
  131. if ( ! is_null( $custom_css_post_id ) ) { 
  132. return get_post( $custom_css_post_id ); 
  133.  
  134. $custom_css_post_id = wp_cache_get( 'custom_css_post_id' ); 
  135.  
  136. if ( false === $custom_css_post_id ) { 
  137. $custom_css_posts = get_posts( array( 
  138. 'posts_per_page' => 1,  
  139. 'post_type' => 'safecss',  
  140. 'post_status' => 'publish',  
  141. 'orderby' => 'date',  
  142. 'order' => 'DESC',  
  143. ) ); 
  144.  
  145. $custom_css_post_id = 0; 
  146. if ( count( $custom_css_posts ) > 0 ) { 
  147. $custom_css_post_id = $custom_css_posts[0]->ID; 
  148.  
  149. // Save post_id=0 to note that no safecss post exists. 
  150. wp_cache_set( 'custom_css_post_id', $custom_css_post_id ); 
  151.  
  152. if ( ! $custom_css_post_id ) { 
  153. return false; 
  154.  
  155. return get_post( $custom_css_post_id ); 
  156.  
  157. /** 
  158. * Get all revisions of the Jetpack CSS CPT entry. 
  159. * @return array 
  160. */ 
  161. public static function get_all_revisions() { 
  162. $post = self::get_post(); 
  163.  
  164. if ( ! $post ) { 
  165. return array(); 
  166.  
  167. $revisions = wp_get_post_revisions( $post->ID, array( 
  168. 'posts_per_page' => -1,  
  169. 'orderby' => 'date',  
  170. 'order' => 'DESC',  
  171. ) ); 
  172.  
  173. return $revisions; 
  174.  
  175. /** 
  176. * Get the options stored for a given revision ID. 
  177. * Jetpack used to version the settings by storing them as meta on the revision. 
  178. * @param integer $post_id Post ID. 
  179. * @return array 
  180. */ 
  181. public static function get_options( $post_id = null ) { 
  182. if ( empty( $post_id ) ) { 
  183. $post = self::get_post(); 
  184. $post_id = $post->ID; 
  185.  
  186. $meta = get_post_meta( $post_id ); 
  187.  
  188. $replace = false; 
  189. if ( isset( $meta['custom_css_add'][0] ) && 'no' === $meta['custom_css_add'][0] ) { 
  190. $replace = true; 
  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. );