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

Fix latest plugin version not being downloaded consistently #1693

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from

Conversation

ShyamGadde
Copy link
Contributor

Summary

Fixes #1690

This is done so that it automatically downloads the latest version
@ShyamGadde ShyamGadde marked this pull request as ready for review November 22, 2024 14:07
Copy link

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: ShyamGadde <[email protected]>
Co-authored-by: westonruter <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@@ -372,7 +372,9 @@ function perflab_install_and_activate_plugin( string $plugin_slug, array &$proce
// Replace new Plugin_Installer_Skin with new Quiet_Upgrader_Skin when output needs to be suppressed.
$skin = new WP_Ajax_Upgrader_Skin( array( 'api' => $plugin_data ) );
$upgrader = new Plugin_Upgrader( $skin );
$result = $upgrader->install( $plugin_data['download_link'] );
// Remove the version number from the link to download the latest plugin version.
$download_link = (string) preg_replace( '#(\/plugin\/[^\/]+)\.\d+\.\d+\.\d+\.zip#', '$1.zip', $plugin_data['download_link'] );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be safer to do:

Suggested change
$download_link = (string) preg_replace( '#(\/plugin\/[^\/]+)\.\d+\.\d+\.\d+\.zip#', '$1.zip', $plugin_data['download_link'] );
$download_link = (string) preg_replace( '#(\/plugin\/[^\/]+)\.\d.*.zip#', '$1.zip', $plugin_data['download_link'] );

The reason being that it's possible for plugin versions to have -rc or -beta tagged on.

For example:
https://wordpress.org/plugins/rest-api/
=>
https://downloads.wordpress.org/plugin/rest-api.2.0-beta15.zip

Granted, it's highly unlikely that we'll ever release one of our plugins with such a Stable Tag, and it's not super common across the repo: https://wpdirectory.net/search/01JDAFQMKVDE6ME17SH47V33D9

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I don't feel strongly about this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worst case in such a scenario is that the original URL ends up getting used without the version removed, which will likely still work (even if it has the even slighter possibility of downloading a stale version).

Copy link
Contributor Author

@ShyamGadde ShyamGadde Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing this out! I hadn’t considered the possibility of version tags like -rc or -beta. That’s an oversight on my part.

However, I believe the regex '#(\/plugin\/[^\/]+)\.\d.*.zip#' might not work as intended because it could capture the plugin name greedily. For instance, in the example provided, the download link might end up being https://downloads.wordpress.org/plugin/rest-api.2.zip instead of https://downloads.wordpress.org/plugin/rest-api.zip.

To address this, one solution could be to simply modify the regex to capture the plugin name non-greedily:

'#(\/plugin\/[^\/]+?)\.\d.*.zip#'

Alternatively, another approach would be to use the following regex:

'#(\/plugin\/[^\/]+)\.\d+\.\d+\.\d+(?:-\w+)?\.zip#'

This pattern accounts for optional tags like -rc or -beta and aligns with the regex used in the versions command, so it seems like a good choice to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note in the example https://downloads.wordpress.org/plugin/rest-api.2.0-beta15.zip there is no patch version either, just a major and minor. So that would need to be optional as well.

This should address the greediness issue, by using \.\d.*?.zip instead of \.\d.*.zip:

$download_link = (string) preg_replace( '#(\/plugin\/[^\/]+)\.\d.*?.zip#', '$1.zip', $plugin_data['download_link'] );

@westonruter westonruter added this to the performance-lab n.e.x.t milestone Nov 22, 2024
@westonruter westonruter added [Type] Bug An existing feature is broken [Plugin] Performance Lab Issue relates to work in the Performance Lab Plugin only labels Nov 22, 2024
@@ -372,7 +372,9 @@ function perflab_install_and_activate_plugin( string $plugin_slug, array &$proce
// Replace new Plugin_Installer_Skin with new Quiet_Upgrader_Skin when output needs to be suppressed.
$skin = new WP_Ajax_Upgrader_Skin( array( 'api' => $plugin_data ) );
$upgrader = new Plugin_Upgrader( $skin );
$result = $upgrader->install( $plugin_data['download_link'] );
// Remove the version number from the link to download the latest plugin version.
$download_link = (string) preg_replace( '#(\/plugin\/[^\/]+)\.\d+\.\d+\.\d+(?:-\w+)?\.zip#', '$1.zip', $plugin_data['download_link'] );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$download_link = (string) preg_replace( '#(\/plugin\/[^\/]+)\.\d+\.\d+\.\d+(?:-\w+)?\.zip#', '$1.zip', $plugin_data['download_link'] );
$download_link = (string) preg_replace( '#(\/plugin\/[^\/]+)\.\d.*?.zip#', '$1.zip', $plugin_data['download_link'] );

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or rather, per #1693 (comment)

Suggested change
$download_link = (string) preg_replace( '#(\/plugin\/[^\/]+)\.\d+\.\d+\.\d+(?:-\w+)?\.zip#', '$1.zip', $plugin_data['download_link'] );
$download_link = (string) preg_replace( '#(\/plugin\/[^\/.]+)\..+?\.zip#', '$1.zip', $plugin_data['download_link'] );

@westonruter
Copy link
Member

Lastly, note that many plugins also use Stable tag: trunk: https://wpdirectory.net/search/01JDB0D3QBVXR8KWR1X9BRYYPB

I haven't seen trunk show up in the download URL, but in theory it might?

Here's another search for plugins using non-standard versions: https://wpdirectory.net/search/01JDB0R6MBNNEMH0A1X8AHKQYC

The WP Umbrella plugin version doesn't begin with a digit:

https://downloads.wordpress.org/plugin/wp-health.v2.16.4.zip

That being said, perhaps we should just strip off everything up to the first . since I don't believe any plugin is allowed to have that as a character in the slug.

@westonruter
Copy link
Member

Again, this is all likely splitting hairs since none of the plugins we include here should ever use a non-standard version!

@@ -372,7 +372,9 @@ function perflab_install_and_activate_plugin( string $plugin_slug, array &$proce
// Replace new Plugin_Installer_Skin with new Quiet_Upgrader_Skin when output needs to be suppressed.
$skin = new WP_Ajax_Upgrader_Skin( array( 'api' => $plugin_data ) );
$upgrader = new Plugin_Upgrader( $skin );
$result = $upgrader->install( $plugin_data['download_link'] );
// Remove the version number from the link to download the latest plugin version.
$download_link = (string) preg_replace( '#(\/plugin\/[^\/]+)\.\d+\.\d+\.\d+(?:-\w+)?\.zip#', '$1.zip', $plugin_data['download_link'] );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of regexing this from the download_link. Is there no externally controlled place (API responses or elsewhere) that exposes the format we want here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Plugin] Performance Lab Issue relates to work in the Performance Lab Plugin only [Type] Bug An existing feature is broken
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Old Plugin Version Downloaded Due to Cached API Response in Transient
3 participants