This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 235
/
mentions-search.php
220 lines (187 loc) · 6 KB
/
mentions-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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/**
* Search Facebook for friend or page
*
* @since 1.1
*/
class Facebook_Mentions_Search {
/**
* Respond to WordPress admin AJAX requests
* Adds custom wp_ajax_* actions
*
* @since 1.1
*/
public static function wp_ajax_handlers() {
add_action( 'wp_ajax_facebook_mentions_friends_autocomplete', array( 'Facebook_Mentions_Search', 'search_endpoint_friends' ) );
add_action( 'wp_ajax_facebook_mentions_pages_autocomplete', array( 'Facebook_Mentions_Search', 'search_endpoint_pages' ) );
}
/**
* Check for minimum requirements before conducting a Facebook search
* Sets up a JSON response
*
* @since 1.1
* @return null|string discovered search term or null if error generated
*/
public static function search_endpoint_setup() {
global $facebook;
header( 'Content-Type: application/json; charset=utf-8', true );
if ( array_key_exists( 'REQUEST_METHOD', $_SERVER ) && $_SERVER['REQUEST_METHOD'] !== 'GET' ) {
status_header( 405 );
header( 'Allow: GET', true );
echo json_encode( array( 'error' => 'please use HTTP GET' ) );
return;
}
if ( ! current_user_can( 'edit_posts' ) || empty( $_GET['autocompleteNonce'] ) || ! wp_verify_nonce( $_GET['autocompleteNonce'], 'facebook_autocomplete_nonce' ) ) {
status_header( 403 );
// use WordPress standard rejection message
echo json_encode( array( 'error' => __( 'Cheatin\' uh?' ) ) );
return;
}
if ( ! isset( $facebook ) ) {
status_header( 403 );
echo json_encode( array( 'error' => __( 'Facebook credentials not properly configured on the server', 'facebook' ) ) );
return;
}
if ( ! ( isset( $_GET ) && ! empty( $_GET['q'] ) && $search_term = sanitize_text_field( trim( $_GET['q'] ) ) ) ) {
status_header( 400 );
echo json_encode( array( 'error' => __( 'search term required', 'facebook' ) ) );
return;
}
if ( isset( $search_term ) )
return $search_term;
}
/**
* Search by friends endpoint
*
* @since 1.1
*/
public static function search_endpoint_friends() {
$search_term = self::search_endpoint_setup();
if ( ! $search_term )
exit;
$content = self::friend_search( $search_term );
if ( is_array( $content ) ) {
echo json_encode( $content );
} else {
status_header( 404 );
echo json_encode( array( 'error' => sprintf( __( 'No friends with name matching "%s" found', 'facebook' ), $search_term ) ) );
}
exit;
}
/**
* Search by page name endpoint
*
* @since 1.1
*/
public static function search_endpoint_pages() {
$search_term = self::search_endpoint_setup();
if ( ! $search_term )
exit;
$content = self::page_search( $search_term );
if ( is_array( $content ) ) {
echo json_encode( $content );
} else {
status_header( 404 );
echo json_encode( array( 'error' => sprintf( __( 'No pages found matching "%s."', 'facebook' ), $search_term ) ) );
}
exit;
}
/**
* Search your friends by name
*
* @since 1.1
* @param string $search_term partial name to match
* @return null|array null on error, else array of friends with keys id, uid, name
*/
public static function friend_search( $search_term ) {
global $facebook;
if ( empty( $search_term ) )
return;
$facebook_user_id = $facebook->getUser();
if ( ! $facebook_user_id )
return;
// cached list of all friends
$cache_key = 'facebook_friends_' . $facebook_user_id;
$friends = get_transient( $cache_key );
if ( $friends === false ) {
try {
$friends = $facebook->api( '/me/friends', 'GET', array( 'fields' => 'id,name', 'ref' => 'fbwpp' ) );
} catch ( WP_FacebookApiException $e ) {
return;
}
if ( isset( $friends['data'] ) && is_array( $friends['data'] ) )
$friends = $friends['data'];
else
$friends = array();
set_transient( $cache_key, $friends, 60*15 ); // cache friends list for 15 minutes
}
// no friends to match against
if ( empty( $friends ) )
return;
$search_term = strtolower( $search_term );
// nothing to search against
if ( ! $search_term )
return;
$matched_friends = array();
foreach( $friends as $friend ) {
// enforce minimum requirements
if ( ! isset( $friend['name'] ) || ! isset( $friend['id'] ) )
continue;
// does the search term appear in the name?
if ( strpos( strtolower($friend['name']), $search_term ) !== false )
$matched_friends[] = $friend;
}
if ( ! empty( $matched_friends ) )
return $matched_friends;
}
/**
* Search Facebook pages with a freeform text string
*
* @since 1.1
* @param string $search_term comparison string
*/
public static function page_search( $search_term ) {
global $facebook;
$cache_key = 'facebook_pages_' . $search_term;
$matched_pages = get_transient( $cache_key );
if ( $matched_pages === false ) {
try {
$pages = $facebook->api( '/search', 'GET', array( 'type' => 'page', 'fields' => 'id,name,picture,likes', 'ref' => 'fbwpp', 'q' => $search_term ) );
}
catch (WP_FacebookApiException $e) {
return;
}
if ( ! ( isset( $pages['data'] ) && is_array( $pages['data'] ) ) )
return;
$pages = $pages['data'];
$matched_pages = array();
// cleanup the picture response
foreach ( $pages as $page ) {
if ( ! ( isset( $page['id'] ) && isset( $page['name'] ) ) )
continue;
if ( isset( $page['picture'] ) ) {
if ( isset( $page['picture']['data']['url'] ) && ( ! isset( $page['picture']['data']['is_silhouette'] ) || $page['picture']['data']['is_silhouette'] === false ) ) {
$picture = esc_url_raw( $page['picture']['data']['url'], array( 'http', 'https' ) );
if ( $picture )
$page['image'] = $picture;
}
unset( $page['picture'] );
}
$clean_page = array(
'id' => $page['id'],
'name' => $page['name']
);
if ( isset( $page['image'] ) )
$clean_page['image'] = $page['image'];
if ( isset( $page['likes'] ) )
$clean_page['likes'] = absint( $page['likes'] );
$matched_pages[] = $clean_page;
unset( $clean_page );
}
set_transient( $cache_key, $matched_pages, 60*60 );
}
if ( ! empty( $matched_pages ) )
return $matched_pages;
}
}
?>