From 94946fcb6d46dd0387f411ed5b4b7b66b8421fef Mon Sep 17 00:00:00 2001 From: Andy Fragen Date: Thu, 23 Sep 2021 14:04:35 -0700 Subject: [PATCH 01/20] avoid conflict with PaND --- composer.json | 2 +- js/dismiss-notice.js | 4 ++-- wp-dismiss-notice.php | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 3865ed8..d0ec202 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.1.1", + "version": "0.2.0", "type": "library", "license": "MIT", "authors": [ diff --git a/js/dismiss-notice.js b/js/dismiss-notice.js index d40101d..3ab403e 100644 --- a/js/dismiss-notice.js +++ b/js/dismiss-notice.js @@ -26,11 +26,11 @@ 'action': 'dismiss_admin_notice', 'option_name': option_name, 'dismissible_length': dismissible_length, - 'nonce': window.dismissible_notice.nonce + 'nonce': window.wp_dismiss_notice.nonce }; // Run Ajax request. - $.post(window.dismissible_notice.ajaxurl, data); + $.post(window.wp_dismiss_notice.ajaxurl, data); $this.closest('div[data-dismissible]').hide('slow'); } ); diff --git a/wp-dismiss-notice.php b/wp-dismiss-notice.php index 0acb57c..660deec 100644 --- a/wp-dismiss-notice.php +++ b/wp-dismiss-notice.php @@ -38,9 +38,9 @@ class WP_Dismiss_Notice { wp_localize_script( 'dismissible-notices', - 'dismissible_notice', + 'wp_dismiss_notice', [ - 'nonce' => wp_create_nonce( 'dismissible-notice' ), + 'nonce' => wp_create_nonce( 'wp-dismiss-notice' ), 'ajaxurl' => admin_url( 'admin-ajax.php' ), ] ); @@ -60,7 +60,7 @@ class WP_Dismiss_Notice { $dismissible_length = strtotime( absint( $dismissible_length ) . ' days' ); } - check_ajax_referer( 'dismissible-notice', 'nonce' ); + check_ajax_referer( 'wp-dismiss-notice', 'nonce' ); self::set_admin_notice_cache( $option_name, $dismissible_length ); wp_die(); } From 9ab86e3825af6d862fe9e56cfcb91dd8f8990317 Mon Sep 17 00:00:00 2001 From: Andy Fragen Date: Thu, 23 Sep 2021 21:57:30 -0700 Subject: [PATCH 02/20] remove comment --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bc96416..cbb65a5 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ Example of creating admin notice from afragen/wp-dependency-installer esc_attr( $dismissible ), esc_html( $label ), esc_html( $message ), - $action // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + $action ); } } From 1ce23d619d5f060cad9fa8f0aa605312a8a8e9c0 Mon Sep 17 00:00:00 2001 From: Andy Fragen Date: Sun, 26 Sep 2021 14:58:57 -0700 Subject: [PATCH 03/20] update readme --- README.md | 92 +++---------------------------------------------------- 1 file changed, 4 insertions(+), 88 deletions(-) diff --git a/README.md b/README.md index cbb65a5..7045743 100644 --- a/README.md +++ b/README.md @@ -9,96 +9,12 @@ 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. - -```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. - * - * @return bool/string false or Admin notice. - */ - 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 - ); - } - } - } +
...
``` From a0ced9836671d1374ed5d7f47e63ca313f5f54b9 Mon Sep 17 00:00:00 2001 From: Andy Fragen Date: Sun, 26 Sep 2021 15:09:34 -0700 Subject: [PATCH 04/20] bump --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d0ec202..2599e5c 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.2.1", "type": "library", "license": "MIT", "authors": [ From 1534d3dd13dfaa3c200b2cadd6e8e4de4a291b76 Mon Sep 17 00:00:00 2001 From: Andy Fragen Date: Wed, 15 Dec 2021 14:29:53 -0800 Subject: [PATCH 05/20] check for loadable JS file and use that URL Hopefully makes this code work when called from either plugin or theme --- wp-dismiss-notice.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/wp-dismiss-notice.php b/wp-dismiss-notice.php index 660deec..7b567a5 100644 --- a/wp-dismiss-notice.php +++ b/wp-dismiss-notice.php @@ -28,9 +28,16 @@ class WP_Dismiss_Notice { return; } + $theme_dir_uri = dirname( get_template_directory_uri() ); + $plugin_js_url = plugins_url( 'js/dismiss-notice.js', __FILE__, 'wp-dismiss-notice' ); + + $js_url = 200 === wp_remote_retrieve_response_code( wp_remote_head( $plugin_js_url ) ) + ? $plugin_js_url + : str_replace( plugins_url(), $theme_dir_uri, $plugin_js_url ); + wp_enqueue_script( 'dismissible-notices', - plugins_url( 'js/dismiss-notice.js', __FILE__ ), + $js_url, [ 'jquery', 'common' ], false, true From cfb4384f6f00caa0e505eeb039aebc7541154996 Mon Sep 17 00:00:00 2001 From: Andy Fragen Date: Wed, 15 Dec 2021 16:18:13 -0800 Subject: [PATCH 06/20] actual fix Thanks for testing @jivedig --- wp-dismiss-notice.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/wp-dismiss-notice.php b/wp-dismiss-notice.php index 7b567a5..1fe45d2 100644 --- a/wp-dismiss-notice.php +++ b/wp-dismiss-notice.php @@ -28,12 +28,12 @@ class WP_Dismiss_Notice { return; } - $theme_dir_uri = dirname( get_template_directory_uri() ); - $plugin_js_url = plugins_url( 'js/dismiss-notice.js', __FILE__, 'wp-dismiss-notice' ); + $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 = 200 === wp_remote_retrieve_response_code( wp_remote_head( $plugin_js_url ) ) ? $plugin_js_url - : str_replace( plugins_url(), $theme_dir_uri, $plugin_js_url ); + : get_stylesheet_directory_uri() . $composer_js_path; wp_enqueue_script( 'dismissible-notices', From c2e737fe141a2f831da662373f7967b8b0faf35b Mon Sep 17 00:00:00 2001 From: Andy Fragen Date: Wed, 15 Dec 2021 16:19:50 -0800 Subject: [PATCH 07/20] bump --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 2599e5c..95266a8 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.1", + "version": "0.2.2", "type": "library", "license": "MIT", "authors": [ From 3b9d3b775fcffdeabab1fcba49514b8992bdded8 Mon Sep 17 00:00:00 2001 From: Andy Fragen Date: Sun, 19 Dec 2021 13:50:51 -0800 Subject: [PATCH 08/20] slightly better test for JS, allow for WP_Error WP_Error assumes JS in plugin's vendor folder. --- wp-dismiss-notice.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/wp-dismiss-notice.php b/wp-dismiss-notice.php index 1fe45d2..7aa0da2 100644 --- a/wp-dismiss-notice.php +++ b/wp-dismiss-notice.php @@ -31,7 +31,9 @@ class WP_Dismiss_Notice { $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 = 200 === wp_remote_retrieve_response_code( wp_remote_head( $plugin_js_url ) ) + // Test to get correct URL for JS. + $response = wp_remote_head( $plugin_js_url ); + $js_url = ( 200 === wp_remote_retrieve_response_code( $response ) ) || is_wp_error( $response ) ? $plugin_js_url : get_stylesheet_directory_uri() . $composer_js_path; From 21949732f1ea21d81500bdda8f882ac7eb0f98af Mon Sep 17 00:00:00 2001 From: Andy Fragen Date: Sun, 19 Dec 2021 13:53:57 -0800 Subject: [PATCH 09/20] bump --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 95266a8..3662ecf 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.2", + "version": "0.2.3", "type": "library", "license": "MIT", "authors": [ From a5c05306a4c2f0b18aed7d9b27e97b247dc7773a Mon Sep 17 00:00:00 2001 From: Andy Fragen Date: Sun, 15 May 2022 17:35:10 -0700 Subject: [PATCH 10/20] save wp_remote_get check for a week in transient --- composer.json | 2 +- wp-dismiss-notice.php | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 3662ecf..a7f7d00 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.3", + "version": "0.2.4", "type": "library", "license": "MIT", "authors": [ diff --git a/wp-dismiss-notice.php b/wp-dismiss-notice.php index 7aa0da2..ab1cf06 100644 --- a/wp-dismiss-notice.php +++ b/wp-dismiss-notice.php @@ -32,8 +32,12 @@ class WP_Dismiss_Notice { $plugin_js_url = plugins_url( 'js/dismiss-notice.js', __FILE__, 'wp-dismiss-notice' ); // Test to get correct URL for JS. - $response = wp_remote_head( $plugin_js_url ); - $js_url = ( 200 === wp_remote_retrieve_response_code( $response ) ) || is_wp_error( $response ) + $response = get_transient( 'wp-dismiss-notice' ); + if ( ! $response ) { + $response = wp_remote_head( $plugin_js_url ); + set_transient( 'wp-dismiss-notice', $response, WEEK_IN_SECONDS ); + } + $js_url = ( 200 === wp_remote_retrieve_response_code( $response ) ) || is_wp_error( $response ) ? $plugin_js_url : get_stylesheet_directory_uri() . $composer_js_path; From 71029356cc2a8dfa13ee835c08d8a1b484c0e673 Mon Sep 17 00:00:00 2001 From: Andy Fragen Date: Mon, 16 May 2022 09:32:39 -0700 Subject: [PATCH 11/20] more specific transient name --- composer.json | 2 +- wp-dismiss-notice.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index a7f7d00..1a6cc90 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.2.5", "type": "library", "license": "MIT", "authors": [ diff --git a/wp-dismiss-notice.php b/wp-dismiss-notice.php index ab1cf06..e7b4e4c 100644 --- a/wp-dismiss-notice.php +++ b/wp-dismiss-notice.php @@ -32,10 +32,10 @@ class WP_Dismiss_Notice { $plugin_js_url = plugins_url( 'js/dismiss-notice.js', __FILE__, 'wp-dismiss-notice' ); // Test to get correct URL for JS. - $response = get_transient( 'wp-dismiss-notice' ); + $response = get_transient( 'wp-dismiss-notice_jsurl' ); if ( ! $response ) { $response = wp_remote_head( $plugin_js_url ); - set_transient( 'wp-dismiss-notice', $response, WEEK_IN_SECONDS ); + set_transient( 'wp-dismiss-notice_jsurl', $response, WEEK_IN_SECONDS ); } $js_url = ( 200 === wp_remote_retrieve_response_code( $response ) ) || is_wp_error( $response ) ? $plugin_js_url From d6511c4eea243449dc6d15efcc807aa8ab0d3a91 Mon Sep 17 00:00:00 2001 From: Andy Fragen Date: Mon, 25 Jul 2022 08:44:19 -0700 Subject: [PATCH 12/20] add filter for non-standard composer vendor-dir --- README.md | 10 ++++++++++ composer.json | 2 +- wp-dismiss-notice.php | 9 ++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) 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 1a6cc90..b2da7e6 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.5", + "version": "0.2.6", "type": "library", "license": "MIT", "authors": [ diff --git a/wp-dismiss-notice.php b/wp-dismiss-notice.php index e7b4e4c..5585094 100644 --- a/wp-dismiss-notice.php +++ b/wp-dismiss-notice.php @@ -28,7 +28,14 @@ class WP_Dismiss_Notice { return; } - $composer_js_path = '/vendor/afragen/wp-dismiss-notice/js/dismiss-notice.js'; + /** + * 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'; $plugin_js_url = plugins_url( 'js/dismiss-notice.js', __FILE__, 'wp-dismiss-notice' ); // Test to get correct URL for JS. From 8b690bc045620811d3c0724c2a2746cf1baab809 Mon Sep 17 00:00:00 2001 From: Andy Fragen Date: Mon, 2 Jan 2023 11:27:07 -0800 Subject: [PATCH 13/20] update to not conflict with users of PaND --- composer.json | 2 +- js/dismiss-notice.js | 2 +- wp-dismiss-notice.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index b2da7e6..9bfa926 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.6", + "version": "0.3.0", "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 5585094..7c47b78 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' ] ); } /** From 2c421b09e838daa98ea645613584ac9d7d2b1a86 Mon Sep 17 00:00:00 2001 From: Andy Fragen Date: Thu, 30 Mar 2023 13:06:00 -0700 Subject: [PATCH 14/20] use JSON as safe data storage Bypass unserialize from get_transient wanting to load class of stored object --- composer.json | 2 +- wp-dismiss-notice.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 9bfa926..60ddf16 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.3.0", + "version": "0.3.1", "type": "library", "license": "MIT", "authors": [ diff --git a/wp-dismiss-notice.php b/wp-dismiss-notice.php index 7c47b78..47a5d03 100644 --- a/wp-dismiss-notice.php +++ b/wp-dismiss-notice.php @@ -39,10 +39,10 @@ class WP_Dismiss_Notice { $plugin_js_url = plugins_url( 'js/dismiss-notice.js', __FILE__, 'wp-dismiss-notice' ); // Test to get correct URL for JS. - $response = get_transient( 'wp-dismiss-notice_jsurl' ); + $response = json_decode( get_transient( 'wp-dismiss-notice_jsurl' ) ); if ( ! $response ) { $response = wp_remote_head( $plugin_js_url ); - set_transient( 'wp-dismiss-notice_jsurl', $response, WEEK_IN_SECONDS ); + set_transient( 'wp-dismiss-notice_jsurl', json_encode( $response ), WEEK_IN_SECONDS ); } $js_url = ( 200 === wp_remote_retrieve_response_code( $response ) ) || is_wp_error( $response ) ? $plugin_js_url From 5a3dd3198c819a7e3df3062399f7c41548b5a8ab Mon Sep 17 00:00:00 2001 From: Andy Fragen Date: Thu, 30 Mar 2023 14:37:09 -0700 Subject: [PATCH 15/20] parse response and save --- composer.json | 2 +- wp-dismiss-notice.php | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 60ddf16..d138380 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.3.1", + "version": "0.3.2", "type": "library", "license": "MIT", "authors": [ diff --git a/wp-dismiss-notice.php b/wp-dismiss-notice.php index 47a5d03..1427301 100644 --- a/wp-dismiss-notice.php +++ b/wp-dismiss-notice.php @@ -39,12 +39,15 @@ class WP_Dismiss_Notice { $plugin_js_url = plugins_url( 'js/dismiss-notice.js', __FILE__, 'wp-dismiss-notice' ); // Test to get correct URL for JS. - $response = json_decode( get_transient( 'wp-dismiss-notice_jsurl' ) ); + $response = get_transient( 'wp-dismiss-notice_jsurl' ); if ( ! $response ) { $response = wp_remote_head( $plugin_js_url ); - set_transient( 'wp-dismiss-notice_jsurl', json_encode( $response ), WEEK_IN_SECONDS ); + $response = is_wp_error( $response ) ? $response->get_error_message() : wp_remote_retrieve_response_code( $response ); + if ( is_int( $response ) ) { + set_transient( 'wp-dismiss-notice_jsurl', $response, WEEK_IN_SECONDS ); + } } - $js_url = ( 200 === wp_remote_retrieve_response_code( $response ) ) || is_wp_error( $response ) + $js_url = ( 200 === $response ) ? $plugin_js_url : get_stylesheet_directory_uri() . $composer_js_path; From 606c0d5222ad223fb420bb33db947e592b213920 Mon Sep 17 00:00:00 2001 From: Andy Fragen Date: Tue, 4 Apr 2023 18:03:20 -0700 Subject: [PATCH 16/20] cast to int --- composer.json | 2 +- wp-dismiss-notice.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index d138380..ca838ad 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.3.2", + "version": "0.3.3", "type": "library", "license": "MIT", "authors": [ diff --git a/wp-dismiss-notice.php b/wp-dismiss-notice.php index 1427301..3036664 100644 --- a/wp-dismiss-notice.php +++ b/wp-dismiss-notice.php @@ -47,7 +47,7 @@ class WP_Dismiss_Notice { set_transient( 'wp-dismiss-notice_jsurl', $response, WEEK_IN_SECONDS ); } } - $js_url = ( 200 === $response ) + $js_url = ( 200 === (int) $response ) ? $plugin_js_url : get_stylesheet_directory_uri() . $composer_js_path; From a195d3badb28965f6a33e04e7b488b7d8d29df39 Mon Sep 17 00:00:00 2001 From: Andy Fragen Date: Wed, 5 Apr 2023 13:00:28 -0700 Subject: [PATCH 17/20] save int to transient, check for object object is stale cache --- composer.json | 2 +- wp-dismiss-notice.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index ca838ad..dd99975 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.3.3", + "version": "0.3.4", "type": "library", "license": "MIT", "authors": [ diff --git a/wp-dismiss-notice.php b/wp-dismiss-notice.php index 3036664..2e19292 100644 --- a/wp-dismiss-notice.php +++ b/wp-dismiss-notice.php @@ -40,9 +40,9 @@ class WP_Dismiss_Notice { // Test to get correct URL for JS. $response = get_transient( 'wp-dismiss-notice_jsurl' ); - if ( ! $response ) { + if ( ! $response || is_object( $response ) ) { $response = wp_remote_head( $plugin_js_url ); - $response = is_wp_error( $response ) ? $response->get_error_message() : wp_remote_retrieve_response_code( $response ); + $response = is_wp_error( $response ) ? 0 : wp_remote_retrieve_response_code( $response ); if ( is_int( $response ) ) { set_transient( 'wp-dismiss-notice_jsurl', $response, WEEK_IN_SECONDS ); } From 2e47f5fec355dc706ca5ec2c6082ebdc5c7d7088 Mon Sep 17 00:00:00 2001 From: Andy Fragen Date: Tue, 30 May 2023 13:29:06 -0700 Subject: [PATCH 18/20] load JS where it exists stop messing with tests for URL and transients --- composer.json | 2 +- wp-dismiss-notice.php | 21 +++++++-------------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index dd99975..042f5d5 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.3.4", + "version": "0.3.5", "type": "library", "license": "MIT", "authors": [ diff --git a/wp-dismiss-notice.php b/wp-dismiss-notice.php index 2e19292..fab0d42 100644 --- a/wp-dismiss-notice.php +++ b/wp-dismiss-notice.php @@ -15,6 +15,7 @@ class WP_Dismiss_Notice { * Init hooks. */ public static function init() { + $version = json_decode( file_get_contents( './composer.json' ) ); add_action( 'admin_enqueue_scripts', [ __CLASS__, 'load_script' ] ); add_action( 'wp_ajax_wp_dismiss_notice', [ __CLASS__, 'dismiss_admin_notice' ] ); } @@ -28,6 +29,9 @@ 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. @@ -36,26 +40,15 @@ class WP_Dismiss_Notice { */ $vendor_dir = apply_filters( 'dismiss_notice_vendor_dir', '/vendor' ); $composer_js_path = untrailingslashit( $vendor_dir ) . '/afragen/wp-dismiss-notice/js/dismiss-notice.js'; - $plugin_js_url = plugins_url( 'js/dismiss-notice.js', __FILE__, 'wp-dismiss-notice' ); - - // Test to get correct URL for JS. - $response = get_transient( 'wp-dismiss-notice_jsurl' ); - if ( ! $response || is_object( $response ) ) { - $response = wp_remote_head( $plugin_js_url ); - $response = is_wp_error( $response ) ? 0 : wp_remote_retrieve_response_code( $response ); - if ( is_int( $response ) ) { - set_transient( 'wp-dismiss-notice_jsurl', $response, WEEK_IN_SECONDS ); - } + if ( '/vendor' !== $vendor_dir ) { + $js_url = home_url( $composer_js_path ); } - $js_url = ( 200 === (int) $response ) - ? $plugin_js_url - : get_stylesheet_directory_uri() . $composer_js_path; wp_enqueue_script( 'dismissible-notices', $js_url, [ 'jquery', 'common' ], - false, + $version, true ); From 63fbe9fd01f240fff382aa761dd52c12a16bd715 Mon Sep 17 00:00:00 2001 From: Andy Fragen Date: Tue, 30 May 2023 13:35:45 -0700 Subject: [PATCH 19/20] oops, remove test code --- composer.json | 2 +- wp-dismiss-notice.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 042f5d5..d33f48a 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.3.5", + "version": "0.3.6", "type": "library", "license": "MIT", "authors": [ diff --git a/wp-dismiss-notice.php b/wp-dismiss-notice.php index fab0d42..c913101 100644 --- a/wp-dismiss-notice.php +++ b/wp-dismiss-notice.php @@ -15,7 +15,6 @@ class WP_Dismiss_Notice { * Init hooks. */ public static function init() { - $version = json_decode( file_get_contents( './composer.json' ) ); add_action( 'admin_enqueue_scripts', [ __CLASS__, 'load_script' ] ); add_action( 'wp_ajax_wp_dismiss_notice', [ __CLASS__, 'dismiss_admin_notice' ] ); } From 3e2c694ca891fe94771457f54a3a5457a70c0aec Mon Sep 17 00:00:00 2001 From: Andy Fragen Date: Tue, 30 May 2023 14:45:15 -0700 Subject: [PATCH 20/20] add test for install to theme --- composer.json | 2 +- wp-dismiss-notice.php | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d33f48a..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.3.6", + "version": "0.3.7", "type": "library", "license": "MIT", "authors": [ diff --git a/wp-dismiss-notice.php b/wp-dismiss-notice.php index c913101..ad669ef 100644 --- a/wp-dismiss-notice.php +++ b/wp-dismiss-notice.php @@ -39,6 +39,14 @@ class WP_Dismiss_Notice { */ $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 ); }