diff --git a/assets/js/notice.js b/assets/js/notice.js index 486d6a679b17d2b236c0fd091e98a7c935522fb4..1b53c5a185b842ff9afc28dc8350403b1be7e7cf 100644 --- a/assets/js/notice.js +++ b/assets/js/notice.js @@ -1,6 +1,5 @@ jQuery( document ).on( 'click', '.notice-dismiss', function() { var notice_name = jQuery(this).closest('div.notice').data('notice-name'); -console.log(notice_name); if ('' !== notice_name) { jQuery.ajax({ url: ajaxurl, diff --git a/assets/js/notice.min.js b/assets/js/notice.min.js index 5126caa317ddbfd5c0a4181663bde6cab677cecc..34d4075e46ac7caabda8da5080cd4a6edc80c05a 100644 --- a/assets/js/notice.min.js +++ b/assets/js/notice.min.js @@ -1 +1 @@ -jQuery(document).on("click",".notice-dismiss",function(){var a=jQuery(this).closest("div.notice").data("notice-name");console.log(a);if(""!==a){jQuery.ajax({url:ajaxurl,type:"post",data:{action:"wpdesk_notice_dismiss",notice_name:a},success:function(b){}})}}); \ No newline at end of file +jQuery(document).on("click",".notice-dismiss",function(){var a=jQuery(this).closest("div.notice").data("notice-name");if(""!==a){jQuery.ajax({url:ajaxurl,type:"post",data:{action:"wpdesk_notice_dismiss",notice_name:a},success:function(b){}})}}); \ No newline at end of file diff --git a/init.php b/init.php index 4e8909615f95f62e2d813054d1e5d0903c81b22e..5d612e24468851a2deed16cb33c22ae1404718ff 100644 --- a/init.php +++ b/init.php @@ -1,18 +1,17 @@ <?php -require_once './vendor/autoload.php'; +require_once __DIR__ . '/vendor/autoload.php'; if (!class_exists('\WPDesk\Notice\AjaxHandler')) { - require_once './WPDesk/Notice/AjaxHandler.php'; + require_once __DIR__ . '/src/WPDesk/Notice/AjaxHandler.php'; } if (!class_exists('\WPDesk\Notice\Notice')) { - require_once './WPDesk/Notice/Notice.php'; + require_once __DIR__ . 'src/WPDesk/Notice/Notice.php'; } if (!class_exists('\WPDesk\Notice\PermanentDismissibleNotice')) { - require_once './WPDesk/Notice/PermanentDismissibleNotice.php'; + require_once __DIR__ . '/src/WPDesk/Notice/PermanentDismissibleNotice.php'; } if (!class_exists('\WPDesk\Notice\Factory')) { - require_once './WPDesk/Notice/Factory.php'; + require_once __DIR__ . '/src/WPDesk/Notice/Factory.php'; } -require_once './WPDesk/notice-functions.php'; - +require_once __DIR__ . '/src/WPDesk/notice-functions.php'; diff --git a/src/WPDesk/Notice/AjaxHandler.php b/src/WPDesk/Notice/AjaxHandler.php index 0df30006a9ccd180d936303f3dbca6e9a1db977a..43f4665989f9b058fdcdbf7cdcf405152dc2d31e 100644 --- a/src/WPDesk/Notice/AjaxHandler.php +++ b/src/WPDesk/Notice/AjaxHandler.php @@ -30,9 +30,9 @@ class AjaxHandler implements HookablePluginDependant /** * AjaxHandler constructor. * - * @param string $assetsURL Assets URL. + * @param string|null $assetsURL Assets URL. */ - public function __construct($assetsURL) + public function __construct($assetsURL = null) { $this->assetsURL = $assetsURL; } @@ -42,7 +42,11 @@ class AjaxHandler implements HookablePluginDependant */ public function hooks() { - add_action('admin_enqueue_scripts', [$this, 'enqueueAdminScripts']); + if ($this->assetsURL) { + add_action('admin_enqueue_scripts', [$this, 'enqueueAdminScripts']); + } else { + add_action('admin_head', [$this,'addScriptToAdminHead']); + } add_action('wp_ajax_wpdesk_notice_dismiss', [$this, 'processAjaxNoticeDismiss']); } @@ -61,6 +65,14 @@ class AjaxHandler implements HookablePluginDependant wp_enqueue_script(self::SCRIPT_HANDLE); } + /** + * Add Java Script to admin header. + */ + public function addScriptToAdminHead() + { + include 'views/admin-head-js.php'; + } + /** * Process AJAX notice dismiss. * diff --git a/src/WPDesk/Notice/views/admin-head-js.php b/src/WPDesk/Notice/views/admin-head-js.php new file mode 100644 index 0000000000000000000000000000000000000000..f3774d921165f1ef03c03577d25b40d088939bf6 --- /dev/null +++ b/src/WPDesk/Notice/views/admin-head-js.php @@ -0,0 +1,4 @@ +<script type="text/javascript"> + <?php include dirname(__FILE__) . '/../../../../assets/js/notice.min.js'; ?> + +</script> diff --git a/src/WPDesk/notice-functions.php b/src/WPDesk/notice-functions.php index ca810c57f33136f6394a40908a0182155784a04e..d8d3ac990dc038385e848722e3a49cdad2815ad3 100644 --- a/src/WPDesk/notice-functions.php +++ b/src/WPDesk/notice-functions.php @@ -1,5 +1,35 @@ <?php +if (!function_exists('WPDeskInitNoticeAjaxHandler')) { + /** + * Init notices AJAX Handler. + * + * @param string|null $assetsUrl + * + * @return \WPDesk\Notice\AjaxHandler + */ + function WPDeskInitNoticeAjaxHandler($assetsUrl = null) + { + $ajax_handler = new \WPDesk\Notice\AjaxHandler($assetsUrl); + $ajax_handler->hooks(); + return $ajax_handler; + } +} + +if (!function_exists('wpdesk_init_notice_ajax_handler')) { + /** + * Alias for {@see WPDeskInitNoticeAjaxHandler()} function. + * + * @param null $assetsUrl + * + * @return \WPDesk\Notice\AjaxHandler + */ + function wpdesk_init_notice_ajax_handler($assetsUrl = null) + { + return WPDeskInitNoticeAjaxHandler($assetsUrl); + } +} + if (!function_exists('WPDeskNotice')) { /** * Creates Notice. diff --git a/src/assets/js/notice.js b/src/assets/js/notice.js deleted file mode 100644 index c084824e5a10ead47df75b94addabc5d44300f42..0000000000000000000000000000000000000000 --- a/src/assets/js/notice.js +++ /dev/null @@ -1,16 +0,0 @@ -jQuery( document ).on( 'click', '.notice-dismiss', function() { - var notice_name = jQuery(this).closest('div.notice').data('notice-name'); - console.log(notice_name); - if ('' !== notice_name) { - jQuery.ajax({ - url: ajaxurl, - type: 'post', - data: { - action: 'wpdesk_notice_dismiss', - notice_name: notice_name - }, - success: function (response) { - } - }); - } -}); diff --git a/src/assets/js/notice.min.js b/src/assets/js/notice.min.js deleted file mode 100644 index 5126caa317ddbfd5c0a4181663bde6cab677cecc..0000000000000000000000000000000000000000 --- a/src/assets/js/notice.min.js +++ /dev/null @@ -1 +0,0 @@ -jQuery(document).on("click",".notice-dismiss",function(){var a=jQuery(this).closest("div.notice").data("notice-name");console.log(a);if(""!==a){jQuery.ajax({url:ajaxurl,type:"post",data:{action:"wpdesk_notice_dismiss",notice_name:a},success:function(b){}})}}); \ No newline at end of file diff --git a/tests/integration/TestAjaxHandler.php b/tests/integration/TestAjaxHandler.php index 08f8fa843d5219f38ef1e741e0b74c2d5ea99a1f..50f1fb188cef19cd6731f0633bb5ebb3d6d2d917 100644 --- a/tests/integration/TestAjaxHandler.php +++ b/tests/integration/TestAjaxHandler.php @@ -10,7 +10,7 @@ class TestAjaxHandler extends WP_UnitTestCase const NOTICE_NAME = 'test_notice_name'; const WP_DEFAULT_PRIORITY = 10; - public function testHooks() + public function testHooksWithAssetsURL() { $ajaxHandler = new AjaxHandler(self::ASSETS_URL); $ajaxHandler->hooks(); @@ -25,6 +25,21 @@ class TestAjaxHandler extends WP_UnitTestCase ); } + public function testHooksWithoutAssetsURL() + { + $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']) + ); + } + public function testEnqueueAdminScripts() { $ajaxHandler = new AjaxHandler(self::ASSETS_URL); @@ -40,7 +55,21 @@ class TestAjaxHandler extends WP_UnitTestCase ); } - public function testProcessAjaxNoticeDismiss() { + public function testAddScriptToAdminHead() + { + $ajaxHandler = new AjaxHandler(); + $ajaxHandler->hooks(); + + $this->expectOutputString('<script type="text/javascript"> + jQuery(document).on("click",".notice-dismiss",function(){var a=jQuery(this).closest("div.notice").data("notice-name");if(""!==a){jQuery.ajax({url:ajaxurl,type:"post",data:{action:"wpdesk_notice_dismiss",notice_name:a},success:function(b){}})}}); +</script> +'); + + $ajaxHandler->addScriptToAdminHead(); + } + + public function testProcessAjaxNoticeDismiss() + { $_POST[AjaxHandler::POST_FIELD_NOTICE_NAME] = self::NOTICE_NAME; $ajaxHandler = new AjaxHandler(self::ASSETS_URL); diff --git a/tests/integration/TestFunctions.php b/tests/integration/TestFunctions.php index 056d1203539b188954f9e1a1252bf32a4bfda28a..eee485acbcef16a385d0e657447384bca700765d 100644 --- a/tests/integration/TestFunctions.php +++ b/tests/integration/TestFunctions.php @@ -99,4 +99,14 @@ class TestFunctions extends WP_UnitTestCase $notice->showNotice(); } + /** + * Test WPDeskInitNoticeAjaxHandler function. + */ + public function testWPDeskInitNoticeAjaxHandler() + { + $ajax_handler = wpdesk_init_notice_ajax_handler(); + + $this->assertInstanceOf(\WPDesk\Notice\AjaxHandler::class, $ajax_handler); + } + }