Skip to content

Commit

Permalink
Use AJAX rather than pre-filling for Readers autoselect.
Browse files Browse the repository at this point in the history
See #23.
  • Loading branch information
boonebgorges committed Apr 26, 2016
1 parent ab8ab33 commit 505eeec
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 25 deletions.
43 changes: 21 additions & 22 deletions assets/js/single.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,29 @@
responsive_iframes();

if ( $reader_selector.length && $.isFunction( $.fn.select2 ) ) {
// Load the Potential Readers list asynchronously.
$.post( ajaxurl, {
action: 'cacsp_potential_readers',
paper_id: SocialPaperI18n.paper_id
},
function( response ) {
if ( response.success ) {
$reader_selector.select2( {
placeholder: SocialPaperI18n.reader_placeholder,
data: response.data.potential
} );

// Mark existing readers as 'selected'.
var selected = [];
$.each( response.data.existing, function( k, ex ) {
selected.push( ex.id );
} );

$reader_selector.val( selected ).trigger( 'change' );

force_select2_width();
}
$reader_selector.select2( {
ajax: {
url: SocialPaperI18n.rest_url + 'readers/',
dataType: 'json',
delay: 500,
data: function( params ) {
return {
search: params.term
};
},
processResults: function( results ) {
var formatted_results = {};
formatted_results.results = results;
return formatted_results;
}
},
placeholder: SocialPaperI18n.reader_placeholder
} );

$reader_selector.trigger( 'change' );

force_select2_width();

$( '#cacsp-group-selector' ).select2( {
placeholder: SocialPaperI18n.group_placeholder
} );
Expand Down
1 change: 1 addition & 0 deletions includes/hooks-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ function cacsp_asset_single_enqueue_handler() {
'spammed' => __( 'You have successfuly marked the comment as spam.', 'social-paper' ),
'trashed' => __( 'You have successfuly trashed the comment.', 'social-paper' ),
'paper_id' => get_queried_object_id(),
'rest_url' => trailingslashit( rest_url( 'social-paper/' . Social_Paper::$rest_api_version ) ),
) );
}
add_action( 'wp_enqueue_scripts', 'cacsp_asset_single_enqueue_handler', 999 );
Expand Down
52 changes: 49 additions & 3 deletions includes/readers.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ function cacsp_paper_reader_selector( $paper_id ) {
) );

$selected = array();
if ( ! empty( $users['users'] ) ) {
foreach ( $users['users'] as $user ) {
if ( ! empty( $existing['users'] ) ) {
foreach ( $existing['users'] as $user ) {
$user_id = (int) $user->ID;

$user_data[] = array(
$selected[] = array(
'id' => $user_id,
'text' => html_entity_decode( $user->display_name, ENT_QUOTES, 'UTF-8' ),
);
Expand All @@ -97,6 +97,50 @@ function cacsp_paper_reader_selector( $paper_id ) {
wp_nonce_field( 'cacsp-reader-selector', 'cacsp-reader-selector-nonce' );
}

/**
* Register REST route for Readers autocomplete.
*/
function cacsp_register_rest_route_readers_autocomplete() {
$version = Social_Paper::$rest_api_version;
register_rest_route( 'social-paper/' . $version, '/readers/', array(
'methods' => 'GET',
'callback' => 'cacsp_readers_route_cb',
'permission_callback' => function() {
return current_user_can( 'read' );
},
) );
}
add_action( 'rest_api_init', 'cacsp_register_rest_route_readers_autocomplete' );

/**
* Callback for /readers/ REST route.
*
* At the moment, supports only 'search' queries.
*/
function cacsp_readers_route_cb( WP_REST_REQUEST $request ) {
global $wpdb;

$q = $request->get_param( 'search' );

if ( ! $q ) {
return array();
}

// WP provides pretty much no way to do this.
$escaped = esc_sql( $wpdb->esc_like( $q ) );
$found = $wpdb->get_results( "SELECT ID, display_name, user_login FROM $wpdb->users WHERE display_name LIKE '%{$escaped}%' OR user_login LIKE '%{$escaped}%' OR user_nicename LIKE '%{$escaped}%' AND spam = 0 AND deleted = 0 LIMIT 100" );

$items = array();
foreach ( $found as $_found ) {
$items[] = array(
'id' => $_found->ID,
'text' => $_found->display_name,
);
}

return $items;
}

/**
* Save reader selection data sent via AJAX.
*
Expand Down Expand Up @@ -185,6 +229,8 @@ function cacsp_save_paper_status( $post_id ) {
/**
* Fetch a JSON list of potential readers, to populate the Select2 interface.
*
* No longer used.
*
* @since 1.1.0
*/
function cacsp_potential_readers_cb() {
Expand Down
9 changes: 9 additions & 0 deletions social-paper.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ class Social_Paper {
*/
public static $is_buffer = false;

/**
* REST API version.
*
* Used for namespacing endpoints.
*
* @var string
*/
public static $rest_api_version = 'v1.0';

/**
* Static initializer.
*/
Expand Down

0 comments on commit 505eeec

Please sign in to comment.