-
Notifications
You must be signed in to change notification settings - Fork 10
/
icon-picker.php
340 lines (285 loc) · 6.81 KB
/
icon-picker.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
/**
* Icon Picker
*
* @package Icon_Picker
* @version 0.5.0
* @author Dzikri Aziz <[email protected]>
*
*
* Plugin Name: Icon Picker
* Plugin URI: http://kucrut.org/
* Description: Pick an icon of your choice.
* Version: 0.5.0
* Author: Dzikri Aziz
* Author URI: http://kucrut.org/
* License: GPLv2
* Text Domain: icon-picker
* Domain Path: /languages
*/
final class Icon_Picker {
const VERSION = '0.5.0';
/**
* Icon_Picker singleton
*
* @static
* @since 0.1.0
* @access protected
* @var Icon_Picker
*/
protected static $instance;
/**
* Plugin directory path
*
* @access protected
* @since 0.1.0
* @var array
*/
protected $dir;
/**
* Plugin directory url path
*
* @since 0.1.0
* @access protected
* @var array
*/
protected $url;
/**
* Icon types registry
*
* @since 0.1.0
* @access protected
* @var Icon_Picker_Types_Registry
*/
protected $registry;
/**
* Loader
*
* @since 0.1.0
* @access protected
* @var Icon_Picker_Loader
*/
protected $loader;
/**
* Whether the functionality is loaded on admin
*
* @since 0.1.0
* @access protected
* @var bool
*/
protected $is_admin_loaded = false;
/**
* Default icon types
*
* @since 0.1.0
* @access protected
* @var array
*/
protected $default_types = array(
'dashicons' => 'Dashicons',
'elusive' => 'Elusive',
'fa' => 'Font_Awesome',
'foundation-icons' => 'Foundation',
'genericon' => 'Genericons',
'image' => 'Image',
'svg' => 'Svg',
);
/**
* Setter magic
*
* @since 0.1.0
* @param string $name Property name.
* @return bool
*/
public function __isset( $name ) {
return isset( $this->$name );
}
/**
* Getter magic
*
* @since 0.1.0
* @param string $name Property name.
* @return mixed NULL if attribute doesn't exist.
*/
public function __get( $name ) {
if ( isset( $this->$name ) ) {
return $this->$name;
}
return null;
}
/**
* Get instance
*
* @since 0.1.0
* @param array $args Arguments {@see Icon_Picker::__construct()}.
* @return Icon_Picker
*/
public static function instance( $args = array() ) {
if ( is_null( self::$instance ) ) {
self::$instance = new self( $args );
}
return self::$instance;
}
/**
* Constructor
*
* @since 0.1.0
* @access protected
* @param array $args {
* Optional. Arguments to override class property defaults.
*
* @type string $dir Plugin directory path (without trailing slash).
* @type string $url Plugin directory url path (without trailing slash).
* }
* @return Icon_Picker
*/
protected function __construct( $args = array() ) {
$defaults = array(
'dir' => untrailingslashit( plugin_dir_path( __FILE__ ) ),
'url' => untrailingslashit( plugin_dir_url( __FILE__ ) ),
);
$args = wp_parse_args( $args, $defaults );
$keys = array_keys( get_object_vars( $this ) );
// Disallow.
unset( $args['registry'] );
unset( $args['loader'] );
unset( $args['is_admin_loaded'] );
foreach ( $keys as $key ) {
if ( isset( $args[ $key ] ) ) {
$this->$key = $args[ $key ];
}
}
$locale = apply_filters( 'plugin_locale', get_locale(), 'icon-picker' );
load_textdomain( 'icon-picker', WP_LANG_DIR . "/icon-picker/icon-picker-{$locale}.mo" );
load_plugin_textdomain( 'icon-picker', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
add_action( 'wp_loaded', array( $this, 'init' ) );
}
/**
* Register icon types
*
* @since 0.1.0
* @wp_hook action wp_loaded
* @return void
*/
public function init() {
// Initialize icon types registry.
$this->init_registry();
// Initialize loader.
$this->init_loader();
// Initialize field.
$this->init_field();
/**
* Fires when Icon Picker is ready
*
* @since 0.1.0
* @param Icon_Picker $this Icon_Picker instance.
*/
do_action( 'icon_picker_init', $this );
}
/**
* Initialize icon types registry
*
* @since 0.1.0
* @access protected
* @return void
*/
protected function init_registry() {
require_once "{$this->dir}/includes/registry.php";
$this->registry = Icon_Picker_Types_Registry::instance();
$this->register_default_types();
/**
* Fires when Icon Picker's Registry is ready and the default types are registered.
*
* @since 0.1.0
* @param Icon_Picker $this Icon_Picker instance.
*/
do_action( 'icon_picker_types_registry_ready', $this );
}
/**
* Register default icon types
*
* @since 0.1.0
* @access protected
*/
protected function register_default_types() {
require_once "{$this->dir}/includes/fontpack.php";
Icon_Picker_Fontpack::instance();
/**
* Allow themes/plugins to disable one or more default types
*
* @since 0.1.0
* @param array $default_types Default icon types.
*/
$default_types = array_filter( (array) apply_filters( 'icon_picker_default_types', $this->default_types ) );
/**
* Validate filtered default types
*/
$default_types = array_intersect( $this->default_types, $default_types );
if ( empty( $default_types ) ) {
return;
}
foreach ( $default_types as $filename => $class_suffix ) {
$class_name = "Icon_Picker_Type_{$class_suffix}";
require_once "{$this->dir}/includes/types/{$filename}.php";
$this->registry->add( new $class_name() );
}
}
/**
* Initialize loader
*
* @since 0.1.0
* @access protected
* @return void
*/
protected function init_loader() {
require_once "{$this->dir}/includes/loader.php";
$this->loader = Icon_Picker_Loader::instance();
/**
* Fires when Icon Picker's Registry is ready and the default types are registered.
*
* @since 0.1.0
* @param Icon_Picker $this Icon_Picker instance.
*/
do_action( 'icon_picker_loader_ready', $this );
}
/**
* Initialize field functionalities
*
* @since 0.2.0
* @access protected
*/
protected function init_field() {
require_once "{$this->dir}/includes/fields/base.php";
add_filter( 'cmb_field_types', array( $this, 'register_cmb_field' ) );
}
/**
* Register the field for Custom Meta Boxes
*
* @since 0.2.0
* @wp_hook filter cmb_field_types
* @link https://github.com/humanmade/Custom-Meta-Boxes/ Custom Meta Boxes
*
* @param array $field_types Available CMB field types.
*
* @return array
*/
public function register_cmb_field( $field_types ) {
require_once "{$this->dir}/includes/fields/cmb.php";
$field_types['icon'] = 'Icon_Picker_Field_Cmb';
return $field_types;
}
/**
* Load icon picker functionality on an admin page
*
* @since 0.1.0
* @return void
*/
public function load() {
if ( true === $this->is_admin_loaded ) {
return;
}
$this->loader->load();
$this->is_admin_loaded = true;
}
}
add_action( 'plugins_loaded', array( 'Icon_Picker', 'instance' ), 7 );