我最终自己解决了这个问题,但得到了@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. :)";
}
}
}