Skip to content
Snippets Groups Projects
Commit 48aa5fae authored by Grzegorz Rola's avatar Grzegorz Rola
Browse files

Merge branch 'bugfix/init-file' into 'master'

Bugfix/init file

See merge request !12
parents 80b4ad4c 8f22d9a7
No related branches found
Tags 2.3
1 merge request!12Bugfix/init file
Pipeline #9641 passed with stages
in 2 minutes and 41 seconds
jQuery( document ).on( 'click', '.notice-dismiss', function() { jQuery( document ).on( 'click', '.notice-dismiss', function() {
var notice_name = jQuery(this).closest('div.notice').data('notice-name'); var notice_name = jQuery(this).closest('div.notice').data('notice-name');
console.log(notice_name);
if ('' !== notice_name) { if ('' !== notice_name) {
jQuery.ajax({ jQuery.ajax({
url: ajaxurl, url: ajaxurl,
......
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){}})}}); 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 \ No newline at end of file
<?php <?php
require_once './vendor/autoload.php'; require_once __DIR__ . '/vendor/autoload.php';
if (!class_exists('\WPDesk\Notice\AjaxHandler')) { 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')) { 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')) { 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')) { 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';
...@@ -30,9 +30,9 @@ class AjaxHandler implements HookablePluginDependant ...@@ -30,9 +30,9 @@ class AjaxHandler implements HookablePluginDependant
/** /**
* AjaxHandler constructor. * 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; $this->assetsURL = $assetsURL;
} }
...@@ -42,7 +42,11 @@ class AjaxHandler implements HookablePluginDependant ...@@ -42,7 +42,11 @@ class AjaxHandler implements HookablePluginDependant
*/ */
public function hooks() 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']); add_action('wp_ajax_wpdesk_notice_dismiss', [$this, 'processAjaxNoticeDismiss']);
} }
...@@ -61,6 +65,14 @@ class AjaxHandler implements HookablePluginDependant ...@@ -61,6 +65,14 @@ class AjaxHandler implements HookablePluginDependant
wp_enqueue_script(self::SCRIPT_HANDLE); 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. * Process AJAX notice dismiss.
* *
......
<script type="text/javascript">
<?php include dirname(__FILE__) . '/../../../../assets/js/notice.min.js'; ?>
</script>
<?php <?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')) { if (!function_exists('WPDeskNotice')) {
/** /**
* Creates Notice. * Creates Notice.
......
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) {
}
});
}
});
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
...@@ -10,7 +10,7 @@ class TestAjaxHandler extends WP_UnitTestCase ...@@ -10,7 +10,7 @@ class TestAjaxHandler extends WP_UnitTestCase
const NOTICE_NAME = 'test_notice_name'; const NOTICE_NAME = 'test_notice_name';
const WP_DEFAULT_PRIORITY = 10; const WP_DEFAULT_PRIORITY = 10;
public function testHooks() public function testHooksWithAssetsURL()
{ {
$ajaxHandler = new AjaxHandler(self::ASSETS_URL); $ajaxHandler = new AjaxHandler(self::ASSETS_URL);
$ajaxHandler->hooks(); $ajaxHandler->hooks();
...@@ -25,6 +25,21 @@ class TestAjaxHandler extends WP_UnitTestCase ...@@ -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() public function testEnqueueAdminScripts()
{ {
$ajaxHandler = new AjaxHandler(self::ASSETS_URL); $ajaxHandler = new AjaxHandler(self::ASSETS_URL);
...@@ -40,7 +55,21 @@ class TestAjaxHandler extends WP_UnitTestCase ...@@ -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; $_POST[AjaxHandler::POST_FIELD_NOTICE_NAME] = self::NOTICE_NAME;
$ajaxHandler = new AjaxHandler(self::ASSETS_URL); $ajaxHandler = new AjaxHandler(self::ASSETS_URL);
......
...@@ -99,4 +99,14 @@ class TestFunctions extends WP_UnitTestCase ...@@ -99,4 +99,14 @@ class TestFunctions extends WP_UnitTestCase
$notice->showNotice(); $notice->showNotice();
} }
/**
* Test WPDeskInitNoticeAjaxHandler function.
*/
public function testWPDeskInitNoticeAjaxHandler()
{
$ajax_handler = wpdesk_init_notice_ajax_handler();
$this->assertInstanceOf(\WPDesk\Notice\AjaxHandler::class, $ajax_handler);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment