class-customizer.php
104 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The Kirki Customizer class. |
| 4 | * |
| 5 | * @package kirki |
| 6 | * @since 5.0.0 |
| 7 | */ |
| 8 | |
| 9 | namespace Kirki; |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | use Kirki\L10n; |
| 16 | use Kirki\Compatibility\Modules; |
| 17 | use Kirki\Compatibility\Framework; |
| 18 | use Kirki\Compatibility\Kirki; |
| 19 | |
| 20 | if (!class_exists(__NAMESPACE__ . '\\Customizer', false)) { |
| 21 | /** |
| 22 | * The Customizer class. |
| 23 | */ |
| 24 | class Customizer |
| 25 | { |
| 26 | /** |
| 27 | * Init the customizer. |
| 28 | * |
| 29 | * @return void |
| 30 | */ |
| 31 | public static function init() |
| 32 | { |
| 33 | require_once __DIR__ . '/lib/class-aricolor.php'; |
| 34 | require_once __DIR__ . '/lib/class-kirki-color.php'; |
| 35 | require_once dirname(__DIR__) . '/vendor/autoload.php'; |
| 36 | require_once __DIR__ . '/bootstrap.php'; |
| 37 | |
| 38 | if (!defined('KIRKI_PLUGIN_DIR')) { |
| 39 | define('KIRKI_PLUGIN_DIR', dirname(__DIR__)); |
| 40 | } |
| 41 | |
| 42 | if (!defined('KIRKI_PLUGIN_URL')) { |
| 43 | define('KIRKI_PLUGIN_URL', URL::get_from_path(dirname(__DIR__))); |
| 44 | } |
| 45 | |
| 46 | // Start Kirki. |
| 47 | global $kirki; |
| 48 | $kirki = Framework::get_instance(); |
| 49 | |
| 50 | // Instantiate the modules. |
| 51 | $kirki->modules = new Modules(); |
| 52 | |
| 53 | // Instantiate classes. |
| 54 | new Kirki(); |
| 55 | new L10n('kirki', dirname(__DIR__) . '/languages'); |
| 56 | |
| 57 | // Add an empty config for global fields. |
| 58 | Kirki::add_config(''); |
| 59 | |
| 60 | // Load custom config if exists. |
| 61 | $custom_config_path = dirname(__DIR__) . '/custom-config.php'; |
| 62 | if (file_exists($custom_config_path)) { |
| 63 | require_once $custom_config_path; |
| 64 | } |
| 65 | // Load packages. |
| 66 | require_once __DIR__ . '/packages/index.php'; |
| 67 | |
| 68 | // Handle tests. |
| 69 | if (defined('KIRKI_TEST') && true === constant('KIRKI_TEST') && file_exists(__DIR__ . '/example.php')) { |
| 70 | include_once __DIR__ . '/example.php'; |
| 71 | } |
| 72 | |
| 73 | // Enqueue common assets. |
| 74 | add_action('customize_controls_enqueue_scripts', [__CLASS__, 'enqueue_assets'], 5); |
| 75 | add_action('customize_preview_init', [__CLASS__, 'enqueue_assets'], 5); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Enqueue common assets. |
| 80 | * |
| 81 | * @param array $deps The script dependencies. |
| 82 | * @return void |
| 83 | */ |
| 84 | public static function enqueue_assets($deps = []) |
| 85 | { |
| 86 | // Preview frame is NOT admin, Pane frame IS admin. |
| 87 | $is_preview_frame = is_customize_preview() && !is_admin(); |
| 88 | |
| 89 | if (empty($deps)) { |
| 90 | $deps = ['jquery', 'wp-element']; |
| 91 | if ($is_preview_frame) { |
| 92 | $deps[] = 'customize-preview'; |
| 93 | } else { |
| 94 | $deps[] = 'customize-controls'; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | $file = $is_preview_frame ? 'preview' : 'controls'; |
| 99 | |
| 100 | wp_enqueue_style('kirki-customizer', URL::get_from_path(dirname(__DIR__) . "/assets/customizer/{$file}.min.css"), [], KIRKI_VERSION); |
| 101 | wp_enqueue_script('kirki-customizer', URL::get_from_path(dirname(__DIR__) . "/assets/customizer/{$file}.min.js"), $deps, KIRKI_VERSION, true); |
| 102 | } |
| 103 | } |
| 104 | } |