custom-font-migration.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. // Use an early priority to migrate legacy font settings before registering fonts
  3. add_action( 'after_setup_theme', 'migrate_blockbase_custom_fonts', 0 );
  4. function migrate_blockbase_custom_fonts() {
  5. $heading_font_slug = null;
  6. $body_font_slug = null;
  7. $font_settings = wp_get_global_settings( array( 'typography', 'fontFamilies' ) );
  8. // Extract font slugs from legacy data structure.
  9. // Look first for fonts customized via Customizer, then for fonts configured in the child theme.json "the old way"
  10. if ( isset( $font_settings['custom'] ) && is_array( $font_settings['custom'] ) ) {
  11. $font_stuff = $font_settings['custom'];
  12. } else {
  13. $font_stuff = $font_settings['theme'];
  14. }
  15. foreach ( $font_stuff as $font_setting ) {
  16. if ( strpos( $font_setting['slug'], 'heading' ) !== false && array_key_exists('fontSlug', $font_setting) ) {
  17. $heading_font_slug = $font_setting['fontSlug'];
  18. }
  19. if ( strpos( $font_setting['slug'], 'body' ) !== false && array_key_exists('fontSlug', $font_setting) ) {
  20. $body_font_slug = $font_setting['fontSlug'];
  21. }
  22. }
  23. if( ! $body_font_slug && ! $heading_font_slug ) {
  24. //nothing to convert
  25. return;
  26. }
  27. // Get the user's global styles CPT id
  28. $user_custom_post_type_id = WP_Theme_JSON_Resolver::get_user_global_styles_post_id();
  29. $global_styles_controller = new WP_REST_Global_Styles_Controller();
  30. $global_styles = fetch_global_styles( $user_custom_post_type_id, $global_styles_controller );
  31. // converts data to array (in some cases settings and styles are objects insted of arrays)
  32. $new_settings = (array) $global_styles->data['settings'];
  33. $new_styles = (array) $global_styles->data['styles'];
  34. if ( $body_font_slug ) {
  35. $new_styles = array_merge(
  36. $new_styles,
  37. array(
  38. 'typography' => array(
  39. 'fontFamily' => "var:preset|font-family|$body_font_slug",
  40. ),
  41. )
  42. );
  43. }
  44. if ( $heading_font_slug ) {
  45. $new_styles = array_merge(
  46. $new_styles,
  47. array(
  48. 'blocks' => array(
  49. 'core/post-title' => array(
  50. 'typography' => array(
  51. 'fontFamily' => "var:preset|font-family|$heading_font_slug",
  52. ),
  53. ),
  54. 'core/heading' => array(
  55. 'typography' => array(
  56. 'fontFamily' => "var:preset|font-family|$heading_font_slug",
  57. ),
  58. ),
  59. ),
  60. )
  61. );
  62. }
  63. // Set new typography settings (copy from Blockbase theme.json file)
  64. $parent_theme_json_data = json_decode( file_get_contents( get_template_directory() . '/theme.json'), true );
  65. $parent_theme = new WP_Theme_JSON( $parent_theme_json_data );
  66. $parent_font_families = $parent_theme->get_data()['settings']['typography']['fontFamilies'];
  67. $new_settings['typography']['fontFamilies'] = $parent_font_families;
  68. update_global_styles( $new_settings, $new_styles, $user_custom_post_type_id, $global_styles_controller );
  69. }
  70. /**
  71. * Updates the global styles CPT.
  72. *
  73. * @param array $new_settings New global styles to update.
  74. * @param array $new_styles New global styles settings to update.
  75. * @param int $user_custom_post_type_id ID of global styles CPT.
  76. * @param object $global_styles_controller Controller that handles REST requests for global styles.
  77. *
  78. * @return void
  79. */
  80. function update_global_styles( $new_settings, $new_styles, $user_custom_post_type_id, $global_styles_controller ) {
  81. $update_request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' );
  82. $update_request->set_param( 'id', $user_custom_post_type_id );
  83. $update_request->set_param( 'settings', $new_settings );
  84. $update_request->set_param( 'styles', $new_styles );
  85. $global_styles_controller->update_item( $update_request );
  86. delete_transient( 'global_styles' );
  87. delete_transient( 'global_styles_' . get_stylesheet() );
  88. delete_transient( 'gutenberg_global_styles' );
  89. delete_transient( 'gutenberg_global_styles_' . get_stylesheet() );
  90. }
  91. /**
  92. * Retrieves the global styles cpt.
  93. *
  94. * @param int $user_custom_post_type_id ID of global styles CPT.
  95. * @param object $global_styles_controller Controller that handles REST requests for global styles.
  96. *
  97. * @return array
  98. */
  99. function fetch_global_styles( $user_custom_post_type_id, $global_styles_controller ) {
  100. $get_request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' );
  101. $get_request->set_param( 'id', $user_custom_post_type_id );
  102. $global_styles = $global_styles_controller->get_item( $get_request );
  103. return $global_styles;
  104. }