Skip to content

Commit

Permalink
Merge pull request #2560 from 10up/fix/facets-tax-query
Browse files Browse the repository at this point in the history
Fix/facets tax query
  • Loading branch information
felipeelia authored Feb 22, 2022
2 parents 17acb04 + 1f082cb commit 3df3d0c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
24 changes: 21 additions & 3 deletions includes/classes/Feature/Facets/Facets.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,11 @@ public function get_selected() {
);

$allowed_args = $this->get_allowed_query_args();
$filter_name = $this->get_filter_name();

foreach ( $_GET as $key => $value ) { // phpcs:ignore WordPress.Security.NonceVerification
if ( 0 === strpos( $key, 'filter_' ) ) {
$taxonomy = str_replace( 'filter_', '', $key );
if ( 0 === strpos( $key, $filter_name ) ) {
$taxonomy = str_replace( $filter_name, '', $key );

$filters['taxonomies'][ $taxonomy ] = array(
'terms' => array_fill_keys( array_map( 'trim', explode( ',', trim( $value, ',' ) ) ), true ),
Expand Down Expand Up @@ -423,7 +424,7 @@ public function build_query_url( $filters ) {

foreach ( $tax_filters as $taxonomy => $filter ) {
if ( ! empty( $filter['terms'] ) ) {
$query_param[ 'filter_' . $taxonomy ] = implode( ',', array_keys( $filter['terms'] ) );
$query_param[ $this->get_filter_name() . $taxonomy ] = implode( ',', array_keys( $filter['terms'] ) );
}
}
}
Expand Down Expand Up @@ -514,4 +515,21 @@ public function get_allowed_query_args() {
*/
return apply_filters( 'ep_facet_allowed_query_args', $args );
}

/**
* Get the facet filter name.
*
* @return string The filter name.
*/
protected function get_filter_name() {
/**
* Filter the facet filter name that's added to the URL
*
* @hook ep_facet_filter_name
* @since 4.0.0
* @param {string} Facet filter name
* @return {string} New facet filter name
*/
return apply_filters( 'ep_facet_filter_name', 'ep_filter_' );
}
}
18 changes: 9 additions & 9 deletions tests/php/features/TestFacet.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ public function tearDown() {
public function testGetSelected() {
$facet_feature = Features::factory()->get_registered_feature( 'facets' );

parse_str( 'filter_taxonomy=dolor', $_GET );
parse_str( 'ep_filter_taxonomy=dolor', $_GET );
$selected = $facet_feature->get_selected();
$this->assertSelectedTax( [ 'dolor' => true ], 'taxonomy', $selected );

parse_str( 'filter_taxonomy=dolor,sit', $_GET );
parse_str( 'ep_filter_taxonomy=dolor,sit', $_GET );
$selected = $facet_feature->get_selected();
$this->assertSelectedTax( [ 'dolor' => true, 'sit' => true ], 'taxonomy', $selected );

parse_str( 'filter_taxonomy=dolor,sit&filter_othertax=amet', $_GET );
parse_str( 'ep_filter_taxonomy=dolor,sit&ep_filter_othertax=amet', $_GET );
$selected = $facet_feature->get_selected();

$this->assertIsArray( $selected );
Expand All @@ -62,13 +62,13 @@ public function testGetSelected() {
$this->assertSame( [ 'dolor' => true, 'sit' => true ], $selected['taxonomies']['taxonomy']['terms'] );
$this->assertSame( [ 'amet' => true ], $selected['taxonomies']['othertax']['terms'] );

parse_str( 's=searchterm&filter_taxonomy=dolor', $_GET );
parse_str( 's=searchterm&ep_filter_taxonomy=dolor', $_GET );
$selected = $facet_feature->get_selected();
$this->assertSelectedTax( [ 'dolor' => true ], 'taxonomy', $selected );
$this->assertArrayHasKey( 's', $selected );
$this->assertSame( 'searchterm', $selected['s'] );

parse_str( 'post_type=posttype&filter_taxonomy=dolor', $_GET );
parse_str( 'post_type=posttype&ep_filter_taxonomy=dolor', $_GET );
$selected = $facet_feature->get_selected();
$this->assertSelectedTax( [ 'dolor' => true ], 'taxonomy', $selected );
$this->assertArrayHasKey( 'post_type', $selected );
Expand All @@ -95,10 +95,10 @@ public function testBuildQueryUrl() {
]
];

$this->assertEquals( '/?filter_category=augue', $facet_feature->build_query_url( $filters ) );
$this->assertEquals( '/?ep_filter_category=augue', $facet_feature->build_query_url( $filters ) );

$filters['s'] = 'dolor';
$this->assertEquals( '/?filter_category=augue&s=dolor', $facet_feature->build_query_url( $filters ) );
$this->assertEquals( '/?ep_filter_category=augue&s=dolor', $facet_feature->build_query_url( $filters ) );

unset( $filters['s'] );
$filters = [
Expand All @@ -112,12 +112,12 @@ public function testBuildQueryUrl() {
]
];

$this->assertEquals( '/?filter_category=augue%2Cconsectetur', $facet_feature->build_query_url( $filters ) );
$this->assertEquals( '/?ep_filter_category=augue%2Cconsectetur', $facet_feature->build_query_url( $filters ) );

$_SERVER['REQUEST_URI'] = 'test/page/1';

$filters['s'] = 'dolor';
$this->assertEquals( 'test/?filter_category=augue%2Cconsectetur&s=dolor', $facet_feature->build_query_url( $filters ) );
$this->assertEquals( 'test/?ep_filter_category=augue%2Cconsectetur&s=dolor', $facet_feature->build_query_url( $filters ) );
}

/**
Expand Down

0 comments on commit 3df3d0c

Please sign in to comment.