Preset.php
95 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Automatic preset scripts calculation for Kirki controls. |
| 4 | * |
| 5 | * @package kirki-framework/module-preset |
| 6 | * @author Themeum |
| 7 | * @copyright Copyright (c) 2023, Themeum |
| 8 | * @license https://opensource.org/licenses/MIT |
| 9 | * @since 1.0.0 |
| 10 | */ |
| 11 | |
| 12 | namespace Kirki\Module; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; |
| 16 | } |
| 17 | |
| 18 | use Kirki\URL; |
| 19 | |
| 20 | /** |
| 21 | * Adds styles to the customizer. |
| 22 | */ |
| 23 | class Preset { |
| 24 | |
| 25 | /** |
| 26 | * The class instance. |
| 27 | * |
| 28 | * @static |
| 29 | * @access private |
| 30 | * @since 1.0.0 |
| 31 | * @var object |
| 32 | */ |
| 33 | private static $instance; |
| 34 | |
| 35 | /** |
| 36 | * An array of preset controls and their arguments. |
| 37 | * |
| 38 | * @static |
| 39 | * @access private |
| 40 | * @since 1.0.0 |
| 41 | * @var array |
| 42 | */ |
| 43 | private static $preset_controls = []; |
| 44 | |
| 45 | /** |
| 46 | * Get the one, true instance of this class. |
| 47 | * |
| 48 | * @static |
| 49 | * @access public |
| 50 | * @since 1.0.0 |
| 51 | * @return object |
| 52 | */ |
| 53 | public static function get_instance() { |
| 54 | if ( null === self::$instance ) { |
| 55 | self::$instance = new self(); |
| 56 | } |
| 57 | return self::$instance; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Constructor. |
| 62 | * |
| 63 | * @access public |
| 64 | * @since 1.0.0 |
| 65 | */ |
| 66 | public function __construct() { |
| 67 | add_action( 'customize_controls_print_footer_scripts', [ $this, 'customize_controls_print_footer_scripts' ] ); |
| 68 | add_filter( 'kirki_field_add_control_args', [ $this, 'field_add_control_args' ] ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Filter control arguments. |
| 73 | * |
| 74 | * @access public |
| 75 | * @since 1.0 |
| 76 | * @param array $args The field arguments. |
| 77 | * @return array |
| 78 | */ |
| 79 | public function field_add_control_args( $args ) { |
| 80 | if ( isset( $args['preset'] ) && is_array( $args['preset'] ) ) { |
| 81 | self::$preset_controls[ $args['settings'] ] = $args['preset']; |
| 82 | } |
| 83 | return $args; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Enqueue scripts. |
| 88 | * |
| 89 | * @access public |
| 90 | * @since 1.0.0 |
| 91 | */ |
| 92 | public function customize_controls_print_footer_scripts() { |
| 93 | wp_localize_script( 'kirki-customizer', 'kirkiPresetControls', self::$preset_controls ); |
| 94 | } |
| 95 | } |