From cf7d96d867cce6670dd8a1d1a3c21b990ef686a6 Mon Sep 17 00:00:00 2001 From: Nestor Soriano Date: Wed, 30 Jun 2021 10:38:20 +0200 Subject: [PATCH] Add a lookup data regeneration for single product mechanism. A new "Lookup data" metabox has been added to the product page with a "Renegerate" button that regenerates the attributes lookup data for that product. --- .../DataRegenerator.php | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/Internal/ProductAttributesLookup/DataRegenerator.php b/src/Internal/ProductAttributesLookup/DataRegenerator.php index 88f0539d799..b466832e849 100644 --- a/src/Internal/ProductAttributesLookup/DataRegenerator.php +++ b/src/Internal/ProductAttributesLookup/DataRegenerator.php @@ -6,6 +6,7 @@ namespace Automattic\WooCommerce\Internal\ProductAttributesLookup; use Automattic\WooCommerce\Internal\ProductAttributesLookup\LookupDataStore; +use Automattic\WooCommerce\Utilities\ArrayUtil; defined( 'ABSPATH' ) || exit; @@ -66,6 +67,23 @@ class DataRegenerator { $this->run_regeneration_step_callback(); } ); + + add_action( + 'add_meta_boxes', + function() { + $this->add_product_regeneration_metabox(); + }, + 999 + ); + + add_action( + 'save_post_product', + function( $product_id ) { + $this->on_save_product( $product_id ); + }, + 999, + 1 + ); } /** @@ -395,4 +413,60 @@ CREATE TABLE ' . $this->lookup_table_name . '( update_option( 'woocommerce_attribute_lookup__enabled', $enable ? 'yes' : 'no' ); } + + /** + * Add a metabox in the product page with a button to regenerate the product attributes lookup data for the product. + */ + private function add_product_regeneration_metabox() { + if ( ! $this->data_store->is_feature_visible() ) { + return; + } + + add_meta_box( + 'woocommerce-product-foobars', + __( 'Lookup data', 'woocommerce' ), + function() { + $this->metabox_output(); + }, + 'product', + 'side', + 'low' + ); + } + + /** + * HTML output for the lookup data regeneration metabox. + */ + private function metabox_output() { + // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped + wp_nonce_field( 'regenerate-attributes-lookup-data', '_wc_regenerate_attributes_lookup_data_nonce' ); + ?> +

+ + data_store->is_feature_visible() || 'regenerate-attributes-lookup-data' !== ArrayUtil::get_value_or_default( $_POST, 'woocommerce-product-lookup-action' ) ) { + return; + } + + if ( ! wc_get_product( $product_id ) ) { + return; + } + + $this->data_store->create_data_for_product( $product_id ); + } }