WC_Comments::add_comment_rating │ public static │ WC 1.0

Rating field for comments.

Method of the class: WC_Comments{}

No Hooks.

Returns

null. Nothing (null).

Usage

$result = WC_Comments::add_comment_rating( $comment_id );
$comment_id(int) (required)
Comment ID.

WC_Comments::add_comment_rating() code WC 11.2.1

public static function add_comment_rating( $comment_id ) {
	if ( isset( $_POST['rating'], $_POST['comment_post_ID'] ) && 'product' === get_post_type( absint( $_POST['comment_post_ID'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Anonymous product reviews are submitted through the core comment form, which carries no WooCommerce nonce; the post ID is read through absint().
		$raw_rating = is_scalar( $_POST['rating'] ) ? wp_unslash( $_POST['rating'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Anonymous product reviews are submitted through the core comment form, which carries no WooCommerce nonce; the value is range checked and cast to an integer below.

		// Stored ratings are whole numbers from 1 to 5. Numeric input is range checked before it
		// is truncated, so 0.5 and 5.9 are rejected rather than stored as 0 and 5, while 4.7
		// still stores 4 as before.
		if ( is_numeric( $raw_rating ) && ( $raw_rating < 1 || $raw_rating > 5 ) ) {
			return;
		}

		$rating = intval( $raw_rating );

		if ( $rating < 1 || $rating > 5 ) {
			return;
		}

		add_comment_meta( $comment_id, 'rating', $rating, true );

		$post_id = isset( $_POST['comment_post_ID'] ) ? absint( $_POST['comment_post_ID'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Anonymous product reviews are submitted through the core comment form, which carries no WooCommerce nonce; the post ID is read through absint().
		if ( $post_id ) {
			self::clear_transients( $post_id );
		}
	}
}