mirror of
https://github.com/afragen/wp-dismiss-notice.git
synced 2025-11-23 10:57:59 +08:00
Compare commits
20 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3e2c694ca8 | ||
|
|
63fbe9fd01 | ||
|
|
2e47f5fec3 | ||
|
|
a195d3badb | ||
|
|
606c0d5222 | ||
|
|
5a3dd3198c | ||
|
|
2c421b09e8 | ||
|
|
8b690bc045 | ||
|
|
d6511c4eea | ||
|
|
71029356cc | ||
|
|
a5c05306a4 | ||
|
|
21949732f1 | ||
|
|
3b9d3b775f | ||
|
|
c2e737fe14 | ||
|
|
7864d89816 | ||
|
|
cfb4384f6f | ||
|
|
1534d3dd13 | ||
|
|
a0ced98366 | ||
|
|
1ce23d619d | ||
|
|
9ab86e3825 |
4 changed files with 37 additions and 88 deletions
92
README.md
92
README.md
|
|
@ -9,96 +9,22 @@ Initialize the class.
|
|||
|
||||
`new \WP_Dismiss_Notice();` in your project.
|
||||
|
||||
Admin notice format.
|
||||
### Admin notice format.
|
||||
|
||||
You must add `dependency-installer` to the admin notice class as well as `data-dismissible='dependency-installer-<plugin basename>-<timeout>'`
|
||||
to the admin notice div class. <timeout> values are from one day '1' to 'forever'. Default timeout is 14 days.
|
||||
You must add `data-dismissible='<admin notice identifier>-<timeout>'` to the admin notice div class. `<timeout>` values are from one day '1' to 'forever'. Default timeout is 14 days. The `<admin notice identifier>` should be some unique value based upon the admin notice that you wish to dismiss.
|
||||
|
||||
Example using WooCommerce with a 14 day dismissible notice.
|
||||
Example using a 14 day dismissible notice.
|
||||
|
||||
```html
|
||||
<div class="notice-warning notice is-dismissible dependency-installer" data-dismissible="dependency-installer-woocommerce-14">...</div>
|
||||
<div class="notice-warning notice is-dismissible" data-dismissible="my_admin_notice_<hash>-14">...</div>
|
||||
```
|
||||
|
||||
Example filter to adjust timeout.
|
||||
Use this filter to adjust the timeout for the dismissal. Default is 14 days.
|
||||
This example filter can be used to modify the default timeout.
|
||||
The example filter will change the default timout for all plugin dependencies.
|
||||
You can specify the exact plugin timeout by modifying the following line in the filter.
|
||||
Use the filter `dismiss_notice_vendor_dir` if you have set the composer `vendor-dir` to a non-standard location.
|
||||
|
||||
```php
|
||||
$timeout = 'woocommerce' !== $source ? $timeout : 30;
|
||||
```
|
||||
|
||||
```php
|
||||
add_filter(
|
||||
'wp_plugin_dependency_timeout',
|
||||
function( $timeout, $source ) {
|
||||
$timeout = basename( __DIR__ ) !== $source ? $timeout : 30;
|
||||
return $timeout;
|
||||
},
|
||||
10,
|
||||
2
|
||||
);
|
||||
```
|
||||
|
||||
Example of creating admin notice from afragen/wp-dependency-installer
|
||||
|
||||
```php
|
||||
/**
|
||||
* Display admin notices / action links.
|
||||
* Filter composer.json vendor directory.
|
||||
* Some people don't use the standard vendor directory.
|
||||
*
|
||||
* @return bool/string false or Admin notice.
|
||||
* @param string Composer vendor directory.
|
||||
*/
|
||||
public function admin_notices() {
|
||||
if ( ! current_user_can( 'update_plugins' ) ) {
|
||||
return false;
|
||||
}
|
||||
foreach ( $this->notices as $notice ) {
|
||||
$status = isset( $notice['status'] ) ? $notice['status'] : 'notice-info';
|
||||
$class = esc_attr( $status ) . ' notice is-dismissible dependency-installer';
|
||||
$source = isset( $notice['source'] ) ? $notice['source'] : __( 'Dependency' );
|
||||
$label = esc_html( $this->get_dismiss_label( $source ) );
|
||||
$message = '';
|
||||
$action = '';
|
||||
$dismissible = '';
|
||||
|
||||
if ( isset( $notice['message'] ) ) {
|
||||
$message = esc_html( $notice['message'] );
|
||||
}
|
||||
|
||||
if ( isset( $notice['action'] ) ) {
|
||||
$action = sprintf(
|
||||
' <a href="javascript:;" class="wpdi-button" data-action="%1$s" data-slug="%2$s">%3$s Now »</a> ',
|
||||
esc_attr( $notice['action'] ),
|
||||
esc_attr( $notice['slug'] ),
|
||||
esc_html( ucfirst( $notice['action'] ) )
|
||||
);
|
||||
}
|
||||
if ( isset( $notice['slug'] ) ) {
|
||||
/**
|
||||
* Filters the dismissal timeout.
|
||||
*
|
||||
* @since 1.4.1
|
||||
*
|
||||
* @param string|int '14' Default dismissal in days.
|
||||
* @param string $notice['source'] Plugin slug of calling plugin.
|
||||
* @return string|int Dismissal timeout in days.
|
||||
*/
|
||||
$timeout = apply_filters( 'wp_plugin_dependency_timeout', '14', $source );
|
||||
$dependency = dirname( $notice['slug'] );
|
||||
$dismissible = empty( $timeout ) ? '' : sprintf( 'dependency-installer-%1$s-%2$s', esc_attr( $dependency ), esc_attr( $timeout ) );
|
||||
}
|
||||
if ( WP_Dismiss_Notice::is_admin_notice_active( $dismissible ) ) {
|
||||
printf(
|
||||
'<div class="%1$s" data-dismissible="%2$s"><p><strong>[%3$s]</strong> %4$s%5$s</p></div>',
|
||||
esc_attr( $class ),
|
||||
esc_attr( $dismissible ),
|
||||
esc_html( $label ),
|
||||
esc_html( $message ),
|
||||
$action // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
$vendor_dir = apply_filters( 'dismiss_notice_vendor_dir', '/vendor' );
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "afragen/wp-dismiss-notice",
|
||||
"description": "Library for time dismissible WordPress admin notices.",
|
||||
"version": "0.2.0",
|
||||
"version": "0.3.7",
|
||||
"type": "library",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@
|
|||
option_name = attr_value.join('-');
|
||||
|
||||
data = {
|
||||
'action': 'dismiss_admin_notice',
|
||||
'action': 'wp_dismiss_notice',
|
||||
'option_name': option_name,
|
||||
'dismissible_length': dismissible_length,
|
||||
'nonce': window.wp_dismiss_notice.nonce
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ class WP_Dismiss_Notice {
|
|||
*/
|
||||
public static function init() {
|
||||
add_action( 'admin_enqueue_scripts', [ __CLASS__, 'load_script' ] );
|
||||
add_action( 'wp_ajax_dismiss_admin_notice', [ __CLASS__, 'dismiss_admin_notice' ] );
|
||||
add_action( 'wp_ajax_wp_dismiss_notice', [ __CLASS__, 'dismiss_admin_notice' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -28,11 +28,34 @@ class WP_Dismiss_Notice {
|
|||
return;
|
||||
}
|
||||
|
||||
$js_url = plugins_url( 'js/dismiss-notice.js', __FILE__, 'wp-dismiss-notice' );
|
||||
$version = json_decode( file_get_contents( __DIR__ . '/composer.json' ) )->version;
|
||||
|
||||
/**
|
||||
* Filter composer.json vendor directory.
|
||||
* Some people don't use the standard vendor directory.
|
||||
*
|
||||
* @param string Composer vendor directory.
|
||||
*/
|
||||
$vendor_dir = apply_filters( 'dismiss_notice_vendor_dir', '/vendor' );
|
||||
$composer_js_path = untrailingslashit( $vendor_dir ) . '/afragen/wp-dismiss-notice/js/dismiss-notice.js';
|
||||
|
||||
$theme_js_url = get_theme_file_uri( $composer_js_path );
|
||||
$theme_js_file = parse_url( $theme_js_url, PHP_URL_PATH );
|
||||
|
||||
if ( file_exists( ABSPATH . $theme_js_file ) ) {
|
||||
$js_url = $theme_js_url;
|
||||
}
|
||||
|
||||
if ( '/vendor' !== $vendor_dir ) {
|
||||
$js_url = home_url( $composer_js_path );
|
||||
}
|
||||
|
||||
wp_enqueue_script(
|
||||
'dismissible-notices',
|
||||
plugins_url( 'js/dismiss-notice.js', __FILE__ ),
|
||||
$js_url,
|
||||
[ 'jquery', 'common' ],
|
||||
false,
|
||||
$version,
|
||||
true
|
||||
);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue