From 88e0ee1ca745c8dad85ed2dee73bdb4002a33f72 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Wed, 17 Jan 2024 15:52:10 +0100 Subject: [PATCH] Hook timing --- includes/MslsPlugin.php | 9 +++++---- tests/test-mslsplugin.php | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/includes/MslsPlugin.php b/includes/MslsPlugin.php index d8d41a0b..8affb1fe 100644 --- a/includes/MslsPlugin.php +++ b/includes/MslsPlugin.php @@ -58,13 +58,10 @@ public static function init() { } add_action( 'init', [ $obj, 'admin_bar_init' ] ); + add_action( 'admin_enqueue_scripts', [ $obj, 'custom_enqueue' ] ); \lloc\Msls\ContentImport\Service::instance()->register(); - if ( is_admin() || is_admin_bar_showing() ) { - add_action( 'admin_enqueue_scripts', [ $obj, 'custom_enqueue' ] ); - } - if ( is_admin() ) { add_action( 'admin_menu', [ MslsAdmin::class, 'init' ] ); add_action( 'load-post.php', [ MslsMetaBox::class, 'init' ] ); @@ -268,6 +265,10 @@ public function admin_bar_init() { * @return boolean */ public function custom_enqueue() { + if ( ! is_admin_bar_showing() ) { + return false; + } + $ver = defined( 'MSLS_PLUGIN_VERSION' ) ? constant( 'MSLS_PLUGIN_VERSION' ) : false; $postfix = defined( 'SCRIPT_DEBUG' ) && constant( 'SCRIPT_DEBUG' ) ? '' : '.min'; diff --git a/tests/test-mslsplugin.php b/tests/test-mslsplugin.php index a3ceaec2..e8848fcf 100644 --- a/tests/test-mslsplugin.php +++ b/tests/test-mslsplugin.php @@ -10,6 +10,7 @@ class WP_Test_MslsPlugin extends Msls_UnitTestCase { function test_admin_menu_without_autocomplete(): void { + Functions\expect( 'is_admin_bar_showing' )->once()->andReturnTrue(); Functions\expect( 'wp_enqueue_style' )->twice(); Functions\expect( 'plugins_url' )->twice()->andReturn( 'https://lloc.de/wp-content/plugins' ); @@ -21,11 +22,13 @@ function test_admin_menu_without_autocomplete(): void { } function test_admin_menu_with_autocomplete(): void { + Functions\expect( 'is_admin_bar_showing' )->once()->andReturnTrue(); Functions\expect( 'wp_enqueue_style' )->twice(); Functions\expect( 'plugins_url' )->times( 3 )->andReturn( 'https://lloc.de/wp-content/plugins' ); Functions\expect( 'wp_enqueue_script' )->once(); $options = \Mockery::mock( MslsOptions::class ); + $options->activate_autocomplete = true; $test = new MslsPlugin( $options ); @@ -33,7 +36,18 @@ function test_admin_menu_with_autocomplete(): void { $this->assertTrue( $test->custom_enqueue() ); } - function test_init_widget_not_excluded(): void { + function test_admin_menu_admin_bar_not_showing(): void { + Functions\expect( 'is_admin_bar_showing' )->once()->andReturnFalse(); + + $options = \Mockery::mock( MslsOptions::class ); + + $options->activate_autocomplete = true; + + $test = new MslsPlugin( $options ); + + $this->assertFalse( $test->custom_enqueue() ); + } + function test_init_widget_not_excluded(): void { Functions\expect( 'register_widget' )->once(); $options = \Mockery::mock( MslsOptions::class );