PluginProbe
WPSSO Core – Complete Schema Markup and Meta Tags / 22.7.0
WPSSO Core – Complete Schema Markup and Meta Tags v22.7.0
22.7.0 22.6.1 22.6.0 22.5.3 22.5.2 22.5.1 22.4.0 22.3.0 22.2.1 22.2.0 22.1.3 22.1.2 22.1.1 22.1.0 22.0.0 21.13.3 21.13.2 trunk 21.13.1
wpsso / lib / integ / review / yotpowc.php

yotpowc.php in WPSSO Core – Complete Schema Markup and Meta Tags 22.7.0, at lib/integ/review/yotpowc.php

345 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * License: GPLv3
4 * License URI: https://www.gnu.org/licenses/gpl.txt
5 * Copyright 2012-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( 'WpssoIntegReviewYotpoWc' ) ) {
14
15 class WpssoIntegReviewYotpoWc {
16
17 private $p; // Wpsso class object.
18
19 public function __construct( &$plugin ) {
20
21 $this->p =& $plugin;
22
23 if ( $this->p->debug->enabled ) {
24
25 $this->p->debug->mark();
26 }
27
28 $this->p->util->add_plugin_filters( $this, array(
29 'og_ecom_woocommerce' => 2,
30 ) );
31 }
32
33 public function filter_og_ecom_woocommerce( array $mt_ecom, array $mod ) {
34
35 if ( $this->p->debug->enabled ) {
36
37 $this->p->debug->mark();
38 }
39
40 $yotpo = $this->get_yotpo_instance();
41
42 $have_schema = $this->p->avail[ 'p' ][ 'schema' ] ? true : false;
43
44 if ( ! is_object( $yotpo ) ) {
45
46 if ( $this->p->debug->enabled ) {
47
48 $this->p->debug->log( 'exiting early: get_yotpo_instance() did not return an object' );
49 }
50
51 return $mt_ecom;
52 }
53
54 /*
55 * Add rating meta tags.
56 */
57 if ( apply_filters( 'wpsso_og_add_mt_rating', true, $mod ) ) {
58
59 if ( $this->p->debug->enabled ) {
60
61 $this->p->debug->log( 'add rating meta tags is true' );
62
63 $this->p->debug->log( 'calling get_product_bottom_line() for product ID ' . $mod[ 'id' ] );
64 }
65
66 $resp = $yotpo->get_product_bottom_line( array(
67 'product_id' => $mod[ 'id' ],
68 ) );
69
70 if ( isset( $resp[ 'response' ][ 'bottomline' ][ 'average_score' ] ) &&
71 isset( $resp[ 'response' ][ 'bottomline' ][ 'total_reviews' ] ) ) {
72
73 if ( $this->p->debug->enabled ) {
74
75 $this->p->debug->log( 'adding product rating from yotpo API response' );
76 }
77
78 $mt_ecom[ 'product:rating:average' ] = (float) $resp[ 'response' ][ 'bottomline' ][ 'average_score' ];
79 $mt_ecom[ 'product:rating:count' ] = (int) $resp[ 'response' ][ 'bottomline' ][ 'total_reviews' ];
80 $mt_ecom[ 'product:rating:worst' ] = 1;
81 $mt_ecom[ 'product:rating:best' ] = 5;
82
83 } elseif ( $this->p->debug->enabled ) {
84
85 $this->p->debug->log( 'error: average_score and/or total_reviews missing from response' );
86 }
87
88 } elseif ( $this->p->debug->enabled ) {
89
90 $this->p->debug->log( 'add rating meta tags is false' );
91 }
92
93 /*
94 * Add reviews meta tags.
95 */
96 if ( apply_filters( 'wpsso_og_add_mt_reviews', $have_schema, $mod ) ) {
97
98 if ( $this->p->debug->enabled ) {
99
100 $this->p->debug->log( 'add review meta tags is true' );
101
102 $this->p->debug->log( 'calling get_product_reviews() for product ID ' . $mod[ 'id' ] );
103 }
104
105 $resp = $yotpo->get_product_reviews( array(
106 'product_id' => $mod[ 'id' ],
107 'page' => 1,
108 'count' => WPSSO_SCHEMA_REVIEWS_MAX,
109 'since_date' => '',
110 ) );
111
112 if ( isset( $resp[ 'response' ][ 'total_reviews' ] ) && isset( $resp[ 'response' ][ 'reviews' ] ) ) {
113
114 if ( $this->p->debug->enabled ) {
115
116 $this->p->debug->log( 'adding product reviews from yotpo API response' );
117 }
118
119 if ( is_array( $resp[ 'response' ][ 'reviews' ] ) ) { // Just in case.
120
121 foreach ( $resp[ 'response' ][ 'reviews' ] as $review ) {
122
123 $single_review = array(
124 'review:id' => isset( $review[ 'id' ] ) ? $review[ 'id' ] : '',
125 'review:url' => '',
126 'review:title' => isset( $review[ 'title' ] ) ? $review[ 'title' ] : '',
127 'review:description' => isset( $review[ 'content' ] ) ? $review[ 'content' ] : '',
128 'review:created_time' => isset( $review[ 'created_at' ] ) ? $review[ 'created_at' ] : '',
129 'review:author:id' => isset( $review[ 'user' ][ 'id' ] ) ? $review[ 'user' ][ 'id' ] : '',
130 'review:author:name' => isset( $review[ 'user' ][ 'display_name' ] ) ? $review[ 'user' ][ 'display_name' ] : '',
131 'review:rating:value' => isset( $review[ 'score' ] ) ? (float) $review[ 'score' ] : 0,
132 'review:rating:worst' => 1,
133 'review:rating:best' => 5,
134 );
135
136 $mt_ecom[ 'product:reviews' ][] = $single_review;
137 }
138
139 } elseif ( $this->p->debug->enabled ) {
140
141 $this->p->debug->log( 'error: reviews in response is not an array' );
142 }
143
144 $mt_ecom[ 'product:review:count' ] = (int) $resp[ 'response' ][ 'total_reviews' ];
145
146 } elseif ( $this->p->debug->enabled ) {
147
148 $this->p->debug->log( 'error: total_reviews and/or reviews missing from response' );
149 }
150
151 } elseif ( $this->p->debug->enabled ) {
152
153 $this->p->debug->log( 'add review meta tags is false' );
154 }
155
156 return $mt_ecom;
157 }
158
159 private function get_yotpo_instance() {
160
161 if ( $this->p->debug->enabled ) {
162
163 $this->p->debug->mark();
164 }
165
166 static $yotpo = null; // Only load the Yotpo class once.
167
168 if ( null !== $yotpo ) {
169
170 if ( $this->p->debug->enabled ) {
171
172 $this->p->debug->log( 'exiting early: yotpo class object already defined' );
173 }
174
175 return $yotpo;
176 }
177
178 $plugin_dir = false;
179 $plugin_slug = 'yotpo-social-reviews-for-woocommerce';
180 $settings = get_option( 'yotpo_settings' );
181 $error_pre = sprintf( __( '%s error:', 'wpsso' ), __METHOD__ );
182
183 if ( empty( $settings[ 'app_key' ] ) ) {
184
185 if ( $this->p->debug->enabled ) {
186
187 $this->p->debug->log( 'exiting early: "app_key" missing from yotpo settings' );
188 }
189
190 if ( is_admin() ) {
191
192 // translators: %s is "App Key".
193 $error_msg = sprintf( __( 'Yotpo for WooCommerce "%s" option value is empty.', 'wpsso' ), 'App Key' );
194
195 $this->p->notice->err( $error_msg );
196
197 SucomUtil::safe_error_log( $error_pre . ' ' . $error_msg );
198 }
199
200 return $yotpo = false;
201 }
202
203 if ( empty( $settings[ 'secret' ] ) ) {
204
205 if ( $this->p->debug->enabled ) {
206
207 $this->p->debug->log( 'exiting early: "secret" missing from yotpo settings' );
208 }
209
210 if ( is_admin() ) {
211
212 // translators: %s is "Secret Token".
213 $error_msg = sprintf( __( 'Yotpo for WooCommerce "%s" option value is empty.', 'wpsso' ), 'Secret Token' );
214
215 $this->p->notice->err( $error_msg );
216
217 SucomUtil::safe_error_log( $error_pre . ' ' . $error_msg );
218 }
219
220 return $yotpo = false;
221 }
222
223 if ( ! function_exists( 'wc_yotpo_compatible' ) ) { // Just in case
224
225 if ( $this->p->debug->enabled ) {
226
227 $this->p->debug->log( 'exiting early: wc_yotpo_compatible() function missing' );
228 }
229
230 if ( is_admin() ) {
231
232 // translators: %s is "wc_yotpo_compatible()".
233 $error_msg = sprintf( __( 'Yotpo for WooCommerce %s function is missing.', 'wpsso' ), '<code>wc_yotpo_compatible()</code>' );
234
235 $this->p->notice->err( $error_msg );
236
237 SucomUtil::safe_error_log( $error_pre . ' ' . $error_msg );
238 }
239
240 return $yotpo = false;
241
242 }
243
244 if ( ! wc_yotpo_compatible() ) {
245
246 if ( $this->p->debug->enabled ) {
247
248 $this->p->debug->log( 'exiting early: wc_yotpo_compatible() returned false' );
249 }
250
251 if ( is_admin() ) {
252
253 // translators: %s is "wc_yotpo_compatible()".
254 $error_msg = sprintf( __( 'Yotpo for WooCommerce %s function returned false.', 'wpsso' ), 'wc_yotpo_compatible()' );
255
256 $this->p->notice->err( $error_msg );
257
258 SucomUtil::safe_error_log( $error_pre . ' ' . $error_msg );
259 }
260
261 return $yotpo = false;
262 }
263
264 if ( defined( 'WPMU_PLUGIN_DIR' ) && file_exists( WPMU_PLUGIN_DIR . '/' . $plugin_slug ) ) {
265
266 $plugin_dir = WPMU_PLUGIN_DIR;
267
268 } elseif ( defined( 'WP_PLUGIN_DIR' ) && file_exists( WP_PLUGIN_DIR . '/' . $plugin_slug ) ) {
269
270 $plugin_dir = WP_PLUGIN_DIR;
271
272 } else {
273
274 if ( $this->p->debug->enabled ) {
275
276 $this->p->debug->log( 'exiting early: "' . $plugin_slug . '" not in the WPMU_PLUGIN_DIR or WP_PLUGIN_DIR folders' );
277 }
278
279 if ( is_admin() ) {
280
281 // translators: %s is "yotpo-social-reviews-for-woocommerce".
282 $error_msg = sprintf( __( '"%s" plugin not found in the WPMU_PLUGIN_DIR or WP_PLUGIN_DIR folders.', 'wpsso' ), $plugin_slug );
283
284 $this->p->notice->err( $error_msg );
285
286 SucomUtil::safe_error_log( $error_pre . ' ' . $error_msg );
287 }
288
289 return $yotpo = false;
290 }
291
292 $api_lib = realpath( $plugin_dir . '/' . $plugin_slug . '/lib/yotpo-api/Yotpo.php' );
293
294 if ( ! file_exists( $api_lib ) ) {
295
296 if ( $this->p->debug->enabled ) {
297
298 $this->p->debug->log( 'exiting early: API library file "' . $api_lib . '" not found' );
299 }
300
301 if ( is_admin() ) {
302
303 // translators: %s is the API library file path.
304 $error_msg = sprintf( __( 'Yotpo for WooCommerce API library file "%s" not found.', 'wpsso' ), $api_lib );
305
306 $this->p->notice->err( $error_msg );
307
308 SucomUtil::safe_error_log( $error_pre . ' ' . $error_msg );
309 }
310
311 return $yotpo = false;
312 }
313
314 if ( $this->p->debug->enabled ) {
315
316 $this->p->debug->log( 'loading api library file ' . $api_lib );
317 }
318
319 require_once $api_lib;
320
321 if ( ! class_exists( 'Yotpo' ) ) {
322
323 if ( $this->p->debug->enabled ) {
324
325 $this->p->debug->log( 'exiting early: Yotpo API class does not exist' );
326 }
327
328 if ( is_admin() ) {
329
330 // translators: %s is the API library file path.
331 $error_msg = sprintf( __( 'Yotpo for WooCommerce "%s" class does not exist.', 'wpsso' ), 'Yotpo' );
332
333 $this->p->notice->err( $error_msg );
334
335 SucomUtil::safe_error_log( $error_pre . ' ' . $error_msg );
336 }
337
338 return $yotpo = false;
339 }
340
341 return $yotpo = new Yotpo( $settings[ 'app_key' ], $settings[ 'secret' ] );
342 }
343 }
344 }
345