diff --git a/.editorconfig b/.editorconfig index d8c58929d60bca053b56a126723508d7aa050c5d..c394f5014f27a47bd99ee010987ad626ae7e31c8 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,15 +7,16 @@ root = true [*] +indent_style = space +indent_size = 4 charset = utf-8 end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true -indent_style = tab [*.yml] indent_style = space indent_size = 2 [*.md] -trim_trailing_whitespace = false \ No newline at end of file +trim_trailing_whitespace = false diff --git a/src/WPDesk/Notice/AjaxHandler.php b/src/WPDesk/Notice/AjaxHandler.php index fb1495ad62b30858b40f12ca9171edb7a6f0fb4e..7b8009e25144f0267a0a95abf577b6dc95aa01ed 100644 --- a/src/WPDesk/Notice/AjaxHandler.php +++ b/src/WPDesk/Notice/AjaxHandler.php @@ -15,12 +15,57 @@ class AjaxHandler implements HookablePluginDependant use PluginAccess; + const SCRIPTS_VERSION = '1'; + const POST_FIELD_NOTICE_NAME = 'notice-name'; + + /** + * @var string + */ + private $assetsURL; + + /** + * AjaxHandler constructor. + * + * @param string $assetsURL Assets URL. + */ + public function __construct($assetsURL) + { + $this->assetsURL = $assetsURL; + } + /** * Hooks. */ public function hooks() { - add_action(); + add_action('admin_enqueue_scripts', [$this, 'enqueueAdminScripts']); + add_action('wp_ajax_wpdesk_notice_dismiss', [$this, 'processAjaxNoticeDismiss']); + } + + /** + * Enqueue admin scripts. + */ + public function enqueueAdminScripts() + { + $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; + wp_register_script( + 'wpdesk_notice', + trailingslashit($this->assetsURL) . 'js/' . $suffix . '.js', + array( 'jquery' ), + self::SCRIPTS_VERSION + ); + } + + /** + * Process AJAX notice dismiss. + */ + public function processAjaxNoticeDismiss() + { + if (isset($_POST[self::POST_FIELD_NOTICE_NAME])) { + $noticeName = $_POST[self::POST_FIELD_NOTICE_NAME]; + delete_option(PermanentDismissibleNotice::OPTION_NAME_PREFIX . $noticeName); + } + die(); } } diff --git a/src/WPDesk/Notice/DismissibleNotice.php b/src/WPDesk/Notice/PermanentDismissibleNotice.php similarity index 61% rename from src/WPDesk/Notice/DismissibleNotice.php rename to src/WPDesk/Notice/PermanentDismissibleNotice.php index 3028fc116a80e119b7ae4c15ae9816d4b7fc11ad..6895a4ddf2e8984ff861747a4436e2f85d908ff4 100644 --- a/src/WPDesk/Notice/DismissibleNotice.php +++ b/src/WPDesk/Notice/PermanentDismissibleNotice.php @@ -7,9 +7,16 @@ namespace WPDesk\Notice; * * @package WPDesk\Notice */ -class DismissibleNotice extends Notice +class PermanentDismissibleNotice extends Notice { + const OPTION_NAME_PREFIX = 'wpdesk_notice_dismiss_'; + + /** + * @var string + */ + private $noticeName; + /** * @var string */ @@ -20,12 +27,13 @@ class DismissibleNotice extends Notice * * @param string $noticeType Notice type. * @param string $noticeContent Notice content. - * @param string $noticeDismissOptionName Notice dismiss option name. + * @param string $noticeName Notice dismiss option name. */ - public function __construct($noticeType, $noticeContent, $noticeDismissOptionName) + public function __construct($noticeType, $noticeContent, $noticeName) { parent::__construct($noticeType, $noticeContent, true); - $this->noticeDismissOptionName = $noticeContent; + $this->noticeName = $noticeName; + $this->noticeDismissOptionName = static::OPTION_NAME_PREFIX . $noticeName; } /** @@ -36,7 +44,7 @@ class DismissibleNotice extends Notice protected function getAttributesAsString() { $attributesAsString = parent::getAttributesAsString(); - $attributesAsString .= sprintf('data-dismiss-option="%1$s"', esc_attr($this->noticeDismissOptionName)); + $attributesAsString .= sprintf('data-notice-name="%1$s"', esc_attr($this->noticeName)); return $attributesAsString; }