Skip to content

Commit 36a1a74

Browse files
committed
separate data from front-end rendering for jquery-token-input
1 parent ac5a431 commit 36a1a74

File tree

3 files changed

+103
-37
lines changed

3 files changed

+103
-37
lines changed

fb-social-publisher-mentioning.php

+25-15
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ function fb_friend_page_autocomplete() {
4848

4949
foreach ($results as $result) {
5050
$output[$count]['id'] = '[' . esc_attr($result[1]) . '|' . esc_attr($result[0]) . ']';
51-
$output[$count]['name'] = '<img src="' . esc_url( 'http://graph.facebook.com/' . $result[1] . '/picture/' ) . '" width="25" height="25"> &nbsp;' . esc_html($result[0]);
52-
51+
$output[$count]['uid'] = $result[1];
52+
$output[$count]['name'] = $result[0];
5353
$count++;
5454
}
5555
}
5656

57+
if ( ! headers_sent() )
58+
header( 'Content-Type: application/json; charset=utf-8', true );
5759
echo json_encode($output);
5860
exit;
5961
}
@@ -64,13 +66,14 @@ function fb_friend_page_autocomplete() {
6466

6567
if ( false === ( $pages = get_transient( 'fb_pages_' . $_GET['q']) ) ) {
6668
try {
67-
$pages = $facebook->api( '/search', 'GET', array( 'access_token' => '', 'q' => $_GET['q'], 'type' => 'page', 'fields' => 'picture,name,id,likes', 'ref' => 'fbwpp' ) );
69+
$pages = $facebook->api( '/search', 'GET', array( 'type' => 'page', 'fields' => 'picture,name,id,likes', 'ref' => 'fbwpp', 'q' => $_GET['q'] ) );
6870
}
6971
catch (WP_FacebookApiException $e) {}
7072
set_transient( 'fb_pages_' . $_GET['q'], $pages, 60*60 );
7173
}
7274

7375
if ( isset($pages['data']) ) {
76+
$pages_clean = array();
7477
foreach($pages['data'] as $page) {
7578
if (isset($page['name']) && isset($page['picture']) && isset($page['id']) && isset($page['likes']))
7679
$pages_clean[$page['name']] = array($page['picture'], $page['name'], $page['id'], $page['likes']);
@@ -80,6 +83,7 @@ function fb_friend_page_autocomplete() {
8083
exit;
8184
}
8285

86+
$results = array();
8387
if ( isset($_GET['q']) ) {
8488
$q = strtolower($_GET['q']);
8589

@@ -95,29 +99,35 @@ function fb_friend_page_autocomplete() {
9599
$count = 0;
96100

97101
foreach ($results as $result) {
98-
$output[$count]['id'] = '[' . esc_attr($result[1][2]) . '|' . esc_attr($result[1][1]) . ']';
99-
$output[$count]['name'] = '<img src="' . esc_url($result[1][0]) . '" width="25" height="25"> &nbsp;' . esc_attr($result[1][1]) . ' (' . fb_short_number(esc_attr($result[1][3])) . ' likes)';
102+
$item = array();
103+
$item['id'] = '[' . esc_attr($result[1][2]) . '|' . esc_attr($result[1][1]) . ']';
104+
105+
$image_uri = esc_url_raw( $result[1][0], array('http','https') );
106+
if ( $image_uri )
107+
$item['image'] = $image_uri;
108+
unset( $image_uri );
109+
110+
$item['name'] = $result[1][1];
111+
112+
$likes = absint( $result[1][3] );
113+
if ( $likes )
114+
$item['likes'] = $likes;
115+
unset( $likes );
100116

117+
$output[$count] = $item;
118+
unset($item);
101119
$count++;
102120
}
103121
}
104122

123+
if ( ! headers_sent() )
124+
header( 'Content-Type: application/json; charset=utf-8', true );
105125
echo json_encode($output);
106126
exit;
107127
}
108128
}
109129
}
110130

111-
112-
function fb_short_number($num) {
113-
if( $num > 1000000 )
114-
return round(($num/1000000),0) . 'm';
115-
else if( $num > 1000 )
116-
return round(($num/1000),0).'k';
117-
118-
return number_format_i18n($num);
119-
}
120-
121131
add_action( 'add_meta_boxes', 'fb_add_page_mention_box' );
122132
add_action( 'save_post', 'fb_add_page_mention_box_save' );
123133

scripts/fb-admin.dev.js

+77-21
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,82 @@ function redirectWithParam(key, value) {
4747
}
4848
}
4949

50-
jQuery(function() {
51-
jQuery("#suggest-friends").tokenInput(ajaxurl + "?fb-friends=1&action=fb_friend_page_autocomplete&autocompleteNonce=" +FBNonce.autocompleteNonce, {
52-
theme: "facebook",
53-
preventDuplicates: true,
54-
hintText: "Type to find a friend.",
55-
animateDropdown: false,
56-
noResultsText: "No friend found.",
57-
});
58-
});
50+
function fbShowDebugInfo() {
51+
jQuery("#debug-output").show();
52+
}
5953

60-
jQuery(function() {
61-
jQuery("#suggest-pages").tokenInput(ajaxurl + "?fb-pages=1&action=fb_friend_page_autocomplete&autocompleteNonce=" +FBNonce.autocompleteNonce, {
62-
theme: "facebook",
63-
preventDuplicates: true,
64-
hintText: "Type to find a page.",
65-
animateDropdown: false,
66-
noResultsText: "No page found.",
67-
});
68-
});
54+
// avoid collisions
55+
var FB_WP = FB_WP || {};
56+
FB_WP.admin = {
57+
friend_suggest: {hint:"Type to find a friend.",noresults:"No friend found."}, // TODO: override me with translations
58+
page_suggest: {hint:"Type to find a page.",noresults:"No page found."},
59+
short_number: function(num) {
60+
if( num > 1000000 ) {
61+
return Math.round((num/1000000)).toString() + "m";
62+
} else if( num > 1000 ) {
63+
return Math.round((num/1000),0).toString() + "k";
64+
}
6965

70-
function fbShowDebugInfo() {
71-
jQuery('#debug-output').show();
72-
}
66+
return num;
67+
},
68+
token_input: { // loopj jquery-token-input
69+
friends: function() {
70+
jQuery("#suggest-friends").tokenInput(ajaxurl + "?" + jQuery.param({"fb-friends":1,"action":"fb_friend_page_autocomplete","autocompleteNonce":FBNonce.autocompleteNonce}), {
71+
theme: "facebook",
72+
preventDuplicates: true,
73+
hintText: FB_WP.admin.friend_suggest.hint,
74+
animateDropdown: false,
75+
noResultsText: FB_WP.admin.friend_suggest.noresults,
76+
resultsFormatter: function(friend) {
77+
// we want at least a name
78+
if ( friend.name === undefined ) {
79+
return "";
80+
}
81+
var li = jQuery("<li />");
82+
// picture served in HTTP scheme
83+
// no mixed content just to make the list item pretty
84+
if ( document.location.protocol === "http:" && friend.uid !== undefined ) {
85+
li.append( jQuery("<img />").attr({width:25,height:25,src:"http://graph.facebook.com/"+friend.uid+"/picture?width=25&height=25",alt:friend.name}).css("margin-right","2em") );
86+
}
87+
li.append( jQuery("<span />").text(friend.name).text() );
88+
return jQuery("<ul />").append(li).html();
89+
}
90+
});
91+
},
92+
pages: function() {
93+
jQuery("#suggest-pages").tokenInput(ajaxurl + "?" + jQuery.param({"fb-pages":1,"action":"fb_friend_page_autocomplete","autocompleteNonce":FBNonce.autocompleteNonce}), {
94+
theme: "facebook",
95+
preventDuplicates: true,
96+
hintText: FB_WP.admin.page_suggest.hint,
97+
animateDropdown: false,
98+
minChars: 2,
99+
noResultsText: FB_WP.admin.page_suggest.noresults,
100+
resultsFormatter: function(page) {
101+
// we want at least a name
102+
if ( page.name === undefined ) {
103+
return "";
104+
}
105+
var li = jQuery("<li />");
106+
// account for no image or image inclusion causing mixed scheme
107+
if ( document.location.protocol === "http:" && page.image !== undefined ) {
108+
li.append( jQuery("<img />").attr({width:25,height:25,src:page.image,alt:page.name}).css("margin-right","2em") );
109+
}
110+
var text = page.name;
111+
if ( page.likes !== undefined ) {
112+
text = text + " (" + FB_WP.admin.short_number(page.likes) + " likes)";
113+
}
114+
li.append( jQuery("<span />").text(text).text() );
115+
return jQuery("<ul />").append(li).html();
116+
}
117+
});
118+
}
119+
},
120+
init: function() {
121+
FB_WP.admin.token_input.friends();
122+
FB_WP.admin.token_input.pages();
123+
}
124+
};
125+
126+
jQuery(function() {
127+
FB_WP.admin.init();
128+
});

scripts/fb-admin.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)