Skip to content

Commit

Permalink
Upgrade Select2
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Carbis committed May 30, 2016
1 parent c0024de commit c3f6c65
Show file tree
Hide file tree
Showing 119 changed files with 12,862 additions and 5,987 deletions.
63 changes: 11 additions & 52 deletions classes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,6 @@ public function __construct( $plugin ) {

// Ajax users list.
add_action( 'wp_ajax_wp_stream_filters', array( $this, 'ajax_filters' ) );

// Ajax user's name by ID.
add_action( 'wp_ajax_wp_stream_get_filter_value_by_id', array( $this, 'get_filter_value_by_id' ) );

// Ajax users list.
add_action( 'wp_ajax_wp_stream_filters', array( $this, 'ajax_filters' ) );

// Ajax user's name by ID.
add_action( 'wp_ajax_wp_stream_get_filter_value_by_id', array( $this, 'get_filter_value_by_id' ) );
}

/**
Expand Down Expand Up @@ -347,8 +338,8 @@ public function register_menu() {
* @return void
*/
public function admin_enqueue_scripts( $hook ) {
wp_register_script( 'wp-stream-select2', $this->plugin->locations['url'] . 'ui/lib/select2/select2.js', array( 'jquery' ), '3.5.2', true );
wp_register_style( 'wp-stream-select2', $this->plugin->locations['url'] . 'ui/lib/select2/select2.css', array(), '3.5.2' );
wp_register_script( 'wp-stream-select2', $this->plugin->locations['url'] . 'ui/lib/select2/js/select2.js', array( 'jquery' ), '3.5.2', true );
wp_register_style( 'wp-stream-select2', $this->plugin->locations['url'] . 'ui/lib/select2/css/select2.css', array(), '3.5.2' );
wp_register_script( 'wp-stream-timeago', $this->plugin->locations['url'] . 'ui/lib/timeago/jquery.timeago.js', array(), '1.4.1', true );

$locale = strtolower( substr( get_locale(), 0, 2 ) );
Expand Down Expand Up @@ -855,6 +846,12 @@ public function filter_role_caps( $allcaps, $cap, $role ) {
* @action wp_ajax_wp_stream_filters
*/
public function ajax_filters() {
if ( ! defined( 'DOING_AJAX' ) || ! current_user_can( $this->plugin->admin->settings_cap ) ) {
return;
}

check_ajax_referer( 'stream_filters_user_search_nonce', 'nonce' );

switch ( wp_stream_filter_input( INPUT_GET, 'filter' ) ) {
case 'user_id':
$users = array_merge(
Expand Down Expand Up @@ -884,47 +881,9 @@ function( $user ) use ( $search ) {
}

if ( isset( $results ) ) {
echo wp_stream_json_encode( array_values( $results ) ); // xss ok
}

if ( defined( 'WP_STREAM_TESTS' ) && WP_STREAM_TESTS ) {
return;
echo wp_stream_json_encode( $results ); // xss ok
}

die();
}

/**
* @action wp_ajax_wp_stream_get_filter_value_by_id
*/
public function get_filter_value_by_id() {
$filter = wp_stream_filter_input( INPUT_POST, 'filter' );

switch ( $filter ) {
case 'user_id':
$id = wp_stream_filter_input( INPUT_POST, 'id' );

if ( '0' === $id ) {
$value = 'WP-CLI';

break;
}

$user = get_userdata( $id );

if ( ! $user || is_wp_error( $user ) ) {
$value = '';
} else {
$value = $user->display_name;
}

break;
default:
$value = '';
}

echo wp_stream_json_encode( $value ); // xss ok

if ( defined( 'WP_STREAM_TESTS' ) && WP_STREAM_TESTS ) {
return;
}
Expand All @@ -936,11 +895,11 @@ public function get_users_record_meta( $authors ) {
$authors_records = array();

foreach ( $authors as $user_id => $args ) {
$author = new Author( $user_id );
$author = new Author( $args->ID );

$authors_records[ $user_id ] = array(
'text' => $author->get_display_name(),
'id' => $user_id,
'id' => $author->id,
'label' => $author->get_display_name(),
'icon' => $author->get_avatar_src( 32 ),
'title' => '',
Expand Down
1 change: 0 additions & 1 deletion classes/class-export.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class Export {
* Class constructor
*
* @param Plugin $plugin The plugin object.
* @return void
*/
public function __construct( $plugin ) {
$this->plugin = $plugin;
Expand Down
10 changes: 9 additions & 1 deletion classes/class-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,13 @@ function assemble_records( $column ) {
$total_users = $user_count['total_users'];

if ( $total_users > $this->plugin->admin->preload_users_max ) {
return array();
$selected_user = wp_stream_filter_input( INPUT_GET, 'user_id' );
if ( $selected_user ) {
$user = new Author( $selected_user );
return array( $selected_user => $user->get_display_name() );
} else {
return array();
}
}

$users = array_map(
Expand Down Expand Up @@ -621,6 +627,8 @@ function filters_form() {

$filters_string .= sprintf( '<input type="submit" id="record-query-submit" class="button" value="%s" />', __( 'Filter', 'stream' ) );

$filters_string .= wp_nonce_field( 'stream_filters_user_search_nonce', 'stream_filters_user_search_nonce' );

// Parse all query vars into an array
$query_vars = array();

Expand Down
15 changes: 0 additions & 15 deletions tests/tests/test-class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,21 +389,6 @@ public function test_ajax_filters() {
$this->assertInternalType( 'array', $data );
}

public function test_get_filter_value_by_id() {
$_POST['filter'] = 'user_id';
$_POST['id'] = get_current_user_id();

ob_start();
$this->admin->get_filter_value_by_id();
$json = ob_get_clean();

$this->assertNotEmpty( $json );
$data = json_decode( $json );
$this->assertNotFalse( $data );
$this->assertNotEmpty( $data );
$this->assertInternalType( 'string', $data );
}

public function test_get_users_record_meta() {
$user_id = get_current_user_id();
$authors = array(
Expand Down
107 changes: 49 additions & 58 deletions ui/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,39 @@ jQuery( function( $ ) {

$( '.toplevel_page_wp_stream :input.chosen-select' ).each( function( i, el ) {
var args = {},
formatResult = function( record, container ) {
var result = '',
$elem = $( record.element ),
icon = '';

if ( '- ' === record.text.substring( 0, 2 ) ) {
record.text = record.text.substring( 2 );
}

if ( undefined !== record.icon ) {
icon = record.icon;
} else if ( undefined !== $elem.attr( 'data-icon' ) ) {
icon = $elem.data( 'icon' );
}
if ( icon ) {
result += '<img src="' + icon + '" class="wp-stream-select2-icon">';
}

result += record.text;

// Add more info to the container
container.attr( 'title', $elem.attr( 'title' ) );

return result;
},
formatSelection = function( record ) {
if ( '- ' === record.text.substring( 0, 2 ) ) {
record.text = record.text.substring( 2 );
}
return record.text;
};

if ( $( el ).find( 'option' ).length > 0 ) {
formatResult = function( record, container ) {
var result = '',
$elem = $( record.element ),
icon = '';

if ( '- ' === record.text.substring( 0, 2 ) ) {
record.text = record.text.substring( 2 );
}

if ( undefined !== record.icon ) {
icon = record.icon;
} else if ( undefined !== $elem.attr( 'data-icon' ) ) {
icon = $elem.data( 'icon' );
}
if ( icon ) {
result += '<img src="' + icon + '" class="wp-stream-select2-icon">';
}

result += record.text;

// Add more info to the container
container.attr( 'title', $elem.attr( 'title' ) );

return result;
},
formatSelection = function( record ) {
if ( '- ' === record.text.substring( 0, 2 ) ) {
record.text = record.text.substring( 2 );
}
return record.text;
};

if ( $( el ).find( 'option' ).not( ':selected' ).not( ':empty' ).length > 0 ) {
args = {
minimumResultsForSearch: 10,
formatResult: formatResult,
Expand All @@ -62,40 +62,31 @@ jQuery( function( $ ) {
width: '165px',
ajax: {
url: ajaxurl,
datatype: 'json',
dataType: 'json',
quietMillis: 100,
data: function( term ) {
return {
action: 'wp_stream_filters',
nonce: $( '#stream_filters_user_search_nonce' ).val(),
filter: $( el ).attr( 'name' ),
q: term
q: term.term
};
},
results: function( data ) {
return { results: data };
processResults: function( data ) {
var results = [];
$.each( data, function( index, item ){
results.push({
id: item.id,
text: item.label
});
});
return {
results: results
};
}
},
formatResult: formatResult,
formatSelection: formatSelection,
initSelection: function( element, callback ) {
var id = $( element ).val();
if ( '' !== id ) {
$.post(
ajaxurl,
{
action: 'wp_stream_get_filter_value_by_id',
filter: $( element ).attr( 'name' ),
id: id
},
function( response ) {
callback({
id: id,
text: response
});
},
'json'
);
}
}
formatSelection: formatSelection
};
}

Expand Down
107 changes: 0 additions & 107 deletions ui/lib/select2/CONTRIBUTING.md

This file was deleted.

Loading

0 comments on commit c3f6c65

Please sign in to comment.