From 06d0bbefc410eacb4afd02dd2b61e21a3d98eb5f Mon Sep 17 00:00:00 2001 From: Peter Fabian Date: Fri, 7 Feb 2020 00:20:42 +0100 Subject: [PATCH 1/5] Don't run setup wizard redirects from AS. --- includes/admin/class-wc-admin.php | 4 ++++ includes/wc-core-functions.php | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/includes/admin/class-wc-admin.php b/includes/admin/class-wc-admin.php index b9c4e47c719..6f56304ad04 100644 --- a/includes/admin/class-wc-admin.php +++ b/includes/admin/class-wc-admin.php @@ -127,6 +127,10 @@ class WC_Admin { * For setup wizard, transient must be present, the user must have access rights, and we must ignore the network/bulk plugin updaters. */ public function admin_redirects() { + if ( wc_is_running_from_async_action_scheduler() ) { + return; + } + // phpcs:disable WordPress.Security.NonceVerification.Recommended // Nonced plugin install redirects (whitelisted). if ( ! empty( $_GET['wc-install-plugin-redirect'] ) ) { diff --git a/includes/wc-core-functions.php b/includes/wc-core-functions.php index f742e09042b..465515b7b68 100644 --- a/includes/wc-core-functions.php +++ b/includes/wc-core-functions.php @@ -2273,3 +2273,13 @@ function wc_load_cart() { WC()->initialize_session(); WC()->initialize_cart(); } + +/** + * Test whether the context of execution comes from async action scheduler. + * + * @since 4.0.0 + * @return bool + */ +function wc_is_running_from_async_action_scheduler() { + return isset( $_REQUEST['action'] ) && 'as_async_request_queue_runner' === $_REQUEST['action']; +} From 4c4f9f11645be91982840a834f50be6dbef4d0f6 Mon Sep 17 00:00:00 2001 From: Peter Fabian Date: Fri, 7 Feb 2020 00:21:03 +0100 Subject: [PATCH 2/5] Don't save notices when running from AS. --- includes/admin/class-wc-admin-notices.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/includes/admin/class-wc-admin-notices.php b/includes/admin/class-wc-admin-notices.php index 9f4a1869a44..a7dfd8cd573 100644 --- a/includes/admin/class-wc-admin-notices.php +++ b/includes/admin/class-wc-admin-notices.php @@ -49,7 +49,9 @@ class WC_Admin_Notices { add_action( 'switch_theme', array( __CLASS__, 'reset_admin_notices' ) ); add_action( 'woocommerce_installed', array( __CLASS__, 'reset_admin_notices' ) ); add_action( 'wp_loaded', array( __CLASS__, 'hide_notices' ) ); - add_action( 'shutdown', array( __CLASS__, 'store_notices' ) ); + if ( ! wc_is_running_from_async_action_scheduler() ) { + add_action( 'shutdown', array( __CLASS__, 'store_notices' ) ); + } if ( current_user_can( 'manage_woocommerce' ) ) { add_action( 'admin_print_styles', array( __CLASS__, 'add_notices' ) ); From 5fc2e8fba9e8090895479091813f8e9968c1482a Mon Sep 17 00:00:00 2001 From: Peter Fabian Date: Fri, 7 Feb 2020 11:06:35 +0100 Subject: [PATCH 3/5] Only skip the store_notices during new install in AS. --- includes/admin/class-wc-admin-notices.php | 2 +- includes/class-wc-install.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/admin/class-wc-admin-notices.php b/includes/admin/class-wc-admin-notices.php index a7dfd8cd573..a688a45e930 100644 --- a/includes/admin/class-wc-admin-notices.php +++ b/includes/admin/class-wc-admin-notices.php @@ -49,7 +49,7 @@ class WC_Admin_Notices { add_action( 'switch_theme', array( __CLASS__, 'reset_admin_notices' ) ); add_action( 'woocommerce_installed', array( __CLASS__, 'reset_admin_notices' ) ); add_action( 'wp_loaded', array( __CLASS__, 'hide_notices' ) ); - if ( ! wc_is_running_from_async_action_scheduler() ) { + if ( ! WC_Install::is_new_install() || ! wc_is_running_from_async_action_scheduler() ) { add_action( 'shutdown', array( __CLASS__, 'store_notices' ) ); } diff --git a/includes/class-wc-install.php b/includes/class-wc-install.php index 01309f87ce2..357ae88bd3c 100644 --- a/includes/class-wc-install.php +++ b/includes/class-wc-install.php @@ -319,7 +319,7 @@ class WC_Install { * @since 3.2.0 * @return boolean */ - private static function is_new_install() { + public static function is_new_install() { $product_count = array_sum( (array) wp_count_posts( 'product' ) ); return is_null( get_option( 'woocommerce_version', null ) ) || ( 0 === $product_count && -1 === wc_get_page_id( 'shop' ) ); From 0eb758b8e2420c7273985b216001906387fc9255 Mon Sep 17 00:00:00 2001 From: Peter Fabian Date: Fri, 7 Feb 2020 11:32:33 +0100 Subject: [PATCH 4/5] Apply patch until a new AS version comes out. --- bin/package-update.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/bin/package-update.sh b/bin/package-update.sh index 0a3cc1b6a95..d45306be6cb 100755 --- a/bin/package-update.sh +++ b/bin/package-update.sh @@ -39,3 +39,10 @@ find ./packages/woocommerce-admin -iname '*.js' -exec sed -i.bak -e "s/, 'woocom # Cleanup backup files find ./packages -name "*.bak" -type f -delete output 2 "Done!" + +# Apply patches +output 2 "Applying patch #450 to Action Schduler" +cd packages/action-scheduler +curl -O https://patch-diff.githubusercontent.com/raw/woocommerce/action-scheduler/pull/450.patch +patch -p1 < 450.patch +output 2 "Done!" From 4f485a14deaedff4ba41fb95a1d50b180108563e Mon Sep 17 00:00:00 2001 From: Peter Fabian Date: Fri, 7 Feb 2020 12:58:31 +0100 Subject: [PATCH 5/5] Added explanation comments. --- includes/admin/class-wc-admin-notices.php | 3 +++ includes/admin/class-wc-admin.php | 2 ++ 2 files changed, 5 insertions(+) diff --git a/includes/admin/class-wc-admin-notices.php b/includes/admin/class-wc-admin-notices.php index a688a45e930..9fa28b060f0 100644 --- a/includes/admin/class-wc-admin-notices.php +++ b/includes/admin/class-wc-admin-notices.php @@ -49,6 +49,9 @@ class WC_Admin_Notices { add_action( 'switch_theme', array( __CLASS__, 'reset_admin_notices' ) ); add_action( 'woocommerce_installed', array( __CLASS__, 'reset_admin_notices' ) ); add_action( 'wp_loaded', array( __CLASS__, 'hide_notices' ) ); + // @TODO: This prevents Action Scheduler async jobs from storing empty list of notices during WC installation. + // That could lead to OBW not starting and 'Run setup wizard' notice not appearing in WP admin, which we want + // to avoid. if ( ! WC_Install::is_new_install() || ! wc_is_running_from_async_action_scheduler() ) { add_action( 'shutdown', array( __CLASS__, 'store_notices' ) ); } diff --git a/includes/admin/class-wc-admin.php b/includes/admin/class-wc-admin.php index 6f56304ad04..23d27e5bd73 100644 --- a/includes/admin/class-wc-admin.php +++ b/includes/admin/class-wc-admin.php @@ -127,6 +127,8 @@ class WC_Admin { * For setup wizard, transient must be present, the user must have access rights, and we must ignore the network/bulk plugin updaters. */ public function admin_redirects() { + // Don't run this fn from Action Scheduler requests, as it would clear _wc_activation_redirect transient. + // That means OBW would never be shown. if ( wc_is_running_from_async_action_scheduler() ) { return; }