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 / ReviewGraphShortcode.php

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

47 lines 2.1 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 ReviewGraphShortcode implements ShortcodeContract
10 {
11 public function render(array $attrs, ?string $content = null) : string
12 {
13 $attrs = shortcode_atts(['title' => null, 'product_id' => null, 'post_id' => null], $attrs);
14 // Check if both product_id and post_id are provided.
15 if (!Client::getSync()) {
16 return '<div class="warning">Error: Please complete the synchronization process of ReviewX.</div>';
17 }
18 // Check if both product_id and post_id are provided.
19 if (!empty($attrs['product_id']) && !empty($attrs['post_id'])) {
20 return '<div class="warning">Error: Please use only one of "product_id" or "post_id" in the shortcode.</div>';
21 }
22 // Determine whether we're dealing with a product or a post.
23 $isProduct = !empty($attrs['product_id']);
24 $id = $isProduct ? (int) $attrs['product_id'] : (int) $attrs['post_id'];
25 if (!$id && is_singular()) {
26 $id = (int) get_the_ID();
27 $isProduct = 'product' === \get_post_type($id);
28 }
29 $data = $this->productWiseReviewShow($id, $isProduct);
30 // Title handling
31 if ($attrs['title'] === 'false') {
32 $title = 'false';
33 } elseif ($attrs['title'] === 'true' || empty($attrs['title'])) {
34 $title = $data['postTitle'];
35 } else {
36 $title = \esc_html($attrs['title']);
37 }
38 return View::render('storefront/shortcode/reviewGraph', ['title' => $title, 'data' => \json_encode($data)]);
39 }
40 public function productWiseReviewShow($id, bool $isProduct) : array
41 {
42 $post = \get_post($id);
43 $attributes = ['product' => ['id' => $id], 'postTitle' => $post ? $post->post_title : \false, 'postType' => $post ? $post->post_type : '', 'domain' => ['baseDomain' => Helper::domainSupport(), 'baseRestUrl' => Helper::getRestAPIurl()]];
44 return $attributes;
45 }
46 }
47