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. * * @param mixed $input * @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($input = null) { if ( $this->wasCheckTriggered ) { return $input; } $this->wasCheckTriggered = true; $this->scheduler->maybeCheckForUpdates(); return $input; } }