在发表评论后在Facebook上发布消息

时间:2013-09-09 作者:aguidis

我想了解一些关于如何改进允许人们评论帖子的代码的建议。

只有与Facebook连接的用户才能提交评论。我开始在评论中直接开发这个特性。php模板。它可以工作,但还有一个功能允许人们在发送评论前勾选复选框,就可以在墙上发布消息。

我在开发这个函数性时遇到了一些麻烦,因为我需要一个Facebook类的实例。我的评论中已经有了。php和我想知道是否有一种方法可以使用它而不是创建一个新的。代码如下:

comments.php:

<?php 
locate_template( \'include/facebook.php\', true );

$config = array();
$config[\'appId\'] = get_option( \'appId\' );
$config[\'secret\'] = get_option( \'secret\' );

$facebook = new Facebook($config);
$session = $facebook->getUser();
if(!$session){
    $loginUrl = $facebook->getLoginUrl(array(\'scope\'=>\'publish_stream, email\', \'redirect_uri\'=>\'http://www.myredirecturi.fr\'));
}

if( !isset( $session ) || empty( $session ) ){    
    ?>
    <div class="btns">
        <div class="fb-connect">
            <a href="<?php echo $loginUrl; ?>"><img src="<?php echo get_template_directory_uri(); ?>/img/btn-fb.png" alt=""></a>
        </div>
    </div>
    <?php
}

$nb_coms = get_comments_number();
if( $nb_coms != 0 ){
    wp_list_comments(\'callback=custom_comment&avatar_size=50\');
}
?>

<?php
if($session){    
    try {    
        $me = $facebook->api(\'/me\');
        $username = $me[\'username\'];
        $firstname = $me[\'first_name\'];
        $email = $me[\'email\'];

        // I display here my custom comment form        
    }    
    catch(FacebookApiException $e){    
        error_log($e);    
    }
}
?>

functions.php

<?php
add_action( \'comment_post\', \'custom_save_comment_data\' );  
function custom_save_comment_data( $comment_id ) {  

    add_comment_meta( $comment_id, \'fb_username\', $_POST[\'fb_username\'] );

    //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 = $facebook->api("/$user/feed","POST",$params);
    //}
}
?>
你建议如何传递facebook对象?我开发这些功能的方式正确吗?或者我应该创建一个插件?如果是这样,为什么?

2 个回复
最合适的回答,由SO网友:kaiser 整理而成

或者我应该创建一个插件?如果是这样,为什么?

是的,你应该:分离关注点。主题是应用程序的视图。由于facebook功能是应用程序的一部分,因此它属于插件。

将内容移动到插件中有一个很大的好处:如果您交换主题,您可以轻松地切换主题,而不必失去功能,也不必再次编写代码。

并且始终使用挂钩和(甚至更好的)过滤器将内容引入模板。钩状物the_content, start_loop, end_loopthe_post 非常适合将插件输出附加到视图。当核心将它们注入循环时,它们按默认值存在。

SO网友:aguidis

当我开发这个小插件时,我使用了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>
我很快就清理了代码,所以我不知道测试时它是否看起来很好。

结束

相关推荐

获取wp_list_Comments的完整代码,而不是替换它

我想知道这是否可行,因为我不想编辑评论的标准/默认布局。我已经试过了,但不知怎么的,它不会把列表项放在我的div部分。这就是为什么我想找到full code of wp_list_comments; 我想更改其中的列表项。。。这可能吗?如果是,我在哪里可以找到它?