2019-08-12 17:54:04 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WooCommerce Admin: Feature plugin main class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Automattic\WooCommerce\Admin;
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
2020-09-28 04:35:10 +00:00
|
|
|
use \Automattic\WooCommerce\Admin\Notes\Notes;
|
2020-10-28 17:12:14 +00:00
|
|
|
use \Automattic\WooCommerce\Admin\Notes\OrderMilestones;
|
|
|
|
use \Automattic\WooCommerce\Admin\Notes\WooSubscriptionsNotes;
|
|
|
|
use \Automattic\WooCommerce\Admin\Notes\TrackingOptIn;
|
|
|
|
use \Automattic\WooCommerce\Admin\Notes\WooCommercePayments;
|
|
|
|
use \Automattic\WooCommerce\Admin\Notes\InstallJPAndWCSPlugins;
|
|
|
|
use \Automattic\WooCommerce\Admin\Notes\SetUpAdditionalPaymentTypes;
|
|
|
|
use \Automattic\WooCommerce\Admin\Notes\TestCheckout;
|
|
|
|
use \Automattic\WooCommerce\Admin\Notes\SellingOnlineCourses;
|
2021-01-13 00:09:22 +00:00
|
|
|
use \Automattic\WooCommerce\Admin\Notes\MerchantEmailNotifications\MerchantEmailNotifications;
|
2021-01-10 23:24:28 +00:00
|
|
|
use \Automattic\WooCommerce\Admin\Notes\WelcomeToWooCommerceForStoreUsers;
|
2021-01-15 01:34:00 +00:00
|
|
|
use \Automattic\WooCommerce\Admin\Notes\ManageStoreActivityFromHomeScreen;
|
2021-03-26 00:07:20 +00:00
|
|
|
use \Automattic\WooCommerce\Admin\Notes\NavigationNudge;
|
2022-01-12 20:41:12 +00:00
|
|
|
use \Automattic\WooCommerce\Admin\Notes\MagentoMigration;
|
2021-06-23 21:02:05 +00:00
|
|
|
use Automattic\WooCommerce\Admin\Features\Features;
|
2019-08-12 17:54:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Feature plugin main class.
|
|
|
|
*
|
|
|
|
* @internal This file will not be bundled with woo core, only the feature plugin.
|
|
|
|
* @internal Note this is not called WC_Admin due to a class already existing in core with that name.
|
|
|
|
*/
|
2019-08-12 21:52:09 +00:00
|
|
|
class FeaturePlugin {
|
2019-08-12 17:54:04 +00:00
|
|
|
/**
|
|
|
|
* The single instance of the class.
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
protected static $instance = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function __construct() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get class instance.
|
|
|
|
*
|
|
|
|
* @return object Instance.
|
|
|
|
*/
|
|
|
|
final public static function instance() {
|
|
|
|
if ( null === static::$instance ) {
|
|
|
|
static::$instance = new static();
|
|
|
|
}
|
|
|
|
return static::$instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init the feature plugin, only if we can detect both Gutenberg and WooCommerce.
|
|
|
|
*/
|
|
|
|
public function init() {
|
2021-04-16 01:59:44 +00:00
|
|
|
// Load the page controller functions file first to prevent fatal errors when disabling WooCommerce Admin.
|
|
|
|
$this->define_constants();
|
|
|
|
require_once WC_ADMIN_ABSPATH . '/includes/page-controller-functions.php';
|
2020-10-28 17:12:14 +00:00
|
|
|
require_once WC_ADMIN_ABSPATH . '/src/Notes/DeprecatedNotes.php';
|
2019-11-19 23:23:27 +00:00
|
|
|
require_once WC_ADMIN_ABSPATH . '/includes/core-functions.php';
|
|
|
|
require_once WC_ADMIN_ABSPATH . '/includes/feature-config.php';
|
|
|
|
require_once WC_ADMIN_ABSPATH . '/includes/wc-admin-update-functions.php';
|
2021-09-10 13:50:58 +00:00
|
|
|
require_once WC_ADMIN_ABSPATH . '/includes/class-experimental-abtest.php';
|
2019-11-19 23:23:27 +00:00
|
|
|
|
2019-08-12 17:54:04 +00:00
|
|
|
register_activation_hook( WC_ADMIN_PLUGIN_FILE, array( $this, 'on_activation' ) );
|
|
|
|
register_deactivation_hook( WC_ADMIN_PLUGIN_FILE, array( $this, 'on_deactivation' ) );
|
2019-11-19 23:23:27 +00:00
|
|
|
if ( did_action( 'plugins_loaded' ) ) {
|
|
|
|
self::on_plugins_loaded();
|
|
|
|
} else {
|
2020-03-12 17:57:57 +00:00
|
|
|
// Make sure we hook into `plugins_loaded` before core's Automattic\WooCommerce\Package::init().
|
|
|
|
// If core is network activated but we aren't, the packaged version of WooCommerce Admin will
|
|
|
|
// attempt to use a data store that hasn't been loaded yet - because we've defined our constants here.
|
|
|
|
// See: https://github.com/woocommerce/woocommerce-admin/issues/3869.
|
2020-03-12 13:46:13 +00:00
|
|
|
add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ), 9 );
|
2019-11-19 23:23:27 +00:00
|
|
|
}
|
2019-08-12 17:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Install DB and create cron events when activated.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function on_activation() {
|
2019-08-12 21:52:09 +00:00
|
|
|
Install::create_tables();
|
|
|
|
Install::create_events();
|
2019-08-12 17:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove WooCommerce Admin scheduled actions on deactivate.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function on_deactivation() {
|
2020-01-16 18:46:05 +00:00
|
|
|
// Don't clean up if the WooCommerce Admin package is in core.
|
|
|
|
// NOTE: Any future divergence from the core package will need to be accounted for here.
|
|
|
|
if ( defined( 'WC_ADMIN_PACKAGE_EXISTS' ) && WC_ADMIN_PACKAGE_EXISTS ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-12 17:54:04 +00:00
|
|
|
// Check if we are deactivating due to dependencies not being satisfied.
|
|
|
|
// If WooCommerce is disabled we can't include files that depend upon it.
|
2019-11-07 07:37:35 +00:00
|
|
|
if ( ! $this->has_satisfied_dependencies() ) {
|
2019-08-12 17:54:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->includes();
|
2019-08-12 21:52:09 +00:00
|
|
|
ReportsSync::clear_queued_actions();
|
2020-09-28 04:35:10 +00:00
|
|
|
Notes::clear_queued_actions();
|
2019-08-12 17:54:04 +00:00
|
|
|
wp_clear_scheduled_hook( 'wc_admin_daily' );
|
2019-10-01 23:35:37 +00:00
|
|
|
wp_clear_scheduled_hook( 'generate_category_lookup_table' );
|
2019-08-12 17:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup plugin once all other plugins are loaded.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function on_plugins_loaded() {
|
|
|
|
$this->load_plugin_textdomain();
|
|
|
|
|
2019-11-07 07:37:35 +00:00
|
|
|
if ( ! $this->has_satisfied_dependencies() ) {
|
2019-08-12 17:54:04 +00:00
|
|
|
add_action( 'admin_init', array( $this, 'deactivate_self' ) );
|
|
|
|
add_action( 'admin_notices', array( $this, 'render_dependencies_notice' ) );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->hooks();
|
2021-06-23 21:02:05 +00:00
|
|
|
$this->includes();
|
2019-08-12 17:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define Constants.
|
|
|
|
*/
|
|
|
|
protected function define_constants() {
|
|
|
|
$this->define( 'WC_ADMIN_APP', 'wc-admin-app' );
|
|
|
|
$this->define( 'WC_ADMIN_ABSPATH', dirname( __DIR__ ) . '/' );
|
|
|
|
$this->define( 'WC_ADMIN_DIST_JS_FOLDER', 'dist/' );
|
|
|
|
$this->define( 'WC_ADMIN_DIST_CSS_FOLDER', 'dist/' );
|
|
|
|
$this->define( 'WC_ADMIN_PLUGIN_FILE', WC_ADMIN_ABSPATH . 'woocommerce-admin.php' );
|
|
|
|
// WARNING: Do not directly edit this version number constant.
|
|
|
|
// It is updated as part of the prebuild process from the package.json value.
|
2022-01-25 22:55:50 +00:00
|
|
|
$this->define( 'WC_ADMIN_VERSION_NUMBER', '3.3.0-dev' );
|
2019-08-12 17:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load Localisation files.
|
|
|
|
*/
|
|
|
|
protected function load_plugin_textdomain() {
|
|
|
|
load_plugin_textdomain( 'woocommerce-admin', false, basename( dirname( __DIR__ ) ) . '/languages' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Include WC Admin classes.
|
|
|
|
*/
|
|
|
|
public function includes() {
|
2021-06-23 21:02:05 +00:00
|
|
|
// Initialize Database updates, option migrations, and Notes.
|
2019-08-12 21:52:09 +00:00
|
|
|
Install::init();
|
|
|
|
Events::instance()->init();
|
2021-06-23 21:02:05 +00:00
|
|
|
Notes::init();
|
|
|
|
|
|
|
|
// Initialize Plugins Installer.
|
2021-04-16 19:45:43 +00:00
|
|
|
PluginsInstaller::init();
|
2022-01-31 14:17:14 +00:00
|
|
|
PluginsHelper::init();
|
2019-08-12 17:54:04 +00:00
|
|
|
|
2021-06-23 21:02:05 +00:00
|
|
|
// Initialize API.
|
|
|
|
API\Init::instance();
|
2019-08-12 17:54:04 +00:00
|
|
|
|
2021-06-23 21:02:05 +00:00
|
|
|
if ( Features::is_enabled( 'analytics' ) ) {
|
|
|
|
// Initialize Reports syncing.
|
|
|
|
ReportsSync::init();
|
|
|
|
CategoryLookup::instance()->init();
|
|
|
|
|
|
|
|
// Initialize Reports exporter.
|
|
|
|
ReportExporter::init();
|
|
|
|
}
|
2019-10-01 23:35:37 +00:00
|
|
|
|
2019-08-12 17:54:04 +00:00
|
|
|
// Admin note providers.
|
|
|
|
// @todo These should be bundled in the features/ folder, but loading them from there currently has a load order issue.
|
2020-10-28 17:12:14 +00:00
|
|
|
new WooSubscriptionsNotes();
|
|
|
|
new OrderMilestones();
|
|
|
|
new TrackingOptIn();
|
|
|
|
new WooCommercePayments();
|
|
|
|
new InstallJPAndWCSPlugins();
|
|
|
|
new SetUpAdditionalPaymentTypes();
|
|
|
|
new TestCheckout();
|
|
|
|
new SellingOnlineCourses();
|
2021-01-10 23:24:28 +00:00
|
|
|
new WelcomeToWooCommerceForStoreUsers();
|
2021-01-15 01:34:00 +00:00
|
|
|
new ManageStoreActivityFromHomeScreen();
|
2021-03-26 00:07:20 +00:00
|
|
|
new NavigationNudge();
|
2022-01-12 20:41:12 +00:00
|
|
|
new MagentoMigration();
|
2020-06-05 01:51:25 +00:00
|
|
|
|
2021-01-13 00:09:22 +00:00
|
|
|
// Initialize MerchantEmailNotifications.
|
|
|
|
MerchantEmailNotifications::init();
|
2019-08-12 17:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-18 00:16:02 +00:00
|
|
|
* Set up our admin hooks and plugin loader.
|
2019-08-12 17:54:04 +00:00
|
|
|
*/
|
|
|
|
protected function hooks() {
|
2020-06-11 00:26:20 +00:00
|
|
|
add_filter( 'woocommerce_admin_features', array( $this, 'replace_supported_features' ), 0 );
|
2019-08-12 17:54:04 +00:00
|
|
|
|
2020-07-06 17:39:58 +00:00
|
|
|
Loader::get_instance();
|
2019-08-12 17:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-07 07:37:35 +00:00
|
|
|
* Get an array of dependency error messages.
|
2019-08-12 17:54:04 +00:00
|
|
|
*
|
2019-11-07 07:37:35 +00:00
|
|
|
* @return array
|
2019-08-12 17:54:04 +00:00
|
|
|
*/
|
2019-11-07 07:37:35 +00:00
|
|
|
protected function get_dependency_errors() {
|
|
|
|
$errors = array();
|
|
|
|
$wordpress_version = get_bloginfo( 'version' );
|
2021-02-18 22:22:56 +00:00
|
|
|
$minimum_wordpress_version = '5.4';
|
|
|
|
$minimum_woocommerce_version = '4.8';
|
2019-11-07 07:37:35 +00:00
|
|
|
$wordpress_minimum_met = version_compare( $wordpress_version, $minimum_wordpress_version, '>=' );
|
|
|
|
$woocommerce_minimum_met = class_exists( 'WooCommerce' ) && version_compare( WC_VERSION, $minimum_woocommerce_version, '>=' );
|
|
|
|
|
2019-08-12 17:54:04 +00:00
|
|
|
if ( ! $woocommerce_minimum_met ) {
|
2019-11-07 07:37:35 +00:00
|
|
|
$errors[] = sprintf(
|
|
|
|
/* translators: 1: URL of WooCommerce plugin, 2: The minimum WooCommerce version number */
|
|
|
|
__( 'The WooCommerce Admin feature plugin requires <a href="%1$s">WooCommerce</a> %2$s or greater to be installed and active.', 'woocommerce-admin' ),
|
|
|
|
'https://wordpress.org/plugins/woocommerce/',
|
|
|
|
$minimum_woocommerce_version
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $wordpress_minimum_met ) {
|
|
|
|
$errors[] = sprintf(
|
|
|
|
/* translators: 1: URL of WordPress.org, 2: The minimum WordPress version number */
|
|
|
|
__( 'The WooCommerce Admin feature plugin requires <a href="%1$s">WordPress</a> %2$s or greater to be installed and active.', 'woocommerce-admin' ),
|
|
|
|
'https://wordpress.org/',
|
|
|
|
$minimum_wordpress_version
|
|
|
|
);
|
2019-08-12 17:54:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 07:37:35 +00:00
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if all dependencies for the wc-admin plugin are loaded.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-03-19 01:52:07 +00:00
|
|
|
public function has_satisfied_dependencies() {
|
2019-11-07 07:37:35 +00:00
|
|
|
$dependency_errors = $this->get_dependency_errors();
|
|
|
|
return 0 === count( $dependency_errors );
|
2019-08-12 17:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deactivates this plugin.
|
|
|
|
*/
|
|
|
|
public function deactivate_self() {
|
|
|
|
deactivate_plugins( plugin_basename( WC_ADMIN_PLUGIN_FILE ) );
|
2020-04-27 09:30:32 +00:00
|
|
|
unset( $_GET['activate'] ); // phpcs:ignore CSRF ok.
|
2019-08-12 17:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify users of the plugin requirements.
|
|
|
|
*/
|
|
|
|
public function render_dependencies_notice() {
|
2019-11-07 07:37:35 +00:00
|
|
|
$message = $this->get_dependency_errors();
|
|
|
|
printf( '<div class="error"><p>%s</p></div>', implode( ' ', $message ) ); /* phpcs:ignore xss ok. */
|
2019-08-12 17:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overwrites the allowed features array using a local `feature-config.php` file.
|
|
|
|
*
|
|
|
|
* @param array $features Array of feature slugs.
|
|
|
|
*/
|
|
|
|
public function replace_supported_features( $features ) {
|
2019-12-05 23:06:11 +00:00
|
|
|
$feature_config = apply_filters( 'woocommerce_admin_get_feature_config', wc_admin_get_feature_config() );
|
2019-08-12 17:54:04 +00:00
|
|
|
$features = array_keys( array_filter( $feature_config ) );
|
|
|
|
return $features;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define constant if not already set.
|
|
|
|
*
|
|
|
|
* @param string $name Constant name.
|
|
|
|
* @param string|bool $value Constant value.
|
|
|
|
*/
|
|
|
|
protected function define( $name, $value ) {
|
|
|
|
if ( ! defined( $name ) ) {
|
|
|
|
define( $name, $value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prevent cloning.
|
|
|
|
*/
|
|
|
|
private function __clone() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prevent unserializing.
|
|
|
|
*/
|
2020-09-30 03:51:25 +00:00
|
|
|
public function __wakeup() {
|
|
|
|
die();
|
|
|
|
}
|
2019-08-14 23:35:02 +00:00
|
|
|
}
|