Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IP exclude rules settings #1036

Merged
merged 10 commits into from
Sep 26, 2019
8 changes: 4 additions & 4 deletions classes/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,12 +551,12 @@ public function sanitize_settings( $input ) {
// Support all values in multidimentional arrays too.
array_walk_recursive(
$output[ $name ],
function ( &$v, $k ) {
$v = trim( $v );
function ( &$v ) {
$v = sanitize_text_field( trim( $v ) );
}
);
} else {
$output[ $name ] = trim( $input[ $name ] );
$output[ $name ] = sanitize_text_field( trim( $input[ $name ] ) );
}
}
}
Expand Down Expand Up @@ -843,7 +843,7 @@ public function render_field( $field ) {
$exclude_rows = array();

// Prepend an empty row.
$current_value['exclude_row'] = array( 'helper' => '' ) + ( isset( $current_value['exclude_row'] ) ? $current_value['exclude_row'] : array() );
$current_value['exclude_row'] = ( isset( $current_value['exclude_row'] ) ? $current_value['exclude_row'] : array() ) + array( 'helper' => '' );

foreach ( $current_value['exclude_row'] as $key => $value ) {
// Prepare values.
Expand Down
46 changes: 21 additions & 25 deletions ui/js/exclude.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/* globals jQuery, ajaxurl, wp_stream_regenerate_alt_rows */
jQuery(
function( $ ) {
var initSettingsSelect2 = function() {
var $excludeRows = $( '.stream-exclude-list tbody tr:not(.hidden)' );
var $placeholderRow = $( '.stream-exclude-list tr.helper' );

var initSettingsSelect2 = function( $rowsWithSelect2 ) {
var $input_user;

$( '.stream-exclude-list tr:not(.hidden) select.select2-select.connector_or_context' ).each(
$( 'select.select2-select.connector_or_context', $rowsWithSelect2 ).each(
function( k, el ) {
$( el ).select2(
{
Expand Down Expand Up @@ -64,7 +67,7 @@ jQuery(
}
);

$( '.stream-exclude-list tr:not(.hidden) select.select2-select.action' ).each(
$( 'select.select2-select.action', $rowsWithSelect2 ).each(
function( k, el ) {
$( el ).select2(
{
Expand All @@ -74,7 +77,7 @@ jQuery(
}
);

$( '.stream-exclude-list tr:not(.hidden) select.select2-select.author_or_role' ).each(
$( 'select.select2-select.author_or_role', $rowsWithSelect2 ).each(
function( k, el ) {
$input_user = $( el );

Expand Down Expand Up @@ -173,7 +176,7 @@ jQuery(
}
);

$( '.stream-exclude-list tr:not(.hidden) select.select2-select.ip_address' ).each(
$( 'select.select2-select.ip_address', $rowsWithSelect2 ).each(
function( k, el ) {
var $input_ip = $( el ),
searchTerm = '';
Expand Down Expand Up @@ -271,7 +274,7 @@ jQuery(
}
);

$( '.stream-exclude-list tr:not(.hidden) .exclude_rules_remove_rule_row' ).on(
$( '.exclude_rules_remove_rule_row', $rowsWithSelect2 ).on(
'click', function() {
var $thisRow = $( this ).closest( 'tr' );

Expand All @@ -283,16 +286,16 @@ jQuery(
);
};

initSettingsSelect2();
initSettingsSelect2( $excludeRows );

$( '.stream-exclude-list tr:not(.hidden) select.select2-select.author_or_role' ).each(
$( 'select.select2-select.author_or_role', $excludeRows ).each(
function() {
var $option = $( '<option selected>' + $( this ).data( 'selected-text' ) + '</option>' ).val( $( this ).data( 'selected-id' ) );
$( this ).append( $option ).trigger( 'change' );
}
);

$( '.stream-exclude-list tr:not(.hidden) select.select2-select.connector_or_context' ).each(
$( 'select.select2-select.connector_or_context', $excludeRows ).each(
function() {
var parts = [
$( this ).siblings( '.connector' ).val(),
Expand All @@ -307,25 +310,12 @@ jQuery(

$( '#exclude_rules_new_rule' ).on(
'click', function() {
var $excludeList = $( 'table.stream-exclude-list' );

$( 'tr:not(.hidden) select.select2-select', $excludeList ).each(
function() {
$( this ).select2( 'destroy' );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't recreate the select2 fields for rows with them already present.

}
);

var $lastRow = $( 'tr', $excludeList ).last(),
$newRow = $lastRow.clone();
var $newRow = $placeholderRow.clone();

$newRow.removeAttr( 'class' );
$( '.stream-exclude-list tbody :input' ).off();
$( ':input', $newRow ).off().val( '' );

$lastRow.after( $newRow );

initSettingsSelect2();
$newRow.insertBefore( $placeholderRow );

initSettingsSelect2( $newRow );
recalculate_rules_found();
recalculate_rules_selected();
}
Expand Down Expand Up @@ -369,6 +359,12 @@ jQuery(
$( '.stream-exclude-list tbody tr:not(.hidden) select.select2-select.ip_address', this ).each(
function() {
var firstSelected = $( 'option:selected', this ).first();

// Ugly hack to ensure we always pass an empty value or the order of rows gets messed up.
if ( ! firstSelected.length ) {
$( this ).append( '<option selected="selected"></option>' );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the core fix -- we add a dummy option and mark it selected to ensure it gets delivered to the backend.

}

$( 'option:selected:not(:first)', this ).each(
function() {
firstSelected.attr( 'value', firstSelected.attr( 'value' ) + ',' + $( this ).attr( 'value' ) );
Expand Down