コード例 #1
0
function yasr_visitor_votes_readonly_callback($atts)
{
    $shortcode_html = NULL;
    //Avoid undefined variable outside is_singular && is_main_query
    extract(shortcode_atts(array('size' => 'small', 'postid' => FALSE), $atts));
    //If it's not specified use get_the_id
    if (!$postid) {
        $post_id = get_the_ID();
    } else {
        $post_id = $postid;
    }
    $votes = yasr_get_visitor_votes($post_id);
    $medium_rating = 0;
    //Avoid undefined variable
    if (!$votes) {
        $votes = 0;
        //Avoid undefined variable if there is not overall rating
        $votes_number = 0;
        //Avoid undefined variable
    } else {
        foreach ($votes as $user_votes) {
            $votes_number = $user_votes->number_of_votes;
            if ($votes_number != 0) {
                $medium_rating = $user_votes->sum_votes / $votes_number;
            } else {
                $medium_rating = 0;
            }
        }
    }
    $medium_rating = round($medium_rating, 1);
    $stars_attribute = yasr_stars_size($size);
    $shortcode_html = "<div id=\"yasr_visitor_votes_readonly_{$post_id}\" class=\"yasr-visitor-votes_readonly\">";
    $span_after_rate_it = "";
    $shortcode_html .= "<div class=\"{$stars_attribute['class']}\" id=\"yasr_rateit_visitor_votes_readonly_{$post_id}\" data-rateit-starwidth=\"{$stars_attribute['px_size']}\" data-rateit-starheight=\"{$stars_attribute['px_size']}\" data-rateit-value=\"{$medium_rating}\" data-rateit-step=\"1\" data-rateit-resetable=\"false\" data-rateit-readonly=\"true\"></div>";
    $shortcode_html .= "</div>";
    //IF show visitor votes in loop is disabled use is_singular && is_main query
    if (YASR_SHOW_VISITOR_VOTES_IN_LOOP === 'disabled') {
        if (is_singular() && is_main_query()) {
            return $shortcode_html;
        }
    } elseif (YASR_SHOW_VISITOR_VOTES_IN_LOOP === 'enabled') {
        return $shortcode_html;
    }
    // } //End (!is_feed)
}
コード例 #2
0
ファイル: WPJsonLD.class.php プロジェクト: bmhm/wp-jsonld
 public function create_jsonld_blogposting()
 {
     $markup = $this->createBlogPosting();
     $markup->addContext();
     $markup->author = $this->createAuthorEntity();
     $markup->publisher = $this->createOrganization();
     $markup->image = $this->createImage();
     $markup->mainEntityOfPage = $this->createMainEntity($markup->{'@type'}, $markup->{'@id'});
     // create rating if yasr is installed.
     if (function_exists("yasr_get_visitor_votes")) {
         $visitorVotes = yasr_get_visitor_votes();
         if ($visitorVotes) {
             $markup->aggregateRating = $this->createRating();
         }
     }
     $scriptcontents = json_encode($markup, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
     return $scriptcontents;
 }
コード例 #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;
        }
    }
    $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;
    }
}
コード例 #4
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;
        }
    }
}