Skip to content

Commit

Permalink
Merge pull request #2229 from 10up/fix/facets-pagination
Browse files Browse the repository at this point in the history
Fix/facets pagination
  • Loading branch information
Rahmon authored Jun 25, 2021
2 parents b607dd0 + d917bf1 commit b7744dc
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 8 deletions.
13 changes: 5 additions & 8 deletions includes/classes/Feature/Facets/Facets.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,16 +441,13 @@ public function build_query_url( $filters ) {
*/
$query_string = apply_filters( 'ep_facet_query_string', $query_string );

if ( is_post_type_archive() ) {
$pagination = strpos( $_SERVER['REQUEST_URI'], '/page' );

if ( false !== $pagination ) {
$url = substr( $_SERVER['REQUEST_URI'], 0, $pagination );
return strtok( $url, '?' ) . ( ( ! empty( $query_string ) ) ? '/?' . $query_string : '' );
}
$url = $_SERVER['REQUEST_URI'];
$pagination = strpos( $url, '/page' );
if ( false !== $pagination ) {
$url = substr( $url, 0, $pagination );
}

return strtok( $_SERVER['REQUEST_URI'], '?' ) . ( ( ! empty( $query_string ) ) ? '?' . $query_string : '' );
return strtok( trailingslashit( $url ), '?' ) . ( ( ! empty( $query_string ) ) ? '?' . $query_string : '' );
}

/**
Expand Down
81 changes: 81 additions & 0 deletions tests/php/features/TestFacet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Test facet feature
*
* @package elasticpress
*/

namespace ElasticPressTest;

use ElasticPress\Features as Features;

/**
* Facet test class
*/
class TestFacets extends BaseTestCase {

/**
* Setup each test.
*
* @since 3.6.0
*/
public function setUp() {
parent::setUp();
}

/**
* Clean up after each test.
*
* @since 3.6.0
*/
public function tearDown() {
parent::tearDown();
}

/**
* Test build query URL
*
* @since 3.6.0
* @group facets
*
*/
public function testBuildQueryUrl() {
$facet_feature = Features::factory()->get_registered_feature( 'facets' );

$filters = [
'taxonomies' => [
'category' => [
'terms' => [
'augue' => 1
]
]
]
];

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

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

set_query_var( 's', '' );
$filters = [
'taxonomies' => [
'category' => [
'terms' => [
'augue' => 1,
'consectetur' => 1
]
]
]
];

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

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

set_query_var( 's', 'dolor' );
$this->assertEquals( 'test/?s=dolor&filter_category=augue,consectetur', $facet_feature->build_query_url( $filters ) );

}

}

0 comments on commit b7744dc

Please sign in to comment.