wp-customize-fonts.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. <?php
  2. require_once 'wp-customize-global-styles-setting.php';
  3. require_once '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( $theme_font_families, function( $font_family ) {
  284. return $font_family['slug'] === "body-font";
  285. } );
  286. $body_font_default = array_shift( $body_font_default_array );
  287. $heading_font_default_array = array_filter( $theme_font_families, function( $font_family ) {
  288. return $font_family['slug'] === "heading-font";
  289. } );
  290. $heading_font_default = array_shift( $heading_font_default_array );
  291. // See if the child theme has been updated. If not then show a notice.
  292. if ( ! $body_font_default && ! $heading_font_default ) {
  293. $wp_customize->add_control(
  294. $this->section_key . '-v1-blockbase-format-notice',
  295. array(
  296. 'type' => 'hidden',
  297. 'description' => '<div class="notice notice-warning">
  298. <p>' . __( "Your theme needs to be updated before you can customize fonts", 'blockbase' ) . '</p>
  299. </div>',
  300. 'settings' => array(),
  301. 'section' => $this->section_key,
  302. )
  303. );
  304. return;
  305. }
  306. if ( array_key_exists( 'custom', $merged_json['settings']['typography']['fontFamilies'] ) ) {
  307. $merged_font_families = $merged_json['settings']['typography']['fontFamilies']['custom'];
  308. $body_font_selected_array = array_filter( $merged_font_families, function( $font_family ) {
  309. return $font_family['slug'] === "body-font";
  310. } );
  311. $body_font_selected = array_shift( $body_font_selected_array );
  312. $heading_font_selected_array = array_filter( $merged_font_families, function( $font_family ) {
  313. return $font_family['slug'] === "heading-font";
  314. } );
  315. $heading_font_selected = array_shift( $heading_font_selected_array );
  316. // NOTE: This should be removed once Gutenberg 12.1 lands stably in all environments
  317. } else if ( array_key_exists( 'user', $merged_json['settings']['typography']['fontFamilies'] ) ) {
  318. $merged_font_families = $merged_json['settings']['typography']['fontFamilies']['user'];
  319. $body_font_selected_array = array_filter( $merged_font_families, function( $font_family ) {
  320. return $font_family['slug'] === "body-font";
  321. } );
  322. $body_font_selected = array_shift( $body_font_selected_array );
  323. $heading_font_selected_array = array_filter( $merged_font_families, function( $font_family ) {
  324. return $font_family['slug'] === "heading-font";
  325. } );
  326. $heading_font_selected = array_shift( $heading_font_selected_array );
  327. // End Gutenberg < 12.1 compatibility patch
  328. } else {
  329. $body_font_selected = $body_font_default;
  330. $heading_font_selected = $heading_font_default;
  331. }
  332. // If there's no selected font then the user is probably using the old format for font customization
  333. if ( $body_font_selected && $heading_font_selected ) {
  334. $body_font_selected_font_family = $body_font_selected['fontFamily'];
  335. $body_font_selected_font_slug = $body_font_selected['fontSlug'];
  336. $heading_font_selected_font_family = $heading_font_selected['fontFamily'];
  337. $heading_font_selected_font_slug = $heading_font_selected['fontSlug'];
  338. } else {
  339. $body_font_selected_font_family = null;
  340. $body_font_selected_font_slug = null;
  341. $heading_font_selected_font_family = null;
  342. $heading_font_selected_font_slug = null;
  343. }
  344. $this->font_settings = array(
  345. 'body' => $body_font_selected_font_family,
  346. 'heading' => $heading_font_selected_font_family,
  347. );
  348. // Add a reset button
  349. $this->font_control_default_body = $body_font_default['fontSlug'];
  350. $this->font_control_default_heading = $heading_font_default['fontSlug'];
  351. $wp_customize->add_control(
  352. $this->section_key . '-reset-button',
  353. array(
  354. 'type' => 'button',
  355. 'settings' => array(),
  356. 'section' => $this->section_key,
  357. 'input_attrs' => array(
  358. 'value' => __( 'Reset to Default', 'blockbase' ),
  359. 'class' => 'button button-link',
  360. ),
  361. )
  362. );
  363. $this->add_setting_and_control( $wp_customize, 'body', __( 'Body font', 'blockbase' ), $body_font_default['fontSlug'], $body_font_selected_font_slug, 'sanitize_title' );
  364. $this->add_setting_and_control( $wp_customize, 'heading', __( 'Heading font', 'blockbase' ), $heading_font_default['fontSlug'], $heading_font_selected_font_slug, 'sanitize_title' );
  365. }
  366. function get_font_family( $array, $configuration ) {
  367. $variable = get_settings_array( $array, $configuration );
  368. $slug = preg_replace( '/var\(--wp--preset--font-family--(.*)\)/', '$1', $variable );
  369. if ( ! isset( $this->fonts[ $slug ] ) ) {
  370. $this->fonts[ $slug ] = $this->build_font_from_theme_data( $slug, $configuration );
  371. }
  372. return $this->fonts[ $slug ];
  373. }
  374. function build_font_from_theme_data( $slug, $configuration ) {
  375. $new_font = array();
  376. $font_families = $configuration['settings']['typography']['fontFamilies']['theme'];
  377. foreach ( $font_families as $font_family ) {
  378. if ( $font_family['slug'] === $slug ) {
  379. $new_font['fontFamily'] = $font_family['fontFamily'];
  380. $new_font['name'] = $font_family['name'];
  381. if ( ! empty( $font_family['google'] ) ) {
  382. $new_font['google'] = $font_family['google'];
  383. }
  384. }
  385. }
  386. $new_font['slug'] = $slug;
  387. return $new_font;
  388. }
  389. function add_setting_and_control( $wp_customize, $name, $label, $default, $user_value, $sanitize_callback ) {
  390. $setting_name = $this->section_key . $name;
  391. $global_styles_setting = new WP_Customize_Global_Styles_Setting(
  392. $wp_customize,
  393. $setting_name,
  394. array(
  395. 'default' => $default,
  396. 'user_value' => $user_value
  397. )
  398. );
  399. $wp_customize->add_setting( $global_styles_setting,
  400. array(
  401. 'sanitize_callback' => $sanitize_callback
  402. )
  403. );
  404. $choices = array();
  405. foreach ( $this->fonts as $font_slug => $font_setting ) {
  406. $choices[ $font_slug ] = $font_setting['name'];
  407. }
  408. $wp_customize->add_control(
  409. $setting_name,
  410. array(
  411. 'section' => $this->section_key,
  412. 'label' => $label,
  413. 'type' => 'select',
  414. 'choices' => $choices,
  415. )
  416. );
  417. // Update the setting to the dirty value.
  418. // This is needed to preserve the settings when other Customizer settings change.
  419. $dirty_value = $wp_customize->get_setting( $setting_name )->post_value();
  420. if ( ! empty( $dirty_value ) ) {
  421. $wp_customize->get_setting( $setting_name )->user_value = $dirty_value;
  422. }
  423. }
  424. function unset_property_if_it_exists( $object, $property ) {
  425. if ( isset( $object->{$property} ) ) {
  426. unset( $object->{$property} );
  427. }
  428. }
  429. function handle_customize_save_after( $wp_customize ) {
  430. $body_value = $wp_customize->get_setting( $this->section_key . 'body' )->value();
  431. $heading_value = $wp_customize->get_setting( $this->section_key . 'heading' )->value();
  432. if ( ! isset( $body_value ) && ! isset( $heading_value ) ) {
  433. return;
  434. }
  435. $body_default = $wp_customize->get_setting( $this->section_key . 'body' )->default;
  436. $heading_default = $wp_customize->get_setting( $this->section_key . 'heading' )->default;
  437. if ( ! isset( $body_value ) ) {
  438. $body_value = $body_default;
  439. }
  440. if ( ! isset( $heading_value ) ) {
  441. $heading_value = $heading_default;
  442. }
  443. $body_setting = $this->fonts[ $body_value ];
  444. $body_setting['name'] = "Body (" . $body_setting['name'] . ")";
  445. $body_setting['fontSlug'] = $body_setting['slug'];
  446. $body_setting['slug'] = "body-font";
  447. $heading_setting = $this->fonts[ $heading_value ];
  448. $heading_setting['name'] = "Heading (" . $heading_setting['name'] . ")";
  449. $heading_setting['fontSlug'] = $heading_setting['slug'];
  450. $heading_setting['slug'] = "heading-font";
  451. // Set up variables for the theme.json.
  452. $font_families = array(
  453. $body_setting,
  454. $heading_setting,
  455. );
  456. $body_font_family_variable = 'var(--wp--preset--font-family--' . $body_setting['slug'] . ')';
  457. $heading_font_family_variable = 'var(--wp--preset--font-family--' . $heading_setting['slug'] . ')';
  458. // Get the user's theme.json from the CPT.
  459. if ( method_exists( 'WP_Theme_JSON_Resolver_Gutenberg', 'get_user_global_styles_post_id' ) ) { // This is the new name.
  460. $user_custom_post_type_id = WP_Theme_JSON_Resolver_Gutenberg::get_user_global_styles_post_id();
  461. } else if ( method_exists( 'WP_Theme_JSON_Resolver_Gutenberg', 'get_user_custom_post_type_id' ) ) { // This is the old name.
  462. $user_custom_post_type_id = WP_Theme_JSON_Resolver_Gutenberg::get_user_custom_post_type_id();
  463. }
  464. $user_theme_json_post = get_post( $user_custom_post_type_id );
  465. $user_theme_json_post_content = json_decode( $user_theme_json_post->post_content );
  466. // Set meta settings.
  467. $user_theme_json_post_content->version = 1;
  468. $user_theme_json_post_content->isGlobalStylesUserThemeJSON = true;
  469. $this->unset_property_if_it_exists( $user_theme_json_post_content->settings->typography, 'fontFamilies' );
  470. // Set the typography settings.
  471. $user_theme_json_post_content = set_settings_array(
  472. $user_theme_json_post_content,
  473. array( 'settings', 'typography', 'fontFamilies', 'custom' ),
  474. $font_families
  475. );
  476. //If the typeface choices === the default then we remove it instead
  477. if ( $body_value === $body_default && $heading_value === $heading_default ) {
  478. // These lines need to stay for backwards compatibility.
  479. $this->unset_property_if_it_exists( $user_theme_json_post_content->styles->typography, 'fontFamily' );
  480. $this->unset_property_if_it_exists( $user_theme_json_post_content->styles->elements->h1->typography, 'fontFamily' );
  481. $this->unset_property_if_it_exists( $user_theme_json_post_content->styles->elements->h2->typography, 'fontFamily' );
  482. $this->unset_property_if_it_exists( $user_theme_json_post_content->styles->elements->h3->typography, 'fontFamily' );
  483. $this->unset_property_if_it_exists( $user_theme_json_post_content->styles->elements->h4->typography, 'fontFamily' );
  484. $this->unset_property_if_it_exists( $user_theme_json_post_content->styles->elements->h5->typography, 'fontFamily' );
  485. $this->unset_property_if_it_exists( $user_theme_json_post_content->styles->elements->h6->typography, 'fontFamily' );
  486. $this->unset_property_if_it_exists( $user_theme_json_post_content->styles->blocks->{'core/button'}->typography, 'fontFamily' );
  487. $this->unset_property_if_it_exists( $user_theme_json_post_content->styles->blocks->{'core/post-title'}->typography, 'fontFamily' );
  488. $this->unset_property_if_it_exists( $user_theme_json_post_content->styles->blocks->{'core/pullquote'}->typography, 'fontFamily' );
  489. }
  490. // Update the theme.json with the new settings.
  491. $user_theme_json_post->post_content = json_encode( $user_theme_json_post_content );
  492. wp_update_post( $user_theme_json_post );
  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;