Skip to content

Instantly share code, notes, and snippets.

@dkvadratu
Created February 8, 2021 08:28
Show Gist options
  • Save dkvadratu/842732a5ae64b63df8241b7652a61ce8 to your computer and use it in GitHub Desktop.
Save dkvadratu/842732a5ae64b63df8241b7652a61ce8 to your computer and use it in GitHub Desktop.
{WP} Custom facebook reviews functionality.
<?php
/**
* @purpose Show facebook comments in Cart page, for mobile and desktop
* @pages Cart
* @version 2019.02.14
*/
add_shortcode('dk_fb_comments', 'dk_fb_get_comments_local');
function dk_fb_get_comments_local()
{
$json_file = site_url() . "/_fb/comments.json";
ob_start();
?>
<div class="row row-main facebookReviews"></div>
<div class="large-12 col text-center">
<a href="" target="_self" class="button success facebookButton">Rodyti daugiau atsiliepimų</a>
</div>
<script>
"use strict";
jQuery(window).load(function ($) {
const facebookReviewsDiv = jQuery('#cartFacebookReviews');
if (facebookReviewsDiv !== null || facebookReviewsDiv !== undefined) {
var firstFour = [];
var allComments = [];
var facebookReviewsRows = facebookReviewsDiv.find('.facebookReviews');
var facebookReviewsFile = '<?php echo $json_file; ?>';
const loadMoreReviewsBtn = jQuery('.button.facebookButton');
//read file and add html
jQuery.getJSON(facebookReviewsFile, function (data) {
allComments = data;
firstFour = allComments.slice(0, 4);
allComments.splice(0, 4);
}).done(function () {
let commentsHtml = generateHtml(firstFour);
facebookReviewsRows.html(commentsHtml)
}).fail(function () {
facebookReviewsRows.html('<div class="text-center"><p>Atsiprašome įvyko klaida.</p></div>');
loadMoreReviewsBtn.attr('href', '/atsiliepimai/').text("Visi facebook atsiliepimai").attr('target', '_blank');
});
//set position when page loaded
if (jQuery(window).width() > 850) {
jQuery('form.woocommerce-cart-form').after(facebookReviewsDiv);
facebookReviewsDiv.fadeIn(1000);
} else {
facebookReviewsDiv.fadeIn(500);
}
//on btn click
loadMoreReviewsBtn.on('click', function (e) {
let loadMore = allComments.slice(0, 6);
let commentsHtml = generateHtml(loadMore);
loadMoreReviewsBtn.hide();
if (loadMoreReviewsBtn.attr('href').length > 0) {
return
}
//if want to show:
/*
allComments.splice(0, 6);
//count how many left and remove btn
if ( allComments.length === 0 ){
loadMoreReviewsBtn.hide();
if(loadMoreReviewsBtn.attr('href').length > 0){
return
}
}
*/
facebookReviewsRows.append(commentsHtml);
e.preventDefault();
});
}
function generateHtml(array) {
let html = '';
jQuery.each(array, function (key, value) {
let commentDate = new Date(value.date).getTime();
let today = new Date().getTime();
let difference_ms = Math.abs(today - commentDate);
let diffDays = Math.floor(difference_ms / 86400000);
let diffText = '';
if (diffDays < 1) {
let difHours = Math.floor(difference_ms / 3600000);
if (difHours < 0) {
diffText = '1 val.';
} else {
diffText = difHours + ' val.';
}
} else {
if (diffDays >= 14) {
diffText = Math.floor(diffDays / 7) + ' sav.';
} else {
diffText = diffDays + ' d.';
}
}
html += '<div class="large-12 col">';
html += '<div class="col-inner">';
html += '<div class="row row-small align-middle">';
html += '<div class="col small-3 medium-1 large-1 text-center">';
html += '<img src="' + value.picture + '" alt="' + value.username + '" class="facebookImage">';
html += '</div>';
html += '<div class="col small-9 medium-11 large-11">';
html += '<div class="col-inner">';
html += '<span class="facebookName">' + value.username + '<span class="facebookDate"> ⋆ ' + diffText + '</span></span>';
html += '<span class="facebookComment">' + value.message + '</span>';
html += '</div></div></div></div></div>';
});
return html;
}
});
</script>
<?php
return ob_get_clean();
}
<?php
//Store reviews trough FB Webhook
//https://developers.facebook.com/docs/graph-api/webhooks/reference/application/#plugin_comment
if ($_GET['hub_verify_token'] === 'lastFourComments') {
echo $_GET['hub_challenge'];
}
$input = file_get_contents('php://input');
$input = json_decode($input);
$comment_info = $input->entry[0]->changes[0]->value;
$new_message = [];
$updatedArr = [];
if( isset($comment_info) && !empty($comment_info) ){
$picture = "https://graph.facebook.com/" . $comment_info->from->id . "/picture?type=square";
$new_message = [
"user_id" => $comment_info->from->id,
"username" => $comment_info->from->name,
"picture" => $picture,
"message" => $comment_info->message,
"date" => $comment_info->created_time,
];
}
$updatedArr[] = $new_message;
$string = file_get_contents("comments.json");
$json_a = json_decode($string);
if( !is_null($json_a) ){
$updatedArr = array_merge($updatedArr, $json_a);
}
$updatedArrJson = json_encode($updatedArr);
file_put_contents("comments.json", $updatedArrJson);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment