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

Issue/22 #43

Merged
merged 6 commits into from
Sep 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions inc/init/posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,59 @@ function ( $request = null ){
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' );

$query_args = array(
'orderby' => 'display_name',
'offset' => $request->page_limit * ( $request->page - 1 ),
'number' => $request->page_limit,
);

if ( ! empty( $request->search ) ){
$query_args['search'] = "*{$request->search}*";
$query_args['search_columns'] = array(
'user_login',
'user_nicename',
'user_email',
'user_url',
);
}

$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 );
}
);
43 changes: 43 additions & 0 deletions ui/js/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,49 @@
});
}( window.jQuery, window._ ) );

//Author fields
( function( $ ){
'use strict';
$(document).ready(function(){
$( '.field-select2-author' ).each(function(){
var $select = $(this);

$select.select2({
width: 400,
multiple: true,
allowClear: true,
escapeMarkup: function (m) { return m; },
formatSelection: function ( author ){
return _.template('<abbr title="<%= ID %>: <%= data.user_email %>"><%= roles %>: <%= data.display_name %></abbr>')( author )
},
formatResult: function ( author ){
return _.template('<abbr title="<%= ID %>: <%= data.user_email %>"><%= roles %>: <%= data.display_name %></abbr>')( author )
},
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.data.ID;
result.text = result.data.display_name;
} );
return data;
}
}
});
});
});
}( jQuery ) );

// Date Fields
( function( $ ){
'use strict';
Expand Down
2 changes: 1 addition & 1 deletion view/posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
<th scope="row"><label for="fakerpress_author"><?php _e( 'Author', 'fakerpress' ); ?></label></th>
<td>
<div id="fakerpress[author]">
<input type='hidden' class='field-select2-simple' name='fakerpress_author' data-possibilities='<?php echo json_encode( $_json_users_output ); ?>' />
<input type='hidden' class='field-select2-simple field-select2-author' name='fakerpress_authors'/>
</div>
<p class="description"><?php _e( 'Choose some users to be authors of posts generated.', 'fakerpress' ); ?></p>
</td>
Expand Down