From 40c1447f0c78ea1fa13f90e76a6488a1e78d1e9e Mon Sep 17 00:00:00 2001 From: Luan Cuba Date: Tue, 26 Aug 2014 19:03:46 -0300 Subject: [PATCH 1/6] add ajax, html and php functions to search user for author field with ajax --- inc/init/posts.php | 56 ++++++++++++++++++++++++++++++++++++++++++++++ inc/init/terms.php | 4 ---- ui/js/fields.js | 37 ++++++++++++++++++++++++++++++ view/posts.php | 2 +- 4 files changed, 94 insertions(+), 5 deletions(-) diff --git a/inc/init/posts.php b/inc/init/posts.php index ad04a3a..14cfa33 100644 --- a/inc/init/posts.php +++ b/inc/init/posts.php @@ -127,6 +127,62 @@ function ( $request = null ){ $response->more = false; } + return ( Admin::$is_ajax ? exit( json_encode( $response ) ) : $response ); + } +); + +add_action( + 'wp_ajax_' . Plugin::$slug . '.search_authors', + function ( $request = null ){ + $response = (object) array( + 'status' => false, + 'message' => __( 'Your request has failed', 'fakerpress' ), + 'results' => array(), + 'more' => true, + ); + + if ( ( ! Admin::$is_ajax && is_null( $request ) ) || ! is_user_logged_in() ){ + return ( Admin::$is_ajax ? exit( json_encode( $response ) ) : $response ); + } + + $request = (object) wp_parse_args( + $request, + array( + 'search' => isset( $_POST['search'] ) ? $_POST['search'] : '', + 'page' => absint( isset( $_POST['page'] ) ? $_POST['page'] : 0 ), + 'page_limit' => absint( isset( $_POST['page_limit'] ) ? $_POST['page_limit'] : 10 ), + ) + ); + + $response->status = true; + $response->message = __( 'Request successful', 'fakerpress' ); + + if ( ! empty( $request->search ) ){ + $users = new \WP_User_Query( + array( + 'search' => "*{$request->search}*", + 'search_columns' => array( + 'user_login', + 'user_nicename', + 'user_email', + 'user_url', + ), + 'orderby' => 'display_name', + 'offset' => $request->page_limit * ( $request->page - 1 ), + 'number' => $request->page_limit, + ) + ); + + foreach ( $users->results as $result ){ + $response->results[] = $result->data; + } + } + + if ( empty( $response->results ) || count( $response->results ) < $request->page_limit ){ + $response->more = false; + } + + return ( Admin::$is_ajax ? exit( json_encode( $response ) ) : $response ); } ); diff --git a/inc/init/terms.php b/inc/init/terms.php index 28a5bd9..51d0386 100644 --- a/inc/init/terms.php +++ b/inc/init/terms.php @@ -81,10 +81,6 @@ function ( $request = null ){ ) ); - if ( is_null( $request->post_type ) || empty( $request->post_type ) ){ - $request->post_type = get_post_types( array( 'public' => true ) ); - } - $response->status = true; $response->message = __( 'Request successful', 'fakerpress' ); diff --git a/ui/js/fields.js b/ui/js/fields.js index 706744b..ef7ed3a 100644 --- a/ui/js/fields.js +++ b/ui/js/fields.js @@ -56,6 +56,43 @@ }); }( window.jQuery, window._ ) ); +//Author fields +( function( $ ){ + 'use strict'; + $(document).ready(function(){ + $( '.field-select2-author' ).each(function(){ + var $select = $(this); + + $select.select2({ + width: 400, + minimumInputLength: 2, + multiple: true, + allowClear: true, + ajax: { + dataType: 'json', + type: 'POST', + url: window.ajaxurl, + data: function (author, page) { + return { + action: 'fakerpress.search_authors', + search: author, // search author + page_limit: 10, + page: page, + }; + }, + results: function ( data ) { // parse the results into the format expected by Select2. + $.each( data.results, function( k, result ){ + result.id = result.ID; + result.text = result.display_name; + } ); + return data; + } + } + }); + }); + }); +}( jQuery ) ); + // Date Fields ( function( $ ){ 'use strict'; diff --git a/view/posts.php b/view/posts.php index 8cd60d7..5d3aa87 100644 --- a/view/posts.php +++ b/view/posts.php @@ -155,7 +155,7 @@
- +

From 692d94505e10def4588aa9021e23466681b409fc Mon Sep 17 00:00:00 2001 From: Luan Cuba Date: Tue, 26 Aug 2014 19:39:14 -0300 Subject: [PATCH 2/6] added abbr to authors results --- ui/js/fields.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ui/js/fields.js b/ui/js/fields.js index ef7ed3a..9ff387c 100644 --- a/ui/js/fields.js +++ b/ui/js/fields.js @@ -68,11 +68,18 @@ minimumInputLength: 2, multiple: true, allowClear: true, + escapeMarkup: function (m) { return m; }, + formatSelection: function ( author ){ + return _.template('<%= display_name %>')( author ) + }, + formatResult: function ( author ){ + return _.template('<%= display_name %>')( author ) + }, ajax: { dataType: 'json', type: 'POST', url: window.ajaxurl, - data: function (author, page) { + data: function ( author, page ) { return { action: 'fakerpress.search_authors', search: author, // search author From 046359233ceaa7a6e4049532e80e2b14f1633f07 Mon Sep 17 00:00:00 2001 From: Luan Cuba Date: Tue, 26 Aug 2014 19:56:35 -0300 Subject: [PATCH 3/6] added role to author result abbr --- inc/init/posts.php | 2 +- ui/js/fields.js | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/inc/init/posts.php b/inc/init/posts.php index 14cfa33..19b9b97 100644 --- a/inc/init/posts.php +++ b/inc/init/posts.php @@ -174,7 +174,7 @@ function ( $request = null ){ ); foreach ( $users->results as $result ){ - $response->results[] = $result->data; + $response->results[] = $result; } } diff --git a/ui/js/fields.js b/ui/js/fields.js index 9ff387c..d684b3e 100644 --- a/ui/js/fields.js +++ b/ui/js/fields.js @@ -70,10 +70,10 @@ allowClear: true, escapeMarkup: function (m) { return m; }, formatSelection: function ( author ){ - return _.template('<%= display_name %>')( author ) + return _.template('<%= data.display_name %> : <%= roles %>')( author ) }, formatResult: function ( author ){ - return _.template('<%= display_name %>')( author ) + return _.template('<%= data.display_name %> : <%= roles %>')( author ) }, ajax: { dataType: 'json', @@ -89,8 +89,8 @@ }, results: function ( data ) { // parse the results into the format expected by Select2. $.each( data.results, function( k, result ){ - result.id = result.ID; - result.text = result.display_name; + result.id = result.data.ID; + result.text = result.data.display_name; } ); return data; } From de2900ba5352b51c734a1f6c355f3175ee471cdc Mon Sep 17 00:00:00 2001 From: Luan Cuba Date: Tue, 26 Aug 2014 20:03:07 -0300 Subject: [PATCH 4/6] added the code accidentally removed --- inc/init/terms.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/inc/init/terms.php b/inc/init/terms.php index 51d0386..28a5bd9 100644 --- a/inc/init/terms.php +++ b/inc/init/terms.php @@ -81,6 +81,10 @@ function ( $request = null ){ ) ); + if ( is_null( $request->post_type ) || empty( $request->post_type ) ){ + $request->post_type = get_post_types( array( 'public' => true ) ); + } + $response->status = true; $response->message = __( 'Request successful', 'fakerpress' ); From a900113a9107d9d311ab99d29c6abd11c49e73fb Mon Sep 17 00:00:00 2001 From: Gustavo Bordoni Date: Wed, 3 Sep 2014 08:43:35 -0300 Subject: [PATCH 5/6] Change the User select2 field Output a bit --- ui/js/fields.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ui/js/fields.js b/ui/js/fields.js index d684b3e..8610b09 100644 --- a/ui/js/fields.js +++ b/ui/js/fields.js @@ -65,15 +65,14 @@ $select.select2({ width: 400, - minimumInputLength: 2, multiple: true, allowClear: true, escapeMarkup: function (m) { return m; }, formatSelection: function ( author ){ - return _.template('<%= data.display_name %> : <%= roles %>')( author ) + return _.template('<%= roles %>: <%= data.display_name %>')( author ) }, formatResult: function ( author ){ - return _.template('<%= data.display_name %> : <%= roles %>')( author ) + return _.template('<%= roles %>: <%= data.display_name %>')( author ) }, ajax: { dataType: 'json', From 7e6f1517e77ed9a202476e07f7fcdbb988f65bef Mon Sep 17 00:00:00 2001 From: Gustavo Bordoni Date: Wed, 3 Sep 2014 08:44:06 -0300 Subject: [PATCH 6/6] Changing the behavior to show all users when querying without any search --- inc/init/posts.php | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/inc/init/posts.php b/inc/init/posts.php index 19b9b97..d8647ea 100644 --- a/inc/init/posts.php +++ b/inc/init/posts.php @@ -157,32 +157,32 @@ function ( $request = null ){ $response->status = true; $response->message = __( 'Request successful', 'fakerpress' ); + $query_args = array( + 'orderby' => 'display_name', + 'offset' => $request->page_limit * ( $request->page - 1 ), + 'number' => $request->page_limit, + ); + if ( ! empty( $request->search ) ){ - $users = new \WP_User_Query( - array( - 'search' => "*{$request->search}*", - 'search_columns' => array( - 'user_login', - 'user_nicename', - 'user_email', - 'user_url', - ), - 'orderby' => 'display_name', - 'offset' => $request->page_limit * ( $request->page - 1 ), - 'number' => $request->page_limit, - ) + $query_args['search'] = "*{$request->search}*"; + $query_args['search_columns'] = array( + 'user_login', + 'user_nicename', + 'user_email', + 'user_url', ); + } - foreach ( $users->results as $result ){ - $response->results[] = $result; - } + $users = new \WP_User_Query( $query_args ); + + foreach ( $users->results as $result ){ + $response->results[] = $result; } if ( empty( $response->results ) || count( $response->results ) < $request->page_limit ){ $response->more = false; } - return ( Admin::$is_ajax ? exit( json_encode( $response ) ) : $response ); } );