Move the UI to set/unset product attribute lookup table usage to settings.

The UI was previously in the tools page, it's now a setting under
products - advanced.
This commit is contained in:
Nestor Soriano 2021-06-30 15:14:14 +02:00
parent cf7d96d867
commit b97e792f40
No known key found for this signature in database
GPG Key ID: 08110F3518C12CAD
3 changed files with 65 additions and 73 deletions

View File

@ -9,7 +9,6 @@ use Automattic\WooCommerce\Internal\DependencyManagement\AbstractServiceProvider
use Automattic\WooCommerce\Internal\ProductAttributesLookup\DataRegenerator;
use Automattic\WooCommerce\Internal\ProductAttributesLookup\Filterer;
use Automattic\WooCommerce\Internal\ProductAttributesLookup\LookupDataStore;
use Automattic\WooCommerce\Proxies\LegacyProxy;
/**
* Service provider for the ProductAttributesLookupServiceProvider namespace.
@ -32,7 +31,7 @@ class ProductAttributesLookupServiceProvider extends AbstractServiceProvider {
*/
public function register() {
$this->share( DataRegenerator::class )->addArgument( LookupDataStore::class );
$this->share( Filterer::class )->addArgument( LookupDataStore::class )->addArgument( LegacyProxy::class );
$this->share( Filterer::class )->addArgument( LookupDataStore::class );
$this->share( LookupDataStore::class );
}
}

View File

@ -122,15 +122,6 @@ class DataRegenerator {
}
}
/**
* Tells if a regeneration is already in progress.
*
* @return bool True if a regeneration is already in progress.
*/
public function regeneration_is_in_progress() {
return ! is_null( get_option( 'woocommerce_attribute_lookup__last_products_page_processed', null ) );
}
/**
* Delete all the existing data related to the lookup table, including the table itself.
*
@ -144,6 +135,7 @@ class DataRegenerator {
delete_option( 'woocommerce_attribute_lookup__enabled' );
delete_option( 'woocommerce_attribute_lookup__last_product_id_to_process' );
delete_option( 'woocommerce_attribute_lookup__last_products_page_processed' );
$this->data_store->unset_regeneration_in_progress_flag();
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$wpdb->query( 'DROP TABLE IF EXISTS ' . $this->lookup_table_name );
@ -190,6 +182,7 @@ CREATE TABLE ' . $this->lookup_table_name . '(
return false;
}
$this->data_store->set_regeneration_in_progress_flag();
update_option( 'woocommerce_attribute_lookup__last_product_id_to_process', current( $last_existing_product_id ) );
update_option( 'woocommerce_attribute_lookup__last_products_page_processed', 0 );
@ -201,7 +194,7 @@ CREATE TABLE ' . $this->lookup_table_name . '(
* schedules the next step if necessary.
*/
private function run_regeneration_step_callback() {
if ( ! $this->regeneration_is_in_progress() ) {
if ( ! $this->data_store->regeneration_is_in_progress() ) {
return;
}
@ -269,19 +262,7 @@ CREATE TABLE ' . $this->lookup_table_name . '(
delete_option( 'woocommerce_attribute_lookup__last_product_id_to_process' );
delete_option( 'woocommerce_attribute_lookup__last_products_page_processed' );
update_option( 'woocommerce_attribute_lookup__enabled', 'no' );
}
/**
* Check if the lookup table exists in the database.
*
* @return bool True if the lookup table exists in the database.
*/
private function lookup_table_exists() {
global $wpdb;
$query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $this->lookup_table_name ) );
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
return $this->lookup_table_name === $wpdb->get_var( $query );
$this->data_store->unset_regeneration_in_progress_flag();
}
/**
@ -295,8 +276,8 @@ CREATE TABLE ' . $this->lookup_table_name . '(
return $tools_array;
}
$lookup_table_exists = $this->lookup_table_exists();
$generation_is_in_progress = $this->regeneration_is_in_progress();
$lookup_table_exists = $this->data_store->check_lookup_table_exists();
$generation_is_in_progress = $this->data_store->regeneration_is_in_progress();
// Regenerate table.
@ -355,35 +336,6 @@ CREATE TABLE ' . $this->lookup_table_name . '(
);
}
if ( $lookup_table_exists && ! $generation_is_in_progress ) {
// Enable or disable table usage.
if ( 'yes' === get_option( 'woocommerce_attribute_lookup__enabled' ) ) {
$tools_array['disable_product_attributes_lookup_table_usage'] = array(
'name' => __( 'Disable the product attributes lookup table usage', 'woocommerce' ),
'desc' => __( 'The product attributes lookup table usage is currently enabled, use this tool to disable it.', 'woocommerce' ),
'button' => __( 'Disable', 'woocommerce' ),
'requires_refresh' => true,
'callback' => function () {
$this->enable_or_disable_lookup_table_usage( false );
return __( 'Product attributes lookup table usage has been disabled.', 'woocommerce' );
},
);
} else {
$tools_array['enable_product_attributes_lookup_table_usage'] = array(
'name' => __( 'Enable the product attributes lookup table usage', 'woocommerce' ),
'desc' => __( 'The product attributes lookup table usage is currently disabled, use this tool to enable it.', 'woocommerce' ),
'button' => __( 'Enable', 'woocommerce' ),
'requires_refresh' => true,
'callback' => function () {
$this->enable_or_disable_lookup_table_usage( true );
return __( 'Product attributes lookup table usage has been enabled.', 'woocommerce' );
},
);
}
}
return $tools_array;
}
@ -393,7 +345,7 @@ CREATE TABLE ' . $this->lookup_table_name . '(
* @throws \Exception The regeneration is already in progress.
*/
private function initiate_regeneration_from_tools_page() {
if ( $this->regeneration_is_in_progress() ) {
if ( $this->data_store->regeneration_is_in_progress() ) {
throw new \Exception( 'Product attributes lookup table is already regenerating.' );
}
@ -407,7 +359,7 @@ CREATE TABLE ' . $this->lookup_table_name . '(
* @throws \Exception A lookup table regeneration is currently in progress.
*/
private function enable_or_disable_lookup_table_usage( $enable ) {
if ( $this->regeneration_is_in_progress() ) {
if ( $this->data_store->regeneration_is_in_progress() ) {
throw new \Exception( "Can't enable or disable the attributes lookup table usage while it's regenerating." );
}

View File

@ -76,7 +76,7 @@ class LookupDataStore {
add_filter(
'woocommerce_get_sections_products',
function ( $products ) {
if ( $this->is_feature_visible() ) {
if ( $this->is_feature_visible() && $this->check_lookup_table_exists() ) {
$products['advanced'] = __( 'Advanced', 'woocommerce' );
}
return $products;
@ -88,21 +88,39 @@ class LookupDataStore {
add_filter(
'woocommerce_get_settings_products',
function ( $settings, $section_id ) {
if ( 'advanced' === $section_id && $this->is_feature_visible() ) {
$settings[] =
array(
'title' => __( 'Product attributes lookup table', 'woocommerce' ),
'type' => 'title',
if ( 'advanced' === $section_id && $this->is_feature_visible() && $this->check_lookup_table_exists() ) {
$title_item = array(
'title' => __( 'Product attributes lookup table', 'woocommerce' ),
'type' => 'title',
);
$regeneration_is_in_progress = $this->regeneration_is_in_progress();
if ( $regeneration_is_in_progress ) {
$title_item['desc'] = __( 'These settings are not available while the lookup table regeneration is in progress.', 'woocommerce' );
}
$settings[] = $title_item;
if ( ! $regeneration_is_in_progress ) {
$settings[] = array(
'title' => __( 'Enable table usage', 'woocommerce' ),
'desc' => __( 'Use the product attributes lookup table for catalog filtering.', 'woocommerce' ),
'id' => 'woocommerce_attribute_lookup__enable',
'default' => 'no',
'type' => 'checkbox',
'checkboxgroup' => 'start',
);
$settings[] = array(
'title' => __( 'Direct updates', 'woocommerce' ),
'desc' => __( 'Update the table directly upon product changes, instead of scheduling a deferred update.', 'woocommerce' ),
'id' => 'woocommerce_attribute_lookup__direct_updates',
'default' => 'no',
'type' => 'checkbox',
'checkboxgroup' => 'start',
);
$settings[] = array(
'title' => __( 'Direct updates', 'woocommerce' ),
'desc' => __( 'Update the table directly upon product changes, instead of scheduling a deferred update.', 'woocommerce' ),
'id' => 'woocommerce_attribute_lookup__direct_updates',
'default' => 'no',
'type' => 'checkbox',
'checkboxgroup' => 'start',
);
}
$settings[] = array( 'type' => 'sectionend' );
}
@ -132,7 +150,7 @@ AND table_name = %s;',
);
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
return 0 !== $wpdb->get_var( $query );
return (bool) $wpdb->get_var( $query );
}
/**
@ -655,4 +673,27 @@ AND table_name = %s;',
);
// phpcs:enable WordPress.DB.PreparedSQL.NotPrepared
}
/**
* Tells if a lookup table regeneration is currently in progress.
*
* @return bool True if a lookup table regeneration is already in progress.
*/
public function regeneration_is_in_progress() {
return 'yes' === get_option( 'woocommerce_attribute_lookup__regeneration_in_progress', null );
}
/**
* Set a permanent flag (via option) indicating that the lookup table regeneration is in process.
*/
public function set_regeneration_in_progress_flag() {
update_option( 'woocommerce_attribute_lookup__regeneration_in_progress', 'yes' );
}
/**
* Remove the flag indicating that the lookup table regeneration is in process.
*/
public function unset_regeneration_in_progress_flag() {
delete_option( 'woocommerce_attribute_lookup__regeneration_in_progress' );
}
}