Define DOING_AJAX sooner.

To enable its use during loading time.
This commit is contained in:
Peter Fabian 2019-07-26 16:52:31 +02:00
parent ba517650a1
commit d7d8210518
3 changed files with 37 additions and 2 deletions

View File

@ -17,7 +17,6 @@ class WC_AJAX {
* Hook in ajax handlers.
*/
public static function init() {
add_action( 'init', array( __CLASS__, 'define_ajax' ), 0 );
add_action( 'template_redirect', array( __CLASS__, 'do_wc_ajax' ), 0 );
self::add_ajax_events();
}
@ -35,6 +34,8 @@ class WC_AJAX {
/**
* Set WC AJAX constant and headers.
*
* @deprecated 3.7.1
*/
public static function define_ajax() {
// phpcs:disable

View File

@ -231,6 +231,16 @@ final class WooCommerce {
$this->define( 'WC_TEMPLATE_DEBUG_MODE', false );
$this->define( 'WC_NOTICE_MIN_PHP_VERSION', '5.6.20' );
$this->define( 'WC_NOTICE_MIN_WP_VERSION', '4.9' );
// To allow for conditional includes to use AJAX constants.
if ( ! empty( $_GET['wc-ajax'] ) ) {
$this->define( 'DOING_AJAX', true );
$this->define( 'WC_DOING_AJAX', true );
if ( ! WP_DEBUG || ( WP_DEBUG && ! WP_DEBUG_DISPLAY ) ) {
@ini_set( 'display_errors', 0 ); // Turn off display_errors during AJAX events to prevent malformed JSON.
}
$GLOBALS['wpdb']->hide_errors();
}
}
/**
@ -291,7 +301,7 @@ final class WooCommerce {
* @param string $type admin, ajax, cron or frontend.
* @return bool
*/
private function is_request( $type ) {
public function is_request( $type ) {
switch ( $type ) {
case 'admin':
return is_admin();

View File

@ -31,6 +31,21 @@ class Packages {
'woocommerce-rest-api' => '\\Automattic\\WooCommerce\\RestApi\\Package',
];
/**
* Array of package names and contexts when they should NOT be loaded.
*
* @var array Key is the package name/directory, value is an array of contexts when to not load the package.
*/
protected static $package_exclude_in_contexts = [
'woocommerce-blocks' => [
'ajax',
'cron',
],
'woocommerce-rest-api' => [
'frontend',
],
];
/**
* Init the package loader.
*
@ -64,6 +79,15 @@ class Packages {
*/
protected static function load_packages() {
foreach ( self::$packages as $package_name => $package_class ) {
// Exclude packages in certain pre-defined contexts.
if ( isset( self::$package_exclude_in_contexts[ $package_name ] ) ) {
foreach ( self::$package_exclude_in_contexts[ $package_name ] as $excluded_context ) {
if ( WC()->is_request( $excluded_context ) ) {
continue 2;
}
}
}
if ( ! self::package_exists( $package_name ) ) {
self::missing_package( $package_name );
continue;