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

WooCommerce weight by date #3462

Merged
merged 13 commits into from
Jun 7, 2023
43 changes: 35 additions & 8 deletions includes/classes/Feature/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

namespace ElasticPress\Feature\Search;

use ElasticPress\Feature as Feature;
use ElasticPress\Indexables as Indexables;
use ElasticPress\Utils as Utils;
use ElasticPress\Feature;
use ElasticPress\Features;
use ElasticPress\Indexables;
use ElasticPress\Utils;

/**
* Search feature class
Expand Down Expand Up @@ -428,19 +429,32 @@ public function filter_query_post_type_for_search( $post_type, $query ) {
/**
* Returns true/false if decaying is/isn't enabled
*
* @param array $args WP_Query args
*
* @return bool
*/
public function is_decaying_enabled() {
public function is_decaying_enabled( $args = [] ) {
$settings = $this->get_settings();

$settings = wp_parse_args(
$settings,
[
'decaying_enabled' => true,
]
);

return (bool) $settings['decaying_enabled'];
$is_decaying_enabled = (bool) $settings['decaying_enabled'];

/**
* Filter to modify decaying
*
* @hook ep_is_decaying_enabled
* @since 4.6.0
* @param {bool} $is_decaying_enabled Whether decay by date is enabled or not
* @param {array} $settings Settings
* @param {array} $args WP_Query args
* @return {bool} Decaying
*/
return apply_filters( 'ep_is_decaying_enabled', $is_decaying_enabled, $settings, $args );
}

/**
Expand All @@ -455,9 +469,11 @@ public function weight_recent( $formatted_args, $args ) {
if ( empty( $args['s'] ) ) {
return $formatted_args;
}
if ( ! $this->is_decaying_enabled() ) {

if ( ! $this->is_decaying_enabled( $args ) ) {
return $formatted_args;
}

/**
* Filter search date weighting scale
*
Expand All @@ -468,6 +484,7 @@ public function weight_recent( $formatted_args, $args ) {
* @return {string} New decay function
*/
$decay_function = apply_filters( 'epwr_decay_function', 'exp', $formatted_args, $args );

/**
* Filter search date weighting field
*
Expand Down Expand Up @@ -629,7 +646,17 @@ public function output_feature_box_settings() {
<div class="field-name status"><?php esc_html_e( 'Weight results by date', 'elasticpress' ); ?></div>
<div class="input-wrap">
<label><input name="settings[decaying_enabled]" type="radio" <?php checked( (bool) $settings['decaying_enabled'] ); ?> value="1"><?php esc_html_e( 'Enabled', 'elasticpress' ); ?></label><br>
<label><input name="settings[decaying_enabled]" type="radio" <?php checked( ! (bool) $settings['decaying_enabled'] ); ?> value="0"><?php esc_html_e( 'Disabled', 'elasticpress' ); ?></label>
<label><input name="settings[decaying_enabled]" type="radio" <?php checked( ! (bool) $settings['decaying_enabled'] ); ?> value="0"><?php esc_html_e( 'Disabled', 'elasticpress' ); ?></label><br>
<?php
/**
* Fires after the default Weight results by date settings
*
* @since 4.6.0
* @hook ep_weight_settings_after_search
* @param {array} $settings settings array
*/
do_action( 'ep_weight_settings_after_search', $settings );
?>
</div>
</div>
<div class="field">
Expand Down
3 changes: 2 additions & 1 deletion includes/classes/Feature/Search/Weighting.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
namespace ElasticPress\Feature\Search;

use ElasticPress\Features;
use ElasticPress\Feature;
use ElasticPress\Indexable\Post\Post;
use ElasticPress\Utils as Utils;
use ElasticPress\Utils;

/**
* Controls search weighting and search fields dashboard
Expand Down
50 changes: 50 additions & 0 deletions includes/classes/Feature/WooCommerce/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,11 @@ public function setup() {
if ( $this->is_orders_autosuggest_enabled() ) {
$this->orders->setup();
}

// Add WooCommerce Settings for Weight results by date
add_action( 'ep_weight_settings_after_search', [ $this, 'add_weight_settings_search' ] );
// Modify decaying based on WooCommerce Settings
add_filter( 'ep_is_decaying_enabled', [ $this, 'maybe_disable_decaying' ], 10, 3 );
}

/**
Expand Down Expand Up @@ -1372,4 +1377,49 @@ public function is_orders_autosuggest_available() : bool {
public function is_orders_autosuggest_enabled() : bool {
return $this->is_orders_autosuggest_available() && '1' === $this->get_setting( 'orders' );
}

/**
* Add weight by date settings related to WooCommerce
*
* @since 4.6.0
* @param array $settings Current settings.
*/
public function add_weight_settings_search( $settings ) {
?>
<label><input name="settings[decaying_enabled]" type="radio" <?php checked( $settings['decaying_enabled'], 'disabled_only_products' ); ?> value="disabled_only_products"><?php esc_html_e( 'Disabled for product only queries', 'elasticpress' ); ?></label><br>
<label><input name="settings[decaying_enabled]" type="radio" <?php checked( $settings['decaying_enabled'], 'disabled_includes_products' ); ?> value="disabled_includes_products"><?php esc_html_e( 'Disabled for any query that includes products', 'elasticpress' ); ?></label>
<?php
}

/**
* Conditionally disable decaying by date based on WooCommerce Decay settings.
*
* @since 4.6.0
* @param bool $is_decaying_enabled Whether decay by date is enabled or not
* @param array $settings Settings
* @param array $args WP_Query args
* @return bool
*/
public function maybe_disable_decaying( $is_decaying_enabled, $settings, $args ) {
if ( ! in_array( $settings['decaying_enabled'], [ 'disabled_only_products', 'disabled_includes_products' ], true ) ) {
return $is_decaying_enabled;
}

if ( ! isset( $args['post_type'] ) || ! in_array( 'product', (array) $args['post_type'], true ) ) {
return $is_decaying_enabled;
}

$post_types = (array) $args['post_type'];

if ( 'disabled_only_products' === $settings['decaying_enabled'] && count( $post_types ) > 1 ) {
return $is_decaying_enabled;
}

if ( 'disabled_includes_products' === $settings['decaying_enabled'] && ! in_array( 'product', $post_types, true ) ) {
return $is_decaying_enabled;
}

return false;
}

}
47 changes: 11 additions & 36 deletions tests/php/features/TestSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,16 @@ public function testDecayingEnabled() {
);

$this->assertTrue( isset( $this->fired_actions['ep_formatted_args'] ) );
$this->assertTrue(
isset(
$this->fired_actions['ep_formatted_args']['query'],
$this->fired_actions['ep_formatted_args']['query']['function_score'],
$this->fired_actions['ep_formatted_args']['query']['function_score']['functions'],
$this->fired_actions['ep_formatted_args']['query']['function_score']['functions'][0],
$this->fired_actions['ep_formatted_args']['query']['function_score']['functions'][0]['exp'],
$this->fired_actions['ep_formatted_args']['query']['function_score']['functions'][0]['exp']['post_date_gmt'],
$this->fired_actions['ep_formatted_args']['query']['function_score']['functions'][0]['exp']['post_date_gmt']['scale'],
$this->fired_actions['ep_formatted_args']['query']['function_score']['functions'][0]['exp']['post_date_gmt']['decay'],
$this->fired_actions['ep_formatted_args']['query']['function_score']['functions'][0]['exp']['post_date_gmt']['offset']
)
);
$this->assertDecayEnabled( $this->fired_actions['ep_formatted_args']['query'] );

/**
* Test the `ep_is_decaying_enabled` filter
*/
add_filter( 'ep_is_decaying_enabled', '__return_true' );
$this->assertTrue( ElasticPress\Features::factory()->get_registered_feature( 'search' )->is_decaying_enabled() );
add_filter( 'ep_is_decaying_enabled', '__return_false' );
$this->assertFalse( ElasticPress\Features::factory()->get_registered_feature( 'search' )->is_decaying_enabled() );

}

/**
Expand Down Expand Up @@ -207,18 +204,7 @@ public function testDecayingDisabled() {
);

$this->assertTrue( isset( $this->fired_actions['ep_formatted_args'] ) );
$this->assertTrue(
! isset(
$this->fired_actions['ep_formatted_args']['query']['function_score'],
$this->fired_actions['ep_formatted_args']['query']['function_score']['functions'],
$this->fired_actions['ep_formatted_args']['query']['function_score']['functions'][0],
$this->fired_actions['ep_formatted_args']['query']['function_score']['functions'][0]['exp'],
$this->fired_actions['ep_formatted_args']['query']['function_score']['functions'][0]['exp']['post_date_gmt'],
$this->fired_actions['ep_formatted_args']['query']['function_score']['functions'][0]['exp']['post_date_gmt']['scale'],
$this->fired_actions['ep_formatted_args']['query']['function_score']['functions'][0]['exp']['post_date_gmt']['decay'],
$this->fired_actions['ep_formatted_args']['query']['function_score']['functions'][0]['exp']['post_date_gmt']['offset']
)
);
$this->assertDecayDisabled( $this->fired_actions['ep_formatted_args']['query'] );
$this->assertTrue(
isset(
$this->fired_actions['ep_formatted_args']['query']['bool'],
Expand All @@ -227,17 +213,6 @@ public function testDecayingDisabled() {
);
}

/**
* Catch ES query args.
*
* @group search
* @param array $args ES query args.
*/
public function catch_ep_formatted_args( $args ) {
$this->fired_actions['ep_formatted_args'] = $args;
return $args;
}

/**
* Test allowed tags for highlighting sub-feature.
*
Expand Down
80 changes: 80 additions & 0 deletions tests/php/features/TestWooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -1187,4 +1187,84 @@ public function testIsOrdersAutosuggestEnabled() {
remove_filter( 'ep_woocommerce_orders_autosuggest_available', '__return_true' );
$this->assertFalse( $woocommerce_feature->is_orders_autosuggest_enabled() );
}

/**
* Test if decaying is disabled on products.
*
* @since 4.6.0
* @dataProvider decayingDisabledOnProductsProvider
* @group woocommerce
*
* @param string $setting Value for `decaying_enabled`
* @param array|string $post_type Post types to be queried
* @param string $assert Assert method name (`assertDecayDisabled` or `assertDecayEnabled`)
*/
public function testDecayingDisabledOnProducts( $setting, $post_type, $assert ) {
ElasticPress\Features::factory()->activate_feature( 'woocommerce' );
ElasticPress\Features::factory()->setup_features();

// Test decaying for product query when disabled_only_products is enabled
ElasticPress\Features::factory()->update_feature(
'search',
[
'active' => true,
'decaying_enabled' => $setting,
]
);

$query = new \WP_Query();
$query_args = [
's' => 'test',
'post_type' => $post_type,
];
$formatted_args = \ElasticPress\Indexables::factory()->get( 'post' )->format_args( $query_args, $query );

$this->$assert( $formatted_args['query'] );
}

/**
* Data provider for the testDecayingDisabledOnProducts method.
*
* @since 4.6.0
* @return array
*/
public function decayingDisabledOnProductsProvider() : array {
return [
[
'disabled_only_products',
'product',
'assertDecayDisabled',
],
[
'disabled_only_products',
[ 'product' ],
'assertDecayDisabled',
],
[
'disabled_only_products',
[ 'product', 'post' ],
'assertDecayEnabled',
],
[
'disabled_includes_products',
'product',
'assertDecayDisabled',
],
[
'disabled_includes_products',
[ 'product' ],
'assertDecayDisabled',
],
[
'disabled_includes_products',
[ 'product', 'post' ],
'assertDecayDisabled',
],
[
'disabled_includes_products',
[ 'post', 'page' ],
'assertDecayEnabled',
],
];
}
}
62 changes: 62 additions & 0 deletions tests/php/includes/classes/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,66 @@ protected function setup_factory() {
$this->ep_factory->category = new TermFactory( $this, 'category' );
$this->ep_factory->product = new ProductFactory();
}

/**
* Catch ES query args.
*
* @param array $args ES query args.
*/
public function catch_ep_formatted_args( $args ) {
$this->fired_actions['ep_formatted_args'] = $args;
return $args;
}

/**
* Assert function to check if Decay is enabled
*
* @param array $query ES query
*/
public function assertDecayEnabled( $query ) {
$this->assertTrue(
isset(
$query['function_score'],
$query['function_score']['functions'],
$query['function_score']['functions'][0],
$query['function_score']['functions'][0]['exp'],
$query['function_score']['functions'][0]['exp']['post_date_gmt'],
$query['function_score']['functions'][0]['exp']['post_date_gmt']['scale'],
$query['function_score']['functions'][0]['exp']['post_date_gmt']['decay'],
$query['function_score']['functions'][0]['exp']['post_date_gmt']['offset']
)
);
$this->assertFalse(
isset(
$query['bool'],
$query['bool']['should']
)
);
}
/**
* Assert function to check if Decay is disabled
*
* @param array $query ES query
*/
public function assertDecayDisabled( $query ) {
$this->assertFalse(
isset(
$query,
$query['function_score'],
$query['function_score']['functions'],
$query['function_score']['functions'][0],
$query['function_score']['functions'][0]['exp'],
$query['function_score']['functions'][0]['exp']['post_date_gmt'],
$query['function_score']['functions'][0]['exp']['post_date_gmt']['scale'],
$query['function_score']['functions'][0]['exp']['post_date_gmt']['decay'],
$query['function_score']['functions'][0]['exp']['post_date_gmt']['offset']
)
);
$this->assertTrue(
isset(
$query['bool'],
$query['bool']['should']
)
);
}
}