diff --git a/assets/css/admin.css b/assets/css/admin.css new file mode 100644 index 0000000000000000000000000000000000000000..7dabae8cd0f26616f320819e48d32e8575be8833 --- /dev/null +++ b/assets/css/admin.css @@ -0,0 +1 @@ +.wpdesk-notice-gutenberg { display: none; } \ No newline at end of file diff --git a/assets/js/notice.js b/assets/js/notice.js index c5c396fe3cd7bf1da27ce530900dd42154f4a6c4..bcfcaace278322f6c5555180c3c07c5b643da310 100644 --- a/assets/js/notice.js +++ b/assets/js/notice.js @@ -1,25 +1,27 @@ -jQuery( document ).on( 'click', '.notice-dismiss', function() { - const $notice_div= jQuery(this).closest('div.notice'); - const notice_name = $notice_div.data('notice-name'); - const source = $notice_div.data('source'); - const security = $notice_div.data('security'); - if ('' !== notice_name) { - jQuery.ajax({ - url: ajaxurl, - type: 'post', - data: { - security: security, - action: 'wpdesk_notice_dismiss', - notice_name: notice_name, - source: source, - }, - success: function (response) { - } - }); - } -}); +jQuery( document ).ready(function() { + jQuery(document).on('click', '.notice-dismiss', function () { + const $notice_div= jQuery(this).closest('div.notice'); + const notice_name = $notice_div.data('notice-name'); + const source = $notice_div.data('source'); + const security = $notice_div.data('security'); + if ('' !== notice_name) { + jQuery.ajax({ + url: ajaxurl, + type: 'post', + data: { + security: security, + action: 'wpdesk_notice_dismiss', + notice_name: notice_name, + source: source, + }, + success: function (response) { + } + }); + } + }); -jQuery( document ).on( 'click', '.notice-dismiss-link', function() { - jQuery(this).closest('div.notice').data('source',jQuery(this).data('source')); - jQuery(this).closest('div.notice').find('.notice-dismiss').click(); -}); + jQuery( document ).on( 'click', '.notice-dismiss-link', function() { + jQuery(this).closest('div.notice').data('source',jQuery(this).data('source')); + jQuery(this).closest('div.notice').find('.notice-dismiss').click(); + }); +} ); diff --git a/src/WPDesk/Notice/AjaxHandler.php b/src/WPDesk/Notice/AjaxHandler.php index 64c9b7141b4bdc34ef6c2725831842ef26e3240a..4fba4efc1227436f3499ca6be5e65c47a521a5fb 100644 --- a/src/WPDesk/Notice/AjaxHandler.php +++ b/src/WPDesk/Notice/AjaxHandler.php @@ -22,6 +22,7 @@ class AjaxHandler implements HookablePluginDependant { const SCRIPTS_VERSION = '4'; const SCRIPT_HANDLE = 'wpdesk_notice'; + const SCRIPT_HANDLE_GUTENBERG = 'wpdesk_notice_gutenberg'; /** * @var string @@ -34,21 +35,28 @@ class AjaxHandler implements HookablePluginDependant { * @param string|null $assetsURL Assets URL. */ public function __construct( $assetsURL = null ) { - $this->assetsURL = $assetsURL; + $this->assetsURL = $assetsURL ?? dirname(__FILE__, 5) . '/assets/'; } /** * Hooks. */ public function hooks() { - if ( $this->assetsURL ) { - add_action( 'admin_enqueue_scripts', [ $this, 'enqueueAdminScripts' ] ); - } else { - add_action( 'admin_head', [ $this, 'addScriptToAdminHead' ] ); - } + add_action( 'admin_enqueue_scripts', [ $this, 'enqueueAdminScripts' ] ); add_action( 'wp_ajax_wpdesk_notice_dismiss', [ $this, 'processAjaxNoticeDismiss' ] ); } + public function isBlockEditor():bool + { + if ( !function_exists( 'get_current_screen' ) ) { + return false; + } + + $screen = \get_current_screen(); + + return is_object($screen) ? $screen->is_block_editor() : false; + } + /** * Enqueue admin scripts. */ @@ -60,13 +68,24 @@ class AjaxHandler implements HookablePluginDependant { self::SCRIPTS_VERSION ); wp_enqueue_script( self::SCRIPT_HANDLE ); - } - /** - * Add Java Script to admin header. - */ - public function addScriptToAdminHead() { - include __DIR__ . '/views/admin-head-js.php'; + if($this->isBlockEditor()){ + wp_register_script( + self::SCRIPT_HANDLE, + trailingslashit( $this->assetsURL ) . 'js/gutenberg.js', + [ 'jquery' ], + self::SCRIPTS_VERSION + ); + wp_enqueue_script( self::SCRIPT_HANDLE ); + }else{ + wp_register_style( + self::SCRIPT_HANDLE, + trailingslashit( $this->assetsURL ) . 'css/admin.css', + [], + self::SCRIPTS_VERSION + ); + wp_enqueue_style( self::SCRIPT_HANDLE ); + } } /** diff --git a/src/WPDesk/Notice/Notice.php b/src/WPDesk/Notice/Notice.php index 047152a8863346d9cd808bc6632aac3f3fe540bd..9c95a732ea0d14093afe8ce05168789ad245e1fb 100644 --- a/src/WPDesk/Notice/Notice.php +++ b/src/WPDesk/Notice/Notice.php @@ -92,20 +92,6 @@ class Notice $this->addAction(); } - /** - * @return bool - */ - public function isBlockEditor() - { - if ( !function_exists( 'get_current_screen' ) ) { - require_once ABSPATH . '/wp-admin/includes/screen.php'; - } - - $screen = \get_current_screen(); - - return is_object($screen) ? $screen->is_block_editor() : false; - } - /** * @return string */ @@ -186,7 +172,6 @@ class Notice [$this, 'showNotice'], self::ADMIN_FOOTER_BASE_PRIORITY + intval($this->priority) ); - add_action('admin_head', [$this,'addGutenbergScript']); $this->actionAdded = true; } } @@ -207,16 +192,6 @@ class Notice } } - /** - * Enqueue admin scripts. - */ - public function addGutenbergScript() - { - if ($this->isBlockEditor()) { - include_once __DIR__ . '/views/admin-head-js-gutenberg.php'; - } - } - /** * Add attribute. * diff --git a/tests/codeception/tests/integration/AjaxHandlerTest.php b/tests/codeception/tests/integration/AjaxHandlerTest.php index 486a60f273cc35bbe5f4fe5447854b858c0563f3..ec8af8833c5f646fa13be01267b927446cdb1f5e 100644 --- a/tests/codeception/tests/integration/AjaxHandlerTest.php +++ b/tests/codeception/tests/integration/AjaxHandlerTest.php @@ -48,10 +48,6 @@ class AjaxHandlerTest extends WPTestCase { $ajaxHandler = new AjaxHandler(); $ajaxHandler->hooks(); - $this->assertEquals( - self::WP_DEFAULT_PRIORITY, - has_action( 'admin_head', [ $ajaxHandler, 'addScriptToAdminHead' ] ) - ); $this->assertEquals( self::WP_DEFAULT_PRIORITY, has_action( 'wp_ajax_wpdesk_notice_dismiss', [ $ajaxHandler, 'processAjaxNoticeDismiss' ] ) @@ -73,20 +69,6 @@ class AjaxHandlerTest extends WPTestCase { ); } - public function testAddScriptToAdminHead() { - $ajaxHandler = new AjaxHandler(); - $ajaxHandler->hooks(); - - $this->expectOutputString( '<script type="text/javascript">' - . "\n " - . file_get_contents( __DIR__ . '/../../../../assets/js/notice.js' ) - . '</script> -' - ); - - $ajaxHandler->addScriptToAdminHead(); - } - public function testProcessAjaxNoticeDismiss() { $user_name = 'test_user'; $random_password = wp_generate_password( $length = 12, $include_standard_special_chars = false );