Initial Commit - Plugin v1.0.0
This commit is contained in:
25
includes/admin/enqueue.php
Normal file
25
includes/admin/enqueue.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
function fbr_admin_enqueue(){
|
||||
|
||||
if( !isset($_GET['page']) || $_GET['page'] != 'fbr_plugin_opts' ){
|
||||
return;
|
||||
}
|
||||
|
||||
wp_register_style(
|
||||
'fbr_bootstrap',
|
||||
plugins_url( '/assets/styles/bootstrap.min.css', FBR_PLUGIN_URL )
|
||||
);
|
||||
|
||||
wp_enqueue_style( 'fbr_bootstrap' );
|
||||
|
||||
wp_register_script(
|
||||
'fbr_bootstrap',
|
||||
plugins_url( '/assets/scripts/bootstrap.min.js', FBR_PLUGIN_URL ),
|
||||
array('jquery'),
|
||||
'1.0.0',
|
||||
true
|
||||
);
|
||||
|
||||
wp_enqueue_script( 'fbr_bootstrap' );
|
||||
}
|
||||
69
includes/admin/fb-login-cb.php
Normal file
69
includes/admin/fb-login-cb.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
function fbr_fb_login_cb(){
|
||||
|
||||
$fbr_opts = get_option( 'fbr_opts' );
|
||||
extract( $fbr_opts );
|
||||
|
||||
$fb = new Facebook\Facebook([
|
||||
'app_id' => $fbr_app_id,
|
||||
'app_secret' => $fbr_app_secret,
|
||||
'default_graph_version' => 'v2.2',
|
||||
]);
|
||||
|
||||
$helper = $fb->getRedirectLoginHelper();
|
||||
|
||||
try {
|
||||
$accessToken = $helper->getAccessToken();
|
||||
} catch(Facebook\Exceptions\FacebookResponseException $e) {
|
||||
// When Graph returns an error
|
||||
echo 'Graph returned an error: ' . $e->getMessage();
|
||||
exit;
|
||||
} catch(Facebook\Exceptions\FacebookSDKException $e) {
|
||||
// When validation fails or other local issues
|
||||
echo 'Facebook SDK returned an error: ' . $e->getMessage();
|
||||
exit;
|
||||
}
|
||||
|
||||
if (! isset($accessToken)) {
|
||||
if ($helper->getError()) {
|
||||
header('HTTP/1.0 401 Unauthorized');
|
||||
echo "Error: " . $helper->getError() . "\n";
|
||||
echo "Error Code: " . $helper->getErrorCode() . "\n";
|
||||
echo "Error Reason: " . $helper->getErrorReason() . "\n";
|
||||
echo "Error Description: " . $helper->getErrorDescription() . "\n";
|
||||
} else {
|
||||
header('HTTP/1.0 400 Bad Request');
|
||||
echo 'Bad request';
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
// The OAuth 2.0 client handler helps us manage access tokens
|
||||
$oAuth2Client = $fb->getOAuth2Client();
|
||||
|
||||
// Get the access token metadata from /debug_token
|
||||
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
|
||||
|
||||
// Validation (these will throw FacebookSDKException's when they fail)
|
||||
$tokenMetadata->validateAppId($fbr_app_id); // Replace {app-id} with your app id
|
||||
// If you know the user ID this access token belongs to, you can validate it here
|
||||
//$tokenMetadata->validateUserId('123');
|
||||
$tokenMetadata->validateExpiration();
|
||||
|
||||
if (! $accessToken->isLongLived()) {
|
||||
// Exchanges a short-lived access token for a long-lived one
|
||||
try {
|
||||
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
|
||||
} catch (Facebook\Exceptions\FacebookSDKException $e) {
|
||||
echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
$_SESSION['fb_access_token'] = (string) $accessToken;
|
||||
|
||||
// User is logged in with a long-lived access token.
|
||||
// You can redirect them to a members-only page.
|
||||
wp_redirect( admin_url( 'admin.php?page=fbr_plugin_opts&status=2' ) );
|
||||
}
|
||||
10
includes/admin/init.php
Normal file
10
includes/admin/init.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
function fbr_admin_init(){
|
||||
include( 'enqueue.php' );
|
||||
include( 'options-page.php' );
|
||||
include( 'fb-login-cb.php' );
|
||||
|
||||
add_action( 'admin_enqueue_scripts', 'fbr_admin_enqueue' );
|
||||
add_action( 'admin_post_fbr_fb_login_cb', 'fbr_fb_login_cb' );
|
||||
}
|
||||
11
includes/admin/menus.php
Normal file
11
includes/admin/menus.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
function fbr_admin_menus(){
|
||||
add_menu_page(
|
||||
'Facebook Ratings Options',
|
||||
'FB Ratings',
|
||||
'edit_theme_options',
|
||||
'fbr_plugin_opts',
|
||||
'fbr_plugin_opts_page'
|
||||
);
|
||||
}
|
||||
142
includes/admin/options-page.php
Normal file
142
includes/admin/options-page.php
Normal file
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
function fbr_plugin_opts_page(){
|
||||
|
||||
$fbr_opts = get_option( 'fbr_opts' );
|
||||
extract( $fbr_opts );
|
||||
|
||||
$isSetup = ( $fbr_app_id && $fbr_app_secret && isset( $_SESSION['fb_access_token'] ) ) ? true : false;
|
||||
|
||||
if( $isSetup ) {
|
||||
|
||||
$fb = new Facebook\Facebook([
|
||||
'app_id' => $fbr_app_id,
|
||||
'app_secret' => $fbr_app_secret,
|
||||
'default_graph_version' => 'v2.2',
|
||||
'default_access_token' => $_SESSION['fb_access_token']
|
||||
]);
|
||||
|
||||
try {
|
||||
// Get the \Facebook\GraphNodes\GraphUser object for the current user.
|
||||
// If you provided a 'default_access_token', the '{access-token}' is optional.
|
||||
$response = $fb->get('/me/accounts');
|
||||
} catch(\Facebook\Exceptions\FacebookResponseException $e) {
|
||||
// When Graph returns an error
|
||||
echo 'Graph returned an error: ' . $e->getMessage();
|
||||
exit;
|
||||
} catch(\Facebook\Exceptions\FacebookSDKException $e) {
|
||||
// When validation fails or other local issues
|
||||
echo 'Facebook SDK returned an error: ' . $e->getMessage();
|
||||
exit;
|
||||
}
|
||||
|
||||
$graphEdge = $response->getGraphEdge();
|
||||
}
|
||||
?>
|
||||
<div class="wrap">
|
||||
<h2>Facebook Ratings</h2>
|
||||
<p>Connectez-vous et autorisez Facebook Ratings à accéder à vos Pages.</p>
|
||||
<?php if( isset( $_GET['status'] ) ) {
|
||||
switch( $_GET['status'] ){
|
||||
case 1:
|
||||
fbr_create_alert('Options sauvegardées !');
|
||||
break;
|
||||
case 2:
|
||||
fbr_create_alert('Connexion à Facebook réussie !');
|
||||
break;
|
||||
case 3:
|
||||
fbr_create_alert('Page enregistrée avec succès !');
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} ?>
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
|
||||
<?php if( $isSetup ) { ?>
|
||||
<form method="POST" action="admin-post.php">
|
||||
<input type="hidden" name="action" value="fbr_select_page" />
|
||||
<?php wp_nonce_field( 'fbr_select_page_verify' ); ?>
|
||||
<div class="form-group">
|
||||
<label for="user-page-select">
|
||||
<?php _e( 'Mes Pages: ', 'wp-fb-ratings' ); ?>
|
||||
</label>
|
||||
<select id="user-page-select" class="form-control" name="fbr_page_selected" required>
|
||||
<option selected>Choisissez une Page</option>
|
||||
<?php foreach( $graphEdge as $graphNode ) {
|
||||
$isSelected = null;
|
||||
if( $fbr_page_id == $graphNode->getField( 'id' ) ) $isSelected = 'selected';
|
||||
echo '<option '. $isSelected .' value="'. $graphNode->getField( 'id' ) .','. $graphNode->getField( 'access_token' ) .'">'. $graphNode->getField( 'name' ) .'</option>';
|
||||
} ?>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<button type="submit" class="btn btn-primary">
|
||||
<?php _e( 'Enregistrer les modifications', 'wp-fb-ratings' ); ?>
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
|
||||
<?php } else { ?>
|
||||
|
||||
<form method="POST" action="admin-post.php">
|
||||
<input type="hidden" name="action" value="fbr_save_options" />
|
||||
<?php wp_nonce_field( 'fbr_save_options_verify' ); ?>
|
||||
<div class="alert alert-secondary" role="alert">
|
||||
Plus d'infos sur commence créer un App: <a href="https://developers.facebook.com/docs/apps/register?locale=fr_FR" target="_blank">ici</a>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="fbr-app-id">
|
||||
<?php _e( "ID de l'App : ", 'wp-fb-ratings' ); ?>
|
||||
</label>
|
||||
<input
|
||||
id="fbr-app-id"
|
||||
type="text"
|
||||
class="form-control"
|
||||
name="fbr_app_id"
|
||||
value="<?php echo ($fbr_app_id) ? $fbr_app_id : null; ?>"
|
||||
placeholder="Ex: 012345678901234"
|
||||
minlength="15"
|
||||
maxlength="15"
|
||||
required />
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="fbr-app-secret">
|
||||
<?php _e( "Clé Secrète de l'App", 'wp-fb-ratings' ); ?>
|
||||
</label>
|
||||
<input
|
||||
id="fbr-app-secret"
|
||||
type="password"
|
||||
class="form-control"
|
||||
name="fbr_app_secret"
|
||||
value="<?php echo ($fbr_app_secret) ? $fbr_app_secret : null; ?>"
|
||||
placeholder="Ex: 3ee4c5282f968b1e32d170cf7f2fe78e"
|
||||
minlength="32"
|
||||
maxlength="32"
|
||||
required />
|
||||
</div>
|
||||
<hr class="my-4">
|
||||
<div class="form-group">
|
||||
<button type="submit" class="btn btn-primary">
|
||||
<?php _e( 'Connecter mon App', 'wp-fb-ratings' ); ?>
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<?php } ?>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
function fbr_create_alert( $content ){
|
||||
$html = '<div class="alert alert-success" role="alert">';
|
||||
$html .= __( $content, 'wp-fb-ratings' );
|
||||
$html .= '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>';
|
||||
$html .= '</div>';
|
||||
echo $html;
|
||||
}
|
||||
Reference in New Issue
Block a user