public static function get_ratings_single_scores($post_id, $review_id, $template_id) { // Old Rating //$ratings = get_post_meta( $post_id, 'rwp_ratings', true ); //$old_ratings = ( isset( $ratings[ $review_id ] ) ) ? $ratings[ $review_id ] : array( 'rating_count' => 0, 'rating_total_score' => 0 ); // Templates $templates = self::get_option('rwp_templates'); $template = isset($templates[$template_id]) ? $templates[$template_id] : array(); // Preferences $preferences = RWP_Reviewer::get_option('rwp_preferences'); // New Ratings $ratings = get_post_meta($post_id, 'rwp_rating_' . $review_id); $new_ratings = is_array($ratings) ? $ratings : array(); $moderation = self::preferences_field('preferences_rating_before_appears', $preferences, true); $new_ratings = RWP_Reviewer::filter_ratings($new_ratings, $moderation); if (empty($template)) { return array(); } $order = self::template_field('template_criteria_order', $template, true); $criteria = self::template_field('template_criterias', $template, true); $order = $order == null ? array_keys($criteria) : $order; $scores = array(); $count = count($new_ratings); if ($count > 0) { $precision = self::get_decimal_places(self::preferences_field('preferences_step', $preferences, true)); foreach ($order as $i) { $scores[$i] = 0; } foreach ($new_ratings as $rating) { if (!is_array($rating)) { $rating = maybe_unserialize($rating); } foreach ($order as $i) { $scores[$i] += isset($rating['rating_score']) && isset($rating['rating_score'][$i]) ? $rating['rating_score'][$i] : 0; } } foreach ($order as $i) { $scores[$i] = round($scores[$i] / $count, $precision); } } else { foreach ($order as $i) { $scores[$i] = 0; } } //self::pretty_print($new_ratings); //self::pretty_print($old_ratings); //self::pretty_print($scores); return array('scores' => $scores, 'count' => $count); }
public function do_shortcode($atts) { extract(shortcode_atts(array('id' => '', 'theme' => '', 'template' => '', 'rating' => '', 'branch' => '', 'post' => get_the_ID()), $atts)); $this->post_id = $post; $post_id = $post; //return "Review " . $theme; $review_id = intval($id); //echo "Review ID: " . $review_id; //echo "Branch: " . $branch . '<br/>'; if ($review_id != $this->auto_review_id) { // Manul Review // Get post reviews $reviews = get_post_meta($this->post_id, 'rwp_reviews', true); // Check if user has inserted a valid review ID if (!isset($reviews[$id])) { return '<p>' . __('No review found! Insert a valid review ID.', $this->plugin_slug) . '</p>'; } // Get Review $this->review = $reviews[$id]; } else { // Auto Review $this->review = $this->get_auto_review($template); } // Get Options $this->preferences = RWP_Reviewer::get_option('rwp_preferences'); $templates = RWP_Reviewer::get_option('rwp_templates'); $this->template = isset($templates[$this->review['review_template']]) ? $templates[$this->review['review_template']] : array(); $this->template['template_theme'] = empty($theme) ? $this->template['template_theme'] : 'rwp-theme-' . $theme; // Ratings per page $this->ratings_per_page = $this->preferences_field('preferences_users_reviews_per_page', true); // Rating param $this->preferences['preferences_rating_mode'] = empty($rating) ? $this->preferences['preferences_rating_mode'] : $rating; // Utility variable $this->is_UR = $this->review_field('review_type', true) == 'UR' ? true : false; $img = $this->review_field('review_image', true); if ($this->review_field('review_use_featured_image', true) == 'no') { $this->has_img = !empty($img) ? true : false; } else { $this->has_img = has_post_thumbnail($this->post_id); } // Ratings $ratings = get_post_meta($post_id, 'rwp_rating_' . $this->review['review_id']); $this->ratings = is_array($ratings) ? $ratings : array(); // Filter ratings $moderation = $this->preferences_field('preferences_rating_before_appears', true); $this->ratings = RWP_Reviewer::filter_ratings($this->ratings, $moderation); $rating_blacklist = get_post_meta($post_id, 'rwp_rating_blacklist', true); $this->rating_blacklist = is_array($rating_blacklist) ? $rating_blacklist : array(); $likes_blacklist = get_post_meta($post_id, 'rwp_likes_blacklist', true); $this->likes_blacklist = is_array($likes_blacklist) ? $likes_blacklist : array(); $likes = get_post_meta($post_id, 'rwp_likes', true); $this->likes = is_array($likes) ? $likes : array(); // Ratings Scores $this->ratings_scores = RWP_Reviewer::get_ratings_single_scores($this->post_id, $this->review_field('review_id', true), $this->review_field('review_template', true)); // Branch $this->branch = $branch; // Google rich snippets $this->snippets = new RWP_Snippets(); ob_start(); include 'themes/layout.php'; //RWP_Reviewer::pretty_print( $this->ratings ); //RWP_Reviewer::pretty_print( $this->rating_blacklist ); //RWP_Reviewer::pretty_print( $this->likes ); //RWP_Reviewer::pretty_print( $this->likes_blacklist ); //RWP_Reviewer::pretty_print( $this->review ); //RWP_Reviewer::pretty_print( $this->template ); //RWP_Reviewer::pretty_print( $this->preferences ); return ob_get_clean(); }