Giter Club home page Giter Club logo

persist-admin-notices-dismissal's Introduction

Persist Admin notice Dismissals

Latest Stable Version Total Downloads

Simple framework library that persists the dismissal of admin notices across pages in WordPress dashboard.

Installation

Run composer require collizo4sky/persist-admin-notices-dismissal

Alternatively, clone or download this repo into the vendor/ folder in your plugin, and include/require the persist-admin-notices-dismissal.php file like so

require  __DIR__ . '/vendor/persist-admin-notices-dismissal/persist-admin-notices-dismissal.php';
add_action( 'admin_init', array( 'PAnD', 'init' ) );

or let Composer's autoloader do the work.

How to Use

Firstly, install and activate this library within a plugin.

Say you have the following markup as your admin notice,

function sample_admin_notice__success() {
	?>
	<div class="updated notice notice-success is-dismissible">
    	<p><?php _e( 'Done!', 'sample-text-domain' ); ?></p>
	</div>
	<?php
}
add_action( 'admin_notices', 'sample_admin_notice__success' );

To make it hidden forever when dismissed, add the following data attribute data-dismissible="disable-done-notice-forever" to the div markup like so:

function sample_admin_notice__success() {
	if ( ! PAnD::is_admin_notice_active( 'disable-done-notice-forever' ) ) {
		return;
	}

	?>
	<div data-dismissible="disable-done-notice-forever" class="updated notice notice-success is-dismissible">
		<p><?php _e( 'Done!', 'sample-text-domain' ); ?></p>
	</div>
	<?php
}
add_action( 'admin_init', array( 'PAnD', 'init' ) );
add_action( 'admin_notices', 'sample_admin_notice__success' );

Autoloaders

When using the framework with an autoloader you must also load the class outside of the admin_notices or network_admin_notices hooks. The reason is that these hooks come after the admin_enqueue_script hook that loads the javascript.

Just add the following in your main plugin file.

add_action( 'admin_init', array( 'PAnD', 'init' ) );

Usage Instructions and Examples

If you have two notices displayed when certain actions are triggered; firstly, choose a string to uniquely identify them, e.g. notice-one and notice-two

To make the first notice never appear once dismissed, its data-dismissible attribute will be data-dismissible="notice-one-forever" where notice-one is its unique identifier and forever is the dismissal time period.

To make the second notice only hidden for 2 days, its data-dismissible attribute will be data-dismissible="notice-two-2" where notice-two is its unique identifier and the 2, the number of days it will be hidden is the dismissal time period.

You must append the dismissal time period to the end of your unique identifier with a hyphen (-) and this value must be an integer. The only exception is the string forever.

To actually make the dismissed admin notice not to appear, use the is_admin_notice_active() function like so:

function sample_admin_notice__success1() {
	if ( ! PAnD::is_admin_notice_active( 'notice-one-forever' ) ) {
		return;
	}

	?>
	<div data-dismissible="notice-one-forever" class="updated notice notice-success is-dismissible">
		<p><?php _e( 'Done 1!', 'sample-text-domain' ); ?></p>
	</div>
	<?php
}

function sample_admin_notice__success2() {
	if ( ! PAnD::is_admin_notice_active( 'notice-two-2' ) ) {
		return;
	}

	?>
	<div data-dismissible="notice-two-2" class="updated notice notice-success is-dismissible">
		<p><?php _e( 'Done 2!', 'sample-text-domain' ); ?></p>
	</div>
	<?php
}

add_action( 'admin_init', array( 'PAnD', 'init' ) );
add_action( 'admin_notices', 'sample_admin_notice__success1' );
add_action( 'admin_notices', 'sample_admin_notice__success2' );

Please note that if you cleanup after your plugin deletion please try to make the removal of stored options as specific as possible. Otherwise you may end up deleting the stored options from other projects.

A filter hook is available to return the proper URL to the Javascript file. An example usage is as follows, especially if this is being used in a theme.

add_filter( 'pand_theme_loader', '__return_true' );

The pand_theme_loader runs the following hook if true. You can directly change the URL to the Javascript file by using another hook in the following manner by changing the return value.

add_filter(
	'pand_dismiss_notice_js_url',
	function( $js_url, $composer_path ) {
		return get_stylesheet_directory_uri() . $composer_path;
	},
	10,
	2
);

Cool beans. Isn't it?

persist-admin-notices-dismissal's People

Contributors

afragen avatar lkoudal avatar surbma avatar w3guy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

persist-admin-notices-dismissal's Issues

WooCommerce WPCS

The WooCommerce CS is more stricter, than normal WPCS. It is giving errors for some @ tags in the comments:

  • @author tags are prohibited (WooCommerce.Commenting.CommentTags.AuthorTag)
  • @license tags are prohibited (WooCommerce.Commenting.CommentTags.LicenseTag)
  • @access tags are prohibited (WooCommerce.Commenting.CommentTags.AccessTag)

Please consider to change them to comply with WCCS also. This modification is required to validate the code for WooCommerce.com repository.

Example change in the persist-admin-notices-dismissal.php file:
@author Collins Agbonghama, Andy Fragen -> Author: Collins Agbonghama, Andy Fragen

Thank you if you consider this change in the code. That would make PAnD code valid for WC standards as well.

User & role specific dismissal

Hi,

I have a new project, where I need to set notification, but want to save closing on a per user basis. Even I need a solution to target user roles also to display different notifications to different roles.

Is this something you have plans to develop? Are there any similar plans for a future version of this application?

It needs to move saving from site options to user meta, I know. But it could open up new possibilities for developers.

Thank you if you consider to make this happen!

Theoretical collision

It's possible that 2 or more notices have the same name and collisions will result. I played with adding a hash but it wouldn't work as a static.

I'm still playing with it.

MultiSite - Scoping Dismissal to Blog or Site

Hey guys, just checking out your plugin for persistent, dismissable admin notices. I spotted that your using "get_site_option" and "update_site_option" which will indeed cover both standalone and multisite implementation of WordPress. However, if on a Multisite install you want to scope the dismissal to each blog, not the site as a whole using those functions will prevent that use case.

Was thinking maybe an additional argument / option for scope e.g. "site" / "blog" which could be used to conditionally call the required option functions. (Noting that this would also require extending the jquery routine to pop the scope also.)

Any appetite for that change?

not workng

Hi,
after i have create plugin and have add this line to the plugin i get this error:
Parse error: syntax error, unexpected 'add_action' (T_STRING)

require  __DIR__ . '/vendor/persist-admin-notices-dismissal/persist-admin-notices-dismissal.php'
add_action( 'admin_init', array( 'PAnD', 'init' ) );


function sample_admin_notice__success1() {
if ( ! is_admin_notice_active( 'notice-one-forever' ) ) {
        return;
    }
 
    ?>
    <div data-dismissible="notice-one-forever" class="updated notice notice-success is-dismissible">
        <p><?php _e( 'Done 1!', 'sample-text-domain' ); ?></p>
    </div>
    <?php
}
add_action( 'admin_notices', 'sample_admin_notice__success1' );

if i change from "require DIR" to:
include_once( plugin_dir_path( __FILE__ ) . '/vendor/persist-admin-notices-dismissal/persist-admin-notices-dismissal.php' );

i get this error:

Fatal error: Uncaught Error: Call to undefined function is_admin_notice_active() in

jquery deprecated warning

persist-admin-notices-dismissal/dismiss-notice.js: jQuery.fn.click() event shorthand is deprecated

switching to .on('click', func) is pretty straightforward :)

cleanup at uninstall issue

Great to have the notice dismissal status being strored in options iso transients (asked some people to test for whom transients did not work) and nice you thought of cleanup code to remove PAnD-options, but ... assuming a use case where 2 or more plugins are installed that use PAnD, this approach deletes the PAnD-options of all plugins, not only of the one being uninstalled?

So what if we save the option as

$cache_key = 'pand-' .md5(<calling plugin slug>) . '-' . md5( $id );

And delete only the pand-options for the plugin being uninstalled.

Question is if we can auto-populate or if we would need that to be passed as an option (which is ... tedious)?

frank

Update for WPCS

There are a couple of remaining WPCS errors.

----------------------------------------------------------------------------------------------------------------------------------
FOUND 11 ERRORS AND 3 WARNINGS AFFECTING 10 LINES
----------------------------------------------------------------------------------------------------------------------------------
   1 | ERROR   | [ ] There must be no blank lines before the file comment (Squiz.Commenting.FileComment.SpacingAfterOpen)
  23 | ERROR   | [ ] Only one @author tag is allowed in a file comment (Squiz.Commenting.FileComment.DuplicateAuthorTag)
  88 | ERROR   | [ ] Version parameter is not explicitly set or has been set to an equivalent of "false" for wp_enqueue_script;
     |         |     This means that the WordPress core version will be used which is not recommended for plugin or theme
     |         |     development. (WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion)
 110 | ERROR   | [ ] Detected usage of a possibly undefined superglobal array index: $_POST['option_name']. Use isset() or
     |         |     empty() to check the index exists before using it
     |         |     (WordPress.Security.ValidatedSanitizedInput.InputNotValidated)
 110 | ERROR   | [ ] $_POST data not unslashed before sanitization. Use wp_unslash() or similar
     |         |     (WordPress.Security.ValidatedSanitizedInput.MissingUnslash)
 111 | ERROR   | [ ] Detected usage of a possibly undefined superglobal array index: $_POST['dismissible_length']. Use isset() or
     |         |     empty() to check the index exists before using it
     |         |     (WordPress.Security.ValidatedSanitizedInput.InputNotValidated)
 111 | ERROR   | [ ] $_POST data not unslashed before sanitization. Use wp_unslash() or similar
     |         |     (WordPress.Security.ValidatedSanitizedInput.MissingUnslash)
 113 | WARNING | [ ] Found: !=. Use strict comparisons (=== or !==). (WordPress.PHP.StrictComparisons.LooseComparison)
 114 | ERROR   | [ ] Inline comments must end in full-stops, exclamation marks, or question marks
     |         |     (Squiz.Commenting.InlineComment.InvalidEndChar)
 115 | WARNING | [ ] Found: ==. Use strict comparisons (=== or !==). (WordPress.PHP.StrictComparisons.LooseComparison)
 137 | WARNING | [ ] Found: ==. Use strict comparisons (=== or !==). (WordPress.PHP.StrictComparisons.LooseComparison)

Considered packaging as a framework?

This would be great as a drop-in to a vendor directory and a simple require_once() rather than as a plugin.

Any thoughts to creating a branch for a drop-in framework?

Multiple notices and only first selected

I've been hacking on this all day.

If there are 2 or more notices. Selecting that last notice seems to only get the data from the first notice in $_POST. What this means is that selecting the second notice makes the first notice dismiss.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.