From c269953dd9daab07adac9c8d0809f2ccd83f7001 Mon Sep 17 00:00:00 2001 From: Grzegorz Rola <grola@seostudio.pl> Date: Wed, 26 Sep 2018 11:43:46 +0200 Subject: [PATCH] init --- .editorconfig | 5 +- src/WPDesk/Notice/AjaxHandler.php | 47 ++++++++++++++++++- ...ice.php => PermanentDismissibleNotice.php} | 18 +++++-- 3 files changed, 62 insertions(+), 8 deletions(-) rename src/WPDesk/Notice/{DismissibleNotice.php => PermanentDismissibleNotice.php} (61%) diff --git a/.editorconfig b/.editorconfig index d8c5892..c394f50 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 fb1495a..7b8009e 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 3028fc1..6895a4d 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; } -- GitLab