From 049876ac8f371df754e71f9f769ebf921dba5d3f Mon Sep 17 00:00:00 2001 From: Steve Grunwell Date: Fri, 16 Mar 2018 18:54:17 +0000 Subject: [PATCH] Add the "woocommerce_install_get_tables" filter to WC_Install::get_tables() This commit adds a new filter, "woocommerce_install_get_tables", to the WC_Install::get_tables() method, enabling WooCommerce extensions to register new, WooCommerce-specific tables, ensuring these tables can be cleaned up automatically should WooCommerce be uninstalled (useful for things like custom table data stores). Nothing gets added by default, but this provides an integration point for plugins like WooCommerce Custom Orders Table. --- includes/class-wc-install.php | 9 +++++++++ tests/unit-tests/util/install.php | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/includes/class-wc-install.php b/includes/class-wc-install.php index 16fdb1a4d1a..e9e059463b5 100644 --- a/includes/class-wc-install.php +++ b/includes/class-wc-install.php @@ -780,6 +780,15 @@ CREATE TABLE {$wpdb->prefix}woocommerce_termmeta ( $tables[] = "{$wpdb->prefix}woocommerce_termmeta"; } + /** + * Filter the list of known WooCommerce tables. + * + * If WooCommerce plugins need to add new tables, they can inject them here. + * + * @param array $tables An array of WooCommerce-specific database table names. + */ + $tables = apply_filters( 'woocommerce_install_get_tables', $tables ); + return $tables; } diff --git a/tests/unit-tests/util/install.php b/tests/unit-tests/util/install.php index f323b26de7e..4ecb176fd40 100644 --- a/tests/unit-tests/util/install.php +++ b/tests/unit-tests/util/install.php @@ -125,4 +125,25 @@ class WC_Tests_Install extends WC_Unit_Test_Case { $this->assertEquals( $tables, WC_Install::get_tables() ); } + + /** + * Test - get tables should apply the woocommerce_install_get_tables filter. + */ + public function test_get_tables_enables_filter() { + $default = WC_Install::get_tables(); + $added = $this->append_table_to_get_tables( array() ); + + add_filter( 'woocommerce_install_get_tables', array( $this, 'append_table_to_get_tables' ) ); + + $this->assertEquals( $added, array_values( array_diff( WC_Install::get_tables(), $default ) ) ); + } + + /** + * Filter callback for test_get_tables_enables_filter(). + */ + public function append_table_to_get_tables( $tables ) { + $tables[] = 'some_table_name'; + + return $tables; + } }