Automattic\WooCommerce\Internal\OrderReviews

SubmissionHandler::insert_review │ private static │ WC 1.0

Insert a review through wp_new_comment(), so it gets the same filters, spam checks and notifications as a comment posted on the product page.

Method of the class: SubmissionHandler{}

No Hooks.

Returns

int|false|\WP_Error. Comment ID, false on failure, or the error from the comment checks.

Usage

$result = SubmissionHandler::insert_review( $comment_data );
$comment_data(array) (required)
Unslashed comment data.

SubmissionHandler::insert_review() code WC 11.1.2

private static function insert_review( array $comment_data ) {
	// Sanitize the address as core's pre_comment_author_email chain would, but
	// keep it un-encoded (sanitize_email preserves `&`) so the eligibility
	// lookups still match the order's billing email.
	$raw_email      = wp_slash( sanitize_email( (string) $comment_data['comment_author_email'] ) );
	$keep_raw_email = function () use ( $raw_email ) {
		return $raw_email;
	};

	// These reviews are tied to a real purchased line item, so don't hold them
	// behind the "commenter must already have an approved comment" gate that
	// core applies to anonymous blog commenters.
	add_filter( 'pre_option_comment_previously_approved', '__return_zero' );
	// Variations share the parent product, so identical text on two of them is not a duplicate.
	add_filter( 'duplicate_comment_id', '__return_zero' );
	// Keep the sanitized address as-is; core's chain would otherwise encode `&`.
	add_filter( 'pre_comment_author_email', $keep_raw_email, PHP_INT_MAX );
	// Rows are capped at one review per purchased item, and core's flood query matches order notes by IP.
	add_filter( 'wp_is_comment_flood', '__return_false', PHP_INT_MAX );

	try {
		return wp_new_comment( wp_slash( $comment_data ), true );
	} finally {
		remove_filter( 'pre_option_comment_previously_approved', '__return_zero' );
		remove_filter( 'duplicate_comment_id', '__return_zero' );
		remove_filter( 'pre_comment_author_email', $keep_raw_email, PHP_INT_MAX );
		remove_filter( 'wp_is_comment_flood', '__return_false', PHP_INT_MAX );
	}
}