Plugin.php
151 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Entity; |
| 6 | |
| 7 | use AC\Plugin\Version; |
| 8 | use AC\PluginUpdate; |
| 9 | |
| 10 | class Plugin |
| 11 | { |
| 12 | |
| 13 | private string $basename; |
| 14 | |
| 15 | private string $dir; |
| 16 | |
| 17 | private string $url; |
| 18 | |
| 19 | private Version $version; |
| 20 | |
| 21 | public function __construct(string $basename, string $dir, string $url, Version $version) |
| 22 | { |
| 23 | $this->basename = $basename; |
| 24 | $this->dir = $dir; |
| 25 | $this->url = $url; |
| 26 | $this->version = $version; |
| 27 | } |
| 28 | |
| 29 | public static function create(string $file, Version $version): self |
| 30 | { |
| 31 | return new self( |
| 32 | plugin_basename($file), |
| 33 | plugin_dir_path($file), |
| 34 | plugin_dir_url($file), |
| 35 | $version |
| 36 | ); |
| 37 | } |
| 38 | |
| 39 | public function get_version(): Version |
| 40 | { |
| 41 | return $this->version; |
| 42 | } |
| 43 | |
| 44 | public function get_basename(): string |
| 45 | { |
| 46 | return $this->basename; |
| 47 | } |
| 48 | |
| 49 | public function get_dir(): string |
| 50 | { |
| 51 | return $this->dir; |
| 52 | } |
| 53 | |
| 54 | public function get_url(): string |
| 55 | { |
| 56 | return $this->url; |
| 57 | } |
| 58 | |
| 59 | public function get_dirname(): string |
| 60 | { |
| 61 | return dirname($this->basename); |
| 62 | } |
| 63 | |
| 64 | public function is_installed(): bool |
| 65 | { |
| 66 | return null !== $this->get_header_data(); |
| 67 | } |
| 68 | |
| 69 | public function is_active(): bool |
| 70 | { |
| 71 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 72 | |
| 73 | return is_plugin_active($this->basename); |
| 74 | } |
| 75 | |
| 76 | public function is_network_active(): bool |
| 77 | { |
| 78 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 79 | |
| 80 | return is_plugin_active_for_network($this->basename); |
| 81 | } |
| 82 | |
| 83 | public function get_name(): ?string |
| 84 | { |
| 85 | return $this->get_header_var('Name'); |
| 86 | } |
| 87 | |
| 88 | private function get_plugins(): array |
| 89 | { |
| 90 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 91 | |
| 92 | // use `get_plugins` (cached) over `get_plugin_data` (non cached) |
| 93 | return get_plugins(); |
| 94 | } |
| 95 | |
| 96 | private function get_plugin_updates(): array |
| 97 | { |
| 98 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 99 | |
| 100 | return get_plugin_updates(); |
| 101 | } |
| 102 | |
| 103 | public function has_update(): bool |
| 104 | { |
| 105 | return null !== $this->get_update(); |
| 106 | } |
| 107 | |
| 108 | public function get_update(): ?PluginUpdate |
| 109 | { |
| 110 | $updates = $this->get_plugin_updates(); |
| 111 | |
| 112 | if ( ! array_key_exists($this->basename, $updates)) { |
| 113 | return null; |
| 114 | } |
| 115 | |
| 116 | $data = $updates[$this->basename]; |
| 117 | |
| 118 | if ( ! property_exists($data, 'update')) { |
| 119 | return null; |
| 120 | } |
| 121 | |
| 122 | if ( ! property_exists($data->update, 'new_version')) { |
| 123 | return null; |
| 124 | } |
| 125 | |
| 126 | $version = new Version($data->update->new_version); |
| 127 | |
| 128 | if ( ! $version->is_valid() || $version->is_lte($this->version)) { |
| 129 | return null; |
| 130 | } |
| 131 | |
| 132 | $package = property_exists($data->update, 'package') && $data->update->package |
| 133 | ? $data->update->package |
| 134 | : null; |
| 135 | |
| 136 | return new PluginUpdate(new Version($data->update->new_version), $package); |
| 137 | } |
| 138 | |
| 139 | private function get_header_data(): ?array |
| 140 | { |
| 141 | $plugins = $this->get_plugins(); |
| 142 | |
| 143 | return $plugins[$this->basename] ?? null; |
| 144 | } |
| 145 | |
| 146 | private function get_header_var(string $var): ?string |
| 147 | { |
| 148 | return $this->get_header_data()[$var] ?? null; |
| 149 | } |
| 150 | |
| 151 | } |