forked from blutz/media-credit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.php
61 lines (54 loc) · 1.96 KB
/
search.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
if ( isset( $_GET['q'] ) ) {
$dir = "../../..";
require_once("$dir/wp-config.php");
require_once("$dir/wp-admin/includes/user.php");
require_once("$dir/wp-includes/user.php");
if ($authors = get_editable_authors_by_name( $current_user->id, $_GET['q'], $_GET['limit'] ) ) {
foreach ( $authors as $author )
echo "$author->display_name|$author->ID\n";
/* --- For jQuery UI autocomplete:
foreach ( $authors as $author )
$results[] = (object) array("id"=>$author->ID, "label"=>$author->display_name, "value"=>$author->display_name);
echo json_encode($results);
*/
}
echo '';
}
/**
* Returns the users that are editable by $user_id (normally the current user) and that contain $name within their
* display name. Important to use this function rather than just selected all users for WPMU bloggers.
*
* Basis for this function is proudly stolen from wp-{admin/}includes/user.php :)
*/
function get_editable_authors_by_name( $user_id, $name, $limit ) {
global $wpdb;
// get_editable_user_ids was deprecated in WordPress 3.1 and moved to a file that does not get included above, so
// if the function doesn't exist, then we know we're on a site at WP >= 3.1. Let's used some non-deprecated
// goodness instead.
if ( function_exists ( 'get_editable_user_ids' ) ) {
$editable = get_editable_user_ids( $user_id );
} else {
$editable = get_users( array(
'who' => 'authors',
'include_selected' => true
) );
}
if ( !$editable ) {
return false;
} else {
$editable = join(',', $editable);
// Prepare autocomplete term for query: add wildcard after, and replace all spaces with wildcards
$name = str_replace( ' ', '%', $name ) . '%';
$authors = $wpdb->get_results( $wpdb->prepare( "
SELECT ID, display_name
FROM $wpdb->users
WHERE ID IN ($editable)
AND upper(display_name) LIKE %s
ORDER BY display_name
LIMIT 0, $limit",
strtoupper($name) ));
}
return apply_filters('get_editable_authors_by_name', $authors, $name);
}
?>