-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost-star.php
172 lines (140 loc) · 4.4 KB
/
post-star.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
<?php
/**
* Plugin Name: Post Star
* Plugin URI: http://lbideias.com.br
* Description: This plugin allows users to rate your post and generates an average to determine the quality of content.
* Author: leobaiano
* Author URI: http://lbideias.com.br/
* Version: 1.0
* License: GPLv2 or later
* Text Domain: lb_ps
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) exit;
// Sets the plugin path.
define( 'PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
/**
* Class Post Star
* @version 1.0
* @author Leo Baiano <[email protected]>
*/
class PostStar {
public function __construct() {
add_action( 'init', array( $this, 'createTable' ) );
add_action( 'wp_footer', array( $this, 'loadScriptRating' ) );
add_action( 'wp_ajax_add_rate', 'ajax_add_rate' );
add_action( 'wp_ajax_nopriv_add_rate', 'ajax_add_rate' );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 999 );
}
/**
* Create table Rating
*/
public function createTable() {
global $wpdb;
$tableRating = $wpdb->prefix . 'lb_ps_rating';
if ( $wpdb->get_var( "SHOW TABLES LIKE '$tableRating'" ) != $tableRating ) {
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$sql = "CREATE TABLE IF NOT EXISTS `$tableRating` (
`id` int(11) NOT NULL,
`post_id` int(11) NOT NULL,
`rating` int(1) NOT NULL,
`user_ip` varchar(13) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`status` int(1) NOT NULL
);";
dbDelta( $sql );
}
}
/**
* Load scripts
*/
public function enqueue_scripts() {
wp_enqueue_style( 'lb_ps', plugins_url( 'libs/jrating/jRating.jquery.css', __FILE__ ), array(), null, 'all' );
wp_enqueue_script( 'lb_ps', plugins_url( 'libs/jrating/jRating.jquery.js', __FILE__ ), array( 'jquery' ), null, true );
}
public function checkVote( $postID, $userIP ) {
global $wpdb;
$tableRating = $wpdb->prefix . 'lb_ps_rating';
$check = $wpdb->get_row("SELECT * FROM $tableRating WHERE post_id = '$postID' AND user_ip = '$userIP'");
return $check;
}
public static function addRating( $postID, $rating ) {
global $wpdb;
$tableRating = $wpdb->prefix . 'lb_ps_rating';
$userIP = $_SERVER['REMOTE_ADDR'];
$check = self::checkVote( $postID, $userIP );
if ( empty( $check ) ) {
$wpdb->insert( $tableRating, array(
'post_id' => $postID,
'rating' => $rating,
'user_ip' => $userIP,
'status' => 1
)
);
return true;
}
else{
return false;
}
}
public static function getScorePost( $postID ) {
global $wpdb;
$tableRating = $wpdb->prefix . 'lb_ps_rating';
$rates = $wpdb->get_row( "SELECT *, avg(rating) rate FROM $tableRating WHERE post_id = '$postID'" );
if( empty( $rates->rate ) )
return 0;
else
return round( $rates->rate );
}
public function loadScriptRating() {
$disable = 'false';
if( is_single() ){
$postID = get_the_ID();
$userIP = $_SERVER['REMOTE_ADDR'];
$check = self::checkVote( $postID, $userIP );
if ( !empty( $check ) )
$disable = 'true';
}
echo '
<script>
jQuery(document).ready(function(){
jQuery(".lb_ps_rating").jRating({
length : 5,
bigStarsPath : "' . plugins_url("libs/jrating/icons/stars.png", __FILE__ ) . '",
phpPath : "' . plugins_url("post-star/post_star.php", __FILE__ ) . '",
step : true,
rateMax : 5,
decimalLength : 0,
isDisabled : ' . $disable . '
});
});
</script>
';
}
}
new PostStar;
function displayPostStar( $postID) {
$urlAjaxAdmin = admin_url('admin-ajax.php');
$score = PostStar::getScorePost( $postID );
$view .= '<section class="lb_ps_container">';
$view .= '<div class="lb_ps_rating" data-average="' . $score . '" data-id="' . $postID . '" data-urlAdmin="'. $urlAjaxAdmin . '"></div>';
$view .= '</section>';
echo $view;
}
function ajax_add_rate() {
$postID = $_GET['postID'];
$rating = $_GET['rating'];
$addVote = PostStar::addRating( $postID, $rating );
if( $addVote )
$status[] = array(
"status" => 1,
"menssage" => "Success: Your vote has been counted."
);
else
$status[] = array(
"status" => 0,
"menssage" => "Error: This IP has already voted for this post."
);
echo json_encode($status);
die();
}