diff --git a/assets/js/admin/wc-enhanced-select.js b/assets/js/admin/wc-enhanced-select.js index 4eac44f6371..843ce80640c 100644 --- a/assets/js/admin/wc-enhanced-select.js +++ b/assets/js/admin/wc-enhanced-select.js @@ -72,6 +72,49 @@ jQuery( function( $ ) { $( this ).selectWoo( select2_args ).addClass( 'enhanced' ); }); + function display_result( self, select2_args ) { + select2_args = $.extend( select2_args, getEnhancedSelectFormatString() ); + + $( self ).selectWoo( select2_args ).addClass( 'enhanced' ); + + if ( $( self ).data( 'sortable' ) ) { + var $select = $(self); + var $list = $( self ).next( '.select2-container' ).find( 'ul.select2-selection__rendered' ); + + $list.sortable({ + placeholder : 'ui-state-highlight select2-selection__choice', + forcePlaceholderSize: true, + items : 'li:not(.select2-search__field)', + tolerance : 'pointer', + stop: function() { + $( $list.find( '.select2-selection__choice' ).get().reverse() ).each( function() { + var id = $( self ).data( 'data' ).id; + var option = $select.find( 'option[value="' + id + '"]' )[0]; + $select.prepend( option ); + } ); + } + }); + // Keep multiselects ordered alphabetically if they are not sortable. + } else if ( $( self ).prop( 'multiple' ) ) { + $( self ).on( 'change', function(){ + var $children = $( self ).children(); + $children.sort(function(a, b){ + var atext = a.text.toLowerCase(); + var btext = b.text.toLowerCase(); + + if ( atext > btext ) { + return 1; + } + if ( atext < btext ) { + return -1; + } + return 0; + }); + $( self ).html( $children ); + }); + } + } + // Ajax product search box $( ':input.wc-product-search' ).filter( ':not(.enhanced)' ).each( function() { var select2_args = { @@ -112,46 +155,48 @@ jQuery( function( $ ) { } }; - select2_args = $.extend( select2_args, getEnhancedSelectFormatString() ); + display_result( this, select2_args ); + }); + + // Ajax Page Search. + $( ':input.wc-page-search' ).filter( ':not(.enhanced)' ).each( function() { + var select2_args = { + allowClear: $( this ).data( 'allow_clear' ) ? true : false, + placeholder: $( this ).data( 'placeholder' ), + minimumInputLength: $( this ).data( 'minimum_input_length' ) ? $( this ).data( 'minimum_input_length' ) : '3', + escapeMarkup: function( m ) { + return m; + }, + ajax: { + url: wc_enhanced_select_params.ajax_url, + dataType: 'json', + delay: 250, + data: function( params ) { + return { + term : params.term, + action : $( this ).data( 'action' ) || 'woocommerce_json_search_pages', + security : wc_enhanced_select_params.search_pages_nonce, + exclude : $( this ).data( 'exclude' ), + post_status : $( this ).data( 'post_status' ), + limit : $( this ).data( 'limit' ), + }; + }, + processResults: function( data ) { + var terms = []; + if ( data ) { + $.each( data, function( id, text ) { + terms.push( { id: id, text: text } ); + } ); + } + return { + results: terms + }; + }, + cache: true + } + }; $( this ).selectWoo( select2_args ).addClass( 'enhanced' ); - - if ( $( this ).data( 'sortable' ) ) { - var $select = $(this); - var $list = $( this ).next( '.select2-container' ).find( 'ul.select2-selection__rendered' ); - - $list.sortable({ - placeholder : 'ui-state-highlight select2-selection__choice', - forcePlaceholderSize: true, - items : 'li:not(.select2-search__field)', - tolerance : 'pointer', - stop: function() { - $( $list.find( '.select2-selection__choice' ).get().reverse() ).each( function() { - var id = $( this ).data( 'data' ).id; - var option = $select.find( 'option[value="' + id + '"]' )[0]; - $select.prepend( option ); - } ); - } - }); - // Keep multiselects ordered alphabetically if they are not sortable. - } else if ( $( this ).prop( 'multiple' ) ) { - $( this ).on( 'change', function(){ - var $children = $( this ).children(); - $children.sort(function(a, b){ - var atext = a.text.toLowerCase(); - var btext = b.text.toLowerCase(); - - if ( atext > btext ) { - return 1; - } - if ( atext < btext ) { - return -1; - } - return 0; - }); - $( this ).html( $children ); - }); - } }); // Ajax customer search boxes diff --git a/includes/admin/class-wc-admin-assets.php b/includes/admin/class-wc-admin-assets.php index 567e2efe7ca..01e62199ba5 100644 --- a/includes/admin/class-wc-admin-assets.php +++ b/includes/admin/class-wc-admin-assets.php @@ -145,6 +145,7 @@ if ( ! class_exists( 'WC_Admin_Assets', false ) ) : 'search_products_nonce' => wp_create_nonce( 'search-products' ), 'search_customers_nonce' => wp_create_nonce( 'search-customers' ), 'search_categories_nonce' => wp_create_nonce( 'search-categories' ), + 'search_pages_nonce' => wp_create_nonce( 'search-pages' ), ) ); diff --git a/includes/admin/class-wc-admin-settings.php b/includes/admin/class-wc-admin-settings.php index b36d9a889d6..69506066749 100644 --- a/includes/admin/class-wc-admin-settings.php +++ b/includes/admin/class-wc-admin-settings.php @@ -579,6 +579,47 @@ if ( ! class_exists( 'WC_Admin_Settings', false ) ) : post_title, + $option_value + ); + } + ?> +