From dc13bbe170d6a7957c8dfa49d7a8d8a2acd10ce5 Mon Sep 17 00:00:00 2001 From: MlKilderkin Date: Mon, 7 Aug 2023 15:17:41 +0300 Subject: [PATCH 1/5] Show update notice only if version equal or higher --- src/Uplink/Resources/Plugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Uplink/Resources/Plugin.php b/src/Uplink/Resources/Plugin.php index d463cc32..eb9088c3 100644 --- a/src/Uplink/Resources/Plugin.php +++ b/src/Uplink/Resources/Plugin.php @@ -69,7 +69,7 @@ public function check_for_updates( $transient, $force_fetch = false ) { } // In order to show relevant issues on plugins page parse response data and add it to transient - if ( in_array( $results->get_result(), [ 'expired', 'invalid' ] ) ) { + if ( version_compare( $this->get_version_from_response( $results ), $this->get_installed_version(), '>=' ) && in_array( $results->get_result(), [ 'expired', 'invalid' ] ) ) { /** @var \stdClass $transient */ if ( ! isset( $transient->response ) ) { $transient->response = []; From d8abbb70356948a7a4e695f139999d9b33695a12 Mon Sep 17 00:00:00 2001 From: MlKilderkin Date: Mon, 7 Aug 2023 19:44:14 +0300 Subject: [PATCH 2/5] Add prefexing --- src/Uplink/Admin/Plugins_Page.php | 7 ++++--- src/assets/js/notices.js | 33 ++++++++++++++++++------------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/Uplink/Admin/Plugins_Page.php b/src/Uplink/Admin/Plugins_Page.php index 0540887d..3a2b27c6 100644 --- a/src/Uplink/Admin/Plugins_Page.php +++ b/src/Uplink/Admin/Plugins_Page.php @@ -139,11 +139,12 @@ public function store_admin_notices( string $page ) { $notices = apply_filters( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/plugin_notices', [] ); $path = preg_replace( '/.*\/vendor/', plugin_dir_url( $this->get_plugin()->get_path() ) . 'vendor', dirname( __DIR__, 2 ) ); $js_src = apply_filters( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/admin_js_source', $path . '/assets/js/notices.js' ); - $handle = 'stellarwp_uplink-notices'; + $handle = sprintf( 'stellarwp_uplink-%s', Config::get_hook_prefix() ); + + $action_postfix = str_replace( '-', '_', sanitize_title( Config::get_hook_prefix() ) ); wp_register_script( $handle, $js_src, [ 'jquery' ], '1.0.0', true ); - wp_localize_script( $handle, 'stellarwp_uplink_plugin_notices', $notices ); - wp_enqueue_script( $handle ); + wp_localize_script( $handle, sprintf( 'stellarwp_uplink_plugin_notices_%s', $action_postfix ), $notices ); } /** diff --git a/src/assets/js/notices.js b/src/assets/js/notices.js index 15fe9b16..931fd4b9 100644 --- a/src/assets/js/notices.js +++ b/src/assets/js/notices.js @@ -1,5 +1,3 @@ -var stellarwp_uplink_plugin_notices = stellarwp_uplink_plugin_notices || {}; - /** * Appends license key notifications inline within the plugin table. * @@ -10,22 +8,29 @@ var stellarwp_uplink_plugin_notices = stellarwp_uplink_plugin_notices || {}; 'use strict'; my.init = function() { - for ( var plugin_file in stellarwp_uplink_plugin_notices ) { - if ( ! stellarwp_uplink_plugin_notices.hasOwnProperty( plugin_file ) ) { // eslint-disable-line no-prototype-builtins,max-len - continue; + $( 'tr.active' ).each( function() { + var $el = $( this ); + var slug = $el.data( 'slug' ).replace( '-', '_' ); + var row = window[`stellarwp_uplink_plugin_notices_${slug}`]; + + if (!row) { + return; } - var $row = $( stellarwp_uplink_plugin_notices[ plugin_file ].message_row_html ); - var $active_plugin_row = $( 'tr[data-plugin="' + plugin_file + '"].active' ); + for ( var plugin_file in stellarwp_uplink_plugin_notices ) { + if ( ! stellarwp_uplink_plugin_notices.hasOwnProperty( plugin_file ) ) { // eslint-disable-line no-prototype-builtins,max-len + continue; + } - // Add the .update class to the plugin row and append our new row with the update message - $active_plugin_row.addClass( 'update' ).after( $row ); - } + var $row = $( stellarwp_uplink_plugin_notices[ plugin_file ].message_row_html ); + + // Add the .update class to the plugin row and append our new row with the update message + $el.addClass( 'update' ).after( $row ); + } + } ); }; $( function() { - if ( 'object' === typeof stellarwp_uplink_plugin_notices ) { - my.init(); - } + my.init(); }); -})( jQuery, stellarwp_uplink_plugin_notices ); +})( jQuery, {} ); From 67e6e370f12c7a8e1459d136e09fe20ee1885fb1 Mon Sep 17 00:00:00 2001 From: James Kemp Date: Tue, 8 Aug 2023 14:05:00 +0100 Subject: [PATCH 3/5] Move notices script inline --- .gitignore | 1 + src/Uplink/Admin/Plugins_Page.php | 77 +++++++++++++++++++++++-------- src/assets/js/notices.js | 36 --------------- 3 files changed, 58 insertions(+), 56 deletions(-) delete mode 100644 src/assets/js/notices.js diff --git a/.gitignore b/.gitignore index 17a68b2d..48e74aad 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ files/ repo/ vendor/ +.idea/ diff --git a/src/Uplink/Admin/Plugins_Page.php b/src/Uplink/Admin/Plugins_Page.php index 3a2b27c6..ec83926a 100644 --- a/src/Uplink/Admin/Plugins_Page.php +++ b/src/Uplink/Admin/Plugins_Page.php @@ -103,26 +103,20 @@ public function display_plugin_messages( string $page ) { ); $this->plugin_notice = [ - 'slug' => $this->get_plugin()->get_path(), + 'slug' => $this->get_plugin()->get_slug(), 'message_row_html' => $message_row_html, ]; - - add_filter( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/plugin_notices', [ $this, 'add_notice_to_plugin_notices' ] ); } /** - * @param array $notices + * Get plugin notice. + * + * @since 1.0.0 * - * @return array + * @return array */ - public function add_notice_to_plugin_notices( array $notices ) : array { - if ( ! $this->plugin_notice || $this->get_plugin()->is_network_licensed() ) { - return $notices; - } - - $notices[ $this->plugin_notice['slug'] ] = $this->plugin_notice; - - return $notices; + public function get_plugin_notice() { + return apply_filters( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/plugin_notices', $this->plugin_notice ); } /** @@ -136,15 +130,58 @@ public function store_admin_notices( string $page ) { if ( 'plugins.php' !== $page ) { return; } - $notices = apply_filters( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/plugin_notices', [] ); - $path = preg_replace( '/.*\/vendor/', plugin_dir_url( $this->get_plugin()->get_path() ) . 'vendor', dirname( __DIR__, 2 ) ); - $js_src = apply_filters( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/admin_js_source', $path . '/assets/js/notices.js' ); - $handle = sprintf( 'stellarwp_uplink-%s', Config::get_hook_prefix() ); - $action_postfix = str_replace( '-', '_', sanitize_title( Config::get_hook_prefix() ) ); + add_action( 'admin_footer', [ $this, 'output_notices_script' ] ); + } - wp_register_script( $handle, $js_src, [ 'jquery' ], '1.0.0', true ); - wp_localize_script( $handle, sprintf( 'stellarwp_uplink_plugin_notices_%s', $action_postfix ), $notices ); + /** + * Output the plugin-specific notices script. + * + * @since 1.0.0 + * + * @return string + */ + public function output_notices_script() { + $slug = $this->get_plugin()->get_slug(); + $notice = $this->get_plugin_notice(); + + if ( empty( $notice ) ) { + return; + } + ?> + + Date: Tue, 8 Aug 2023 14:17:37 +0100 Subject: [PATCH 4/5] Removed unused tests --- tests/wpunit/Admin/Plugins_PageTest.php | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/tests/wpunit/Admin/Plugins_PageTest.php b/tests/wpunit/Admin/Plugins_PageTest.php index fa0a69d1..af92d1d9 100644 --- a/tests/wpunit/Admin/Plugins_PageTest.php +++ b/tests/wpunit/Admin/Plugins_PageTest.php @@ -31,17 +31,4 @@ public function test_it_should_bail_if_there_is_no_plugin() { $handler = new Plugins_Page(); $this->assertNull( $handler->display_plugin_messages( 'plugins.php' ) ); } - - public function test_add_notice_to_plugin_notices_should_return_same() { - $handler = new Plugins_Page(); - $this->assertSame( [], $handler->add_notice_to_plugin_notices( [] ) ); - } - - public function test_add_notice_to_plugin_notices_should_return_updated() { - $handler = new Plugins_Page(); - $handler->plugin_notice = [ 'slug' => 'sample', 'message_row_html' => '
' ]; - - $this->assertSame( [ 'sample' => $handler->plugin_notice ], $handler->add_notice_to_plugin_notices( [] ) ); - } - } From 41f362be73b824e62790837dccd301ff4615f8cc Mon Sep 17 00:00:00 2001 From: James Kemp Date: Tue, 8 Aug 2023 14:19:41 +0100 Subject: [PATCH 5/5] Fix return comment --- src/Uplink/Admin/Plugins_Page.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Uplink/Admin/Plugins_Page.php b/src/Uplink/Admin/Plugins_Page.php index ec83926a..ea69c496 100644 --- a/src/Uplink/Admin/Plugins_Page.php +++ b/src/Uplink/Admin/Plugins_Page.php @@ -139,7 +139,7 @@ public function store_admin_notices( string $page ) { * * @since 1.0.0 * - * @return string + * @return void */ public function output_notices_script() { $slug = $this->get_plugin()->get_slug();