-
Notifications
You must be signed in to change notification settings - Fork 3
/
imdb-post-ratings.php
446 lines (381 loc) · 12.7 KB
/
imdb-post-ratings.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<?php
/*
Plugin Name: IMDB Post Ratings
Plugin URI: http://wedevs.com/
Description: Post ratings plugin that acts like IMDB
Version: 0.1
Author: Tareq Hasan
Author URI: http://tareq.wedevs.com/
License: GPL2
*/
/**
* Copyright (c) 2013 Tareq Hasan (email: tareq@wedevs.com). All rights reserved.
*
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* **********************************************************************
*/
// don't call the file directly
if ( !defined( 'ABSPATH' ) )
exit;
// load the widget
require_once dirname( __FILE__ ) . '/top-rated-widget.php';
/**
* IMDB_Post_Ratings class
*
* @class IMDB_Post_Ratings The class that holds the entire IMDB_Post_Ratings plugin
*/
class IMDB_Post_Ratings {
/**
* @var string table name
*/
private $table;
/**
* @var object $wpdb object
*/
private $db;
/**
* Constructor for the IMDB_Post_Ratings class
*
* Sets up all the appropriate hooks and actions
* within our plugin.
*
* @uses register_activation_hook()
* @uses register_deactivation_hook()
* @uses is_admin()
* @uses add_action()
*/
public function __construct() {
global $wpdb;
// setup table name
$this->db = $wpdb;
$this->table = $this->db->prefix . 'imdb_rating';
// Activate and deactivate hooks
register_activation_hook( __FILE__, array($this, 'activate') );
register_deactivation_hook( __FILE__, array($this, 'deactivate') );
// Localize our plugin
add_action( 'init', array($this, 'localization_setup') );
// Loads frontend scripts and styles
add_action( 'wp_enqueue_scripts', array($this, 'enqueue_scripts') );
// Ajax vote
add_action( 'wp_ajax_ipr_vote', array($this, 'ajax_insert_vote') );
add_action( 'wp_ajax_ip_vote_del', array($this, 'ajax_delete_vote') );
}
/**
* Initializes the IMDB_Post_Ratings() class
*
* Checks for an existing IMDB_Post_Ratings() instance
* and if it doesn't find one, creates it.
*/
public static function init() {
static $instance = false;
if ( !$instance ) {
$instance = new IMDB_Post_Ratings();
}
return $instance;
}
/**
* Placeholder for activation function
*
* Nothing being called here yet.
*/
public function activate() {
$sql = "CREATE TABLE {$this->table} (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`post_id` int(11) unsigned NOT NULL,
`post_type` varchar(20) NOT NULL,
`user_id` int(11) unsigned NOT NULL,
`vote` mediumint(2) unsigned NOT NULL DEFAULT '1',
`updated` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `post_id` (`post_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
$this->db->query( $sql );
}
/**
* Placeholder for deactivation function
*
* Nothing being called here yet.
*/
public function deactivate() {
}
/**
* Initialize plugin for localization
*
* @uses load_plugin_textdomain()
*/
public function localization_setup() {
load_plugin_textdomain( 'ipr', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Enqueue scripts and styles
*
* Allows plugin assets to be loaded.
*
* @uses wp_enqueue_script()
* @uses wp_localize_script()
* @uses wp_enqueue_style
*/
public function enqueue_scripts() {
/**
* All styles goes here
*/
wp_enqueue_style( 'ipr-styles', plugins_url( 'css/style.css', __FILE__ ) );
/**
* All scripts goes here
*/
wp_enqueue_script( 'ipr-scripts', plugins_url( 'js/script.js', __FILE__ ), array('jquery'), false, true );
wp_localize_script( 'ipr-scripts', 'ipr', array(
'action' => 'ipr_vote',
'del_action' => 'ip_vote_del',
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'ipr-ratings' ),
'loggedin' => is_user_logged_in() ? 'true' : 'false',
'loginMessage' => __( 'Please login to vote', 'ipr' ),
'errorMessage' => __( 'Something went wrong', 'ipr' )
) );
}
function flush_cache( $post_id ) {
delete_transient( 'ipr_rating_' . $post_id );
}
/**
* Ajax handler for inserting a vote
*
* @return void
*/
function ajax_insert_vote() {
check_ajax_referer( 'ipr-ratings', 'nonce' );
// bail out if not logged in
if ( !is_user_logged_in() ) {
wp_send_json_error();
}
// so, the user is logged in huh? proceed on
$post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0;
$vote = isset( $_POST['vote'] ) ? intval( $_POST['vote'] ) : 0;
$user_id = get_current_user_id();
if ( $post_id && $vote ) {
// if already voted, then simply update the existing
// else, add a new vote
if ( $this->get_user_vote( $post_id, $user_id ) ) {
$this->update_vote( $post_id, $user_id, $vote );
} else {
$this->add_vote( $post_id, $user_id, $vote );
}
}
// flush the cache
$this->flush_cache( $post_id );
wp_send_json_success( array(
'vote_i18n' => number_format_i18n( $vote ),
'vote' => $vote
) );
}
/**
* Ajax handler for deleting a vote
*
* @return void
*/
function ajax_delete_vote() {
check_ajax_referer( 'ipr-ratings', 'nonce' );
// bail out if not logged in
if ( !is_user_logged_in() ) {
wp_send_json_error();
}
// so, the user is logged in huh? proceed on
$post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0;
$user_id = get_current_user_id();
$this->delete_vote( $post_id, $user_id );
// flush the cache
$this->flush_cache( $post_id );
wp_send_json_success( array(
'vote_i18n' => '-',
'vote' => '0'
) );
}
/**
* Gets a user vote for a post
*
* @param int $post_id
* @param int $user_id
* @return bool|object
*/
function get_user_vote( $post_id, $user_id ) {
$sql = "SELECT vote FROM {$this->table} WHERE post_id = %d AND user_id = %d";
return $this->db->get_row( $this->db->prepare( $sql, $post_id, $user_id ) );
}
/**
* Insert a user vote
*
* @param int $post_id
* @param int $user_id
* @param int $vote
* @return bool
*/
public function add_vote( $post_id, $user_id, $vote ) {
$post_type = get_post_field( 'post_type', $post_id );
return $this->db->insert(
$this->table, array(
'post_id' => $post_id,
'post_type' => $post_type,
'user_id' => $user_id,
'vote' => $vote,
'updated' => current_time( 'mysql' )
), array(
'%d',
'%s',
'%d',
'%d',
'%s'
)
);
}
/**
* Update a user vote
*
* @param int $post_id
* @param int $user_id
* @param int $vote
* @return bool
*/
public function update_vote( $post_id, $user_id, $vote ) {
return $this->db->update(
$this->table, array(
'post_id' => $post_id,
'user_id' => $user_id,
'vote' => $vote,
'updated' => current_time( 'mysql' )
),
//where
array(
'post_id' => $post_id,
'user_id' => $user_id
),
//data format
array(
'%d',
'%d',
'%d',
'%s'
),
//where format
array(
'%d',
'%d'
)
);
}
/**
* Delete a user vote
*
* @param int $post_id
* @param int $user_id
* @return bool
*/
public function delete_vote( $post_id, $user_id ) {
$query = "DELETE FROM {$this->table} WHERE post_id = %d AND user_id = %d";
return $this->db->query( $this->db->prepare( $query, $post_id, $user_id ) );
}
/**
* Get rating for a post
*
* @param int $post_id
* @return obj
*/
function get_rating( $post_id ) {
$result = get_transient( 'ipr_rating_' . $post_id );
if ( false === $result ) {
$sql = "SELECT SUM(vote) / COUNT(id) AS rating, COUNT(id) AS voter
FROM {$this->table}
WHERE post_id = %d";
$result = $this->db->get_row( $this->db->prepare( $sql, $post_id ) );
set_transient( 'ipr_rating_' . $post_id, $result, 3600 );
}
return $result;
}
/**
* Get top rated posts
*
* @param int $post_type
* @param int $count
* @param int $offset
* @return array
*/
function get_top_rated( $post_type = 'post', $count = 10, $offset = 0 ) {
$sql = "SELECT format(SUM(im.vote) / COUNT(im.id), 2) AS rating, COUNT(im.id) AS voter, im.post_id
FROM {$this->table} im
LEFT JOIN {$this->db->posts} p ON p.ID = im.post_id
WHERE im.post_type = '$post_type' AND p.post_status = 'publish'
GROUP BY im.post_id
ORDER BY rating DESC
LIMIT $offset, $count";
// cache the result
$result = get_transient( 'ipr_top_' . $post_type );
if ( false === $result ) {
$result = $this->db->get_results( $sql );
set_transient( 'ipr_top_' . $post_type, $result, 3600 );
}
return $result;
}
/**
* Display the rating bar
*
* @global object $post
* @param int $post_id
*/
public function rating_input( $post_id = null ) {
global $post;
if ( !$post_id ) {
$post_id = $post->ID;
}
$base_rating = apply_filters( 'ip_base_rating', 10 );
$given_vote = $this->get_user_vote( $post_id, get_current_user_id() );
$user_voted = false;
// check if user voted
if ( $given_vote ) {
$user_vote = $given_vote->vote;
$user_vote_i18n = number_format_i18n( $given_vote->vote );
$user_voted = true;
} else {
$user_vote = 0;
$user_vote_i18n = '-';
}
?>
<div class="ip-rating-container">
<span class="ip-rating-stars">
<?php for ($i = 1; $i <= $base_rating; $i++) { ?>
<span title="<?php printf( __( 'Click to rate: %s', 'ipr' ), number_format_i18n( $i ) ); ?>" data-i18n="<?php echo number_format_i18n( $i ); ?>" data-id="<?php echo $post_id; ?>" data-vote="<?php echo $i; ?>"></span>
<?php } ?>
</span> <!-- .ip-rating-stars -->
<span class="ip-rating-preview">
<span class="ip-rating-hover-value" data-vote="<?php echo $user_vote; ?>"><?php echo $user_vote_i18n; ?></span>
<span class="ip-rating-sep">/</span>
<span class="ip-baserating"><?php echo number_format_i18n( $base_rating ); ?></span>
</span> <!-- .ip-rating-preview -->
<span class="ip-rating-cancel">
<span class="ipr-loading ipr-hide"></span>
<a href="#" data-id="<?php echo $post_id; ?>" class="ip-delete<?php echo $user_voted ? '' : ' ipr-hide'; ?>"><span>X</span></a>
</span> <!-- .ip-rating-cancel -->
</div> <!-- .ip-rating-container -->
<?php
}
}
// IMDB_Post_Ratings
$imdb_ratings = IMDB_Post_Ratings::init();