diff --git a/README.md b/README.md index 7045743..bd4f9a1 100644 --- a/README.md +++ b/README.md @@ -18,3 +18,13 @@ Example using a 14 day dismissible notice. ```html
...
``` + +Use the filter `dismiss_notice_vendor_dir` if you have set the composer `vendor-dir` to a non-standard location. + + /** + * 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' ); diff --git a/composer.json b/composer.json index a7f7d00..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.4", + "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 ab1cf06..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,24 +28,34 @@ class WP_Dismiss_Notice { return; } - $composer_js_path = '/vendor/afragen/wp-dismiss-notice/js/dismiss-notice.js'; - $plugin_js_url = plugins_url( 'js/dismiss-notice.js', __FILE__, 'wp-dismiss-notice' ); + $js_url = plugins_url( 'js/dismiss-notice.js', __FILE__, 'wp-dismiss-notice' ); + $version = json_decode( file_get_contents( __DIR__ . '/composer.json' ) )->version; - // Test to get correct URL for JS. - $response = get_transient( 'wp-dismiss-notice' ); - if ( ! $response ) { - $response = wp_remote_head( $plugin_js_url ); - set_transient( 'wp-dismiss-notice', $response, WEEK_IN_SECONDS ); + /** + * 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 ); } - $js_url = ( 200 === wp_remote_retrieve_response_code( $response ) ) || is_wp_error( $response ) - ? $plugin_js_url - : get_stylesheet_directory_uri() . $composer_js_path; wp_enqueue_script( 'dismissible-notices', $js_url, [ 'jquery', 'common' ], - false, + $version, true );