Plugin.php
78 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Entity; |
| 6 | |
| 7 | use AC\Asset\Location; |
| 8 | use AC\Plugin\Version; |
| 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( |
| 22 | string $basename, |
| 23 | string $dir, |
| 24 | string $url, |
| 25 | Version $version |
| 26 | ) { |
| 27 | $this->basename = $basename; |
| 28 | $this->dir = $dir; |
| 29 | $this->url = $url; |
| 30 | $this->version = $version; |
| 31 | } |
| 32 | |
| 33 | public static function from_plugin_file(string $file, Version $version): self |
| 34 | { |
| 35 | return new self( |
| 36 | plugin_basename($file), |
| 37 | plugin_dir_path($file), |
| 38 | plugin_dir_url($file), |
| 39 | $version |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | public function get_basename(): string |
| 44 | { |
| 45 | return $this->basename; |
| 46 | } |
| 47 | |
| 48 | public function get_dir(): string |
| 49 | { |
| 50 | return $this->dir; |
| 51 | } |
| 52 | |
| 53 | public function get_url(): string |
| 54 | { |
| 55 | return $this->url; |
| 56 | } |
| 57 | |
| 58 | public function get_location(): Location |
| 59 | { |
| 60 | return new Location\Absolute($this->url, $this->dir); |
| 61 | } |
| 62 | |
| 63 | public function get_dirname(): string |
| 64 | { |
| 65 | return dirname($this->basename); |
| 66 | } |
| 67 | |
| 68 | public function get_version(): Version |
| 69 | { |
| 70 | return $this->version; |
| 71 | } |
| 72 | |
| 73 | public function is_network_active(): bool |
| 74 | { |
| 75 | return is_plugin_active_for_network($this->basename); |
| 76 | } |
| 77 | |
| 78 | } |