diff --git a/README.md b/README.md index b96ce4a16b77277c951ae31a2f52362fa71f2f2a..2aa5ce9200517d084333f258b5b1612196884c6a 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,20 @@ require_once('/path/to/notice/src/init.php'); Simple usage looks like: ```php -$notice = new \WPDesk\Notice\Notice( 'info', 'Notice text goes here' ); +$notice = wpdesk_notice('Notice text goes here'); + +// Is equivalent to: +$notice = WPDeskNotice('Notice text goes here'); + +// Is equivalent to: +$notice = \WPDesk\Notice\Factory::notice('Notice text goes here'); + +// Is equivalent to: +$notice = new \WPDesk\Notice\Notice('Notice text goes here'); ``` -Notice must be used before WordPress action `admin_notices`. WordPress admin actions order is listed [here](https://codex.wordpress.org/Plugin_API/Action_Reference#Actions_Run_During_an_Admin_Page_Request). +Notice must be used before WordPress action `admin_notices`. WordPress admin actions order is listed [here](https://codex.wordpress.org/Plugin_API/Action_Reference#Actions_Run_During_an_Admin_Page_Request). + +## Project documentation + +PHPDoc: https://wpdesk.gitlab.io/wp-notice/index.html diff --git a/composer.json b/composer.json index b8bf84395e3de21adc9b84db656b975742c3c6b0..0d2a30309fe01f4379746d82de7c68ffbe0a729b 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/phpunit-integration.xml b/phpunit-integration.xml index b989fa53c2b5307d4f55789c5353b45f2748f8d1..4ab26660b6b026743711f94b76a0f2866493ed5f 100644 --- a/phpunit-integration.xml +++ b/phpunit-integration.xml @@ -12,6 +12,8 @@ <directory suffix=".php">src</directory> <exclude> <file>src/init.php</file> + <directory suffix=".php">tests</directory> + <directory suffix=".php">vendor</directory> </exclude> </whitelist> </filter> diff --git a/src/WPDesk/Notice/AjaxHandler.php b/src/WPDesk/Notice/AjaxHandler.php index 4ffe395134a3c31ffb8de3dc1f6356e483f72263..0df30006a9ccd180d936303f3dbca6e9a1db977a 100644 --- a/src/WPDesk/Notice/AjaxHandler.php +++ b/src/WPDesk/Notice/AjaxHandler.php @@ -6,7 +6,9 @@ use WPDesk\PluginBuilder\Plugin\HookablePluginDependant; use WPDesk\PluginBuilder\Plugin\PluginAccess; /** - * Class Notice. + * Class AjaxHandler + * + * AjaxHandler for dismissible notices. * * @package WPDesk\Notice */ @@ -61,6 +63,8 @@ class AjaxHandler implements HookablePluginDependant /** * Process AJAX notice dismiss. + * + * Updates corresponded WordPress option and fires wpdesk_notice_dismissed_notice action with notice name. */ public function processAjaxNoticeDismiss() { diff --git a/src/WPDesk/Notice/Factory.php b/src/WPDesk/Notice/Factory.php new file mode 100644 index 0000000000000000000000000000000000000000..2deacf115b81f7bbdf10c8940767bf9c66fca306 --- /dev/null +++ b/src/WPDesk/Notice/Factory.php @@ -0,0 +1,48 @@ +<?php + +namespace WPDesk\Notice; + +/** + * Class Factory + * + * Factory for notices. + * @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 cb22a16da8e5ba7fe2b7679e3da756ce5c7b8f83..57c774d22203c5e10fc8e831fdaded1d0e6298fd 100644 --- a/src/WPDesk/Notice/Notice.php +++ b/src/WPDesk/Notice/Notice.php @@ -3,8 +3,9 @@ namespace WPDesk\Notice; /** - * Class Notice. + * Class Notice * + * WordPress admin notice. * @package WPDesk\Notice */ class Notice @@ -34,7 +35,7 @@ class Notice * * @var bool */ - protected $isDismissible; + protected $dismissible; /** * Notice hook priority. @@ -46,7 +47,7 @@ class Notice * Is action added? * @var bool */ - private $isActionAdded = false; + private $actionAdded = false; /** * Attributes. @@ -59,36 +60,104 @@ class Notice /** * WPDesk_Flexible_Shipping_Notice constructor. * - * @param string $noticeType Notice type. * @param string $noticeContent Notice content. - * @param bool $isDismissible Is dismissible. + * @param string $noticeType Notice type. + * @param bool $dismissible Is dismissible. * @param int $priority Notice priority. */ - public function __construct($noticeType, $noticeContent, $isDismissible = false, $priority = 10) + public function __construct($noticeContent, $noticeType = 'info', $dismissible = false, $priority = 10) { - $this->noticeType = $noticeType; $this->noticeContent = $noticeContent; - $this->isDismissible = $isDismissible; - $this->priority = $priority; + $this->noticeType = $noticeType; + $this->dismissible = $dismissible; + $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->dismissible; + } + + /** + * @param bool $dismissible + */ + public function setDismissible($dismissible) + { + $this->dismissible = $dismissible; + } + + /** + * @return int + */ + public function getPriority() + { + return $this->priority; + } + + /** + * @param int $priority + */ + public function setPriority($priority) + { + $this->priority = $priority; + if ($this->actionAdded) { + $this->removeAction(); + $this->addAction(); + } + } + /** * Add notice action. */ protected function addAction() { - if (!$this->isActionAdded) { + if (!$this->actionAdded) { add_action('admin_notices', [$this, 'showNotice'], $this->priority); - $this->isActionAdded = true; + $this->actionAdded = true; } } protected function removeAction() { - if ($this->isActionAdded) { + if ($this->actionAdded) { remove_action('admin_notices', [$this, 'showNotice'], $this->priority); - $this->isActionAdded = false; + $this->actionAdded = false; } } @@ -104,7 +173,7 @@ class Notice } else { $notice_class = 'notice notice-' . $this->noticeType; } - if ($this->isDismissible) { + if ($this->dismissible) { $notice_class .= ' is-dismissible'; } return $notice_class; diff --git a/src/WPDesk/Notice/PermanentDismissibleNotice.php b/src/WPDesk/Notice/PermanentDismissibleNotice.php index fb421c4272744a97deefa8392da8c0a08a659ad2..ea31b5aabbf4cf8f99bb6154a69fd9cee7094a7c 100644 --- a/src/WPDesk/Notice/PermanentDismissibleNotice.php +++ b/src/WPDesk/Notice/PermanentDismissibleNotice.php @@ -3,8 +3,9 @@ namespace WPDesk\Notice; /** - * Class Notice. + * Class PermanentDismissibleNotice * + * WordPress admin dismissible notice. * @package WPDesk\Notice */ class PermanentDismissibleNotice extends Notice @@ -26,15 +27,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 0000000000000000000000000000000000000000..c7f6dd261e99c9f8f334bcb8c407bb9542e7ccf0 --- /dev/null +++ b/src/WPDesk/functions.php @@ -0,0 +1,63 @@ +<?php + +/** + * Creates Notice. + * + * @param string $noticeContent Notice content. + * @param string $noticeType Notice type. + * @param bool $dismissible Dismissible notice. + * @param int $priority Notice priority, + * + * @return \WPDesk\Notice\Notice + */ +function WPDeskNotice($noticeContent, $noticeType = 'info', $dismissible = false, $priority = 10) +{ + return \WPDesk\Notice\Factory::notice($noticeContent, $noticeType, $dismissible, $priority); +} + +/** + * Creates Notice. + * + * Alias for {@see WPDeskNotice()} function. + * + * @param string $noticeContent Notice content. + * @param string $noticeType Notice type. + * @param bool $dismissible Dismissible notice. + * @param int $priority Notice priority, + * + * @return \WPDesk\Notice\Notice + */ +function wpdesk_notice($noticeContent, $noticeType = 'info', $dismissible = false, $priority = 10) +{ + return WPDeskNotice($noticeContent, $noticeType, $dismissible, $priority); +} + +/** + * Creates Permanent Dismissible Notice. + * + * @param string $noticeContent Notice content. + * @param string $noticeType Notice type. + * @param int $priority Notice priority. + * + * @return \WPDesk\Notice\Notice + */ +function WPDeskPermanentDismissibleNotice($noticeContent, $noticeType = 'info', $priority = 10) +{ + return \WPDesk\Notice\Factory::permanentDismissibleNotice($noticeContent, $noticeType, $priority); +} + +/** + * Creates Permanent Dismissible Notice. + * + * Alias for {@see WPDeskPermanentDismissibleNotice()} function. + * + * @param string $noticeContent Notice content. + * @param string $noticeType Notice type. + * @param int $priority Notice priority. + * + * @return \WPDesk\Notice\Notice + */ +function wpdesk_permanent_dismissible_notice($noticeContent, $noticeType = 'info', $priority = 10) +{ + return WPDeskPermanentDismissibleNotice($noticeContent, $noticeType, $priority); +} diff --git a/tests/integration/TestFunctions.php b/tests/integration/TestFunctions.php new file mode 100644 index 0000000000000000000000000000000000000000..2e054b2deee45de992486308439e2305e28d3939 --- /dev/null +++ b/tests/integration/TestFunctions.php @@ -0,0 +1,32 @@ +<?php + +use \WPDesk\Notice\Notice; +use \WPDesk\Notice\PermanentDismissibleNotice; + +/** + * Class TestFunctions + */ +class TestFunctions extends WP_UnitTestCase +{ + + /** + * Test WPDeskNotice function. + */ + public function testWPDeskNotice() + { + $notice = wpdesk_notice('test'); + + $this->assertInstanceOf(Notice::class, $notice); + } + + /** + * Test WPDeskPermanentDismissibleNotice function. + */ + public function testWPDeskPermanentDismissibleNotice() + { + $notice = wpdesk_permanent_dismissible_notice('test'); + + $this->assertInstanceOf(PermanentDismissibleNotice::class, $notice); + } + +} diff --git a/tests/integration/TestNotice.php b/tests/integration/TestNotice.php index 96101e13a34312d4ea0bb7ea036bec2126c80645..3cdad40a27110414ffccb6e795931a33584a145d 100644 --- a/tests/integration/TestNotice.php +++ b/tests/integration/TestNotice.php @@ -16,20 +16,90 @@ class TestNotice extends WP_UnitTestCase public function testShowNotice() { - $notice = new Notice(Notice::NOTICE_TYPE_INFO, 'test'); + $notice = new Notice('test'); $this->expectOutputString('<div class="notice notice-info"><p>test</p></div>'); $notice->showNotice(); } + public function testShowNoticeError() + { + $notice = new Notice('test', Notice::NOTICE_TYPE_ERROR); + + $this->expectOutputString('<div class="notice notice-error"><p>test</p></div>'); + + $notice->showNotice(); + } + + public function testShowNoticeWarning() + { + $notice = new Notice('test', Notice::NOTICE_TYPE_WARNING); + + $this->expectOutputString('<div class="notice notice-warning"><p>test</p></div>'); + + $notice->showNotice(); + } + + public function testShowNoticeSuccess() + { + $notice = new Notice('test', Notice::NOTICE_TYPE_SUCCESS); + + $this->expectOutputString('<div class="notice notice-success"><p>test</p></div>'); + + $notice->showNotice(); + } + public function testShowNoticeDismissible() { - $notice = new Notice(Notice::NOTICE_TYPE_INFO, 'test', true); + $notice = new Notice('test', Notice::NOTICE_TYPE_INFO, true); $this->expectOutputString('<div class="notice notice-info is-dismissible"><p>test</p></div>'); $notice->showNotice(); } + public function testNoticeContent() + { + $noticeContent = 'test'; + + $notice = new Notice($noticeContent); + + $this->assertEquals($noticeContent, $notice->getNoticeContent()); + + $noticeContent = 'test 2'; + $notice->setNoticeContent($noticeContent); + $this->assertEquals($noticeContent, $notice->getNoticeContent()); + } + + public function testNoticeType() + { + $notice = new Notice('test', Notice::NOTICE_TYPE_INFO); + + $this->assertEquals(Notice::NOTICE_TYPE_INFO, $notice->getNoticeType()); + + $notice->setNoticeType(Notice::NOTICE_TYPE_ERROR); + $this->assertEquals(Notice::NOTICE_TYPE_ERROR, $notice->getNoticeType()); + } + + public function testDismissible() + { + $notice = new Notice('test'); + + $this->assertFalse($notice->isDismissible()); + + $notice->setDismissible(true); + $this->assertTrue($notice->isDismissible()); + } + + public function testPriority() + { + $notice = new Notice('test'); + + $this->assertEquals(10, $notice->getPriority()); + + $notice->setPriority(20); + $this->assertEquals(20, $notice->getPriority()); + } + } diff --git a/tests/integration/TestPermanentDismissinleNotice.php b/tests/integration/TestPermanentDismissinleNotice.php index 2a403359a2b0bc2bc7b5a1ae8dc35576a546f699..b250b3247070ac2febff20877391bf847639eb53 100644 --- a/tests/integration/TestPermanentDismissinleNotice.php +++ b/tests/integration/TestPermanentDismissinleNotice.php @@ -41,8 +41,8 @@ class TestPermanentDismissinleNotice extends WP_UnitTestCase public function testShowNotice() { $notice = new PermanentDismissibleNotice( - PermanentDismissibleNotice::NOTICE_TYPE_INFO, 'test', + PermanentDismissibleNotice::NOTICE_TYPE_INFO, 'test_name' );