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 / Products / ReviewSummaryShortcode.php

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

56 lines 2.3 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\Products;
4
5 use ReviewX\Utilities\Auth\Client;
6 use ReviewX\Utilities\Helper;
7 use ReviewX\WPDrill\Contracts\ShortcodeContract;
8 use ReviewX\WPDrill\Facades\View;
9 class ReviewSummaryShortcode implements ShortcodeContract
10 {
11 public function render(array $attrs, ?string $content = null) : string
12 {
13 // Define default attributes to accept both product_id and post_id.
14 $attrs = shortcode_atts(['title' => null, 'product_id' => null, 'post_id' => null], $attrs);
15 // Check if both product_id and post_id are provided.
16 if (!Client::getSync()) {
17 return '<div class="warning">Error: Please complete the synchronization process of ReviewX.</div>';
18 }
19 // If both IDs are provided, return an error.
20 if (!empty($attrs['product_id']) && !empty($attrs['post_id'])) {
21 return '<div class="warning">Error: Please use only one of "product_id" or "post_id" in the shortcode.</div>';
22 }
23 // Determine the type and set the ID.
24 $isProduct = !empty($attrs['product_id']);
25 $id = $isProduct ? (int) $attrs['product_id'] : (int) $attrs['post_id'];
26 if (!$id && is_singular()) {
27 $id = (int) get_the_ID();
28 $isProduct = 'product' === \get_post_type($id);
29 }
30 // Prepare the data.
31 $data = $this->productWiseReviewShow($id, $isProduct);
32 // Title handling
33 if ($attrs['title'] === 'false') {
34 $title = 'false';
35 } elseif ($attrs['title'] === 'true' || empty($attrs['title'])) {
36 $title = $data['postTitle'];
37 } else {
38 $title = \esc_html($attrs['title']);
39 }
40 return View::render('storefront/shortcode/reviewSummary', ['title' => $title, 'data' => \json_encode($data)]);
41 }
42 /**
43 * Build the data structure for the review summary.
44 *
45 * @param int $id
46 * @param bool $isProduct
47 * @return string JSON encoded attributes.
48 */
49 public function productWiseReviewShow($id, $isProduct) : array
50 {
51 $post = \get_post($id);
52 $attributes = ['product' => ['id' => $id], 'postTitle' => $post ? $post->post_title : \false, 'postType' => $post ? $post->post_type : '', 'domain' => ['baseDomain' => Helper::domainSupport(), 'baseRestUrl' => Helper::getRestAPIurl()]];
53 return $attributes;
54 }
55 }
56