Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: adds support for alternate WP styles #3

Merged
merged 5 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,58 @@ $notice = AdminNotices::show('my_notice', 'This is a notice')
->urgency($urgency);
```

### `alternateStyles($useAlternate)`, `standardStyles()`

**Default:** false

Sets whether the notice should use the alternate WordPress notice styles. **Only works when the
wrapper is enabled.**

Parameters:

1. `bool $useAlternate = true` - Whether the notice should use the alternate WordPress notice
styles

```php
use StellarWP\AdminNotices\AdminNotices;

// Use the alternate WordPress notice styles
$notice = AdminNotices::show('my_notice', 'This is a notice')
->alternateStyles();

// Use the standard WordPress notice styles, only necessary to revert back
$notice = AdminNotices::show('my_notice', 'This is a notice')
->alternateStyles()
->standardStyles();
```

### `inline($inline)`, `notInline()`

**Default:** false

Sets whether the notice should be displayed in the WP "inline" location, at the top of the admin
page. **Only works when the wrapper is enabled.**

Parameters:

1. `bool $inline = true` - Whether the notice should be displayed inline

```php
use StellarWP\AdminNotices\AdminNotices;

// Display the notice inline
$notice = AdminNotices::show('my_notice', 'This is a notice')
->inline();

// Display the notice in the standard location
$notice = AdminNotices::show('my_notice', 'This is a notice')
->inline(false);

// Also has an alias for readability
$notice = AdminNotices::show('my_notice', 'This is a notice')
->notInline();
```

### `dismissible($dismissible)`, `notDismissible()`

**Default:** false
Expand Down
4 changes: 4 additions & 0 deletions src/Actions/RenderAdminNotice.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ private function getWrapperClasses(AdminNotice $notice): string
$classes[] = 'inline';
}

if ($notice->usesAlternateStyles()) {
$classes[] = 'notice-alt';
}

return implode(' ', $classes);
}
}
59 changes: 59 additions & 0 deletions src/AdminNotice.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class AdminNotice
*/
protected $urgency;

/**
* @var bool
*/
protected $alternateStyles = false;

/**
* @var bool
*/
Expand Down Expand Up @@ -238,26 +243,80 @@ public function urgency($urgency): self
return $this;
}

/**
* Alias for setting the urgency to info
*
* @since 1.1.0
*/
public function asInfo(): self
{
return $this->urgency(NoticeUrgency::info());
}

/**
* Alias for setting the urgency to success
*
* @since 1.1.0
*/
public function asSuccess(): self
{
return $this->urgency(NoticeUrgency::success());
}

/**
* Alias for setting the urgency to warning
*
* @since 1.1.0
*/
public function asWarning(): self
{
return $this->urgency(NoticeUrgency::warning());
}

/**
* Alias for setting the urgency to error
*
* @since 1.1.0
*/
public function asError(): self
{
return $this->urgency(NoticeUrgency::error());
}

/**
* Uses the alternate WP notice styles
*
* @unreleased
*/
public function alternateStyles(bool $altStyle = true): self
{
$this->alternateStyles = $altStyle;

return $this;
}

/**
* Uses the standard WP notice styles
*
* @unreleased
*/
public function standardStyles(): self
{
$this->alternateStyles = false;

return $this;
}

/**
* Returns whether the notice uses the alternate WP notice styles
*
* @unreleased
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @unreleased
* @unreleased

Do these need to be updated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stratease I'll update them as after this PR is merged and as part of prepping for release. 😄

*/
public function usesAlternateStyles(): bool
{
return $this->alternateStyles;
}

/**
* Sets the notice to display without the standard WordPress wrapper
*
Expand Down
18 changes: 9 additions & 9 deletions tests/unit/Actions/DisplayNoticesInAdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DisplayNoticesInAdminTest extends TestCase
protected $originalServer;

/**
* @unreleased
* @since 1.0.0
*/
protected function setUp(): void
{
Expand All @@ -30,7 +30,7 @@ protected function setUp(): void
}

/**
* @unreleased
* @since 1.0.0
*/
protected function tearDown(): void
{
Expand All @@ -39,7 +39,7 @@ protected function tearDown(): void
}

/**
* @unreleased
* @since 1.0.0
*/
public function testShouldEchoNothingWithNoNotices(): void
{
Expand All @@ -50,7 +50,7 @@ public function testShouldEchoNothingWithNoNotices(): void
}

/**
* @unreleased
* @since 1.0.0
*/
public function testShouldAcceptMultipleNotices(): void
{
Expand All @@ -66,7 +66,7 @@ public function testShouldAcceptMultipleNotices(): void
* @covers ::passesDateLimits
* @dataProvider passDateLimitsDataProvider
*
* @unreleased
* @since 1.0.0
*/
public function testPassesDateLimits(AdminNotice $notice, bool $shouldPass): void
{
Expand Down Expand Up @@ -104,7 +104,7 @@ public function passDateLimitsDataProvider(): array
* @dataProvider passWhenCallbackDataProvider
* @covers ::passesWhenCallback
*
* @unreleased
* @since 1.0.0
*/
public function testPassesWhenCallback(AdminNotice $notice, bool $shouldPass): void
{
Expand All @@ -120,7 +120,7 @@ public function testPassesWhenCallback(AdminNotice $notice, bool $shouldPass): v
}

/**
* @unreleased
* @since 1.0.0
*/
public function passWhenCallbackDataProvider(): array
{
Expand All @@ -145,7 +145,7 @@ public function passWhenCallbackDataProvider(): array
* @dataProvider passUserCapabilitiesDataProvider
* @covers ::passesUserCapabilities
*
* @unreleased
* @since 1.0.0
*/
public function testPassesUserCapabilities(AdminNotice $notice, bool $shouldPass): void
{
Expand Down Expand Up @@ -241,7 +241,7 @@ public function testShouldPassScreenConditionsWhenConditionMatchesWPScreen(): vo
/**
* Produces a simple mock with predictable output.
*
* @unreleased
* @since 1.0.0
*/
private function getSimpleMockNotice($output): AdminNotice
{
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/Actions/RenderAdminNoticeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class RenderAdminNoticeTest extends TestCase
{
/**
* @unreleased
* @since 1.0.0
*/
public function testShouldRenderNoticeWithoutWrapper(): void
{
Expand All @@ -25,7 +25,7 @@ public function testShouldRenderNoticeWithoutWrapper(): void
/**
* Tests the wrapper and that the default urgency is info
*
* @unreleased
* @since 1.0.0
*/
public function testShouldRenderNoticeInWrapper(): void
{
Expand All @@ -42,7 +42,7 @@ public function testShouldRenderNoticeInWrapper(): void
}

/**
* @unreleased
* @since 1.0.0
*/
public function testShouldIncludeDismissibleClass(): void
{
Expand All @@ -59,7 +59,7 @@ public function testShouldIncludeDismissibleClass(): void
}

/**
* @unreleased
* @since 1.0.0
*/
public function testShouldIncludeAutoParagraphs(): void
{
Expand All @@ -77,7 +77,7 @@ public function testShouldIncludeAutoParagraphs(): void
}

/**
* @unreleased
* @since 1.0.0
*/
public function testShouldRenderCallbackOutput(): void
{
Expand Down
Loading
Loading