wp-customize-fonts-control.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. wp.customize.bind( 'ready', () => {
  2. let resetButton;
  3. let fontBodyControl;
  4. let fontHeadingControl;
  5. wp.customize.control(
  6. 'customize-global-styles-fonts-reset-button',
  7. ( control ) => {
  8. control.container
  9. .find( '.button' )
  10. .on( 'click', resetFontSelection );
  11. resetButton = control.container[ 0 ];
  12. resetButton.hidden = determineIfSetToDetault();
  13. }
  14. );
  15. // If the body and heading controls are null then the font customization is using
  16. // the old format. We need to hide these controls so that the user is forced to
  17. // reset to defaults before making other changes.
  18. wp.customize.control( 'customize-global-styles-fontsbody', ( control ) => {
  19. fontBodyControl = control.container[ 0 ];
  20. fontBodyControl.hidden = determineIfNull();
  21. } );
  22. wp.customize.control(
  23. 'customize-global-styles-fontsheading',
  24. ( control ) => {
  25. fontHeadingControl = control.container[ 0 ];
  26. fontHeadingControl.hidden = determineIfNull();
  27. }
  28. );
  29. wp.customize(
  30. 'customize-global-styles-fontsbody',
  31. bindControlToHideResetButton
  32. );
  33. wp.customize(
  34. 'customize-global-styles-fontsheading',
  35. bindControlToHideResetButton
  36. );
  37. function bindControlToHideResetButton( control ) {
  38. control.bind( () => {
  39. resetButton.hidden = false;
  40. fontHeadingControl.hidden = false;
  41. fontBodyControl.hidden = false;
  42. } );
  43. }
  44. function determineIfSetToDetault() {
  45. return (
  46. fontControlDefaultBody[ 0 ] ===
  47. wp.customize.settings.settings[
  48. 'customize-global-styles-fontsbody'
  49. ].value &&
  50. fontControlDefaultHeading[ 0 ] ===
  51. wp.customize.settings.settings[
  52. 'customize-global-styles-fontsheading'
  53. ].value
  54. );
  55. }
  56. function determineIfNull() {
  57. return ! (
  58. wp.customize.settings.settings[
  59. 'customize-global-styles-fontsbody'
  60. ].value &&
  61. wp.customize.settings.settings[
  62. 'customize-global-styles-fontsheading'
  63. ].value
  64. );
  65. }
  66. function resetFontSelection() {
  67. const shouldWeReload = determineIfNull();
  68. wp.customize( 'customize-global-styles-fontsbody', ( item ) => {
  69. item.set( fontControlDefaultBody[ 0 ] );
  70. } );
  71. wp.customize( 'customize-global-styles-fontsheading', ( item ) => {
  72. item.set( fontControlDefaultHeading[ 0 ] );
  73. } );
  74. resetButton.hidden = true;
  75. // If the body and heading controls are null then the font customization is using
  76. // the old format. We need to get the user to reset to default and then reload
  77. // before they make other customizations.
  78. if ( shouldWeReload ) {
  79. wp.customize.previewer.save();
  80. wp.customize.previewer.refresh();
  81. }
  82. }
  83. } );