Skip to content

Commit

Permalink
Merge pull request #10 from humanmade/add-search-results-filter
Browse files Browse the repository at this point in the history
Add filter to modify search results from AJAX
  • Loading branch information
mikeselander authored Jun 22, 2020
2 parents 7ef20f1 + b23fdbb commit 67225cb
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions src/Admin_Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,30 @@ public static function get_possible_bylines_for_search( $search, $ignored = arra
$terms = get_terms( $term_args );
if ( $terms && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$byline = Byline::get_by_term_id( $term->term_id );
$bylines[] = array(
$byline_object = Byline::get_by_term_id( $term->term_id );

$byline_data = array(
// Select2 specific.
'id' => (int) $term->term_id,
'text' => $term->name,
// Bylines specific.
'term' => (int) $term->term_id,
'display_name' => $term->name,
'user_id' => $byline->user_id,
'avatar_url' => get_avatar_url( $byline->user_email, 32 ),
'user_id' => $byline_object->user_id,
'avatar_url' => get_avatar_url( $byline_object->user_email, 32 ),
);
if ( $byline->user_id ) {
$ignored[] = 'u' . $byline->user_id;

/**
* Filter that allows search results to be modified or enhanced individually.
*
* @param array $byline_data Results for bylines search
* @param Byline|\WP_User $byline_object Object with stored data about the byline.
* @param string $search Search query
*/
$bylines[] = apply_filters( 'bylines_search_result_data', $byline_data, $byline_object, $search );

if ( $byline_object->user_id ) {
$ignored[] = 'u' . $byline_object->user_id;
}
}
}
Expand All @@ -155,7 +166,7 @@ public static function get_possible_bylines_for_search( $search, $ignored = arra
$user_args = apply_filters( 'bylines_user_search_args', $user_args );
$users = get_users( $user_args );
foreach ( $users as $user ) {
$bylines[] = array(
$byline_data = array(
// Select2 specific.
'id' => 'u' . $user->ID,
'text' => $user->display_name,
Expand All @@ -165,13 +176,27 @@ public static function get_possible_bylines_for_search( $search, $ignored = arra
'user_id' => $user->ID,
'avatar_url' => get_avatar_url( $user->user_email, 32 ),
);

/**
* Documented above.
*/
$bylines[] = apply_filters( 'bylines_search_result_data', $byline_data, $user, $search );
}
// Sort alphabetically by display name.
usort(
$bylines, function( $a, $b ) {
return strcmp( $a['display_name'], $b['display_name'] );
}
);

/**
* Filter that allows search results to be modified or enhanced.
*
* @param array $bylines Results for bylines search
* @param string $search Search query
*/
$bylines = apply_filters( 'bylines_search_results', $bylines, $search );

return $bylines;
}

Expand Down

0 comments on commit 67225cb

Please sign in to comment.