Compare commits

..

No commits in common. "main" and "0.1.1" have entirely different histories.
main ... 0.1.1

4 changed files with 93 additions and 42 deletions

View file

@ -9,22 +9,96 @@ Initialize the class.

`new \WP_Dismiss_Notice();` in your project.

### Admin notice format.
Admin notice format.

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.
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.

Example using a 14 day dismissible notice.
Example using WooCommerce with a 14 day dismissible notice.

```html
<div class="notice-warning notice is-dismissible" data-dismissible="my_admin_notice_<hash>-14">...</div>
<div class="notice-warning notice is-dismissible dependency-installer" data-dismissible="dependency-installer-woocommerce-14">...</div>
```

Use the filter `dismiss_notice_vendor_dir` if you have set the composer `vendor-dir` to a non-standard location.
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.

```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
/**
* Filter composer.json vendor directory.
* Some people don't use the standard vendor directory.
* Display admin notices / action links.
*
* @param string Composer vendor directory.
* @return bool/string false or Admin notice.
*/
$vendor_dir = apply_filters( 'dismiss_notice_vendor_dir', '/vendor' );
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 &raquo;</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
);
}
}
}
```

View file

@ -1,7 +1,7 @@
{
"name": "afragen/wp-dismiss-notice",
"description": "Library for time dismissible WordPress admin notices.",
"version": "0.3.7",
"version": "0.1.1",
"type": "library",
"license": "MIT",
"authors": [

View file

@ -23,14 +23,14 @@
option_name = attr_value.join('-');

data = {
'action': 'wp_dismiss_notice',
'action': 'dismiss_admin_notice',
'option_name': option_name,
'dismissible_length': dismissible_length,
'nonce': window.wp_dismiss_notice.nonce
'nonce': window.dismissible_notice.nonce
};

// Run Ajax request.
$.post(window.wp_dismiss_notice.ajaxurl, data);
$.post(window.dismissible_notice.ajaxurl, data);
$this.closest('div[data-dismissible]').hide('slow');
}
);

View file

@ -16,7 +16,7 @@ class WP_Dismiss_Notice {
*/
public static function init() {
add_action( 'admin_enqueue_scripts', [ __CLASS__, 'load_script' ] );
add_action( 'wp_ajax_wp_dismiss_notice', [ __CLASS__, 'dismiss_admin_notice' ] );
add_action( 'wp_ajax_dismiss_admin_notice', [ __CLASS__, 'dismiss_admin_notice' ] );
}

/**
@ -28,42 +28,19 @@ 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',
$js_url,
plugins_url( 'js/dismiss-notice.js', __FILE__ ),
[ 'jquery', 'common' ],
$version,
false,
true
);

wp_localize_script(
'dismissible-notices',
'wp_dismiss_notice',
'dismissible_notice',
[
'nonce' => wp_create_nonce( 'wp-dismiss-notice' ),
'nonce' => wp_create_nonce( 'dismissible-notice' ),
'ajaxurl' => admin_url( 'admin-ajax.php' ),
]
);
@ -83,7 +60,7 @@ class WP_Dismiss_Notice {
$dismissible_length = strtotime( absint( $dismissible_length ) . ' days' );
}

check_ajax_referer( 'wp-dismiss-notice', 'nonce' );
check_ajax_referer( 'dismissible-notice', 'nonce' );
self::set_admin_notice_cache( $option_name, $dismissible_length );
wp_die();
}