Initial Commit - Plugin v1.0.0
This commit is contained in:
22
includes/activate.php
Normal file
22
includes/activate.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
function fbr_activate_plugin(){
|
||||
if( version_compare( get_bloginfo( 'version' ), '4.5', '<' ) ){
|
||||
wp_die( __( 'You must update Wordpress to use this plugin', 'fbsr' ) );
|
||||
}
|
||||
|
||||
// Setup Option Page fields
|
||||
$fbr_opts = get_option( 'fbr_opts' );
|
||||
|
||||
if( !$fbr_opts ){
|
||||
$opts = [
|
||||
'fbr_app_id' => null,
|
||||
'fbr_app_secret' => null,
|
||||
'fbr_page_id' => null,
|
||||
'fbr_page_access_token' => null,
|
||||
'fbr_ratings' => array(),
|
||||
];
|
||||
|
||||
add_option( 'fbr_opts', $opts );
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
22
includes/deactivate.php
Normal file
22
includes/deactivate.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
function fbr_deactivate_plugin(){
|
||||
if( version_compare( get_bloginfo( 'version' ), '4.5', '<' ) ){
|
||||
wp_die( __( 'You must update Wordpress to use this plugin', 'fbsr' ) );
|
||||
}
|
||||
|
||||
// Setup Option Page fields
|
||||
$fbr_opts = get_option( 'fbr_opts' );
|
||||
|
||||
if( $fbr_opts ){
|
||||
$opts = [
|
||||
'fbr_app_id' => null,
|
||||
'fbr_app_secret' => null,
|
||||
'fbr_page_id' => null,
|
||||
'fbr_page_access_token' => null,
|
||||
'fbr_ratings' => array(),
|
||||
];
|
||||
|
||||
update_option( 'fbr_opts', $fbr_opts );
|
||||
}
|
||||
}
|
||||
21
includes/front/enqueue.php
Normal file
21
includes/front/enqueue.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
function fbr_enqueue_scripts(){
|
||||
|
||||
wp_register_style(
|
||||
'fbr_slider',
|
||||
plugins_url( '/assets/styles/slider.css', FBR_PLUGIN_URL )
|
||||
);
|
||||
|
||||
wp_enqueue_style( 'fbr_slider' );
|
||||
|
||||
wp_register_script(
|
||||
'fbr_slider',
|
||||
plugins_url( '/assets/scripts/slider.js', FBR_PLUGIN_URL ),
|
||||
array('jquery'),
|
||||
'1.0.0',
|
||||
true
|
||||
);
|
||||
|
||||
wp_enqueue_script( 'fbr_slider' );
|
||||
}
|
||||
5
includes/init.php
Normal file
5
includes/init.php
Normal file
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
function fbr_init(){
|
||||
|
||||
}
|
||||
5
includes/widgets.php
Normal file
5
includes/widgets.php
Normal file
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
function fbr_widgets_init(){
|
||||
register_widget( 'FB_Ratings_Slider_Widget' );
|
||||
}
|
||||
161
includes/widgets/ratings-slider.php
Normal file
161
includes/widgets/ratings-slider.php
Normal file
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
class FB_Ratings_Slider_Widget extends WP_Widget
|
||||
{
|
||||
/**
|
||||
* Sets up the widgets name etc
|
||||
*/
|
||||
public function __construct() {
|
||||
$widget_ops = array(
|
||||
'description' => 'Displays Page Ratings into a neat slider.',
|
||||
);
|
||||
parent::__construct(
|
||||
'fbr_ratings_slider_widget',
|
||||
'FB Ratings Slider',
|
||||
$widget_ops
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs the options form on admin
|
||||
*
|
||||
* @param array $instance The widget options
|
||||
*/
|
||||
public function form( $instance ) {
|
||||
// outputs the options form on admin
|
||||
}
|
||||
|
||||
/**
|
||||
* Processing widget options on save
|
||||
*
|
||||
* @param array $new_instance The new options
|
||||
* @param array $old_instance The previous options
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function update( $new_instance, $old_instance ) {
|
||||
// processes widget options to be saved
|
||||
$instance = [];
|
||||
|
||||
$fbr_opts = get_option( 'fbr_opts' );
|
||||
extract( $fbr_opts );
|
||||
|
||||
$fbr_opts['fbr_ratings'] = array();
|
||||
|
||||
$fb = new Facebook\Facebook([
|
||||
'app_id' => $fbr_app_id,
|
||||
'app_secret' => $fbr_app_secret,
|
||||
'default_graph_version' => 'v2.2',
|
||||
]);
|
||||
|
||||
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('/'. $fbr_page_id .'/ratings?limit=15&fields=reviewer,has_rating,has_review,review_text,rating,created_time', $fbr_page_access_token);
|
||||
} 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();
|
||||
|
||||
|
||||
foreach( $graphEdge as $graphNode ) {
|
||||
if( $graphNode->getField( 'has_rating' ) && $graphNode->getField('has_review') ){
|
||||
array_push($fbr_opts['fbr_ratings'], $graphNode->asArray());
|
||||
}
|
||||
}
|
||||
|
||||
error_log(print_r($fbr_opts['fbr_ratings'], true));
|
||||
|
||||
update_option( 'fbr_opts', $fbr_opts );
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs the content of the widget
|
||||
*
|
||||
* @param array $args
|
||||
* @param array $instance
|
||||
*/
|
||||
public function widget( $args, $instance ) {
|
||||
// outputs the content of the widget
|
||||
extract( $args );
|
||||
// extract( $instance );
|
||||
|
||||
$fbr_opts = get_option( 'fbr_opts' );
|
||||
extract( $fbr_opts );
|
||||
|
||||
echo $before_widget;
|
||||
|
||||
?>
|
||||
<div id="ratings-slider" class="carousel slide" data-ride="false">
|
||||
<img class="slider-bg d-block w-100" src="<?php echo plugin_dir_url( FBR_PLUGIN_URL ) . '/assets/images/ratings-slider-placeholder.jpg'; ?>" alt="FB Ratings">
|
||||
<ol class="carousel-indicators">
|
||||
<?php foreach( $fbr_ratings as $key => $rating ) : ?>
|
||||
<li
|
||||
data-target="#ratings-slider"
|
||||
data-slide-to="<?php echo $key; ?>"
|
||||
class="<?php echo ( $key == 0 ) ? 'active' : ''; ?>"
|
||||
>
|
||||
<?php
|
||||
if( $key == 0 ) {
|
||||
echo jouvanceau_get_svg( array( 'icon' => 'dot-plain', 'id' => '', 'class' => 'd-block' ) );
|
||||
} else {
|
||||
echo jouvanceau_get_svg( array( 'icon' => 'dot-empty', 'id' => '', 'class' => 'd-block' ) );
|
||||
}
|
||||
?>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
</ol>
|
||||
<div class="carousel-inner">
|
||||
<?php foreach( $fbr_ratings as $key => $rating ) : ?>
|
||||
<div class="carousel-item <?php echo ( $key == 0 ) ? 'active' : ''; ?>">
|
||||
<div class="carousel-caption">
|
||||
<h2><?php fbr_get_stars_display( $rating['rating'] ); ?></h2>
|
||||
<p>
|
||||
<?php if( strlen( $rating['review_text'] ) > 255 ) {
|
||||
echo substr( $rating['review_text'], 0, 255 ) . '...';
|
||||
} else {
|
||||
echo $rating['review_text'];
|
||||
} ?>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<a class="carousel-control-prev" href="#ratings-slider" role="button" data-slide="prev">
|
||||
<?php echo jouvanceau_get_svg( array( 'icon' => 'left-arrow', 'id' => '', 'class' => '' ) ); ?>
|
||||
<span class="sr-only">Previous</span>
|
||||
</a>
|
||||
<a class="carousel-control-next" href="#ratings-slider" role="button" data-slide="next">
|
||||
<?php echo jouvanceau_get_svg( array( 'icon' => 'right-arrow', 'id' => '', 'class' => '' ) ); ?>
|
||||
<span class="sr-only">Next</span>
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<?php
|
||||
echo $after_widget;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
function fbr_get_stars_display( $rating ){
|
||||
$html = '';
|
||||
|
||||
for( $i = 0; $i < 5; $i++ ){
|
||||
if( $rating > $i ) {
|
||||
$html .= '<span>' . jouvanceau_get_svg( array( 'icon' => 'star-plain', 'id' => '', 'class' => '' ) ) . '</span>';
|
||||
} else {
|
||||
$html .= '<span>' . jouvanceau_get_svg( array( 'icon' => 'star-empty', 'id' => '', 'class' => '' ) ) . '</span>';
|
||||
}
|
||||
}
|
||||
|
||||
echo $html;
|
||||
}
|
||||
Reference in New Issue
Block a user