function yasr_insert_overall_rating_callback()
{
    if (isset($_POST['rating']) && $_POST['post_id'] && $_POST['post_id'] != '') {
        $rating = $_POST['rating'];
        $post_id = $_POST['post_id'];
        $nonce = $_POST['nonce'];
    } else {
        exit;
    }
    if (!current_user_can('publish_posts')) {
        wp_die(__('You do not have sufficient permissions to access this page.', 'yet-another-stars-rating'));
    }
    if (!wp_verify_nonce($nonce, 'yasr_nonce_insert_overall_rating')) {
        die('Security check');
    }
    if ($rating > 5) {
        $rating = 5;
    }
    global $wpdb;
    //If update works means that visitor already rated this post
    $update_result = $wpdb->update(YASR_VOTES_TABLE, array('overall_rating' => $rating), array('post_id' => $post_id), array('%s'));
    //If update result fails this is a new post or post has no visitor ratings
    if (!$update_result) {
        $replace_result = $wpdb->replace(YASR_VOTES_TABLE, array('post_id' => $post_id, 'overall_rating' => $rating, 'review_type' => YASR_ITEMTYPE), array('%d', '%s', '%s'));
        $snippet_type = yasr_get_snippet_type();
        //If there is not sinppet type, can happen when an user choose the snippet but doesn't use overall rating
        if (!$snippet_type) {
            $wpdb->replace(YASR_VOTES_TABLE, array('review_type' => YASR_ITEMTYPE), array('%s'));
        }
    }
    // End if(!$update_result)
    if ($update_result || $replace_result) {
        if ($rating != '-1') {
            $text = __("You've rated it ", "yet-another-stars-rating");
            echo $text . $rating;
        } else {
            $text = __("You've reset the vote", "yet-another-stars-rating");
            echo $text;
        }
    }
    die;
    // this is required to return a proper result
}
function yasr_add_schema($content)
{
    //Add buddypress compatibility
    if (function_exists('bp_is_active')) {
        //Return content only if is page. This will disable schema for all page.
        //Dunno why, but if I try to return $content after if (YASR_SNIPPET == 'overall_rating')
        //or (YASR_SNIPPET == 'visitor_rating') $content will have only wp content, losing the buddypress one
        if (is_page()) {
            return $content;
        }
    }
    $schema = NULL;
    //To avoid undefined variable notice outside the loop
    $review_choosen = yasr_get_snippet_type();
    if (YASR_SNIPPET == 'overall_rating') {
        $overall_rating = yasr_get_overall_rating();
        if ($overall_rating && $overall_rating != '-1' && $overall_rating != '0.0') {
            if (is_singular() && is_main_query()) {
                global $post;
                if ($review_choosen == "Place") {
                    $title = "<span itemprop=\"itemReviewed\" itemscope itemtype=\"http://schema.org/LocalBusiness\">  <span itemprop=\"name\">" . get_the_title() . "</span></span>";
                } elseif ($review_choosen == "Other") {
                    $title = "<span itemprop=\"itemReviewed\" itemscope itemType=\"http://schema.org/BlogPosting\">  <span itemprop=\"name\">" . get_the_title() . "</span></span>";
                } else {
                    $title = "<span itemprop=\"itemReviewed\" itemscope itemtype=\"http://schema.org/Product\">  <span itemprop=\"name\">" . get_the_title() . "</span></span>";
                }
                $div = "<div class=\"yasr_schema\" itemscope itemtype=\"http://schema.org/Review\">";
                $author = "<span itemprop=\"author\" itemscope itemtype=\"http://schema.org/Person\">" . __(' reviewed by ', 'yasr') . "<span itemprop=\"name\">" . get_the_author() . "</span></span>";
                $date = __(' on ', 'yasr') . "<meta itemprop=\"datePublished\" content=\"" . get_the_date('c') . "\"> " . get_the_date();
                $rating = "<span itemprop=\"reviewRating\" itemscope itemtype=\"http://schema.org/Rating\"> " . __(' rated ', 'yasr') . "<span itemprop=\"ratingValue\">" . $overall_rating . "</span>" . __(' of', 'yasr') . " <span itemprop=\"bestRating\">5</span></span>";
                $end_div = "</div>";
                $schema = $div . $title . $author . $date . $rating . $end_div;
            }
        }
        //END id if $overall_rating != '-1'
    }
    //end if ($choosen_snippet['snippet'] == 'overall_rating')
    if (YASR_SNIPPET == 'visitor_rating') {
        $visitor_votes = yasr_get_visitor_votes();
        if ($visitor_votes) {
            foreach ($visitor_votes as $rating) {
                $visitor_rating['votes_number'] = $rating->number_of_votes;
                $visitor_rating['sum'] = $rating->sum_votes;
            }
        } else {
            $visitor_rating = NULL;
        }
        if ($visitor_rating['sum'] != 0 && $visitor_rating['votes_number'] != 0) {
            $average_rating = $visitor_rating['sum'] / $visitor_rating['votes_number'];
            $average_rating = round($average_rating, 1);
            if ($review_choosen == 'Place') {
                $div_1 = "<div class=\"yasr_schema\" itemscope itemtype=\"http://schema.org/LocalBusiness\">";
            }
            if ($review_choosen == 'Other') {
                $div_1 = "<div class=\"yasr_schema\" itemscope itemType=\"http://schema.org/BlogPosting\">";
            } else {
                $div_1 = "<div class=\"yasr_schema\" itemscope itemtype=\"http://schema.org/Product\">";
            }
            $title = "<span itemprop=\"name\">" . get_the_title() . "</span>";
            $author = __(' written by ', 'yasr') . get_the_author();
            $span_1 = "<span itemprop=\"aggregateRating\" itemscope itemtype=\"http://schema.org/AggregateRating\">";
            $rating = __(' average rating ', 'yasr') . "<span itemprop=\"ratingValue\">" . $average_rating . "</span>/<span itemprop=\"bestRating\">5</span>";
            $n_ratings = " - <span itemprop=\"ratingCount\"> " . $visitor_rating['votes_number'] . "</span>" . __(' user ratings', 'yasr');
            $end_span_1 = "</span>";
            $end_div_1 = "</div>";
            $schema = $div_1 . $title . $author . $span_1 . $rating . $n_ratings . $end_span_1 . $end_div_1;
        }
    }
    if (is_singular() && is_main_query() && !is_404()) {
        return $content . $schema;
    } else {
        return $content;
    }
}
//End if YASR_METABOX_OVERALL_RATING == 'numbers'
?>


    <hr>

    <div class="yasr-choose-reviews-types"><?php 
_e("This review is about a...");
?>
        <br />

        <?php 
$i18n_array_review = __('Product, Place, Other', 'yasr');
$array_review_type = explode(',', $i18n_array_review);
$review_type = array_map('trim', $array_review_type);
$review_type_choosen = yasr_get_snippet_type();
?>

        <select id="yasr-choose-reviews-types-list">

            <?php 
foreach ($review_type as $type) {
    if ($type == $review_type_choosen) {
        echo "<option value=\"{$type}\" selected>{$type}</option>";
    } else {
        echo "<option value=\"{$type}\">{$type}</option>";
    }
}
?>

        </select>
Example #4
0
function yasr_select_itemtype()
{
    $i18n_array_review = __('Product, Place, Recipe, BlogPosting', 'yet-another-stars-rating');
    $array_review_type = explode(',', $i18n_array_review);
    $review_type = array_map('trim', $array_review_type);
    $review_type_choosen = yasr_get_snippet_type();
    switch ($review_type_choosen) {
        case 'Product':
            $review_type_choosen = 1;
            break;
        case 'Place':
            $review_type_choosen = 2;
            break;
        case 'Recipe':
            $review_type_choosen = 3;
            break;
        case 'Other':
            $review_type_choosen = 4;
            break;
    }
    ?>

        <select id="yasr-choose-reviews-types-list"> 

            <?php 
    $i = 1;
    foreach ($review_type as $type) {
        if ($i == $review_type_choosen) {
            echo "<option value=\"{$i}\" selected>{$type}</option>";
        } else {
            echo "<option value=\"{$i}\">{$type}</option>";
        }
        $i = $i + 1;
    }
    ?>

        </select>

        <?php 
}