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 HPOS compatibility notice for WooCommerce Orders #3861

Merged
merged 7 commits into from
Mar 19, 2024
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
45 changes: 44 additions & 1 deletion includes/classes/Feature/WooCommerce/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function setup() {
add_action( 'parse_query', [ $this, 'maybe_hook_woocommerce_search_fields' ], 1 );
add_action( 'parse_query', [ $this, 'search_order' ], 11 );
add_action( 'pre_get_posts', [ $this, 'translate_args' ], 11, 1 );
add_filter( 'ep_admin_notices', [ $this, 'hpos_compatibility_notice' ] );
}

/**
Expand Down Expand Up @@ -335,6 +336,48 @@ public function get_supported_post_types() : array {
return $supported_post_types;
}

/**
* Display a notice if WooCommerce Orders are not compatible with ElasticPress
*
* If the user has WooCommerce, Protected Content, and HPOS enabled, orders will not go through ElasticPress.
*
* @param array $notices Current EP notices
* @return array
*/
public function hpos_compatibility_notice( array $notices ) : array {
$current_screen = \get_current_screen();
if ( empty( $current_screen->id ) || 'woocommerce_page_wc-orders' !== $current_screen->id ) {
return $notices;
}

if ( \ElasticPress\Utils\get_option( 'ep_hide_wc_orders_incompatible_notice' ) ) {
return $notices;
}

$protected_content = \ElasticPress\Features::factory()->get_registered_feature( 'protected_content' );
if ( ! $protected_content->is_active() ) {
return $notices;
}

if (
! class_exists( '\Automattic\WooCommerce\Utilities\OrderUtil' )
|| ! method_exists( '\Automattic\WooCommerce\Utilities\OrderUtil', 'custom_orders_table_usage_is_enabled' ) ) {
return $notices;
}

if ( ! \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled() ) {
return $notices;
}

$notices['wc_orders_incompatible'] = [
'html' => esc_html__( "Although the WooCommerce and Protected Content features are enabled, ElasticPress will not integrate with the WooCommerce Orders list if WooCommerce's High-performance order storage is enabled.", 'elasticpress' ),
'type' => 'warning',
'dismiss' => true,
];

return $notices;
}

/**
* If the query has a search term, add the order fields that need to be searched.
*
Expand Down Expand Up @@ -477,7 +520,7 @@ public function __call( $method_name, $arguments ) {
"\ElasticPress\Features::factory()->get_registered_feature( 'woocommerce' )->orders_autosuggest->{$method_name}()" // phpcs:ignore
);

if ( $this->woocommerce->is_orders_autosuggest_enabled() && method_exists( $this->woocommerce->orders_autosuggest, $method_name ) ) {
if ( $this->woocommerce->orders_autosuggest->is_enabled() && method_exists( $this->woocommerce->orders_autosuggest, $method_name ) ) {
call_user_func_array( [ $this->woocommerce->orders_autosuggest, $method_name ], $arguments );
}
}
Expand Down
144 changes: 140 additions & 4 deletions includes/classes/Feature/WooCommerce/OrdersAutosuggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace ElasticPress\Feature\WooCommerce;

use ElasticPress\Elasticsearch;
use ElasticPress\Features;
use ElasticPress\Indexables;
use ElasticPress\REST;
use ElasticPress\Utils;
Expand Down Expand Up @@ -36,12 +37,23 @@ class OrdersAutosuggest {
protected $search_template;

/**
* Initialize feature.
* WooCommerce feature object instance
*
* @return void
* @since 5.1.0
* @var WooCommerce
*/
public function __construct() {
$this->index = Indexables::factory()->get( 'post' )->get_index_name();
protected $woocommerce;

/**
* Class constructor
*
* @param WooCommerce|null $woocommerce WooCommerce feature object instance
*/
public function __construct( WooCommerce $woocommerce = null ) {
$this->index = Indexables::factory()->get( 'post' )->get_index_name();
$this->woocommerce = $woocommerce ?
$woocommerce :
Features::factory()->get_registered_feature( 'woocommerce' );
}

/**
Expand All @@ -50,6 +62,13 @@ public function __construct() {
* @return void
*/
public function setup() {
add_filter( 'ep_woocommerce_settings_schema', [ $this, 'add_settings_schema' ] );

// Orders Autosuggest feature.
if ( ! $this->is_enabled() ) {
return;
}

add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_assets' ] );
add_filter( 'ep_after_update_feature', [ $this, 'after_update_feature' ], 10, 3 );
add_filter( 'ep_after_sync_index', [ $this, 'epio_save_search_template' ] );
Expand Down Expand Up @@ -548,4 +567,121 @@ public function maybe_set_posts_where( $where, $query ) {

return $where;
}

/**
* Whether orders autosuggest is available or not
*
* @since 5.1.0
* @return boolean
*/
public function is_available() : bool {
/**
* Whether the autosuggest feature is available for non
* ElasticPress.io customers.
*
* @since 4.5.0
* @hook ep_woocommerce_orders_autosuggest_available
* @param {boolean} $available Whether the feature is available.
*/
return apply_filters( 'ep_woocommerce_orders_autosuggest_available', Utils\is_epio() && $this->is_hpos_compatible() );
}

/**
* Whether orders autosuggest is enabled or not
*
* @since 5.1.0
* @return boolean
*/
public function is_enabled() : bool {
return $this->is_available() && '1' === $this->woocommerce->get_setting( 'orders' );
}

/**
* Whether the current setup is compatible with WooCommerce's HPOS or not
*
* @since 5.1.0
* @return boolean
*/
public function is_hpos_compatible() {
if (
! class_exists( '\Automattic\WooCommerce\Utilities\OrderUtil' )
|| ! method_exists( '\Automattic\WooCommerce\Utilities\OrderUtil', 'custom_orders_table_usage_is_enabled' ) ) {
return true;
}

if ( ! \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled() ) {
return true;
}

if ( wc_get_container()->get( \Automattic\WooCommerce\Internal\DataStores\Orders\DataSynchronizer::class )->data_sync_is_enabled() ) {
return true;
}

return false;
}

/**
* Add the orders autosuggest field to the WooCommerce feature schema
*
* @since 5.1.0
* @param array $settings_schema Current settings schema
* @return array
*/
public function add_settings_schema( array $settings_schema ) : array {
$available = $this->is_available();

$settings_schema[] = [
'default' => '0',
'disabled' => ! $available,
'help' => $this->get_setting_help_message(),
'key' => 'orders',
'label' => __( 'Show suggestions when searching for Orders', 'elasticpress' ),
'requires_sync' => true,
'type' => 'checkbox',
];

return $settings_schema;
}

/**
* Return the help message for the setting schema field
*
* @since 5.1.0
* @return string
*/
protected function get_setting_help_message() : string {
$available = $this->is_available();

$epio_autosuggest_kb_link = 'https://elasticpress.zendesk.com/hc/en-us/articles/13374461690381-Configuring-ElasticPress-io-Order-Autosuggest';

if ( $available ) {
/* translators: 1: <a> tag (ElasticPress.io); 2. </a>; 3: <a> tag (KB article); 4. </a>; */
$message = __( 'You are directly connected to %1$sElasticPress.io%2$s! Enable autosuggest for Orders to enhance Dashboard results and quickly find WooCommerce Orders. %3$sLearn More%4$s.', 'elasticpress' );

return sprintf(
wp_kses( $message, 'ep-html' ),
'<a href="https://elasticpress.io/" target="_blank">',
'</a>',
'<a href="' . esc_url( $epio_autosuggest_kb_link ) . '" target="_blank">',
'</a>'
);
}

if ( ! $this->is_hpos_compatible() ) {
return esc_html__( 'Currently, autosuggest for orders is only available if WooCommerce order data storage is set in legacy or compatibility mode.', 'elasticpress' );
}

/* translators: 1: <a> tag (ElasticPress.io); 2. </a>; 3: <a> tag (KB article); 4. </a>; */
$message = __( 'Due to the sensitive nature of orders, this autosuggest feature is available only to %1$sElasticPress.io%2$s customers. %3$sLearn More%4$s.', 'elasticpress' );

$message = sprintf(
wp_kses( $message, 'ep-html' ),
'<a href="https://elasticpress.io/" target="_blank">',
'</a>',
'<a href="' . esc_url( $epio_autosuggest_kb_link ) . '" target="_blank">',
'</a>'
);

return $message;
}
}
Loading
Loading