Section_Icons.php
134 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Try to automatically generate the script necessary for adding icons to panels & section |
| 4 | * |
| 5 | * @package Kirki |
| 6 | * @category Core |
| 7 | * @author Themeum |
| 8 | * @copyright Copyright (c) 2023, Themeum |
| 9 | * @license https://opensource.org/licenses/MIT |
| 10 | * @since 3.0.0 |
| 11 | */ |
| 12 | |
| 13 | namespace Kirki\Module; |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit; |
| 17 | } |
| 18 | |
| 19 | use Kirki\URL; |
| 20 | |
| 21 | /** |
| 22 | * Adds scripts for icons in sections & panels. |
| 23 | */ |
| 24 | class Section_Icons { |
| 25 | |
| 26 | /** |
| 27 | * An array of panels and sections with icons. |
| 28 | * |
| 29 | * @static |
| 30 | * @access private |
| 31 | * @var array |
| 32 | */ |
| 33 | private static $icons = []; |
| 34 | |
| 35 | /** |
| 36 | * An array of panels. |
| 37 | * |
| 38 | * @access private |
| 39 | * @since 1.0 |
| 40 | * @var array |
| 41 | */ |
| 42 | private $panels = []; |
| 43 | |
| 44 | /** |
| 45 | * An array of sections. |
| 46 | * |
| 47 | * @access private |
| 48 | * @since 1.0 |
| 49 | * @var array |
| 50 | */ |
| 51 | private $sections = []; |
| 52 | |
| 53 | /** |
| 54 | * The class constructor. |
| 55 | * |
| 56 | * @access public |
| 57 | */ |
| 58 | public function __construct() { |
| 59 | |
| 60 | add_action( 'customize_controls_enqueue_scripts', [ $this, 'customize_controls_enqueue_scripts' ], 99 ); |
| 61 | add_action( 'kirki_panel_added', [ $this, 'panel_added' ], 10, 2 ); |
| 62 | add_action( 'kirki_section_added', [ $this, 'section_added' ], 10, 2 ); |
| 63 | |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Adds icon for a section/panel. |
| 68 | * |
| 69 | * @access public |
| 70 | * @since 3.0.0 |
| 71 | * @param string $id The panel or section ID. |
| 72 | * @param string $icon The icon to add. |
| 73 | * @param string $context Lowercase 'section' or 'panel'. |
| 74 | */ |
| 75 | public function add_icon( $id, $icon, $context = 'section' ) { |
| 76 | |
| 77 | self::$icons[ $context ][ $id ] = trim( $icon ); |
| 78 | |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Hooks in kirki_panel_added to populate $this->panels. |
| 83 | * |
| 84 | * @access public |
| 85 | * @since 1.0 |
| 86 | * @param string $id The panel ID. |
| 87 | * @param array $args The panel arguments. |
| 88 | */ |
| 89 | public function panel_added( $id, $args ) { |
| 90 | |
| 91 | if ( isset( $args['icon'] ) ) { |
| 92 | $args['id'] = $id; |
| 93 | $this->panels[] = $args; |
| 94 | } |
| 95 | |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Hooks in kirki_section_added to populate $this->sections. |
| 100 | * |
| 101 | * @access public |
| 102 | * @since 1.0 |
| 103 | * @param string $id The section ID. |
| 104 | * @param array $args The section arguments. |
| 105 | */ |
| 106 | public function section_added( $id, $args ) { |
| 107 | |
| 108 | if ( isset( $args['icon'] ) ) { |
| 109 | $args['id'] = $id; |
| 110 | $this->sections[] = $args; |
| 111 | } |
| 112 | |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Format the script in a way that will be compatible with WordPress. |
| 117 | */ |
| 118 | public function customize_controls_enqueue_scripts() { |
| 119 | |
| 120 | // Parse panels and find ones with icons. |
| 121 | foreach ( $this->panels as $panel ) { |
| 122 | $this->add_icon( $panel['id'], $panel['icon'], 'panel' ); |
| 123 | } |
| 124 | |
| 125 | // Parse sections and find ones with icons. |
| 126 | foreach ( $this->sections as $section ) { |
| 127 | $this->add_icon( $section['id'], $section['icon'], 'section' ); |
| 128 | } |
| 129 | |
| 130 | wp_localize_script( 'kirki-customizer', 'kirkiIcons', self::$icons ); |
| 131 | |
| 132 | } |
| 133 | |
| 134 | } |