Advertisement
Dudo1985

Untitled

Nov 4th, 2016
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.27 KB | None | 0 0
  1. remove_filter('the_content', 'yasr_add_schema');
  2.  
  3. add_filter('the_content', 'yasr_add_schema_archives');
  4.  
  5.     function yasr_add_schema_archives($content) {
  6.  
  7.         //Add buddypress compatibility
  8.         if (function_exists('bp_is_active')) {
  9.  
  10.             //Return content only if is page. This will disable schema for all page.
  11.             //If I try to return $content after if (YASR_SNIPPET == 'overall_rating')
  12.             //or (YASR_SNIPPET == 'visitor_rating') $content will have only wp content, losing the buddypress one
  13.             if (is_page()) {
  14.  
  15.                 return $content;
  16.  
  17.             }
  18.  
  19.         }
  20.  
  21.         $script_type = '<script type="application/ld+json">';
  22.  
  23.         $end_script_type = '</script>';
  24.  
  25.         $review_choosen = yasr_get_snippet_type();
  26.  
  27.         $rich_snippet["@context"] = "http://schema.org/";
  28.  
  29.         //if doesn't exists a filter for yasr_filter_schema_jsonld $review_chosen value is assigned to $filtered_schema...
  30.         $filtered_schema = apply_filters( 'yasr_filter_schema_jsonld', $review_choosen );
  31.  
  32.         //So check here if $schema != $review_choosen
  33.         if ($filtered_schema !== $review_choosen) {
  34.  
  35.             return $content . $script_type . $filtered_schema . $end_script_type;
  36.  
  37.         }
  38.  
  39.         $author = get_the_author();
  40.  
  41.         $review_name = get_the_title();
  42.  
  43.         $date = get_the_date('c');
  44.  
  45.         $date_modified = get_the_modified_date('c');
  46.  
  47.         $post_image_url = ''; //avoid undefined
  48.         $logo_image_url = ''; //avoid undefined
  49.  
  50.         if (defined('YASR_BLOGPOSTING_ORGANIZATION_LOGO')) {
  51.  
  52.             $logo_image_url = YASR_BLOGPOSTING_ORGANIZATION_LOGO;
  53.  
  54.             $post_image_url =  $logo_image_url; //this will be overwritten if has_post_thumbnail is true
  55.  
  56.             $logo_image_url_absolute = $_SERVER['DOCUMENT_ROOT'] . parse_url(YASR_BLOGPOSTING_ORGANIZATION_LOGO, PHP_URL_PATH);
  57.  
  58.             $post_image_size = @getimagesize($logo_image_url_absolute);  //the @ should be useless, just to be safe
  59.  
  60.             $logo_image_size = @getimagesize($logo_image_url_absolute);  //the @ should be useless, just to be safe
  61.  
  62.         }
  63.  
  64.         else {
  65.  
  66.             $post_image_size[0] = 0;
  67.             $post_image_size[1] = 0;
  68.  
  69.             $logo_image_size[0] = 0;
  70.             $logo_image_size[1] = 0;
  71.  
  72.         }
  73.  
  74.         //if exists featuread image get the url and overwrite the variable
  75.         if (has_post_thumbnail() ) {
  76.  
  77.             $post_image_url =  wp_get_attachment_url(get_post_thumbnail_id());
  78.  
  79.             $post_image_url_absolute = $_SERVER['DOCUMENT_ROOT'] . parse_url($post_image_url, PHP_URL_PATH);
  80.  
  81.             $post_image_size = @getimagesize($post_image_url_absolute);  //the @ should be useless, just to be safe
  82.  
  83.         }
  84.  
  85.  
  86.         if ($review_choosen == "Product") {
  87.  
  88.             $rich_snippet["@type"]="Product";
  89.  
  90.         }
  91.  
  92.         elseif ($review_choosen == "Recipe") {
  93.  
  94.             $rich_snippet["@type"]="Recipe";
  95.  
  96.             $rich_snippet["image"] = array(
  97.                 "@type" => "ImageObject",
  98.                 "url" => $post_image_url,
  99.                 "width" => $post_image_size[0],
  100.                 "height" => $post_image_size[1]
  101.             );
  102.  
  103.         }
  104.  
  105.         elseif ($review_choosen == "Place") {
  106.  
  107.             $rich_snippet["@type"]="LocalBusiness";
  108.  
  109.         }
  110.  
  111.         elseif ($review_choosen == "Other") {
  112.  
  113.             $rich_snippet["@type"] = "BlogPosting";
  114.  
  115.             $rich_snippet["datePublished"] = $date;
  116.  
  117.             $rich_snippet["headline"] = $review_name;
  118.  
  119.             $rich_snippet["mainEntityOfPage"]  = array(
  120.                 "@type" => "WebPage",
  121.                 "@id" => get_permalink()
  122.             );
  123.  
  124.  
  125.             $rich_snippet["author"] = array(
  126.                 "@type" => "Person",
  127.                 "name" => "$author"
  128.             );
  129.  
  130.             $rich_snippet["publisher"] = array(
  131.                 "@type" => "Organization",
  132.                 "name" => YASR_BLOGPOSTING_ORGANIZATION_NAME,
  133.                 "logo" => array(
  134.                     "@type" => "ImageObject",
  135.                     "url" =>  $logo_image_url,
  136.                     "width" => $logo_image_size[0],
  137.                     "height" => $logo_image_size[1]
  138.                 )
  139.  
  140.             );
  141.  
  142.             $rich_snippet["dateModified"] =  $date_modified;
  143.  
  144.             $rich_snippet["image"] = array(
  145.                 "@type" => "ImageObject",
  146.                 "url" => $post_image_url,
  147.                 "width" => $post_image_size[0],
  148.                 "height" => $post_image_size[1]
  149.             );
  150.  
  151.         }
  152.  
  153.         if (YASR_SNIPPET == 'overall_rating') {
  154.  
  155.             $overall_rating=yasr_get_overall_rating(FALSE, FALSE);
  156.  
  157.             if($overall_rating && $overall_rating != '-1' && $overall_rating != '0.0') {
  158.  
  159.                     global $post;
  160.  
  161.                     //name
  162.                     $rich_snippet["name"] = $review_name;
  163.  
  164.                     $rich_snippet["Review"] = array (
  165.                         "@type" => "Review",
  166.                         "name" => "$review_name",
  167.                         "author" => array(
  168.                             "@type" => "Person",
  169.                             "name" => "$author"
  170.                         ),
  171.                         "datePublished" => "$date",
  172.                         "reviewRating" => array(
  173.                             "@type" => "Rating",
  174.                             "ratingValue" => "$overall_rating",
  175.                         ),
  176.                     );
  177.  
  178.             } //END id if $overall_rating != '-1'
  179.  
  180.             else {
  181.  
  182.                 return $content;
  183.  
  184.             }
  185.  
  186.         }  //end if ($choosen_snippet['snippet'] == 'overall_rating')
  187.  
  188.         if (YASR_SNIPPET == 'visitor_rating') {
  189.  
  190.             $visitor_votes = yasr_get_visitor_votes (4336, FALSE);
  191.  
  192.             if ($visitor_votes) {
  193.  
  194.                 foreach ($visitor_votes as $rating) {
  195.                     $visitor_rating['votes_number']=$rating->number_of_votes;
  196.                     $visitor_rating['sum']=$rating->sum_votes;
  197.                 }
  198.  
  199.             }
  200.  
  201.             else {
  202.  
  203.                 return $content;
  204.  
  205.             }
  206.  
  207.             if ($visitor_rating['sum'] != 0 && $visitor_rating['votes_number'] != 0) {
  208.  
  209.                 $average_rating = $visitor_rating['sum'] / $visitor_rating['votes_number'];
  210.  
  211.                 $average_rating = round($average_rating, 1);
  212.  
  213.                 //name
  214.                 $rich_snippet["name"] = $review_name;
  215.  
  216.                 $rich_snippet["aggregateRating"] = array (
  217.                     "@type" => "AggregateRating",
  218.                     "ratingValue" => "$average_rating",
  219.                     "ratingCount" => $visitor_rating['votes_number'],
  220.                 );
  221.  
  222.             }
  223.  
  224.             else {
  225.  
  226.                 return $content;
  227.  
  228.             }
  229.  
  230.         }
  231.  
  232.         if ( !is_404() ) {
  233.             return $content . $script_type . json_encode($rich_snippet) . $end_script_type;
  234.         }
  235.  
  236.         else {
  237.             return $content;
  238.         }
  239.  
  240.  
  241.  
  242.     } //End function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement