| @@ -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. |
| @@ -74,8 +77,9 @@ | ||
| 74 | 77 | </div> |
| 75 | 78 | <table class="form-fields wpsc-setting-tbl"> |
| 76 | 79 | <thead> |
| 77 | 80 | <tr> |
| 81 | + <th style="width: 30px;"><?php esc_attr_e( 'Sort', 'supportcandy' ); ?></th> | |
| 78 | 82 | <th><?php esc_attr_e( 'Field', 'supportcandy' ); ?></th> |
| 79 | 83 | <th><?php esc_attr_e( 'Required', 'supportcandy' ); ?></th> |
| 80 | 84 | <th><?php esc_attr_e( 'Visibility Conditions', 'supportcandy' ); ?></th> |
| 81 | 85 | <th><?php esc_attr_e( 'Width', 'supportcandy' ); ?></th> |
| @@ -91,9 +95,10 @@ | ||
| 91 | 95 | } |
| 92 | 96 | $vis_decoded = is_string( $settings['visibility'] ) ? json_decode( $settings['visibility'], true ) : $settings['visibility']; |
| 93 | 97 | $visibility = ( ! empty( $vis_decoded ) ) ? __( 'Yes', 'supportcandy' ) : __( 'No', 'supportcandy' ); |
| 94 | 98 | ?> |
| 95 | - <tr> | |
| 99 | + <tr data-id="<?php echo esc_attr( $slug ); ?>"> | |
| 100 | + <td class="sort-handle"><?php WPSC_Icons::get( 'sort' ); ?></td> | |
| 96 | 101 | <td><?php echo esc_attr( $cf->name ); ?></td> |
| 97 | 102 | <td><?php echo esc_attr( $settings['is-required'] ? __( 'Yes', 'supportcandy' ) : __( 'No', 'supportcandy' ) ); ?></td> |
| 98 | 103 | <td><?php echo esc_attr( $visibility ); ?></td> |
| 99 | 104 | <td><?php echo esc_attr( isset( $width[ $settings['width'] ] ) ? $width[ $settings['width'] ] : $settings['width'] ); ?></td> |
| @@ -117,11 +122,13 @@ | ||
| 117 | 122 | </div> |
| 118 | 123 | <script> |
| 119 | 124 | jQuery('table.form-fields').DataTable({ |
| 120 | 125 | ordering: false, |
| 126 | + autoWidth: false, | |
| 121 | 127 | pageLength: 20, |
| 122 | 128 | bLengthChange: false, |
| 123 | - columnDefs: [ | |
| 129 | + columnDefs: [ | |
| 130 | + { targets: 0, width: '30px', searchable: false }, | |
| 124 | 131 | { targets: -1, searchable: false }, |
| 125 | 132 | { targets: '_all', className: 'dt-left' } |
| 126 | 133 | ], |
| 127 | 134 | layout: { |
| @@ -149,8 +156,37 @@ | ||
| 149 | 156 | }, |
| 150 | 157 | }, |
| 151 | 158 | language: supportcandy.translations.datatables |
| 152 | 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 | + }); | |
| 153 | 189 | </script> |
| 154 | 190 | <?php |
| 155 | 191 | wp_die(); |
| 156 | 192 | } |
| @@ -575,8 +611,47 @@ | ||
| 575 | 611 | } |
| 576 | 612 | |
| 577 | 613 | update_option( 'wpsc-tff', $tff ); |
| 578 | 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 ); | |
| 579 | 654 | } |
| 580 | 655 | |
| 581 | 656 | /** |
| 582 | 657 | * Delete form field |