-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathreviews.php
182 lines (155 loc) · 6.95 KB
/
reviews.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/*
Configuration.
Note: You'll need a Google maps API key - requires both Maps JS and Places API services enabled.
*/
$place_ID = ''; // Get from: https://developers.google.com/maps/documentation/javascript/examples/places-placeid-finder
$business_type = ''; // Example: FinancialService (http://schema.org)
$business_name = '';
$street_address = '';
$locality = ''; // Example: Docklands (http://schema.org/addressLocality)
$region = '';
$post_code = '';
$logo_path = 'logo.png';
$min_star = '1'; // The minimum star rating (min = 1)
$max_rows = '5'; // The maximum number of results (max = 5)
$api_key = '';
?>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=<?php echo $api_key; ?>&libraries=places"></script>
<script>
(function($) {
$.googlePlaces = function(element, options) {
var defaults = {
placeId: '<?php echo $place_ID; ?>',
render: ['reviews'],
min_rating: <?php echo $min_star; ?>,
max_rows: <?php echo $max_rows; ?>,
rotateTime: false
};
var plugin = this;
plugin.settings = {}
var $element = $(element),
element = element;
plugin.init = function() {
plugin.settings = $.extend({}, defaults, options);
$element.html("<div id='the-reviews'></div>"); // create a plug for google to load data into
initialize_place(function(place){
plugin.place_data = place;
// render specified sections
if(plugin.settings.render.indexOf('reviews') > -1){
renderReviews(plugin.place_data.reviews);
if(!!plugin.settings.rotateTime) {
initRotation();
}
}
});
}
var initialize_place = function(c){
var map = new google.maps.Map(document.getElementById('the-reviews'));
var request = {
placeId: plugin.settings.placeId
};
var service = new google.maps.places.PlacesService(map);
service.getDetails(request, function(place, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
c(place);
}
});
}
var sort_by_date = function(ray) {
ray.sort(function(a, b){
var keyA = new Date(a.time),
keyB = new Date(b.time);
// Compare the 2 dates
if(keyA < keyB) return -1;
if(keyA > keyB) return 1;
return 0;
});
return ray;
}
var filter_minimum_rating = function(reviews){
for (var i = reviews.length -1; i >= 0; i--) {
if(reviews[i].rating < plugin.settings.min_rating){
reviews.splice(i,1);
}
}
return reviews;
}
var renderReviews = function(reviews) {
reviews = sort_by_date(reviews);
reviews = filter_minimum_rating(reviews);
var html = "";
var row_count = (plugin.settings.max_rows > 0)? plugin.settings.max_rows - 1 : reviews.length - 1;
// make sure the row_count is not greater than available records
row_count = (row_count > reviews.length-1)? reviews.length -1 : row_count;
for (var i = row_count; i >= 0; i--) {
var stars = renderStars(reviews[i].rating);
var date = convertTime(reviews[i].time);
html = html+"<div class='review-item' itemprop='review' itemscope itemtype='http://schema.org/Review'><img src='"+reviews[i].profile_photo_url+"'/><div class='review-inner'><meta itemprop='itemReviewed' content='http://schema.org/<?php echo $business_type; ?>' /><p class='review-text' itemprop='description'>"+reviews[i].text+"</p><div class='review-meta'><span class='review-author' itemprop='author'>"+reviews[i].author_name+"</span><span class='review-sep'>, </span><span class='review-date' itemprop='datePublished'>"+date+"</span></div>"+stars+"</div></div>"
};
$element.append(html);
}
var initRotation = function() {
var $reviewEls = $element.children('.review-item');
var currentIdx = $reviewEls.length > 0 ? 0 : false;
$reviewEls.hide();
if(currentIdx !== false) {
$($reviewEls[currentIdx]).show();
setInterval(function(){
if(++currentIdx >= $reviewEls.length) {
currentIdx = 0;
}
$reviewEls.hide();
$($reviewEls[currentIdx]).fadeIn('slow');
}, plugin.settings.rotateTime);
}
}
var renderStars = function(rating) {
var stars = "<div class='review-stars' itemprop='reviewRating' itemscope itemtype='http://schema.org/Rating'><meta itemprop='worstRating' content='1'/><meta itemprop='ratingValue' content='" + rating + "'/><meta itemprop='bestRating' content='5'/><ul>";
// fill in gold stars
for (var i = 0; i < rating; i++) {
stars = stars+"<li class='star'>★</li>";
};
// fill in empty stars
if(rating < 5){
for (var i = 0; i < (5 - rating); i++) {
stars = stars+"<li class='star inactive'>★</li>";
};
}
stars = stars+"</ul></div>";
return stars;
}
var convertTime = function(UNIX_timestamp) {
var a = new Date(UNIX_timestamp * 1000);
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var time = months[a.getMonth()] + ' ' + a.getDate() + ', ' + a.getFullYear();
return time;
}
plugin.init();
}
$.fn.googlePlaces = function(options) {
return this.each(function() {
if (undefined == $(this).data('googlePlaces')) {
var plugin = new $.googlePlaces(this, options);
$(this).data('googlePlaces', plugin);
}
});
}
})(jQuery);
</script>
<meta itemscope itemtype="http://schema.org/<?php echo $business_type; ?>" />
<meta itemprop="name" content="<?php echo $business_name; ?>" />
<meta itemprop="image" content="<?php echo $logo_path; ?>" />
<meta itemprop="address" itemscope itemtype="http://schema.org/PostalAddress"/>
<meta itemprop="streetAddress" content="<?php echo $street_address; ?>"/>
<meta itemprop="addressLocality" content="<?php echo $locality; ?>"/>
<meta itemprop="addressRegion" content="<?php echo $region; ?>"/>
<meta itemprop="postalCode" content="<?php echo $post_code; ?>"/>
<div id="google-reviews"></div>
<script>
$(document).ready(function() {
$("#google-reviews").googlePlaces({});
});
</script>
<link rel="stylesheet" id="google-reviews-css" href="google-reviews.css" type="text/css" media="all" />