Skip to content

Instantly share code, notes, and snippets.

@pryley
Created December 1, 2012 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pryley/4179688 to your computer and use it in GitHub Desktop.
Save pryley/4179688 to your computer and use it in GitHub Desktop.
Allow Facebook App ID/Secret verification with Options Framework
/** OPTIONS FRAMEWORK - FACEBOOK VERIFICATION
/ ================================================================================ */
// Verify Facebook App ID and Secret
add_action( 'admin_notices', 'pr_facebook_verify' );
function pr_facebook_verify() {
if ( get_option( 'fb_token' ) != '' ) {
$screen = get_current_screen();
$app_id = get_option( 'fb_appid_isvalid' );
$secret = get_option( 'fb_secret_isvalid' );
if ( !$app_id ) {
if ( $screen->parent_base == 'themes' && $screen->id == 'appearance_page_options-framework' )
add_settings_error( 'options-framework', 'bad_id', __( 'Facebook could not validate your Facebook App ID and Secret!', 'playground' ), 'error' );
else
echo "<div class='error'><p>".__( 'Facebook could not validate your Facebook App ID and Secret!', 'playground' )." <a href='".admin_url( 'themes.php?page=options-framework#of-option-facebookconnect' )."'>".__( 'Click here','playground' )."</a> to fix it.</p></div>";
} else if ( !$secret ) {
if ( $screen->parent_base == 'themes' && $screen->id == 'appearance_page_options-framework' )
add_settings_error( 'options-framework', 'bad_secret', __( 'Facebook could not validate your Facebook App Secret!', 'playground' ), 'error' );
else
echo "<div class='error'><p>".__( 'Facebook could not validate your Facebook App Secret!', 'playground' )." <a href='".admin_url( 'themes.php?page=options-framework#of-option-facebookconnect' )."'>".__( 'Click here','playground' )."</a> to fix it.</p></div>";
}
}
}
add_action( 'optionsframework_after_validate', 'optionsframework_on_validate', 10, 2 );
function optionsframework_on_validate( $options ) {
$app_id = $options['fb_app_id'];
$secret = $options['fb_app_secret'];
if ( $app_id || $secret ) {
$object = json_decode( file_get_contents( "https://graph.facebook.com/{$app_id}" ) );
if ( $object && isset( $object->link ) && strstr( $object->link, 'http://www.facebook.com/apps/application.php' ) ) {
// App ID is valid
update_option( 'fb_appid_isvalid', 1 );
$response = wp_remote_get( "https://graph.facebook.com/oauth/access_token?client_id={$app_id}&client_secret={$secret}&grant_type=client_credentials" );
if ( is_array( $response ) && strpos( $response['body'], 'access_token=' ) !== FALSE ) {
// App secret is valid
update_option( 'fb_secret_isvalid', 1 );
// Store token
update_option( 'fb_token', substr($response['body'], 13) );
// Show successs alert
add_settings_error( 'options-framework', 'connected', __( 'You have successfully connected to Facebook!', 'playground' ), 'updated fade' );
} else {
//App secret is NOT valid
update_option( 'fb_secret_isvalid', 0 );
// token = FALSE
update_option( 'fb_token', 0 );
}
} else {
// App ID is NOT valid, can't check for valid secret
update_option( 'fb_appid_isvalid', 0 );
update_option( 'fb_secret_isvalid', 0 );
// token = FALSE
update_option( 'fb_token', 0 );
}
} else {
// Values are empty, clear all options
update_option( 'fb_appid_isvalid', '' );
update_option( 'fb_secret_isvalid', '' );
update_option( 'fb_token', '' );
}
// Now all we have to do is check that `get_option( 'fb_token' )` is not blank or false before loading any Facebook API stuff.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment