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 Glyph_Field extends Field { |
16
|
|
|
static $options = array(); |
|
|
|
|
17
|
|
|
|
18
|
|
|
public $none_label = ''; |
19
|
|
|
|
20
|
|
|
public $button_label = ''; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Admin initialization actions |
24
|
|
|
*/ |
25
|
|
|
public function admin_init() { |
26
|
|
|
$this->none_label = __( 'None', 'carbon-fields' ); |
27
|
|
|
$this->button_label = __( 'Select Icon', 'carbon-fields' ); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Hook administration scripts and styles. |
32
|
|
|
*/ |
33
|
|
|
public static function admin_enqueue_scripts() { |
34
|
|
|
wp_enqueue_style( 'fontawesome', \Carbon_Fields\URL . '/assets/fontawesome/font-awesome.min.css', array(), '4.7.0' ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Generate the item options for the glyph field. |
39
|
|
|
* |
40
|
|
|
* @return array $options The selectable options of the glyph field. |
41
|
|
|
*/ |
42
|
|
|
public function get_options() { |
43
|
|
|
if ( empty( $options ) ) { |
|
|
|
|
44
|
|
|
$data = Yaml::parse( file_get_contents( \Carbon_Fields\DIR . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'fontawesome' . DIRECTORY_SEPARATOR . 'fontawesome.yml' ) ); |
|
|
|
|
45
|
|
|
static::$options = array( |
|
|
|
|
46
|
|
|
''=>array( |
|
|
|
|
47
|
|
|
'name' => $this->none_label, |
48
|
|
|
'id' => '', |
49
|
|
|
'categories' => array(), |
50
|
|
|
'class'=>'fa', |
|
|
|
|
51
|
|
|
'contents'=>' ', |
|
|
|
|
52
|
|
|
), |
53
|
|
|
); |
54
|
|
|
foreach ( $data['icons'] as $icon ) { |
55
|
|
|
static::$options[ $icon['id'] ] = array( |
|
|
|
|
56
|
|
|
'name'=>$icon['name'], |
|
|
|
|
57
|
|
|
'id'=>$icon['id'], |
|
|
|
|
58
|
|
|
'categories'=>$icon['categories'], |
|
|
|
|
59
|
|
|
'class'=>'fa fa-' . $icon['id'], |
|
|
|
|
60
|
|
|
'contents'=>'', |
|
|
|
|
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Filter the final list of options, available to a certain glyph field. |
67
|
|
|
* |
68
|
|
|
* @param array $options Unfiltered options items. |
69
|
|
|
* @param string $name Name of the glyph field. |
70
|
|
|
*/ |
71
|
|
|
$options = apply_filters( 'carbon_glyph_options', static::$options, $this->get_name() ); |
|
|
|
|
72
|
|
|
|
73
|
|
|
return $options; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns an array that holds the field data, suitable for JSON representation. |
78
|
|
|
* This data will be available in the Underscore template and the Backbone Model. |
79
|
|
|
* |
80
|
|
|
* @param bool $load Should the value be loaded from the database or use the value from the current instance. |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public function to_json( $load ) { |
|
|
|
|
84
|
|
|
$field_data = parent::to_json( $load ); |
85
|
|
|
|
86
|
|
|
$field_data = array_merge( $field_data, array( |
87
|
|
|
'options' => $this->get_options(), |
88
|
|
|
'button_label' => $this->button_label, |
89
|
|
|
) ); |
90
|
|
|
|
91
|
|
|
return $field_data; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* The main Underscore template of this field. |
96
|
|
|
*/ |
97
|
|
|
public function template() { |
98
|
|
|
?> |
99
|
|
|
<div class="carbon-glyph-container"> |
100
|
|
|
<input type="hidden" name="{{{ name }}}" value="{{{ value }}}" class="carbon-glyph-value" /> |
101
|
|
|
<a href="#" class="carbon-glyph-preview"> |
102
|
|
|
<i class="{{{ (value && typeof options[value] !== 'undefined') ? options[value].class : 'hidden' }}}"></i> |
103
|
|
|
<span class="button">{{{ button_label }}}</span> |
104
|
|
|
</a> |
105
|
|
|
|
106
|
|
|
<div class="carbon-glyph-popup hidden"> |
107
|
|
|
<div class="carbon-glyph-search dashicons-before dashicons-search"> |
108
|
|
|
<input type="text" value="" placeholder="<?php esc_attr_e( 'Search...', 'carbon-fields' ); ?>" /> |
109
|
|
|
</div> |
110
|
|
|
<div class="carbon-glyph-scroll"> |
111
|
|
|
<ul class="carbon-glyph-list"> |
112
|
|
|
<# if (options) { #> |
113
|
|
|
<# _.each(options, function(item) { #> |
114
|
|
|
<li class="carbon-glyph-icon-container carbon-glyph-icon-container-{{{ item.id }}}"> |
115
|
|
|
<a href="#" class="carbon-glyph-icon-trigger {{{ value == item.id ? 'active' : '' }}}" data-value="{{{ item.id }}}"> |
116
|
|
|
<i class="{{{ item.class }}}">{{{ item.contents }}}</i> |
117
|
|
|
<span>{{{ item.name }}}</span> |
118
|
|
|
</a> |
119
|
|
|
</li> |
120
|
|
|
<# }); #> |
121
|
|
|
<# } #> |
122
|
|
|
</ul> |
123
|
|
|
</div> |
124
|
|
|
</div> |
125
|
|
|
</div> |
126
|
|
|
<?php |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.