From 8b690bc045620811d3c0724c2a2746cf1baab809 Mon Sep 17 00:00:00 2001 From: Andy Fragen Date: Mon, 2 Jan 2023 11:27:07 -0800 Subject: [PATCH 1/8] 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 2/8] 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 3/8] 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 4/8] 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 5/8] 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 6/8] 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 7/8] 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 8/8] 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 ); }