UpdatePlugin.php
1.37 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
<?php
namespace ACP\Updates;
use AC\Entity\Plugin;
use AC\Plugin\Version;
use AC\Registerable;
use ACP\Storage\PluginsDataFactory;
use stdClass;
/**
* Hooks into the WordPress update process for plugins
*/
class UpdatePlugin implements Registerable
{
private $plugin;
private $storage_factory;
public function __construct(Plugin $plugin, PluginsDataFactory $storage_factory)
{
$this->plugin = $plugin;
$this->storage_factory = $storage_factory;
}
public function register(): void
{
/**
* For testing purpose use `wp_clean_update_cache()`
*/
add_filter('pre_set_site_transient_update_plugins', [$this, 'check_update']);
}
public function check_update($transient)
{
$data = $this->storage_factory->create()->get();
if (empty($data) || ! is_array($data)) {
return $transient;
}
$dir_name = $this->plugin->get_dirname();
if ( ! isset($data[$dir_name])) {
return $transient;
}
$plugin_data = (object)$data[$dir_name];
if (null === $transient) {
$transient = new stdClass();
}
if ($this->plugin->get_version()->is_lt(new Version($plugin_data->new_version))) {
$transient->response[$this->plugin->get_basename()] = $plugin_data;
}
return $transient;
}
}