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

[Feature] Check for updates if WP-CLI checks for updates #558

Closed
hirasso opened this issue Jan 22, 2024 · 6 comments
Closed

[Feature] Check for updates if WP-CLI checks for updates #558

hirasso opened this issue Jan 22, 2024 · 6 comments

Comments

@hirasso
Copy link

hirasso commented Jan 22, 2024

Hi there!

The Problem

The WP-CLI command wp plugin ... has a few commands where they automatically check for plugin updates, for example:

  • wp plugin status
  • wp plugin list

I just noticed that Plugin Update Checker doesn't check for updates if these commands are being executed.

Proposed Solution

Both these commands call wp_update_plugins directly if being executed.

Would it be possible to automatically check for updates in Plugin Update Checker as well, if one of those commands is being executed? I just checked and it seems like in wp_update_plugins there is no action being triggered. So it might be hard to achieve updates based on only that function.

Alternatives considered

Implement a custom wp cli command in userland, something like this:

$bb_checker = PucFactory::buildUpdateChecker(
  "https://bitbucket.org/user/$plugin_slug", // Metadata URL.
  $file_path, // Full path to the main plugin file.
  $plugin_slug, // Plugin slug. Usually it's the same as the name of the directory.
  24*7 // <-- update interval in hours
);

if (defined('WP_CLI') && WP_CLI) {
  \WP_CLI::add_command('bb-updater check', function() use ($bb_updater) {
    $bb_updater->checkForUpdates();
  });
}

That works, but it would be amazing to get this out of the box.

@YahnisElsts
Copy link
Owner

wp_update_plugins modifies the update_plugins transient, which triggers filters that could be used to hook into the function (e.g. pre_set_site_transient_{$transient}. However, that transient is also used in other places (in Cron, and potentially on every admin page load), so, for better performance, I would want a way to check if the filter is being called inside WP-CLI, preferably even a way to identify the current WP-CLI command.

I believe the first part can be covered by checking if the WP_CLI constant is defined, but I'm not sure about identifying the running command. Do you happen to know how to do that?

@hirasso
Copy link
Author

hirasso commented Jan 31, 2024

@YahnisElsts thanks for getting back to me! I asked in the cli slack channel and got the following answer for how to get information about the current WP-CLI command:

For the current command, try:

$args = WP_CLI::get_runner()->arguments;

print_r( $args );

For wp cron event run wp_version_check, the output should be:

Array
(
   [0] => cron
   [1] => event
   [2] => run
   [3] => wp_version_check
)

Based on that, I played around a bit:

/**
 * Check for WP-CLI commands running wp_update_plugins
 */
function isWpCliUpdateCheck(): bool
{
  if (!defined('WP_CLI') || !WP_CLI) return false;

  // get the args of the current command
  $args = \WP_CLI::get_runner()->arguments;

  // convert the args array back to the command string
  $command = implode(' ', $args);

  // check for wp-cli commands that are known to trigger a check for plugin updates
  return match ($command) {
    'plugin list' => true,
    'plugin status' => true,
    'cron event run wp_version_check' => true,
    // 'foo bar' => true, ???
    default => false
  };
}

if (isWpCliUpdateCheck()) {
  // ... run the update checker
}

While this technically works, it will be a pain to maintain. These commands could change at any time, not even mentioning theme updates...

So I think we should continue to look for a reliably way to detect wp_update_plugins.

@hirasso
Copy link
Author

hirasso commented Jan 31, 2024

Actually, re-reading your comment:

wp_update_plugins modifies the update_plugins transient, which triggers filters that could be used to hook into the function (e.g. pre_set_site_transient_{$transient})

Maybe we could get away with combining the two?

  1. hook into the appropriate filter
  2. check if currently running a WP_CLI command starting with "plugin"
  3. run the update checker

YahnisElsts added a commit that referenced this issue Feb 2, 2024
@YahnisElsts
Copy link
Owner

It looks like WP-CLI provides a before_invoke:<command> hook that can be used to detect when certain (sub-)commands run more directly. I've written a basic implementation that hooks into the relevant plugin and theme commands. For consistency with wp_update_plugins(), this implementation doesn't check for updates every time but just triggers the normal scheduled update check logic.

This could use more testing - give it a try.

@hirasso
Copy link
Author

hirasso commented Feb 17, 2024

Works like a charm! I tested it with two plugins – one managed by WordPress (relevanssi), one managed by plugin update checker (rh-admin-utils). Here is the relevant command output: Before updating puc, rh-admin-utils didn't detect that there was a new version available, after updating puc it detected the new version:

Before

❯ wp plugin list
+---------------------------------------+--------+-----------+------------+
| name                                  | status | update    | version    |
+---------------------------------------+--------+-----------+------------+
| relevanssi                            | active | available | 4.22.0     |
| rh-admin-utils                        | active | none      | 1.9.4      |
+---------------------------------------+--------+-----------+------------+

After

❯ wp plugin list
+---------------------------------------+--------+-----------+------------+
| name                                  | status | update    | version    |
+---------------------------------------+--------+-----------+------------+
| relevanssi                            | active | available | 4.22.0     |
| rh-admin-utils                        | active | available | 1.9.4      |
+---------------------------------------+--------+-----------+------------+

Also, sorry for keeping you waiting for so long... your help means a lot!

@YahnisElsts
Copy link
Owner

Sounds good. I've made a new release (5.4) that includes this feature, so I'll close the issue now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants