在短代码中有条件地加载Facebook PHP SDK

时间:2012-02-27 作者:Jared

从3.3开始,我们可以在短代码函数中有条件地将脚本排队,但当我尝试使用一些PHP类(使用session_start()__construct() 函数)您可以猜到,它给了我headers already sent 错误

问题是(这是使用Facebook PHP SDK in conjunction with the JS SDK), 我只想在帖子上存在一个短代码,并且当前帖子已经填写了2个元字段(类需要这2个值[应用ID和应用机密])的情况下实例化这个类。

有没有简单的解决方案?如果是这样,我可以在下面的代码中更改什么来允许这样做?

public function fb_connect_shortcode( $atts, $content = null ) {
    extract( shortcode_atts( array( ), $atts ) ); /**/ global $post;
    $app_id = get_post_meta( $post->ID, $this->meta_prefix . \'AppID\', true );
    $app_secret = get_post_meta( $post->ID, $this->meta_prefix . \'AppSecret\', true );

    if( $app_id !== \'\' && $app_secret !== \'\' ) {

        /**
         * This is the class I am trying to instantiate conditionally
         * but since it uses session_start(), it can\'t send out headers
         */
        $facebook = new Facebook( array(
          \'appId\'  => $app_id,
          \'secret\' => $app_secret,
        ) );

        // See if there is a user from a cookie
        $user = $facebook->getUser();

        if( $user ) {
            try {
                // Proceed knowing you have a logged in user who\'s authenticated.
                $user_profile = $facebook->api( \'/me\' );
                $app_id = $facebook->getAppId();
            } catch( FacebookApiException $e ) {
                echo \'<pre>\' . htmlspecialchars( print_r( $e, true ) ) . \'</pre>\';
                $user = null;
            }
        }

        wp_enqueue_script( \'jw-fbsdk\', plugins_url( \'jw-fbsdk.js\', __FILE__ ), array(), \'3.3.1\', true );
        wp_localize_script( \'jw-fbsdk\', \'jwuc\', array(
            \'appId\' => $app_id,
            \'channelUrl\' => plugins_url( \'channel.php\', __FILE__ )
        ) );

        if( is_null( $content ) )
            $content = \'Connect with Facebook\';

        if( isset( $user_profile ) && $user_profile ) {
            return $user_profile[\'name\'];
        } else {
            return \'<div class="fb-login-button" data-scope="email,publish_stream,read_stream,status_update">\' . $content . \'</div>\';
        }

    } else {

        return "You forgot to add your App ID and/or App Secret! Facebook needs these. :)";

    }

}

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

我最终自己解决了这个问题,但得到了@Bainternet的帮助(检查帖子内容中是否存在短代码)。

我所做的是在我的类中创建一个变量,名为facebook, 我在这里存储PHP SDK类。我用了wp 钩子来启动Facebook类如果一些事情设置正确,这个钩子允许我使用global $post 这样我就可以抓住元值。

require \'facebook.php\';
$urgent = JW_Urgent::getInstance();

class JW_Urgent {

    private static $instance;
    public $facebook;

    // Construct the class\' data
    private function __construct() {

        $this->facebook = null;

        add_action( \'wp\', array( &$this, \'define_facebook\' ) );
        add_shortcode( \'jw-connect\', array( &$this, \'fb_connect_shortcode\' ) );

    }

    public static function getInstance() {
        if ( is_null( self::$instance ) )
            self::$instance = new JW_Urgent();

        return self::$instance;
    }


    public function define_facebook() {
        global $post;

        $app_id = get_post_meta( $post->ID, $this->meta_prefix . \'AppID\', true );
        $app_secret = get_post_meta( $post->ID, $this->meta_prefix . \'AppSecret\', true );

        if( !is_null( $post ) && ( $app_id !== \'\' && $app_secret !== \'\' ) && $post->post_type == $this->post_type ) {

            if( strpos( $post->post_content, \'[jw-connect\' ) !== false ) {

                $this->facebook = new Facebook( array(
                    \'appId\' => $app_id,
                    \'secret\' => $app_secret
                ) );

            }

        }

    }

    public function fb_connect_shortcode( $atts, $content = null ) {
        extract( shortcode_atts( array( ), $atts ) ); /**/ global $post;
        $app_id = get_post_meta( $post->ID, $this->meta_prefix . \'AppID\', true );
        $app_secret = get_post_meta( $post->ID, $this->meta_prefix . \'AppSecret\', true );

        if( $app_id !== \'\' && $app_secret !== \'\' && !is_null( $this->facebook ) ) {

            // See if there is a user from a cookie
            $user = $this->facebook->getUser();

            if( $user ) {
                try {
                    // Proceed knowing you have a logged in user who\'s authenticated.
                    $user_profile = $this->facebook->api( \'/me\' );
                    $app_id = $this->facebook->getAppId();
                } catch( FacebookApiException $e ) {
                    echo \'<pre>\' . htmlspecialchars( print_r( $e, true ) ) . \'</pre>\';
                    $user = null;
                }
            }

            wp_enqueue_script( \'jw-fbsdk\', plugins_url( \'jw-fbsdk.js\', __FILE__ ), array(), \'3.3.1\', true );
            wp_localize_script( \'jw-fbsdk\', \'jwuc\', array(
                \'appId\' => $app_id,
                \'channelUrl\' => plugins_url( \'channel.php\', __FILE__ )
            ) );

            if( is_null( $content ) )
                $content = \'Connect with Facebook\';

            if( isset( $user_profile ) && $user_profile ) {
                return $user_profile[\'name\'];
            } else {
                return \'<div class="fb-login-button" data-scope="email,publish_stream,read_stream,status_update">\' . $content . \'</div>\';
            }

        } else {

            return "You forgot to add your App ID and/or App Secret! Facebook needs these. :)";

        }

    }

}

SO网友:Bainternet

您可以尝试使用the_posts 筛选以搜索您的短代码并需要sdk,如下所示:

function has_my_FB_shortcode($posts) {
    if ( empty($posts) )
        return $posts;
    $found = false;
    foreach ($posts as $post) {
        if ( stripos($post->post_content, \'[my_shortcode\') ){
            $found = true;
            break;
        }
    }

    if ($found)
        require(\'path/to/facebook_sdk.php\');

    return $posts;
}
add_action(\'the_posts\', \'has_my_FB_shortcode\');

结束