resources
4 years ago
ajax-customers.php
4 years ago
ajax-posts.php
4 years ago
ajax-products.php
4 years ago
ajax-terms.php
4 years ago
buttons.php
5 years ago
checkbox-array.php
4 years ago
checkbox.php
4 years ago
colorpicker.php
4 years ago
copy-to-clipboard.php
4 years ago
country-select.php
4 years ago
custom.php
5 years ago
customtabs.php
5 years ago
date-format.php
4 years ago
datepicker.php
4 years ago
dimensions.php
4 years ago
email.php
4 years ago
hidden.php
4 years ago
html.php
5 years ago
icons.php
4 years ago
image-dimensions.php
5 years ago
image-gallery.php
5 years ago
inline-fields.php
4 years ago
list-table.php
4 years ago
multi-colorpicker.php
5 years ago
multi-select.php
5 years ago
number.php
4 years ago
onoff.php
4 years ago
password.php
4 years ago
preview.php
4 years ago
radio.php
4 years ago
select-buttons.php
4 years ago
select-images.php
4 years ago
select-mailchimp.php
4 years ago
select.php
4 years ago
sep.php
5 years ago
sidebarlist.php
4 years ago
sidebars.php
5 years ago
simple-text.php
4 years ago
slider.php
4 years ago
text-array.php
4 years ago
text-button.php
4 years ago
text.php
4 years ago
textarea-codemirror.php
4 years ago
textarea-editor.php
4 years ago
textarea.php
4 years ago
title.php
4 years ago
toggle-element-fixed.php
4 years ago
toggle-element.php
4 years ago
upload.php
4 years ago
ajax-terms.php
120 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Template for displaying the ajax-terms field |
| 4 | * Note: the stored value is an array if WooCommerce >= 3.0; string otherwise |
| 5 | * |
| 6 | * @var array $field The field. |
| 7 | * @package YITH\PluginFramework\Templates\Fields |
| 8 | */ |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 11 | |
| 12 | yith_plugin_fw_enqueue_enhanced_select(); |
| 13 | |
| 14 | $default_field = array( |
| 15 | 'id' => '', |
| 16 | 'name' => '', |
| 17 | 'class' => 'yith-term-search', |
| 18 | 'no_value' => false, |
| 19 | 'multiple' => false, |
| 20 | 'data' => array(), |
| 21 | 'style' => 'width:400px', |
| 22 | 'value' => '', |
| 23 | ); |
| 24 | |
| 25 | foreach ( $default_field as $field_key => $field_value ) { |
| 26 | if ( empty( $field[ $field_key ] ) ) { |
| 27 | $field[ $field_key ] = $field_value; |
| 28 | } |
| 29 | } |
| 30 | unset( $field_key ); |
| 31 | unset( $field_value ); |
| 32 | |
| 33 | list ( $field_id, $class, $no_value, $multiple, $data, $name, $style, $value ) = yith_plugin_fw_extract( $field, 'id', 'class', 'no_value', 'multiple', 'data', 'name', 'style', 'value' ); |
| 34 | |
| 35 | if ( $no_value ) { |
| 36 | $value = array(); |
| 37 | } |
| 38 | |
| 39 | $default_data = array( |
| 40 | 'action' => 'yith_plugin_fw_json_search_terms', |
| 41 | 'placeholder' => __( 'Search for a category...', 'yith-plugin-fw' ), |
| 42 | 'allow_clear' => false, |
| 43 | 'taxonomy' => 'category', |
| 44 | 'term_field' => 'id', |
| 45 | ); |
| 46 | $data = wp_parse_args( $data, $default_data ); |
| 47 | $show_id = isset( $data['show_id'] ) && $data['show_id']; |
| 48 | |
| 49 | // Separate select2 needed data and other data. |
| 50 | $select2_custom_attributes = array(); |
| 51 | $select2_data = array(); |
| 52 | $select2_data_keys = array( 'placeholder', 'allow_clear', 'action' ); |
| 53 | foreach ( $data as $d_key => $d_value ) { |
| 54 | if ( in_array( $d_key, $select2_data_keys, true ) ) { |
| 55 | $select2_data[ $d_key ] = $d_value; |
| 56 | } else { |
| 57 | $select2_custom_attributes[ 'data-' . $d_key ] = $d_value; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | $term_field = $data['term_field']; |
| 62 | |
| 63 | // Populate data-selected by value. |
| 64 | $data_selected = array(); |
| 65 | if ( ! empty( $value ) ) { |
| 66 | if ( $multiple ) { |
| 67 | if ( 'id' === $term_field ) { |
| 68 | $value = is_array( $value ) ? array_map( 'absint', $value ) : explode( ',', $value ); |
| 69 | } else { |
| 70 | $value = is_array( $value ) ? $value : explode( ',', $value ); |
| 71 | } |
| 72 | foreach ( $value as $term_value ) { |
| 73 | $the_term = get_term_by( $term_field, $term_value, $data['taxonomy'] ); |
| 74 | if ( is_object( $the_term ) ) { |
| 75 | $term_title = wp_kses_post( html_entity_decode( $the_term->name, ENT_QUOTES, get_bloginfo( 'charset' ) ) ); |
| 76 | $term_title_suffix = $show_id ? " (#{$the_term->term_id})" : ''; |
| 77 | $data_selected[ $term_value ] = $term_title . $term_title_suffix; |
| 78 | } else { |
| 79 | $data_selected[ $term_value ] = '#' . $term_value; |
| 80 | } |
| 81 | } |
| 82 | } else { |
| 83 | $term_value = 'id' === $term_field ? absint( $value ) : $value; |
| 84 | $the_term = get_term_by( $term_field, $term_value, $data['taxonomy'] ); |
| 85 | if ( is_object( $the_term ) ) { |
| 86 | $term_title = wp_kses_post( html_entity_decode( $the_term->name, ENT_QUOTES, get_bloginfo( 'charset' ) ) ); |
| 87 | $term_title_suffix = $show_id ? " (#{$the_term->term_id})" : ''; |
| 88 | $data_selected[ $term_value ] = $term_title . $term_title_suffix; |
| 89 | } else { |
| 90 | $data_selected[ $term_value ] = '#' . $term_value; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // Parse $value to string to prevent issue with wc2.6. |
| 96 | $value = is_array( $value ) ? implode( ',', $value ) : $value; |
| 97 | ?> |
| 98 | <div class="yith-plugin-fw-select2-wrapper"> |
| 99 | <?php |
| 100 | if ( function_exists( 'yit_add_select2_fields' ) ) { |
| 101 | yit_add_select2_fields( |
| 102 | array( |
| 103 | 'id' => $field_id, |
| 104 | 'name' => $name, |
| 105 | 'class' => $class, |
| 106 | 'data-multiple' => $multiple, |
| 107 | 'data-placeholder' => $select2_data['placeholder'], |
| 108 | 'data-allow_clear' => $select2_data['allow_clear'], |
| 109 | 'data-action' => $select2_data['action'], |
| 110 | 'custom-attributes' => $select2_custom_attributes, |
| 111 | 'style' => $style, |
| 112 | 'value' => $value, |
| 113 | 'data-selected' => $data_selected, |
| 114 | 'data-term-field' => $term_field, |
| 115 | ) |
| 116 | ); |
| 117 | } |
| 118 | ?> |
| 119 | </div> |
| 120 |