source: trunk/www.guidonia.net/wp/wp-content/plugins/wordbook/wordbook.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 38.8 KB
Line 
1<?php
2/*
3Plugin Name: Wordbook
4Plugin URI: http://www.tsaiberspace.net/projects/wordpress/wordbook/
5Description: Cross-post your blog updates to your Facebook account. Navigate to <a href="admin.php?page=wordbook">Settings &rarr; Wordbook</a> for configuration.
6Author: Robert Tsai
7Author URI: http://www.tsaiberspace.net/
8Version: 0.14.7
9*/
10
11/*
12 * Copyright 2009 Robert Tsai (email : wordpress@tsaiberspace.net)
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the Free
16 * Software Foundation; either version 2 of the License, or (at your option)
17 * any later version.
18 *
19 * This program is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22 * more details.
23 *
24 * You should have received a copy of the GNU General Public License along with
25 * this program; if not, write to the Free Software Foundation, Inc., 51
26 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29global $table_prefix, $wp_version;
30require_once(ABSPATH . WPINC . '/pluggable.php');
31
32define('WORDBOOK_DEBUG', false);
33define('WORDBOOK_TESTING', false);
34
35$facebook_config['debug'] = WORDBOOK_TESTING && !$_POST['action'];
36
37define('WORDBOOK_FB_APIKEY', '21e0776b27318e5867ec665a5b18a850');
38define('WORDBOOK_FB_SECRET', 'f342d13c5094bef736842e4832420e8f');
39define('WORDBOOK_TEMPLATE_ID', 53818207731);
40define('WORDBOOK_FB_APIVERSION', '1.0');
41define('WORDBOOK_FB_DOCPREFIX',
42 'http://wiki.developers.facebook.com/documentation.php?v='
43 . WORDBOOK_FB_APIVERSION . '&method=');
44define('WORDBOOK_FB_MAXACTIONLEN', 60);
45define('WORDBOOK_FB_PUBLISH_STREAM', 'publish_stream');
46
47define('WORDBOOK_OPTIONS', 'wordbook_options');
48define('WORDBOOK_OPTION_SCHEMAVERS', 'schemavers');
49
50define('WORDBOOK_ERRORLOGS', $table_prefix . 'wordbook_errorlogs');
51define('WORDBOOK_POSTLOGS', $table_prefix . 'wordbook_postlogs');
52define('WORDBOOK_USERDATA', $table_prefix . 'wordbook_userdata');
53
54define('WORDBOOK_EXCERPT_SHORTSTORY', 256);
55define('WORDBOOK_EXCERPT_WIDEBOX', 96);
56define('WORDBOOK_EXCERPT_NARROWBOX', 40);
57
58define('WORDBOOK_MINIMUM_ADMIN_LEVEL', 2); /* Author role or above. */
59define('WORDBOOK_OPTIONS_PAGENAME', 'wordbook');
60define('WORDBOOK_OPTIONS_URL', 'admin.php?page=' . WORDBOOK_OPTIONS_PAGENAME);
61
62define('WORDBOOK_SCHEMA_VERSION', 5);
63
64$wordbook_wp_version_tuple = explode('.', $wp_version);
65define('WORDBOOK_WP_VERSION', $wordbook_wp_version_tuple[0] * 10 +
66 $wordbook_wp_version_tuple[1]);
67
68if (function_exists('json_encode')) {
69 define('WORDBOOK_JSON_ENCODE', 'PHP');
70} else {
71 define('WORDBOOK_JSON_ENCODE', 'Wordbook');
72}
73
74if (function_exists('simplexml_load_string')) {
75 define('WORDBOOK_SIMPLEXML', 'PHP');
76} else {
77 define('WORDBOOK_SIMPLEXML', 'Facebook');
78}
79
80if (substr(phpversion(), 0, 2) == '5.') {
81 define('FACEBOOK_PHP_API', 'PHP5');
82} else {
83 define('FACEBOOK_PHP_API', 'PHP4');
84}
85
86function wordbook_debug($message) {
87 if (WORDBOOK_DEBUG) {
88 $fp = fopen('/tmp/wb.log', 'a');
89 $date = date('D M j, g:i:s a');
90 fwrite($fp, "$date: $message");
91 fclose($fp);
92 }
93}
94
95function wordbook_load_apis() {
96 if (defined('WORDBOOK_APIS_LOADED')) {
97 return;
98 }
99 if (WORDBOOK_JSON_ENCODE == 'Wordbook') {
100 function json_encode($var) {
101 if (is_array($var)) {
102 $encoded = '{';
103 $first = true;
104 foreach ($var as $key => $value) {
105 if (!$first) {
106 $encoded .= ',';
107 } else {
108 $first = false;
109 }
110 $encoded .= "\"$key\":"
111 . json_encode($value);
112 }
113 $encoded .= '}';
114 return $encoded;
115 }
116 if (is_string($var)) {
117 return "\"$var\"";
118 }
119 return $var;
120 }
121 }
122 if (FACEBOOK_PHP_API == 'PHP5') {
123 if (!class_exists('Facebook')) {
124 /* Defend against other plugins. */
125 require_once('facebook-platform/php/facebook.php');
126 }
127 require_once('wordbook_php5.php');
128 }
129 define('WORDBOOK_APIS_LOADED', true);
130}
131
132/******************************************************************************
133 * Wordbook options.
134 */
135
136function wordbook_options() {
137 return get_option(WORDBOOK_OPTIONS);
138}
139
140function wordbook_set_options($options) {
141 update_option(WORDBOOK_OPTIONS, $options);
142}
143
144function wordbook_get_option($key) {
145 $options = wordbook_options();
146 return isset($options[$key]) ? $options[$key] : null;
147}
148
149function wordbook_set_option($key, $value) {
150 $options = wordbook_options();
151 $options[$key] = $value;
152 wordbook_set_options($options);
153}
154
155function wordbook_delete_option($key) {
156 $options = wordbook_options();
157 unset($options[$key]);
158 update_option(WORDBOOK_OPTIONS, $options);
159}
160
161/******************************************************************************
162 * Plugin deactivation - tidy up database.
163 */
164
165function wordbook_deactivate() {
166 global $wpdb;
167
168 wp_cache_flush();
169 $errors = array();
170 foreach (array(
171 WORDBOOK_ERRORLOGS,
172 WORDBOOK_POSTLOGS,
173 WORDBOOK_USERDATA,
174 ) as $tablename) {
175 $result = $wpdb->query("
176 DROP TABLE IF EXISTS $tablename
177 ");
178 if ($result === false)
179 $errors[] = "Failed to drop $tablename";
180 }
181 delete_option(WORDBOOK_OPTIONS);
182 wp_cache_flush();
183
184 if ($errors) {
185 echo '<div id="message" class="updated fade">' . "\n";
186 foreach ($errors as $errormsg) {
187 _e("$errormsg<br />\n");
188 }
189 echo "</div>\n";
190 }
191}
192
193/******************************************************************************
194 * DB schema.
195 */
196
197function wordbook_upgrade() {
198 global $wpdb, $table_prefix;
199
200 $options = wordbook_options();
201
202 if ($options && isset($options[WORDBOOK_OPTION_SCHEMAVERS]) &&
203 $options[WORDBOOK_OPTION_SCHEMAVERS] ==
204 WORDBOOK_SCHEMA_VERSION) {
205 return;
206 }
207
208 wp_cache_flush();
209 if (!$options || !isset($options[WORDBOOK_OPTION_SCHEMAVERS]) ||
210 $options[WORDBOOK_OPTION_SCHEMAVERS] < 5) {
211 $errors = array();
212
213 foreach (array(
214 WORDBOOK_ERRORLOGS,
215 WORDBOOK_POSTLOGS,
216 WORDBOOK_USERDATA,
217 $table_prefix . 'wordbook_onetimecode',
218 ) as $tablename) {
219 $result = $wpdb->query("
220 DROP TABLE IF EXISTS $tablename
221 ");
222 if ($result === false)
223 $errors[] = "Failed to drop $tablename";
224 }
225
226 $result = $wpdb->query('
227 CREATE TABLE ' . WORDBOOK_POSTLOGS . ' (
228 `postid` BIGINT(20) NOT NULL
229 , `timestamp` TIMESTAMP
230 )
231 ');
232 if ($result === false)
233 $errors[] = 'Failed to create ' . WORDBOOK_POSTLOGS;
234
235 $result = $wpdb->query('
236 CREATE TABLE ' . WORDBOOK_ERRORLOGS . ' (
237 `timestamp` TIMESTAMP
238 , `user_ID` BIGINT(20) UNSIGNED NOT NULL
239 , `method` VARCHAR(255) NOT NULL
240 , `error_code` INT NOT NULL
241 , `error_msg` VARCHAR(80) NOT NULL
242 , `postid` BIGINT(20) NOT NULL
243 )
244 ');
245 if ($result === false)
246 $errors[] = 'Failed to create ' . WORDBOOK_ERRORLOGS;
247
248 $result = $wpdb->query('
249 CREATE TABLE ' . WORDBOOK_USERDATA . ' (
250 `user_ID` BIGINT(20) UNSIGNED NOT NULL
251 , `use_facebook` TINYINT(1) NOT NULL DEFAULT 1
252 , `onetime_data` LONGTEXT NOT NULL
253 , `facebook_error` LONGTEXT NOT NULL
254 , `secret` VARCHAR(80) NOT NULL
255 , `session_key` VARCHAR(80) NOT NULL
256 )
257 ');
258 if ($result === false)
259 $errors[] = 'Failed to create ' . WORDBOOK_USERDATA;
260
261 if ($errors) {
262 echo '<div id="message" class="updated fade">' . "\n";
263 foreach ($errors as $errormsg) {
264 _e("$errormsg<br />\n");
265 }
266 echo "</div>\n";
267 return;
268 }
269
270 $options = array(
271 WORDBOOK_OPTION_SCHEMAVERS => 5,
272 );
273 }
274
275 wordbook_set_options($options);
276 wp_cache_flush();
277}
278
279function wordbook_delete_user($user_id) {
280 global $wpdb;
281 $errors = array();
282 foreach (array(
283 WORDBOOK_USERDATA,
284 WORDBOOK_ERRORLOGS,
285 ) as $tablename) {
286 $result = $wpdb->query('
287 DELETE FROM ' . $tablename . '
288 WHERE user_ID = ' . $user_id . '
289 ');
290 if ($result === false)
291 $errors[] = "Failed to remove user $user_id from $tablename";
292 }
293 if ($errors) {
294 echo '<div id="message" class="updated fade">' . "\n";
295 foreach ($errors as $errormsg) {
296 _e("$errormsg<br />\n");
297 }
298 echo "</div>\n";
299 }
300}
301
302/******************************************************************************
303 * Wordbook user data.
304 */
305
306function wordbook_get_userdata($user_id) {
307 global $wpdb;
308
309 $rows = $wpdb->get_results('
310 SELECT *
311 FROM ' . WORDBOOK_USERDATA . '
312 WHERE user_ID = ' . $user_id . '
313 ');
314 if ($rows) {
315 $rows[0]->onetime_data = unserialize($rows[0]->onetime_data);
316 $rows[0]->facebook_error =
317 unserialize($rows[0]->facebook_error);
318 $rows[0]->secret = unserialize($rows[0]->secret);
319 $rows[0]->session_key = unserialize($rows[0]->session_key);
320 return $rows[0];
321 }
322 return null;
323}
324
325function wordbook_set_userdata($use_facebook, $onetime_data, $facebook_error,
326 $secret, $session_key) {
327 global $user_ID, $wpdb;
328 wordbook_delete_userdata();
329 $result = $wpdb->query("
330 INSERT INTO " . WORDBOOK_USERDATA . " (
331 user_ID
332 , use_facebook
333 , onetime_data
334 , facebook_error
335 , secret
336 , session_key
337 ) VALUES (
338 " . $user_ID . "
339 , " . ($use_facebook ? 1 : 0) . "
340 , '" . serialize($onetime_data) . "'
341 , '" . serialize($facebook_error) . "'
342 , '" . serialize($secret) . "'
343 , '" . serialize($session_key) . "'
344 )
345 ");
346}
347
348function wordbook_update_userdata($wbuser) {
349 return wordbook_set_userdata($wbuser->use_facebook,
350 $wbuser->onetime_data, $wbuser->facebook_error, $wbuser->secret,
351 $wbuser->session_key);
352}
353
354function wordbook_set_userdata_facebook_error($wbuser, $method, $error_code,
355 $error_msg, $postid) {
356 $wbuser->facebook_error = array(
357 'method' => $method,
358 'error_code' => $error_code,
359 'error_msg' => $error_msg,
360 'postid' => $postid,
361 );
362 wordbook_update_userdata($wbuser);
363 wordbook_appendto_errorlogs($method, $error_code, $error_msg, $postid);
364}
365
366function wordbook_clear_userdata_facebook_error($wbuser) {
367 $wbuser->facebook_error = null;
368 return wordbook_update_userdata($wbuser);
369}
370
371function wordbook_delete_userdata() {
372 global $user_ID;
373 wordbook_delete_user($user_ID);
374}
375
376/******************************************************************************
377 * Post logs - record time of last post to Facebook
378 */
379
380function wordbook_trim_postlogs() {
381 /* Forget that something has been posted to Facebook if it's been
382 * longer than some delta of time. */
383 global $wpdb;
384 $result = $wpdb->query('
385 DELETE FROM ' . WORDBOOK_POSTLOGS . '
386 WHERE timestamp < DATE_SUB(CURDATE(), INTERVAL 1 DAY)
387 ');
388}
389
390function wordbook_postlogged($postid) {
391 global $wpdb;
392 $rows = $wpdb->get_results('
393 SELECT *
394 FROM ' . WORDBOOK_POSTLOGS . '
395 WHERE postid = ' . $postid . '
396 AND timestamp > DATE_SUB(CURDATE(), INTERVAL 1 DAY)
397 ');
398 return $rows ? true : false;
399}
400
401function wordbook_insertinto_postlogs($postid) {
402 global $wpdb;
403 wordbook_deletefrom_postlogs($postid);
404 if (!WORDBOOK_TESTING) {
405 $result = $wpdb->query('
406 INSERT INTO ' . WORDBOOK_POSTLOGS . ' (
407 postid
408 ) VALUES (
409 ' . $postid . '
410 )
411 ');
412 }
413}
414
415function wordbook_deletefrom_postlogs($postid) {
416 global $wpdb;
417 $result = $wpdb->query('
418 DELETE FROM ' . WORDBOOK_POSTLOGS . '
419 WHERE postid = ' . $postid . '
420 ');
421}
422
423/******************************************************************************
424 * Error logs - record errors
425 */
426
427function wordbook_hyperlinked_method($method) {
428 return '<a href="'
429 . WORDBOOK_FB_DOCPREFIX . $method . '"'
430 . ' title="Facebook API documentation" target="facebook"'
431 . '>'
432 . $method
433 . '</a>';
434}
435
436function wordbook_trim_errorlogs() {
437 global $wpdb;
438 $result = $wpdb->query('
439 DELETE FROM ' . WORDBOOK_ERRORLOGS . '
440 WHERE timestamp < DATE_SUB(CURDATE(), INTERVAL 7 DAY)
441 ');
442}
443
444function wordbook_clear_errorlogs() {
445 global $user_ID, $wpdb;
446 $result = $wpdb->query('
447 DELETE FROM ' . WORDBOOK_ERRORLOGS . '
448 WHERE user_ID = ' . $user_ID . '
449 ');
450 if ($result === false) {
451 echo '<div id="message" class="updated fade">';
452 _e('Failed to clear error logs.');
453 echo "</div>\n";
454 }
455}
456
457function wordbook_appendto_errorlogs($method, $error_code, $error_msg,
458 $postid) {
459 global $user_ID, $wpdb;
460 if ($postid == null) {
461 $postid = 0;
462 $user_id = $user_ID;
463 } else {
464 $post = get_post($postid);
465 $user_id = $post->post_author;
466 }
467 $result = $wpdb->query('
468 INSERT INTO ' . WORDBOOK_ERRORLOGS . ' (
469 user_ID
470 , method
471 , error_code
472 , error_msg
473 , postid
474 ) VALUES (
475 ' . $user_id . '
476 , "' . $method . '"
477 , ' . $error_code . '
478 , "' . $error_msg . '"
479 , ' . $postid . '
480 )
481 ');
482}
483
484function wordbook_deletefrom_errorlogs($postid) {
485 global $wpdb;
486 $result = $wpdb->query('
487 DELETE FROM ' . WORDBOOK_ERRORLOGS . '
488 WHERE postid = ' . $postid . '
489 ');
490}
491
492function wordbook_render_errorlogs() {
493 global $user_ID, $wpdb;
494
495 $rows = $wpdb->get_results('
496 SELECT *
497 FROM ' . WORDBOOK_ERRORLOGS . '
498 WHERE user_ID = ' . $user_ID . '
499 ORDER BY timestamp
500 ');
501 if ($rows) {
502?>
503
504 <h3><?php _e('Errors'); ?></h3>
505 <div class="wordbook_errors">
506
507 <p>
508 Your blog is OK, but Wordbook was unable to update your Mini-Feed:
509 </p>
510
511 <table class="wordbook_errorlogs">
512 <tr>
513 <th>Timestamp</th>
514 <th>Post</th>
515 <th>Method</th>
516 <th>Error Code</th>
517 <th>Error Message</th>
518 </tr>
519
520<?php
521 foreach ($rows as $row) {
522 $hyperlinked_post = '';
523 if (($post = get_post($row->postid))) {
524 $hyperlinked_post = '<a href="'
525 . get_permalink($row->postid) . '">'
526 . get_the_title($row->postid) . '</a>';
527 }
528 $hyperlinked_method=
529 wordbook_hyperlinked_method($row->method);
530?>
531
532 <tr>
533 <td><?php echo $row->timestamp; ?></td>
534 <td><?php echo $hyperlinked_post; ?></td>
535 <td><?php echo $hyperlinked_method; ?></td>
536 <td><?php echo $row->error_code; ?></td>
537 <td><?php echo $row->error_msg; ?></td>
538 </tr>
539
540<?php
541 }
542?>
543
544 </table>
545
546 <form action="<?php echo WORDBOOK_OPTIONS_URL; ?>" method="post">
547 <input type="hidden" name="action" value="clear_errorlogs" />
548 <p class="submit" style="text-align: center;">
549 <input type="submit" value="<?php _e('Clear Errors'); ?>" />
550 </p>
551 </form>
552
553 </div>
554
555<?php
556 }
557}
558
559/******************************************************************************
560 * Wordbook setup and administration.
561 */
562
563function wordbook_admin_load() {
564 if (!$_POST['action'])
565 return;
566
567 switch ($_POST['action']) {
568
569 case 'one_time_code':
570 $token = $_POST['one_time_code'];
571 $fbclient = wordbook_fbclient(null);
572 list($result, $error_code, $error_msg) =
573 wordbook_fbclient_getsession($fbclient, $token);
574 if ($result) {
575 wordbook_clear_errorlogs();
576 $onetime_data = null;
577 $secret = $result['secret'];
578 $session_key = $result['session_key'];
579 } else {
580 $onetime_data = array(
581 'onetimecode' => $token,
582 'error_code' => $error_code,
583 'error_msg' => $error_msg,
584 );
585 $secret = null;
586 $session_key = null;
587 }
588 $use_facebook = true;
589 $facebook_error = null;
590 wordbook_set_userdata($use_facebook, $onetime_data,
591 $facebook_error, $secret, $session_key);
592 wp_redirect(WORDBOOK_OPTIONS_URL);
593 break;
594
595 case 'delete_userdata':
596 wordbook_delete_userdata();
597 wp_redirect(WORDBOOK_OPTIONS_URL);
598 break;
599
600 case 'clear_errorlogs':
601 wordbook_clear_errorlogs();
602 wp_redirect(WORDBOOK_OPTIONS_URL);
603 break;
604
605 case 'no_facebook':
606 wordbook_set_userdata(false, null, null, null);
607 wp_redirect(WORDBOOK_OPTIONS_URL);
608 break;
609 }
610
611 exit;
612}
613
614function wordbook_admin_head() {
615?>
616 <style type="text/css">
617 .wordbook_setup { margin: 0 3em; }
618 .wordbook_notices { margin: 0 3em; }
619 .wordbook_status { margin: 0 3em; }
620 .wordbook_errors { margin: 0 3em; }
621 .wordbook_thanks { margin: 0 3em; }
622 .wordbook_thanks ul { margin: 1em 0 1em 2em; list-style-type: disc; }
623 .wordbook_support { margin: 0 3em; }
624 .wordbook_support ul { margin: 1em 0 1em 2em; list-style-type: disc; }
625 .facebook_picture {
626 float: right;
627 border: 1px solid black;
628 padding: 2px;
629 margin: 0 0 1ex 2ex;
630 }
631 .wordbook_errorcolor { color: #c00; }
632 table.wordbook_errorlogs { text-align: center; }
633 table.wordbook_errorlogs th, table.wordbook_errorlogs td {
634 padding: 0.5ex 1.5em;
635 }
636 table.wordbook_errorlogs th { background-color: #999; }
637 table.wordbook_errorlogs td { background-color: #f66; }
638 </style>
639<?php
640}
641
642function wordbook_option_notices() {
643 global $user_ID, $wp_version;
644 wordbook_upgrade();
645 wordbook_trim_postlogs();
646 wordbook_trim_errorlogs();
647 $errormsg = null;
648 if (WORDBOOK_WP_VERSION < 22) {
649 $errormsg = sprintf(__('Wordbook requires'
650 . ' <a href="%s">WordPress</a>-2.2'
651 . ' or newer (you appear to be running version %s).'),
652 'http://wordpress.org/download/', $wp_version);
653 } else if (!($options = wordbook_options()) ||
654 !isset($options[WORDBOOK_OPTION_SCHEMAVERS]) ||
655 $options[WORDBOOK_OPTION_SCHEMAVERS] <
656 WORDBOOK_SCHEMA_VERSION ||
657 !($wbuser = wordbook_get_userdata($user_ID)) ||
658 ($wbuser->use_facebook && !$wbuser->session_key)) {
659 $errormsg = sprintf(__('<a href="%s">Wordbook</a>'
660 . ' needs to be set up.'),
661 WORDBOOK_OPTIONS_URL);
662 } else if ($wbuser->facebook_error) {
663 $method = $wbuser->facebook_error['method'];
664 $error_code = $wbuser->facebook_error['error_code'];
665 $error_msg = $wbuser->facebook_error['error_msg'];
666 $postid = $wbuser->facebook_error['postid'];
667 $suffix = '';
668 if ($postid != null && ($post = get_post($postid))) {
669 wordbook_deletefrom_postlogs($postid);
670 $suffix = ' for <a href="'
671 . get_permalink($postid) . '">'
672 . get_the_title($postid) . '</a>';
673 }
674 $errormsg = sprintf(__('<a href="%s">Wordbook</a>'
675 . ' failed to communicate with Facebook' . $suffix . ':'
676 . ' method = %s, error_code = %d (%s).'
677 . " Your blog is OK, but Facebook didn't get"
678 . ' the update.'),
679 WORDBOOK_OPTIONS_URL,
680 wordbook_hyperlinked_method($method),
681 $error_code,
682 $error_msg);
683 wordbook_clear_userdata_facebook_error($wbuser);
684 }
685
686 if ($errormsg) {
687?>
688
689 <h3><?php _e('Notices'); ?></h3>
690
691 <div class="wordbook_notices" style="background-color: #f66;">
692 <p><?php echo $errormsg; ?></p>
693 </div>
694
695<?php
696 }
697}
698
699function wordbook_option_setup($wbuser) {
700?>
701
702 <h3><?php _e('Setup'); ?></h3>
703 <div class="wordbook_setup">
704
705 <p>Wordbook needs to be linked to your Facebook account. This link will be used to publish your WordPress blog updates to your Mini-Feed and your friends' News Feeds, and will not be used for any other purpose.</p>
706
707 <p>First, log in to your Facebook account to generate a one-time code. Record the one-time code and return to this page:</p>
708
709 <div style="text-align: center;"><a href="http://www.facebook.com/code_gen.php?v=<?php echo WORDBOOK_FB_APIVERSION; ?>&api_key=<?php echo WORDBOOK_FB_APIKEY; ?>" target="facebook"><img src="http://static.ak.facebook.com/images/devsite/facebook_login.gif" /></a></div>
710
711 <form action="<?php echo WORDBOOK_OPTIONS_URL; ?>" method="post">
712 <p>Next, enter the one-time code obtained in the previous step:</p>
713 <div style="text-align: center;">
714 <input type="text" name="one_time_code" id="one_time_code"
715 value="<?php echo $wbuser->onetime_data['onetimecode']; ?>" size="9" />
716 </div>
717 <input type="hidden" name="action" value="one_time_code" />
718
719<?php
720 if ($wbuser) {
721 wordbook_render_onetimeerror($wbuser);
722 $wbuser->onetime_data = null;
723 wordbook_update_userdata($wbuser);
724 }
725?>
726
727 <p style="text-align: center;"><input type="submit" value="<?php _e('Submit &raquo;'); ?>" /></p>
728 </form>
729
730 <form action="<?php echo WORDBOOK_OPTIONS_URL; ?>" method="post">
731 <p>Or, if you don't use Facebook or don't want to post to Facebook:</p>
732 <input type="hidden" name="action" value="no_facebook" />
733 <p style="text-align: center;"><input type="submit" value="<?php _e('I don\'t want to use Facebook &raquo;'); ?>" /></p>
734 </form>
735
736 </div>
737
738<?php
739}
740
741function wordbook_option_status($wbuser) {
742?>
743
744 <h3><?php _e('Status'); ?></h3>
745 <div class="wordbook_status">
746
747<?php
748 $show_paypal = false;
749 $fbclient = wordbook_fbclient($wbuser);
750 list($fbuid, $users, $error_code, $error_msg) =
751 wordbook_fbclient_getinfo($fbclient, array(
752 'is_app_user',
753 'first_name',
754 'name',
755 'status',
756 'pic',
757 ));
758 $profile_url = "http://www.facebook.com/profile.php?id=$fbuid";
759
760 if ($fbuid) {
761 if (is_array($users)) {
762 $user = $users[0];
763
764 if ($user['pic']) {
765?>
766
767 <div class="facebook_picture">
768 <a href="<?php echo $profile_url; ?>" target="facebook">
769 <img src="<?php echo $user['pic']; ?>" /></a>
770 </div>
771
772<?php
773 }
774
775 if (!($name = $user['first_name']))
776 $name = $user['name'];
777
778 if ($user['status']['message']) {
779?>
780
781 <p>
782 <a href="<?php echo $profile_url; ?>"><?php echo $name; ?></a>
783 <i><?php echo $user['status']['message']; ?></i>
784 (<?php echo date('D M j, g:i a', $user['status']['time']); ?>).
785 </p>
786
787<?php
788 } else {
789?>
790
791 <p>
792 Hi,
793 <a href="<?php echo $profile_url; ?>"><?php echo $name; ?></a>!
794 </p>
795
796<?php
797 }
798
799
800 if ($user['is_app_user']) {
801 $show_paypal = true;
802 wordbook_fbclient_setfbml($wbuser, $fbclient,
803 null, null);
804?>
805
806 <p>Wordbook appears to be configured and working just fine.</p>
807
808<?php
809 list($has_permission, $error_code, $error_msg) =
810 wordbook_fbclient_has_app_permission(
811 $fbclient, WORDBOOK_FB_PUBLISH_STREAM);
812 if (!$has_permission) {
813?>
814
815 <p>
816 Wordbook requires authorization to publish stories to your
817 Facebook News Feed:
818 </p>
819
820 <div style="text-align: center;">
821 <a href="http://www.facebook.com/authorize.php?v=<?php echo WORDBOOK_FB_APIVERSION; ?>&api_key=<?php echo WORDBOOK_FB_APIKEY; ?>&ext_perm=<?php echo WORDBOOK_FB_PUBLISH_STREAM; ?>" target="facebook">
822 <img src="http://static.ak.facebook.com/images/devsite/facebook_login.gif" />
823 </a>
824 </div>
825
826<?php
827 }
828?>
829 <p>If you like, you can start over from the beginning:</p>
830
831<?php
832 } else {
833?>
834
835 <p>Wordbook is able to connect to Facebook.</p>
836
837 <p>Next, add the <a href="http://www.facebook.com/apps/application.php?id=3353257731" target="facebook">Wordbook</a> application to your Facebook profile:</p>
838
839 <div style="text-align: center;"><a href="http://www.facebook.com/add.php?api_key=<?php echo WORDBOOK_FB_APIKEY; ?>" target="facebook"><img src="http://static.ak.facebook.com/images/devsite/facebook_login.gif" /></a></div>
840
841 <p>Or, you can start over from the beginning:</p>
842
843<?php
844 }
845 } else {
846?>
847
848 <p>Wordbook is configured and working, but <a href="http://developers.facebook.com/documentation.php?v=1.0&method=users.getInfo" target="facebook">facebook.users.getInfo</a> failed (no Facebook user for uid <?php echo $fbuid; ?>).</p>
849
850 <p>Try resetting the configuration:</p>
851
852<?php
853 }
854 } else {
855?>
856
857 <p>Failed to communicate with Facebook: <a href="http://developers.facebook.com/documentation.php?v=1.0&method=users.getLoggedInUser" target="facebook">error_code = <?php echo $error_code; ?> (<?php echo $error_msg; ?>)</a>.</p>
858
859 <p>Try resetting the configuration:</p>
860
861<?php
862 }
863?>
864
865 <form action="<?php echo WORDBOOK_OPTIONS_URL; ?>" method="post">
866 <input type="hidden" name="action" value="delete_userdata" />
867 <p style="text-align: center;"><input type="submit" value="<?php _e('Reset Configuration'); ?>" /></p>
868 </form>
869
870 </div>
871
872<?php
873 return array($show_paypal);
874}
875
876function wordbook_option_thanks($donors) {
877?>
878
879 <h3><?php _e('Thanks'); ?></h3>
880 <div class="wordbook_thanks">
881
882 Special thanks to:
883
884 <ul>
885
886<?php
887 foreach ($donors as $url => $title) {
888?>
889
890 <li><a href="<?php echo $url; ?>" target="_blank"><?php echo $title; ?></a></li>
891
892<?php
893 }
894?>
895
896 </ul>
897
898 If you find this plugin useful, please consider making a
899 donation to support its continued development; any amount is
900 welcome (for acknowledgement, please provide your URL as a note
901 on the PayPal form):
902
903 <div style="text-align: center; margin: 1em auto;">
904 <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
905 <input type="hidden" name="cmd" value="_s-xclick">
906 <input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but21.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
907 <img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
908 <input type="hidden" name="encrypted" value="-----BEGIN PKCS7-----MIIHLwYJKoZIhvcNAQcEoIIHIDCCBxwCAQExggEwMIIBLAIBADCBlDCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERMA8GA1UEAxQIbGl2ZV9hcGkxHDAaBgkqhkiG9w0BCQEWDXJlQHBheXBhbC5jb20CAQAwDQYJKoZIhvcNAQEBBQAEgYBUWkHYpAwvakglczL/Ad59cRgEq2dUA2rwW8Y7gwHExMnOJn1f7guOWKhkMd/yepZypX5SSVjdvioyJDJCyuyotidiyCdQes0fc1AwI1CEsdAP6dJD/02B3heGlbQmxoNPYKXIzhKUGN5zUVCJCrQkq6BBYpvx5cgJnPMor+koyzELMAkGBSsOAwIaBQAwgawGCSqGSIb3DQEHATAUBggqhkiG9w0DBwQIrBbFtSlBYJWAgYiz+/4bpOww9Hsw0a4j45Y5eeHKeqUNPiHmZT4RE0q4JPgHnP8FshcJiRXlNOwK99u9dX8C5KEk9mrLNHdc4QYMrjUWqVSmCKWYQCOudTGMas5q940y+vMaxAUqI0xHAZCvYm8xf4/+z4yGGkRSesObV7Lh0QEDlJ2/Eq/D8tZGxavK8roLvT2poIIDhzCCA4MwggLsoAMCAQICAQAwDQYJKoZIhvcNAQEFBQAwgY4xCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJDQTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEUMBIGA1UEChMLUGF5UGFsIEluYy4xEzARBgNVBAsUCmxpdmVfY2VydHMxETAPBgNVBAMUCGxpdmVfYXBpMRwwGgYJKoZIhvcNAQkBFg1yZUBwYXlwYWwuY29tMB4XDTA0MDIxMzEwMTMxNVoXDTM1MDIxMzEwMTMxNVowgY4xCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJDQTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEUMBIGA1UEChMLUGF5UGFsIEluYy4xEzARBgNVBAsUCmxpdmVfY2VydHMxETAPBgNVBAMUCGxpdmVfYXBpMRwwGgYJKoZIhvcNAQkBFg1yZUBwYXlwYWwuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDBR07d/ETMS1ycjtkpkvjXZe9k+6CieLuLsPumsJ7QC1odNz3sJiCbs2wC0nLE0uLGaEtXynIgRqIddYCHx88pb5HTXv4SZeuv0Rqq4+axW9PLAAATU8w04qqjaSXgbGLP3NmohqM6bV9kZZwZLR/klDaQGo1u9uDb9lr4Yn+rBQIDAQABo4HuMIHrMB0GA1UdDgQWBBSWn3y7xm8XvVk/UtcKG+wQ1mSUazCBuwYDVR0jBIGzMIGwgBSWn3y7xm8XvVk/UtcKG+wQ1mSUa6GBlKSBkTCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERMA8GA1UEAxQIbGl2ZV9hcGkxHDAaBgkqhkiG9w0BCQEWDXJlQHBheXBhbC5jb22CAQAwDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQUFAAOBgQCBXzpWmoBa5e9fo6ujionW1hUhPkOBakTr3YCDjbYfvJEiv/2P+IobhOGJr85+XHhN0v4gUkEDI8r2/rNk1m0GA8HKddvTjyGw/XqXa+LSTlDYkqI8OwR8GEYj4efEtcRpRYBxV8KxAW93YDWzFGvruKnnLbDAF6VR5w/cCMn5hzGCAZowggGWAgEBMIGUMIGOMQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExFjAUBgNVBAcTDU1vdW50YWluIFZpZXcxFDASBgNVBAoTC1BheVBhbCBJbmMuMRMwEQYDVQQLFApsaXZlX2NlcnRzMREwDwYDVQQDFAhsaXZlX2FwaTEcMBoGCSqGSIb3DQEJARYNcmVAcGF5cGFsLmNvbQIBADAJBgUrDgMCGgUAoF0wGAYJKoZIhvcNAQkDMQsGCSqGSIb3DQEHATAcBgkqhkiG9w0BCQUxDxcNMDcwODIzMDUzMTE3WjAjBgkqhkiG9w0BCQQxFgQUpvpxTNUCpKTewgppBRTWYi2GpX8wDQYJKoZIhvcNAQEBBQAEgYBRJa2+lvwQ0xUIE3h+PLjZQDceUblOgBcj/0gD/BD9T2sxS1RrlDg0P6HujD9DS83gmmt79FGuX0okwtdLp4a7N+5IgdJdshGymMY07cHxQjcNBoXyQH24PRYW9CPCJ0Rfeqj5b0KHO/TB2pvMTg0qW7AmbtIotpC4qAVJ+buHHA==-----END PKCS7-----
909">
910 </form>
911 </div>
912 </div>
913
914<?php
915}
916
917function wordbook_version_ok($currentvers, $minimumvers) {
918 $current = preg_split('/\D+/', $currentvers);
919 $minimum = preg_split('/\D+/', $minimumvers);
920 for ($ii = 0; $ii < min(count($current), count($minimum)); $ii++) {
921 if ($current[$ii] < $minimum[$ii])
922 return false;
923 }
924 if (count($current) < count($minimum))
925 return false;
926 return true;
927}
928
929function wordbook_option_support() {
930 global $wp_version;
931?>
932
933 <h3><?php _e('Support'); ?></h3>
934 <div class="wordbook_support">
935
936 For feature requests, bug reports, and general support:
937
938 <ul>
939
940 <li>Check the <a
941 href="http://wordpress.org/extend/plugins/wordbook/other_notes/"
942 target="wordpress">WordPress.org Notes</a>.</li>
943
944 <li>Try the <a
945 href="http://www.facebook.com/board.php?uid=3353257731"
946 target="facebook">Wordbook Discussion Board</a>.</li>
947
948 <li>Consider upgrading to the <a
949 href="http://wordpress.org/download/">latest stable release</a>
950 of WordPress.</li>
951
952 </ul>
953
954 Please provide the following information about your installation:
955
956 <ul>
957<?php
958
959 $wb_version = 'Unknown';
960 if (($wordbook_php = file(__FILE__)) &&
961 (($versionlines = array_values(preg_grep('/^Version:/',
962 $wordbook_php)))) &&
963 (($versionstrs = explode(':', $versionlines[0]))) &&
964 count($versionstrs) >= 2) {
965 $wb_version = trim($versionstrs[1]);
966 }
967
968 $phpvers = phpversion();
969 $mysqlvers = function_exists('mysqli_get_client_info') ?
970 mysqli_get_client_info() :
971 'Unknown';
972
973 $info = array(
974 'Wordbook' => $wb_version,
975 'Facebook PHP API' => FACEBOOK_PHP_API,
976 'JSON library' => WORDBOOK_JSON_ENCODE,
977 'SimpleXML library' => WORDBOOK_SIMPLEXML,
978 'WordPress' => $wp_version,
979 'PHP' => $phpvers,
980 'MySQL' => $mysqlvers,
981 );
982
983 $version_errors = array();
984 $phpminvers = '5.0';
985 $mysqlminvers = '4.0';
986 if (!wordbook_version_ok($phpvers, $phpminvers)) {
987 /* PHP-5.0 or greater. */
988 $version_errors['PHP'] = $phpminvers;
989 }
990 if ($mysqlvers != 'Unknown' &&
991 !wordbook_version_ok($mysqlvers, $mysqlminvers)) {
992 /* MySQL-4.0 or greater. */
993 $version_errors['MySQL'] = $mysqlminvers;
994 }
995
996 foreach ($info as $key => $value) {
997 $suffix = '';
998 if (($minvers = $version_errors[$key])) {
999 $suffix = " <span class=\"wordbook_errorcolor\">"
1000 . " (need $key version $minvers or greater)"
1001 . " </span>";
1002 }
1003 echo "<li>$key: <b>$value</b>$suffix</li>";
1004 }
1005 if (!function_exists('simplexml_load_string')) {
1006 echo "<li>XML: your PHP is missing <code>simplexml_load_string()</code></li>";
1007 }
1008?>
1009 </ul>
1010
1011<?php
1012 if ($version_errors) {
1013?>
1014
1015 <div class="wordbook_errorcolor">
1016 Your system does not meet the <a
1017 href="http://wordpress.org/about/requirements/">WordPress minimum
1018 reqirements</a>. Things are unlikely to work.
1019 </div>
1020
1021<?php
1022 } else if ($mysqlvers == 'Unknown') {
1023?>
1024
1025 <div>
1026 Please ensure that your system meets the <a
1027 href="http://wordpress.org/about/requirements/">WordPress minimum
1028 reqirements</a>.
1029 </div>
1030
1031<?php
1032 }
1033?>
1034 </div>
1035
1036<?php
1037}
1038
1039function wordbook_option_manager() {
1040 global $user_ID;
1041?>
1042
1043<div class="wrap">
1044 <h2><?php _e('Wordbook'); ?></h2>
1045
1046<?php
1047 wordbook_option_notices();
1048
1049 if (($wbuser = wordbook_get_userdata($user_ID)) &&
1050 $wbuser->session_key) {
1051 list($show_paypal) = wordbook_option_status($wbuser);
1052 wordbook_render_errorlogs();
1053 if ($show_paypal) {
1054 wordbook_option_thanks(array(
1055 'http://thecamaras.net/' =>
1056 'The Camaras',
1057 'http://www.steve-c.co.uk/' =>
1058 "Steve's Space",
1059 'http://alex.tsaiberspace.net/' =>
1060 'The .Plan',
1061 'http://drunkencomputing.com/' =>
1062 'drunkencomputing',
1063 'http://trentadams.com/' =>
1064 'life by way of media',
1065 'http://www.mounthermon.org/' =>
1066 'Mount Hermon',
1067 'http://superjudas.net/' =>
1068 'Superjudas bloggt',
1069 'http://blog.ofsteel.net/' =>
1070 'Blood, Glory & Steel',
1071 'http://shashikiran.com/' =>
1072 'itinerant',
1073 'http://www.patricksimpson.nl/' =>
1074 'Patrick Simpson',
1075 'http://hippocratesinsandiego.com/' =>
1076 'Hippocrates in San Diego',
1077 'http://www.peterbakke.com/' =>
1078 "It's a Small World After All",
1079 ));
1080 }
1081 } else {
1082 wordbook_option_setup($wbuser);
1083 }
1084 wordbook_option_support();
1085?>
1086
1087</div>
1088
1089<?php
1090}
1091
1092function wordbook_admin_menu() {
1093 $hook = add_options_page('Wordbook Option Manager', 'Wordbook',
1094 WORDBOOK_MINIMUM_ADMIN_LEVEL, WORDBOOK_OPTIONS_PAGENAME,
1095 'wordbook_option_manager');
1096 add_action("load-$hook", 'wordbook_admin_load');
1097 add_action("admin_head-$hook", 'wordbook_admin_head');
1098}
1099
1100/******************************************************************************
1101 * One-time code (Facebook)
1102 */
1103
1104function wordbook_render_onetimeerror($wbuser) {
1105 if (($result = $wbuser->onetime_data)) {
1106?>
1107
1108 <p>There was a problem with the one-time code "<?php echo $result['onetimecode']; ?>": <a href="http://developers.facebook.com/documentation.php?v=1.0&method=auth.getSession" target="facebook">error_code = <?php echo $result['error_code']; ?> (<?php echo $result['error_msg']; ?>)</a>. Try re-submitting it, or try generating a new one-time code.</p>
1109
1110<?php
1111 }
1112}
1113
1114/******************************************************************************
1115 * Facebook API wrappers.
1116 */
1117
1118function wordbook_fbclient($wbuser) {
1119 wordbook_load_apis();
1120 $secret = null;
1121 $session_key = null;
1122 if ($wbuser) {
1123 $secret = $wbuser->secret;
1124 $session_key = $wbuser->session_key;
1125 }
1126 if (!$secret)
1127 $secret = WORDBOOK_FB_SECRET;
1128 if (!$session_key)
1129 $session_key = '';
1130 return wordbook_rest_client($secret, $session_key);
1131}
1132
1133function wordbook_fbclient_facebook_finish($wbuser, $result, $method,
1134 $error_code, $error_msg, $postid) {
1135 if ($error_code) {
1136 wordbook_set_userdata_facebook_error($wbuser, $method,
1137 $error_code, $error_msg, $postid);
1138 } else {
1139 wordbook_clear_userdata_facebook_error($wbuser);
1140 }
1141 return $result;
1142}
1143
1144function wordbook_fbclient_setfbml($wbuser, $fbclient, $postid,
1145 $exclude_postid) {
1146 list($result, $error_code, $error_msg) = wordbook_fbclient_setfbml_impl(
1147 $fbclient, wordbook_fbmltext($exclude_postid));
1148 return wordbook_fbclient_facebook_finish($wbuser, $result,
1149 'profile.setFBML', $error_code, $error_msg, $postid);
1150}
1151
1152function wordbook_fbclient_publishaction($wbuser, $fbuid, $fbname, $fbclient,
1153 $postid) {
1154 $post = get_post($postid);
1155 $post_link = get_permalink($postid);
1156 $post_title = get_the_title($postid);
1157 $post_content = $post->post_content;
1158 preg_match_all('/<img \s+ [^>]* src \s* = \s* "(.*?)"/ix',
1159 $post_content, $matches);
1160 $images = array();
1161 foreach ($matches[1] as $ii => $imgsrc) {
1162 if ($imgsrc) {
1163 if (stristr(substr($imgsrc, 0, 8), '://') ===
1164 false) {
1165 /* Fully-qualify src URL if necessary. */
1166 $scheme = $_SERVER['HTTPS'] ? 'https' : 'http';
1167 $new_imgsrc = "$scheme://"
1168 . $_SERVER['SERVER_NAME'];
1169 if ($imgsrc[0] == '/') {
1170 $new_imgsrc .= $imgsrc;
1171 }
1172 $imgsrc = $new_imgsrc;
1173 }
1174 $images[] = array(
1175 'src' => $imgsrc,
1176 'href' => $post_link,
1177 );
1178 }
1179 }
1180 $template_data = array(
1181 'images' => $images,
1182 'post_link' => $post_link,
1183 'post_title' => $post_title,
1184 'post_excerpt' => wordbook_post_excerpt($post_content,
1185 WORDBOOK_EXCERPT_SHORTSTORY),
1186 );
1187 list($result, $error_code, $error_msg, $method) =
1188 wordbook_fbclient_publishaction_impl($fbclient,
1189 WORDBOOK_TEMPLATE_ID, $template_data);
1190 return wordbook_fbclient_facebook_finish($wbuser, $result,
1191 $method, $error_code, $error_msg, $postid);
1192}
1193
1194/******************************************************************************
1195 * WordPress hooks: update Facebook when a blog entry gets published.
1196 */
1197
1198function wordbook_post_excerpt($content, $maxlength) {
1199 $excerpt = apply_filters('the_excerpt', $content);
1200 if (function_exists('strip_shortcodes')) {
1201 $excerpt = strip_shortcodes($excerpt);
1202 }
1203 $excerpt = strip_tags($excerpt);
1204 if (strlen($excerpt) > $maxlength) {
1205 $excerpt = substr($excerpt, 0, $maxlength - 3) . '...';
1206 }
1207 return $excerpt;
1208}
1209
1210function wordbook_fbmltext($exclude_postid) {
1211 /* Set the Wordbook box to contain a summary of the blog front page
1212 * (just those posts written by this user). Don't show
1213 * password-protected posts. */
1214 global $user_ID, $user_identity, $user_login, $wpdb;
1215
1216 $blog_link = get_bloginfo('url');
1217 $blog_name = get_bloginfo('name');
1218 $blog_atitle = '';
1219 if (($blog_description = get_bloginfo('description'))) {
1220 $blog_atitle = $blog_description;
1221 } else {
1222 $blog_atitle = $blog_name;
1223 }
1224 $author_link = "$blog_link/author/$user_login/";
1225 $text = <<<EOM
1226<style>
1227 td { vertical-align: top; }
1228 td.time { text-align: right; padding-right: 1ex; }
1229</style>
1230<fb:subtitle>
1231 Blog posts from <a href="$author_link" title="$user_identity's posts at $blog_name" target="$blog_name">$user_identity</a> at <a href="$blog_link" title="$blog_atitle" target="$blog_name">$blog_name</a>
1232</fb:subtitle>
1233EOM;
1234
1235 $posts_per_page = get_option('posts_per_page');
1236 if ($posts_per_page <= 0) {
1237 $posts_per_page = 10;
1238 }
1239 $exclude_postid_selector = $exclude_postid == null ? "" :
1240 "AND ID != $exclude_postid";
1241 $postidrows = $wpdb->get_results("
1242 SELECT ID
1243 FROM $wpdb->posts
1244 WHERE post_type = 'post'
1245 AND post_status = 'publish'
1246 AND post_author = $user_ID
1247 AND post_password = ''
1248 $exclude_postid_selector
1249 ORDER BY post_date DESC
1250 LIMIT $posts_per_page
1251 ");
1252
1253 $postid = 0;
1254 if ($postidrows) {
1255 $postid = $postidrows[0]->ID;
1256 $text .= <<<EOM
1257<div class="minifeed clearfix">
1258 <table>
1259EOM;
1260 foreach ($postidrows as $postidrow) {
1261 $post = get_post($postidrow->ID);
1262 $post_link = get_permalink($postidrow->ID);
1263 $post_title = get_the_title($postidrow->ID);
1264 $post_date_gmt = strtotime($post->post_date);
1265 $post_excerpt_wide = wordbook_post_excerpt(
1266 $post->post_content, WORDBOOK_EXCERPT_WIDEBOX);
1267 $post_excerpt_narrow = wordbook_post_excerpt(
1268 $post->post_content,
1269 WORDBOOK_EXCERPT_NARROWBOX);
1270 $text .= <<<EOM
1271 <tr>
1272 <td class="time">
1273 <span class="date">
1274 <fb:time t="$post_date_gmt" />
1275 </span>
1276 </td>
1277 <td>
1278 <a href="$post_link" target="$blog_name">$post_title</a>:
1279 <fb:wide>$post_excerpt_wide</fb:wide>
1280 <fb:narrow>$post_excerpt_narrow</fb:narrow>
1281 </td>
1282 </tr>
1283EOM;
1284 }
1285 $text .= <<<EOM
1286 </table>
1287</div>
1288EOM;
1289 } else {
1290 $text .= "I haven't posted anything (yet).";
1291 }
1292
1293 return $text;
1294}
1295
1296function wordbook_publish_action($post) {
1297 wordbook_deletefrom_errorlogs($post->ID);
1298 if ($post->post_password != '') {
1299 /* Don't publish password-protected posts to news feed. */
1300 return null;
1301 }
1302 if (!($wbuser = wordbook_get_userdata($post->post_author)) ||
1303 !$wbuser->session_key) {
1304 return null;
1305 }
1306
1307 /* If publishing a new blog post, update text in "Wordbook" box. */
1308
1309 $fbclient = wordbook_fbclient($wbuser);
1310 if ($post->post_type == 'post' && !wordbook_fbclient_setfbml($wbuser,
1311 $fbclient, $post->ID, null)) {
1312 return null;
1313 }
1314
1315 /*
1316 * Publish posts to Mini-Feed.
1317 *
1318 * Don't spam Facebook by re-publishing
1319 * already-published posts. According to
1320 * http://developers.facebook.com/documentation.php?v=1.0&method=feed.publishTemplatizedAction,
1321 * a user can only publish 10 times within a rolling 48-hour window.
1322 */
1323
1324 if (!wordbook_postlogged($post->ID)) {
1325 list($fbuid, $users, $error_code, $error_msg) =
1326 wordbook_fbclient_getinfo($fbclient, array('name'));
1327 if ($fbuid && is_array($users) && ($user = $users[0])) {
1328 $fbname = $user['name'];
1329 } else {
1330 $fbname = 'A friend';
1331 }
1332 wordbook_fbclient_publishaction($wbuser, $fbuid, $fbname,
1333 $fbclient, $post->ID);
1334 wordbook_insertinto_postlogs($post->ID);
1335 }
1336
1337 return null;
1338}
1339
1340function wordbook_transition_post_status($newstatus, $oldstatus, $post) {
1341 if ($newstatus == 'publish') {
1342 return wordbook_publish_action($post);
1343 }
1344
1345 $postid = $post->ID;
1346 if (($wbuser = wordbook_get_userdata($post->post_author)) &&
1347 $wbuser->session_key) {
1348 $fbclient = wordbook_fbclient($wbuser);
1349 list($result, $error_code, $error_msg) =
1350 wordbook_fbclient_setfbml($wbuser, $fbclient, $postid,
1351 $postid);
1352 }
1353}
1354
1355function wordbook_delete_post($postid) {
1356 $post = get_post($postid);
1357 if (($wbuser = wordbook_get_userdata($post->post_author)) &&
1358 $wbuser->session_key) {
1359 $fbclient = wordbook_fbclient($wbuser);
1360 list($result, $error_code, $error_msg) =
1361 wordbook_fbclient_setfbml($wbuser, $fbclient, $postid,
1362 $postid);
1363 }
1364 wordbook_deletefrom_errorlogs($postid);
1365 wordbook_deletefrom_postlogs($postid);
1366}
1367
1368/******************************************************************************
1369 * Register hooks with WordPress.
1370 */
1371
1372/* Plugin maintenance. */
1373register_deactivation_hook(__FILE__, 'wordbook_deactivate');
1374add_action('delete_user', 'wordbook_delete_user');
1375if (current_user_can(WORDBOOK_MINIMUM_ADMIN_LEVEL)) {
1376 add_action('admin_menu', 'wordbook_admin_menu');
1377}
1378
1379/* Post/page maintenance and publishing hooks. */
1380add_action('delete_post', 'wordbook_delete_post');
1381
1382if (WORDBOOK_WP_VERSION >= 23) {
1383 define('WORDBOOK_HOOK_PRIORITY', 10); /* Default; see add_action(). */
1384 add_action('transition_post_status', 'wordbook_transition_post_status',
1385 WORDBOOK_HOOK_PRIORITY, 3);
1386} else {
1387 /* WordPress-2.2. */
1388 function wordbook_publish($postid) {
1389 $post = get_post($postid);
1390 return wordbook_transition_post_status('publish', null, $post);
1391 }
1392 add_action('publish_post', 'wordbook_publish');
1393 add_action('publish_page', 'wordbook_publish');
1394}
1395
1396?>
Note: See TracBrowser for help on using the repository browser.