Merge pull request #29181 from woocommerce/fix/27233

Use AJAX for Page Configuration
This commit is contained in:
jonathansadowski 2021-03-23 11:46:00 -05:00 committed by GitHub
commit 2e60d47a01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 176 additions and 47 deletions

View File

@ -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

View File

@ -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' ),
)
);

View File

@ -579,6 +579,47 @@ if ( ! class_exists( 'WC_Admin_Settings', false ) ) :
<?php
break;
case 'single_select_page_with_search':
$option_value = $value['value'];
$page = get_post( $option_value );
if ( ! is_null( $page ) ) {
$page = get_post( $option_value );
$option_display_name = sprintf(
/* translators: 1: page name 2: page ID */
__( '%1$s (ID: %2$s)', 'woocommerce' ),
$page->post_title,
$option_value
);
}
?>
<tr valign="top" class="single_select_page">
<th scope="row" class="titledesc">
<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php echo $tooltip_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></label>
</th>
<td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>">
<select
name="<?php echo esc_attr( $value['id'] ); ?>"
id="<?php echo esc_attr( $value['id'] ); ?>"
style="<?php echo esc_attr( $value['css'] ); ?>"
class="<?php echo esc_attr( $value['class'] ); ?>"
<?php echo implode( ' ', $custom_attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
data-placeholder="<?php esc_attr_e( 'Search for a page&hellip;', 'woocommerce' ); ?>"
data-allow_clear="true"
data-exclude="<?php echo wc_esc_json( wp_json_encode( $value['args']['exclude'] ) ); ?>"
>
<option value=""></option>
<?php if ( ! is_null( $page ) ) { ?>
<option value="<?php echo esc_attr( $option_value ); ?>" selected="selected">
<?php echo wp_strip_all_tags( $option_display_name ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</option>
<?php } ?>
</select> <?php echo $description; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</td>
</tr>
<?php
break;
// Single country selects.
case 'single_select_country':
$country_setting = (string) $value['value'];

View File

@ -74,9 +74,9 @@ class WC_Settings_Advanced extends WC_Settings_Page {
/* Translators: %s Page contents. */
'desc' => 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,

View File

@ -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.
*/