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 + ); + } + ?> + + + + + + + + + sprintf( __( 'Page contents: [%s]', 'woocommerce' ), apply_filters( 'woocommerce_cart_shortcode_tag', 'woocommerce_cart' ) ), 'id' => 'woocommerce_cart_page_id', - 'type' => 'single_select_page', + 'type' => 'single_select_page_with_search', 'default' => '', - 'class' => 'wc-enhanced-select-nostd', + 'class' => 'wc-page-search', 'css' => 'min-width:300px;', 'args' => array( 'exclude' => @@ -94,9 +94,9 @@ class WC_Settings_Advanced extends WC_Settings_Page { /* Translators: %s Page contents. */ 'desc' => sprintf( __( 'Page contents: [%s]', 'woocommerce' ), apply_filters( 'woocommerce_checkout_shortcode_tag', 'woocommerce_checkout' ) ), 'id' => 'woocommerce_checkout_page_id', - 'type' => 'single_select_page', - 'default' => '', - 'class' => 'wc-enhanced-select-nostd', + 'type' => 'single_select_page_with_search', + 'default' => wc_get_page_id( 'checkout' ), + 'class' => 'wc-page-search', 'css' => 'min-width:300px;', 'args' => array( 'exclude' => @@ -114,9 +114,9 @@ class WC_Settings_Advanced extends WC_Settings_Page { /* Translators: %s Page contents. */ 'desc' => sprintf( __( 'Page contents: [%s]', 'woocommerce' ), apply_filters( 'woocommerce_my_account_shortcode_tag', 'woocommerce_my_account' ) ), 'id' => 'woocommerce_myaccount_page_id', - 'type' => 'single_select_page', + 'type' => 'single_select_page_with_search', 'default' => '', - 'class' => 'wc-enhanced-select-nostd', + 'class' => 'wc-page-search', 'css' => 'min-width:300px;', 'args' => array( 'exclude' => @@ -134,9 +134,9 @@ class WC_Settings_Advanced extends WC_Settings_Page { 'desc' => __( 'If you define a "Terms" page the customer will be asked if they accept them when checking out.', 'woocommerce' ), 'id' => 'woocommerce_terms_page_id', 'default' => '', - 'class' => 'wc-enhanced-select-nostd', + 'class' => 'wc-page-search', 'css' => 'min-width:300px;', - 'type' => 'single_select_page', + 'type' => 'single_select_page_with_search', 'args' => array( 'exclude' => wc_get_page_id( 'checkout' ) ), 'desc_tip' => true, 'autoload' => false, diff --git a/includes/class-wc-ajax.php b/includes/class-wc-ajax.php index 5dbd366f4a3..bf7025f1fd0 100644 --- a/includes/class-wc-ajax.php +++ b/includes/class-wc-ajax.php @@ -155,6 +155,7 @@ class WC_AJAX { 'json_search_downloadable_products_and_variations', 'json_search_customers', 'json_search_categories', + 'json_search_pages', 'term_ordering', 'product_ordering', 'refund_line_items', @@ -1766,6 +1767,47 @@ class WC_AJAX { wp_send_json( apply_filters( 'woocommerce_json_search_found_categories', $found_categories ) ); } + /** + * Ajax request handling for page searching. + */ + public static function json_search_pages() { + ob_start(); + + check_ajax_referer( 'search-pages', 'security' ); + + if ( ! current_user_can( 'manage_woocommerce' ) ) { + wp_die( -1 ); + } + + $search_text = isset( $_GET['term'] ) ? wc_clean( wp_unslash( $_GET['term'] ) ) : ''; + $limit = isset( $_GET['limit'] ) ? absint( wp_unslash( $_GET['limit'] ) ) : -1; + $exclude_ids = ! empty( $_GET['exclude'] ) ? array_map( 'absint', (array) wp_unslash( $_GET['exclude'] ) ) : array(); + + $args = array( + 'no_found_rows' => true, + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'posts_per_page' => $limit, + 'post_type' => 'page', + 'post_status' => array( 'publish', 'private', 'draft' ), + 's' => $search_text, + 'post__not_in' => $exclude_ids, + ); + $search_results_query = new WP_Query( $args ); + + $pages_results = array(); + foreach ( $search_results_query->get_posts() as $post ) { + $pages_results[ $post->ID ] = sprintf( + /* translators: 1: page name 2: page ID */ + __( '%1$s (ID: %2$s)', 'woocommerce' ), + get_the_title( $post ), + $post->ID + ); + } + + wp_send_json( apply_filters( 'woocommerce_json_search_found_pages', $pages_results ) ); + } + /** * Ajax request handling for categories ordering. */