当我开发这个小插件时,我使用了Danny van Kooten\'s插件:\'Recent Facebook Posts\'.插件文件夹的内容是:
classes /
facebook-php-sdk/
base_facebook.php
facebook.php
fb_ca_chain_bundle.crt
class-fcwpc.php
fbconnect_wordpress_comments.php
fbconnect_wordpress_comments.php:
<?php
/*
Plugin Name: FB Connect WP Comments
Description: Allow people to comment if they are Facebook connected.
Version: 1.0
*/
require_once dirname(__FILE__) . \'/classes/class-fcwpc.php\';
$FBWPC = FBWPC::get_instance();
?>
class-fcwpc.php:
<?php
class FBWPC {
private static $instance;
private static $fb_instance;
private $options;
public static function get_instance() {
if(!self::$instance) self::$instance = new FBWPC();
return self::$instance;
}
public function __construct() {
add_action(\'init\', array($this, \'on_init\'));
add_action( \'comment_post\', array($this, \'save_comment_data\') );
add_shortcode(\'fbconnect-wp-comments\', array($this, \'shortcode_output\'));
}
public function on_init() {
if(!session_id() && !headers_sent()) {
session_start();
}
}
public function get_options() {
if(!$this->options) {
$defaults = array(
\'app_id\' => get_option( \'appId\' ),
\'app_secret\' => get_option( \'secret\' )
);
$this->options = $defaults;
}
return $this->options;
}
public function getFBApi() {
if(!self::$fb_instance) {
// Only load Facebook class if it has not been loaded yet
// Other plugins may have loaded the class already at this point.
if(!class_exists("Facebook")) {
require_once dirname(__FILE__) . \'/facebook-php-sdk/facebook.php\';
}
$opts = $this->get_options();
self::$fb_instance = new Facebook(array(
\'appId\' => trim($opts[\'app_id\']),
\'secret\' => trim($opts[\'app_secret\']),
));
}
return self::$fb_instance;
}
public function get_session() {
$fb = $this->getFBApi();
$session = $fb->getUser();
return $session;
}
public function save_comment_data( $comment_id ) {
$fb = $this->getFBApi();
// I need this data to display the profil picture thanks to the url : http://graph.facebook.com/\'. $fb_username .\'/picture
add_comment_meta( $comment_id, \'fb_username\', $_POST[\'fb_username\'] );
// test message
if( isset($_POST[\'publish\']) ){
$params = array(
\'message\' => "Hurray! This works :)",
\'name\' => "This is my title",
\'caption\' => "My Caption",
\'description\' => "Some Description...",
\'link\' => "http://stackoverflow.com",
\'picture\' => "http://i.imgur.com/VUBz8.png",
);
$user = $_POST[\'fb_userid\'];
$post = $fb->api("/$user/feed","POST",$params);
}
}
public function shortcode_output()
{
$session = $this->get_session();
$fb = $this->getFBApi();
if(!$session){
$loginUrl = $fb->getLoginUrl(array(\'scope\'=>\'publish_stream, email\', \'redirect_uri\'=>\'http://yourRedirectUri.com/\'));
}
if( !isset( $session ) || empty( $session ) ){
$output = \'<div class="btns">
<div class="fb-connect">
<a href="\'. $loginUrl. \'"><img src="\'. get_template_directory_uri(). \'/img/btn-fb.png" alt=""></a>
</div>
</div>\';
}
$output .= \'<ul>\';
if( $nb_coms != 0 ){
$comments = get_comments();
foreach($comments as $comment) :
$fb_username = get_comment_meta( $comment->comment_ID, \'fb_username\', true );
$output .= \'<li id="list-comment-\'. $comment->comment_ID .\'">
<img src="http://graph.facebook.com/\'. $fb_username .\'/picture" alt="" height="100" width="100">
<span class="name">\'. $comment->comment_author .\'</span>
<p>\'. $comment->comment_content .\'</p>
<div class="date">\'. $comment->comment_date .\'</div>
</li>\';
endforeach;
}
$output .= \'</ul>\';
if ( comments_open() ) :
if($session){
try {
$me = $fb->api(\'/me\');
$username = $me[\'username\'];
$firstname = $me[\'first_name\'];
$email = $me[\'email\'];
$output .= \'<form action="\'. get_site_url(). \'/wp-comments-post.php" method="post">
<img src="http://graph.facebook.com/\'. $username. \'/picture" alt="" height="50" width="50">
<div class="fieldslist">
<input id="author" name="author" type="hidden" value="\'. $firstname. \'">
<input id="fb_username" name="fb_username" type="hidden" value="\'. $username. \'">
<input id="email" name="email" type="hidden" value="\'. $email. \'">
<input id="fb_userid" name="fb_userid" type="hidden" value="\'. $session. \'">
<textarea name="comment" cols="45" rows="8" aria-required="true"></textarea>
<div class="submit">
<input type="checkbox" name="publish">
<label>Publish on your wall</label>
<input type="hidden" name="comment_post_ID" value="\'. get_the_ID(). \'" id="comment_post_ID">
<input type="submit" value="Comment">
</div>
</div>
</form>\';
}
catch(FacebookApiException $e){
error_log($e);
}
}
endif;
return $output;
}
}
文件
comments.php 在您的主题中
<div class="coms">
<?php echo do_shortcode(\'[fbconnect-wp-comments]\'); ?>
</div>
我很快就清理了代码,所以我不知道测试时它是否看起来很好。