Skip to content

Commit

Permalink
Merge pull request #345 from x-team/issue-335
Browse files Browse the repository at this point in the history
Issue 335: Autocomplete not working correctly in Exclude IP Addressees field
  • Loading branch information
frankiejarrett committed Mar 19, 2014
2 parents a4cfc25 + 7e040a5 commit 22ee462
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 13 deletions.
49 changes: 42 additions & 7 deletions includes/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public static function load() {
// Ajax callback function to search users
add_action( 'wp_ajax_stream_get_users', array( __CLASS__, 'get_users' ) );

// Ajax callback function to search IPs
add_action( 'wp_ajax_stream_get_ips', array( __CLASS__, 'get_ips' ) );

}

/**
Expand All @@ -73,17 +76,14 @@ public static function load() {
* @return void
*/
public static function get_users(){
if ( ! defined( 'DOING_AJAX' ) ) {
return;
}
if ( ! current_user_can( WP_Stream_Admin::SETTINGS_CAP ) ) {
if ( ! defined( 'DOING_AJAX' ) || ! current_user_can( WP_Stream_Admin::SETTINGS_CAP ) ) {
return;
}

check_ajax_referer( 'stream_get_users', 'nonce' );

$response = (object) array(
'status' => false,
'status' => false,
'message' => __( 'There was an error in the request', 'stream' ),
);

Expand Down Expand Up @@ -138,6 +138,38 @@ public static function get_users(){
wp_send_json_success( $response );
}

/**
* Ajax callback function to search IP addresses that is used on exclude setting page
*
* @uses WP_User_Query WordPress User Query class.
* @return void
*/
public static function get_ips(){
if ( ! defined( 'DOING_AJAX' ) || ! current_user_can( WP_Stream_Admin::SETTINGS_CAP ) ) {
return;
}

check_ajax_referer( 'stream_get_ips', 'nonce' );

global $wpdb;

$results = $wpdb->get_col(
$wpdb->prepare(
"
SELECT distinct(`ip`)
FROM `{$wpdb->stream}`
WHERE `ip` LIKE %s
ORDER BY inet_aton(`ip`) ASC
LIMIT %d;
",
like_escape( $_POST['find'] ) . '%',
$_POST['limit']
)
);

wp_send_json_success( $results );
}

/**
* Filter the columns to search in a WP_User_Query search.
*
Expand Down Expand Up @@ -260,6 +292,7 @@ public static function get_fields() {
'desc' => __( 'No activity will be logged for these IP addresses.', 'stream' ),
'class' => 'ip-addresses',
'default' => array(),
'nonce' => 'stream_get_ips',
),
),
),
Expand Down Expand Up @@ -371,6 +404,7 @@ public static function render_field( $field ) {
$href = isset( $field['href'] ) ? $field['href'] : null;
$after_field = isset( $field['after_field'] ) ? $field['after_field'] : null;
$title = isset( $field['title'] ) ? $field['title'] : null;
$nonce = isset( $field['nonce'] ) ? $field['nonce'] : null;
$current_value = self::$options[ $section . '_' . $name ];

if ( is_callable( $current_value ) ) {
Expand Down Expand Up @@ -506,13 +540,14 @@ public static function render_field( $field ) {
esc_attr( $name )
);
$output .= sprintf(
'<input type="hidden" data-values=\'%1$s\' data-selected=\'%2$s\' value="%3$s" class="select2-select %4$s" data-select-placeholder="%5$s-%6$s-select-placeholder" />',
'<input type="hidden" data-values=\'%1$s\' data-selected=\'%2$s\' value="%3$s" class="select2-select %4$s" data-select-placeholder="%5$s-%6$s-select-placeholder" %7$s />',
esc_attr( json_encode( $data_values ) ),
esc_attr( json_encode( $selected_values ) ),
esc_attr( implode( ',', $current_value ) ),
$class,
esc_attr( $section ),
esc_attr( $name )
esc_attr( $name ),
isset( $nonce ) ? sprintf( ' data-nonce="%s"', esc_attr( wp_create_nonce( $nonce ) ) ) : ''
);
// to store data with default value if nothing is selected
$output .= sprintf(
Expand Down
65 changes: 59 additions & 6 deletions ui/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,75 @@ jQuery(function($){
});
$( '.stream_page_wp_stream_settings input[type=hidden].select2-select.ip-addresses').each(function( k, el ){
var $input = $(el);
var $ip_regex = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/;

$input.select2({
tags:$input.data('selected'),
width:350,
query: function (query){
var data = {results: []};
if(typeof (query.term) !== 'undefined' && query.term.match($ip_regex) != null ){
data.results.push({id: query.term, text: query.term });
ajax: {
type: 'POST',
url: ajaxurl,
dataType: 'json',
quietMillis: 500,
data: function (term) {
return {
find: term,
limit: 10,
action: 'stream_get_ips',
nonce: $input.data('nonce')
};
},
results: function (response) {
var answer = {
results: []
};

if (response.success !== true || response.data === undefined ) {
return answer;
}

$.each(response.data, function (key, ip ) {
answer.results.push({
id: ip,
text: ip
});
});

return answer;
}
query.callback(data);
},
initSelection: function (item, callback) {
callback( item.data( 'selected' ) );
},
formatNoMatches : function(){
return '';
},
createSearchChoice: function(term) {
var ip_chunks = [];

ip_chunks = term.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);

if (ip_chunks === null) {
return;
}

// remove whole match
ip_chunks.shift();

ip_chunks = $.grep(
ip_chunks,
function(chunk) {
return (chunk.charAt(0) !== '0' && parseInt(chunk, 10) <= 255);
}
);

if (ip_chunks.length < 4) {
return;
}

return {
id: term,
text: term
};
}
}).on('change',function(e){
stream_select2_change_handler( e , $input );
Expand Down

0 comments on commit 22ee462

Please sign in to comment.