| @@ -32,8 +32,11 @@ | ||
| 32 | 32 | |
| 33 | 33 | // Delete. |
| 34 | 34 | add_action( 'wp_ajax_wpsc_delete_tff', array( __CLASS__, 'delete_tff' ) ); |
| 35 | 35 | |
| 36 | + // Sorting. | |
| 37 | + add_action( 'wp_ajax_wpsc_set_tff_load_order', array( __CLASS__, 'set_load_order' ) ); | |
| 38 | + | |
| 36 | 39 | // Remove if custom field deleted. |
| 37 | 40 | add_action( 'wpsc_delete_custom_field', array( __CLASS__, 'delete_custom_field' ), 10, 1 ); |
| 38 | 41 | |
| 39 | 42 | // visibility conditions filter. |
| @@ -51,8 +54,13 @@ | ||
| 51 | 54 | wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 52 | 55 | } |
| 53 | 56 | |
| 54 | 57 | $tff = get_option( 'wpsc-tff', array() ); |
| 58 | + $width = array( | |
| 59 | + '1/3' => __( '1/3rd of row', 'supportcandy' ), | |
| 60 | + 'half' => __( 'Half width', 'supportcandy' ), | |
| 61 | + 'full' => __( 'Full width', 'supportcandy' ), | |
| 62 | + ); | |
| 55 | 63 | ?> |
| 56 | 64 | |
| 57 | 65 | <div class="wpsc-setting-header"> |
| 58 | 66 | <h2><?php esc_attr_e( 'Ticket Form Fields', 'supportcandy' ); ?></h2> |
| @@ -69,9 +77,13 @@ | ||
| 69 | 77 | </div> |
| 70 | 78 | <table class="form-fields wpsc-setting-tbl"> |
| 71 | 79 | <thead> |
| 72 | 80 | <tr> |
| 81 | + <th style="width: 30px;"><?php esc_attr_e( 'Sort', 'supportcandy' ); ?></th> | |
| 73 | 82 | <th><?php esc_attr_e( 'Field', 'supportcandy' ); ?></th> |
| 83 | + <th><?php esc_attr_e( 'Required', 'supportcandy' ); ?></th> | |
| 84 | + <th><?php esc_attr_e( 'Visibility Conditions', 'supportcandy' ); ?></th> | |
| 85 | + <th><?php esc_attr_e( 'Width', 'supportcandy' ); ?></th> | |
| 74 | 86 | <th><?php esc_attr_e( 'Actions', 'supportcandy' ); ?></th> |
| 75 | 87 | </tr> |
| 76 | 88 | </thead> |
| 77 | 89 | <tbody> |
| @@ -80,11 +92,17 @@ | ||
| 80 | 92 | $cf = WPSC_Custom_Field::get_cf_by_slug( $slug ); |
| 81 | 93 | if ( ! $cf ) { |
| 82 | 94 | continue; |
| 83 | 95 | } |
| 96 | + $vis_decoded = is_string( $settings['visibility'] ) ? json_decode( $settings['visibility'], true ) : $settings['visibility']; | |
| 97 | + $visibility = ( ! empty( $vis_decoded ) ) ? __( 'Yes', 'supportcandy' ) : __( 'No', 'supportcandy' ); | |
| 84 | 98 | ?> |
| 85 | - <tr> | |
| 99 | + <tr data-id="<?php echo esc_attr( $slug ); ?>"> | |
| 100 | + <td class="sort-handle"><?php WPSC_Icons::get( 'sort' ); ?></td> | |
| 86 | 101 | <td><?php echo esc_attr( $cf->name ); ?></td> |
| 102 | + <td><?php echo esc_attr( $settings['is-required'] ? __( 'Yes', 'supportcandy' ) : __( 'No', 'supportcandy' ) ); ?></td> | |
| 103 | + <td><?php echo esc_attr( $visibility ); ?></td> | |
| 104 | + <td><?php echo esc_attr( isset( $width[ $settings['width'] ] ) ? $width[ $settings['width'] ] : $settings['width'] ); ?></td> | |
| 87 | 105 | <td> |
| 88 | 106 | <span class="wpsc-link" onclick="wpsc_get_edit_tff(<?php echo esc_attr( $cf->id ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_edit_tff' ) ); ?>');"><?php esc_attr_e( 'Edit', 'supportcandy' ); ?></span> |
| 89 | 107 | <?php |
| 90 | 108 | if ( ! ( $cf->slug == 'name' || $cf->slug == 'email' ) ) : |
| @@ -104,11 +122,13 @@ | ||
| 104 | 122 | </div> |
| 105 | 123 | <script> |
| 106 | 124 | jQuery('table.form-fields').DataTable({ |
| 107 | 125 | ordering: false, |
| 126 | + autoWidth: false, | |
| 108 | 127 | pageLength: 20, |
| 109 | 128 | bLengthChange: false, |
| 110 | - columnDefs: [ | |
| 129 | + columnDefs: [ | |
| 130 | + { targets: 0, width: '30px', searchable: false }, | |
| 111 | 131 | { targets: -1, searchable: false }, |
| 112 | 132 | { targets: '_all', className: 'dt-left' } |
| 113 | 133 | ], |
| 114 | 134 | layout: { |
| @@ -136,8 +156,37 @@ | ||
| 136 | 156 | }, |
| 137 | 157 | }, |
| 138 | 158 | language: supportcandy.translations.datatables |
| 139 | 159 | }); |
| 160 | + | |
| 161 | + jQuery(function() { | |
| 162 | + // Enable sorting with jQuery UI. | |
| 163 | + jQuery('table.form-fields tbody').sortable({ | |
| 164 | + handle: '.sort-handle', | |
| 165 | + helper: function(e, tr) { | |
| 166 | + var $originals = tr.children(); | |
| 167 | + var $helper = tr.clone(); | |
| 168 | + $helper.children().each(function(index) { | |
| 169 | + jQuery(this).width($originals.eq(index).width()); | |
| 170 | + }); | |
| 171 | + return $helper; | |
| 172 | + }, | |
| 173 | + update: function(event, ui) { | |
| 174 | + var slugs = jQuery(this).sortable('toArray', { attribute: 'data-id' }); | |
| 175 | + jQuery.post( | |
| 176 | + supportcandy.ajax_url, | |
| 177 | + { | |
| 178 | + action: 'wpsc_set_tff_load_order', | |
| 179 | + _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_set_tff_load_order' ) ); ?>', | |
| 180 | + slugs: slugs | |
| 181 | + }, | |
| 182 | + function(response) { | |
| 183 | + | |
| 184 | + } | |
| 185 | + ); | |
| 186 | + } | |
| 187 | + }); | |
| 188 | + }); | |
| 140 | 189 | </script> |
| 141 | 190 | <?php |
| 142 | 191 | wp_die(); |
| 143 | 192 | } |
| @@ -562,8 +611,47 @@ | ||
| 562 | 611 | } |
| 563 | 612 | |
| 564 | 613 | update_option( 'wpsc-tff', $tff ); |
| 565 | 614 | wp_die(); |
| 615 | + } | |
| 616 | + | |
| 617 | + /** | |
| 618 | + * Set load order | |
| 619 | + * | |
| 620 | + * @return void | |
| 621 | + */ | |
| 622 | + public static function set_load_order() { | |
| 623 | + | |
| 624 | + if ( check_ajax_referer( 'wpsc_set_tff_load_order', '_ajax_nonce', false ) != 1 ) { | |
| 625 | + wp_send_json_error( 'Unauthorized request!', 400 ); | |
| 626 | + } | |
| 627 | + | |
| 628 | + if ( ! WPSC_Functions::is_site_admin() ) { | |
| 629 | + wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); | |
| 630 | + } | |
| 631 | + | |
| 632 | + $slugs = isset( $_POST['slugs'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['slugs'] ) ) : array(); | |
| 633 | + if ( ! $slugs ) { | |
| 634 | + wp_send_json_error( 'Bad Request', 400 ); | |
| 635 | + } | |
| 636 | + | |
| 637 | + $tff = get_option( 'wpsc-tff', array() ); | |
| 638 | + $new_tff = array(); | |
| 639 | + foreach ( $slugs as $slug ) { | |
| 640 | + if ( isset( $tff[ $slug ] ) ) { | |
| 641 | + $new_tff[ $slug ] = $tff[ $slug ]; | |
| 642 | + } | |
| 643 | + } | |
| 644 | + | |
| 645 | + // Append any remaining fields (safety net) that were not part of the posted order. | |
| 646 | + foreach ( $tff as $slug => $field ) { | |
| 647 | + if ( ! isset( $new_tff[ $slug ] ) ) { | |
| 648 | + $new_tff[ $slug ] = $field; | |
| 649 | + } | |
| 650 | + } | |
| 651 | + | |
| 652 | + update_option( 'wpsc-tff', $new_tff ); | |
| 653 | + wp_send_json( array( 'success' => true ), 200 ); | |
| 566 | 654 | } |
| 567 | 655 | |
| 568 | 656 | /** |
| 569 | 657 | * Delete form field |