PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 1.8
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v1.8
2.3.2 2.3.1 2.3.0 2.2.8 2.2.7 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.11.0 1.11.1 1.11.2 1.6 1.7 1.8 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 1.9.10 1.9.11 All 60 releases
merchant / inc / classes / class-merchant-metabox.php

class-merchant-metabox.php in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 1.8, at inc/classes/class-merchant-metabox.php

1,082 lines 39.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Metabox.
5 *
6 * @package Merchant
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly
11 }
12
13 /**
14 * Metabox class.
15 *
16 */
17 if ( ! class_exists( 'Merchant_Metabox' ) ) {
18 class Merchant_Metabox {
19
20 private static $initialized = null;
21
22 /**
23 * Options.
24 *
25 */
26 public static $options = array();
27
28 /**
29 * Constructor.
30 *
31 */
32 public function __construct() {
33 if ( ! is_null( self::$initialized ) ) {
34 return;
35 }
36
37 self::$initialized = true;
38
39 add_action( 'load-post.php', array( $this, 'init_metabox' ) );
40 add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
41 add_action( 'wp_ajax_merchant_select_ajax', array( $this, 'select_content_ajax' ) );
42 }
43
44 /**
45 * Init metabox.
46 *
47 */
48 public function init_metabox() {
49 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_metabox_scripts' ) );
50 add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
51 add_action( 'save_post', array( $this, 'save_metabox' ) );
52 }
53
54 /**
55 * Enqueue scripts.
56 *
57 */
58 public function enqueue_metabox_scripts() {
59 wp_enqueue_code_editor(
60 array(
61 'type' => 'text/html',
62 'codemirror' => array(
63 'indentUnit' => 2,
64 'tabSize' => 2
65 ),
66 ) );
67
68 wp_enqueue_script( 'merchant-select2', MERCHANT_URI . 'assets/vendor/select2/select2.full.min.js', array( 'jquery' ), '4.0.13', true );
69 wp_enqueue_style( 'merchant-select2', MERCHANT_URI . 'assets/vendor/select2/select2.min.css', array(), '4.0.13', 'all' );
70
71 wp_enqueue_style( 'merchant-metabox-styles', MERCHANT_URI . 'assets/css/admin/metabox.min.css', array(), MERCHANT_VERSION );
72 wp_enqueue_script( 'merchant-metabox-scripts', MERCHANT_URI . 'assets/js/admin/merchant-metabox.min.js', array( 'jquery', 'jquery-ui-sortable' ), MERCHANT_VERSION, true );
73
74 wp_localize_script( 'merchant-metabox-scripts', 'merchant_metabox', array(
75 'ajaxurl' => admin_url( 'admin-ajax.php' ),
76 'ajaxnonce' => wp_create_nonce( 'merchant_metabox' ),
77 ) );
78 }
79
80 /**
81 * Select content ajax callback.
82 *
83 */
84 public function select_content_ajax() {
85 $term = ( isset( $_GET['term'] ) ) ? sanitize_text_field( wp_unslash( $_GET['term'] ) ) : '';
86 $nonce = ( isset( $_GET['nonce'] ) ) ? sanitize_text_field( wp_unslash( $_GET['nonce'] ) ) : '';
87 $source = ( isset( $_GET['source'] ) ) ? sanitize_text_field( wp_unslash( $_GET['source'] ) ) : '';
88
89 // Check current user capabilities
90 if ( ! current_user_can( 'manage_options' ) ) {
91 wp_send_json_error( 'You are not allowed to do this.' );
92 }
93
94 if ( ! empty( $term ) && ! empty( $source ) && ! empty( $nonce ) && wp_verify_nonce( $nonce, 'merchant_metabox' ) ) {
95 $options = array();
96
97 switch ( $source ) {
98 case 'post':
99 case 'product':
100 $query = new WP_Query( array(
101 's' => $term,
102 'post_type' => $source,
103 'post_status' => 'publish',
104 'posts_per_page' => 25,
105 'order' => 'DESC',
106 ) );
107
108 if ( ! empty( $query->posts ) ) {
109 foreach ( $query->posts as $post ) {
110 $options[] = array(
111 'id' => $post->ID,
112 'text' => $post->post_title,
113 );
114 }
115 }
116
117 break;
118 }
119
120 wp_send_json_success( $options );
121 } else {
122 wp_send_json_error();
123 }
124 }
125
126 /**
127 * Options.
128 *
129 */
130 public function metabox_options() {
131 /**
132 * Hook: merchant_metabox_options
133 *
134 * @since 1.0
135 */
136 do_action( 'merchant_metabox_options', self::$options );
137
138 /**
139 * Hook: merchant_metabox_options_filter
140 *
141 * @since 1.0
142 */
143 self::$options = apply_filters( 'merchant_metabox_options_filter', self::$options );
144
145 // Set priority order
146 self::$options = wp_list_sort( self::$options, array( 'priority' => 'ASC' ), 'ASC', true );
147
148 foreach ( self::$options as $key => $value ) {
149 self::$options[ $key ]['fields'] = wp_list_sort( $value['fields'], array( 'priority' => 'ASC' ), 'ASC', true );
150 }
151
152 return self::$options;
153 }
154
155 /**
156 * Add section.
157 *
158 */
159 public function add_section( $id, $args ) {
160 if ( ! empty( $args['post_type'] ) && ! in_array( get_post_type(), $args['post_type'] ) ) {
161 return;
162 }
163
164 if ( ! empty( $args['exclude'] ) && in_array( get_post_type(), $args['exclude'] ) ) {
165 return;
166 }
167
168 $args = wp_parse_args( $args, array(
169 'title' => '',
170 'fields' => array(),
171 'priority' => ( count( self::$options ) + 1 ) * 10,
172 ) );
173
174 self::$options[ $id ] = $args;
175 }
176
177 /**
178 * Add field.
179 *
180 */
181 public function add_field( $id, $args ) {
182 if ( ( ! empty( $args['post_type'] ) && ! in_array( get_post_type(), $args['post_type'] ) ) || empty( self::$options[ $args['section'] ] ) ) {
183 return;
184 }
185
186 $args = wp_parse_args( $args, array(
187 'priority' => ( count( self::$options[ $args['section'] ]['fields'] ) + 1 ) * 10,
188 ) );
189
190 self::$options[ $args['section'] ]['fields'][ $id ] = $args;
191 }
192
193 /**
194 * Add metabox.
195 *
196 */
197 public function add_metabox( $post_type ) {
198 global $post;
199
200 // Do not render the metabox on attachment post type.
201 if ( 'attachment' === $post_type ) {
202 return;
203 }
204
205 $types = get_post_types( array(
206 'public' => true,
207 ) );
208
209 if ( ! in_array( $post_type, $types ) ) {
210 return;
211 }
212
213 $metabox_title = esc_html__( 'Merchant Options', 'merchant' );
214 switch ( $post_type ) {
215 case 'post':
216 $metabox_title = esc_html__( 'Merchant Post Options', 'merchant' );
217 break;
218
219 case 'page':
220 $metabox_title = esc_html__( 'Merchant Page Options', 'merchant' );
221 break;
222
223 case 'product':
224 $metabox_title = esc_html__( 'Merchant Product Options', 'merchant' );
225 break;
226 }
227
228 // Botiga theme compatibility:
229 // Do not render the metabox in the Botiga templates builder.
230 if ( class_exists( 'Botiga_Modules' ) && Botiga_Modules::is_module_active( 'templates' ) ) {
231 unset( $types['athemes_hf'] );
232 }
233
234 /**
235 * Hook: merchant_metabox_title
236 *
237 * @since 1.0
238 */
239 $metabox_title = apply_filters( 'merchant_metabox_title', $metabox_title, $post_type );
240
241 add_meta_box( 'merchant_metabox', $metabox_title, array( $this, 'render_metabox_content' ), $types, 'normal', 'low' );
242 }
243
244 /**
245 * Metabox content.
246 *
247 */
248 public function render_metabox_content( $post ) {
249 $options = $this->metabox_options();
250 $post_type = get_post_type( $post );
251
252 wp_nonce_field( 'merchant_metabox', 'merchant_metabox_nonce' );
253
254 echo '<div class="merchant-metabox">';
255 $has_tabs = ( ! empty( array_filter( array_column( $options, 'title' ) ) ) ) ? true : false;
256
257 if ( ! empty( $has_tabs ) ) {
258 echo '<div class="merchant-metabox-tabs">';
259
260 $num = 0;
261 foreach ( $options as $option ) {
262 if ( ! empty( $option['title'] ) ) {
263 $active = ( 0 === $num ) ? ' active' : '';
264 echo '<a href="#" class="merchant-metabox-tab' . esc_attr( $active ) . '">' . esc_html( $option['title'] ) . '</a>';
265
266 $num ++;
267 }
268 }
269
270 echo '</div>';
271 }
272
273 echo '<div class="merchant-metabox-contents">';
274
275 $num = 0;
276 foreach ( $options as $option ) {
277 $active = ( 0 === $num ) ? ' active' : '';
278 echo '<div class="merchant-metabox-content' . esc_attr( $active ) . '">';
279
280 if ( ! empty( $option['title'] ) ) {
281 echo '<h4 class="merchant-metabox-content-title">' . esc_html( $option['title'] ) . '</h4>';
282 }
283
284 if ( ! empty( $option['fields'] ) ) {
285 foreach ( $option['fields'] as $field_id => $field ) {
286 $separator = ( ! empty( $field['separator'] ) ) ? $field['separator'] : 'after';
287 $classes = array();
288 $classes[] = 'merchant-metabox-field';
289 $classes[] = 'merchant-metabox-field-separator-' . $separator;
290 $classes[] = 'merchant-metabox-field-' . $field['type'];
291
292 if ( ! empty( $field['class'] ) ) {
293 $classes[] = $field['class'];
294 }
295
296 if ( ! empty( $field['inline'] ) ) {
297 $classes[] = 'merchant-metabox-field-inline';
298 }
299
300 if ( ! empty( $field['depend'] ) ) {
301 $depend_meta = get_post_meta( $post->ID, $field['depend'], true );
302
303 if ( empty( $depend_meta ) ) {
304 $classes[] = 'merchant-metabox-field-hidden';
305 }
306
307 echo '<div class="' . esc_attr( join( ' ', $classes ) ) . '" data-depend-on="' . esc_attr( $field['depend'] ) . '">';
308 } else {
309 echo '<div class="' . esc_attr( join( ' ', $classes ) ) . '">';
310 }
311
312 if ( isset( $field['title'] ) || isset( $field['subtitle'] ) ) {
313 echo '<div class="merchant-metabox-field-title">';
314
315 if ( ! empty( $field['title'] ) ) {
316 echo '<h4>' . wp_kses_post( $field['title'] ) . '</h4>';
317 }
318
319 if ( ! empty( $field['subtitle'] ) ) {
320 echo '<small class="merchant-metabox-field-subtitle">' . wp_kses_post( $field['subtitle'] ) . '</small>';
321 }
322
323 echo '</div>';
324 }
325
326 echo '<div class="merchant-metabox-field-content">';
327
328 $meta = get_post_meta( $post->ID, $field_id );
329 $default = ( isset( $field['default'] ) ) ? $field['default'] : null;
330 $value = ( isset( $meta[0] ) ) ? $meta[0] : $default;
331
332 $this->get_field( $field_id, $field, $value );
333
334 if ( ! empty( $field['desc'] ) ) {
335 echo '<div class="merchant-metabox-field-description">' . wp_kses_post( $field['desc'] ) . '</div>';
336 }
337
338 echo '</div>';
339
340 echo '</div>';
341 }
342 }
343
344 echo '</div>';
345
346 $num ++;
347 }
348
349 echo '</div>';
350 echo '</div>';
351 }
352
353 /**
354 * Save metabox.
355 *
356 */
357 public function save_metabox( $post_id ) {
358 if ( ! isset( $_POST['merchant_metabox_nonce'] ) ) {
359 return $post_id;
360 }
361
362 $nonce = sanitize_key( wp_unslash( $_POST['merchant_metabox_nonce'] ) );
363 if ( ! wp_verify_nonce( $nonce, 'merchant_metabox' ) ) {
364 return $post_id;
365 }
366
367 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
368 return $post_id;
369 }
370
371 if ( ! current_user_can( 'edit_page', $post_id ) ) {
372 return $post_id;
373 }
374
375 $options = $this->metabox_options();
376 if ( empty( $options ) ) {
377 return $post_id;
378 }
379
380 foreach ( $options as $option ) {
381 if ( ! empty( $option['fields'] ) ) {
382 foreach ( $option['fields'] as $field_id => $field ) {
383 if ( in_array( $field['type'], array( 'content' ) ) ) {
384 continue;
385 }
386
387 $value = $this->sanitize( $field, $field_id, $_POST );
388
389 update_post_meta( $post_id, $field_id, $value );
390 }
391 }
392 }
393 }
394
395 /**
396 * Sanitize.
397 *
398 */
399 public function sanitize( $field, $field_id, $post_data ) {
400 $value = isset( $post_data[ $field_id ] ) ? wp_unslash( $post_data[ $field_id ] ) : null;
401
402 switch ( $field['type'] ) {
403 case 'text':
404 case 'coupons':
405 case 'sidebar-select':
406 case 'size-chart-select':
407 return sanitize_text_field( $value );
408 break;
409
410 case 'textarea':
411 return sanitize_textarea_field( $value );
412 break;
413
414 case 'checkbox':
415 case 'switcher':
416 return ( '1' === $value ) ? 1 : 0;
417 break;
418
419 case 'number':
420 return absint( $value );
421 break;
422
423 case 'select':
424 case 'choices':
425 return ( in_array( $value, array_keys( $field['options'] ) ) ) ? sanitize_key( $value ) : '';
426 break;
427
428 case 'wc-attributes':
429 case 'select-ajax':
430 return ( is_array( $value ) && ! empty( $value ) ) ? array_filter( array_map( 'sanitize_text_field', $value ) ) : array();
431 break;
432
433 case 'wc-attributes':
434 return ( is_array( $value ) && ! empty( $value ) ) ? array_filter( array_map( 'sanitize_text_field', $value ) ) : array();
435 break;
436
437 case 'repeater':
438 case 'uploads':
439 return ( is_array( $value ) && ! empty( $value ) ) ? array_filter( map_deep( $value, 'sanitize_text_field' ) ) : array();
440 break;
441
442 case 'size-chart':
443 case 'uploads':
444 return ( is_array( $value ) && ! empty( $value ) ) ? array_filter( map_deep( $value, 'sanitize_text_field' ) ) : array();
445 break;
446
447 case 'wp-editor':
448 return wp_kses_post( $value );
449 break;
450
451 case 'flexible-content':
452 return is_array( $value ) && ! empty( $value ) ? array_map( function ( $sub_fields ) use ( $field ) {
453 foreach ( $sub_fields as $sub_field => $value ) {
454 $sub_fields[ $sub_field ] = $this->sanitize( $field['layouts'][ $sub_fields['type'] ]['fields'][ $sub_field ], $sub_field, $sub_fields );
455 }
456
457 return $sub_fields;
458 }, $value ) : array();
459 break;
460 }
461
462 return $value;
463 }
464
465 /**
466 * Get field.
467 *
468 */
469 public function get_field( $field_id, $field, $value ) {
470 switch ( $field['type'] ) {
471 case 'text':
472 if ( isset( $field['append'] ) || isset( $field['prepend'] ) ) {
473 echo '<div class="merchant-metabox-field-input-container">';
474 if ( isset( $field['prepend'] ) ) {
475 echo '<div class="merchant-metabox-field-prepend">' . esc_attr( $field['prepend'] ) . '</div>';
476 }
477 echo '<input type="text" name="' . esc_attr( $field_id ) . '" value="' . esc_attr( $value ) . '" />';
478 if ( isset( $field['append'] ) ) {
479 echo '<div class="merchant-metabox-field-append">' . esc_attr( $field['append'] ) . '</div>';
480 }
481 echo '</div>';
482 } else {
483 echo '<input type="text" name="' . esc_attr( $field_id ) . '" value="' . esc_attr( $value ) . '" />';
484 }
485 break;
486
487 case 'number':
488 $style = '';
489 $step = 'any';
490 if ( isset( $field['style'] ) && ! empty( $field['style'] ) ) {
491 $style = 'style="' . esc_attr( str_replace( [ '&', '=' ], [ '; ', ': ' ], http_build_query( $field['style'] ) ) ) . '"';
492 }
493 if ( isset( $field['step'] ) && ! empty( $field['step'] ) ) {
494 $step = $field['step'];
495 }
496 if ( isset( $field['append'] ) || isset( $field['prepend'] ) ) {
497 echo '<div class="merchant-metabox-field-input-container">';
498 if ( isset( $field['prepend'] ) ) {
499 echo '<div class="merchant-metabox-field-prepend">' . esc_attr( $field['prepend'] ) . '</div>';
500 }
501 echo '<input step="' . esc_attr( $step ) . '" type="number" name="' . esc_attr( $field_id ) . '" value="' . esc_attr( $value ) . '" ' . wp_kses_post( $style ) . ' />';
502 if ( isset( $field['append'] ) ) {
503 echo '<div class="merchant-metabox-field-append">' . esc_attr( $field['append'] ) . '</div>';
504 }
505 echo '</div>';
506 } else {
507 echo '<input step="' . esc_attr( $step ) . '" type="number" '.' name="' . esc_attr( $field_id ) . '" value="' . esc_attr( $value ) . '" ' . wp_kses_post( $style ) . ' />';
508 }
509 break;
510
511 case 'textarea':
512 echo '<textarea name="' . esc_attr( $field_id ) . '">' . esc_textarea( $value ) . '</textarea>';
513 break;
514
515 case 'checkbox':
516 case 'switcher':
517 $field = wp_parse_args( $field, array(
518 'label' => '',
519 ) );
520
521 echo '<label>';
522 echo '<input type="checkbox" name="' . esc_attr( $field_id ) . '" value="1"' . checked( $value, true, false ) . ' />';
523
524 if ( 'switcher' === $field['type'] ) {
525 echo '<i></i>';
526 }
527
528 if ( ! empty( $field['label'] ) ) {
529 echo '<span>' . esc_html( $field['label'] ) . '</span>';
530 }
531 echo '</label>';
532 break;
533
534 case 'select':
535 echo '<select name="' . esc_attr( $field_id ) . '">';
536
537 foreach ( $field['options'] as $key => $option ) {
538 echo '<option value="' . esc_attr( $key ) . '"' . selected( $key, $value, false ) . '>' . esc_html( $option ) . '</option>';
539 }
540
541 echo '</select>';
542 break;
543
544 case 'select-ajax':
545 $field = wp_parse_args( $field, array(
546 'source' => 'post',
547 ) );
548 $ids = ( is_array( $value ) && ! empty( $value ) ) ? $value : (array) $value;
549 if ( isset( $field['multiple'] ) ) {
550 $multiple = $field['multiple'] === true ? 'multiple' : '';
551 } else {
552 $multiple = 'multiple';
553 }
554
555 echo '<select name="' . esc_attr( $field_id ) . '[]" ' . esc_attr( $multiple ) . ' data-source="' . esc_attr( $field['source'] ) . '">';
556
557 if ( ! empty( $ids ) ) {
558 foreach ( $ids as $id ) {
559 switch ( $field['source'] ) {
560 case 'post':
561 case 'product':
562 $post = get_post( $id );
563
564 if ( ! empty( $post ) ) {
565 echo '<option value="' . esc_attr( $post->ID ) . '" selected>' . esc_html( $post->post_title ) . '</option>';
566 }
567 break;
568 }
569 }
570 }
571
572 echo '</select>';
573 break;
574
575 case 'wc-attributes':
576 $attributes = wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_id' );
577 $values = ( is_array( $value ) && ! empty( $value ) ) ? $value : array();
578
579 if ( ! empty( $attributes ) ) {
580 echo '<div class="merchant-metabox-field-attributes">';
581 echo '<ul class="merchant-sortable">';
582
583 $selected_attributes = array();
584 foreach ( $values as $id ) {
585 if ( isset( $attributes[ $id ] ) ) {
586 $selected_attributes[ $id ] = $attributes[ $id ];
587 unset( $attributes[ $id ] );
588 }
589 }
590
591 $attributes = array_replace( $selected_attributes, $attributes );
592 foreach ( $attributes as $attribute_id => $attribute_label ) {
593 $checked = ( in_array( $attribute_id, $values ) ) ? ' checked' : '';
594
595 echo '<li class="merchant-sortable-item">';
596 echo '<label>';
597 echo '<input type="checkbox" name="' . esc_attr( $field_id ) . '[]" value="' . esc_attr( $attribute_id ) . '"' . esc_attr( $checked ) . ' />';
598 echo '<span>' . esc_html( $attribute_label ) . '</span>';
599 echo '</label>';
600 echo '<span class="merchant-sortable-move dashicons dashicons-menu"></span>';
601 echo '</li>';
602 }
603
604 echo '</ul>';
605 echo '</div>';
606 }
607
608 break;
609
610 case 'choices':
611 echo '<div class="merchant-metabox-field-choices-images">';
612
613 foreach ( $field['options'] as $key => $option ) {
614 echo '<label>';
615 echo '<input type="radio" name="' . esc_attr( $field_id ) . '" value="' . esc_attr( $key ) . '"' . checked( $value, $key, false ) . ' />';
616 echo '<figure><img src="' . esc_url( sprintf( $option['image'],
617 MERCHANT_URI ) ) . '" title="' . esc_attr( $option['label'] ) . '" alt="' . esc_attr( $option['label'] ) . '" /></figure>';
618 echo '</label>';
619 }
620
621 echo '</div>';
622 break;
623
624 case 'content':
625 echo wp_kses_post( $field['content'] );
626 break;
627
628 case 'repeater':
629 $field = wp_parse_args( $field, array(
630 'button' => '',
631 ) );
632
633 echo '<div class="merchant-metabox-field-repeater-content" data-id="' . esc_attr( $field_id ) . '">';
634 $values = ( is_array( $value ) && ! empty( $value ) ) ? $value : array();
635
636 echo '<ul class="merchant-metabox-field-repeater-list">';
637 echo '<li class="merchant-metabox-field-repeater-list-item hidden">';
638 if ( isset( $field['fields'] ) ) {
639 echo '<div class="merchant-metabox-field-repeater-list-item-fields">';
640 foreach ( $field['fields'] as $sub_field_id => $sub_field ) {
641 echo '<div class="merchant-metabox-field-repeater-list-item-field">';
642 if ( isset( $sub_field['title'] ) ) {
643 echo '<span class="merchant-metabox-field-repeater-list-item-field-title">' . esc_html( $sub_field['title'] ) . '</span>';
644 }
645 echo '<div class="merchant-metabox-field-repeater-list-item-field-input" data-id="' . esc_attr( $sub_field_id ) . '">';
646 if ( $sub_field['type'] === 'text' ) {
647 echo '<input type="text" name="" value="" data-name="' . esc_attr( $field_id ) . '[0][' . esc_attr( $sub_field_id ) . ']" />';
648 }
649 if ( $sub_field['type'] === 'number' ) {
650 echo '<input type="number" name="" value="" data-name="' . esc_attr( $field_id ) . '[0][' . esc_attr( $sub_field_id ) . ']" />';
651 }
652 echo '</div>';
653 echo '</div>';
654 }
655 echo '</div>';
656 } else {
657 echo '<input type="text" name="" value="" data-name="' . esc_attr( $field_id ) . '[]" />';
658 }
659 echo '<span class="merchant-metabox-field-repeater-move dashicons dashicons-menu"></span>';
660 echo '<span class="merchant-metabox-field-repeater-remove dashicons dashicons-trash"></span>';
661 echo '</li>';
662
663 foreach ( $values as $key => $value ) {
664 echo '<li class="merchant-metabox-field-repeater-list-item">';
665 if ( isset( $field['fields'] ) ) {
666 echo '<div class="merchant-metabox-field-repeater-list-item-fields">';
667 foreach ( $field['fields'] as $sub_field_id => $sub_field ) {
668 echo '<div class="merchant-metabox-field-repeater-list-item-field">';
669 if ( isset( $sub_field['title'] ) ) {
670 echo '<span class="merchant-metabox-field-repeater-list-item-field-title">' . esc_attr( $sub_field['title'] ) . '</span>';
671 }
672 echo '<div class="merchant-metabox-field-repeater-list-item-field-input" data-id="' . esc_attr( $sub_field_id ) . '">';
673 if ( $sub_field['type'] === 'text' ) {
674 echo '<input type="text" name="' . esc_attr( $field_id ) . '[' . esc_attr( $key ) . '][' . esc_attr( $sub_field_id ) . ']" value="' . esc_attr( $value[ $sub_field_id ] ) . '" />';
675 }
676 if ( $sub_field['type'] === 'number' ) {
677 echo '<input type="number" name="' . esc_attr( $field_id ) . '[' . esc_attr( $key ) . '][' . esc_attr( $sub_field_id ) . ']" value="' . esc_attr( $value[ $sub_field_id ] ) . '" />';
678 }
679 echo '</div>';
680 echo '</div>';
681 }
682 echo '</div>';
683 } else {
684 echo '<input type="text" name="' . esc_attr( $field_id ) . '[]" value="' . esc_attr( $value ) . '" />';
685 }
686
687 echo '<span class="merchant-metabox-field-repeater-move dashicons dashicons-menu"></span>';
688 echo '<span class="merchant-metabox-field-repeater-remove dashicons dashicons-trash"></span>';
689 echo '</li>';
690 }
691 echo '</ul>';
692 echo '<button class="merchant-metabox-field-repeater-add button button-primary">' . esc_html( $field['button'] ) . '</button>';
693 echo '</div>';
694
695 break;
696
697 case 'media':
698 $placeholder = class_exists( 'Woocommerce' ) ? wc_placeholder_img_src( 'thumbnail' ) : MERCHANT_URI . '/assets/placeholder.svg';
699 $hidden_class = ( empty( $value ) ) ? ' hidden' : '';
700
701 if ( ! empty( $value ) ) {
702 $attachment = wp_get_attachment_image_src( $value, 'thumbnail' );
703 $thumbnail = ( is_array( $attachment ) && ! empty( $attachment[0] ) ) ? $attachment[0] : $placeholder;
704 } else {
705 $thumbnail = $placeholder;
706 }
707
708 echo '<div class="merchant-metabox-field-media-content">';
709 echo '<figure class="merchant-metabox-field-media-preview">';
710 echo '<img src="' . esc_url( $thumbnail ) . '" data-placeholder="' . esc_url( $placeholder ) . '" />';
711 echo '</figure>';
712
713 echo '<div class="merchant-metabox-field-media-button">';
714 echo '<a href="#" class="merchant-metabox-field-media-upload button">' . esc_html__( 'Upload/Add Image', 'merchant' ) . '</a>';
715 echo '<a href="#" class="merchant-metabox-field-media-remove merchant-button-remove button' . esc_attr( $hidden_class ) . '">' . esc_html__( 'Remove Image', 'merchant' ) . '</a>';
716 echo '<input type="hidden" name="' . esc_attr( $field_id ) . '" value="' . esc_attr( $value ) . '" class="merchant-metabox-field-media-input" />';
717 echo '</div>';
718 echo '</div>';
719
720 break;
721
722 case 'uploads':
723 $field = wp_parse_args( $field, array(
724 'button' => '',
725 'library' => 'image',
726 ) );
727
728 $enable_thumb = isset( $field['enable_thumb'] ) && $field['enable_thumb'];
729
730 echo '<div class="merchant-metabox-field-uploads-content">';
731 $values = ( is_array( $value ) && ! empty( $value ) ) ? $value : array();
732 $name = 'video' === $field['library'] ? $field_id . '[0][src]' : $field_id . '[]';
733 $thumb_name = $field_id . '[0][thumb]';
734
735 echo '<ul class="merchant-metabox-field-uploads-list" data-library="' . esc_attr( $field['library'] ) . '">';
736 echo '<li class="merchant-metabox-field-uploads-list-item hidden">';
737
738 if ( 'video' === $field['library'] || $enable_thumb ) {
739 echo '<div class="merchant-metabox-field-uploads-thumbnail">';
740 echo '<a href="#" class="merchant-metabox-field-uploads-thumbnail-remove dashicons dashicons-dismiss" style="display:none"></a>';
741 echo '<a href="#" class="merchant-metabox-field-uploads-thumbnail-upload"><span>+</span></a>';
742 echo '<input type="hidden" name="" value="" data-name="' . esc_attr( $thumb_name ) . '" />';
743 echo '</div>';
744 }
745
746 echo '<input type="text" name="" value="" data-name="' . esc_attr( $name ) . '" />';
747 echo '<button class="merchant-metabox-field-uploads-upload button">' . esc_html__( 'Upload', 'merchant' ) . '</button>';
748 echo '<span class="merchant-metabox-field-uploads-move dashicons dashicons-menu"></span>';
749 echo '<span class="merchant-metabox-field-uploads-remove dashicons dashicons-trash"></span>';
750 echo '</li>';
751
752 foreach ( $values as $key => $value ) {
753 $item_name = 'video' === $field['library'] || $enable_thumb ? str_replace( '0', $key, $name ) : $name;
754 $item_value = is_array( $value ) ? ( isset( $value['src'] ) ? $value['src'] : '' ) : $value;
755
756 echo '<li class="merchant-metabox-field-uploads-list-item">';
757 if ( 'video' === $field['library'] || $enable_thumb ) {
758 $item_thumb = is_array( $value ) && isset( $value['thumb'] ) ? $value['thumb'] : '';
759 $item_thumb_name = str_replace( '0', $key, $thumb_name );
760
761 echo '<div class="merchant-metabox-field-uploads-thumbnail">';
762 echo '<a href="#" class="merchant-metabox-field-uploads-thumbnail-remove dashicons dashicons-dismiss" ' . ( empty( $item_thumb ) ? 'style="display: none"' : '' ) . '></a>';
763 echo '<a href="#" class="merchant-metabox-field-uploads-thumbnail-upload">';
764 echo empty( $item_thumb )
765 ? '<span>+</span>'
766 : '<img src="' . esc_url( wp_get_attachment_thumb_url( $item_thumb ) ) . '" /><span style="display: none">+</span>';
767 echo '</a>';
768 echo '<input type="hidden" name="' . esc_attr( $item_thumb_name ) . '" value="' . absint( $item_thumb ) . '" />';
769 echo '</div>';
770 }
771 echo '<input type="text" name="' . esc_attr( $item_name ) . '" value="' . esc_attr( $item_value ) . '" />';
772
773 echo '<button class="merchant-metabox-field-uploads-upload button">' . esc_html__( 'Upload', 'merchant' ) . '</button>';
774 echo '<span class="merchant-metabox-field-uploads-move dashicons dashicons-menu"></span>';
775 echo '<span class="merchant-metabox-field-uploads-remove dashicons dashicons-trash"></span>';
776 echo '</li>';
777 }
778
779 echo '</ul>';
780 echo '<button class="merchant-metabox-field-uploads-add button button-primary">' . esc_html( $field['button'] ) . '</button>';
781 echo '</div>';
782
783 break;
784
785 case 'size-chart':
786 $field = wp_parse_args( $field, array(
787 'button' => '',
788 ) );
789
790 echo '<div class="merchant-metabox-field-size-chart-content">';
791 echo '<ul>';
792 echo '<li class="hidden">';
793 echo '<table>';
794 echo '<thead>';
795 echo '<tr>';
796 echo '<td colspan="100%">';
797 echo '<label>';
798 echo '<strong>' . esc_html__( 'Size Name', 'merchant' ) . ':</strong>';
799 echo '<input type="text" value="" data-name="' . esc_attr( $field_id ) . '[0][name]" />';
800 echo '</label>';
801 echo '</td>';
802 echo '</tr>';
803 echo '</thead>';
804 echo '<tbody>';
805 echo '<tr>';
806 for ( $a = 0; $a < 4; $a ++ ) {
807 echo '<td><div class="merchant-buttons"><a href="#" class="merchant-add-col">+</a><a href="#" class="merchant-del-col">-</a></div></td>';
808 }
809 echo '<td><a href="#" class="merchant-duplicate" title="' . esc_attr__( 'Duplicate', 'merchant' ) . '"><i class="dashicons dashicons-admin-page"></i></td>';
810 echo '</tr>';
811 for ( $b = 0; $b < 4; $b ++ ) {
812 echo '<tr>';
813 for ( $c = 0; $c < 4; $c ++ ) {
814 echo '<td><input type="text" value="" data-name="' . esc_attr( $field_id ) . '[0][sizes][0][0]" /></td>';
815 }
816 echo '<td><div class="merchant-buttons"><a href="#" class="merchant-add-row">+</a><a href="#" class="merchant-del-row">-</a></div></td>';
817 echo '</tr>';
818 }
819 echo '</tbody>';
820 echo '<tfoot>';
821 echo '<tr>';
822 echo '<td colspan="100%">';
823 echo '<a href="#" class="merchant-remove button button-primary">' . esc_html__( 'Remove', 'merchant' ) . '</a>';
824 echo '</td>';
825 echo '</tr>';
826 echo '</tfoot>';
827 echo '</table>';
828 echo '</li>';
829 $tabs = ( is_array( $value ) && ! empty( $value ) ) ? $value : array();
830 if ( ! empty( $tabs ) ) {
831 foreach ( $tabs as $tab_key => $tab ) {
832 $name = ( ! empty( $tab['name'] ) ) ? $tab['name'] : '';
833 $sizes = ( ! empty( $tab['sizes'] ) ) ? $tab['sizes'] : array();
834 echo '<li>';
835 echo '<table>';
836 echo '<thead>';
837 echo '<tr>';
838 echo '<td colspan="100%">';
839 echo '<label>';
840 echo '<strong>' . esc_html__( 'Size Name', 'merchant' ) . ':</strong>';
841 echo '<input type="text" value="' . esc_attr( $name ) . '" name="' . esc_attr( $field_id . '[' . $tab_key . '][name]' ) . '" />';
842 echo '</label>';
843 echo '</td>';
844 echo '</tr>';
845 echo '</thead>';
846 echo '<tbody>';
847 foreach ( $sizes as $row_key => $rows ) {
848 if ( 0 === $row_key ) {
849 echo '<tr>';
850 for ( $i = 0; $i < count( $rows ); $i ++ ) {
851 echo '<td><div class="merchant-buttons"><a href="#" class="merchant-add-col">+</a><a href="#" class="merchant-del-col">-</a></div></td>';
852 }
853 echo '<td><a href="#" class="merchant-duplicate" title="' . esc_attr__( 'Duplicate', 'merchant' ) . '"><i class="dashicons dashicons-admin-page"></i></td>';
854 echo '</tr>';
855 }
856 echo '<tr>';
857 foreach ( $rows as $col_key => $col ) {
858 echo '<td><input type="text" name="' . esc_attr( $field_id . '[' . $tab_key . '][sizes][' . $row_key . '][' . $col_key . ']' ) . '" value="' . esc_attr( $col ) . '" /></td>';
859 }
860 echo '<td><div class="merchant-buttons"><a href="#" class="merchant-add-row">+</a><a href="#" class="merchant-del-row">-</a></div></td>';
861 echo '</tr>';
862 }
863 echo '</tbody>';
864 echo '<tfoot>';
865 echo '<tr>';
866 echo '<td colspan="100%">';
867 echo '<a href="#" class="merchant-remove button button-primary">' . esc_html__( 'Remove', 'merchant' ) . '</a>';
868 echo '</td>';
869 echo '</tr>';
870 echo '</tfoot>';
871 echo '</table>';
872 echo '</li>';
873 }
874 }
875 echo '</ul>';
876 echo '<button class="merchant-add button button-primary">' . esc_html__( 'Add Size Chart', 'merchant' ) . '</button>';
877 echo '</div>';
878
879 break;
880
881 case 'size-chart-select':
882 $options = array();
883 $posts = get_posts( array(
884 'post_type' => 'merchant_size_chart',
885 'posts_per_page' => - 1,
886 'post_status' => 'publish'
887 ) );
888
889 if ( ! is_wp_error( $posts ) && ! empty( $posts ) ) {
890 foreach ( $posts as $_post ) {
891 $options[ $_post->ID ] = $_post->post_title;
892 }
893 }
894
895 echo '<select name="' . esc_attr( $field_id ) . '">';
896 echo '<option value="">' . esc_html__( 'Select a size chart', 'merchant' ) . '</option>';
897
898 foreach ( $options as $key => $option ) {
899 echo '<option value="' . esc_attr( $key ) . '"' . selected( $key, $value, false ) . '>' . esc_html( $option ) . '</option>';
900 }
901 echo '</select>';
902
903 break;
904
905 case 'sidebar-select':
906 global $wp_registered_sidebars;
907
908 $options = array();
909 if ( ! empty( $wp_registered_sidebars ) ) {
910 foreach ( $wp_registered_sidebars as $sidebar ) {
911 $options[ $sidebar['id'] ] = $sidebar['name'];
912 }
913 }
914
915 echo '<select name="' . esc_attr( $field_id ) . '">';
916 echo '<option value="">' . esc_html__( 'Default', 'merchant' ) . '</option>';
917
918 foreach ( $options as $key => $option ) {
919 echo '<option value="' . esc_attr( $key ) . '"' . selected( $key, $value, false ) . '>' . esc_html( $option ) . '</option>';
920 }
921 echo '</select>';
922
923 break;
924
925 case 'wp-editor':
926 $field = wp_parse_args( $field, array(
927 'height' => 150,
928 ) );
929
930 wp_editor( $value, $field_id, array(
931 'editor_height' => $field['height'],
932 ) );
933
934 break;
935
936 case 'code-editor':
937 echo '<textarea name="' . esc_attr( $field_id ) . '">' . esc_textarea( $value ) . '</textarea>';
938 break;
939
940 case 'coupons':
941 $coupons = get_posts( array(
942 'posts_per_page' => - 1,
943 'orderby' => 'title',
944 'order' => 'asc',
945 'post_type' => 'shop_coupon',
946 'post_status' => 'publish',
947 ) );
948 if ( $coupons ) {
949 echo '<select name="' . esc_attr( $field_id ) . '">';
950 echo '<option value="">' . esc_html__( 'Select a coupon', 'merchant' ) . '</option>';
951
952 foreach ( $coupons as $coupon ) {
953 $coupon_code = strtolower( $coupon->post_title );
954
955 echo '<option value="' . esc_attr( $coupon_code ) . '"' . selected( esc_attr( $coupon_code ), $value, false ) . '>';
956 echo esc_html( $coupon_code );
957 echo '</option>';
958 }
959
960 echo '</select>';
961 echo '<a href="' . esc_url( admin_url( 'edit.php?post_type=shop_coupon' ) ) . '" target="_blank" style="margin-left: 10px;">' . esc_html__( 'Manage coupons',
962 'merchant' ) . '</a>';
963 } else {
964 echo '<div style="color: red;">';
965 echo wp_kses_post(
966 sprintf(
967 /* Translators: 1. Coupon admin url 2. Link target attribute value */
968 __( 'No coupons found! <a href="%1$s" target="%2$s">Create a new coupon</a>', 'merchant' ),
969 admin_url( 'post-new.php?post_type=shop_coupon' ),
970 '_blank'
971 )
972 );
973 echo '</div>';
974 echo '<input type="hidden" name="' . esc_attr( $field_id ) . '" value="" />';
975 }
976 break;
977
978 case 'flexible-content':
979 $field = wp_parse_args( $field, array(
980 'button' => '',
981 ) );
982 $values = ( is_array( $value ) && ! empty( $value ) ) ? $value : array();
983
984 $empty = empty( $values ) ? 'empty' : '';
985
986 echo '<ul data-id="' . esc_attr( $field_id ) . '" class="merchant-metabox-field-flexible-content-list ' . esc_attr( $empty ) . '">';
987
988 ob_start();
989 echo '<li class="merchant-metabox-field-flexible-content-item">';
990 foreach ( $field['layouts'] as $layout_type => $layout ) {
991 echo '<div data-layout="' . esc_attr( $layout_type ) . '">';
992 echo '<div class="merchant-metabox-field-flexible-content-item-header">';
993 echo '<div class="merchant-metabox-field-flexible-content-item-count">0</div>';
994 echo '<div class="merchant-metabox-field-flexible-content-item-title">' . esc_html( $layout['title'] ) . '</div>';
995 echo '<div class="merchant-metabox-field-flexible-content-item-actions">';
996 echo '<span class="merchant-metabox-field-flexible-content-move dashicons dashicons-menu"></span>';
997 echo '<span class="merchant-metabox-field-flexible-content-remove dashicons dashicons-trash"></span>';
998 echo '</div>';
999 echo '</div>';
1000 echo '<div class="merchant-metabox-field-flexible-content-item-content">';
1001 foreach ( $layout['fields'] as $sub_field_key => $sub_field ) {
1002 $sub_field_classes = array( 'merchant-metabox-field-flexible-content-item-' . esc_attr( $sub_field['type'] ) );
1003 $sub_field_id = $field_id . '[0][' . $sub_field_key . ']';
1004 $sub_field_value = '';
1005 if ( $sub_field['type'] === 'select-ajax' ) {
1006 $sub_field_classes[] = 'merchant-metabox-field-flexible-content-select-ajax-clone';
1007 $sub_field_value = array();
1008 $sub_field = wp_parse_args( $sub_field, array(
1009 'source' => 'post',
1010 ) );
1011 }
1012 echo '<div class="' . esc_attr( implode( ' ', $sub_field_classes ) ) . '">';
1013 echo '<label>';
1014 echo esc_html( $sub_field['title'] );
1015 echo '</label>';
1016 echo '<div class="merchant-metabox-field-flexible-content-item-field" data-id="' . esc_attr( $sub_field_key ) . '">';
1017 $this->get_field( $sub_field_id, $sub_field, $sub_field_value );
1018 echo '</div>';
1019 echo '</div>';
1020 }
1021 echo '</div>';
1022 echo '<input type="hidden" name="' . esc_attr( $field_id ) . '[0][layout]" value="' . esc_attr( $layout_type ) . '">';
1023 echo '</div>';
1024 }
1025 echo '</li>';
1026 $sub_fields = ob_get_clean();
1027
1028 echo wp_kses( str_replace( 'name=', 'data-name=', $sub_fields ), merchant_kses_allowed_tags( array( 'all' ) ) );
1029
1030 foreach ( $values as $value_key => $value ) {
1031 echo '<li class="merchant-metabox-field-flexible-content-item">';
1032 echo '<div class="merchant-metabox-field-flexible-content-item-header">';
1033 echo '<div class="merchant-metabox-field-flexible-content-item-count">' . esc_html( ( $value_key + 1 ) ) . '</div>';
1034 echo '<div class="merchant-metabox-field-flexible-content-item-title">' . esc_html( $field['layouts'][ $value['layout'] ]['title'] ) . '</div>';
1035 echo '<div class="merchant-metabox-field-flexible-content-item-actions">';
1036 echo '<span class="merchant-metabox-field-flexible-content-move dashicons dashicons-menu"></span>';
1037 echo '<span class="merchant-metabox-field-flexible-content-remove dashicons dashicons-trash"></span>';
1038 echo '</div>';
1039 echo '</div>';
1040 echo '<div class="merchant-metabox-field-flexible-content-item-content">';
1041 foreach ( $field['layouts'][ $value['layout'] ]['fields'] as $sub_field_key => $sub_field ) {
1042 $sub_field_classes = array( 'merchant-metabox-field-flexible-content-' . esc_attr( $sub_field['type'] ) );
1043 $sub_field_id = $field_id . '[' . $value_key . '][' . $sub_field_key . ']';
1044 $sub_field_value = isset( $value[ $sub_field_key ] ) ? $value[ $sub_field_key ] : '';
1045 if ( $sub_field['type'] === 'select-ajax' ) {
1046 $sub_field_classes[] = 'merchant-metabox-field-flexible-content-select-ajax';
1047 $sub_field_value = empty( $sub_field_value ) ? array() : $sub_field_value;
1048 $sub_field = wp_parse_args( $sub_field, array(
1049 'source' => 'post',
1050 ) );
1051 }
1052 echo '<div class="' . esc_attr( implode( ' ', $sub_field_classes ) ) . '">';
1053 echo '<label>';
1054 echo esc_html( $sub_field['title'] );
1055 echo '</label>';
1056 echo '<div class="merchant-metabox-field-flexible-content-' . esc_attr( $sub_field['type'] ) . '-field" data-id="' . esc_attr( $sub_field_key ) . '">';
1057 $this->get_field( $sub_field_id, $sub_field, $sub_field_value );
1058 echo '</div>';
1059 echo '</div>';
1060 }
1061 echo '</div>';
1062 echo '<input type="hidden" name="' . esc_attr( $field_id ) . '[' . esc_attr( $value_key ) . '][layout]" value="' . esc_attr( $value['layout'] ) . '">';
1063 echo '</li>';
1064 }
1065
1066 echo '</ul>';
1067 echo '<div class="merchant-metabox-field-flexible-content-add-wrapper">';
1068 echo '<div class="merchant-metabox-field-flexible-content-add-list">';
1069 foreach ( $field['layouts'] as $layout_type => $layout ) {
1070 echo '<a class="merchant-metabox-field-flexible-content-add" data-id="' . esc_attr( $field_id ) . '" data-layout="' . esc_attr( $layout_type ) . '" href="#">' . esc_html( $layout['title'] ) . '</a>';
1071 }
1072 echo '</div>';
1073 echo '<button class="merchant-metabox-field-flexible-content-add-button button button-primary">' . esc_html( $field['button'] ) . '</button>';
1074 echo '</div>';
1075
1076 break;
1077 }
1078 }
1079
1080 }
1081 }
1082