PHP code example of duckdev / wp-flash-notices

1. Go to this page and download the library: Download duckdev/wp-flash-notices library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

duckdev / wp-flash-notices example snippets


$notices = \FoxeLabs\Notices\Notices::get_instance( 'my_plugin' );
$notices->register();

$notices = new \FoxeLabs\Notices\Notices( 'my_plugin', $my_store, $my_renderer );

$notices->add( $key, $message, $type = 'info', $dismissible = true, $network = false );

// Notice with a green bar.
$notices->add( 'settings-saved', 'Your settings have been saved.', 'success' );

// Notice with a red bar.
$notices->add( 'import-failed', 'The import could not be completed.', 'error' );

// Notice with a yellow bar.
$notices->add( 'license-expiring', 'Your license expires in 3 days.', 'warning' );

// Notice with a blue bar, without a dismiss button.
$notices->add( 'sync-running', 'A sync is currently running.', 'info', false );

// Network admin notice (multisite).
$notices->add( 'network-update', 'All sites have been updated.', 'success', true, true );

$notice  = $notices->get( 'settings-saved' ); // ?Notice
$notices->fetch();                            // Notice[] keyed by notice key
$notices->clear();                            // Delete the stored queue

do_action( 'my_plugin_front_notices' );

use FoxeLabs\Notices\Contracts\RendererInterface;
use FoxeLabs\Notices\Support\Notice;

class MyRenderer implements RendererInterface {

	public function render( array $notices ): void {
		foreach ( $notices as $notice ) {
			printf( '<div class="my-toast my-toast--%s">%s</div>', esc_attr( $notice->type() ), esc_html( $notice->message() ) );
		}
	}
}

$notices = new \FoxeLabs\Notices\Notices( 'my_plugin', null, new MyRenderer() );
$notices->register();

use FoxeLabs\Notices\Storage\TransientStore;
use FoxeLabs\Notices\Support\KeyPrefixer;

$store   = new TransientStore( new KeyPrefixer( 'my_plugin' ), HOUR_IN_SECONDS );
$notices = new \FoxeLabs\Notices\Notices( 'my_plugin', $store );

src/
├── Notices.php                      # Container + entry point
├── Contracts/
│   ├── StoreInterface.php
│   └── RendererInterface.php
├── Storage/
│   └── TransientStore.php           # (site_)transient wrapper, separate site + network queues
├── Rendering/
│   └── AdminNoticeRenderer.php      # Core admin notice markup
├── Support/
│   ├── KeyPrefixer.php              # Shared transient key + hook name prefixing
│   └── Notice.php                   # Immutable notice value object
└── Exceptions/
    └── NoticeException.php