diff --git a/src/WPDesk/Notice/AjaxHandler.php b/src/WPDesk/Notice/AjaxHandler.php index be5311e8e2317ec0acae870ef0a7a04487aedfb6..263e84b7ff630f977fdbd356c785ffc01792e152 100644 --- a/src/WPDesk/Notice/AjaxHandler.php +++ b/src/WPDesk/Notice/AjaxHandler.php @@ -94,10 +94,13 @@ class AjaxHandler implements HookablePluginDependant { PermanentDismissibleNotice::OPTION_VALUE_DISMISSED ); do_action( 'wpdesk_notice_dismissed_notice', $noticeName, $source ); + if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { + wp_send_json_success(); + } } } if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { - die(); + wp_send_json_error(); } } diff --git a/tests/codeception/tests/integration/AjaxHandlerTest.php b/tests/codeception/tests/integration/AjaxHandlerTest.php index 13f1572d43c91d92b5c88cdebb64365e138f7d61..c0f40f9992362437d5efe3657ea9a396eef03657 100644 --- a/tests/codeception/tests/integration/AjaxHandlerTest.php +++ b/tests/codeception/tests/integration/AjaxHandlerTest.php @@ -81,7 +81,7 @@ class AjaxHandlerTest extends WPTestCase { $_POST[ AjaxHandler::POST_FIELD_NOTICE_NAME ] = self::NOTICE_NAME; $_POST[ AjaxHandler::POST_FIELD_SECURITY ] = wp_create_nonce( PermanentDismissibleNotice::OPTION_NAME_PREFIX . sanitize_text_field( self::NOTICE_NAME ) ); - $ajaxHandler = new AjaxHandler( self::ASSETS_URL ); + $ajaxHandler = new AjaxHandler( self::ASSETS_URL ); $ajaxHandler->processAjaxNoticeDismiss(); $this->assertEquals( @@ -90,4 +90,17 @@ class AjaxHandlerTest extends WPTestCase { ); } + public function testShoulfNotProcessAjaxNoticeDismissWhenInvalidNonce() { + $_POST[ AjaxHandler::POST_FIELD_NOTICE_NAME ] = self::NOTICE_NAME; + $_POST[ AjaxHandler::POST_FIELD_SECURITY ] = wp_create_nonce(); + + $ajaxHandler = new AjaxHandler( self::ASSETS_URL ); + $ajaxHandler->processAjaxNoticeDismiss(); + + $this->assertNotEquals( + PermanentDismissibleNotice::OPTION_VALUE_DISMISSED, + get_option( PermanentDismissibleNotice::OPTION_NAME_PREFIX . self::NOTICE_NAME ) + ); + } + }