| @@ -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,18 +92,24 @@ | ||
| 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 | - <a href="javascript:wpsc_get_edit_tff(<?php echo esc_attr( $cf->id ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_edit_tff' ) ); ?>');" class="wpsc-link"><?php esc_attr_e( 'Edit', 'supportcandy' ); ?></a> | |
| 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' ) ) : |
| 91 | 109 | echo esc_attr( ' | ' ); |
| 92 | 110 | ?> |
| 93 | - <a href="javascript:wpsc_delete_tff(<?php echo esc_attr( $cf->id ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_delete_tff' ) ); ?>');" class="wpsc-link"><?php esc_attr_e( 'Delete', 'supportcandy' ); ?></a> | |
| 111 | + <span class="wpsc-link" onclick="wpsc_delete_tff(<?php echo esc_attr( $cf->id ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_delete_tff' ) ); ?>');"><?php esc_attr_e( 'Delete', 'supportcandy' ); ?></span> | |
| 94 | 112 | <?php |
| 95 | 113 | endif; |
| 96 | 114 | ?> |
| 97 | 115 | </td> |
| @@ -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 | } |
| @@ -287,9 +336,9 @@ | ||
| 287 | 336 | */ |
| 288 | 337 | public static function set_add_new_tff() { |
| 289 | 338 | |
| 290 | 339 | if ( check_ajax_referer( 'wpsc_set_add_new_tff', '_ajax_nonce', false ) != 1 ) { |
| 291 | - wp_send_json_error( 'Unauthorised request!', 401 ); | |
| 340 | + wp_send_json_error( 'Unauthorized request!', 401 ); | |
| 292 | 341 | } |
| 293 | 342 | |
| 294 | 343 | if ( ! WPSC_Functions::is_site_admin() ) { |
| 295 | 344 | wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| @@ -372,9 +421,9 @@ | ||
| 372 | 421 | */ |
| 373 | 422 | public static function get_edit_tff() { |
| 374 | 423 | |
| 375 | 424 | if ( check_ajax_referer( 'wpsc_get_edit_tff', '_ajax_nonce', false ) != 1 ) { |
| 376 | - wp_send_json_error( 'Unauthorised request!', 401 ); | |
| 425 | + wp_send_json_error( 'Unauthorized request!', 401 ); | |
| 377 | 426 | } |
| 378 | 427 | |
| 379 | 428 | if ( ! WPSC_Functions::is_site_admin() ) { |
| 380 | 429 | wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| @@ -495,9 +544,9 @@ | ||
| 495 | 544 | */ |
| 496 | 545 | public static function set_edit_tff() { |
| 497 | 546 | |
| 498 | 547 | if ( check_ajax_referer( 'wpsc_set_edit_tff', '_ajax_nonce', false ) != 1 ) { |
| 499 | - wp_send_json_error( 'Unauthorised request!', 401 ); | |
| 548 | + wp_send_json_error( 'Unauthorized request!', 401 ); | |
| 500 | 549 | } |
| 501 | 550 | |
| 502 | 551 | if ( ! WPSC_Functions::is_site_admin() ) { |
| 503 | 552 | wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| @@ -565,8 +614,47 @@ | ||
| 565 | 614 | wp_die(); |
| 566 | 615 | } |
| 567 | 616 | |
| 568 | 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 ); | |
| 654 | + } | |
| 655 | + | |
| 656 | + /** | |
| 569 | 657 | * Delete form field |
| 570 | 658 | * |
| 571 | 659 | * @return void |
| 572 | 660 | */ |
| @@ -572,9 +660,9 @@ | ||
| 572 | 660 | */ |
| 573 | 661 | public static function delete_tff() { |
| 574 | 662 | |
| 575 | 663 | if ( check_ajax_referer( 'wpsc_delete_tff', '_ajax_nonce', false ) != 1 ) { |
| 576 | - wp_send_json_error( 'Unauthorised request!', 401 ); | |
| 664 | + wp_send_json_error( 'Unauthorized request!', 401 ); | |
| 577 | 665 | } |
| 578 | 666 | |
| 579 | 667 | if ( ! WPSC_Functions::is_site_admin() ) { |
| 580 | 668 | wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| @@ -581,9 +669,9 @@ | ||
| 581 | 669 | } |
| 582 | 670 | |
| 583 | 671 | $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0; |
| 584 | 672 | if ( ! $id ) { |
| 585 | - wp_send_json_error( 'Unauthorised request!', 401 ); | |
| 673 | + wp_send_json_error( 'Unauthorized request!', 401 ); | |
| 586 | 674 | } |
| 587 | 675 | |
| 588 | 676 | $tff = get_option( 'wpsc-tff', array() ); |
| 589 | 677 | |
| @@ -588,9 +676,9 @@ | ||
| 588 | 676 | $tff = get_option( 'wpsc-tff', array() ); |
| 589 | 677 | |
| 590 | 678 | $cf = new WPSC_Custom_Field( $id ); |
| 591 | 679 | if ( ! $cf->id || ! $cf->type::$is_ctf || ! in_array( $cf->field, array( 'customer', 'ticket' ) ) || ! isset( $tff[ $cf->slug ] ) ) { |
| 592 | - wp_send_json_error( 'Unauthorised request!', 401 ); | |
| 680 | + wp_send_json_error( 'Unauthorized request!', 401 ); | |
| 593 | 681 | } |
| 594 | 682 | |
| 595 | 683 | unset( $tff[ $cf->slug ] ); |
| 596 | 684 | update_option( 'wpsc-tff', $tff ); |