ForcePluginUpdate.php
1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
namespace ACP\Service;
use AC\Capabilities;
use AC\Registerable;
use ACP\ActivationTokenFactory;
use ACP\Transient\UpdateCheckTransientHourly;
use ACP\Updates\PluginDataUpdater;
class ForcePluginUpdate implements Registerable
{
private $activation_token_factory;
private $updater;
public function __construct(ActivationTokenFactory $activation_token_factory, PluginDataUpdater $updater)
{
$this->activation_token_factory = $activation_token_factory;
$this->updater = $updater;
}
public function register(): void
{
add_action('admin_init', [$this, 'force_plugin_updates']);
add_action('load-plugins.php', [$this, 'force_plugin_updates_cached'], 9);
add_action('load-update-core.php', [$this, 'force_plugin_updates_cached'], 9);
add_action('load-update.php', [$this, 'force_plugin_updates_cached'], 9);
}
private function is_force_check_request(): bool
{
global $pagenow;
return '1' === filter_input(INPUT_GET, 'force-check') && $pagenow === 'update-core.php' && current_user_can(
Capabilities::MANAGE
);
}
/**
* Forces to check for updates on a manual request
*/
public function force_plugin_updates(): void
{
if ( ! $this->is_force_check_request()) {
return;
}
$this->updater->update($this->activation_token_factory->create());
}
/**
* Forces to check for updates on plugins page
*/
public function force_plugin_updates_cached(): void
{
$transient = new UpdateCheckTransientHourly();
if ($transient->is_expired()) {
$this->updater->update($this->activation_token_factory->create());
$transient->save();
}
}
}