Completed
Pull Request — master (#138)
by
unknown
02:24
created

Icon_Field::admin_init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
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
	public $none_label = '';
17
18
	public $button_label = '';
19
20
	/**
21
	 * Admin initialization actions
22
	 */
23
	public function admin_init() {
24
		$this->none_label = __( 'None', 'carbon-fields' );
25
		$this->button_label = __( 'Select Icon', 'carbon-fields' );
26
	}
27
28
	/**
29
	 * Hook administration scripts and styles.
30
	 */
31
	public static function admin_enqueue_scripts() {
32
		wp_enqueue_style( 'fontawesome', 'https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css', array(), '4.7.0' );
33
	}
34
35
	public function get_default_options() {
36
		$options = array(
37
			''=>array(
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
38
				'name' => $this->none_label,
39
				'id' => '',
40
				'categories' => array(),
41
				'class'=>'fa',
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
42
				'contents'=>'&nbsp;',
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
43
			),
44
		);
45
		return $options;
46
	}
47
48
	public static function get_fontawesome_options() {
49
		static $options = array();
50
51
		if ( empty( $options ) ) {
52
			$data = Yaml::parse( file_get_contents( \Carbon_Fields\DIR . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'fontawesome' . DIRECTORY_SEPARATOR . 'fontawesome.yml' ) );
0 ignored issues
show
introduced by
file_get_contents is highly discouraged, please use wpcom_vip_file_get_contents() instead.
Loading history...
53
			$options = array();
54
			foreach ( $data['icons'] as $icon ) {
55
				$options[ $icon['id'] ] = array(
56
					'name'=>$icon['name'],
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
57
					'id'=>$icon['id'],
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
58
					'categories'=>$icon['categories'],
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
59
					'class'=>'fa fa-' . $icon['id'],
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
60
					'contents'=>'',
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
61
				);
62
			}
63
		}
64
		
65
		return $options;
66
	}
67
68
	public static function get_dashicons_options() {
69
		$data = include( \Carbon_Fields\DIR . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'dashicons' . DIRECTORY_SEPARATOR . 'dashicons.php' );
70
		$options = array();
71
		foreach ( $data as $icon ) {
72
			$options[ $icon ] = array(
73
				'name'=>$icon,
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
74
				'id'=>$icon,
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
75
				'categories'=>array(),
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
76
				'class'=>'dashicons-before ' . $icon,
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
77
				'contents'=>'',
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
78
			);
79
		}
80
		return $options;
81
	}
82
83
	/**
84
	 * Returns an array that holds the field data, suitable for JSON representation.
85
	 * This data will be available in the Underscore template and the Backbone Model.
86
	 *
87
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
88
	 * @return array
89
	 */
90
	public function to_json( $load ) {
91
		$field_data = parent::to_json( $load );
92
93
		$options = $this->options;
94
		if ( empty( $options ) ) {
95
			$options = $this->get_default_options() + static::get_fontawesome_options();
96
		}
97
		$options = apply_filters( 'carbon_icon_options', $options, $this->get_name() );
98
99
		$field_data = array_merge( $field_data, array(
100
			'options' => $options,
101
			'button_label' => $this->button_label,
102
		) );
103
104
		return $field_data;
105
	}
106
107
	/**
108
	 * The main Underscore template of this field.
109
	 */
110
	public function template() {
111
		?>
112
		<div class="carbon-icon-container">
113
			<input type="hidden" name="{{{ name }}}" value="{{{ value }}}" class="carbon-icon-value" />
114
			<a href="#" class="carbon-icon-preview">
115
				<i class="{{{ (value && typeof options[value] !== 'undefined') ? options[value].class : 'hidden' }}}"></i>
116
				<span class="button">{{{ button_label }}}</span>
117
			</a>
118
119
			<div class="carbon-icon-popup hidden">
120
				<div class="carbon-icon-search dashicons-before dashicons-search">
121
					<input type="text" value="" placeholder="<?php esc_attr_e( 'Search...', 'carbon-fields' ); ?>" />
122
				</div>
123
				<div class="carbon-icon-scroll">
124
					<ul class="carbon-icon-list">
125
						<# if (options) { #>
126
							<# _.each(options, function(item) { #>
127
								<li class="carbon-icon-icon-container carbon-icon-icon-container-{{{ item.id }}}">
128
									<a href="#" class="carbon-icon-icon-trigger {{{ value == item.id ? 'active' : '' }}}" data-value="{{{ item.id }}}">
129
										<i class="{{{ item.class }}}">{{{ item.contents }}}</i>
130
										<span>{{{ item.name }}}</span>
131
									</a>
132
								</li>
133
							<# }); #>
134
						<# } #>
135
					</ul>
136
				</div>
137
			</div>
138
		</div>
139
		<?php
140
	}
141
}
142