forked from facebookarchive/wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fb-social-publisher-mentioning.php
424 lines (336 loc) · 14.4 KB
/
fb-social-publisher-mentioning.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?php
add_action( 'admin_init','fb_friend_page_autocomplete' );
add_filter( 'the_content', 'fb_social_publisher_mentioning_output', 30 );
add_action( 'wp_ajax_my_action', 'fb_friend_page_autocomplete' );
function fb_friend_page_autocomplete() {
global $facebook;
$output = array();
$nonce = '';
if (isset($_GET) && isset($_GET['autocompleteNonce']))
$nonce = $_GET['autocompleteNonce'];
// check to see if the submitted nonce matches with the
// generated nonce we created earlier
if ( wp_verify_nonce( $nonce, 'fb_autocomplete_nonce' ) ) {
if ( ! empty($_GET['fb-friends']) ) {
if ( ! isset( $facebook ) )
return;
if ( false === ( $friends = get_transient( 'fb_friends_' . $facebook->getUser() ) ) ) {
try {
$friends = $facebook->api('/me/friends', 'GET', array('ref' => 'fbwpp'));
}
catch (WP_FacebookApiException $e) {}
set_transient( 'fb_friends_' . $facebook->getUser(), $friends, 60*15 );
}
foreach($friends['data'] as $friend) {
$friends_clean[$friend['name']] = $friend['id'];
}
if (isset($_GET['q']) && isset($friends_clean)) {
$q = strtolower($_GET['q']);
if ($q) {
foreach ($friends_clean as $key => $value) {
if ( strpos( strtolower($key), $q ) !== false )
$results[] = array($key, $value);
}
}
}
if ( ! empty($results) ) {
$count = 0;
foreach ($results as $result) {
$output[$count]['id'] = '[' . esc_attr($result[1]) . '|' . esc_attr($result[0]) . ']';
$output[$count]['uid'] = $result[1];
$output[$count]['name'] = $result[0];
$count++;
}
}
if ( ! headers_sent() )
header( 'Content-Type: application/json; charset=utf-8', true );
echo json_encode($output);
exit;
}
if ( ! empty( $_GET['fb-pages'] ) ) {
if ( ! isset( $facebook ) )
return;
if ( false === ( $pages = get_transient( 'fb_pages_' . $_GET['q']) ) ) {
try {
$pages = $facebook->api( '/search', 'GET', array( 'type' => 'page', 'fields' => 'picture,name,id,likes', 'ref' => 'fbwpp', 'q' => $_GET['q'] ) );
}
catch (WP_FacebookApiException $e) {}
set_transient( 'fb_pages_' . $_GET['q'], $pages, 60*60 );
}
if ( isset($pages['data']) ) {
$pages_clean = array();
foreach($pages['data'] as $page) {
if (isset($page['name']) && isset($page['picture']) && isset($page['id']) && isset($page['likes']))
$pages_clean[$page['name']] = array($page['picture'], $page['name'], $page['id'], $page['likes']);
}
} else {
echo 'Error returning results.';
exit;
}
$results = array();
if ( isset($_GET['q']) ) {
$q = strtolower($_GET['q']);
if ( $q && isset($pages_clean) ) {
foreach ($pages_clean as $key => $value) {
if (strpos(strtolower($key), $q) !== false)
$results[] = array($key, $value);
}
}
}
if ( ! empty($results) ) {
$count = 0;
foreach ($results as $result) {
$item = array();
$item['id'] = '[' . esc_attr($result[1][2]) . '|' . esc_attr($result[1][1]) . ']';
$image_uri = esc_url_raw( $result[1][0], array('http','https') );
if ( $image_uri )
$item['image'] = $image_uri;
unset( $image_uri );
$item['name'] = $result[1][1];
$likes = absint( $result[1][3] );
if ( $likes )
$item['likes'] = $likes;
unset( $likes );
$output[$count] = $item;
unset($item);
$count++;
}
}
if ( ! headers_sent() )
header( 'Content-Type: application/json; charset=utf-8', true );
echo json_encode($output);
exit;
}
}
}
add_action( 'add_meta_boxes', 'fb_add_page_mention_box' );
add_action( 'save_post', 'fb_add_page_mention_box_save' );
function fb_add_page_mention_box() {
global $post;
global $facebook;
$options = get_option('fb_options');
if ($post->post_status == 'publish')
return;
if ( isset( $options['social_publisher']['enabled'] ) ) {
$post_types = get_post_types(array('public' => true));
unset($post_types['attachment']);
$post_types = array_values($post_types);
foreach ( $post_types as $post_type ) {
add_meta_box(
'fb_page_mention_box_id',
__( 'Mention Facebook Pages', 'facebook' ),
'fb_add_page_mention_box_content',
$post_type,
'side'
);
}
}
}
function fb_add_page_mention_box_content( $post ) {
global $facebook;
wp_enqueue_script('suggest');
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'fb_page_mention_box_noncename' );
$fb_user = fb_get_current_user();
if (isset ( $fb_user ) ) {
$perms = $facebook->api('/me/permissions', 'GET', array('ref' => 'fbwpp'));
}
if ( isset ( $fb_user ) && isset($perms['data'][0]['manage_pages']) && isset($perms['data'][0]['publish_actions']) && isset($perms['data'][0]['publish_stream'])) {
// The actual fields for data entry
echo '<label for="fb_page_mention_box_autocomplete">';
_e("Page's Name", 'facebook' );
echo '</label> ';
echo '<input type="text" class="widefat" id="suggest-pages" autocomplete="off" name="fb_page_mention_box_autocomplete" value="" size="44" placeholder="' . esc_attr__('Type to find a page.', 'facebook') . '" />';
echo '<label for="fb_page_mention_box_message">';
_e('Message', 'facebook' );
echo '</label> ';
echo '<input type="text" class="widefat" id="pages-mention-message" name="fb_page_mention_box_message" value="" size="44" placeholder="'.esc_attr__('Write something...').'" />';
echo '<p class="howto">';
if ( $post->post_type == 'page' ) {
echo esc_html( __( 'This will add the page and message to the Timeline of each Facebook Page mentioned. They will also appear in the contents of the page.', 'facebook' ) );
} else {
echo esc_html( __( 'This will add the post and message to the Timeline of each Facebook Page mentioned. They will also appear in the contents of the post.', 'facebook') );
}
echo '</p>';
} else {
echo '<p>' . esc_html( __( 'Facebook social publishing is enabled.', 'facebook' ) ) . '</p>';
echo '<p>' . esc_html( sprintf( __( '%s to get full functionality, including adding new Posts to your Timeline and mentioning friends Facebook Pages.', 'facebook' ), '<strong><a href="#" onclick="authFacebook(); return false;">' . esc_html( __( 'Link your Facebook account to your WordPress account', 'facebook' ) ) . '</a></strong>' ) ) . '</p>';
}
}
function fb_add_page_mention_box_save( $post_id ) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
$fb_user = fb_get_current_user();
if (!$fb_user)
return;
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( empty($_POST['fb_page_mention_box_noncename']) || !wp_verify_nonce( $_POST['fb_page_mention_box_noncename'], plugin_basename( __FILE__ ) ) )
return;
// Check permissions
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return;
}
else {
if ( !current_user_can( 'edit_post', $post_id ) )
return;
}
// OK, we're authenticated: we need to find and save the data
if ( isset ( $_POST ) && isset( $_POST['fb_page_mention_box_autocomplete'] ) ) {
$autocomplete_data = $_POST['fb_page_mention_box_autocomplete'];
preg_match_all(
'/\[(.*?)\|(.*?)\]/su',
$autocomplete_data,
$page_details,
PREG_SET_ORDER // formats data into an array of posts
);
// probably using add_post_meta(), update_post_meta(), or
// a custom table (see Further Reading section below)
$pages_details_meta = array();
foreach($page_details as $page_detail) {
$pages_details_meta[] = array('id' => sanitize_text_field($page_detail[1]), 'name' => sanitize_text_field($page_detail[2]));
}
update_post_meta($post_id, 'fb_mentioned_pages', $pages_details_meta );
update_post_meta($post_id, 'fb_mentioned_pages_message', sanitize_text_field($_POST['fb_page_mention_box_message']) );
}
}
add_action( 'add_meta_boxes', 'fb_add_friend_mention_box' );
add_action( 'save_post', 'fb_add_friend_mention_box_save' );
function fb_add_friend_mention_box() {
global $post;
global $facebook;
$options = get_option('fb_options');
if ($post->post_status == 'publish')
return;
if ( isset( $options['social_publisher']['enabled'] ) ) {
$post_types = get_post_types(array('public' => true));
unset($post_types['attachment']);
$post_types = array_values($post_types);
foreach ( $post_types as $post_type ) {
add_meta_box(
'fb_friend_mention_box_id',
__( 'Mention Facebook Friends', 'facebook' ),
'fb_add_friend_mention_box_content',
$post_type,
'side'
);
}
}
}
function fb_add_friend_mention_box_content( $post ) {
global $facebook;
wp_enqueue_script('suggest');
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'fb_friend_mention_box_noncename' );
$fb_user = fb_get_current_user();
if ( isset( $fb_user ) )
$perms = $facebook->api('/me/permissions', 'GET', array('ref' => 'fbwpp'));
if ( isset ( $fb_user ) && isset($perms['data'][0]['manage_pages']) && isset($perms['data'][0]['publish_actions']) && isset($perms['data'][0]['publish_stream'])) {
// The actual fields for data entry
echo '<label for="suggest-friends">';
echo esc_html__( "Friend's Name", 'facebook' );
echo '</label> ';
echo '<input type="text" class="widefat" id="suggest-friends" autocomplete="off" name="fb_friend_mention_box_autocomplete" value="" size="44" placeholder="' . esc_attr__( 'Type to find a friend.', 'facebook' ) . '" />';
echo '<label for="friends-mention-message">';
echo esc_html__('Message', 'facebook' );
echo '</label> ';
echo '<input type="text" class="widefat" id="friends-mention-message" name="fb_friend_mention_box_message" value="" size="44" placeholder="' . esc_attr__( 'Write something...', 'facebook' ) . '" />';
echo '<p class="howto">';
if ( $post->post_type == 'page' ) {
echo esc_html__( 'This will add the page and message to the Timeline of each friend mentioned. They will also appear in the contents of the page.', 'facebook' );
} else {
echo esc_html__( 'This will add the post and message to the Timeline of each friend mentioned. They will also appear in the contents of the post.', 'facebook' );
}
echo '</p>';
} else {
echo '<p>' . esc_html( __('Facebook social publishing is enabled.', 'facebook') ) .'</p>';
echo '<p>' . esc_html( sprintf( __( '%s to get full functionality, including adding new Posts to your Timeline and mentioning friends Facebook Pages.', 'facebook'), '<a href="#" onclick="authFacebook(); return false;"><strong>' . esc_html( __( 'Link your Facebook account to your WordPress account', 'facebook' ) ) . ' </strong></a>' ) ) .'</p>';
}
}
function fb_add_friend_mention_box_save( $post_id ) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
$fb_user = fb_get_current_user();
if (!$fb_user)
return;
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( empty($_POST['fb_friend_mention_box_noncename']) || !wp_verify_nonce( $_POST['fb_friend_mention_box_noncename'], plugin_basename( __FILE__ ) ) )
return;
// Check permissions
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return;
}
else {
if ( !current_user_can( 'edit_post', $post_id ) )
return;
}
// OK, we're authenticated: we need to find and save the data
if ( isset ($_POST) && isset( $_POST['fb_page_mention_box_autocomplete'] ) ) {
$autocomplete_data = $_POST['fb_friend_mention_box_autocomplete'];
preg_match_all(
'/\[(.*?)\|(.*?)\]/su',
$autocomplete_data,
$friend_details,
PREG_SET_ORDER // formats data into an array of posts
);
// probably using add_post_meta(), update_post_meta(), or
// a custom table (see Further Reading section below)
$friends_details_meta = array();
foreach($friend_details as $friend_detail) {
$friends_details_meta[] = array('id' => sanitize_text_field($friend_detail[1]), 'name' => sanitize_text_field($friend_detail[2]));
}
update_post_meta($post_id, 'fb_mentioned_friends', $friends_details_meta);
update_post_meta($post_id, 'fb_mentioned_friends_message', sanitize_text_field($_POST['fb_friend_mention_box_message']));
}
}
function fb_social_publisher_mentioning_output($content) {
global $post;
$options = get_option('fb_options');
if( $post ) {
$fb_mentioned_pages = get_post_meta($post->ID, 'fb_mentioned_pages', true);
$fb_mentioned_friends = get_post_meta($post->ID, 'fb_mentioned_friends', true);
$mentions_entities = '';
if ( ! empty($fb_mentioned_friends) ){
foreach( $fb_mentioned_friends as $fb_mentioned_friend ) {
$mentions_entities .= '<a target="_blank" href="http://www.facebook.com/' . esc_attr($fb_mentioned_friend['id']) . '" title="'.sprintf(esc_attr__('Click to visit %s\'s profile on Facebook.','facebook'), esc_attr($fb_mentioned_friend['name'])) .'"><img src="http://graph.facebook.com/' . esc_attr($fb_mentioned_friend['id']) . '/picture?width=16&height=16" width="16" height="16"> ' . esc_html($fb_mentioned_friend['name']) . '</a> ';
}
}
if ( ! empty($fb_mentioned_pages) ) {
foreach( $fb_mentioned_pages as $fb_mentioned_page ) {
$mentions_entities .= '<a target="_blank" href="http://www.facebook.com/' . esc_attr($fb_mentioned_page['id']) . '" title="'.sprintf(esc_attr__('Click to visit %s\'s profile on Facebook.','facebook'), esc_attr($fb_mentioned_page['name'])).'"><img src="http://graph.facebook.com/' . esc_attr($fb_mentioned_page['id']) . '/picture?width=16&height=16" width="16" height="16"> ' . esc_html($fb_mentioned_page['name']) . '</a> ';
}
}
if ($mentions_entities) {
$mentions = '<div class="fb-mentions entry-meta">' . $mentions_entities . 'mentioned.</div>';
$new_content = '';
if ( isset($options['social_publisher']) ) {
switch ($options['social_publisher']['mentions_position']) {
case 'top':
$new_content = $mentions . $content;
break;
case 'bottom':
$new_content = $content . $mentions;
break;
case 'both':
$new_content = $mentions . $content;
$new_content .= $mentions;
break;
default:
$new_content = $content;
}
if ( empty( $options['social_publisher']['mentions_show_on_homepage'] ) && is_singular() )
$content = $new_content;
else if ( isset($options['social_publisher']['mentions_show_on_homepage']) )
$content = $new_content;
}
}
}
return $content;
}