File: /var/www/kevin-demo/wp-content/themes/fitmencook/includes/comments.php
<?php
/**
* Customize Default WP Comments Section (Woo-safe)
*
* - Adds recipe/product star rating UI to comment form on supported post types
* - Saves rating to comment meta
* - Renders rating above comment text
* - Removes Woo reviews title (Woo-only)
* - Injects average rating in a couple places (only if helper exists)
*/
/**
* Helpers
*/
function fmc_has_is_product(): bool {
return function_exists('is_product');
}
function fmc_is_product_page(): bool {
return fmc_has_is_product() && is_product();
}
function fmc_is_rating_context(): bool {
// We only show rating UI on these contexts
if ( is_singular('recipes') ) return true;
if ( is_singular('meal-plans') ) return true;
if ( fmc_is_product_page() ) return true;
return false;
}
/**
* Rating UI HTML appended to comment form
*/
function fmc_get_rating_html(): string {
$is_product = fmc_is_product_page();
ob_start();
?>
<?php if ( ! $is_product ) : ?>
<div class="rating-checkbox-wrap">
<input id="use-rating" name="use-rating" type="checkbox" value="1">
<label style="display:block" for="use-rating">
<?php esc_html_e('I have made this recipe', 'fitmmencook'); ?>
</label>
</div>
<?php endif; ?>
<select name="rateRecipe" id="recipe-rate">
<option value="0">Rate it</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<div class="recipe_rate-wrapper <?php echo $is_product ? '' : 'disabled'; ?>">
<p class="recipe-rate-label"><?php esc_html_e('Rate the recipe:', 'fitmenCook'); ?></p>
<div
class="rateit svg"
data-rateit-starwidth="19"
data-rateit-starheight="16"
data-rateit-resetable="false"
data-rateit-backingfld="#recipe-rate"
data-rateit-min="0"
></div>
</div>
<?php if ( $is_product ) : ?>
<input type="hidden" name="postType" value="single-product">
<?php endif; ?>
<?php
return (string) ob_get_clean();
}
/**
* Append rating UI to the comment textarea field (on supported post types)
*/
add_filter('comment_form_field_comment', 'fmc_render_stars', 99, 1);
function fmc_render_stars($comment_field) {
if ( ! fmc_is_rating_context() ) {
return $comment_field;
}
return $comment_field . fmc_get_rating_html();
}
/**
* Save rating on comment_post
*/
add_action( 'comment_post', 'fmc_save_rating_value', 10, 3 );
function fmc_save_rating_value( $comment_id, $approved, $commentdata ) {
// Only top-level comments
if ( empty($commentdata['comment_parent']) || (int)$commentdata['comment_parent'] !== 0 ) {
return;
}
// Only for our contexts (guarded)
// Note: On comment_post, conditionals may still work, but we keep it simple and rely on submitted fields.
$recipe_rating = isset($_POST['rateRecipe']) ? (int) wp_strip_all_tags($_POST['rateRecipe']) : 0;
$use_rating = !empty($_POST['use-rating']); // checkbox
$post_type_tag = isset($_POST['postType']) ? wp_strip_all_tags($_POST['postType']) : '';
// Clamp rating 0..5
if ( $recipe_rating < 0 ) $recipe_rating = 0;
if ( $recipe_rating > 5 ) $recipe_rating = 5;
// Save only if user opted in (recipes) OR it's a product review context.
if ( $use_rating || $post_type_tag === 'single-product' ) {
update_comment_meta( $comment_id, 'rating', $recipe_rating );
}
}
/**
* Render rating above the comment text (front-end only, supported contexts only)
*/
add_filter( 'comment_text', 'fmc_add_rating_to_review_text', 10, 1 );
function fmc_add_rating_to_review_text( $text ) {
// Never alter in admin
if ( is_admin() ) return $text;
// Only in our supported contexts
if ( ! fmc_is_rating_context() ) return $text;
$comment_id = get_comment_ID();
if ( ! $comment_id ) return $text;
$rating = get_comment_meta( $comment_id, 'rating', true );
$comment = get_comment( $comment_id );
// No rating? No extra UI
if ( $rating === '' || $rating === null || $rating === false ) return $text;
// Only for top-level comments
if ( $comment && (int)$comment->comment_parent !== 0 ) return $text;
$date = get_comment_date('M d Y', $comment_id);
$rating_val = esc_attr($rating);
$date_html = esc_html($date);
$rating_html = '<div class="comment-items">
<div class="rateit" data-rateit-starwidth="19" data-rateit-starheight="16" data-rateit-value="'.$rating_val.'" data-rateit-ispreset="true" data-rateit-readonly="true"></div>
<p class="comment-date"> '.$date_html.' </p>
</div>';
return $rating_html . $text;
}
/**
* Comment default params
*/
add_filter('comment_form_defaults', 'fmc_comment_default_params', 20);
function fmc_comment_default_params( $defaults ) {
$defaults['title_reply'] = __('Reviews', 'fitmenCook');
return $defaults;
}
/**
* Woo comment title remove (Woo-only filter)
*/
if ( function_exists('add_filter') ) {
add_filter('woocommerce_reviews_title', 'fmc_remove_woo_comments_title');
}
function fmc_remove_woo_comments_title () {
return false;
}
/**
* Avg rating hooks:
* - Woo single product summary (Woo-only)
* - Before comment form (anywhere)
*
* Only call if helper function exists to avoid fatals.
*/
if ( function_exists('add_action') ) {
// Woo-only hook name is harmless to register, but the callback must not assume Woo exists.
add_action('woocommerce_single_product_summary', function() {
if ( function_exists('get_avarage_rating') ) {
get_avarage_rating(get_the_ID(), "sidebar");
}
});
add_action('comment_form_before', function() {
if ( function_exists('get_avarage_rating') ) {
get_avarage_rating(get_the_ID(), "");
}
});
}