wp-customize-fonts.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. <?php
  2. require_once( __DIR__ . '/wp-customize-global-styles-setting.php' );
  3. require_once( __DIR__ . '/wp-customize-utils.php' );
  4. class GlobalStylesFontsCustomizer {
  5. private $section_key = 'customize-global-styles-fonts';
  6. private $font_settings;
  7. private $font_control_default_body;
  8. private $font_control_default_heading;
  9. //Not all fonts support v2 of the API that allows for the shorter URls
  10. //list of supported fonts: https://fonts.google.com/variablefonts
  11. private $fonts = array(
  12. 'system-font' => array(
  13. 'fontFamily' => '-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif',
  14. 'slug' => 'system-font',
  15. 'name' => 'System Font',
  16. ),
  17. 'arvo' => array(
  18. 'fontFamily' => '"Arvo", serif',
  19. 'slug' => 'arvo',
  20. 'name' => 'Arvo',
  21. 'google' => 'family=Arvo:ital,wght@0,400;0,700;1,400;1,700',
  22. ),
  23. 'bodoni-moda' => array(
  24. 'fontFamily' => '"Bodoni Moda", serif',
  25. 'slug' => 'bodoni-moda',
  26. 'name' => 'Bodoni Moda',
  27. 'google' => 'family=Bodoni+Moda:ital,wght@0,400..900;1,400..900',
  28. ),
  29. 'cabin' => array(
  30. 'fontFamily' => '"Cabin", sans-serif',
  31. 'slug' => 'cabin',
  32. 'name' => 'Cabin',
  33. 'google' => 'family=Cabin:ital,wght@0,400..700;1,400..700',
  34. ),
  35. 'chivo' => array(
  36. 'fontFamily' => '"Chivo", sans-serif',
  37. 'slug' => 'chivo',
  38. 'name' => 'Chivo',
  39. 'google' => 'family=Chivo:ital,wght@0,300;0,400;0,700;0,900;1,300;1,400;1,700;1,900',
  40. ),
  41. 'courier-prime' => array(
  42. 'fontFamily' => '"Courier Prime", serif',
  43. 'slug' => 'courier-prime',
  44. 'name' => 'Courier Prime',
  45. 'google' => 'family=Courier+Prime:ital,wght@0,400;0,700;1,400;1,700',
  46. ),
  47. 'dm-sans' => array(
  48. 'fontFamily' => '"DM Sans", sans-serif',
  49. 'slug' => 'dm-sans',
  50. 'name' => 'DM Sans',
  51. 'google' => 'family=DM+Sans:ital,wght@0,400;0,500;0,700;1,400;1,500;1,700"',
  52. ),
  53. 'domine' => array(
  54. 'fontFamily' => '"Domine", serif',
  55. 'slug' => 'domine',
  56. 'name' => 'Domine',
  57. 'google' => 'family=Domine:wght@400..700',
  58. ),
  59. 'eb-garamond' => array(
  60. 'fontFamily' => '"EB Garamond", serif',
  61. 'slug' => 'eb-garamond',
  62. 'name' => 'EB Garamond',
  63. 'google' => 'family=EB+Garamond:ital,wght@0,400..800;1,400..800',
  64. ),
  65. 'fira-sans' => array(
  66. 'fontFamily' => '"Fira Sans", sans-serif',
  67. 'slug' => 'fira-sans',
  68. 'name' => 'Fira Sans',
  69. 'google' => 'family=Fira+Sans:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900',
  70. ),
  71. 'ibm-plex-mono' => array(
  72. 'fontFamily' => '"IBM Plex Mono", monospace',
  73. 'slug' => 'ibm-plex-mono',
  74. 'name' => 'IBM Plex Mono',
  75. 'google' => 'family=IBM+Plex+Mono:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;1,100;1,200;1,300;1,400;1,500;1,600;1,700',
  76. ),
  77. 'inter' => array(
  78. 'fontFamily' => '"Inter", sans-serif',
  79. 'slug' => 'inter',
  80. 'name' => 'Inter',
  81. 'google' => 'family=Inter:wght@100..900',
  82. ),
  83. 'josefin-sans' => array(
  84. 'fontFamily' => '"Josefin Sans", sans-serif',
  85. 'slug' => 'josefin-sans',
  86. 'name' => 'Josefin Sans',
  87. 'google' => 'family=Josefin+Sans:ital,wght@0,100..700;1,100..700',
  88. ),
  89. 'libre-baskerville' => array(
  90. 'fontFamily' => '"Libre Baskerville", serif',
  91. 'slug' => 'libre-baskerville',
  92. 'name' => 'Libre Baskerville',
  93. 'google' => 'family=Libre+Baskerville:ital,wght@0,400;0,700;1,400',
  94. ),
  95. 'libre-franklin' => array(
  96. 'fontFamily' => '"Libre Franklin", sans-serif',
  97. 'slug' => 'libre-franklin',
  98. 'name' => 'Libre Franklin',
  99. 'google' => 'family=Libre+Franklin:ital,wght@0,100..900;1,100..900',
  100. ),
  101. 'lora' => array(
  102. 'fontFamily' => '"Lora", serif',
  103. 'slug' => 'lora',
  104. 'name' => 'Lora',
  105. 'google' => 'family=Lora:ital,wght@0,400..700;1,400..700',
  106. ),
  107. 'merriweather' => array(
  108. 'fontFamily' => '"Merriweather", serif',
  109. 'slug' => 'merriweather',
  110. 'name' => 'Merriweather',
  111. 'google' => 'family=Merriweather:ital,wght@0,300;0,400;0,700;0,900;1,300;1,400;1,700;1,900',
  112. ),
  113. 'montserrat' => array(
  114. 'fontFamily' => '"Montserrat", sans-serif',
  115. 'slug' => 'montserrat',
  116. 'name' => 'Montserrat',
  117. 'google' => 'family=Montserrat:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900',
  118. ),
  119. 'nunito' => array(
  120. 'fontFamily' => '"Nunito", sans-serif',
  121. 'slug' => 'nunito',
  122. 'name' => 'Nunito',
  123. 'google' => 'family=Nunito:ital,wght@0,200;0,300;0,400;0,600;0,700;0,800;0,900;1,200;1,300;1,400;1,600;1,700;1,800;1,900',
  124. ),
  125. 'open-sans' => array(
  126. 'fontFamily' => '"Open Sans", sans-serif',
  127. 'slug' => 'open-sans',
  128. 'name' => 'Open Sans',
  129. 'google' => 'family=Open+Sans:ital,wght@0,300;0,400;0,600;0,700;0,800;1,300;1,400;1,600;1,700;1,800',
  130. ),
  131. 'overpass' => array(
  132. 'fontFamily' => '"Overpass", sans-serif',
  133. 'slug' => 'overpass',
  134. 'name' => 'Overpass',
  135. 'google' => 'family=Overpass:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900',
  136. ),
  137. 'playfair-display' => array(
  138. 'fontFamily' => '"Playfair Display", serif',
  139. 'slug' => 'playfair-display',
  140. 'name' => 'Playfair Display',
  141. 'google' => 'family=Playfair+Display:ital,wght@0,400..900;1,400..900',
  142. ),
  143. 'poppins' => array(
  144. 'fontFamily' => '"Poppins", sans-serif',
  145. 'slug' => 'poppins',
  146. 'name' => 'Poppins',
  147. 'google' => 'family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900',
  148. ),
  149. 'raleway' => array(
  150. 'fontFamily' => '"Raleway", sans-serif',
  151. 'slug' => 'raleway',
  152. 'name' => 'Raleway',
  153. 'google' => 'family=Raleway:ital,wght@0,100..900;1,100..900',
  154. ),
  155. 'red-hat-display' => array(
  156. 'fontFamily' => '"Red Hat Display", sans-serif',
  157. 'slug' => 'red-hat-display',
  158. 'name' => 'Red Hat Display',
  159. 'google' => 'family=Red+Hat+Display:ital,wght@0,400;0,500;0,700;0,900;1,400;1,500;1,700;1,900',
  160. ),
  161. 'roboto' => array(
  162. 'fontFamily' => '"Roboto", sans-serif',
  163. 'slug' => 'roboto',
  164. 'name' => 'Roboto',
  165. 'google' => 'family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,800;1,900',
  166. ),
  167. 'roboto-slab' => array(
  168. 'fontFamily' => '"Roboto Slab", sans-serif',
  169. 'slug' => 'roboto-slab',
  170. 'name' => 'Roboto Slab',
  171. 'google' => 'family=Roboto+Slab:wght@100..900',
  172. ),
  173. 'rubik' => array(
  174. 'fontFamily' => '"Rubik", sans-serif',
  175. 'slug' => 'rubik',
  176. 'name' => 'Rubik',
  177. 'google' => 'family=Rubik:ital,wght@0,300..900;1,300..900',
  178. ),
  179. 'source-sans-pro' => array(
  180. 'fontFamily' => '"Source Sans Pro", sans-serif',
  181. 'slug' => 'source-sans-pro',
  182. 'name' => 'Source Sans Pro',
  183. 'google' => 'family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900',
  184. ),
  185. 'source-serif-pro' => array(
  186. 'fontFamily' => '"Source Serif Pro", serif',
  187. 'slug' => 'source-serif-pro',
  188. 'name' => 'Source Serif Pro',
  189. 'google' => 'family=Source+Serif+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900',
  190. ),
  191. 'space-mono' => array(
  192. 'fontFamily' => '"Space Mono", sans-serif',
  193. 'slug' => 'space-mono',
  194. 'name' => 'Space Mono',
  195. 'google' => 'family=Space+Mono:ital,wght@0,400;0,700;1,400;1,700',
  196. ),
  197. 'work-sans' => array(
  198. 'fontFamily' => '"Work Sans", sans-serif',
  199. 'slug' => 'work-sans',
  200. 'name' => 'Work Sans',
  201. 'google' => 'family=Work+Sans:ital,wght@0,100..900;1,100..900',
  202. ),
  203. );
  204. function __construct() {
  205. add_action( 'customize_register', array( $this, 'initialize' ) );
  206. add_action( 'customize_preview_init', array( $this, 'handle_customize_preview_init' ) );
  207. add_action( 'customize_register', array( $this, 'enqueue_google_fonts' ) );
  208. add_action( 'customize_save_after', array( $this, 'handle_customize_save_after' ) );
  209. add_action( 'customize_controls_enqueue_scripts', array( $this, 'customize_control_js' ) );
  210. }
  211. function handle_customize_preview_init( $wp_customize ) {
  212. $this->update_font_settings( $wp_customize );
  213. $this->customize_preview_js( $wp_customize );
  214. $this->create_customization_style_element( $wp_customize );
  215. }
  216. function customize_preview_js( $wp_customize ) {
  217. if ( $this->fonts && $this->font_settings ) {
  218. wp_enqueue_script( 'customizer-preview-fonts', get_template_directory_uri() . '/inc/customizer/wp-customize-fonts-preview.js', array( 'customize-preview' ) );
  219. wp_localize_script( 'customizer-preview-fonts', 'googleFonts', $this->fonts );
  220. wp_localize_script( 'customizer-preview-fonts', 'fontSettings', $this->font_settings );
  221. }
  222. }
  223. function customize_control_js() {
  224. if ( $this->font_control_default_body && $this->font_control_default_heading ) {
  225. wp_enqueue_script( 'customizer-font-control', get_template_directory_uri() . '/inc/customizer/wp-customize-fonts-control.js', array( 'customize-controls' ), null, true );
  226. wp_localize_script( 'customizer-font-control', 'fontControlDefaultBody', array( $this->font_control_default_body ) );
  227. wp_localize_script( 'customizer-font-control', 'fontControlDefaultHeading', array( $this->font_control_default_heading ) );
  228. }
  229. }
  230. function enqueue_google_fonts() {
  231. wp_enqueue_style( 'blockbase-google-fonts', $this->google_fonts_url(), array(), null );
  232. }
  233. function create_customization_style_element( $wp_customize ) {
  234. if ( $this->font_settings ) {
  235. wp_enqueue_style( 'global-styles-fonts-customizations', ' ', array( 'global-styles' ) ); // This needs to load after global_styles, hence the dependency
  236. $css = 'body {';
  237. $css .= '--wp--preset--font-family--body-font:' . $this->font_settings['body'] . ';';
  238. $css .= '--wp--preset--font-family--heading-font:' . $this->font_settings['heading'] . ';';
  239. $css .= '}';
  240. wp_add_inline_style( 'global-styles-fonts-customizations', $css );
  241. }
  242. }
  243. function update_font_settings( $wp_customize ) {
  244. $body_setting = $wp_customize->get_setting( $this->section_key . 'body' );
  245. $heading_setting = $wp_customize->get_setting( $this->section_key . 'heading' );
  246. if ( $body_setting && $heading_setting ) {
  247. $body_value = $body_setting->post_value();
  248. if ( $body_value ) {
  249. $body_font_setting = $this->fonts[ $body_value ];
  250. $this->font_settings['body'] = $body_font_setting['fontFamily'];
  251. }
  252. $heading_value = $heading_setting->post_value();
  253. if ( $heading_value ) {
  254. $heading_font_setting = $this->fonts[ $heading_value ];
  255. $this->font_settings['heading'] = $heading_font_setting['fontFamily'];
  256. }
  257. }
  258. }
  259. function google_fonts_url() {
  260. $font_families = array();
  261. foreach ( $this->fonts as $font ) {
  262. if ( ! empty( $font['google'] ) ) {
  263. $font_families[] = $font['google'];
  264. }
  265. }
  266. $font_families[] = 'display=swap';
  267. // Make a single request for the theme fonts.
  268. return esc_url_raw( 'https://fonts.googleapis.com/css2?' . implode( '&', $font_families ) );
  269. }
  270. function initialize( $wp_customize ) {
  271. $theme = wp_get_theme();
  272. //Add a Section to the Customizer for these bits
  273. $wp_customize->add_section(
  274. $this->section_key,
  275. array(
  276. 'capability' => 'edit_theme_options',
  277. 'description' => sprintf( __( 'Font Customization for %1$s', 'blockbase' ), $theme->name ),
  278. 'title' => __( 'Fonts', 'blockbase' ),
  279. )
  280. );
  281. $merged_json = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data()->get_raw_data();
  282. $theme_font_families = $merged_json['settings']['typography']['fontFamilies']['theme'];
  283. $body_font_default_array = array_filter(
  284. $theme_font_families,
  285. function( $font_family ) {
  286. return 'body-font' === $font_family['slug'];
  287. }
  288. );
  289. $body_font_default = array_shift( $body_font_default_array );
  290. $heading_font_default_array = array_filter(
  291. $theme_font_families,
  292. function( $font_family ) {
  293. return 'heading-font' === $font_family['slug'];
  294. }
  295. );
  296. $heading_font_default = array_shift( $heading_font_default_array );
  297. // See if the child theme has been updated. If not then show a notice.
  298. if ( ! $body_font_default && ! $heading_font_default ) {
  299. $wp_customize->add_control(
  300. $this->section_key . '-v1-blockbase-format-notice',
  301. array(
  302. 'type' => 'hidden',
  303. 'description' => '<div class="notice notice-warning">
  304. <p>' . __( 'Your theme needs to be updated before you can customize fonts', 'blockbase' ) . '</p>
  305. </div>',
  306. 'settings' => array(),
  307. 'section' => $this->section_key,
  308. )
  309. );
  310. return;
  311. }
  312. if ( array_key_exists( 'custom', $merged_json['settings']['typography']['fontFamilies'] ) ) {
  313. $merged_font_families = $merged_json['settings']['typography']['fontFamilies']['custom'];
  314. $body_font_selected_array = array_filter(
  315. $merged_font_families,
  316. function( $font_family ) {
  317. return 'body-font' === $font_family['slug'];
  318. }
  319. );
  320. $body_font_selected = array_shift( $body_font_selected_array );
  321. $heading_font_selected_array = array_filter(
  322. $merged_font_families,
  323. function( $font_family ) {
  324. return 'heading-font' === $font_family['slug'];
  325. }
  326. );
  327. $heading_font_selected = array_shift( $heading_font_selected_array );
  328. // NOTE: This should be removed once Gutenberg 12.1 lands stably in all environments
  329. } elseif ( array_key_exists( 'user', $merged_json['settings']['typography']['fontFamilies'] ) ) {
  330. $merged_font_families = $merged_json['settings']['typography']['fontFamilies']['user'];
  331. $body_font_selected_array = array_filter(
  332. $merged_font_families,
  333. function( $font_family ) {
  334. return 'body-font' === $font_family['slug'];
  335. }
  336. );
  337. $body_font_selected = array_shift( $body_font_selected_array );
  338. $heading_font_selected_array = array_filter(
  339. $merged_font_families,
  340. function( $font_family ) {
  341. return 'heading-font' === $font_family['slug'];
  342. }
  343. );
  344. $heading_font_selected = array_shift( $heading_font_selected_array );
  345. // End Gutenberg < 12.1 compatibility patch
  346. } else {
  347. $body_font_selected = $body_font_default;
  348. $heading_font_selected = $heading_font_default;
  349. }
  350. // If there's no selected font then the user is probably using the old format for font customization
  351. if ( $body_font_selected && $heading_font_selected ) {
  352. $body_font_selected_font_family = $body_font_selected['fontFamily'];
  353. $body_font_selected_font_slug = $body_font_selected['fontSlug'];
  354. $heading_font_selected_font_family = $heading_font_selected['fontFamily'];
  355. $heading_font_selected_font_slug = $heading_font_selected['fontSlug'];
  356. } else {
  357. $body_font_selected_font_family = null;
  358. $body_font_selected_font_slug = null;
  359. $heading_font_selected_font_family = null;
  360. $heading_font_selected_font_slug = null;
  361. }
  362. $this->font_settings = array(
  363. 'body' => $body_font_selected_font_family,
  364. 'heading' => $heading_font_selected_font_family,
  365. );
  366. // Add a reset button
  367. $this->font_control_default_body = $body_font_default['fontSlug'];
  368. $this->font_control_default_heading = $heading_font_default['fontSlug'];
  369. $wp_customize->add_control(
  370. $this->section_key . '-reset-button',
  371. array(
  372. 'type' => 'button',
  373. 'settings' => array(),
  374. 'section' => $this->section_key,
  375. 'input_attrs' => array(
  376. 'value' => __( 'Reset to Default', 'blockbase' ),
  377. 'class' => 'button button-link',
  378. ),
  379. )
  380. );
  381. $this->add_setting_and_control( $wp_customize, 'body', __( 'Body font', 'blockbase' ), $body_font_default['fontSlug'], $body_font_selected_font_slug, 'sanitize_title' );
  382. $this->add_setting_and_control( $wp_customize, 'heading', __( 'Heading font', 'blockbase' ), $heading_font_default['fontSlug'], $heading_font_selected_font_slug, 'sanitize_title' );
  383. }
  384. function get_font_family( $array, $configuration ) {
  385. $variable = get_settings_array( $array, $configuration );
  386. $slug = preg_replace( '/var\(--wp--preset--font-family--(.*)\)/', '$1', $variable );
  387. if ( ! isset( $this->fonts[ $slug ] ) ) {
  388. $this->fonts[ $slug ] = $this->build_font_from_theme_data( $slug, $configuration );
  389. }
  390. return $this->fonts[ $slug ];
  391. }
  392. function build_font_from_theme_data( $slug, $configuration ) {
  393. $new_font = array();
  394. $font_families = $configuration['settings']['typography']['fontFamilies']['theme'];
  395. foreach ( $font_families as $font_family ) {
  396. if ( $font_family['slug'] === $slug ) {
  397. $new_font['fontFamily'] = $font_family['fontFamily'];
  398. $new_font['name'] = $font_family['name'];
  399. if ( ! empty( $font_family['google'] ) ) {
  400. $new_font['google'] = $font_family['google'];
  401. }
  402. }
  403. }
  404. $new_font['slug'] = $slug;
  405. return $new_font;
  406. }
  407. function add_setting_and_control( $wp_customize, $name, $label, $default, $user_value, $sanitize_callback ) {
  408. $setting_name = $this->section_key . $name;
  409. $global_styles_setting = new WP_Customize_Global_Styles_Setting(
  410. $wp_customize,
  411. $setting_name,
  412. array(
  413. 'default' => $default,
  414. 'user_value' => $user_value,
  415. )
  416. );
  417. $wp_customize->add_setting(
  418. $global_styles_setting,
  419. array(
  420. 'sanitize_callback' => $sanitize_callback,
  421. )
  422. );
  423. $choices = array();
  424. foreach ( $this->fonts as $font_slug => $font_setting ) {
  425. $choices[ $font_slug ] = $font_setting['name'];
  426. }
  427. $wp_customize->add_control(
  428. $setting_name,
  429. array(
  430. 'section' => $this->section_key,
  431. 'label' => $label,
  432. 'type' => 'select',
  433. 'choices' => $choices,
  434. )
  435. );
  436. // Update the setting to the dirty value.
  437. // This is needed to preserve the settings when other Customizer settings change.
  438. $dirty_value = $wp_customize->get_setting( $setting_name )->post_value();
  439. if ( ! empty( $dirty_value ) ) {
  440. $wp_customize->get_setting( $setting_name )->user_value = $dirty_value;
  441. }
  442. }
  443. function handle_customize_save_after( $wp_customize ) {
  444. $body_value = $wp_customize->get_setting( $this->section_key . 'body' )->value();
  445. $heading_value = $wp_customize->get_setting( $this->section_key . 'heading' )->value();
  446. if ( ! isset( $body_value ) && ! isset( $heading_value ) ) {
  447. return;
  448. }
  449. $body_default = $wp_customize->get_setting( $this->section_key . 'body' )->default;
  450. $heading_default = $wp_customize->get_setting( $this->section_key . 'heading' )->default;
  451. if ( ! isset( $body_value ) ) {
  452. $body_value = $body_default;
  453. }
  454. if ( ! isset( $heading_value ) ) {
  455. $heading_value = $heading_default;
  456. }
  457. $body_setting = $this->fonts[ $body_value ];
  458. $body_setting['name'] = 'Body (' . $body_setting['name'] . ')';
  459. $body_setting['fontSlug'] = $body_setting['slug'];
  460. $body_setting['slug'] = 'body-font';
  461. $heading_setting = $this->fonts[ $heading_value ];
  462. $heading_setting['name'] = 'Heading (' . $heading_setting['name'] . ')';
  463. $heading_setting['fontSlug'] = $heading_setting['slug'];
  464. $heading_setting['slug'] = 'heading-font';
  465. // Set up variables for the theme.json.
  466. $font_families = array(
  467. $body_setting,
  468. $heading_setting,
  469. );
  470. $body_font_family_variable = 'var(--wp--preset--font-family--' . $body_setting['slug'] . ')';
  471. $heading_font_family_variable = 'var(--wp--preset--font-family--' . $heading_setting['slug'] . ')';
  472. // Get the user's global styles CPT id
  473. $user_custom_post_type_id = WP_Theme_JSON_Resolver_Gutenberg::get_user_global_styles_post_id();
  474. // API request to get global styles
  475. $get_request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' );
  476. $get_request->set_param( 'id', $user_custom_post_type_id );
  477. $global_styles_controller = new Gutenberg_REST_Global_Styles_Controller();
  478. $global_styles = $global_styles_controller->get_item( $get_request );
  479. // converts data to array (in some cases settings and styles are objects insted of arrays)
  480. $new_settings = (array) $global_styles->data['settings'];
  481. $new_styles = (array) $global_styles->data['styles'];
  482. // Set new typography settings
  483. if ( $font_families ) {
  484. $new_settings['typography']['fontFamilies']['custom'] = $font_families;
  485. }
  486. // Add the updated global styles to the update request
  487. $update_request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' );
  488. $update_request->set_param( 'id', $user_custom_post_type_id );
  489. $update_request->set_param( 'settings', $new_settings );
  490. $update_request->set_param( 'styles', $new_styles );
  491. // Update the theme.json with the new settings.
  492. $updated_global_styles = $global_styles_controller->update_item( $update_request );
  493. delete_transient( 'global_styles' );
  494. delete_transient( 'global_styles_' . get_stylesheet() );
  495. delete_transient( 'gutenberg_global_styles' );
  496. delete_transient( 'gutenberg_global_styles_' . get_stylesheet() );
  497. }
  498. }
  499. new GlobalStylesFontsCustomizer;