PluginInformation.php
1.72 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace AC;
use AC\Plugin\Version;
class PluginInformation {
/**
* @var string
*/
private $basename;
public function __construct( $basename ) {
$this->basename = (string) $basename;
}
public static function create_by_file( $file ) {
return new self( plugin_basename( $file ) );
}
/**
* @return string
*/
public function get_basename() {
return $this->basename;
}
/**
* @return string
*/
public function get_dirname() {
return dirname( $this->basename );
}
/**
* @return bool
*/
public function is_installed() {
return null !== $this->get_header_data();
}
/**
* @return bool
*/
public function is_active() {
return is_plugin_active( $this->basename );
}
/**
* @return bool
*/
public function is_network_active() {
return is_plugin_active_for_network( $this->basename );
}
/**
* @return Version
*/
public function get_version() {
return new Version( (string) $this->get_header( 'Version' ) );
}
/**
* @return string
*/
public function get_name() {
return $this->get_header( 'Name' );
}
/**
* @return array
*/
private function get_plugins() {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
// use `get_plugins` (cached) over `get_plugin_data` (non cached)
return (array) get_plugins();
}
/**
* @return array|null
*/
private function get_header_data() {
$plugins = $this->get_plugins();
if ( ! array_key_exists( $this->basename, $plugins ) ) {
return null;
}
return $plugins[ $this->basename ];
}
/**
* @param string $var
*
* @return string|null
*/
public function get_header( $var ) {
$info = $this->get_header_data();
return $info && isset( $info[ $var ] )
? (string) $info[ $var ]
: null;
}
}