| 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( 'WpssoJsonPropReview' ) ) { |
| 14 |
|
| 15 |
class WpssoJsonPropReview { |
| 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 = 20000 ); |
| 34 |
} |
| 35 |
|
| 36 |
/* |
| 37 |
* Automatically include a review property based on the Open Graph review 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 |
$all_reviews = array(); |
| 91 |
$og_type = isset( $mt_og[ 'og:type' ] ) ? $mt_og[ 'og:type' ] : false; |
| 92 |
|
| 93 |
/* |
| 94 |
* Move any existing properties (from shortcodes, for example) so we can filter them and add new ones. |
| 95 |
*/ |
| 96 |
if ( isset( $json_data[ 'review' ] ) ) { |
| 97 |
|
| 98 |
if ( isset( $json_data[ 'review' ][ 0 ] ) ) { // Has an array of types. |
| 99 |
|
| 100 |
$all_reviews = $json_data[ 'review' ]; |
| 101 |
|
| 102 |
} elseif ( ! empty( $json_data[ 'review' ] ) ) { |
| 103 |
|
| 104 |
$all_reviews[] = $json_data[ 'review' ]; // Markup for a single type. |
| 105 |
} |
| 106 |
|
| 107 |
unset( $json_data[ 'review' ] ); |
| 108 |
} |
| 109 |
|
| 110 |
/* |
| 111 |
* Only pull values from meta tags if this is the main entity markup. |
| 112 |
*/ |
| 113 |
if ( $is_main && $og_type ) { |
| 114 |
|
| 115 |
if ( ! empty( $mt_og[ $og_type . ':reviews' ] ) && is_array( $mt_og[ $og_type . ':reviews' ] ) ) { |
| 116 |
|
| 117 |
if ( $this->p->debug->enabled ) { |
| 118 |
|
| 119 |
$this->p->debug->log( 'adding ' . count( $mt_og[ $og_type . ':reviews' ] ) . ' product reviews from mt_og' ); |
| 120 |
} |
| 121 |
|
| 122 |
foreach ( $mt_og[ $og_type . ':reviews' ] as $mt_review ) { |
| 123 |
|
| 124 |
$single_review = WpssoSchema::get_data_itemprop_from_assoc( $mt_review, array( |
| 125 |
'url' => 'review:url', |
| 126 |
'dateCreated' => 'review:created_time', |
| 127 |
'dateModified' => 'review:updated_time', |
| 128 |
'name' => 'review:title', |
| 129 |
'description' => 'review:description', |
| 130 |
'text' => 'review:text', |
| 131 |
) ); |
| 132 |
|
| 133 |
if ( ! empty( $mt_review[ 'review:rating:value' ] ) ) { |
| 134 |
|
| 135 |
$single_review[ 'reviewRating' ] = WpssoSchema::get_schema_type_context( 'https://schema.org/Rating', |
| 136 |
WpssoSchema::get_data_itemprop_from_assoc( $mt_review, array( |
| 137 |
'ratingValue' => 'review:rating:value', |
| 138 |
'worstRating' => 'review:rating:worst', |
| 139 |
'bestRating' => 'review:rating:best', |
| 140 |
) ) |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
if ( ! empty( $mt_review[ 'review:author:name' ] ) ) { |
| 145 |
|
| 146 |
$single_review[ 'author' ] = WpssoSchema::get_schema_type_context( 'https://schema.org/Person', |
| 147 |
WpssoSchema::get_data_itemprop_from_assoc( $mt_review, array( |
| 148 |
'url' => 'review:author:url', |
| 149 |
'name' => 'review:author:name', |
| 150 |
) ) |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
if ( ! empty( $mt_review[ 'review:image' ] ) ) { |
| 155 |
|
| 156 |
WpssoSchema::add_images_data_mt( $single_review[ 'image' ], $mt_review[ 'review:image' ] ); |
| 157 |
} |
| 158 |
|
| 159 |
if ( ! empty( $mt_review[ 'review:video' ] ) ) { |
| 160 |
|
| 161 |
WpssoSchema::add_videos_data_mt( $single_review[ 'video' ], $mt_review[ 'review:video' ] ); |
| 162 |
} |
| 163 |
|
| 164 |
/* |
| 165 |
* If we have a comment ID then add any replies to the comment. |
| 166 |
*/ |
| 167 |
if ( ! empty( $mt_review[ 'review:id' ] ) ) { |
| 168 |
|
| 169 |
$replies_added = WpssoSchemaSingle::add_comment_reply_data( $single_review[ 'comment' ], |
| 170 |
$mod, $mt_review[ 'review:id' ] ); |
| 171 |
|
| 172 |
if ( ! $replies_added ) { |
| 173 |
|
| 174 |
unset( $single_review[ 'comment' ] ); |
| 175 |
} |
| 176 |
|
| 177 |
/* |
| 178 |
* Update the @id string based on $json_ret[ 'url' ], $type_id, and $comment_id values. |
| 179 |
*/ |
| 180 |
WpssoSchema::update_data_id( $single_review, array( $type_id = 'review', $mt_review[ 'review:id' ] ) ); |
| 181 |
} |
| 182 |
|
| 183 |
/* |
| 184 |
* Add the complete review. |
| 185 |
*/ |
| 186 |
$all_reviews[] = WpssoSchema::get_schema_type_context( 'https://schema.org/Review', $single_review ); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
$json_ret[ 'review' ] = apply_filters( 'wpsso_json_prop_https_schema_org_review', $all_reviews, $mod, $mt_og, $page_type_id, $is_main ); |
| 192 |
|
| 193 |
/* |
| 194 |
* Return if nothing to do. |
| 195 |
*/ |
| 196 |
if ( empty( $json_ret[ 'review' ] ) && empty( $json_data[ 'review' ] ) ) { |
| 197 |
|
| 198 |
if ( $this->p->debug->enabled ) { |
| 199 |
|
| 200 |
$this->p->debug->log( 'exiting early: no reviews' ); |
| 201 |
} |
| 202 |
|
| 203 |
unset( $json_ret[ 'review' ], $json_data[ 'review' ] ); |
| 204 |
|
| 205 |
return WpssoSchema::return_data_from_filter( $json_data, $json_ret, $is_main ); |
| 206 |
} |
| 207 |
|
| 208 |
/* |
| 209 |
* Make sure reviews are allowed by Google for this Schema type. |
| 210 |
*/ |
| 211 |
if ( ! $this->p->schema->allow_review( $page_type_id ) ) { |
| 212 |
|
| 213 |
if ( $this->p->debug->enabled ) { |
| 214 |
|
| 215 |
$this->p->debug->log( 'cannot add review to page type id ' . $page_type_id ); |
| 216 |
} |
| 217 |
|
| 218 |
unset( $json_ret[ 'review' ], $json_data[ 'review' ] ); |
| 219 |
|
| 220 |
if ( $this->p->notice->is_admin_pre_notices() ) { |
| 221 |
|
| 222 |
$page_type_url = $this->p->schema->get_schema_type_url( $page_type_id ); |
| 223 |
|
| 224 |
$notice_msg = sprintf( __( 'Reviews for this markup have been ignored - <a href="%1$s">Google does not allow reviews for the Schema %2$s type</a>.', 'wpsso' ), 'https://developers.google.com/search/docs/data-types/review-snippet', $page_type_url ); |
| 225 |
|
| 226 |
$this->p->notice->warn( $notice_msg ); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
return WpssoSchema::return_data_from_filter( $json_data, $json_ret, $is_main ); |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|