Sindbad~EG File Manager
<?php
/*
* Security : blocking direct access
* Source : http://codex.wordpress.org/Theme_Development#Template_Files
*/
defined('ABSPATH') or die("Access Restricted");
/**
* Register the vote in the db
*
*/
function mwp_getVote($post_id, $voter_ip){
global $wpdb;
$table = $wpdb->prefix . 'ec_stars_votes';
return $wpdb->get_row($wpdb->prepare("SELECT `post_id` FROM $table WHERE `voter_ip` = %s AND `post_id` = %d LIMIT 0, 1", $voter_ip, $post_id));
}
add_action( MWP_AJAX_PREFIX . '_ec_stars_rating', 'handle_vote');
add_action( MWP_AJAX_PREFIX . '_nopriv_ec_stars_rating', 'handle_vote');
function handle_vote(){
global $wpdb;
$table = $wpdb->prefix . 'ec_stars_votes';
// Support for older wp versions
if (! defined('YEAR_IN_SECONDS')) {
define('YEAR_IN_SECONDS', 365 * 24 * 60 * 60);
}
/* Get the POST request data */
// The post id
$post_id = intval(@$_POST['post_id']);
// The rating value (1-5)
$rating = intval(@$_POST['rating']);
// The ip
$IP = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR']
: $_SERVER['REMOTE_ADDR'];
$cookie_name = 'ec_sr_' . $post_id;;
// If we have voted set the cookie and return
if (isset($_COOKIE[$cookie_name]) || mwp_getVote($post_id, $IP) !== null) {
setcookie($cookie_name, 'true', time() + YEAR_IN_SECONDS, '/');
die(json_encode(array('status' => 0)));
}
// else get the current rating and increase it appropiately
$current_rating = intval(get_post_meta($post_id, 'ec_stars_rating', true));
$current_votes = intval(get_post_meta($post_id, 'ec_stars_rating_count', true));
// if there is no rating or no votes, or the rating is invalid, return
if ((empty($current_rating) && $current_rating !== 0) ||
(empty($current_votes) && $current_votes !== 0) ||
$rating > 5 ||
$rating < 1) {
die(json_encode(array(
'status' => 2,
'current_votes' => $current_votes,
'current_rating' => $current_rating
)));
}
// Update with the new rating
update_post_meta($post_id, 'ec_stars_rating', $current_rating + $rating);
update_post_meta($post_id, 'ec_stars_rating_count', $current_votes + 1);
// Insert the vote in the db and set the cookie for a year
$wpdb->insert(
$table,
array(
'voter_ip' => $IP,
'post_id' => $post_id
),
array('%s', '%d')
);
setcookie($cookie_name, 'true', time() + YEAR_IN_SECONDS, '/');
// Return a success message
die(json_encode(array(
'status' => 1,
'votes' => $current_votes + 1,
'total' => $current_rating + $rating,
'result' => ($current_rating + $rating) / ($current_votes + 1)
)));
}
/*
* Increment Post Views
* when caching plugin is being used, the count will be updated
*
*/
add_action( MWP_AJAX_PREFIX . '_mwp_increment_votes', 'mwp_increment_votes' );
add_action( MWP_AJAX_PREFIX . '_nopriv_mwp_increment_votes', 'mwp_increment_votes' );
function mwp_increment_votes() {
if( empty( $_GET['postvotes_id'] ) )
return;
if( !defined( 'WP_CACHE' ) || !WP_CACHE )
return;
if(function_exists('ec_stars_rating')) {
$post_id = intval( $_GET['postvotes_id'] );
$current_rating = intval(get_post_meta($post_id, 'ec_stars_rating', true));
$current_votes = intval(get_post_meta($post_id, 'ec_stars_rating_count', true));
if (empty($current_votes) || $current_votes == '') {
$result = 0;
} else {
$count = $current_rating / $current_votes;
$result = (100 - $count * 100 / 5);
}
die(json_encode(array(
'status' => 1,
'current_votes' => $current_votes,
'current_rating' => $current_rating,
'result' => $result,
)));
}
}
function ajax_postvotes_footer() {
global $post;
if( !defined( 'WP_CACHE' ) || !WP_CACHE )
return;
if(function_exists('ec_stars_rating')) {
if ( !wp_is_post_revision( $post->ID ) && is_single() ) {
?>
<script>
jQuery(document).ready(function($) {
var postvotes_id = '<?php echo $post->ID; ?>';
$.ajax({
type: "GET",
url: "<?php echo MWP_AJAX_ENDPOINT; ?>",
dataType: 'json',
data: ({ action: 'mwp_increment_votes', postvotes_id:postvotes_id}),
success: function(data){
jQuery('.ec-stars-rating-count').text(data.current_votes);
jQuery('.ec-stars-rating-value').text(data.current_rating);
var result_vote = data.result;
jQuery('.ec-stars-overlay').attr('style','width:'+result_vote+'%');
}
})
});
</script>
<?php
}
}
}
add_action('wp_footer', 'ajax_postvotes_footer', 99999);
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists