function wds_post_ratings($echo = true) { $ratings = wds_ratings()->fetch_ratings_template(); if ($echo) { echo $ratings; } return $ratings; }
/** * Handle a user's rating * @since 0.1.0 */ public function post_user_rating() { $security_check_passes = !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' && isset($_POST['nonce'], $_POST['post_id'], $_POST['user_id'], $_POST['rating']) && wp_verify_nonce($_POST['nonce'], 'wds-ratings-nonce'); if (!$security_check_passes) { wp_send_json_error($_POST); } global $wpdb; $ratings_table = $wpdb->prefix . wds_ratings()->ratings_table; $post_id = absint($_POST['post_id']); $user_id = absint($_POST['user_id']); $rating = absint($_POST['rating']); $post_ratings_users = get_post_meta($post_id, '_wds_ratings_users', true); $post_ratings_score = get_post_meta($post_id, '_wds_ratings_score', true); $post_ratings_average = get_post_meta($post_id, '_wds_ratings_average', true); // default to zero if not available $post_ratings_users = $post_ratings_users ? $post_ratings_users : 0; $post_ratings_score = $post_ratings_score ? $post_ratings_score : 0; $post_ratings_average = $post_ratings_average ? $post_ratings_average : 0; $post = get_post($post_id); $old_user_rating = wds_ratings()->get_user_rating($user_id, $post_id); // Is this a new rating for this user? if (!$old_user_rating) { $user_ip = self::get_user_ip(); // log rating into ratings table $rate_log = $wpdb->query($wpdb->prepare("INSERT INTO {$ratings_table} VALUES (%d, %d, %d, %d, %s, %s, %d )", 0, $post_id, $rating, current_time('timestamp'), $user_ip, @gethostbyaddr($user_ip), $user_id)); // update with new rating data $post_ratings_users = intval($post_ratings_users) + 1; $post_ratings_score = $post_ratings_score + intval($rating); $post_ratings_average = round($post_ratings_score / $post_ratings_users, 2); update_post_meta($post_id, '_wds_ratings_users', $post_ratings_users); update_post_meta($post_id, '_wds_ratings_score', $post_ratings_score); update_post_meta($post_id, '_wds_ratings_average', $post_ratings_average); } else { // We're updating the user's rating - so we need the post meta $query = $wpdb->prepare("\n\t\t\t\tUPDATE\n\t\t\t\t\t{$ratings_table}\n\t\t\t\tSET\n\t\t\t\t\trating = %d\n\t\t\t\tWHERE\n\t\t\t\t\tuserid = %d\n\t\t\t\tAND\n\t\t\t\t\tpostid = %d\n\t\t\t\t", $rating, $user_id, $post_id); // Do the query and check for errors if (false === $wpdb->query($query)) { if (isset($wp_error)) { wp_send_json_error($wpdb->last_error); } } // update the post's rating data $post_ratings_score = $post_ratings_score + intval($rating) - intval($old_user_rating); $post_ratings_average = round($post_ratings_score / $post_ratings_users, 2); update_post_meta($post_id, '_wds_ratings_users', $post_ratings_users); update_post_meta($post_id, '_wds_ratings_score', $post_ratings_score); update_post_meta($post_id, '_wds_ratings_average', $post_ratings_average); } // Allow other plugins to hook in do_action('wds_rate_post', $user_id, $post_id, $rating); wp_send_json_success($_POST); }
public function filter_label() { if (!isset($_GET['post']) && !isset($_GET['post_type'])) { $label = __('Post'); } else { $pt = isset($_GET['post_type']) ? $_GET['post_type'] : get_post_type($_GET['post']); $pt_object = get_post_type_object($pt); $label = $pt_object->labels->singular_name; } switch (wds_ratings()->fetch_option('filter_type')) { case 'inclusive': $label = sprintf(__('Show Ratings on this %s', 'wds_ratings'), $label); break; default: $label = sprintf(__('Hide Ratings on this %s', 'wds_ratings'), $label); break; } return $label; }
* @license GPL-2.0+ * @link http://webdevstudios.com * @copyright 2015 WebDevStudios * * Plugin Name: WDS Ratings * Plugin URI: http://www.webdevstudios.com * Description: Allow users to rate posts * Version: 0.4.0 * Author: WebDevStudios * Author URI: http://webdevstudios.com * License: GPL-2.0+ * License URI: http://www.gnu.org/licenses/gpl-2.0.txt * Text Domain: wds_ratings * Domain Path: /languages */ // If this file is called directly, abort. if (!defined('WPINC')) { die; } // load the plugin if (!class_exists('WDS_Ratings')) { require_once plugin_dir_path(__FILE__) . 'wds-ratings-class.php'; // init our class wds_ratings()->hooks(); // include helpers require_once wds_ratings()->path . 'lib/helpers.php'; // include widget if enabled if ('on' === wds_ratings()->fetch_option('enable_widget')) { require_once wds_ratings()->path . 'lib/widget.php'; } }