From 8f22d9a79b503bc611b34f65d724fca95baa1926 Mon Sep 17 00:00:00 2001 From: Grzegorz Rola <grola@seostudio.pl> Date: Mon, 14 Jan 2019 11:10:59 +0000 Subject: [PATCH] Bugfix/init file --- assets/js/notice.js | 1 - assets/js/notice.min.js | 2 +- init.php | 13 +++++---- src/WPDesk/Notice/AjaxHandler.php | 18 ++++++++++--- src/WPDesk/Notice/views/admin-head-js.php | 4 +++ src/WPDesk/notice-functions.php | 30 +++++++++++++++++++++ src/assets/js/notice.js | 16 ----------- src/assets/js/notice.min.js | 1 - tests/integration/TestAjaxHandler.php | 33 +++++++++++++++++++++-- tests/integration/TestFunctions.php | 10 +++++++ 10 files changed, 97 insertions(+), 31 deletions(-) create mode 100644 src/WPDesk/Notice/views/admin-head-js.php delete mode 100644 src/assets/js/notice.js delete mode 100644 src/assets/js/notice.min.js diff --git a/assets/js/notice.js b/assets/js/notice.js index 486d6a6..1b53c5a 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 5126caa..34d4075 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 4e89096..5d612e2 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 0df3000..43f4665 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 0000000..f3774d9 --- /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 ca810c5..d8d3ac9 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 c084824..0000000 --- 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 5126caa..0000000 --- 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 08f8fa8..50f1fb1 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 056d120..eee485a 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); + } + } -- GitLab