WP-CLI: Maybe check for updates when certain WP-CLI commands run

See #558. Needs testing.
This commit is contained in:
Yahnis Elsts 2024-02-02 15:58:59 +02:00
parent c1bf33e770
commit a211943884
4 changed files with 112 additions and 3 deletions

View File

@ -17,7 +17,7 @@ if ( !class_exists(UpdateChecker::class, false) ):
*/
class UpdateChecker extends BaseUpdateChecker {
protected $updateTransient = 'update_plugins';
protected $translationType = 'plugin';
protected $componentType = 'plugin';
public $pluginAbsolutePath = ''; //Full path of the main plugin file.
public $pluginFile = ''; //Plugin filename relative to the plugins directory. Many WP APIs use this to identify plugins.

View File

@ -12,7 +12,7 @@ if ( !class_exists(UpdateChecker::class, false) ):
class UpdateChecker extends BaseUpdateChecker {
protected $filterSuffix = 'theme';
protected $updateTransient = 'update_themes';
protected $translationType = 'theme';
protected $componentType = 'theme';
/**
* @var string Theme directory name.

View File

@ -9,7 +9,16 @@ if ( !class_exists(UpdateChecker::class, false) ):
abstract class UpdateChecker {
protected $filterSuffix = '';
protected $updateTransient = '';
protected $translationType = ''; //This can be "plugin" or "theme".
/**
* @var string This can be "plugin" or "theme".
*/
protected $componentType = '';
/**
* @var string Currently the same as $componentType, but this is an implementation detail that
* depends on how WP works internally, and could therefore change.
*/
protected $translationType = '';
/**
* Set to TRUE to enable error reporting. Errors are raised using trigger_error()
@ -74,6 +83,11 @@ if ( !class_exists(UpdateChecker::class, false) ):
*/
protected $debugBarExtension = null;
/**
* @var WpCliCheckTrigger|null
*/
protected $wpCliCheckTrigger = null;
public function __construct($metadataUrl, $directoryName, $slug = null, $checkPeriod = 12, $optionName = '') {
$this->debugMode = (bool)(constant('WP_DEBUG'));
$this->metadataUrl = $metadataUrl;
@ -91,6 +105,10 @@ if ( !class_exists(UpdateChecker::class, false) ):
}
}
if ( empty($this->translationType) ) {
$this->translationType = $this->componentType;
}
$this->package = $this->createInstalledPackage();
$this->scheduler = $this->createScheduler($checkPeriod);
$this->upgraderStatus = new UpgraderStatus();
@ -103,6 +121,10 @@ if ( !class_exists(UpdateChecker::class, false) ):
}
$this->installHooks();
if ( ($this->wpCliCheckTrigger === null) && defined('WP_CLI') ) {
$this->wpCliCheckTrigger = new WpCliCheckTrigger($this->componentType, $this->scheduler);
}
}
/**

View File

@ -0,0 +1,87 @@
<?php
namespace YahnisElsts\PluginUpdateChecker\v5p3;
use WP_CLI;
/**
* Triggers an update check when relevant WP-CLI commands are executed.
*
* When WP-CLI runs certain commands like "wp plugin status" or "wp theme list", it calls
* wp_update_plugins() and wp_update_themes() to refresh update information. This class hooks into
* the same commands and triggers an update check when they are executed.
*
* Note that wp_update_plugins() and wp_update_themes() do not perform an update check *every* time
* they are called. They use a context-dependent delay between update checks. Similarly, this class
* calls Scheduler::maybeCheckForUpdates(), which also dynamically decides whether to actually
* run a check. If you want to force an update check, call UpdateChecker::checkForUpdates() instead.
*/
class WpCliCheckTrigger {
/**
* @var Scheduler
*/
private $scheduler;
/**
* @var string 'plugin' or 'theme'
*/
private $componentType;
/**
* @var bool Whether an update check was already triggered during the current request
* or script execution.
*/
private $wasCheckTriggered = false;
public function __construct($componentType, Scheduler $scheduler) {
if ( !in_array($componentType, ['plugin', 'theme']) ) {
throw new \InvalidArgumentException('Invalid component type. Must be "plugin" or "theme".');
}
$this->componentType = $componentType;
$this->scheduler = $scheduler;
if ( !defined('WP_CLI') || !class_exists(WP_CLI::class, false) ) {
return; //Nothing to do if WP-CLI is not available.
}
/*
* We can't hook directly into wp_update_plugins(), but we can hook into the WP-CLI
* commands that call it. We'll use the "before_invoke:xyz" hook to trigger update checks.
*/
foreach ($this->getRelevantCommands() as $command) {
WP_CLI::add_hook('before_invoke:' . $command, [$this, 'triggerUpdateCheckOnce']);
}
}
private function getRelevantCommands() {
$result = [];
foreach (['status', 'list', 'update'] as $subcommand) {
$result[] = $this->componentType . ' ' . $subcommand;
}
return $result;
}
/**
* Trigger a potential update check once.
*
* The update transient can be read multiple times during a single WP-CLI command execution.
* For performance, we only want to trigger an update check once.
*
* @param mixed $transient
* @return mixed The input value, unchanged.
* @internal This method is public so that it can be used as a WP-CLI hook callback.
* It should not be called directly.
*
*/
public function triggerUpdateCheckOnce($transient) {
if ( $this->wasCheckTriggered ) {
return $transient;
}
$this->wasCheckTriggered = true;
$this->scheduler->maybeCheckForUpdates();
return $transient;
}
}