.', '') : 0; $per = $nratings ? number_format((float)((($nratings/$ncasts)/5)*100), 2, '.', '') : 0; $Response[$pid]['disable'] = 'false'; if($stars) { $Ips = get_post_meta($pid, '_kksr_ips', true) ? unserialize(base64_decode(get_post_meta($pid, '_kksr_ips', true))) : array(); if(!in_array($ip, $Ips)) { $Ips[] = $ip; } $ips = base64_encode(serialize($Ips)); update_post_meta($pid, '_kksr_ratings', $nratings); update_post_meta($pid, '_kksr_casts', $ncasts); update_post_meta($pid, '_kksr_ips', $ips); update_post_meta($pid, '_kksr_avg', $avg); if (pasargad_get_option('one_rating')){ $Response[$pid]['disable'] = 'true'; }else{ $Response[$pid]['disable'] = 'false'; } do_action('kksr_rate', $pid, $stars, $ip); } else { do_action('psfr_init', $pid, number_format((float)($avg*($total_stars/5)), 2, '.', '').'/'.$total_stars, $ncasts); } $legend = '[total]'; $avrages = number_format((float)($avg*($total_stars/5)), 2, '.', '').'/'.$total_stars; $rating_score = number_format((float)($avg*($total_stars/5)), 2, '.', ''); $user_votes = $ncasts; $legend = str_replace('[total]','امتیاز'.$rating_score.' ( '.$user_votes.' رای )', $legend); $Response[$pid]['legend'] = str_replace('[per]',$per.'px', $legend); $Response[$pid]['fuel'] = $per; } $Response[$pid]['success'] = true; endforeach; echo json_encode($Response); die(); } protected function trim_csv_cb($value) { if(trim($value)!="") return true; return false; } public function markup($id=false) { $id = !$id ? get_the_ID() : $id; $disabled = false; if(get_post_meta($id, '_kksr_ips', true)) { $Ips = unserialize(base64_decode(get_post_meta($id, '_kksr_ips', true))); $ip = $_SERVER['REMOTE_ADDR']; if(in_array($ip, $Ips)) { if (pasargad_get_option('one_rating')){ $disabled = true; }else{ $disabled = false; } } } ////////////////////////////////////////////////////////////////////////////////// // @ output functions ////////////////////////////////////////////////////////////////////////////////// $score = get_post_meta($id, '_kksr_ratings', true) ? get_post_meta($id, '_kksr_ratings', true) : 0; $votes = get_post_meta($id, '_kksr_casts', true) ? get_post_meta($id, '_kksr_casts', true) : 0; $avg = $score ? number_format((float)($score/$votes), 2, '.', '') : 0; $title = get_the_title($id); $postid = get_the_ID($id); $markup = '
'; $total_stars = 5; for($ts = 1; $ts <= $total_stars; $ts++) { $markup .= ''; } $markup .='
'; $markup .= apply_filters('psfr_legend', '[total]', $id); $markup .='
'; return $markup; } public function ps_star_rating($pid=false) { return $this->markup($pid); return ''; } public function ps_star_ratings_get($total=5) { global $wpdb; $table = $wpdb->prefix . 'postmeta'; if(!$cat) $rated_posts_to_db = $wpdb->get_results("SELECT a.ID, a.post_title, b.meta_value AS 'ratings' FROM " . $wpdb->posts . " a, $table b, $table c WHERE a.post_status='publish' AND a.ID=b.post_id AND a.ID=c.post_id AND b.meta_key='_kksr_avg' AND c.meta_key='_kksr_casts' ORDER BY b.meta_value DESC, c.meta_value DESC LIMIT $total"); else { $table2 = $wpdb->prefix . 'term_taxonomy'; $table3 = $wpdb->prefix . 'term_relationships'; $rated_posts_to_db = $wpdb->get_results("SELECT a.ID, a.post_title, b.meta_value AS 'ratings' FROM " . $wpdb->posts . " a, $table b, $table2 c, $table3 d, $table e WHERE c.term_taxonomy_id=d.term_taxonomy_id AND c.term_id=$cat AND d.object_id=a.ID AND a.post_status='publish' AND a.ID=b.post_id AND a.ID=e.post_id AND b.meta_key='_kksr_avg' AND e.meta_key='_kksr_casts' ORDER BY b.meta_value DESC, e.meta_value DESC LIMIT $total"); } return $rated_posts_to_db; } ////////////////////////////////////////////////////////////////////////////////// // @ Updates And order meta_key ////////////////////////////////////////////////////////////////////////////////// public function sort_columns($Args) { $Args = array_merge($Args, array('ps_star_ratings' => 'ps_star_ratings') ); return wp_parse_args($Args); } public function sort_columns_helper($Query) { $orderby = $Query->get( 'orderby'); if($orderby=='ps_star_ratings') { $Query->set('meta_key','_kksr_avg'); $Query->set('orderby','meta_value_num'); } } ////////////////////////////////////////////////////////////////////////////////// // @ Google schema.org for posts rating ////////////////////////////////////////////////////////////////////////////////// \ public function google_schema_legend($legend, $id){ $title = get_the_title($id); $exp = get_the_excerpt($id); $url = get_permalink($id); $best = 5; $score = get_post_meta($id, '_kksr_ratings', true) ? get_post_meta($id, '_kksr_ratings', true) : 0; if($score){ $votes = get_post_meta($id, '_kksr_casts', true) ? get_post_meta($id, '_kksr_casts', true) : 0; $avg = $score ? number_format((float)($score/$votes), 2, '.', '') : 0; $per = $score ? number_format((float)((($score/$votes)/5)*100), 2, '.', '') : 0; $leg = str_replace('[total]', 'امتیاز '.$avg.' ( '.$votes.' رای )', $legend); $snippet .= $leg; $snippet .= '
'; $snippet .= ''; } return $snippet; return $legend; } } $psStarRatings_obj = new pasargad_framework_psStarRatings('Ajax_send_psfr', 'ps Star Ratings', '2.4'); add_filter('psfr_legend', array($psStarRatings_obj, 'google_schema_legend'), 1, 2); ////////////////////////////////////////////////////////////////////////////////// // @ Register scripts ////////////////////////////////////////////////////////////////////////////////// add_action('wp_enqueue_scripts', array($psStarRatings_obj, 'js')); // AJAX add_action('wp_ajax_pasargad_star_rating', array($psStarRatings_obj, 'pasargad_star_rating')); add_action('wp_ajax_nopriv_pasargad_star_rating', array($psStarRatings_obj, 'pasargad_star_rating')); ///////////////////////////////////////////////////////////////////////////////// // @ Add to theme ///////////////////////////////////////////////////////////////////////////////// if(!function_exists('ps_star_ratings')){ function ps_star_ratings($pid=false) { global $psStarRatings_obj; return $psStarRatings_obj->ps_star_rating($pid); } } endif; ?>