PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/class-kirki-enqueue.php

https://gitlab.com/aristath/kirki
PHP | 238 lines | 160 code | 28 blank | 50 comment | 22 complexity | 45e032b80fc72f48e8d8b16fa9d163dc MD5 | raw file
  1. <?php
  2. /**
  3. * Enqueue the scripts that are required by the customizer.
  4. * Any additional scripts that are required by individual controls
  5. * are enqueued in the control classes themselves.
  6. *
  7. * @package Kirki
  8. * @category Core
  9. * @author Aristeides Stathopoulos
  10. * @copyright Copyright (c) 2016, Aristeides Stathopoulos
  11. * @license http://opensource.org/licenses/https://opensource.org/licenses/MIT
  12. * @since 1.0
  13. */
  14. // Exit if accessed directly.
  15. if ( ! defined( 'ABSPATH' ) ) {
  16. exit;
  17. }
  18. if ( ! class_exists( 'Kirki_Enqueue' ) ) {
  19. /**
  20. * Enqueues JS & CSS assets
  21. */
  22. class Kirki_Enqueue {
  23. /**
  24. * The class constructor.
  25. * Adds actions to enqueue our assets.
  26. */
  27. public function __construct() {
  28. add_action( 'customize_controls_enqueue_scripts', array( $this, 'customize_controls_enqueue_scripts' ), 7 );
  29. add_action( 'customize_controls_print_scripts', array( $this, 'branding' ) );
  30. add_action( 'customize_preview_init', array( $this, 'postmessage' ) );
  31. }
  32. /**
  33. * Assets that have to be enqueued in 'customize_controls_enqueue_scripts'.
  34. */
  35. public function customize_controls_enqueue_scripts() {
  36. // Get an array of all our fields.
  37. $fields = Kirki::$fields;
  38. // Do we have tooltips anywhere?
  39. $has_tooltips = false;
  40. foreach ( $fields as $field ) {
  41. if ( $has_tooltips ) {
  42. continue;
  43. }
  44. // Field has tooltip.
  45. if ( isset( $field['tooltip'] ) && ! empty( $field['tooltip'] ) ) {
  46. $has_tooltips = true;
  47. }
  48. // Backwards-compatibility ("help" argument instead of "tooltip").
  49. if ( isset( $field['help'] ) && ! empty( $field['help'] ) ) {
  50. $has_tooltips = true;
  51. }
  52. }
  53. // If we have tooltips, enqueue the tooltips script.
  54. if ( $has_tooltips ) {
  55. wp_enqueue_script( 'kirki-tooltip', trailingslashit( Kirki::$url ) . 'assets/js/tooltip.js', array( 'jquery', 'customize-controls', 'jquery-ui-tooltip' ) );
  56. }
  57. // Enqueue the reset script.
  58. wp_enqueue_script( 'kirki-reset', trailingslashit( Kirki::$url ) . 'assets/js/reset.js', array( 'jquery', 'kirki-set-setting-value' ) );
  59. // Register kirki-functions.
  60. wp_register_script( 'kirki-set-setting-value', trailingslashit( Kirki::$url ) . 'assets/js/functions/set-setting-value.js' );
  61. wp_register_script( 'kirki-validate-css-value', trailingslashit( Kirki::$url ) . 'assets/js/functions/validate-css-value.js' );
  62. // Register serialize.js.
  63. wp_register_script( 'serialize-js', trailingslashit( Kirki::$url ) . 'assets/js/vendor/serialize.js' );
  64. // Register the color-alpha picker.
  65. wp_enqueue_style( 'wp-color-picker' );
  66. wp_register_script( 'wp-color-picker-alpha', trailingslashit( Kirki::$url ) . 'assets/js/vendor/wp-color-picker-alpha.js', array( 'wp-color-picker' ), '1.2', true );
  67. // Register the jquery-ui-spinner.
  68. wp_register_script( 'jquery-ui-spinner', trailingslashit( Kirki::$url ) . 'assets/js/vendor/jquery-ui-spinner', array( 'jquery', 'jquery-ui-core', 'jquery-ui-button' ) );
  69. // Register codemirror.
  70. wp_register_script( 'codemirror', trailingslashit( Kirki::$url ) . 'assets/js/vendor/codemirror/lib/codemirror.js', array( 'jquery' ) );
  71. // Register selectize.
  72. wp_register_script( 'selectize', trailingslashit( Kirki::$url ) . 'assets/js/vendor/selectize.js', array( 'jquery' ) );
  73. // Register the l10n script.
  74. wp_register_script( 'kirki-l10n', trailingslashit( Kirki::$url ) . 'assets/js/l10n.js' );
  75. // An array of control scripts and their dependencies.
  76. $scripts = array(
  77. // Add controls scripts.
  78. 'checkbox' => array( 'jquery', 'customize-base' ),
  79. 'code' => array( 'jquery', 'customize-base', 'codemirror' ),
  80. 'color' => array( 'jquery', 'customize-base', 'wp-color-picker-alpha' ),
  81. 'color-palette' => array( 'jquery', 'customize-base', 'jquery-ui-button' ),
  82. 'dashicons' => array( 'jquery', 'customize-base' ),
  83. 'date' => array( 'jquery', 'customize-base', 'jquery-ui-datepicker' ),
  84. 'dimension' => array( 'jquery', 'customize-base', 'kirki-validate-css-value' ),
  85. 'dropdown-pages' => array( 'jquery', 'customize-base', 'selectize' ),
  86. 'editor' => array( 'jquery', 'customize-base', 'kirki-l10n' ),
  87. 'generic' => array( 'jquery', 'customize-base' ),
  88. 'multicheck' => array( 'jquery', 'customize-base' ),
  89. 'multicolor' => array( 'jquery', 'customize-base', 'wp-color-picker-alpha' ),
  90. 'number' => array( 'jquery', 'customize-base', 'jquery-ui-spinner' ),
  91. 'palette' => array( 'jquery', 'customize-base', 'jquery-ui-button' ),
  92. 'preset' => array( 'jquery', 'customize-base', 'selectize', 'kirki-set-setting-value' ),
  93. 'radio-buttonset' => array( 'jquery', 'customize-base' ),
  94. 'radio-image' => array( 'jquery', 'customize-base' ),
  95. 'radio' => array( 'jquery', 'customize-base' ),
  96. 'repeater' => array( 'jquery', 'customize-base', 'jquery-ui-core', 'jquery-ui-sortable', 'kirki-l10n' ),
  97. 'select' => array( 'jquery', 'customize-base', 'selectize' ),
  98. 'slider' => array( 'jquery', 'customize-base' ),
  99. 'sortable' => array( 'jquery', 'customize-base', 'jquery-ui-core', 'jquery-ui-sortable', 'serialize-js' ),
  100. 'spacing' => array( 'jquery', 'customize-base', 'kirki-validate-css-value' ),
  101. 'switch' => array( 'jquery', 'customize-base' ),
  102. 'toggle' => array( 'jquery', 'customize-base' ),
  103. 'typography' => array( 'jquery', 'customize-base', 'selectize', 'wp-color-picker-alpha' ),
  104. );
  105. foreach ( $scripts as $id => $dependencies ) {
  106. wp_register_script( 'kirki-' . $id, trailingslashit( Kirki::$url ) . 'assets/js/controls/' . $id . '.js', $dependencies, false, true );
  107. }
  108. // Add localization strings.
  109. $l10n = Kirki_l10n::get_strings();
  110. wp_localize_script( 'kirki-l10n', 'kirkiL10n', $l10n );
  111. // Add fonts to our JS objects.
  112. $google_fonts = Kirki_Fonts::get_google_fonts();
  113. $standard_fonts = Kirki_Fonts::get_standard_fonts();
  114. $all_variants = Kirki_Fonts::get_all_variants();
  115. $all_subsets = Kirki_Fonts::get_google_font_subsets();
  116. $standard_fonts_final = array();
  117. foreach ( $standard_fonts as $key => $value ) {
  118. $standard_fonts_final[] = array(
  119. 'family' => $value['stack'],
  120. 'label' => $value['label'],
  121. 'subsets' => array(),
  122. 'is_standard' => true,
  123. 'variants' => array(
  124. array(
  125. 'id' => 'regular',
  126. 'label' => $all_variants['regular'],
  127. ),
  128. array(
  129. 'id' => 'italic',
  130. 'label' => $all_variants['italic'],
  131. ),
  132. array(
  133. 'id' => '700',
  134. 'label' => $all_variants['700'],
  135. ),
  136. array(
  137. 'id' => '700italic',
  138. 'label' => $all_variants['700italic'],
  139. ),
  140. ),
  141. );
  142. }
  143. $google_fonts_final = array();
  144. foreach ( $google_fonts as $family => $args ) {
  145. $label = ( isset( $args['label'] ) ) ? $args['label'] : $family;
  146. $variants = ( isset( $args['variants'] ) ) ? $args['variants'] : array( 'regular', '700' );
  147. $subsets = ( isset( $args['subsets'] ) ) ? $args['subsets'] : array();
  148. $available_variants = array();
  149. foreach ( $variants as $variant ) {
  150. if ( array_key_exists( $variant, $all_variants ) ) {
  151. $available_variants[] = array( 'id' => $variant, 'label' => $all_variants[ $variant ] );
  152. }
  153. }
  154. $available_subsets = array();
  155. foreach ( $subsets as $subset ) {
  156. if ( array_key_exists( $subset, $all_subsets ) ) {
  157. $available_subsets[] = array( 'id' => $subset, 'label' => $all_subsets[ $subset ] );
  158. }
  159. }
  160. $google_fonts_final[] = array(
  161. 'family' => $family,
  162. 'label' => $label,
  163. 'variants' => $available_variants,
  164. 'subsets' => $available_subsets,
  165. );
  166. }
  167. $final = array_merge( $standard_fonts_final, $google_fonts_final );
  168. wp_localize_script( 'kirki-typography', 'kirkiAllFonts', $final );
  169. }
  170. /**
  171. * Enqueues the script responsible for branding the customizer
  172. * and also adds variables to it using the wp_localize_script function.
  173. * The actual branding is handled via JS.
  174. */
  175. public function branding() {
  176. $config = apply_filters( 'kirki/config', array() );
  177. $vars = array(
  178. 'logoImage' => '',
  179. 'description' => '',
  180. );
  181. if ( isset( $config['logo_image'] ) && '' !== $config['logo_image'] ) {
  182. $vars['logoImage'] = esc_url_raw( $config['logo_image'] );
  183. }
  184. if ( isset( $config['description'] ) && '' !== $config['description'] ) {
  185. $vars['description'] = esc_textarea( $config['description'] );
  186. }
  187. if ( ! empty( $vars['logoImage'] ) || ! empty( $vars['description'] ) ) {
  188. wp_register_script( 'kirki-branding', Kirki::$url . '/assets/js/branding.js' );
  189. wp_localize_script( 'kirki-branding', 'kirkiBranding', $vars );
  190. wp_enqueue_script( 'kirki-branding' );
  191. }
  192. }
  193. /**
  194. * Enqueues the postMessage script
  195. * and adds variables to it using the wp_localize_script function.
  196. * The rest is handled via JS.
  197. */
  198. public function postmessage() {
  199. wp_enqueue_script( 'kirki_auto_postmessage', trailingslashit( Kirki::$url ) . 'assets/js/postmessage.js', array( 'customize-preview' ), false, true );
  200. $js_vars_fields = array();
  201. $fields = Kirki::$fields;
  202. foreach ( $fields as $field ) {
  203. if ( isset( $field['transport'] ) && 'postMessage' === $field['transport'] && isset( $field['js_vars'] ) && ! empty( $field['js_vars'] ) && is_array( $field['js_vars'] ) && isset( $field['settings'] ) ) {
  204. $js_vars_fields[ $field['settings'] ] = $field['js_vars'];
  205. }
  206. }
  207. wp_localize_script( 'kirki_auto_postmessage', 'jsvars', $js_vars_fields );
  208. }
  209. }
  210. }