From 08e9039241b994dd3e75f0a86e6f2935f9e97b36 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 24 Sep 2018 10:57:20 -0700 Subject: [PATCH] Prevent empty term status from being interpreted as new-rejected during bulk change --- .../class-amp-validation-error-taxonomy.php | 7 +++++++ .../test-class-amp-validation-error-taxonomy.php | 13 +++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/includes/validation/class-amp-validation-error-taxonomy.php b/includes/validation/class-amp-validation-error-taxonomy.php index 98e0601ca86..bb90d49b545 100644 --- a/includes/validation/class-amp-validation-error-taxonomy.php +++ b/includes/validation/class-amp-validation-error-taxonomy.php @@ -360,6 +360,11 @@ public static function delete_empty_terms() { public static function sanitize_term_status( $status, $options = array() ) { $multiple = ! empty( $options['multiple'] ); + // Catch case where an empty string is supplied. Prevent casting to 0. + if ( ! is_numeric( $status ) && empty( $status ) ) { + return $multiple ? array() : null; + } + if ( is_string( $status ) ) { $statuses = wp_parse_id_list( $status ); } else { @@ -940,6 +945,8 @@ public static function add_term_filter_query_var( $url, $tax ) { array( self::VALIDATION_ERROR_STATUS_QUERY_VAR => $groups ), $url ); + } else { + $url = remove_query_arg( self::VALIDATION_ERROR_STATUS_QUERY_VAR, $url ); } return $url; diff --git a/tests/validation/test-class-amp-validation-error-taxonomy.php b/tests/validation/test-class-amp-validation-error-taxonomy.php index 0ffd35c35af..fc415cb0a02 100644 --- a/tests/validation/test-class-amp-validation-error-taxonomy.php +++ b/tests/validation/test-class-amp-validation-error-taxonomy.php @@ -176,11 +176,16 @@ public function test_delete_empty_terms() { * @covers \AMP_Validation_Error_Taxonomy::sanitize_term_status() */ public function test_sanitize_term_status() { + $this->assertNull( AMP_Validation_Error_Taxonomy::sanitize_term_status( '' ) ); + $this->assertNull( AMP_Validation_Error_Taxonomy::sanitize_term_status( false ) ); + $this->assertNull( AMP_Validation_Error_Taxonomy::sanitize_term_status( null ) ); + $this->assertSame( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_REJECTED_STATUS, AMP_Validation_Error_Taxonomy::sanitize_term_status( '0' ) ); + $this->assertSame( array(), AMP_Validation_Error_Taxonomy::sanitize_term_status( '', array( 'multiple' => true ) ) ); $this->assertNull( AMP_Validation_Error_Taxonomy::sanitize_term_status( '100' ) ); - $this->assertEquals( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_REJECTED_STATUS, AMP_Validation_Error_Taxonomy::sanitize_term_status( (string) AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_REJECTED_STATUS ) ); - $this->assertEquals( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_ACCEPTED_STATUS, AMP_Validation_Error_Taxonomy::sanitize_term_status( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_ACCEPTED_STATUS ) ); - $this->assertEquals( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACK_REJECTED_STATUS, AMP_Validation_Error_Taxonomy::sanitize_term_status( (string) AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACK_REJECTED_STATUS ) ); - $this->assertEquals( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACK_ACCEPTED_STATUS, AMP_Validation_Error_Taxonomy::sanitize_term_status( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACK_ACCEPTED_STATUS, array( 'multiple' => false ) ) ); + $this->assertSame( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_REJECTED_STATUS, AMP_Validation_Error_Taxonomy::sanitize_term_status( (string) AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_REJECTED_STATUS ) ); + $this->assertSame( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_ACCEPTED_STATUS, AMP_Validation_Error_Taxonomy::sanitize_term_status( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_ACCEPTED_STATUS ) ); + $this->assertSame( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACK_REJECTED_STATUS, AMP_Validation_Error_Taxonomy::sanitize_term_status( (string) AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACK_REJECTED_STATUS ) ); + $this->assertSame( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACK_ACCEPTED_STATUS, AMP_Validation_Error_Taxonomy::sanitize_term_status( AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACK_ACCEPTED_STATUS, array( 'multiple' => false ) ) ); $this->assertEquals( array(