Add WC active validation (#35891)

This commit is contained in:
Roy Ho 2022-12-14 07:35:22 -08:00 committed by GitHub
parent 778cb130f2
commit d0a464520d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 97 additions and 48 deletions

View File

@ -28,14 +28,41 @@ require_once plugin_dir_path( __FILE__ ) . '/vendor/autoload_packages.php';
use {{slugPascalCase}}\Admin\Setup;
// phpcs:disable WordPress.Files.FileName
/**
* {{slugPascalCase}} class.
* WooCommerce fallback notice.
*
* @since {{version}}
*/
class {{slugPascalCase}} {
function {{slugSnakeCase}}_missing_wc_notice() {
/* translators: %s WC download URL link. */
echo '<div class="error"><p><strong>' . sprintf( esc_html__( '{{title}} requires WooCommerce to be installed and active. You can download %s here.', '{{slugSnakeCase}}' ), '<a href="https://woocommerce.com/" target="_blank">WooCommerce</a>' ) . '</strong></p></div>';
}
register_activation_hook( __FILE__, '{{slugSnakeCase}}_activate' );
/**
* Activation hook.
*
* @since {{version}}
*/
function {{slugSnakeCase}}_activate() {
if ( ! class_exists( 'WooCommerce' ) ) {
add_action( 'admin_notices', '{{slugSnakeCase}}_missing_wc_notice' );
return;
}
}
if ( ! class_exists( '{{slugSnakeCase}}' ) ) :
/**
* The {{slugSnakeCase}} class.
*/
class {{slugSnakeCase}} {
/**
* This class instance.
*
* @var \{{slugPascalCase}} single instance of this class.
* @var \{{slugSnakeCase}} single instance of this class.
*/
private static $instance;
@ -43,25 +70,25 @@ class {{slugPascalCase}} {
* Constructor.
*/
public function __construct() {
add_action( 'plugins_loaded', array( $this, 'init' ) );
if ( is_admin() ) {
new Setup();
}
}
/**
* Init the plugin once WP is loaded.
* Cloning is forbidden.
*
*/
public function init() {
// If WooCommerce does not exist, deactivate plugin.
if ( ! class_exists( 'WooCommerce' ) ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
public function __clone() {
wc_doing_it_wrong( __FUNCTION__, __( 'Cloning is forbidden.', '{{slugSnakeCase}}' ), $this->version );
}
if ( is_admin() ) {
// Load plugin translations.
$plugin_rel_path = basename( dirname( __FILE__ ) ) . '/languages'; /* Relative to WP_PLUGIN_DIR */
load_plugin_textdomain( '{{slug}}', false, $plugin_rel_path );
new Setup();
}
/**
* Unserializing instances of this class is forbidden.
*
*/
public function __wakeup() {
wc_doing_it_wrong( __FUNCTION__, __( 'Unserializing instances of this class is forbidden.', '{{slugSnakeCase}}' ), $this->version );
}
/**
@ -69,7 +96,7 @@ class {{slugPascalCase}} {
*
* Ensures only one instance can be loaded.
*
* @return \{{slugPascalCase}}
* @return \{{slugSnakeCase}}
*/
public static function instance() {
@ -79,6 +106,24 @@ class {{slugPascalCase}} {
return self::$instance;
}
}
}
endif;
{{slugPascalCase}}::instance();
add_action( 'plugins_loaded', '{{slugSnakeCase}}_init', 10 );
/**
* Initialize the plugin.
*
* @since {{version}}
*/
function {{slugSnakeCase}}_init() {
load_plugin_textdomain( '{{slugSnakeCase}}', false, plugin_basename( dirname( __FILE__ ) ) . '/languages' );
if ( ! class_exists( 'WooCommerce' ) ) {
add_action( 'admin_notices', '{{slugSnakeCase}}_missing_wc_notice' );
return;
}
{{slugSnakeCase}}::instance();
}

View File

@ -0,0 +1,4 @@
Significance: patch
Type: dev
Add WC validation