Compare commits

...

7 commits
v1.0 ... master

Author SHA1 Message Date
Joel James
d3769c722f
Update README.md 2021-05-14 14:56:44 +05:30
Joel James
5f73e11803
Update wp-flash-notices.php 2021-05-14 14:56:09 +05:30
Joel James
cbf03b53d0
Merge pull request #1 from codelight-eu/master
Fix fetching notices by passing the correct value
2021-05-14 14:54:51 +05:30
Mostafa Soufi
8bb8a34a4f Support frontend hook 2021-05-14 12:08:02 +03:00
Mostafa Soufi
4eb163f91f Fix fetching notices by passing the correct value 2021-05-12 12:10:46 +03:00
Joel James
07098f3a56 📦 Releasing 1.0.1 2019-07-25 18:34:17 +05:30
Joel James
62273eb3bd 👌 Added transient name to hooks 2019-07-18 19:34:22 +05:30
3 changed files with 75 additions and 26 deletions

View file

@ -80,4 +80,12 @@ By default all notices are shown within single site admin screens. If you have M
```php
// Register new info notice to the queue.
$notices->add( 'custom-success', 'This is a custom dismissible notice.', 'success', true, true );
```
```


### Frontend Compatibility
If you'd like to print the notices in the front-end, then use the below action to your template.

```php
do_action( 'front_notices' );
```

View file

@ -1,6 +1,7 @@
{
"name": "duckdev/wp-flash-notices",
"description": "WordPress admin notices as flash notice using transient API to display after page reload.",
"version": "1.0.1",
"license": "GPL-2.0+",
"authors": [
{

View file

@ -64,12 +64,27 @@ class WP_Flash_Notices {
*
* @since 1.0.0
*/
public function __construct( $transient = 'jj_wp_flash_notices' ) {
public function __construct( $transient = 'duckdev_wp_flash_notices' ) {
// Make sure you set this unique, otherwise all notices will mixup.
$this->transient = $transient;

/**
* Filter hook to add new notice types.
*
* @param array $types Notice types (default wp notices).
* @param string $transient Transient name.
*
* @since 1.0.1
*/
$this->types = apply_filters(
'wp_flash_notices_notice_types',
$this->types,
$this->transient
);

// Render notices using WP action.
add_action( 'admin_notices', [ $this, 'render' ] );
add_action( 'front_notices', [ $this, 'render' ] );
add_action( 'network_admin_notices', [ $this, 'render' ] );

// Save all queued notices to transient.
@ -106,11 +121,13 @@ class WP_Flash_Notices {
/**
* Filter hook to modify notice item before adding to queue.
*
* @param array $notice Notice item.
* @param string $key Notice key.
* @param array $notice Notice item.
* @param string $transient Transient name to identify the plugin.
*
* @since 1.0.0
*/
apply_filters( 'wp_flash_notices_notice_item', $notice );
apply_filters( 'wp_flash_notices_notice_item', $key, $notice, $this->transient );

// Multisite network admin notices.
if ( is_multisite() && $network ) {
@ -126,14 +143,16 @@ class WP_Flash_Notices {
* Note: The notice item is not yet added to the transient when this
* action hook is fired.
*
* @param array $notice Notice item inserted.
* @param array $notices Current notice queue.
* @param array $network_notices Current network notices queue.
* @param array $notice Notice item inserted.
* @param string $transient Transient name to identify the plugin.
* @param array $notices Current notice queue.
* @param array $network_notices Current network notices queue.
*
* @since 1.0.0
*/
do_action( 'wp_flash_notices_after_queue_insert',
$notice,
$this->transient,
$this->notices,
$this->network_notices
);
@ -163,12 +182,14 @@ class WP_Flash_Notices {
/**
* Action hook fired after saving queued notices to transient.
*
* @param array $notices Notice queue.
* @param array $network_notices Network notices queue.
* @param string $transient Transient name to identify the plugin.
* @param array $notices Notice queue.
* @param array $network_notices Network notices queue.
*
* @since 1.0.0
*/
do_action( 'wp_flash_notices_after_save',
$this->transient,
$this->notices,
$this->network_notices
);
@ -193,13 +214,19 @@ class WP_Flash_Notices {
/**
* Filter hook to modify the notice item.
*
* @param array $notice Current notice.
* @param array $notices Notice list.
* @param bool $network Is network notice?.
* @param array $notice Current notice.
* @param string $transient Transient name to identify the plugin.
* @param array $notices Notice list.
* @param bool $network Is network notice?.
*
* @since 1.0.0
*/
return apply_filters( 'wp_flash_notices_get', $notice, $notices, $network );
return apply_filters( 'wp_flash_notices_get',
$notice,
$this->transient,
$notices,
$network
);
}

/**
@ -223,12 +250,17 @@ class WP_Flash_Notices {
/**
* Filter hook to modify the fetched notices array.
*
* @param array $notices Notice list.
* @param bool $network Is network notice?.
* @param array $notices Notice list.
* @param string $transient Transient name to identify the plugin.
* @param bool $network Is network notice?.
*
* @since 1.0.0
*/
return apply_filters( 'wp_flash_notices_fetch', $notices, $network );
return apply_filters( 'wp_flash_notices_fetch',
$notices,
$this->transient,
$network
);
}

/**
@ -253,11 +285,12 @@ class WP_Flash_Notices {
/**
* Action hook fired after clearing notices from transient.
*
* @param bool $network Network notice?.
* @param string $transient Transient name to identify the plugin.
* @param bool $network Network notice?.
*
* @since 1.0.0
*/
do_action( 'wp_flash_notices_after_clear', $network );
do_action( 'wp_flash_notices_after_clear', $this->transient, $network );
}

/**
@ -278,11 +311,12 @@ class WP_Flash_Notices {
/**
* Filter hook to disable auto rendering of admin notices.
*
* @param bool $enable Should render.
* @param bool $enable Should render.
* @param string $transient Transient name to identify the plugin.
*
* @since 1.0.0
*/
if ( apply_filters( 'wp_flash_notices_auto_render', true ) ) {
if ( apply_filters( 'wp_flash_notices_auto_render', true, $this->transient ) ) {
$network = is_network_admin();

// Get all notices.
@ -316,12 +350,17 @@ class WP_Flash_Notices {
*
* We are using this hook to clear all notices after rendering.
*
* @param array $notices Notices array.
* @param bool $network Is network admin?.
* @param string $transient Transient name to identify the plugin.
* @param array $notices Notices array.
* @param bool $network Is network admin?.
*
* @since 1.0.0
*/
do_action( 'wp_flash_notices_after_render', $notices, $network );
do_action( 'wp_flash_notices_after_render',
$this->transient,
$notices,
$network
);
}
}

@ -345,13 +384,14 @@ class WP_Flash_Notices {
* notices using `clear` method. Otherwise notices will be there
* and there no point in using this library anymore.
*
* @param bool $enable Should clear automatically after render?.
* @param bool $enable Should clear automatically after render?.
* @param string $transient Transient name to identify the plugin.
*
* @since 1.0.0
*/
if ( apply_filters( 'wp_flash_notices_auto_clear', true ) ) {
if ( apply_filters( 'wp_flash_notices_auto_clear', true, $this->transient ) ) {
// Clear all notices.
$this->clear( $network );
}
}
}
}