The installation code should not run on each plugin load, only when activated.

This commit is contained in:
Peter Fabian 2018-09-21 12:14:08 +02:00
parent aa8a09adbe
commit 5aa3aae58a
2 changed files with 29 additions and 10 deletions

View File

@ -26,12 +26,9 @@ class WC_Admin_Api_Init {
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
add_filter( 'rest_endpoints', array( 'WC_Admin_Api_Init', 'filter_rest_endpoints' ), 10, 1 );
add_filter( 'woocommerce_debug_tools', array( 'WC_Admin_Api_Init', 'add_regenerate_tool' ) );
// Initialize report classes.
add_action( 'woocommerce_after_register_post_type', array( 'WC_Admin_Api_Init', 'orders_data_store_init' ), 20 );
add_action( 'woocommerce_after_register_post_type', array( 'WC_Admin_Api_Init', 'order_product_lookup_store_init' ), 20 );
// Create tables.
self::create_db_tables();
// Initialize Orders data store class's static vars.
add_action( 'woocommerce_after_register_post_type', array( 'WC_Admin_Api_Init', 'orders_data_store_init' ), 20 );
}
/**
@ -166,7 +163,7 @@ class WC_Admin_Api_Init {
}
/**
* Init orders product looup store.
* Init orders product lookup store.
*
* @param WC_Background_Updater|null $updater Updater instance.
* @return bool
@ -341,7 +338,11 @@ class WC_Admin_Api_Init {
* Install plugin.
*/
public static function install() {
// Create tables.
self::create_db_tables();
// Initialize report tables.
add_action( 'woocommerce_after_register_post_type', array( 'WC_Admin_Api_Init', 'order_product_lookup_store_init' ), 20 );
}
}

View File

@ -20,6 +20,10 @@ if ( ! defined( 'WC_ADMIN_ABSPATH' ) ) {
define( 'WC_ADMIN_ABSPATH', dirname( __FILE__ ) );
}
if ( ! defined( 'WC_ADMIN_PLUGIN_FILE' ) ) {
define( 'WC_ADMIN_PLUGIN_FILE', __FILE__ );
}
/**
* Notify users of the plugin requirements
*/
@ -32,14 +36,28 @@ function wc_admin_plugins_notice() {
printf( '<div class="error"><p>%s</p></div>', $message ); /* WPCS: xss ok. */
}
function dependencies_satisfied() {
return ( defined( 'GUTENBERG_DEVELOPMENT_MODE' ) || defined( 'GUTENBERG_VERSION' ) )
&& class_exists( 'WooCommerce' );
}
function activate_wc_admin_plugin() {
if ( ! dependencies_satisfied() ) {
return;
}
// Initialize the WC API extensions.
require_once dirname( __FILE__ ) . '/includes/class-wc-admin-api-init.php';
WC_Admin_Api_Init::install();
}
register_activation_hook( WC_ADMIN_PLUGIN_FILE, 'activate_wc_admin_plugin' );
/**
* Set up the plugin, only if we can detect both Gutenberg and WooCommerce
*/
function wc_admin_plugins_loaded() {
if (
! ( defined( 'GUTENBERG_DEVELOPMENT_MODE' ) || defined( 'GUTENBERG_VERSION' ) ) ||
! class_exists( 'WooCommerce' )
) {
if ( ! dependencies_satisfied() ) {
add_action( 'admin_notices', 'wc_admin_plugins_notice' );
return;
}