| 1 |
<?php |
| 2 |
|
| 3 |
namespace ReviewX\Shortcodes\Products; |
| 4 |
|
| 5 |
use ReviewX\Form\ReviewFormHelper; |
| 6 |
use ReviewX\Utilities\Auth\Client; |
| 7 |
use ReviewX\Utilities\Helper; |
| 8 |
use ReviewX\WPDrill\Contracts\ShortcodeContract; |
| 9 |
use ReviewX\WPDrill\Facades\View; |
| 10 |
class ReviewStarCountShortcode implements ShortcodeContract |
| 11 |
{ |
| 12 |
public function render(array $attrs, ?string $content = null) : string |
| 13 |
{ |
| 14 |
$attrs = shortcode_atts(['title' => 'false', 'product_id' => null, 'post_id' => null, 'info' => 'default'], $attrs); |
| 15 |
// Ensure ReviewX sync is complete |
| 16 |
if (!Client::getSync()) { |
| 17 |
return '<div class="warning">Error: Please complete the synchronization process of ReviewX.</div>'; |
| 18 |
} |
| 19 |
$productId = !empty($attrs['product_id']) ? (int) $attrs['product_id'] : 0; |
| 20 |
$postId = !empty($attrs['post_id']) ? (int) $attrs['post_id'] : 0; |
| 21 |
// Prevent both IDs at once |
| 22 |
if ($productId && $postId) { |
| 23 |
return '<div class="warning">Error: Please use only one of "product_id" or "post_id" in the shortcode.</div>'; |
| 24 |
} |
| 25 |
// Determine the final ID to use |
| 26 |
$id = $productId ?: $postId; |
| 27 |
// Auto-detect from global post if neither ID provided |
| 28 |
if (!$productId && !$postId) { |
| 29 |
global $post; |
| 30 |
} else { |
| 31 |
$post = \get_post($id); |
| 32 |
} |
| 33 |
if (!$post) { |
| 34 |
return '<div class="warning">No post or product found!</div>'; |
| 35 |
} |
| 36 |
$data = $this->getReviewsData($post); |
| 37 |
if (!$data['post_type_enabled']) { |
| 38 |
return '<div class="warning notice notice-error"><b>Error:</b> This post type isn\'t enabled in ReviewX.</div>'; |
| 39 |
} |
| 40 |
// Title handling |
| 41 |
if ($attrs['title'] === 'false') { |
| 42 |
$title = 'false'; |
| 43 |
} elseif ($attrs['title'] === 'true' || empty($attrs['title'])) { |
| 44 |
$title = $data['postTitle']; |
| 45 |
} else { |
| 46 |
$title = \esc_html($attrs['title']); |
| 47 |
} |
| 48 |
return View::render('storefront/shortcode/reviewsStarCount', ['title' => $title, 'data' => $data, 'info' => $attrs['info'] ?: 'default', 'postType' => $data['postType']]); |
| 49 |
} |
| 50 |
private function getReviewsData($post) : array |
| 51 |
{ |
| 52 |
$id = $post->ID ?? 0; |
| 53 |
// Check enabled post types even if $post is null |
| 54 |
$enabledPostTypes = (new ReviewFormHelper())->rvxEnabledPostTypes(); |
| 55 |
$postTypeEnabled = 0; |
| 56 |
if ($post) { |
| 57 |
$postTypeEnabled = isset($enabledPostTypes[\strtolower($post->post_type)]) ? 1 : 0; |
| 58 |
} |
| 59 |
// Default data |
| 60 |
$defaultData = ['product' => ['id' => $id, 'post' => $id], 'starCount' => 0.0, 'reviewsCount' => 0, 'post_type_enabled' => $postTypeEnabled, 'postTitle' => $post->post_title ?? '', 'postType' => $post->post_type ?? '', 'domain' => ['baseDomain' => Helper::domainSupport(), 'baseRestUrl' => Helper::getRestAPIurl()]]; |
| 61 |
if (!$post) { |
| 62 |
return $defaultData; |
| 63 |
} |
| 64 |
// Get actual star/review counts |
| 65 |
if ($post->post_type === 'product') { |
| 66 |
$defaultData['starCount'] = (float) \get_post_meta($id, '_wc_average_rating', \true); |
| 67 |
$defaultData['reviewsCount'] = (int) \get_post_meta($id, '_wc_review_count', \true); |
| 68 |
} else { |
| 69 |
$defaultData['starCount'] = (float) \get_post_meta($id, 'rvx_avg_rating', \true); |
| 70 |
$defaultData['reviewsCount'] = (int) \get_post_meta($id, 'rvx_total_reviews', \true); |
| 71 |
// Fallback to comment_count if meta is not set yet |
| 72 |
if (!$defaultData['reviewsCount']) { |
| 73 |
$defaultData['reviewsCount'] = (int) $post->comment_count; |
| 74 |
} |
| 75 |
} |
| 76 |
return $defaultData; |
| 77 |
} |
| 78 |
} |
| 79 |
|