Automattic\WooCommerce\Internal\Admin\ProductReviews

Reviews::handle_edit_review()privateWC 1.0

Ajax callback for editing a review.

This functionality is taken from wp_ajax_edit_comment() and is largely copy and pasted. The only thing we want to change is the review row HTML in the response. WordPress core uses a comment list table and we need to use our own ReviewsListTable{} class to support our custom columns.

This ajax callback is registered with a lower priority than WordPress core's so that our code can run first. If the supplied comment ID is not a review or a reply to a review, then we return early from this method to allow the WordPress core callback to take over.

Method of the class: Reviews{}

No Hooks.

Return

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->handle_edit_review(): void;

Reviews::handle_edit_review() code WC 8.6.1

private function handle_edit_review(): void {
	// Don't interfere with comment functionality relating to the reviews meta box within the product editor.
	if ( sanitize_text_field( wp_unslash( $_POST['mode'] ?? '' ) ) === 'single' ) {
		return;
	}

	check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' );

	$comment_id = isset( $_POST['comment_ID'] ) ? (int) sanitize_text_field( wp_unslash( $_POST['comment_ID'] ) ) : 0;

	if ( empty( $comment_id ) || ! current_user_can( 'edit_comment', $comment_id ) ) {
		wp_die( -1 );
	}

	$review = get_comment( $comment_id );

	// Bail silently if this is not a review, or a reply to a review. That allows `wp_ajax_edit_comment()` to handle any further actions.
	if ( ! $this->is_review_or_reply( $review ) ) {
		return;
	}

	if ( empty( $review->comment_ID ) ) {
		wp_die( -1 );
	}

	if ( empty( $_POST['content'] ) ) {
		wp_die( esc_html__( 'Error: Please type your review text.', 'woocommerce' ) );
	}

	if ( isset( $_POST['status'] ) ) {
		$_POST['comment_status'] = sanitize_text_field( wp_unslash( $_POST['status'] ) );
	}

	$updated = edit_comment();
	if ( is_wp_error( $updated ) ) {
		wp_die( esc_html( $updated->get_error_message() ) );
	}

	$position      = isset( $_POST['position'] ) ? (int) sanitize_text_field( wp_unslash( $_POST['position'] ) ) : -1;
	$wp_list_table = $this->make_reviews_list_table();

	ob_start();
	$wp_list_table->single_row( $review );
	$review_list_item = ob_get_clean();

	$x = new WP_Ajax_Response();

	$x->add(
		array(
			'what'     => 'edit_comment',
			'id'       => $review->comment_ID,
			'data'     => $review_list_item,
			'position' => $position,
		)
	);

	$x->send();
}