PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 1.6
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v1.6
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.6, at inc/classes/class-merchant-metabox.php

1,078 lines 39.4 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 if ( isset( $field['style'] ) && ! empty( $field['style'] ) ) {
490 $style = 'style="' . esc_attr( str_replace( [ '&', '=' ], [ '; ', ': ' ], http_build_query( $field['style'] ) ) ) . '"';
491 }
492 if ( isset( $field['append'] ) || isset( $field['prepend'] ) ) {
493 echo '<div class="merchant-metabox-field-input-container">';
494 if ( isset( $field['prepend'] ) ) {
495 echo '<div class="merchant-metabox-field-prepend">' . esc_attr( $field['prepend'] ) . '</div>';
496 }
497 echo '<input type="number" name="' . esc_attr( $field_id ) . '" value="' . esc_attr( $value ) . '" ' . wp_kses_post( $style ) . ' />';
498 if ( isset( $field['append'] ) ) {
499 echo '<div class="merchant-metabox-field-append">' . esc_attr( $field['append'] ) . '</div>';
500 }
501 echo '</div>';
502 } else {
503 echo '<input type="number" name="' . esc_attr( $field_id ) . '" value="' . esc_attr( $value ) . '" ' . wp_kses_post( $style ) . ' />';
504 }
505 break;
506
507 case 'textarea':
508 echo '<textarea name="' . esc_attr( $field_id ) . '">' . esc_textarea( $value ) . '</textarea>';
509 break;
510
511 case 'checkbox':
512 case 'switcher':
513 $field = wp_parse_args( $field, array(
514 'label' => '',
515 ) );
516
517 echo '<label>';
518 echo '<input type="checkbox" name="' . esc_attr( $field_id ) . '" value="1"' . checked( $value, true, false ) . ' />';
519
520 if ( 'switcher' === $field['type'] ) {
521 echo '<i></i>';
522 }
523
524 if ( ! empty( $field['label'] ) ) {
525 echo '<span>' . esc_html( $field['label'] ) . '</span>';
526 }
527 echo '</label>';
528 break;
529
530 case 'select':
531 echo '<select name="' . esc_attr( $field_id ) . '">';
532
533 foreach ( $field['options'] as $key => $option ) {
534 echo '<option value="' . esc_attr( $key ) . '"' . selected( $key, $value, false ) . '>' . esc_html( $option ) . '</option>';
535 }
536
537 echo '</select>';
538 break;
539
540 case 'select-ajax':
541 $field = wp_parse_args( $field, array(
542 'source' => 'post',
543 ) );
544 $ids = ( is_array( $value ) && ! empty( $value ) ) ? $value : (array) $value;
545 if ( isset( $field['multiple'] ) ) {
546 $multiple = $field['multiple'] === true ? 'multiple' : '';
547 } else {
548 $multiple = 'multiple';
549 }
550
551 echo '<select name="' . esc_attr( $field_id ) . '[]" ' . esc_attr( $multiple ) . ' data-source="' . esc_attr( $field['source'] ) . '">';
552
553 if ( ! empty( $ids ) ) {
554 foreach ( $ids as $id ) {
555 switch ( $field['source'] ) {
556 case 'post':
557 case 'product':
558 $post = get_post( $id );
559
560 if ( ! empty( $post ) ) {
561 echo '<option value="' . esc_attr( $post->ID ) . '" selected>' . esc_html( $post->post_title ) . '</option>';
562 }
563 break;
564 }
565 }
566 }
567
568 echo '</select>';
569 break;
570
571 case 'wc-attributes':
572 $attributes = wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_id' );
573 $values = ( is_array( $value ) && ! empty( $value ) ) ? $value : array();
574
575 if ( ! empty( $attributes ) ) {
576 echo '<div class="merchant-metabox-field-attributes">';
577 echo '<ul class="merchant-sortable">';
578
579 $selected_attributes = array();
580 foreach ( $values as $id ) {
581 if ( isset( $attributes[ $id ] ) ) {
582 $selected_attributes[ $id ] = $attributes[ $id ];
583 unset( $attributes[ $id ] );
584 }
585 }
586
587 $attributes = array_replace( $selected_attributes, $attributes );
588 foreach ( $attributes as $attribute_id => $attribute_label ) {
589 $checked = ( in_array( $attribute_id, $values ) ) ? ' checked' : '';
590
591 echo '<li class="merchant-sortable-item">';
592 echo '<label>';
593 echo '<input type="checkbox" name="' . esc_attr( $field_id ) . '[]" value="' . esc_attr( $attribute_id ) . '"' . esc_attr( $checked ) . ' />';
594 echo '<span>' . esc_html( $attribute_label ) . '</span>';
595 echo '</label>';
596 echo '<span class="merchant-sortable-move dashicons dashicons-menu"></span>';
597 echo '</li>';
598 }
599
600 echo '</ul>';
601 echo '</div>';
602 }
603
604 break;
605
606 case 'choices':
607 echo '<div class="merchant-metabox-field-choices-images">';
608
609 foreach ( $field['options'] as $key => $option ) {
610 echo '<label>';
611 echo '<input type="radio" name="' . esc_attr( $field_id ) . '" value="' . esc_attr( $key ) . '"' . checked( $value, $key, false ) . ' />';
612 echo '<figure><img src="' . esc_url( sprintf( $option['image'],
613 MERCHANT_URI ) ) . '" title="' . esc_attr( $option['label'] ) . '" alt="' . esc_attr( $option['label'] ) . '" /></figure>';
614 echo '</label>';
615 }
616
617 echo '</div>';
618 break;
619
620 case 'content':
621 echo wp_kses_post( $field['content'] );
622 break;
623
624 case 'repeater':
625 $field = wp_parse_args( $field, array(
626 'button' => '',
627 ) );
628
629 echo '<div class="merchant-metabox-field-repeater-content" data-id="' . esc_attr( $field_id ) . '">';
630 $values = ( is_array( $value ) && ! empty( $value ) ) ? $value : array();
631
632 echo '<ul class="merchant-metabox-field-repeater-list">';
633 echo '<li class="merchant-metabox-field-repeater-list-item hidden">';
634 if ( isset( $field['fields'] ) ) {
635 echo '<div class="merchant-metabox-field-repeater-list-item-fields">';
636 foreach ( $field['fields'] as $sub_field_id => $sub_field ) {
637 echo '<div class="merchant-metabox-field-repeater-list-item-field">';
638 if ( isset( $sub_field['title'] ) ) {
639 echo '<span class="merchant-metabox-field-repeater-list-item-field-title">' . esc_html( $sub_field['title'] ) . '</span>';
640 }
641 echo '<div class="merchant-metabox-field-repeater-list-item-field-input" data-id="' . esc_attr( $sub_field_id ) . '">';
642 if ( $sub_field['type'] === 'text' ) {
643 echo '<input type="text" name="" value="" data-name="' . esc_attr( $field_id ) . '[0][' . esc_attr( $sub_field_id ) . ']" />';
644 }
645 if ( $sub_field['type'] === 'number' ) {
646 echo '<input type="number" name="" value="" data-name="' . esc_attr( $field_id ) . '[0][' . esc_attr( $sub_field_id ) . ']" />';
647 }
648 echo '</div>';
649 echo '</div>';
650 }
651 echo '</div>';
652 } else {
653 echo '<input type="text" name="" value="" data-name="' . esc_attr( $field_id ) . '[]" />';
654 }
655 echo '<span class="merchant-metabox-field-repeater-move dashicons dashicons-menu"></span>';
656 echo '<span class="merchant-metabox-field-repeater-remove dashicons dashicons-trash"></span>';
657 echo '</li>';
658
659 foreach ( $values as $key => $value ) {
660 echo '<li class="merchant-metabox-field-repeater-list-item">';
661 if ( isset( $field['fields'] ) ) {
662 echo '<div class="merchant-metabox-field-repeater-list-item-fields">';
663 foreach ( $field['fields'] as $sub_field_id => $sub_field ) {
664 echo '<div class="merchant-metabox-field-repeater-list-item-field">';
665 if ( isset( $sub_field['title'] ) ) {
666 echo '<span class="merchant-metabox-field-repeater-list-item-field-title">' . esc_attr( $sub_field['title'] ) . '</span>';
667 }
668 echo '<div class="merchant-metabox-field-repeater-list-item-field-input" data-id="' . esc_attr( $sub_field_id ) . '">';
669 if ( $sub_field['type'] === 'text' ) {
670 echo '<input type="text" name="' . esc_attr( $field_id ) . '[' . esc_attr( $key ) . '][' . esc_attr( $sub_field_id ) . ']" value="' . esc_attr( $value[ $sub_field_id ] ) . '" />';
671 }
672 if ( $sub_field['type'] === 'number' ) {
673 echo '<input type="number" name="' . esc_attr( $field_id ) . '[' . esc_attr( $key ) . '][' . esc_attr( $sub_field_id ) . ']" value="' . esc_attr( $value[ $sub_field_id ] ) . '" />';
674 }
675 echo '</div>';
676 echo '</div>';
677 }
678 echo '</div>';
679 } else {
680 echo '<input type="text" name="' . esc_attr( $field_id ) . '[]" value="' . esc_attr( $value ) . '" />';
681 }
682
683 echo '<span class="merchant-metabox-field-repeater-move dashicons dashicons-menu"></span>';
684 echo '<span class="merchant-metabox-field-repeater-remove dashicons dashicons-trash"></span>';
685 echo '</li>';
686 }
687 echo '</ul>';
688 echo '<button class="merchant-metabox-field-repeater-add button button-primary">' . esc_html( $field['button'] ) . '</button>';
689 echo '</div>';
690
691 break;
692
693 case 'media':
694 $placeholder = class_exists( 'Woocommerce' ) ? wc_placeholder_img_src( 'thumbnail' ) : MERCHANT_URI . '/assets/placeholder.svg';
695 $hidden_class = ( empty( $value ) ) ? ' hidden' : '';
696
697 if ( ! empty( $value ) ) {
698 $attachment = wp_get_attachment_image_src( $value, 'thumbnail' );
699 $thumbnail = ( is_array( $attachment ) && ! empty( $attachment[0] ) ) ? $attachment[0] : $placeholder;
700 } else {
701 $thumbnail = $placeholder;
702 }
703
704 echo '<div class="merchant-metabox-field-media-content">';
705 echo '<figure class="merchant-metabox-field-media-preview">';
706 echo '<img src="' . esc_url( $thumbnail ) . '" data-placeholder="' . esc_url( $placeholder ) . '" />';
707 echo '</figure>';
708
709 echo '<div class="merchant-metabox-field-media-button">';
710 echo '<a href="#" class="merchant-metabox-field-media-upload button">' . esc_html__( 'Upload/Add Image', 'merchant' ) . '</a>';
711 echo '<a href="#" class="merchant-metabox-field-media-remove merchant-button-remove button' . esc_attr( $hidden_class ) . '">' . esc_html__( 'Remove Image', 'merchant' ) . '</a>';
712 echo '<input type="hidden" name="' . esc_attr( $field_id ) . '" value="' . esc_attr( $value ) . '" class="merchant-metabox-field-media-input" />';
713 echo '</div>';
714 echo '</div>';
715
716 break;
717
718 case 'uploads':
719 $field = wp_parse_args( $field, array(
720 'button' => '',
721 'library' => 'image',
722 ) );
723
724 $enable_thumb = isset( $field['enable_thumb'] ) && $field['enable_thumb'];
725
726 echo '<div class="merchant-metabox-field-uploads-content">';
727 $values = ( is_array( $value ) && ! empty( $value ) ) ? $value : array();
728 $name = 'video' === $field['library'] ? $field_id . '[0][src]' : $field_id . '[]';
729 $thumb_name = $field_id . '[0][thumb]';
730
731 echo '<ul class="merchant-metabox-field-uploads-list" data-library="' . esc_attr( $field['library'] ) . '">';
732 echo '<li class="merchant-metabox-field-uploads-list-item hidden">';
733
734 if ( 'video' === $field['library'] || $enable_thumb ) {
735 echo '<div class="merchant-metabox-field-uploads-thumbnail">';
736 echo '<a href="#" class="merchant-metabox-field-uploads-thumbnail-remove dashicons dashicons-dismiss" style="display:none"></a>';
737 echo '<a href="#" class="merchant-metabox-field-uploads-thumbnail-upload"><span>+</span></a>';
738 echo '<input type="hidden" name="" value="" data-name="' . esc_attr( $thumb_name ) . '" />';
739 echo '</div>';
740 }
741
742 echo '<input type="text" name="" value="" data-name="' . esc_attr( $name ) . '" />';
743 echo '<button class="merchant-metabox-field-uploads-upload button">' . esc_html__( 'Upload', 'merchant' ) . '</button>';
744 echo '<span class="merchant-metabox-field-uploads-move dashicons dashicons-menu"></span>';
745 echo '<span class="merchant-metabox-field-uploads-remove dashicons dashicons-trash"></span>';
746 echo '</li>';
747
748 foreach ( $values as $key => $value ) {
749 $item_name = 'video' === $field['library'] || $enable_thumb ? str_replace( '0', $key, $name ) : $name;
750 $item_value = is_array( $value ) ? ( isset( $value['src'] ) ? $value['src'] : '' ) : $value;
751
752 echo '<li class="merchant-metabox-field-uploads-list-item">';
753 if ( 'video' === $field['library'] || $enable_thumb ) {
754 $item_thumb = is_array( $value ) && isset( $value['thumb'] ) ? $value['thumb'] : '';
755 $item_thumb_name = str_replace( '0', $key, $thumb_name );
756
757 echo '<div class="merchant-metabox-field-uploads-thumbnail">';
758 echo '<a href="#" class="merchant-metabox-field-uploads-thumbnail-remove dashicons dashicons-dismiss" ' . ( empty( $item_thumb ) ? 'style="display: none"' : '' ) . '></a>';
759 echo '<a href="#" class="merchant-metabox-field-uploads-thumbnail-upload">';
760 echo empty( $item_thumb )
761 ? '<span>+</span>'
762 : '<img src="' . esc_url( wp_get_attachment_thumb_url( $item_thumb ) ) . '" /><span style="display: none">+</span>';
763 echo '</a>';
764 echo '<input type="hidden" name="' . esc_attr( $item_thumb_name ) . '" value="' . absint( $item_thumb ) . '" />';
765 echo '</div>';
766 }
767 echo '<input type="text" name="' . esc_attr( $item_name ) . '" value="' . esc_attr( $item_value ) . '" />';
768
769 echo '<button class="merchant-metabox-field-uploads-upload button">' . esc_html__( 'Upload', 'merchant' ) . '</button>';
770 echo '<span class="merchant-metabox-field-uploads-move dashicons dashicons-menu"></span>';
771 echo '<span class="merchant-metabox-field-uploads-remove dashicons dashicons-trash"></span>';
772 echo '</li>';
773 }
774
775 echo '</ul>';
776 echo '<button class="merchant-metabox-field-uploads-add button button-primary">' . esc_html( $field['button'] ) . '</button>';
777 echo '</div>';
778
779 break;
780
781 case 'size-chart':
782 $field = wp_parse_args( $field, array(
783 'button' => '',
784 ) );
785
786 echo '<div class="merchant-metabox-field-size-chart-content">';
787 echo '<ul>';
788 echo '<li class="hidden">';
789 echo '<table>';
790 echo '<thead>';
791 echo '<tr>';
792 echo '<td colspan="100%">';
793 echo '<label>';
794 echo '<strong>' . esc_html__( 'Size Name', 'merchant' ) . ':</strong>';
795 echo '<input type="text" value="" data-name="' . esc_attr( $field_id ) . '[0][name]" />';
796 echo '</label>';
797 echo '</td>';
798 echo '</tr>';
799 echo '</thead>';
800 echo '<tbody>';
801 echo '<tr>';
802 for ( $a = 0; $a < 4; $a ++ ) {
803 echo '<td><div class="merchant-buttons"><a href="#" class="merchant-add-col">+</a><a href="#" class="merchant-del-col">-</a></div></td>';
804 }
805 echo '<td><a href="#" class="merchant-duplicate" title="' . esc_attr__( 'Duplicate', 'merchant' ) . '"><i class="dashicons dashicons-admin-page"></i></td>';
806 echo '</tr>';
807 for ( $b = 0; $b < 4; $b ++ ) {
808 echo '<tr>';
809 for ( $c = 0; $c < 4; $c ++ ) {
810 echo '<td><input type="text" value="" data-name="' . esc_attr( $field_id ) . '[0][sizes][0][0]" /></td>';
811 }
812 echo '<td><div class="merchant-buttons"><a href="#" class="merchant-add-row">+</a><a href="#" class="merchant-del-row">-</a></div></td>';
813 echo '</tr>';
814 }
815 echo '</tbody>';
816 echo '<tfoot>';
817 echo '<tr>';
818 echo '<td colspan="100%">';
819 echo '<a href="#" class="merchant-remove button button-primary">' . esc_html__( 'Remove', 'merchant' ) . '</a>';
820 echo '</td>';
821 echo '</tr>';
822 echo '</tfoot>';
823 echo '</table>';
824 echo '</li>';
825 $tabs = ( is_array( $value ) && ! empty( $value ) ) ? $value : array();
826 if ( ! empty( $tabs ) ) {
827 foreach ( $tabs as $tab_key => $tab ) {
828 $name = ( ! empty( $tab['name'] ) ) ? $tab['name'] : '';
829 $sizes = ( ! empty( $tab['sizes'] ) ) ? $tab['sizes'] : array();
830 echo '<li>';
831 echo '<table>';
832 echo '<thead>';
833 echo '<tr>';
834 echo '<td colspan="100%">';
835 echo '<label>';
836 echo '<strong>' . esc_html__( 'Size Name', 'merchant' ) . ':</strong>';
837 echo '<input type="text" value="' . esc_attr( $name ) . '" name="' . esc_attr( $field_id . '[' . $tab_key . '][name]' ) . '" />';
838 echo '</label>';
839 echo '</td>';
840 echo '</tr>';
841 echo '</thead>';
842 echo '<tbody>';
843 foreach ( $sizes as $row_key => $rows ) {
844 if ( 0 === $row_key ) {
845 echo '<tr>';
846 for ( $i = 0; $i < count( $rows ); $i ++ ) {
847 echo '<td><div class="merchant-buttons"><a href="#" class="merchant-add-col">+</a><a href="#" class="merchant-del-col">-</a></div></td>';
848 }
849 echo '<td><a href="#" class="merchant-duplicate" title="' . esc_attr__( 'Duplicate', 'merchant' ) . '"><i class="dashicons dashicons-admin-page"></i></td>';
850 echo '</tr>';
851 }
852 echo '<tr>';
853 foreach ( $rows as $col_key => $col ) {
854 echo '<td><input type="text" name="' . esc_attr( $field_id . '[' . $tab_key . '][sizes][' . $row_key . '][' . $col_key . ']' ) . '" value="' . esc_attr( $col ) . '" /></td>';
855 }
856 echo '<td><div class="merchant-buttons"><a href="#" class="merchant-add-row">+</a><a href="#" class="merchant-del-row">-</a></div></td>';
857 echo '</tr>';
858 }
859 echo '</tbody>';
860 echo '<tfoot>';
861 echo '<tr>';
862 echo '<td colspan="100%">';
863 echo '<a href="#" class="merchant-remove button button-primary">' . esc_html__( 'Remove', 'merchant' ) . '</a>';
864 echo '</td>';
865 echo '</tr>';
866 echo '</tfoot>';
867 echo '</table>';
868 echo '</li>';
869 }
870 }
871 echo '</ul>';
872 echo '<button class="merchant-add button button-primary">' . esc_html__( 'Add Size Chart', 'merchant' ) . '</button>';
873 echo '</div>';
874
875 break;
876
877 case 'size-chart-select':
878 $options = array();
879 $posts = get_posts( array(
880 'post_type' => 'merchant_size_chart',
881 'posts_per_page' => - 1,
882 'post_status' => 'publish'
883 ) );
884
885 if ( ! is_wp_error( $posts ) && ! empty( $posts ) ) {
886 foreach ( $posts as $_post ) {
887 $options[ $_post->ID ] = $_post->post_title;
888 }
889 }
890
891 echo '<select name="' . esc_attr( $field_id ) . '">';
892 echo '<option value="">' . esc_html__( 'Select a size chart', 'merchant' ) . '</option>';
893
894 foreach ( $options as $key => $option ) {
895 echo '<option value="' . esc_attr( $key ) . '"' . selected( $key, $value, false ) . '>' . esc_html( $option ) . '</option>';
896 }
897 echo '</select>';
898
899 break;
900
901 case 'sidebar-select':
902 global $wp_registered_sidebars;
903
904 $options = array();
905 if ( ! empty( $wp_registered_sidebars ) ) {
906 foreach ( $wp_registered_sidebars as $sidebar ) {
907 $options[ $sidebar['id'] ] = $sidebar['name'];
908 }
909 }
910
911 echo '<select name="' . esc_attr( $field_id ) . '">';
912 echo '<option value="">' . esc_html__( 'Default', 'merchant' ) . '</option>';
913
914 foreach ( $options as $key => $option ) {
915 echo '<option value="' . esc_attr( $key ) . '"' . selected( $key, $value, false ) . '>' . esc_html( $option ) . '</option>';
916 }
917 echo '</select>';
918
919 break;
920
921 case 'wp-editor':
922 $field = wp_parse_args( $field, array(
923 'height' => 150,
924 ) );
925
926 wp_editor( $value, $field_id, array(
927 'editor_height' => $field['height'],
928 ) );
929
930 break;
931
932 case 'code-editor':
933 echo '<textarea name="' . esc_attr( $field_id ) . '">' . esc_textarea( $value ) . '</textarea>';
934 break;
935
936 case 'coupons':
937 $coupons = get_posts( array(
938 'posts_per_page' => - 1,
939 'orderby' => 'title',
940 'order' => 'asc',
941 'post_type' => 'shop_coupon',
942 'post_status' => 'publish',
943 ) );
944 if ( $coupons ) {
945 echo '<select name="' . esc_attr( $field_id ) . '">';
946 echo '<option value="">' . esc_html__( 'Select a coupon', 'merchant' ) . '</option>';
947
948 foreach ( $coupons as $coupon ) {
949 $coupon_code = strtolower( $coupon->post_title );
950
951 echo '<option value="' . esc_attr( $coupon_code ) . '"' . selected( esc_attr( $coupon_code ), $value, false ) . '>';
952 echo esc_html( $coupon_code );
953 echo '</option>';
954 }
955
956 echo '</select>';
957 echo '<a href="' . esc_url( admin_url( 'edit.php?post_type=shop_coupon' ) ) . '" target="_blank" style="margin-left: 10px;">' . esc_html__( 'Manage coupons',
958 'merchant' ) . '</a>';
959 } else {
960 echo '<div style="color: red;">';
961 echo wp_kses_post(
962 sprintf(
963 /* Translators: 1. Coupon admin url 2. Link target attribute value */
964 __( 'No coupons found! <a href="%1$s" target="%2$s">Create a new coupon</a>', 'merchant' ),
965 admin_url( 'post-new.php?post_type=shop_coupon' ),
966 '_blank'
967 )
968 );
969 echo '</div>';
970 echo '<input type="hidden" name="' . esc_attr( $field_id ) . '" value="" />';
971 }
972 break;
973
974 case 'flexible-content':
975 $field = wp_parse_args( $field, array(
976 'button' => '',
977 ) );
978 $values = ( is_array( $value ) && ! empty( $value ) ) ? $value : array();
979
980 $empty = empty( $values ) ? 'empty' : '';
981
982 echo '<ul data-id="' . esc_attr( $field_id ) . '" class="merchant-metabox-field-flexible-content-list ' . esc_attr( $empty ) . '">';
983
984 ob_start();
985 echo '<li class="merchant-metabox-field-flexible-content-item">';
986 foreach ( $field['layouts'] as $layout_type => $layout ) {
987 echo '<div data-layout="' . esc_attr( $layout_type ) . '">';
988 echo '<div class="merchant-metabox-field-flexible-content-item-header">';
989 echo '<div class="merchant-metabox-field-flexible-content-item-count">0</div>';
990 echo '<div class="merchant-metabox-field-flexible-content-item-title">' . esc_html( $layout['title'] ) . '</div>';
991 echo '<div class="merchant-metabox-field-flexible-content-item-actions">';
992 echo '<span class="merchant-metabox-field-flexible-content-move dashicons dashicons-menu"></span>';
993 echo '<span class="merchant-metabox-field-flexible-content-remove dashicons dashicons-trash"></span>';
994 echo '</div>';
995 echo '</div>';
996 echo '<div class="merchant-metabox-field-flexible-content-item-content">';
997 foreach ( $layout['fields'] as $sub_field_key => $sub_field ) {
998 $sub_field_classes = array( 'merchant-metabox-field-flexible-content-item-' . esc_attr( $sub_field['type'] ) );
999 $sub_field_id = $field_id . '[0][' . $sub_field_key . ']';
1000 $sub_field_value = '';
1001 if ( $sub_field['type'] === 'select-ajax' ) {
1002 $sub_field_classes[] = 'merchant-metabox-field-flexible-content-select-ajax-clone';
1003 $sub_field_value = array();
1004 $sub_field = wp_parse_args( $sub_field, array(
1005 'source' => 'post',
1006 ) );
1007 }
1008 echo '<div class="' . esc_attr( implode( ' ', $sub_field_classes ) ) . '">';
1009 echo '<label>';
1010 echo esc_html( $sub_field['title'] );
1011 echo '</label>';
1012 echo '<div class="merchant-metabox-field-flexible-content-item-field" data-id="' . esc_attr( $sub_field_key ) . '">';
1013 $this->get_field( $sub_field_id, $sub_field, $sub_field_value );
1014 echo '</div>';
1015 echo '</div>';
1016 }
1017 echo '</div>';
1018 echo '<input type="hidden" name="' . esc_attr( $field_id ) . '[0][layout]" value="' . esc_attr( $layout_type ) . '">';
1019 echo '</div>';
1020 }
1021 echo '</li>';
1022 $sub_fields = ob_get_clean();
1023
1024 echo wp_kses( str_replace( 'name=', 'data-name=', $sub_fields ), merchant_kses_allowed_tags( array( 'all' ) ) );
1025
1026 foreach ( $values as $value_key => $value ) {
1027 echo '<li class="merchant-metabox-field-flexible-content-item">';
1028 echo '<div class="merchant-metabox-field-flexible-content-item-header">';
1029 echo '<div class="merchant-metabox-field-flexible-content-item-count">' . esc_html( ( $value_key + 1 ) ) . '</div>';
1030 echo '<div class="merchant-metabox-field-flexible-content-item-title">' . esc_html( $field['layouts'][ $value['layout'] ]['title'] ) . '</div>';
1031 echo '<div class="merchant-metabox-field-flexible-content-item-actions">';
1032 echo '<span class="merchant-metabox-field-flexible-content-move dashicons dashicons-menu"></span>';
1033 echo '<span class="merchant-metabox-field-flexible-content-remove dashicons dashicons-trash"></span>';
1034 echo '</div>';
1035 echo '</div>';
1036 echo '<div class="merchant-metabox-field-flexible-content-item-content">';
1037 foreach ( $field['layouts'][ $value['layout'] ]['fields'] as $sub_field_key => $sub_field ) {
1038 $sub_field_classes = array( 'merchant-metabox-field-flexible-content-' . esc_attr( $sub_field['type'] ) );
1039 $sub_field_id = $field_id . '[' . $value_key . '][' . $sub_field_key . ']';
1040 $sub_field_value = isset( $value[ $sub_field_key ] ) ? $value[ $sub_field_key ] : '';
1041 if ( $sub_field['type'] === 'select-ajax' ) {
1042 $sub_field_classes[] = 'merchant-metabox-field-flexible-content-select-ajax';
1043 $sub_field_value = empty( $sub_field_value ) ? array() : $sub_field_value;
1044 $sub_field = wp_parse_args( $sub_field, array(
1045 'source' => 'post',
1046 ) );
1047 }
1048 echo '<div class="' . esc_attr( implode( ' ', $sub_field_classes ) ) . '">';
1049 echo '<label>';
1050 echo esc_html( $sub_field['title'] );
1051 echo '</label>';
1052 echo '<div class="merchant-metabox-field-flexible-content-' . esc_attr( $sub_field['type'] ) . '-field" data-id="' . esc_attr( $sub_field_key ) . '">';
1053 $this->get_field( $sub_field_id, $sub_field, $sub_field_value );
1054 echo '</div>';
1055 echo '</div>';
1056 }
1057 echo '</div>';
1058 echo '<input type="hidden" name="' . esc_attr( $field_id ) . '[' . esc_attr( $value_key ) . '][layout]" value="' . esc_attr( $value['layout'] ) . '">';
1059 echo '</li>';
1060 }
1061
1062 echo '</ul>';
1063 echo '<div class="merchant-metabox-field-flexible-content-add-wrapper">';
1064 echo '<div class="merchant-metabox-field-flexible-content-add-list">';
1065 foreach ( $field['layouts'] as $layout_type => $layout ) {
1066 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>';
1067 }
1068 echo '</div>';
1069 echo '<button class="merchant-metabox-field-flexible-content-add-button button button-primary">' . esc_html( $field['button'] ) . '</button>';
1070 echo '</div>';
1071
1072 break;
1073 }
1074 }
1075
1076 }
1077 }
1078