first commit

This commit is contained in:
2023-09-12 21:41:04 +02:00
commit 3361a7f053
13284 changed files with 2116755 additions and 0 deletions

View File

@@ -0,0 +1,117 @@
# Persist Admin notice Dismissals
[![Latest Stable Version](https://poser.pugx.org/collizo4sky/persist-admin-notices-dismissal/v/stable)](https://packagist.org/packages/collizo4sky/persist-admin-notices-dismissal)
[![Total Downloads](https://poser.pugx.org/collizo4sky/persist-admin-notices-dismissal/downloads)](https://packagist.org/packages/collizo4sky/persist-admin-notices-dismissal)
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
```php
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,
```php
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:
```php
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.
```php
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:
```php
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' );
```
You should be a good developer and add the following to your `uninstall.php` file so that we can clean up after ourselves and not leave unnecessary stuff in the options table.
```php
global $wpdb;
$table = is_multisite() ? $wpdb->base_prefix . 'sitemeta' : $wpdb->base_prefix . 'options';
$column = is_multisite() ? 'meta_key' : 'option_name';
$delete_string = 'DELETE FROM ' . $table . ' WHERE ' . $column . ' LIKE %s LIMIT 1000';
$wpdb->query( $wpdb->prepare( $delete_string, array( '%pand-%' ) ) );
```
Cool beans. Isn't it?

View File

@@ -0,0 +1,33 @@
(function ($) {
//shorthand for ready event.
$(
function () {
$( 'div[data-dismissible] button.notice-dismiss' ).on('click',
function (event) {
event.preventDefault();
var $this = $( this );
var attr_value, option_name, dismissible_length, data;
attr_value = $this.parent().attr( 'data-dismissible' ).split( '-' );
// remove the dismissible length from the attribute value and rejoin the array.
dismissible_length = attr_value.pop();
option_name = attr_value.join( '-' );
data = {
'action': 'dismiss_admin_notice',
'option_name': option_name,
'dismissible_length': dismissible_length,
'nonce': dismissible_notice.nonce
};
// We can also pass the url value separately from ajaxurl for front end AJAX implementations
$.post( ajaxurl, data );
}
);
}
)
}(jQuery));

View File

@@ -0,0 +1,159 @@
<?php
/**
* Persist Admin notices Dismissal
*
* Copyright (C) 2016 Collins Agbonghama <http://w3guy.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package Persist Admin notices Dismissal
* @author Collins Agbonghama
* @author Andy Fragen
* @license http://www.gnu.org/licenses GNU General Public License
* @version 1.4.1
*/
/**
* Exit if called directly.
*/
if ( ! defined( 'ABSPATH' ) ) {
die;
}
if ( ! class_exists( 'PAnD' ) ) {
/**
* Class PAnD
*/
class PAnD {
/**
* Init hooks.
*/
public static function init() {
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'load_script' ) );
add_action( 'wp_ajax_dismiss_admin_notice', array( __CLASS__, 'dismiss_admin_notice' ) );
}
/**
* Enqueue javascript and variables.
*/
public static function load_script() {
if ( is_customize_preview() ) {
return;
}
wp_enqueue_script(
'dismissible-notices',
plugins_url( 'dismiss-notice.js', __FILE__ ),
array( 'jquery', 'common' ),
false,
true
);
wp_localize_script(
'dismissible-notices',
'dismissible_notice',
array(
'nonce' => wp_create_nonce( 'dismissible-notice' ),
)
);
}
/**
* Handles Ajax request to persist notices dismissal.
* Uses check_ajax_referer to verify nonce.
*/
public static function dismiss_admin_notice() {
$option_name = sanitize_text_field( $_POST['option_name'] );
$dismissible_length = sanitize_text_field( $_POST['dismissible_length'] );
if ( 'forever' != $dismissible_length ) {
// If $dismissible_length is not an integer default to 1
$dismissible_length = ( 0 == absint( $dismissible_length ) ) ? 1 : $dismissible_length;
$dismissible_length = strtotime( absint( $dismissible_length ) . ' days' );
}
check_ajax_referer( 'dismissible-notice', 'nonce' );
self::set_admin_notice_cache( $option_name, $dismissible_length );
wp_die();
}
/**
* Is admin notice active?
*
* @param string $arg data-dismissible content of notice.
*
* @return bool
*/
public static function is_admin_notice_active( $arg ) {
$array = explode( '-', $arg );
$length = array_pop( $array );
$option_name = implode( '-', $array );
$db_record = self::get_admin_notice_cache( $option_name );
if ( 'forever' == $db_record ) {
return false;
} elseif ( absint( $db_record ) >= time() ) {
return false;
} else {
return true;
}
}
/**
* Returns admin notice cached timeout.
*
* @access public
*
* @param string|bool $id admin notice name or false.
*
* @return array|bool The timeout. False if expired.
*/
public static function get_admin_notice_cache( $id = false ) {
if ( ! $id ) {
return false;
}
$cache_key = 'pand-' . md5( $id );
$timeout = get_site_option( $cache_key );
$timeout = 'forever' === $timeout ? time() + 60 : $timeout;
if ( empty( $timeout ) || time() > $timeout ) {
return false;
}
return $timeout;
}
/**
* Sets admin notice timeout in site option.
*
* @access public
*
* @param string $id Data Identifier.
* @param string|bool $timeout Timeout for admin notice.
*
* @return bool
*/
public static function set_admin_notice_cache( $id, $timeout ) {
$cache_key = 'pand-' . md5( $id );
update_site_option( $cache_key, $timeout );
return true;
}
}
}