ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class-kirki-enqueue.php (12420B)


      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 
     15 // Exit if accessed directly.
     16 if ( ! defined('ABSPATH')) {
     17     exit;
     18 }
     19 
     20 if ( ! class_exists('Kirki_Enqueue')) {
     21 
     22     /**
     23      * Enqueues JS & CSS assets
     24      */
     25     class Kirki_Enqueue
     26     {
     27 
     28         /**
     29          * The class constructor.
     30          * Adds actions to enqueue our assets.
     31          */
     32         public function __construct()
     33         {
     34             add_action('admin_enqueue_scripts', array($this, 'customize_controls_l10n'), 1);
     35             add_action('customize_controls_enqueue_scripts', array($this, 'customize_controls_enqueue_scripts'), 7);
     36             add_action('customize_controls_print_scripts', array($this, 'branding'));
     37             add_action('customize_preview_init', array($this, 'postmessage'));
     38         }
     39 
     40         /**
     41          * L10n helper for controls.
     42          */
     43         public function customize_controls_l10n()
     44         {
     45 
     46             // Register the l10n script.
     47             wp_register_script('kirki-l10n', trailingslashit(Kirki::$url) . 'assets/js/l10n.js');
     48 
     49             // Add localization strings.
     50             // We'll do this on a per-config basis so that the filters are properly applied.
     51             $configs = Kirki::$config;
     52             $l10n    = array();
     53             foreach ($configs as $id => $args) {
     54                 $l10n[$id] = Kirki_l10n::get_strings($id);
     55             }
     56 
     57             wp_localize_script('kirki-l10n', 'kirkiL10n', $l10n);
     58             wp_enqueue_script('kirki-l10n');
     59 
     60         }
     61 
     62         /**
     63          * Assets that have to be enqueued in 'customize_controls_enqueue_scripts'.
     64          */
     65         public function customize_controls_enqueue_scripts()
     66         {
     67 
     68             // Get an array of all our fields.
     69             $fields = Kirki::$fields;
     70 
     71             // Do we have tooltips anywhere?
     72             $has_tooltips = false;
     73             foreach ($fields as $field) {
     74                 if ($has_tooltips) {
     75                     continue;
     76                 }
     77                 // Field has tooltip.
     78                 if (isset($field['tooltip']) && ! empty($field['tooltip'])) {
     79                     $has_tooltips = true;
     80                 }
     81                 // Backwards-compatibility ("help" argument instead of "tooltip").
     82                 if (isset($field['help']) && ! empty($field['help'])) {
     83                     $has_tooltips = true;
     84                 }
     85             }
     86 
     87             // If we have tooltips, enqueue the tooltips script.
     88             /* TODO: if ( $has_tooltips ) { */
     89             wp_enqueue_script('kirki-tooltip', trailingslashit(Kirki::$url) . 'assets/js/tooltip.js', array('jquery', 'customize-controls', 'jquery-ui-tooltip'));
     90             /* TODO: } */
     91 
     92             // Enqueue the reset script.
     93             wp_enqueue_script('kirki-reset', trailingslashit(Kirki::$url) . 'assets/js/reset.js', array('jquery', 'kirki-set-setting-value'));
     94 
     95             // Register kirki-functions.
     96             wp_register_script('kirki-set-setting-value', trailingslashit(Kirki::$url) . 'assets/js/functions/set-setting-value.js');
     97             wp_register_script('kirki-validate-css-value', trailingslashit(Kirki::$url) . 'assets/js/functions/validate-css-value.js');
     98             wp_register_script('kirki-notifications', trailingslashit(Kirki::$url) . 'assets/js/functions/notifications.js', array('kirki-l10n', 'kirki-validate-css-value'));
     99 
    100             // Register serialize.js.
    101             wp_register_script('serialize-js', trailingslashit(Kirki::$url) . 'assets/js/vendor/serialize.js');
    102 
    103             // Register the color-alpha picker.
    104             wp_enqueue_style('wp-color-picker');
    105             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);
    106 
    107             // Register the jquery-ui-spinner.
    108             wp_register_script('jquery-ui-spinner', trailingslashit(Kirki::$url) . 'assets/js/vendor/jquery-ui-spinner', array('jquery', 'jquery-ui-core', 'jquery-ui-button'));
    109 
    110             // Register codemirror.
    111 //            wp_register_script('codemirror', trailingslashit(Kirki::$url) . 'assets/js/vendor/codemirror/lib/codemirror.js', array('jquery'));
    112 
    113             // Register selectize.
    114             wp_register_script('selectize', trailingslashit(Kirki::$url) . 'assets/js/vendor/selectize.js', array('jquery'));
    115 
    116             // An array of control scripts and their dependencies.
    117             $scripts = array(
    118                 // Add controls scripts.
    119                 'checkbox'        => array('jquery', 'customize-base'),
    120                 'code'            => array('jquery', 'customize-base', 'codemirror'),
    121                 'color'           => array('jquery', 'customize-base', 'wp-color-picker-alpha'),
    122                 'color-palette'   => array('jquery', 'customize-base', 'jquery-ui-button'),
    123                 'dashicons'       => array('jquery', 'customize-base'),
    124                 'date'            => array('jquery', 'customize-base', 'jquery-ui-datepicker'),
    125                 'dimension'       => array('jquery', 'customize-base', 'kirki-notifications'),
    126                 'dropdown-pages'  => array('jquery', 'customize-base', 'selectize'),
    127                 'editor'          => array('jquery', 'customize-base', 'kirki-l10n'),
    128                 'generic'         => array('jquery', 'customize-base'),
    129                 'multicheck'      => array('jquery', 'customize-base'),
    130                 'multicolor'      => array('jquery', 'customize-base', 'wp-color-picker-alpha'),
    131                 'number'          => array('jquery', 'customize-base', 'jquery-ui-spinner'),
    132                 'palette'         => array('jquery', 'customize-base', 'jquery-ui-button'),
    133                 'preset'          => array('jquery', 'customize-base', 'selectize', 'kirki-set-setting-value'),
    134                 'radio-buttonset' => array('jquery', 'customize-base'),
    135                 'radio-image'     => array('jquery', 'customize-base'),
    136                 'radio'           => array('jquery', 'customize-base'),
    137                 'repeater'        => array('jquery', 'customize-base', 'jquery-ui-core', 'jquery-ui-sortable', 'kirki-l10n'),
    138                 'select'          => array('jquery', 'customize-base', 'selectize'),
    139                 'slider'          => array('jquery', 'customize-base'),
    140                 'sortable'        => array('jquery', 'customize-base', 'jquery-ui-core', 'jquery-ui-sortable', 'serialize-js'),
    141                 'spacing'         => array('jquery', 'customize-base', 'kirki-notifications'),
    142                 'switch'          => array('jquery', 'customize-base'),
    143                 'toggle'          => array('jquery', 'customize-base'),
    144                 'typography'      => array('jquery', 'customize-base', 'selectize', 'wp-color-picker-alpha'),
    145             );
    146             foreach ($scripts as $id => $dependencies) {
    147                 wp_register_script('kirki-' . $id, trailingslashit(Kirki::$url) . 'assets/js/controls/' . $id . '.js', $dependencies, false, true);
    148             }
    149 
    150             // Add fonts to our JS objects.
    151             $google_fonts   = Kirki_Fonts::get_google_fonts();
    152             $standard_fonts = Kirki_Fonts::get_standard_fonts();
    153             $all_variants   = Kirki_Fonts::get_all_variants();
    154             $all_subsets    = Kirki_Fonts::get_google_font_subsets();
    155 
    156             $standard_fonts_final = array();
    157             foreach ($standard_fonts as $key => $value) {
    158                 $standard_fonts_final[] = array(
    159                     'family'      => $value['stack'],
    160                     'label'       => $value['label'],
    161                     'subsets'     => array(),
    162                     'is_standard' => true,
    163                     'variants'    => array(
    164                         array(
    165                             'id'    => 'regular',
    166                             'label' => $all_variants['regular'],
    167                         ),
    168                         array(
    169                             'id'    => 'italic',
    170                             'label' => $all_variants['italic'],
    171                         ),
    172                         array(
    173                             'id'    => '700',
    174                             'label' => $all_variants['700'],
    175                         ),
    176                         array(
    177                             'id'    => '700italic',
    178                             'label' => $all_variants['700italic'],
    179                         ),
    180                     ),
    181                 );
    182             }
    183 
    184             $google_fonts_final = array();
    185             foreach ($google_fonts as $family => $args) {
    186                 $label    = (isset($args['label'])) ? $args['label'] : $family;
    187                 $variants = (isset($args['variants'])) ? $args['variants'] : array('regular', '700');
    188                 $subsets  = (isset($args['subsets'])) ? $args['subsets'] : array();
    189 
    190                 $available_variants = array();
    191                 foreach ($variants as $variant) {
    192                     if (array_key_exists($variant, $all_variants)) {
    193                         $available_variants[] = array('id' => $variant, 'label' => $all_variants[$variant]);
    194                     }
    195                 }
    196 
    197                 $available_subsets = array();
    198                 foreach ($subsets as $subset) {
    199                     if (array_key_exists($subset, $all_subsets)) {
    200                         $available_subsets[] = array('id' => $subset, 'label' => $all_subsets[$subset]);
    201                     }
    202                 }
    203 
    204                 $google_fonts_final[] = array(
    205                     'family'   => $family,
    206                     'label'    => $label,
    207                     'variants' => $available_variants,
    208                     'subsets'  => $available_subsets,
    209                 );
    210             }
    211             $final = array_merge($standard_fonts_final, $google_fonts_final);
    212             wp_localize_script('kirki-typography', 'kirkiAllFonts', $final);
    213             wp_localize_script('kirki-typography', 'kirkiAllVariantsLabels', $all_variants);
    214         }
    215 
    216         /**
    217          * Enqueues the script responsible for branding the customizer
    218          * and also adds variables to it using the wp_localize_script function.
    219          * The actual branding is handled via JS.
    220          */
    221         public function branding()
    222         {
    223 
    224             $config = apply_filters('kirki/config', array());
    225             $vars   = array(
    226                 'logoImage'   => '',
    227                 'description' => '',
    228             );
    229             if (isset($config['logo_image']) && '' !== $config['logo_image']) {
    230                 $vars['logoImage'] = esc_url_raw($config['logo_image']);
    231             }
    232             if (isset($config['description']) && '' !== $config['description']) {
    233                 $vars['description'] = esc_textarea($config['description']);
    234             }
    235 
    236             if ( ! empty($vars['logoImage']) || ! empty($vars['description'])) {
    237                 wp_register_script('kirki-branding', Kirki::$url . '/assets/js/branding.js');
    238                 wp_localize_script('kirki-branding', 'kirkiBranding', $vars);
    239                 wp_enqueue_script('kirki-branding');
    240             }
    241         }
    242 
    243         /**
    244          * Enqueues the postMessage script
    245          * and adds variables to it using the wp_localize_script function.
    246          * The rest is handled via JS.
    247          */
    248         public function postmessage()
    249         {
    250             wp_enqueue_script('kirki_auto_postmessage', trailingslashit(Kirki::$url) . 'assets/js/postmessage.js', array('customize-preview'), false, true);
    251             $js_vars_fields = array();
    252             $fields         = Kirki::$fields;
    253             foreach ($fields as $field) {
    254                 if (isset($field['transport']) && 'postMessage' === $field['transport'] && isset($field['js_vars']) && ! empty($field['js_vars']) && is_array($field['js_vars']) && isset($field['settings'])) {
    255                     $js_vars_fields[$field['settings']] = $field['js_vars'];
    256                 }
    257             }
    258             wp_localize_script('kirki_auto_postmessage', 'jsvars', $js_vars_fields);
    259         }
    260     }
    261 }