-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-metabox.php
526 lines (455 loc) · 18.9 KB
/
class-metabox.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
<?php
namespace Pluginever\Framework;
if ( ! class_exists( '\Pluginever\Framework\Metabox' ) ):
class Metabox {
/**
* @var object The single instance of the class
* @since 1.0
*/
protected static $_instance = array();
/**
* @var string
*/
protected $version = '1.0.1';
/**
* @var array An array where are saved all metabox settings options
*
* @since 1.0
*/
private $options = array();
/**
* @since 1.0.0
* @var array
*/
private $fields = array();
/**
* Main Instance
*
* @static
*
* @param $id
*
* @return object Main instance
*
* @since 1.0.0
*/
public static function instance( $id ) {
if ( ! isset( self::$_instance[ $id ] ) ) {
self::$_instance[ $id ] = new self( $id );
}
return self::$_instance[ $id ];
}
/**
* Metabox constructor.
*
* @param string $id
*/
function __construct( $id = '' ) {
$this->id = $id;
}
/**
* Init
*
* @since 1.0.0
*
* set options and tabs, add actions to register metabox, scripts and save data
*
* @param array $options
*
* @return void
*/
public function init( $options = array() ) {
$this->fields = self::set_fields( $options['fields'] );
unset( $options['fields'] );
$this->options = self::set_options( $options );
add_action( 'admin_enqueue_scripts', array( $this, 'load_assets' ), 15 );
add_action( 'add_meta_boxes', array( $this, 'register_metabox' ) );
add_action( 'save_post', array( $this, 'save_postdata' ) );
}
public function get_fields() {
return $this->fields;
}
/**
* Enqueue script and styles in admin side
*
* Add style and scripts to administrator
*
* @return void
*/
public function load_assets() {
$current_page = get_current_screen();
if ( ! empty( $current_page->id )
&& in_array( $current_page->id, (array) $this->options['screen'] ) ) {
$suffix = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? '' : '.min';
wp_enqueue_media();
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_style( 'select2', plugins_url( 'assets/css/select2.min.css', __FILE__ ), false, $this->version );
wp_enqueue_style( 'tooltipster', plugins_url( 'assets/css/tooltipster.bundle.min.css', __FILE__ ) );
wp_enqueue_script( 'select2-js', plugins_url( 'assets/js/select2.min.js', __FILE__ ), [ 'jquery' ], $this->version, true );
wp_enqueue_script( 'conditionize', plugins_url( 'assets/js/conditionize.js', __FILE__ ), [ 'jquery' ], $this->version, true );
wp_enqueue_script( 'tooltipster', plugins_url( 'assets/js/tooltipster.bundle.min.js', __FILE__ ), [ 'jquery' ], $this->version, true );
wp_enqueue_script( 'wp-color-picker-alpha', plugins_url( 'assets/js/wp-color-picker-alpha.min.js', __FILE__ ), [
'jquery',
'wp-color-picker'
], $this->version, true );
wp_enqueue_style( 'plvr-framework', plugins_url( "assets/css/framework{$suffix}.css", __FILE__ ), false, $this->version );
wp_enqueue_script( 'plvr-framework', plugins_url( "assets/js/framework.js", __FILE__ ), [ 'jquery' ], $this->version, true );
}
}
/**
* Set metabox options
*
* @since 1.0.0
*
* @param $options
*
* @return array
*/
protected static function set_options( $options ) {
$default = array(
'title' => __( 'Example Metabox', 'pluginever_framework' ),
'screen' => 'post', //or array( 'post-type1', 'post-type2')
'context' => 'normal', //('normal', 'advanced', or 'side')
'priority' => 'high',
'lazy_loading' => 'false',
);
return wp_parse_args( $options, $default );
}
/**
* Get default attributes
*
* @since 1.0.0
* @return array
*/
protected static function get_default_field_attr() {
return array(
'type' => '',
'name' => '',
'id' => '',
'label' => '',
'value' => '',
'placeholder' => '',
'sanitize' => '',
'help' => '',
'tooltip' => '',
'class' => '',
'wrapper_class' => '',
'title' => '',
'addon' => '',
'addon_pos' => '',
'required' => '',
'select2' => '',
'multiple' => '',
'settings' => array(), //for wp_editor
'options' => array(),
'custom_attr' => array(),
'condition' => array(),
'rgba' => 'false',
'parsed' => 'true',
);
}
/**
* Set fields
*
* @since 1.0.0
*
* @param $fields
*
* @return array
*/
protected static function set_fields( $fields ) {
$result = [];
foreach ( $fields as $field ) {
$field = wp_parse_args( $field, self::get_default_field_attr() );
$is_multiple = empty( $field['multiple'] ) ? false : true;
$field_id = empty( $field['id'] ) ? $field['name'] : $field['id'];
$field['id'] = $field_id;
if ( $is_multiple ) {
$field['name'] = "{$field['name']}[]";
$field['custom_attr'] = array_merge( [ 'multiple' => 'multiple' ], $field['custom_attr'] );
}
$result[] = $field;
}
return $result;
}
/**
* Register the metabox
*
* call the wp function add_metabox to add the metabox
*
*
* @return void
*/
public function register_metabox( $post_type ) {
if ( in_array( $post_type, (array) $this->options['screen'] ) ) {
add_meta_box( $this->id, $this->options['title'], array(
$this,
'show_metabox'
), $post_type, $this->options['context'], $this->options['priority'] );
}
}
/**
* Save Post Data
*
* Save the post data in the database when save the post
*
* @param $post_id
*
* @return int
*/
public function save_postdata( $post_id ) {
if ( ! isset( $_POST['pluginever_metabox_nonce'] ) || ! wp_verify_nonce( $_POST['pluginever_metabox_nonce'], 'pluginever_fields_nonce' ) ) {
return false;
}
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
return false;
}
if ( ! isset( $_POST['post_type'] ) ) {
return false;
}
foreach ( $this->fields as $key => $field ) {
if ( in_array( $field['type'], array( 'title' ) ) ) {
continue;
}
$value = isset( $_POST[ $field['id'] ] ) ? self::trim_this( $_POST[ $field['id'] ] ) : '';
if ( isset( $field['sanitize'] ) ) {
$function = sanitize_key( $field['sanitize'] );
$value = self::sanitize_this( $function, $value );
}
if ( $field['type'] == 'checkbox' ) {
update_post_meta( $post_id, $field['id'], $value );
} else if ( $value !== '' ) {
update_post_meta( $post_id, $field['id'], $value );
} else {
delete_post_meta( $post_id, $field['id'] );
}
}
}
/**
* @param $post
*/
public function show_metabox( $post ) {
global $post_id;
$post_id = $post->ID;
ob_start();
$lazy_loading = $this->options['lazy_loading'] == 'true' ? 'plvr-lazy-loading loading' : 'loaded';
echo '<div class="plvr-framework ' . $lazy_loading . '">';
echo '<div class="container">';
echo wp_nonce_field( 'pluginever_fields_nonce', 'pluginever_metabox_nonce' );
do_action( 'plvr_framework_before_metabox-' . $this->id, $post, $this->id );
do_action( 'plvr_framework_before_metabox', $post, $this->id );
foreach ( $this->fields as $field ) {
$class = [];
$wrapper_class = [];
$attributes = '';
if ( ! empty( $field['condition'] ) ) {
$default = array(
'depend_on' => '',
'depend_value' => '',
'depend_cond' => '', // ==, !=, <=, <, >=, > available conditions
);
$conditions = wp_parse_args( $field['condition'], $default );
$wrapper_class[] = 'plvr-conditional';
$attributes = " data-cond-option='{$conditions['depend_on']}' data-cond-value='{$conditions['depend_value']}' data-cond-operator='{$conditions['depend_cond']}' ";
}
//if any special wrapper required around field
$wrapper_class[] = ! empty( $field['wrapper_class'] ) ? sanitize_key( $field['wrapper_class'] ) : '';
$tooltip = ! empty( $field['tooltip'] ) ? strip_tags( $field['tooltip'] ) : false;
if ( $tooltip ) {
$class[] = "plvr-has-tooltip";
}
?>
<div
class="row plvr-form-field <?php echo implode( ' ', $wrapper_class ); ?>" <?php echo $attributes; ?>>
<?php if ( '' !== $field['label'] ): ?>
<div class="col-4">
<label for="<?php echo $field['name']; ?>"><?php echo $field['label'] ?></label>
</div>
<?php endif; ?>
<div class="col <?php echo implode( ' ', $class ); ?>">
<?php if ( $tooltip ) : ?>
<span class="dashicons dashicons-editor-help plvr-tooltip"
title="<?php echo $tooltip; ?>"></span>
<?php endif; ?>
<?php self::add_field( $post_id, $field ); ?>
</div>
</div>
<?php
$output = ob_get_contents();
ob_get_clean();
echo $output;
}
do_action( 'plvr_framework_after_metabox-' . $this->id, $post, $this->id );
do_action( 'plvr_framework_after_metabox', $post, $this->id );
echo '</div>';
echo '</div>';
}
/**
* Build a input fields
*
* @since 1.0.0
*
* @param $post_id
* @param $field
*/
public static function add_field( $post_id, $field ) {
if ( empty( $field['parsed'] ) ) {
$field = wp_parse_args( $field, self::get_default_field_attr() );
}
$field_attributes = array_merge( array(
'name' => $field['name'],
'id' => $field['id'],
'class' => $field['class'],
'placeholder' => $field['placeholder'],
), $field['custom_attr'] );
if ( $field['required'] ) {
$field_attributes['required'] = 'required';
}
$value = get_post_meta( $post_id, $field['id'], true );
$saved_value = self::trim_this( $value ) == '' ? $field['value'] : $value;
//color picker
if ( $field['type'] == 'colorpicker' ) {
$field_attributes['class'] .= ' color-picker';
if ( $field['rgba'] == 'true' ) {
$field_attributes['data-alpha'] = 'true';
}
}
if ( $field['select2'] == 'true' ) {
$field_attributes['class'] .= ' plvr-select2';
}
$custom_attributes = self::get_custom_attribute( $field_attributes );
switch ( $field['type'] ) {
case 'text':
case 'email':
case 'number':
case 'hidden':
case 'url':
echo '<input type="' . $field['type'] . '" value="' . esc_attr( $saved_value ) . '" ' . implode( ' ', $custom_attributes ) . ' />';
break;
case 'colorpicker':
echo '<input type="' . $field['type'] . '" value="' . esc_attr( $saved_value ) . '" ' . implode( ' ', $custom_attributes ) . ' />';
break;
case 'select':
if ( $field['options'] ) {
echo '<select ' . implode( ' ', $custom_attributes ) . '>';
foreach ( $field['options'] as $key => $value ) {
$saved_value = (array) $saved_value;
$selected = in_array( $key, $saved_value ) ? " selected='selected' " : '';
printf( "<option value='%s' %s>%s</option>\n", $key, $selected, $value );
}
echo '</select>';
}
break;
case 'textarea':
echo '<textarea ' . implode( ' ', $custom_attributes ) . '>' . esc_textarea( $saved_value ) . '</textarea>';
break;
case 'wysiwyg':
$default = [
'wpautop' => true,
'media_buttons' => true,
'default_editor' => '',
'drag_drop_upload' => false,
'textarea_name' => '',
'textarea_rows' => 20,
'tabindex' => '',
'tabfocus_elements' => ':prev,:next',
'editor_css' => '',
'editor_class' => '',
'teeny' => false,
'dfw' => false,
'_content_editor_dfw' => false,
'tinymce' => true,
'quicktags' => true
];
$settings = wp_parse_args( $field['settings'], $default );
wp_editor( $saved_value, $field_attributes['name'], $settings );
break;
case 'checkbox':
echo '<span class="checkbox">';
echo '<label for="' . esc_attr( $field_attributes['id'] ) . '">';
echo '<input type="checkbox" ' . checked( $saved_value, '1', false ) . ' value="1" ' . implode( ' ', $custom_attributes ) . ' />';
echo wp_kses_post( $field['title'] );
echo wp_kses_post( $field['help'] );
echo '</label>';
echo '</span>';
break;
case 'radio':
if ( $field['options'] ) {
foreach ( $field['options'] as $key => $value ) {
echo '<div class="checkbox">';
echo '<label><input type="radio" ' . checked( $saved_value, $key, false ) . ' value="' . $key . '" ' . implode( ' ', $custom_attributes ) . ' />' . $value . ' </label>';
echo '</div>';
}
}
break;
case 'date':
echo '<input type="date" format="dd/mm/yyyy" name="' . esc_attr( $field_attributes['id'] ) . '" id="' . esc_attr( $field_attributes['id'] ) . '" value="' . esc_attr( $saved_value ) . '" ' . implode( ' ', $custom_attributes ) . '>';
break;
default:
# code...
break;
}
if ( ! empty( $field['help'] ) ) {
echo '<span class="help">' . $field['help'] . '</span>';
}
}
/**
* Handles an elements custom attribute
*
* @since 1.0.0
*
* @param array $attr as key/value pair
* @param array $other_attr as key/value pair
*
* @return array
*/
protected static function get_custom_attribute( $attr = array(), $other_attr = array() ) {
$custom_attributes = array();
if ( ! empty( $attr ) && is_array( $attr ) ) {
foreach ( $attr as $attribute => $value ) {
if ( $value != '' ) {
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
}
}
}
if ( ! empty( $other_attr ) && is_array( $other_attr ) ) {
foreach ( $attr as $attribute => $value ) {
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
}
}
return $custom_attributes;
}
/**
* Trim data based on type
*
* @since 1.2.9
*
* @param $data
*
* @return array|string
*/
protected static function trim_this( $data ) {
if ( is_array( $data ) ) {
return array_map( 'trim', $data );
}
return trim( $data );
}
/**
* Sanitize value
*
* @param $function
* @param $value
*
* @return array|mixed
*/
protected static function sanitize_this( $function, $value ) {
if ( is_callable( $function ) ) {
if ( is_array( $value ) ) {
return array_map( $function, $value );
}
return call_user_func( $function, $value );
}
return $value;
}
}
endif;