diff --git a/README.md b/README.md index bc96416..bd4f9a1 100644 --- a/README.md +++ b/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--'` - to the admin notice div class. values are from one day '1' to 'forever'. Default timeout is 14 days. +You must add `data-dismissible='-'` to the admin notice div class. `` values are from one day '1' to 'forever'. Default timeout is 14 days. The `` 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 -
...
+
...
``` -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( - ' %3$s Now » ', - 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( - '

[%3$s] %4$s%5$s

', - 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' ); diff --git a/composer.json b/composer.json index d0ec202..99c07dc 100644 --- a/composer.json +++ b/composer.json @@ -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": [ diff --git a/js/dismiss-notice.js b/js/dismiss-notice.js index 3ab403e..0d615ff 100644 --- a/js/dismiss-notice.js +++ b/js/dismiss-notice.js @@ -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 diff --git a/wp-dismiss-notice.php b/wp-dismiss-notice.php index 660deec..ad669ef 100644 --- a/wp-dismiss-notice.php +++ b/wp-dismiss-notice.php @@ -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 );