Ejemplo n.º 1
0
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>
*/
if (!defined('ABSPATH')) {
    exit('You\'re not allowed to see this page');
}
// Exit if accessed directly
$post_id = get_the_ID();
$overall_rating = yasr_get_overall_rating($post_id);
if (!$overall_rating) {
    $overall_rating = "-1";
}
//This is for the select
if ($overall_rating != '-1') {
    $overall_rating_array = explode(".", $overall_rating);
    $int = $overall_rating_array[0];
    if ($int != 5) {
        $dec = $overall_rating_array[1];
    } else {
        $dec = 0;
    }
} else {
    $int = 0;
    $dec = 0;
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;
    }
}
Ejemplo n.º 3
0
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;
        }
    }
    //if is not jsonld (default case)
    if (YASR_SCHEMA_FORMAT != 'jsonld') {
        $schema = NULL;
        //To avoid undefined variable notice outside the loop
        $review_choosen = yasr_get_snippet_type();
        $schema = apply_filters('yasr_filter_schema_microdata', $review_choosen);
        if ($schema) {
            return $content . $schema;
        }
        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;
                    $div = "<div class=\"yasr_schema\" itemscope itemtype=\"http://schema.org/Review\">";
                    $author = "<span itemprop=\"author\" itemscope itemtype=\"http://schema.org/Person\"> " . __('reviewed by', 'yet-another-stars-rating') . " <span itemprop=\"name\"> " . get_the_author() . " </span></span>";
                    $date = __(' on ', 'yet-another-stars-rating') . "<meta itemprop=\"datePublished\" content=\"" . get_the_date('c') . "\"> " . get_the_date();
                    $title = NULL;
                    //avoid undefined
                    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 == "Product") {
                        $title = "<span itemprop=\"itemReviewed\" itemscope itemtype=\"http://schema.org/Product\">  <span itemprop=\"name\"> " . get_the_title() . " </span></span>";
                    } elseif ($review_choosen == "Recipe") {
                        $title = "<span itemprop=\"itemReviewed\" itemscope itemtype=\"http://schema.org/Recipe\">  <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 headline\"> " . get_the_title() . " </span>";
                        $date = __('reviewed on', 'yet-another-stars-rating') . " <meta itemprop=\"datePublished\" content=\"" . get_the_date('c') . "\"> " . get_the_date() . " </span>";
                        $author = __('by ', 'yet-another-stars-rating') . "<span itemprop=\"author\" itemscope itemtype=\"http://schema.org/Person\"><span itemprop=\"name\"> " . get_the_author() . " </span></span>";
                    }
                    $rating = "<span itemprop=\"reviewRating\" itemscope itemtype=\"http://schema.org/Rating\"> " . __('rated', 'yet-another-stars-rating') . " <span itemprop=\"ratingValue\">" . $overall_rating . "</span> " . __('of', 'yet-another-stars-rating') . " <span itemprop=\"bestRating\"> 5 </span></span>";
                    $end_div = "</div>";
                    $schema = $div . $title . $author . $date . $rating . $end_div;
                    if ($review_choosen == "Other") {
                        $schema = $div . $title . $date . $author . $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\"> <span itemprop=\"name\">" . get_the_title() . "</span>";
                    $date = '';
                } elseif ($review_choosen == "Product") {
                    $div_1 = "<div class=\"yasr_schema\" itemscope itemtype=\"http://schema.org/Product\"><span itemprop=\"name\">" . get_the_title() . "</span>";
                    $date = '';
                } elseif ($review_choosen == "Recipe") {
                    $div_1 = "<div class=\"yasr_schema\" itemscope itemtype=\"http://schema.org/Recipe\">  <span itemprop=\"name\"> " . get_the_title() . " </span>";
                    $date = '';
                } elseif ($review_choosen == 'Other') {
                    $div_1 = "<div class=\"yasr_schema\" itemscope itemType=\"http://schema.org/BlogPosting\"><span itemprop=\"name headline\">" . get_the_title() . "</span>";
                    $date = __(' on ', 'yet-another-stars-rating') . "<meta itemprop=\"datePublished\" content=\"" . get_the_date('c') . "\"> " . get_the_date();
                }
                $author = __(' written by ', 'yet-another-stars-rating') . get_the_author();
                $span_1 = "<span itemprop=\"aggregateRating\" itemscope itemtype=\"http://schema.org/AggregateRating\">";
                $rating = __(' average rating ', 'yet-another-stars-rating') . "<span itemprop=\"ratingValue\">" . $average_rating . "</span>/<span itemprop=\"bestRating\">5</span>";
                $n_ratings = " - <span itemprop=\"ratingCount\"> " . $visitor_rating['votes_number'] . "</span>" . __(' user ratings', 'yet-another-stars-rating');
                $end_span_1 = "</span>";
                $end_div_1 = "</div>";
                $schema = $div_1 . $author . $date . $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;
        }
    } else {
        $script_type = '<script type="application/ld+json">';
        $end_script_type = '</script>';
        $review_choosen = yasr_get_snippet_type();
        $rich_snippet["@context"] = "http://schema.org/";
        $schema = apply_filters('yasr_filter_schema_jsonld', $review_choosen);
        if ($schema) {
            return $content . $script_type . $schema . $end_script_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;
                    $author = get_the_author();
                    $review_name = get_the_title();
                    $date = get_the_date('c');
                    //name
                    $rich_snippet["name"] = $review_name;
                    $rich_snippet["Review"] = array("@type" => "Review", "name" => "{$review_name}", "author" => array("@type" => "Person", "name" => "{$author}"), "datePublished" => "{$date}", "reviewRating" => array("@type" => "Rating", "ratingValue" => "{$overall_rating}"));
                    if ($review_choosen == "Product") {
                        $rich_snippet["@type"] = "Product";
                    } elseif ($review_choosen == "Recipe") {
                        $rich_snippet["@type"] = "Recipe";
                    } elseif ($review_choosen == "Place") {
                        $rich_snippet["@type"] = "LocalBusiness";
                    } elseif ($review_choosen == "Other") {
                        $rich_snippet["@type"] = "BlogPosting";
                        $rich_snippet["datePublished"] = $date;
                        $rich_snippet["headline"] = $review_name;
                        $rich_snippet["image"] = wp_get_attachment_url(get_post_thumbnail_id());
                    }
                }
            }
            //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);
                $author = get_the_author();
                $review_name = get_the_title();
                $date = get_the_date('c');
                //name
                $rich_snippet["name"] = $review_name;
                $rich_snippet["aggregateRating"] = array("@type" => "AggregateRating", "ratingValue" => "{$average_rating}", "ratingCount" => $visitor_rating['votes_number']);
                if ($review_choosen == "Product") {
                    $rich_snippet["@type"] = "Product";
                } elseif ($review_choosen == "Place") {
                    $rich_snippet["@type"] = "LocalBusiness";
                } elseif ($review_choosen == "Recipe") {
                    $rich_snippet["@type"] = "Recipe";
                } elseif ($review_choosen == "Other") {
                    $rich_snippet["@type"] = "BlogPosting";
                    $rich_snippet["datePublished"] = $date;
                    $rich_snippet["headline"] = $review_name;
                    $rich_snippet["image"] = wp_get_attachment_url(get_post_thumbnail_id());
                }
            }
        }
        if (is_singular() && is_main_query() && !is_404()) {
            return $content . $script_type . json_encode($rich_snippet) . $end_script_type;
        } else {
            return $content;
        }
    }
}
function shortcode_overall_rating_callback($atts)
{
    extract(shortcode_atts(array('size' => 'large', 'postid' => FALSE), $atts));
    if (!$postid) {
        $overall_rating = yasr_get_overall_rating();
    } else {
        $overall_rating = yasr_get_overall_rating($postid);
    }
    if (!$overall_rating) {
        $overall_rating = "-1";
    }
    $shortcode_html = '';
    if (YASR_TEXT_BEFORE_STARS == 1 && YASR_TEXT_BEFORE_OVERALL != '') {
        $text_before_star = str_replace('%overall_rating%', $overall_rating, YASR_TEXT_BEFORE_OVERALL);
        $shortcode_html = "<div class=\"yasr-container-custom-text-and-overall\">\n                                <span id=\"yasr-custom-text-before-overall\">" . $text_before_star . "</span>";
    }
    $stars_attribute = yasr_stars_size($size);
    $shortcode_html .= "<div class=\"{$stars_attribute['class']}\" id=\"yasr_rateit_overall\" data-rateit-starwidth=\"{$stars_attribute['px_size']}\" data-rateit-starheight=\"{$stars_attribute['px_size']}\" data-rateit-value=\"{$overall_rating}\" data-rateit-step=\"0.1\" data-rateit-resetable=\"false\" data-rateit-readonly=\"true\"></div>";
    if (YASR_TEXT_BEFORE_STARS == 1 && YASR_TEXT_BEFORE_OVERALL != '') {
        $shortcode_html .= "</div>";
    }
    //IF show overall rating in loop is disabled use is_singular && is_main query
    if (YASR_SHOW_OVERALL_IN_LOOP === 'disabled') {
        if (is_singular() && is_main_query()) {
            return $shortcode_html;
        }
    } elseif (YASR_SHOW_OVERALL_IN_LOOP === 'enabled') {
        return $shortcode_html;
    }
}