Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ep_facet_special_slug_taxonomies filter #3506

Merged
merged 18 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions includes/classes/Feature/Facets/Types/Taxonomy/FacetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,12 @@ public function facet_query( $query ) {

$tax_query = $query->get( 'tax_query', [] );

// Account for taxonomies that should be woocommerce attributes, if WC is enabled
$attribute_taxonomies = [];
if ( function_exists( 'wc_attribute_taxonomy_name' ) ) {
$all_attr_taxonomies = wc_get_attribute_taxonomies();

foreach ( $all_attr_taxonomies as $attr_taxonomy ) {
$attribute_taxonomies[ $attr_taxonomy->attribute_name ] = wc_attribute_taxonomy_name( $attr_taxonomy->attribute_name );
}
}
/** This filter is documented below */
$special_taxonomies = apply_filters( 'ep_facet_tax_special_slug_taxonomies', [], $selected_filters );

foreach ( $selected_filters['taxonomies'] as $taxonomy => $filter ) {
$tax_query[] = [
'taxonomy' => isset( $attribute_taxonomies[ $taxonomy ] ) ? $attribute_taxonomies[ $taxonomy ] : $taxonomy,
'taxonomy' => $special_taxonomies[ $taxonomy ] ?? $taxonomy,
'field' => 'slug',
'terms' => array_keys( $filter['terms'] ),
'operator' => ( 'any' === $settings['match_type'] ) ? 'or' : 'and',
Expand Down Expand Up @@ -214,24 +207,27 @@ public function add_query_filters( $filters ) {
}

$selected_filters = $feature->get_selected();

if ( empty( $selected_filters ) || empty( $selected_filters[ $this->get_filter_type() ] ) ) {
return;
}

// Account for taxonomies that should be woocommerce attributes, if WC is enabled
$attribute_taxonomies = [];
if ( function_exists( 'wc_attribute_taxonomy_name' ) ) {
$all_attr_taxonomies = wc_get_attribute_taxonomies();

foreach ( $all_attr_taxonomies as $attr_taxonomy ) {
$attribute_taxonomies[ $attr_taxonomy->attribute_name ] = wc_attribute_taxonomy_name( $attr_taxonomy->attribute_name );
}
}
/**
* Filter for treatment special slugs in taxonomies. This is used in case you need to change the default taxonomy slug.
*
* @since 4.7.0
* @hook ep_facet_tax_special_slug_taxonomies
* @param {array} $special_taxonomies Taxonomies with special slugs.
* @param {array} $selected_filters Selected filters.
* @return {array} New taxonomies with special slugs.
*/
$special_taxonomies = apply_filters( 'ep_facet_tax_special_slug_taxonomies', [], $selected_filters );

$match_type = $feature->get_match_type();

foreach ( $selected_filters['taxonomies'] as $taxonomy => $filter ) {
$taxonomy_slug = $attribute_taxonomies[ $taxonomy ] ?? $taxonomy;

$taxonomy_slug = $special_taxonomies[ $taxonomy ] ?? $taxonomy;

if ( 'any' === $match_type ) {
$filters[] = [
Expand Down
17 changes: 16 additions & 1 deletion includes/classes/Feature/WooCommerce/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public function setup() {
add_filter( 'ep_weighting_default_post_type_weights', [ $this, 'add_product_default_post_type_weights' ], 10, 2 );
add_filter( 'ep_prepare_meta_data', [ $this, 'add_variations_skus_meta' ], 10, 2 );
add_filter( 'request', [ $this, 'admin_product_list_request_query' ], 9 );

add_action( 'pre_get_posts', [ $this, 'translate_args' ], 11, 1 );
add_filter( 'ep_facet_tax_special_slug_taxonomies', [ $this, 'add_taxonomy_attributes' ] );

// Custom product ordering
add_action( 'ep_admin_notices', [ $this, 'maybe_display_notice_about_product_ordering' ] );
Expand Down Expand Up @@ -995,4 +995,19 @@ public function get_orderby_meta_mapping( $meta_key ) : string {

return 'date';
}

/**
* Add taxonomies that should be woocommerce attributes.
*
* @param array $attribute_taxonomies Attribute taxonomies.
* @return array $attribute_taxonomies Attribute taxonomies.
*/
public function add_taxonomy_attributes( array $attribute_taxonomies ) : array {
$all_attr_taxonomies = wc_get_attribute_taxonomies();

foreach ( $all_attr_taxonomies as $attr_taxonomy ) {
$attribute_taxonomies[ $attr_taxonomy->attribute_name ] = wc_attribute_taxonomy_name( $attr_taxonomy->attribute_name );
}
return $attribute_taxonomies;
}
}
32 changes: 32 additions & 0 deletions tests/php/features/TestFacetTypeTaxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,36 @@ public function testAddQueryParams() {

$this->assertSame( $new_query_params, $expected_query_params );
}

/**
* Test the ep_facet_tax_special_slug_taxonomies filter runs.
*
* @since 4.7.0
* @return void
*/
public function test_ep_facet_special_slug_taxonomies_filter() {
add_filter(
'ep_facet_tax_special_slug_taxonomies',
function( $special_slug_taxonomies ) {
$special_slug_taxonomies['testmyfilter'] = 'testmyfilterchangedfilter';
return $special_slug_taxonomies;
},
99999
);

$facet_feature = Features::factory()->get_registered_feature( 'facets' );
$facet_type = $facet_feature->types['taxonomy'];

parse_str( 'ep_filter_taxonomy=dolor,amet&ep_filter_testmyfilter=dolor,amet', $_GET );

$query_filters = $facet_type->add_query_filters( [] );

$sample_test[0]['term']['terms.taxonomy.slug'] = 'dolor';
$sample_test[1]['term']['terms.taxonomy.slug'] = 'amet';
$sample_test[2]['term']['terms.testmyfilterchangedfilter.slug'] = 'dolor';
$sample_test[3]['term']['terms.testmyfilterchangedfilter.slug'] = 'amet';

$this->assertEquals( $sample_test, $query_filters );
$this->assertGreaterThanOrEqual( 1, did_filter( 'ep_facet_tax_special_slug_taxonomies' ) );
felipeelia marked this conversation as resolved.
Show resolved Hide resolved
}
}
38 changes: 38 additions & 0 deletions tests/php/features/WooCommerce/TestWooCommerceProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -1076,4 +1076,42 @@ public function testOrderbyMetaMappingFilter() {

$this->assertSame( $this->products->get_orderby_meta_mapping( 'custom_parameter' ), 'meta.custom_parameter.long' );
}

/**
* Test add_taxonomy_attributes.
*
* @group woocommerce
* @group woocommerce-products
*/
public function test_add_taxonomy_attributes() {
$attributes = wc_get_attribute_taxonomies();

$slugs = wp_list_pluck( $attributes, 'attribute_name' );

if ( ! in_array( 'my_color', $slugs, true ) ) {

$args = array(
'slug' => 'my_color',
'name' => 'My color',
'type' => 'select',
'orderby' => 'menu_order',
'has_archives' => false,
);

wc_create_attribute( $args );
}

$facet_feature = ElasticPress\Features::factory()->get_registered_feature( 'facets' );
$facet_type = $facet_feature->types['taxonomy'];

parse_str( 'ep_filter_taxonomy=dolor,amet&ep_filter_my_color=red', $_GET );

$query_filters = $facet_type->add_query_filters( [] );

$sample_test[0]['term']['terms.taxonomy.slug'] = 'dolor';
$sample_test[1]['term']['terms.taxonomy.slug'] = 'amet';
$sample_test[2]['term']['terms.pa_my_color.slug'] = 'red';

$this->assertEquals( $sample_test, $query_filters );
}
}