Compare commits

...

20 commits
0.2.0 ... main

Author SHA1 Message Date
Andy Fragen
3e2c694ca8 add test for install to theme 2023-05-30 14:45:15 -07:00
Andy Fragen
63fbe9fd01 oops, remove test code 2023-05-30 13:35:45 -07:00
Andy Fragen
2e47f5fec3 load JS where it exists
stop messing with tests for URL and transients
2023-05-30 13:29:06 -07:00
Andy Fragen
a195d3badb save int to transient, check for object
object is stale cache
2023-04-05 13:00:28 -07:00
Andy Fragen
606c0d5222 cast to int 2023-04-04 18:03:20 -07:00
Andy Fragen
5a3dd3198c parse response and save 2023-03-30 14:37:09 -07:00
Andy Fragen
2c421b09e8 use JSON as safe data storage
Bypass unserialize from get_transient wanting to load class of stored object
2023-03-30 13:06:00 -07:00
Andy Fragen
8b690bc045 update to not conflict with users of PaND 2023-01-02 11:27:07 -08:00
Andy Fragen
d6511c4eea add filter for non-standard composer vendor-dir 2022-07-25 08:44:19 -07:00
Andy Fragen
71029356cc more specific transient name 2022-05-16 09:32:39 -07:00
Andy Fragen
a5c05306a4 save wp_remote_get check for a week in transient 2022-05-15 17:35:10 -07:00
Andy Fragen
21949732f1 bump 2021-12-19 13:53:57 -08:00
Andy Fragen
3b9d3b775f slightly better test for JS, allow for WP_Error
WP_Error assumes JS in plugin's vendor folder.
2021-12-19 13:50:51 -08:00
Andy Fragen
c2e737fe14 bump 2021-12-15 16:19:50 -08:00
Andy Fragen
7864d89816 Merge branch 'universal' into main 2021-12-15 16:18:24 -08:00
Andy Fragen
cfb4384f6f actual fix Thanks for testing @jivedig 2021-12-15 16:18:13 -08:00
Andy Fragen
1534d3dd13 check for loadable JS file and use that URL
Hopefully makes this code work when called from either plugin or theme
2021-12-15 14:29:53 -08:00
Andy Fragen
a0ced98366 bump 2021-09-26 15:09:34 -07:00
Andy Fragen
1ce23d619d update readme 2021-09-26 14:58:57 -07:00
Andy Fragen
9ab86e3825 remove comment 2021-09-23 21:57:30 -07:00
4 changed files with 37 additions and 88 deletions

View file

@ -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 &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
);
}
}
}
```
$vendor_dir = apply_filters( 'dismiss_notice_vendor_dir', '/vendor' );

View file

@ -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": [

View file

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

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_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
);