Restore reviews (comments) to the product editor (#37457)

This commit is contained in:
Ron Rennick 2023-04-03 13:02:01 -03:00 committed by GitHub
commit cb7f34de44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 5 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Restores comments (reviews) to the product editor.

View File

@ -168,6 +168,11 @@ class Reviews {
* @return void
*/
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;
@ -235,6 +240,11 @@ class Reviews {
* @return void
*/
private function handle_reply_to_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_post_ID = isset( $_POST['comment_post_ID'] ) ? (int) sanitize_text_field( wp_unslash( $_POST['comment_post_ID'] ) ) : 0; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase

View File

@ -4,6 +4,7 @@ namespace Automattic\WooCommerce\Internal\Admin\ProductReviews;
use Automattic\WooCommerce\Internal\Traits\AccessiblePrivateMethods;
use WP_Comment_Query;
use WP_Screen;
/**
* Tweaks the WordPress comments page to exclude reviews.
@ -116,6 +117,12 @@ class ReviewsCommentsOverrides {
* @return array
*/
protected function exclude_reviews_from_comments( $args ) : array {
$screen = get_current_screen();
// We only wish to intervene if the edit comments screen has been requested.
if ( ! $screen instanceof WP_Screen || 'edit-comments' !== $screen->id ) {
return $args;
}
if ( ! empty( $args['post_type'] ) && $args['post_type'] !== 'any' ) {
$post_types = (array) $args['post_type'];

View File

@ -7,6 +7,7 @@ use Generator;
use ReflectionClass;
use ReflectionException;
use WC_Unit_Test_Case;
use WP_Screen;
/**
* Tests the product reviews overrides for the comments page.
@ -244,20 +245,46 @@ class ReviewsCommentsOverridesTest extends WC_Unit_Test_Case {
* @throws ReflectionException If the method doesn't exist.
*/
public function test_exclude_reviews_from_comments() : void {
global $current_screen;
$original_screen_value = $current_screen;
$overrides = wc_get_container()->get( ReviewsCommentsOverrides::class );
$method = ( new ReflectionClass( $overrides ) )->getMethod( 'exclude_reviews_from_comments' );
$method->setAccessible( true );
$original_args = [
'post_type' => [ 'product' ],
];
$this->assertTrue( in_array( 'product', $original_args['post_type'] ) );
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$current_screen = WP_Screen::get( 'edit-comments' );
$filtered_args = $method->invoke( $overrides, $original_args );
$method = ( new ReflectionClass( $overrides ) )->getMethod( 'exclude_reviews_from_comments' );
$method->setAccessible( true );
$this->assertFalse(
in_array( 'product', $filtered_args['post_type'], true ),
'In the context of the edit-comments screen, the product post type will be removed from the comments query.'
);
$new_args = $method->invoke( $overrides, $original_args );
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$current_screen = WP_Screen::get( 'arbitrary-admin-page' );
$filtered_args = $method->invoke( $overrides, $original_args );
$this->assertFalse( in_array( 'product', $new_args['post_type'] ) );
$this->assertTrue(
in_array( 'product', $filtered_args['post_type'], true ),
'In the context of all other admin screens, the product post type will not be removed from the comments query.'
);
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$current_screen = null;
$filtered_args = $method->invoke( $overrides, $original_args );
$this->assertTrue(
in_array( 'product', $filtered_args['post_type'], true ),
'If the $current_screen global is not set, the product post type will not be removed from the comments query.'
);
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$current_screen = $original_screen_value;
}
}