Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show update notice only if version equal or higher #24

Merged
merged 6 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
files/
repo/
vendor/
.idea/
80 changes: 59 additions & 21 deletions src/Uplink/Admin/Plugins_Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed> $notices
* Get plugin notice.
*
* @since 1.0.0
*
* @return array<mixed>
* @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 );
}

/**
Expand All @@ -136,14 +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 = 'stellarwp_uplink-notices';

wp_register_script( $handle, $js_src, [ 'jquery' ], '1.0.0', true );
wp_localize_script( $handle, 'stellarwp_uplink_plugin_notices', $notices );
wp_enqueue_script( $handle );

add_action( 'admin_footer', [ $this, 'output_notices_script' ] );
}

/**
* Output the plugin-specific notices script.
*
* @since 1.0.0
*
* @return void
*/
public function output_notices_script() {
$slug = $this->get_plugin()->get_slug();
$notice = $this->get_plugin_notice();

if ( empty( $notice ) ) {
return;
}
?>
<script>
/**
* Appends license key notifications inline within the plugin table.
*
* This is done via JS because the options for achieving the same things
* server-side are currently limited.
*/
(function( $, my ) {
'use strict';

my.init = function() {
var $active_plugin_row = $( 'tr.active[data-slug="<?php echo esc_attr( $slug ); ?>"]' );

if ( ! $active_plugin_row.length ) {
return;
}

var notice = <?php echo wp_json_encode( $notice ); ?>;

if ( ! notice.message_row_html ) {
return;
}

// Add the .update class to the plugin row and append our new row with the update message
$active_plugin_row.addClass( 'update' ).after( notice.message_row_html );
};

$( function() {
my.init();
} );
})( jQuery, {} );
</script>
<?php
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Uplink/Resources/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
31 changes: 0 additions & 31 deletions src/assets/js/notices.js

This file was deleted.

13 changes: 0 additions & 13 deletions tests/wpunit/Admin/Plugins_PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<div></div>' ];

$this->assertSame( [ 'sample' => $handler->plugin_notice ], $handler->add_notice_to_plugin_notices( [] ) );
}

}
Loading