custom-font-migration.php 4.4 KB

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