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