Skip to content
Snippets Groups Projects
Commit 55ce2af9 authored by Dyszczo's avatar Dyszczo
Browse files

Merge branch 'feature/factory' into 'master'

Feature/factory

See merge request !4
parents 2910dca2 797f42a1
No related branches found
No related tags found
1 merge request!4Feature/factory
Pipeline #7766 failed
...@@ -45,7 +45,20 @@ require_once('/path/to/notice/src/init.php'); ...@@ -45,7 +45,20 @@ require_once('/path/to/notice/src/init.php');
Simple usage looks like: Simple usage looks like:
```php ```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
...@@ -25,7 +25,8 @@ ...@@ -25,7 +25,8 @@
"wimg/php-compatibility": "^8" "wimg/php-compatibility": "^8"
}, },
"autoload": { "autoload": {
"psr-4": {"WPDesk\\Notice\\": "src/WPDesk/Notice/"} "psr-4": {"WPDesk\\Notice\\": "src/WPDesk/Notice/"},
"files": ["src/WPDesk/functions.php"]
}, },
"autoload-dev": { "autoload-dev": {
}, },
......
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
<directory suffix=".php">src</directory> <directory suffix=".php">src</directory>
<exclude> <exclude>
<file>src/init.php</file> <file>src/init.php</file>
<directory suffix=".php">tests</directory>
<directory suffix=".php">vendor</directory>
</exclude> </exclude>
</whitelist> </whitelist>
</filter> </filter>
......
...@@ -6,7 +6,9 @@ use WPDesk\PluginBuilder\Plugin\HookablePluginDependant; ...@@ -6,7 +6,9 @@ use WPDesk\PluginBuilder\Plugin\HookablePluginDependant;
use WPDesk\PluginBuilder\Plugin\PluginAccess; use WPDesk\PluginBuilder\Plugin\PluginAccess;
/** /**
* Class Notice. * Class AjaxHandler
*
* AjaxHandler for dismissible notices.
* *
* @package WPDesk\Notice * @package WPDesk\Notice
*/ */
...@@ -61,6 +63,8 @@ class AjaxHandler implements HookablePluginDependant ...@@ -61,6 +63,8 @@ class AjaxHandler implements HookablePluginDependant
/** /**
* Process AJAX notice dismiss. * Process AJAX notice dismiss.
*
* Updates corresponded WordPress option and fires wpdesk_notice_dismissed_notice action with notice name.
*/ */
public function processAjaxNoticeDismiss() public function processAjaxNoticeDismiss()
{ {
......
<?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);
}
}
...@@ -3,8 +3,9 @@ ...@@ -3,8 +3,9 @@
namespace WPDesk\Notice; namespace WPDesk\Notice;
/** /**
* Class Notice. * Class Notice
* *
* WordPress admin notice.
* @package WPDesk\Notice * @package WPDesk\Notice
*/ */
class Notice class Notice
...@@ -34,7 +35,7 @@ class Notice ...@@ -34,7 +35,7 @@ class Notice
* *
* @var bool * @var bool
*/ */
protected $isDismissible; protected $dismissible;
/** /**
* Notice hook priority. * Notice hook priority.
...@@ -46,7 +47,7 @@ class Notice ...@@ -46,7 +47,7 @@ class Notice
* Is action added? * Is action added?
* @var bool * @var bool
*/ */
private $isActionAdded = false; private $actionAdded = false;
/** /**
* Attributes. * Attributes.
...@@ -59,36 +60,104 @@ class Notice ...@@ -59,36 +60,104 @@ class Notice
/** /**
* WPDesk_Flexible_Shipping_Notice constructor. * WPDesk_Flexible_Shipping_Notice constructor.
* *
* @param string $noticeType Notice type.
* @param string $noticeContent Notice content. * @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. * @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->noticeContent = $noticeContent;
$this->noticeType = $noticeType; $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; $this->noticeContent = $noticeContent;
$this->isDismissible = $isDismissible; }
/**
* @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; $this->priority = $priority;
if ($this->actionAdded) {
$this->removeAction();
$this->addAction(); $this->addAction();
} }
}
/** /**
* Add notice action. * Add notice action.
*/ */
protected function addAction() protected function addAction()
{ {
if (!$this->isActionAdded) { if (!$this->actionAdded) {
add_action('admin_notices', [$this, 'showNotice'], $this->priority); add_action('admin_notices', [$this, 'showNotice'], $this->priority);
$this->isActionAdded = true; $this->actionAdded = true;
} }
} }
protected function removeAction() protected function removeAction()
{ {
if ($this->isActionAdded) { if ($this->actionAdded) {
remove_action('admin_notices', [$this, 'showNotice'], $this->priority); remove_action('admin_notices', [$this, 'showNotice'], $this->priority);
$this->isActionAdded = false; $this->actionAdded = false;
} }
} }
...@@ -104,7 +173,7 @@ class Notice ...@@ -104,7 +173,7 @@ class Notice
} else { } else {
$notice_class = 'notice notice-' . $this->noticeType; $notice_class = 'notice notice-' . $this->noticeType;
} }
if ($this->isDismissible) { if ($this->dismissible) {
$notice_class .= ' is-dismissible'; $notice_class .= ' is-dismissible';
} }
return $notice_class; return $notice_class;
......
...@@ -3,8 +3,9 @@ ...@@ -3,8 +3,9 @@
namespace WPDesk\Notice; namespace WPDesk\Notice;
/** /**
* Class Notice. * Class PermanentDismissibleNotice
* *
* WordPress admin dismissible notice.
* @package WPDesk\Notice * @package WPDesk\Notice
*/ */
class PermanentDismissibleNotice extends Notice class PermanentDismissibleNotice extends Notice
...@@ -26,15 +27,15 @@ class PermanentDismissibleNotice extends Notice ...@@ -26,15 +27,15 @@ class PermanentDismissibleNotice extends Notice
/** /**
* WPDesk_Flexible_Shipping_Notice constructor. * WPDesk_Flexible_Shipping_Notice constructor.
* *
* @param string $noticeType Notice type.
* @param string $noticeContent Notice content. * @param string $noticeContent Notice content.
* @param string $noticeType Notice type.
* @param string $noticeName Notice dismiss option name. * @param string $noticeName Notice dismiss option name.
* @param int $priority Priority * @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->noticeName = $noticeName;
$this->noticeDismissOptionName = static::OPTION_NAME_PREFIX . $noticeName; $this->noticeDismissOptionName = static::OPTION_NAME_PREFIX . $noticeName;
if (self::OPTION_VALUE_DISMISSED === get_option($this->noticeDismissOptionName, '')) { if (self::OPTION_VALUE_DISMISSED === get_option($this->noticeDismissOptionName, '')) {
......
<?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);
}
<?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);
}
}
...@@ -16,20 +16,90 @@ class TestNotice extends WP_UnitTestCase ...@@ -16,20 +16,90 @@ class TestNotice extends WP_UnitTestCase
public function testShowNotice() 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>'); $this->expectOutputString('<div class="notice notice-info"><p>test</p></div>');
$notice->showNotice(); $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() 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>'); $this->expectOutputString('<div class="notice notice-info is-dismissible"><p>test</p></div>');
$notice->showNotice(); $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());
}
} }
...@@ -41,8 +41,8 @@ class TestPermanentDismissinleNotice extends WP_UnitTestCase ...@@ -41,8 +41,8 @@ class TestPermanentDismissinleNotice extends WP_UnitTestCase
public function testShowNotice() public function testShowNotice()
{ {
$notice = new PermanentDismissibleNotice( $notice = new PermanentDismissibleNotice(
PermanentDismissibleNotice::NOTICE_TYPE_INFO,
'test', 'test',
PermanentDismissibleNotice::NOTICE_TYPE_INFO,
'test_name' 'test_name'
); );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment