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

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