'false', 'product_id' => null, 'post_id' => null, 'info' => 'default'], $attrs);
// Ensure ReviewX sync is complete
if (!Client::getSync()) {
return '
Error: Please complete the synchronization process of ReviewX.
';
}
$productId = !empty($attrs['product_id']) ? (int) $attrs['product_id'] : 0;
$postId = !empty($attrs['post_id']) ? (int) $attrs['post_id'] : 0;
// Prevent both IDs at once
if ($productId && $postId) {
return 'Error: Please use only one of "product_id" or "post_id" in the shortcode.
';
}
// Determine the final ID to use
$id = $productId ?: $postId;
// Auto-detect from global post if neither ID provided
if (!$productId && !$postId) {
global $post;
} else {
$post = \get_post($id);
}
if (!$post) {
return 'No post or product found!
';
}
$data = $this->getReviewsData($post);
if (!$data['post_type_enabled']) {
return 'Error: This post type isn\'t enabled in ReviewX.
';
}
// Title handling
if ($attrs['title'] === 'false') {
$title = 'false';
} elseif ($attrs['title'] === 'true' || empty($attrs['title'])) {
$title = $data['postTitle'];
} else {
$title = \esc_html($attrs['title']);
}
return View::render('storefront/shortcode/reviewsStarCount', ['title' => $title, 'data' => $data, 'info' => $attrs['info'] ?: 'default', 'postType' => $data['postType']]);
}
private function getReviewsData($post) : array
{
$id = $post->ID ?? 0;
// Check enabled post types even if $post is null
$enabledPostTypes = (new ReviewFormHelper())->rvxEnabledPostTypes();
$postTypeEnabled = 0;
if ($post) {
$postTypeEnabled = isset($enabledPostTypes[\strtolower($post->post_type)]) ? 1 : 0;
}
// Default data
$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()]];
if (!$post) {
return $defaultData;
}
// Get actual star/review counts
if ($post->post_type === 'product') {
$defaultData['starCount'] = (float) \get_post_meta($id, '_wc_average_rating', \true);
$defaultData['reviewsCount'] = (int) \get_post_meta($id, '_wc_review_count', \true);
} else {
$defaultData['starCount'] = (float) \get_post_meta($id, 'rvx_avg_rating', \true);
$defaultData['reviewsCount'] = (int) \get_post_meta($id, 'rvx_total_reviews', \true);
// Fallback to comment_count if meta is not set yet
if (!$defaultData['reviewsCount']) {
$defaultData['reviewsCount'] = (int) $post->comment_count;
}
}
return $defaultData;
}
}