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

Bugfix/init file

parent 80b4ad4c
No related branches found
No related tags found
No related merge requests found
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,
......
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
<?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';
......@@ -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()
{
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.
*
......
<script type="text/javascript">
<?php include dirname(__FILE__) . '/../../../../assets/js/notice.min.js'; ?>
</script>
<?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.
......
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
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);
......
......@@ -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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment