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
4 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
toggle-element.php
292 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Template for displaying the toggle-element field |
| 4 | * |
| 5 | * @var array $field The field. |
| 6 | * @package YITH\PluginFramework\Templates\Fields |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 10 | |
| 11 | $defaults = array( |
| 12 | 'id' => '', |
| 13 | 'class' => '', |
| 14 | 'name' => '', |
| 15 | 'add_button' => '', |
| 16 | 'elements' => array(), |
| 17 | 'title' => '', |
| 18 | 'subtitle' => '', |
| 19 | 'onoff_field' => array(), |
| 20 | 'sortable' => false, |
| 21 | 'save_button' => array(), |
| 22 | 'delete_button' => array(), |
| 23 | 'custom_attributes' => '', |
| 24 | ); |
| 25 | $field = wp_parse_args( $field, $defaults ); |
| 26 | |
| 27 | list ( $field_id, $class, $name, $value, $add_button, $elements, $the_title, $subtitle, $onoff_field, $sortable, $save_button, $delete_button, $custom_attributes ) = yith_plugin_fw_extract( $field, 'id', 'class', 'name', 'value', 'add_button', 'elements', 'title', 'subtitle', 'onoff_field', 'sortable', 'save_button', 'delete_button', 'custom_attributes' ); |
| 28 | |
| 29 | $show_add_button = isset( $add_button ) && $add_button; |
| 30 | $add_button_closed = isset( $add_button_closed ) ? $add_button_closed : ''; |
| 31 | $values = isset( $value ) ? $value : get_option( $name, array() ); |
| 32 | $values = maybe_unserialize( $values ); |
| 33 | $sortable = isset( $sortable ) ? $sortable : false; |
| 34 | $class_wrapper = $sortable ? 'ui-sortable' : ''; |
| 35 | $onoff_id = isset( $onoff_field['id'] ) ? $onoff_field['id'] : ''; |
| 36 | $ajax_nonce = wp_create_nonce( 'save-toggle-element' ); |
| 37 | |
| 38 | if ( empty( $values ) && ! $show_add_button && $elements ) { |
| 39 | $values = array(); |
| 40 | // Populate toggle element with the default values. |
| 41 | foreach ( $elements as $element ) { |
| 42 | $values[0][ $element['id'] ] = $element['default']; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | ?> |
| 47 | <div class="yith-toggle_wrapper <?php echo esc_attr( $class_wrapper ); ?>" id="<?php echo esc_attr( $field_id ); ?>" data-nonce="<?php echo esc_attr( $ajax_nonce ); ?>"> |
| 48 | <?php if ( ! empty( $label ) ) : ?> |
| 49 | <label for="<?php esc_attr( $field_id ); ?>"><?php echo esc_html( $label ); ?></label> |
| 50 | <?php endif; ?> |
| 51 | <?php if ( $show_add_button ) : ?> |
| 52 | <button class="yith-add-button yith-add-box-button" |
| 53 | data-box_id="<?php echo esc_attr( $field_id ); ?>_add_box" |
| 54 | data-closed_label="<?php echo esc_attr( $add_button_closed ); ?>" |
| 55 | data-opened_label="<?php echo esc_attr( $add_button ); ?>"><?php echo esc_html( $add_button ); ?></button> |
| 56 | <div id="<?php echo esc_attr( $field_id ); ?>_add_box" class="yith-add-box"></div> |
| 57 | <script type="text/template" id="tmpl-yith-toggle-element-add-box-content-<?php echo esc_attr( $field_id ); ?>"> |
| 58 | <?php foreach ( $elements as $element ) : ?> |
| 59 | <?php |
| 60 | $element['title'] = $element['name']; |
| 61 | $element['type'] = isset( $element['yith-type'] ) ? $element['yith-type'] : $element['type']; |
| 62 | |
| 63 | unset( $element['yith-type'] ); |
| 64 | |
| 65 | $element['value'] = isset( $element['default'] ) ? $element['default'] : ''; |
| 66 | $element['id'] = 'new_' . $element['id']; |
| 67 | $element['name'] = $name . '[{{{data.index}}}][' . $element['id'] . ']'; |
| 68 | $class_element = isset( $element['class_row'] ) ? $element['class_row'] : ''; |
| 69 | if ( ! empty( $element['deps']['id'] ) ) { |
| 70 | $element['deps']['id'] = 'new_' . $element['deps']['id']; |
| 71 | } |
| 72 | if ( ! empty( $element['deps']['target-id'] ) ) { |
| 73 | $element['deps']['target-id'] = 'new_' . $element['deps']['target-id']; |
| 74 | } |
| 75 | |
| 76 | if ( ! empty( $element['required'] ) ) { |
| 77 | $class_element .= ' yith-plugin-fw--required'; |
| 78 | } |
| 79 | ?> |
| 80 | <div class="yith-add-box-row yith-toggle-content-row <?php echo esc_attr( $class_element ); ?> <?php echo '{{{data.index}}}'; ?>" <?php echo yith_field_deps_data( $element ); ?>> |
| 81 | |
| 82 | <label for="<?php echo esc_attr( $element['id'] ); ?>"><?php echo esc_html( $element['title'] ); ?></label> |
| 83 | <div class="yith-plugin-fw-option-with-description"> |
| 84 | <?php yith_plugin_fw_get_field( $element, true ); ?> |
| 85 | <span class="description"><?php echo ! empty( $element['desc'] ) ? wp_kses_post( $element['desc'] ) : ''; ?></span> |
| 86 | </div> |
| 87 | </div> |
| 88 | <?php endforeach; ?> |
| 89 | |
| 90 | <?php if ( ! empty( $save_button ) ) : ?> |
| 91 | <div class="yith-add-box-buttons"> |
| 92 | <button class="button-primary yith-save-button"> |
| 93 | <?php echo esc_html( $save_button['name'] ); ?> |
| 94 | </button> |
| 95 | </div> |
| 96 | <?php endif; ?> |
| 97 | </script> |
| 98 | <?php endif; ?> |
| 99 | |
| 100 | <div class="yith-toggle-elements"> |
| 101 | <?php if ( $values ) : ?> |
| 102 | <?php foreach ( $values as $i => $value ) : ?> |
| 103 | <?php |
| 104 | $title_element = yith_format_toggle_title( $the_title, $value ); |
| 105 | $title_element = apply_filters( 'yith_plugin_fw_toggle_element_title_' . $field_id, $title_element, $elements, $value ); |
| 106 | $subtitle_element = yith_format_toggle_title( $subtitle, $value ); |
| 107 | $subtitle_element = apply_filters( 'yith_plugin_fw_toggle_element_subtitle_' . $field_id, $subtitle_element, $elements, $value ); |
| 108 | ?> |
| 109 | <div id="<?php echo esc_attr( $field_id ); ?>_<?php echo esc_attr( $i ); ?>" |
| 110 | class="yith-toggle-row <?php echo ! empty( $subtitle ) ? 'with-subtitle' : ''; ?> <?php echo esc_attr( $class ); ?>" |
| 111 | data-item_key="<?php echo esc_attr( $i ); ?>" |
| 112 | <?php yith_plugin_fw_html_attributes_to_string( $custom_attributes, true ); ?> |
| 113 | > |
| 114 | <div class="yith-toggle-title"> |
| 115 | <h3> |
| 116 | <span class="title" data-title_format="<?php echo esc_attr( $the_title ); ?>"><?php echo wp_kses_post( $title_element ); ?></span> |
| 117 | <?php if ( ! empty( $subtitle_element ) ) : ?> |
| 118 | <div class="subtitle" data-subtitle_format="<?php echo esc_attr( $subtitle ); ?>"><?php echo wp_kses_post( $subtitle_element ); ?></div> |
| 119 | <?php endif; ?> |
| 120 | </h3> |
| 121 | <span class="yith-toggle"><span class="yith-icon yith-icon-arrow_right ui-sortable-handle"></span></span> |
| 122 | <?php if ( ! empty( $onoff_field ) && is_array( $onoff_field ) ) : ?> |
| 123 | <?php |
| 124 | $onoff_field['value'] = isset( $value[ $onoff_id ] ) ? $value[ $onoff_id ] : ( isset( $onoff_field['default'] ) ? $onoff_field['default'] : '' ); |
| 125 | $onoff_field['type'] = 'onoff'; |
| 126 | $onoff_field['name'] = "{$name}[{$i}][{$onoff_id}]"; |
| 127 | $onoff_field['id'] = $onoff_id . '_' . $i; |
| 128 | unset( $onoff_field['yith-type'] ); |
| 129 | ?> |
| 130 | <span class="yith-toggle-onoff" |
| 131 | <?php if ( ! empty( $onoff_field['ajax_action'] ) ) : ?> |
| 132 | data-ajax_action="<?php echo esc_attr( $onoff_field['ajax_action'] ); ?>" |
| 133 | <?php endif ?> |
| 134 | > |
| 135 | <?php yith_plugin_fw_get_field( $onoff_field, true ); ?> |
| 136 | </span> |
| 137 | |
| 138 | <?php if ( $sortable ) : ?> |
| 139 | <span class="yith-icon yith-icon-drag"></span> |
| 140 | <?php endif ?> |
| 141 | <?php endif; ?> |
| 142 | </div> |
| 143 | <div class="yith-toggle-content"> |
| 144 | <?php if ( $elements && count( $elements ) > 0 ) : ?> |
| 145 | <?php foreach ( $elements as $element ) : ?> |
| 146 | <?php |
| 147 | $element['type'] = isset( $element['yith-type'] ) ? $element['yith-type'] : $element['type']; |
| 148 | unset( $element['yith-type'] ); |
| 149 | $element['title'] = $element['name']; |
| 150 | $element['name'] = $name . "[$i][" . $element['id'] . ']'; |
| 151 | $element['value'] = isset( $value[ $element['id'] ] ) ? $value[ $element['id'] ] : ( isset( $element['default'] ) ? $element['default'] : '' ); |
| 152 | $element['id'] = $element['id'] . '_' . $i; |
| 153 | $element['class_row'] = isset( $element['class_row'] ) ? $element['class_row'] : ''; |
| 154 | |
| 155 | if ( ! empty( $element['deps']['id'] ) ) { |
| 156 | $element['deps']['id'] = $element['deps']['id'] . '_' . $i; |
| 157 | } |
| 158 | if ( ! empty( $element['deps']['target-id'] ) ) { |
| 159 | $element['deps']['target-id'] = $element['deps']['target-id'] . '_' . $i; |
| 160 | } |
| 161 | |
| 162 | if ( ! empty( $element['required'] ) ) { |
| 163 | $element['class_row'] .= ' yith-plugin-fw--required'; |
| 164 | } |
| 165 | ?> |
| 166 | <div class="yith-toggle-content-row <?php echo esc_attr( $element['class_row'] . ' ' . $element['type'] ); ?>" <?php echo yith_field_deps_data( $element ); ?>> |
| 167 | <label for="<?php echo esc_attr( $element['id'] ); ?>"><?php echo esc_html( $element['title'] ); ?></label> |
| 168 | <div class="yith-plugin-fw-option-with-description"> |
| 169 | <?php yith_plugin_fw_get_field( $element, true ); ?> |
| 170 | <span class="description"><?php echo ! empty( $element['desc'] ) ? wp_kses_post( $element['desc'] ) : ''; ?></span> |
| 171 | </div> |
| 172 | </div> |
| 173 | <?php endforeach; ?> |
| 174 | <?php endif; ?> |
| 175 | <div class="yith-toggle-content-buttons"> |
| 176 | <div class="spinner"></div> |
| 177 | <?php if ( $save_button && ! empty( $save_button['id'] ) ) : ?> |
| 178 | <?php |
| 179 | $save_button_class = isset( $save_button['class'] ) ? $save_button['class'] : ''; |
| 180 | $save_button_name = isset( $save_button['name'] ) ? $save_button['name'] : ''; |
| 181 | ?> |
| 182 | <button id="<?php echo esc_attr( $save_button['id'] ); ?>" class="button-primary yith-save-button <?php echo esc_attr( $save_button_class ); ?>"> |
| 183 | <?php echo esc_html( $save_button_name ); ?> |
| 184 | </button> |
| 185 | <?php endif; ?> |
| 186 | <?php if ( $delete_button && ! empty( $delete_button['id'] ) ) : ?> |
| 187 | <?php |
| 188 | $delete_button_class = isset( $delete_button['class'] ) ? $delete_button['class'] : ''; |
| 189 | $delete_button_name = isset( $delete_button['name'] ) ? $delete_button['name'] : ''; |
| 190 | ?> |
| 191 | <button id="<?php echo esc_attr( $delete_button['id'] ); ?>" |
| 192 | class="button-secondary yith-delete-button <?php echo esc_attr( $delete_button_class ); ?>"> |
| 193 | <?php echo esc_html( $delete_button_name ); ?> |
| 194 | </button> |
| 195 | <?php endif; ?> |
| 196 | </div> |
| 197 | </div> |
| 198 | |
| 199 | </div> |
| 200 | <?php endforeach; ?> |
| 201 | <?php endif; ?> |
| 202 | </div> |
| 203 | <script type="text/template" id="tmpl-yith-toggle-element-item-<?php echo esc_attr( $field_id ); ?>"> |
| 204 | <div id="<?php echo esc_attr( $field_id ); ?>_{{{data.index}}}" |
| 205 | class="yith-toggle-row highlight <?php echo ! empty( $subtitle ) ? 'with-subtitle' : ''; ?> <?php echo esc_attr( $class ); ?>" |
| 206 | data-item_key="{{{data.index}}}" |
| 207 | <?php yith_plugin_fw_html_attributes_to_string( $custom_attributes, true ); ?> |
| 208 | > |
| 209 | <div class="yith-toggle-title"> |
| 210 | <h3> |
| 211 | <span class="title" data-title_format="<?php echo esc_attr( $the_title ); ?>"><?php echo wp_kses_post( $the_title ); ?></span> |
| 212 | <div class="subtitle" data-subtitle_format="<?php echo esc_attr( $subtitle ); ?>"><?php echo wp_kses_post( $subtitle ); ?></div> |
| 213 | </h3> |
| 214 | <span class="yith-toggle"><span class="yith-icon yith-icon-arrow_right"></span></span> |
| 215 | <?php if ( ! empty( $onoff_field ) && is_array( $onoff_field ) ) : ?> |
| 216 | <?php |
| 217 | $onoff_field['value'] = isset( $onoff_field['default'] ) ? $onoff_field['default'] : ''; |
| 218 | $onoff_field['type'] = 'onoff'; |
| 219 | $onoff_field['name'] = $name . '[{{{data.index}}}][' . $onoff_id . ']'; |
| 220 | $onoff_field['id'] = $onoff_id; |
| 221 | unset( $onoff_field['yith-type'] ); |
| 222 | ?> |
| 223 | <span class="yith-toggle-onoff" |
| 224 | <?php if ( ! empty( $onoff_field['ajax_action'] ) ) : ?> |
| 225 | data-ajax_action="<?php echo esc_attr( $onoff_field['ajax_action'] ); ?>" |
| 226 | <?php endif ?> |
| 227 | > |
| 228 | <?php yith_plugin_fw_get_field( $onoff_field, true ); ?> |
| 229 | </span> |
| 230 | <?php endif; ?> |
| 231 | <?php if ( $sortable ) : ?> |
| 232 | <span class="yith-icon yith-icon-drag ui-sortable-handle"></span> |
| 233 | <?php endif ?> |
| 234 | </div> |
| 235 | <div class="yith-toggle-content"> |
| 236 | <?php if ( $elements && count( $elements ) > 0 ) : ?> |
| 237 | <?php foreach ( $elements as $element ) : ?> |
| 238 | <?php |
| 239 | $element['type'] = isset( $element['yith-type'] ) ? $element['yith-type'] : $element['type']; |
| 240 | unset( $element['yith-type'] ); |
| 241 | $element['title'] = $element['name']; |
| 242 | $element['name'] = $name . '[{{{data.index}}}][' . $element['id'] . ']'; |
| 243 | $element['id'] = $element['id'] . '_{{{data.index}}}'; |
| 244 | $class_element = isset( $element['class_row'] ) ? $element['class_row'] : ''; |
| 245 | |
| 246 | if ( ! empty( $element['deps']['id'] ) ) { |
| 247 | $element['deps']['id'] = $element['deps']['id'] . '_{{{data.index}}}'; |
| 248 | } |
| 249 | if ( ! empty( $element['deps']['target-id'] ) ) { |
| 250 | $element['deps']['target-id'] = $element['deps']['target-id'] . '_{{{data.index}}}'; |
| 251 | } |
| 252 | |
| 253 | if ( ! empty( $element['required'] ) ) { |
| 254 | $class_element .= ' yith-plugin-fw--required'; |
| 255 | } |
| 256 | |
| 257 | ?> |
| 258 | <div class="yith-toggle-content-row <?php echo esc_attr( $class_element . ' ' . $element['type'] ); ?>" <?php echo yith_field_deps_data( $element ); ?>> |
| 259 | <label for="<?php echo esc_attr( $element['id'] ); ?>"><?php echo esc_html( $element['title'] ); ?></label> |
| 260 | <div class="yith-plugin-fw-option-with-description"> |
| 261 | <?php yith_plugin_fw_get_field( $element, true ); ?> |
| 262 | <span class="description"><?php echo ! empty( $element['desc'] ) ? wp_kses_post( $element['desc'] ) : ''; ?></span> |
| 263 | </div> |
| 264 | </div> |
| 265 | <?php endforeach; ?> |
| 266 | <?php endif; ?> |
| 267 | <div class="yith-toggle-content-buttons"> |
| 268 | <div class="spinner"></div> |
| 269 | <?php if ( $save_button && ! empty( $save_button['id'] ) ) : ?> |
| 270 | <?php |
| 271 | $save_button_class = isset( $save_button['class'] ) ? $save_button['class'] : ''; |
| 272 | $save_button_name = isset( $save_button['name'] ) ? $save_button['name'] : ''; |
| 273 | ?> |
| 274 | <button id="<?php echo esc_attr( $save_button['id'] ); ?>" class="yith-save-button <?php echo esc_attr( $save_button_class ); ?>"> |
| 275 | <?php echo esc_html( $save_button_name ); ?> |
| 276 | </button> |
| 277 | <?php endif; ?> |
| 278 | <?php if ( $delete_button && ! empty( $delete_button['id'] ) ) : ?> |
| 279 | <?php |
| 280 | $delete_button_class = isset( $delete_button['class'] ) ? $delete_button['class'] : ''; |
| 281 | $delete_button_name = isset( $delete_button['name'] ) ? $delete_button['name'] : ''; |
| 282 | ?> |
| 283 | <button id="<?php echo esc_attr( $delete_button['id'] ); ?>" class="button-secondary yith-delete-button <?php echo esc_attr( $delete_button_class ); ?>"> |
| 284 | <?php echo esc_html( $delete_button_name ); ?> |
| 285 | </button> |
| 286 | <?php endif; ?> |
| 287 | </div> |
| 288 | </div> |
| 289 | </div> |
| 290 | </script> |
| 291 | </div> |
| 292 |