1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Field; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Yaml\Yaml; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Association field class. |
9
|
|
|
* Allows selecting and manually sorting entries from various types: |
10
|
|
|
* - Posts |
11
|
|
|
* - Terms |
12
|
|
|
* - Users |
13
|
|
|
* - Comments |
14
|
|
|
*/ |
15
|
|
|
class Icon_Field extends Predefined_Options_Field { |
16
|
|
|
private static $fontawesome_options_cache = array(); |
17
|
|
|
|
18
|
|
|
private static $dashicons_options_cache = array(); |
19
|
|
|
|
20
|
|
|
public $none_label = ''; |
21
|
|
|
|
22
|
|
|
public $button_label = ''; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Admin initialization actions |
26
|
|
|
*/ |
27
|
|
|
public function admin_init() { |
28
|
|
|
$this->none_label = __( 'None', 'carbon-fields' ); |
29
|
|
|
$this->button_label = __( 'Select Icon', 'carbon-fields' ); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Hook administration scripts and styles. |
34
|
|
|
*/ |
35
|
|
|
public static function admin_enqueue_scripts() { |
36
|
|
|
wp_enqueue_style( 'fontawesome', 'https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css', array(), '4.7.0' ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function get_default_options() { |
40
|
|
|
$options = array( |
41
|
|
|
''=>array( |
|
|
|
|
42
|
|
|
'name' => $this->none_label, |
43
|
|
|
'id' => '', |
44
|
|
|
'categories' => array(), |
45
|
|
|
'class'=>'fa', |
|
|
|
|
46
|
|
|
'contents'=>' ', |
|
|
|
|
47
|
|
|
), |
48
|
|
|
); |
49
|
|
|
return $options; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public static function get_fontawesome_options() { |
53
|
|
|
if ( empty( static::$fontawesome_options_cache ) ) { |
|
|
|
|
54
|
|
|
$data = Yaml::parse( file_get_contents( \Carbon_Fields\DIR . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'fontawesome' . DIRECTORY_SEPARATOR . 'fontawesome.yml' ) ); |
|
|
|
|
55
|
|
|
foreach ( $data['icons'] as $icon ) { |
56
|
|
|
static::$fontawesome_options_cache[ $icon['id'] ] = array( |
|
|
|
|
57
|
|
|
'name'=>$icon['name'], |
|
|
|
|
58
|
|
|
'id'=>$icon['id'], |
|
|
|
|
59
|
|
|
'categories'=>$icon['categories'], |
|
|
|
|
60
|
|
|
'class'=>'fa fa-' . $icon['id'], |
|
|
|
|
61
|
|
|
'contents'=>'', |
|
|
|
|
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
return static::$fontawesome_options_cache; |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function add_fontawesome_options() { |
69
|
|
|
return $this->add_options( static::get_fontawesome_options() ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public static function get_dashicons_options() { |
73
|
|
|
if ( empty( static::$dashicons_options_cache ) ) { |
|
|
|
|
74
|
|
|
$data = include( \Carbon_Fields\DIR . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'dashicons' . DIRECTORY_SEPARATOR . 'dashicons.php' ); |
75
|
|
|
foreach ( $data as $icon ) { |
76
|
|
|
static::$dashicons_options_cache[ $icon ] = array( |
|
|
|
|
77
|
|
|
'name'=>$icon, |
|
|
|
|
78
|
|
|
'id'=>$icon, |
|
|
|
|
79
|
|
|
'categories'=>array(), |
|
|
|
|
80
|
|
|
'class'=>'dashicons-before ' . $icon, |
|
|
|
|
81
|
|
|
'contents'=>'', |
|
|
|
|
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
return static::$dashicons_options_cache; |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function add_dashicons_options() { |
89
|
|
|
return $this->add_options( static::get_dashicons_options() ); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Check if there are callbacks and populate the options |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
protected function load_options() { |
|
|
|
|
96
|
|
|
if ( empty( $this->options ) ) { |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ( is_callable( $this->options ) ) { |
101
|
|
|
$options = call_user_func( $this->options ); |
102
|
|
|
if ( ! is_array( $options ) ) { |
103
|
|
|
$options = array(); |
104
|
|
|
} |
105
|
|
|
} else { |
106
|
|
|
$options = $this->options; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->options = $options; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns an array that holds the field data, suitable for JSON representation. |
114
|
|
|
* This data will be available in the Underscore template and the Backbone Model. |
115
|
|
|
* |
116
|
|
|
* @param bool $load Should the value be loaded from the database or use the value from the current instance. |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
public function to_json( $load ) { |
120
|
|
|
$field_data = parent::to_json( $load ); |
121
|
|
|
$this->load_options(); |
122
|
|
|
|
123
|
|
|
$options = $this->options; |
124
|
|
|
if ( empty( $options ) ) { |
125
|
|
|
$options = static::get_fontawesome_options(); |
126
|
|
|
} |
127
|
|
|
$options = $this->get_default_options() + $options; |
128
|
|
|
$options = apply_filters( 'carbon_icon_options', $options, $this->get_name() ); |
129
|
|
|
|
130
|
|
|
$field_data = array_merge( $field_data, array( |
131
|
|
|
'options' => $options, |
132
|
|
|
'button_label' => $this->button_label, |
133
|
|
|
) ); |
134
|
|
|
|
135
|
|
|
return $field_data; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* The main Underscore template of this field. |
140
|
|
|
*/ |
141
|
|
|
public function template() { |
142
|
|
|
?> |
143
|
|
|
<div class="carbon-icon-container"> |
144
|
|
|
<input type="hidden" name="{{{ name }}}" value="{{{ value }}}" class="carbon-icon-value" /> |
145
|
|
|
<a href="#" class="carbon-icon-preview"> |
146
|
|
|
<i class="{{{ (value && typeof options[value] !== 'undefined') ? options[value].class : 'hidden' }}}"></i> |
147
|
|
|
<span class="button">{{{ button_label }}}</span> |
148
|
|
|
</a> |
149
|
|
|
|
150
|
|
|
<div class="carbon-icon-popup hidden"> |
151
|
|
|
<div class="carbon-icon-search dashicons-before dashicons-search"> |
152
|
|
|
<input type="text" value="" placeholder="<?php esc_attr_e( 'Search...', 'carbon-fields' ); ?>" /> |
153
|
|
|
</div> |
154
|
|
|
<div class="carbon-icon-scroll"> |
155
|
|
|
<ul class="carbon-icon-list"> |
156
|
|
|
<# if (options) { #> |
157
|
|
|
<# _.each(options, function(item) { #> |
158
|
|
|
<li class="carbon-icon-icon-container carbon-icon-icon-container-{{{ item.id }}}"> |
159
|
|
|
<a href="#" class="carbon-icon-icon-trigger {{{ value == item.id ? 'active' : '' }}}" data-value="{{{ item.id }}}"> |
160
|
|
|
<i class="{{{ item.class }}}">{{{ item.contents }}}</i> |
161
|
|
|
<span>{{{ item.name }}}</span> |
162
|
|
|
</a> |
163
|
|
|
</li> |
164
|
|
|
<# }); #> |
165
|
|
|
<# } #> |
166
|
|
|
</ul> |
167
|
|
|
</div> |
168
|
|
|
</div> |
169
|
|
|
</div> |
170
|
|
|
<?php |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|