custom-font-migration.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. add_action( 'init', 'migrate_blockbase_custom_fonts', 99 );
  3. function migrate_blockbase_custom_fonts() {
  4. // The data has already been transformed
  5. if ( get_theme_mod( 'blockbase_legacy_font_settings' ) ) {
  6. return;
  7. }
  8. $heading_font_slug = null;
  9. $body_font_slug = null;
  10. // Here we must use gutenberg_get_global_* because it introduces clean_cached_data() which we
  11. // need to leverage as we are modifying the values of global styles settings and styles on page load.
  12. if ( function_exists( 'gutenberg_get_global_settings' ) ) {
  13. $font_families = gutenberg_get_global_settings( array( 'typography', 'fontFamilies' ) );
  14. } else {
  15. $font_families = wp_get_global_settings( array( 'typography', 'fontFamilies' ) );
  16. }
  17. if ( isset( $font_families['custom'] ) && is_array( $font_families['custom'] ) ) {
  18. $font_families = $font_families['custom'];
  19. } else {
  20. // No Customizer font settings found. Mark as transformed and hide the Customizer UI for fonts.
  21. set_theme_mod( 'blockbase_legacy_font_settings', true );
  22. return;
  23. }
  24. // Look first for fonts customized via Customizer, then for fonts configured in the child theme.json "the old way"
  25. // Also count fonts registerd to the blockbase font provider
  26. foreach ( $font_families as $font_family ) {
  27. if ( strpos( $font_family['slug'], 'heading' ) !== false && array_key_exists( 'fontSlug', $font_family ) ) {
  28. $heading_font_slug = $font_family['fontSlug'];
  29. }
  30. if ( strpos( $font_family['slug'], 'body' ) !== false && array_key_exists( 'fontSlug', $font_family ) ) {
  31. $body_font_slug = $font_family['fontSlug'];
  32. }
  33. }
  34. if ( ! $body_font_slug && ! $heading_font_slug ) {
  35. //nothing to convert
  36. return;
  37. }
  38. $theme_user_data = WP_Theme_JSON_Resolver::get_user_data()->get_raw_data();
  39. $new_settings = array();
  40. $new_styles = array();
  41. if ( array_key_exists( 'settings', $theme_user_data ) ) {
  42. $new_settings = $theme_user_data['settings'];
  43. }
  44. if ( array_key_exists( 'styles', $theme_user_data ) ) {
  45. $new_styles = $theme_user_data['styles'];
  46. }
  47. if ( $body_font_slug ) {
  48. $new_styles = array_merge(
  49. $new_styles,
  50. array(
  51. 'typography' => array(
  52. 'fontFamily' => "var:preset|font-family|$body_font_slug",
  53. ),
  54. )
  55. );
  56. }
  57. if ( $heading_font_slug ) {
  58. $new_styles = array_merge(
  59. $new_styles,
  60. array(
  61. 'blocks' => array(
  62. 'core/post-title' => array(
  63. 'typography' => array(
  64. 'fontFamily' => "var:preset|font-family|$heading_font_slug",
  65. ),
  66. ),
  67. 'core/heading' => array(
  68. 'typography' => array(
  69. 'fontFamily' => "var:preset|font-family|$heading_font_slug",
  70. ),
  71. ),
  72. ),
  73. )
  74. );
  75. }
  76. if ( $heading_font_slug || $body_font_slug ) {
  77. unset( $new_settings['typography']['fontFamilies'] );
  78. }
  79. update_global_styles( $new_settings, $new_styles );
  80. set_theme_mod( 'blockbase_legacy_font_settings', true );
  81. }
  82. /**
  83. * Updates the global styles CPT.
  84. *
  85. * @param array $new_settings New global styles to update.
  86. * @param array $new_styles New global styles settings to update.
  87. * @param int $user_custom_post_type_id ID of global styles CPT.
  88. * @param object $global_styles_controller Controller that handles REST requests for global styles.
  89. *
  90. * @return void
  91. */
  92. function update_global_styles( $new_settings, $new_styles ) {
  93. // Get the user's global styles CPT id
  94. $user_custom_post_type_id = WP_Theme_JSON_Resolver::get_user_global_styles_post_id();
  95. $global_styles_controller = new WP_REST_Global_Styles_Controller();
  96. $update_request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' );
  97. $update_request->set_param( 'id', $user_custom_post_type_id );
  98. $update_request->set_param( 'settings', $new_settings );
  99. $update_request->set_param( 'styles', $new_styles );
  100. $global_styles_controller->update_item( $update_request );
  101. // Ideally the call to update_item would delete all of the appropriate transients and caches
  102. delete_transient( 'global_styles' );
  103. delete_transient( 'global_styles_' . get_stylesheet() );
  104. delete_transient( 'gutenberg_global_styles' );
  105. delete_transient( 'gutenberg_global_styles_' . get_stylesheet() );
  106. if ( class_exists( 'WP_Theme_JSON_Resolver_Gutenberg' ) ) {
  107. WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data();
  108. }
  109. }
  110. /**
  111. * Retrieves the global styles cpt.
  112. *
  113. * @param int $user_custom_post_type_id ID of global styles CPT.
  114. * @param object $global_styles_controller Controller that handles REST requests for global styles.
  115. *
  116. * @return array
  117. */
  118. function fetch_global_styles( $user_custom_post_type_id, $global_styles_controller ) {
  119. $get_request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' );
  120. $get_request->set_param( 'id', $user_custom_post_type_id );
  121. $global_styles = $global_styles_controller->get_item( $get_request );
  122. return $global_styles;
  123. }