Completed
Push — develop ( 75cfad...84b093 )
by Aristeides
02:34
created

Kirki_Control_FontAwesome::enqueue()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 4
nop 0
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
1
<?php
2
/**
3
 * Customizer Control: kirki-fontawesome.
4
 *
5
 * @package     Kirki
6
 * @subpackage  Controls
7
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
8
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Select control.
19
 */
20
class Kirki_Control_FontAwesome extends WP_Customize_Control {
21
22
	/**
23
	 * The control type.
24
	 *
25
	 * @access public
26
	 * @var string
27
	 */
28
	public $type = 'kirki-fontawesome';
29
30
	/**
31
	 * Used to automatically generate all CSS output.
32
	 *
33
	 * @access public
34
	 * @var array
35
	 */
36
	public $output = array();
37
38
	/**
39
	 * Data type
40
	 *
41
	 * @access public
42
	 * @var string
43
	 */
44
	public $option_type = 'theme_mod';
45
46
	/**
47
	 * Enqueue control related scripts/styles.
48
	 *
49
	 * @access public
50
	 */
51
	public function enqueue() {
52
53
		if ( class_exists( 'Kirki_Custom_Build' ) ) {
54
			Kirki_Custom_Build::register_dependency( 'jquery' );
55
			Kirki_Custom_Build::register_dependency( 'customize-base' );
56
			Kirki_Custom_Build::register_dependency( 'select2' );
57
			Kirki_Custom_Build::register_dependency( 'jquery-ui-sortable' );
58
		}
59
60
		$script_to_localize = 'kirki-build';
61
		if ( ! class_exists( 'Kirki_Custom_Build' ) || ! Kirki_Custom_Build::is_custom_build() ) {
62
			$script_to_localize = 'kirki-fontawesome';
63
			wp_enqueue_script( 'kirki-fontawesome', trailingslashit( Kirki::$url ) . 'controls/fontawesome/fontawesome.js', array( 'jquery', 'customize-base', 'select2', 'jquery-ui-sortable' ), false, true );
64
			wp_enqueue_style( 'kirki-fontawesome-css', trailingslashit( Kirki::$url ) . 'controls/fontawesome/fontawesome.css', null );
65
			wp_enqueue_style( 'kirki-fontawesome-font-css', trailingslashit( Kirki::$url ) . 'controls/fontawesome/font-awesome.css', null );
66
		}
67
		wp_enqueue_script( 'select2', trailingslashit( Kirki::$url ) . 'assets/vendor/select2/js/select2.full.js', array( 'jquery' ), '4.0.3', true );
68
		wp_enqueue_style( 'select2', trailingslashit( Kirki::$url ) . 'assets/vendor/select2/css/select2.css', array(), '4.0.3' );
69
		wp_enqueue_style( 'kirki-select2', trailingslashit( Kirki::$url ) . 'assets/vendor/select2/kirki.css', null );
70
		$json_path = wp_normalize_path( dirname( __FILE__ ) . '/fontawesome.json' );
71
		wp_localize_script( $script_to_localize, 'fontAwesomeJSON', include( $json_path ) );
72
	}
73
74
	/**
75
	 * Refresh the parameters passed to the JavaScript via JSON.
76
	 *
77
	 * @see WP_Customize_Control::to_json()
78
	 */
79
	public function to_json() {
80
		parent::to_json();
81
82
		$this->json['default'] = $this->setting->default;
83
		if ( isset( $this->default ) ) {
84
			$this->json['default'] = $this->default;
85
		}
86
		$this->json['output']  = $this->output;
87
		$this->json['value']   = $this->value();
88
		$this->json['choices'] = $this->choices;
89
		$this->json['link']    = $this->get_link();
90
		$this->json['id']      = $this->id;
91
92
		$this->json['inputAttrs'] = '';
93
		foreach ( $this->input_attrs as $attr => $value ) {
94
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
95
		}
96
	}
97
98
99
	/**
100
	 * An Underscore (JS) template for this control's content (but not its container).
101
	 *
102
	 * Class variables for this control class are available in the `data` JS object;
103
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
104
	 *
105
	 * @see WP_Customize_Control::print_template()
106
	 *
107
	 * @access protected
108
	 */
109
	protected function content_template() {
110
		?>
111
		<div class="kirki-controls-loading-spinner"><div class="bounce1"></div><div class="bounce2"></div><div class="bounce3"></div></div>
112
		<label>
113
			<# if ( data.label ) { #><span class="customize-control-title">{{ data.label }}</span><# } #>
114
			<# if ( data.description ) { #><span class="description customize-control-description">{{{ data.description }}}</span><# } #>
115
			<select {{{ data.inputAttrs }}} {{{ data.link }}}></select>
116
		</label>
117
		<?php
118
	}
119
120
121
	/**
122
	 * Render the control's content.
123
	 *
124
	 * Allows the content to be overridden without having to rewrite the wrapper in `$this::render()`.
125
	 *
126
	 * Supports basic input types `text`, `checkbox`, `textarea`, `radio`, `select` and `dropdown-pages`.
127
	 * Additional input types such as `email`, `url`, `number`, `hidden` and `date` are supported implicitly.
128
	 *
129
	 * Control content can alternately be rendered in JS. See WP_Customize_Control::print_template().
130
	 *
131
	 * @since 3.0.0
132
	 */
133
	protected function render_content() {}
134
}
135