forked from themeum/kirki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-kirki-scripts-icons.php
108 lines (93 loc) · 2.55 KB
/
class-kirki-scripts-icons.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
* Try to automatically generate the script necessary for adding icons to panels & section
*
* @package Kirki
* @category Core
* @author Aristeides Stathopoulos
* @copyright Copyright (c) 2016, Aristeides Stathopoulos
* @license http://opensource.org/licenses/https://opensource.org/licenses/MIT
* @since 2.0
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'Kirki_Scripts_Icons' ) ) {
/**
* Adds scripts for icons in sections & panels.
*/
class Kirki_Scripts_Icons {
/**
* The script generated for ALL fields
*
* @static
* @access public
* @var string
*/
public static $icons_script = '';
/**
* Whether the script has already been added to the customizer or not.
*
* @static
* @access public
* @var bool
*/
public static $script_added = false;
/**
* The class constructor
*/
public function __construct() {
add_action( 'customize_controls_print_footer_scripts', array( $this, 'enqueue_script' ), 99 );
}
/**
* This works on a per-field basis.
* Once created, the script is added to the $icons_script property.
*
* @static
* @access public
* @param array $args The field definition.
* @return void
*/
public static function generate_script( $args = array() ) {
/**
* If "icon" is not specified
* then no need to proceed.
*/
if ( ! isset( $args['icon'] ) || '' == $args['icon'] ) {
return;
}
/**
* If this is not a panel or section
* then no need to proceed.
*/
if ( ! isset( $args['context'] ) || ! in_array( $args['context'], array( 'panel', 'section' ) ) ) {
return;
}
/**
* If the panel or section ID is not defined
* then early exit.
*/
if ( ! isset( $args['id'] ) ) {
return;
}
$element = '#accordion-' . $args['context'] . '-' . $args['id'] . ' h3';
if ( false !== strpos( $args['icon'], 'dashicons' ) ) {
$args['icon'] = 'dashicons ' . $args['icon'];
}
$script = '$("' . $element . '").prepend(\'<span class="' . esc_attr( $args['icon'] ) . '"></span>\');';
if ( false === strpos( self::$icons_script, $script ) ) {
self::$icons_script .= $script;
}
}
/**
* Format the script in a way that will be compatible with WordPress.
*/
public function enqueue_script() {
if ( ! self::$script_added && '' != self::$icons_script ) {
self::$script_added = true;
echo '<script>jQuery(document).ready(function($) { "use strict"; ' . wp_kses_post( self::$icons_script ) . '});</script>';
}
}
}
}