| 1 |
<?php |
| 2 |
/* |
| 3 |
* License: GPLv3 |
| 4 |
* License URI: https://www.gnu.org/licenses/gpl.txt |
| 5 |
* Copyright 2016-2026 Jean-Sebastien Morisset (https://wpsso.com/) |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
|
| 10 |
die( 'These aren\'t the droids you\'re looking for.' ); |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! class_exists( 'WpssoJsonPropAggregateRating' ) ) { |
| 14 |
|
| 15 |
class WpssoJsonPropAggregateRating { |
| 16 |
|
| 17 |
private $p; // Wpsso class object. |
| 18 |
|
| 19 |
/* |
| 20 |
* Instantiated by Wpsso->init_json_filters(). |
| 21 |
*/ |
| 22 |
public function __construct( &$plugin ) { |
| 23 |
|
| 24 |
$this->p =& $plugin; |
| 25 |
|
| 26 |
if ( $this->p->debug->enabled ) { |
| 27 |
|
| 28 |
$this->p->debug->mark(); |
| 29 |
} |
| 30 |
|
| 31 |
$this->p->util->add_plugin_filters( $this, array( |
| 32 |
'json_data_https_schema_org_thing' => 5, |
| 33 |
), $prio = 10000 ); |
| 34 |
} |
| 35 |
|
| 36 |
/* |
| 37 |
* Automatically include an aggregateRating property based on the Open Graph rating meta tags. |
| 38 |
* |
| 39 |
* $page_type_id is false and $is_main is true when called as part of a collection page part. |
| 40 |
* |
| 41 |
* The Schema standard provides 'aggregateRating' and 'review' properties for these types: |
| 42 |
* |
| 43 |
* Brand |
| 44 |
* CreativeWork |
| 45 |
* Event |
| 46 |
* Offer |
| 47 |
* Organization |
| 48 |
* Place |
| 49 |
* Product |
| 50 |
* Service |
| 51 |
* |
| 52 |
* Unfortunately Google allows the 'aggregateRating' property only for these types: |
| 53 |
* |
| 54 |
* Book |
| 55 |
* Course |
| 56 |
* Event |
| 57 |
* HowTo (includes Recipe) |
| 58 |
* LocalBusiness |
| 59 |
* Movie |
| 60 |
* Product |
| 61 |
* SoftwareApplication |
| 62 |
* |
| 63 |
* And the 'review' property only for these types: |
| 64 |
* |
| 65 |
* Book |
| 66 |
* Course |
| 67 |
* CreativeWorkSeason |
| 68 |
* CreativeWorkSeries |
| 69 |
* Episode |
| 70 |
* Event |
| 71 |
* Game |
| 72 |
* HowTo (includes Recipe) |
| 73 |
* LocalBusiness |
| 74 |
* MediaObject |
| 75 |
* Movie |
| 76 |
* MusicPlaylist |
| 77 |
* MusicRecording |
| 78 |
* Organization |
| 79 |
* Product |
| 80 |
* SoftwareApplication |
| 81 |
*/ |
| 82 |
public function filter_json_data_https_schema_org_thing( $json_data, $mod, $mt_og, $page_type_id, $is_main ) { |
| 83 |
|
| 84 |
if ( $this->p->debug->enabled ) { |
| 85 |
|
| 86 |
$this->p->debug->mark(); |
| 87 |
} |
| 88 |
|
| 89 |
$json_ret = array(); |
| 90 |
|
| 91 |
$aggr_rating = array( |
| 92 |
'ratingValue' => null, |
| 93 |
'ratingCount' => null, |
| 94 |
'worstRating' => 1, |
| 95 |
'bestRating' => 5, |
| 96 |
'reviewCount' => null, |
| 97 |
); |
| 98 |
|
| 99 |
$og_type = isset( $mt_og[ 'og:type' ] ) ? $mt_og[ 'og:type' ] : false; |
| 100 |
|
| 101 |
/* |
| 102 |
* Only pull values from meta tags if this is the main entity markup. |
| 103 |
*/ |
| 104 |
if ( $is_main && $og_type ) { |
| 105 |
|
| 106 |
if ( $this->p->debug->enabled ) { |
| 107 |
|
| 108 |
$this->p->debug->log_arr( 'open graph rating', SucomUtil::preg_grep_keys( '/:rating:/', $mt_og ) ); |
| 109 |
} |
| 110 |
|
| 111 |
WpssoSchema::add_data_itemprop_from_assoc( $aggr_rating, $mt_og, array( |
| 112 |
'ratingValue' => $og_type . ':rating:average', |
| 113 |
'ratingCount' => $og_type . ':rating:count', |
| 114 |
'worstRating' => $og_type . ':rating:worst', |
| 115 |
'bestRating' => $og_type . ':rating:best', |
| 116 |
'reviewCount' => $og_type . ':review:count', |
| 117 |
) ); |
| 118 |
} |
| 119 |
|
| 120 |
$aggr_rating = WpssoSchema::get_schema_type_context( 'https://schema.org/AggregateRating', $aggr_rating ); |
| 121 |
|
| 122 |
$aggr_rating = apply_filters( 'wpsso_json_prop_https_schema_org_aggregaterating', $aggr_rating, $mod, $mt_og, $page_type_id, $is_main ); |
| 123 |
|
| 124 |
if ( $this->p->debug->enabled ) { |
| 125 |
|
| 126 |
$this->p->debug->log_arr( 'aggregate rating', $aggr_rating ); |
| 127 |
} |
| 128 |
|
| 129 |
/* |
| 130 |
* Check for at least two essential meta tags (a rating value, and a rating count or review count). |
| 131 |
* |
| 132 |
* The rating value is expected to be a float and the rating counts are expected to be integers. |
| 133 |
*/ |
| 134 |
if ( ! empty( $aggr_rating[ 'ratingValue' ] ) ) { |
| 135 |
|
| 136 |
if ( ! empty( $aggr_rating[ 'ratingCount' ] ) ) { |
| 137 |
|
| 138 |
if ( empty( $aggr_rating[ 'reviewCount' ] ) ) { // Must be positive if included. |
| 139 |
|
| 140 |
unset( $aggr_rating[ 'reviewCount' ] ); |
| 141 |
} |
| 142 |
|
| 143 |
$json_ret[ 'aggregateRating' ] = $aggr_rating; |
| 144 |
|
| 145 |
} elseif ( ! empty( $aggr_rating[ 'reviewCount' ] ) ) { |
| 146 |
|
| 147 |
if ( empty( $aggr_rating[ 'ratingCount' ] ) ) { // Must be positive if included. |
| 148 |
|
| 149 |
unset( $aggr_rating[ 'ratingCount' ] ); |
| 150 |
} |
| 151 |
|
| 152 |
$json_ret[ 'aggregateRating' ] = $aggr_rating; |
| 153 |
|
| 154 |
} elseif ( $this->p->debug->enabled ) { |
| 155 |
|
| 156 |
$this->p->debug->log( 'aggregate rating ignored: ratingCount and reviewCount are empty' ); |
| 157 |
} |
| 158 |
|
| 159 |
} elseif ( $this->p->debug->enabled ) { |
| 160 |
|
| 161 |
$this->p->debug->log( 'aggregate rating ignored: ratingValue is empty' ); |
| 162 |
} |
| 163 |
|
| 164 |
/* |
| 165 |
* No aggregate rating. |
| 166 |
*/ |
| 167 |
if ( empty( $json_ret[ 'aggregateRating' ] ) && empty( $json_data[ 'aggregateRating' ] ) ) { |
| 168 |
|
| 169 |
if ( $this->p->debug->enabled ) { |
| 170 |
|
| 171 |
$this->p->debug->log( 'no aggregate rating' ); |
| 172 |
} |
| 173 |
|
| 174 |
unset( $json_ret[ 'aggregateRating' ], $json_data[ 'aggregateRating' ] ); |
| 175 |
|
| 176 |
/* |
| 177 |
* Check that aggregate ratings are allowed by Google for this Schema type. |
| 178 |
*/ |
| 179 |
} elseif ( ! $this->p->schema->allow_aggregate_rating( $page_type_id ) ) { |
| 180 |
|
| 181 |
if ( $this->p->debug->enabled ) { |
| 182 |
|
| 183 |
$this->p->debug->log( 'aggregate rating for page type id ' . $page_type_id . ' not allowed' ); |
| 184 |
} |
| 185 |
|
| 186 |
unset( $json_ret[ 'aggregateRating' ], $json_data[ 'aggregateRating' ] ); |
| 187 |
|
| 188 |
if ( $this->p->notice->is_admin_pre_notices() ) { |
| 189 |
|
| 190 |
$page_type_url = $this->p->schema->get_schema_type_url( $page_type_id ); |
| 191 |
|
| 192 |
$notice_msg = sprintf( __( 'An aggregate rating value for this markup has been ignored - <a href="%1$s">Google does not allow an aggregate rating value for the Schema %2$s type</a>.', 'wpsso' ), 'https://developers.google.com/search/docs/data-types/review-snippet', $page_type_url ); |
| 193 |
|
| 194 |
$this->p->notice->warn( $notice_msg ); |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
} elseif ( ! empty( $json_data[ 'url' ] ) ) { // Just in case. |
| 199 |
|
| 200 |
$id_suffix = array( $page_type_id ); |
| 201 |
|
| 202 |
if ( ! empty( $mod[ 'id' ] ) ) $id_suffix[] = $mod[ 'id' ]; // Just in case. |
| 203 |
|
| 204 |
$id_suffix[] = 'rating.aggregate'; |
| 205 |
|
| 206 |
foreach ( SucomUtil::preg_grep_keys( '/^[a-z]/', $json_ret[ 'aggregateRating' ] ) as $val ) $id_suffix[] = $val; |
| 207 |
|
| 208 |
WpssoSchema::update_data_id( $json_ret[ 'aggregateRating' ], $id_suffix, $json_data[ 'url' ] ); |
| 209 |
} |
| 210 |
|
| 211 |
return WpssoSchema::return_data_from_filter( $json_data, $json_ret, $is_main ); |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|