PluginProbe
ReviewX – Multi-Criteria Reviews for WooCommerce with Google Reviews & Schema / trunk
ReviewX – Multi-Criteria Reviews for WooCommerce with Google Reviews & Schema vtrunk
2.5.1 2.5.0 2.4.1 2.4.0 2.3.11 2.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 All 143 releases
reviewx / app / Shortcodes / GoogleReviewLIst.php

GoogleReviewLIst.php in ReviewX – Multi-Criteria Reviews for WooCommerce with Google Reviews & Schema trunk, at app/Shortcodes/GoogleReviewLIst.php

75 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ReviewX\Shortcodes;
4
5 use ReviewX\Api\GoogleReviewApi;
6 use ReviewX\Utilities\Auth\Client;
7 use ReviewX\WPDrill\Contracts\ShortcodeContract;
8 use ReviewX\WPDrill\Facades\View;
9 use ReviewX\Services\GoogleReviewService;
10 class GoogleReviewLIst implements ShortcodeContract
11 {
12 protected $googleReview;
13 protected $googleReviewService;
14 public function __construct()
15 {
16 $this->googleReview = new GoogleReviewApi();
17 $this->googleReviewService = new GoogleReviewService();
18 }
19 public function render(array $attrs, ?string $content = null) : string
20 {
21 $attrs = shortcode_atts(['title' => null, 'id' => null, 'truncate' => '300', 'limit' => '3', 'loadmore' => 'false', 'cache' => null], $attrs);
22 // Check if both product_id and post_id are provided.
23 if (!Client::getSync()) {
24 return '<div class="warning">Error: Please complete the synchronization process of ReviewX.</div>';
25 }
26 // Fetch cached/validated API credentials
27 $credentials = $this->getGoogleApiCredentials();
28 // Bail out early if invalid
29 if (empty($credentials['google_api_key']) || empty($credentials['google_place_id_or_url'])) {
30 return '<div class="warning">Error: Please configure your Google API Key and Place ID/URL in ReviewX.</div>';
31 }
32 // Fetch reviews
33 $reviews = $this->reviews($attrs['cache'], $credentials);
34 $title = isset($attrs['title']) && $attrs['title'] === 'false' ? 'false' : ($attrs['title'] === 'true' || empty($attrs['title']) ? 'Google Reviews' : \esc_html($attrs['title']));
35 return View::render('storefront/shortcode/googleReviewList', ['title' => $title, 'content' => $reviews, 'reviewLimit' => (int) $attrs['limit'] ?: 3, 'loadMore' => !empty($attrs['loadmore']) ? $attrs['loadmore'] : \true, 'truncateLimit' => !empty($attrs['truncate']) ? $attrs['truncate'] : 300]);
36 }
37 protected function reviews($cache_time, array $credentials)
38 {
39 $place_id = $credentials['google_place_id_or_url'];
40 $cache_key = 'rvx_google_reviews_cache_' . \md5($place_id);
41 $cached_reviews = \get_transient($cache_key);
42 if (\is_array($cached_reviews) && isset($cached_reviews['reviews'])) {
43 return $cached_reviews;
44 }
45 $rawReviews = $this->googleReview->googleReviewGet();
46 $reviews = \is_string($rawReviews) ? \json_decode($rawReviews, \true) : (\is_array($rawReviews) ? $rawReviews : []);
47 if (empty($reviews['data']['reviews']) || !\is_array($reviews['data']['reviews'])) {
48 return ['reviews' => []];
49 }
50 $cache_duration = (int) $cache_time ?: DAY_IN_SECONDS;
51 \set_transient($cache_key, $reviews['data'], $cache_duration);
52 return $reviews['data'];
53 }
54 /**
55 * Get and cache Google API credentials for 1 hour.
56 *
57 * @return array
58 */
59 protected function getGoogleApiCredentials() : array
60 {
61 $cached = \get_transient('rvx_google_api_settings');
62 if (\is_array($cached)) {
63 return $cached;
64 }
65 $rawResponse = $this->googleReviewService->googleReviewPlaceApi();
66 $decoded = \is_string($rawResponse) ? \json_decode($rawResponse, \true) : (\is_array($rawResponse) ? $rawResponse : []);
67 $googlePlace = $decoded['data']['creadential']['google_place'] ?? $decoded['data']['credential']['google_place'] ?? [];
68 if (empty($googlePlace['google_api_key']) || empty($googlePlace['google_place_id_or_url'])) {
69 return [];
70 }
71 \set_transient('rvx_google_api_settings', $googlePlace, HOUR_IN_SECONDS);
72 return $googlePlace;
73 }
74 }
75