From a11ce813bb3c302d00469e1d9fe04fa6a1f9557a Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Fri, 3 May 2024 13:29:33 +0200 Subject: [PATCH] fix(Assets) avoid doing-it-wrong notices, register after init --- src/Assets/Assets.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Assets/Assets.php b/src/Assets/Assets.php index 8cf02c1..018c9d8 100755 --- a/src/Assets/Assets.php +++ b/src/Assets/Assets.php @@ -681,6 +681,23 @@ protected function do_enqueue( Asset $asset, bool $force_enqueue = false ): void * */ public function register_in_wp( $assets = null ) { + if ( ! ( + did_action( 'init' ) || did_action( 'wp_enqueue_scripts' ) + || did_action( 'admin_enqueue_scripts' ) || did_action( 'login_enqueue_scripts' ) + ) + ) { + // Registering the asset now would trigger a doing_it_wrong notice: queue the assets to be registered later. + + if ( ! is_array( $assets ) ) { + $assets = [ $assets ]; + } + + // Register later, avoid the doing_it_wrong notice. + $this->assets = array_merge( $this->assets, $assets ); + + return; + } + if ( is_null( $assets ) ) { $assets = $this->get(); }