From 109db09bf8ba4e624e3788f4c847872189f745ec Mon Sep 17 00:00:00 2001 From: Grzegorz Rola <grola@seostudio.pl> Date: Sat, 6 Oct 2018 12:40:36 +0200 Subject: [PATCH] Changed paremters order, added factory, added functions. --- composer.json | 3 +- src/WPDesk/Notice/Factory.php | 47 ++++++++++++ src/WPDesk/Notice/Notice.php | 74 ++++++++++++++++++- .../Notice/PermanentDismissibleNotice.php | 6 +- src/WPDesk/functions.php | 31 ++++++++ 5 files changed, 154 insertions(+), 7 deletions(-) create mode 100644 src/WPDesk/Notice/Factory.php create mode 100644 src/WPDesk/functions.php diff --git a/composer.json b/composer.json index b8bf843..0d2a303 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,8 @@ "wimg/php-compatibility": "^8" }, "autoload": { - "psr-4": {"WPDesk\\Notice\\": "src/WPDesk/Notice/"} + "psr-4": {"WPDesk\\Notice\\": "src/WPDesk/Notice/"}, + "files": ["src/WPDesk/functions.php"] }, "autoload-dev": { }, diff --git a/src/WPDesk/Notice/Factory.php b/src/WPDesk/Notice/Factory.php new file mode 100644 index 0000000..9408ad3 --- /dev/null +++ b/src/WPDesk/Notice/Factory.php @@ -0,0 +1,47 @@ +<?php + +namespace WPDesk\Notice; + +/** + * Class Notice. + * + * @package WPDesk\Notice + */ +class Factory +{ + + /** + * Creates Notice object. + * + * @param string $noticeType Notice type. + * @param string $noticeContent Notice content. + * @param bool $isDismissible Is dismissible. + * @param int $priority Priority. + * + * @return Notice + */ + public static function notice($noticeContent = '', $noticeType = 'info', $isDismissible = false, $priority = 10) + { + return new Notice($noticeType, $noticeContent, $isDismissible, $priority); + } + + /** + * Creates PermanentDismissibleNotice object. + * + * @param string $noticeContent + * @param string $noticeType + * @param string $noticeName + * @param int $priority + * + * @return PermanentDismissibleNotice + */ + public static function permanentDismissibleNotice( + $noticeContent = '', + $noticeType = '', + $noticeName = '', + $priority = 10 + ) { + return new PermanentDismissibleNotice($noticeType, $noticeContent, $noticeName, $priority); + } + +} diff --git a/src/WPDesk/Notice/Notice.php b/src/WPDesk/Notice/Notice.php index cb22a16..3e39731 100644 --- a/src/WPDesk/Notice/Notice.php +++ b/src/WPDesk/Notice/Notice.php @@ -59,20 +59,88 @@ class Notice /** * WPDesk_Flexible_Shipping_Notice constructor. * - * @param string $noticeType Notice type. * @param string $noticeContent Notice content. + * @param string $noticeType Notice type. * @param bool $isDismissible Is dismissible. * @param int $priority Notice priority. */ - public function __construct($noticeType, $noticeContent, $isDismissible = false, $priority = 10) + public function __construct($noticeContent, $noticeType = 'info', $isDismissible = false, $priority = 10) { - $this->noticeType = $noticeType; $this->noticeContent = $noticeContent; + $this->noticeType = $noticeType; $this->isDismissible = $isDismissible; $this->priority = $priority; $this->addAction(); } + /** + * @return string + */ + public function getNoticeContent() + { + return $this->noticeContent; + } + + /** + * @param string $noticeContent + */ + public function setNoticeContent($noticeContent) + { + $this->noticeContent = $noticeContent; + } + + /** + * @return string + */ + public function getNoticeType() + { + return $this->noticeType; + } + + /** + * @param string $noticeType + */ + public function setNoticeType($noticeType) + { + $this->noticeType = $noticeType; + } + + /** + * @return bool + */ + public function isDismissible() + { + return $this->isDismissible; + } + + /** + * @param bool $isDismissible + */ + public function setIsDismissible($isDismissible) + { + $this->isDismissible = $isDismissible; + } + + /** + * @return int + */ + public function getPriority() + { + return $this->priority; + } + + /** + * @param int $priority + */ + public function setPriority($priority) + { + $this->priority = $priority; + if ($this->isActionAdded) { + $this->removeAction(); + $this->addAction(); + } + } + /** * Add notice action. */ diff --git a/src/WPDesk/Notice/PermanentDismissibleNotice.php b/src/WPDesk/Notice/PermanentDismissibleNotice.php index fb421c4..a2a3d59 100644 --- a/src/WPDesk/Notice/PermanentDismissibleNotice.php +++ b/src/WPDesk/Notice/PermanentDismissibleNotice.php @@ -26,15 +26,15 @@ class PermanentDismissibleNotice extends Notice /** * WPDesk_Flexible_Shipping_Notice constructor. * - * @param string $noticeType Notice type. * @param string $noticeContent Notice content. + * @param string $noticeType Notice type. * @param string $noticeName Notice dismiss option name. * @param int $priority Priority */ - public function __construct($noticeType, $noticeContent, $noticeName, $priority = 10) + public function __construct($noticeContent, $noticeType, $noticeName, $priority = 10) { - parent::__construct($noticeType, $noticeContent, true, $priority); + parent::__construct($noticeContent, $noticeType, true, $priority); $this->noticeName = $noticeName; $this->noticeDismissOptionName = static::OPTION_NAME_PREFIX . $noticeName; if (self::OPTION_VALUE_DISMISSED === get_option($this->noticeDismissOptionName, '')) { diff --git a/src/WPDesk/functions.php b/src/WPDesk/functions.php new file mode 100644 index 0000000..e6b9bed --- /dev/null +++ b/src/WPDesk/functions.php @@ -0,0 +1,31 @@ +<?php + +/** + * Creates Notice. + * + * @param $noticeContent + * @param string $noticeType + * @param bool $isDismissible + * @param int $priority + * + * @return \WPDesk\Notice\Notice + */ +function WPDeskNotice($noticeContent, $noticeType = 'info', $isDismissible = false, $priority = 10) +{ + return \WPDesk\Notice\Factory::notice($noticeContent, $noticeType, $isDismissible, $priority); +} + +/** + * Creates Notice. + * + * @param $noticeContent + * @param string $noticeType + * @param bool $isDismissible + * @param int $priority + * + * @return \WPDesk\Notice\Notice + */ +function WPDeskPermanentDismissibleNotice($noticeContent, $noticeType = 'info', $priority = 10) +{ + return \WPDesk\Notice\Factory::permanentDismissibleNotice($noticeContent, $noticeType, $priority); +} -- GitLab