class-otgs-installer-plugin-finder.php
4.18 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
class OTGS_Installer_Plugin_Finder {
/**
* @var array
*/
private $plugins = array();
private $plugin_factory;
private $repositories;
/**
* @var array
*/
private $installed_plugins;
public function __construct( OTGS_Installer_Plugin_Factory $plugin_factory, array $repositories ) {
$this->plugin_factory = $plugin_factory;
$this->repositories = $repositories;
$this->load();
}
private function load() {
if ( ! $this->plugins ) {
foreach ( $this->repositories as $repo_key => $repository ) {
foreach ( $repository['data']['downloads']['plugins'] as $slug => $plugin ) {
$plugin_id = $this->get_installed_plugin_id_by_slug( $plugin['slug'] );
if ( ! $plugin_id ) {
$plugin_id = $this->get_installed_plugin_id_by_name( $plugin['name'] );
}
$this->plugins[] = $this->plugin_factory->create( array(
'name' => $plugin['name'],
'slug' => $plugin['slug'],
'description' => $plugin['description'],
'changelog' => $plugin['changelog'],
'version' => $plugin['version'],
'installed_version' => isset( $this->installed_plugins[ $plugin_id ]['Version'] ) ? $this->installed_plugins[ $plugin_id ]['Version'] : null ,
'date' => $plugin['date'],
'url' => $plugin['url'],
'free_on_wporg' => isset( $plugin['free-on-wporg'] ) ? $plugin['free-on-wporg'] : '',
'fallback_on_wporg' => isset( $plugin['fallback-free-on-wporg'] ) ? $plugin['fallback-free-on-wporg'] : '',
'basename' => $plugin['basename'],
'external_repo' => isset( $plugin['external-repo'] ) ? $plugin['external-repo'] : '',
'is_lite' => isset( $plugin['is-lite'] ) ? $plugin['is-lite'] : '',
'repo' => $repo_key,
'id' => $plugin_id,
'channel' => $plugin['channel'],
'tested' => isset( $plugin['tested'] ) ? $plugin['tested'] : null,
) );
}
}
}
}
/**
* @return OTGS_Installer_Plugin[]
*/
public function get_all() {
return $this->plugins;
}
/**
* @return array<string, string>
*/
public function getLocalPluginVersions() {
$versions = [];
foreach ( $this->plugins as $plugin ) {
$installed_version = $plugin->get_installed_version();
if ( $installed_version ) {
$versions[ $plugin->get_slug() ] = $installed_version;
}
}
return $versions;
}
public function get_otgs_installed_plugins_by_repository() {
$installed_plugins = [];
/** @var OTGS_Installer_Plugin $plugin */
foreach ( $this->plugins as $plugin ) {
if ( $plugin->get_installed_version() ) {
$installed_plugins[ $plugin->get_repo() ][] = [
'id' => $plugin->get_id(),
'slug' => $plugin->get_slug(),
'name' => $plugin->get_name(),
];
}
}
return $installed_plugins;
}
/**
* @param string|int $slug
* @param string $repo
*
* @return null|OTGS_Installer_Plugin
*/
public function get_plugin( $slug, $repo = '' ) {
foreach ( $this->plugins as $plugin ) {
if ( $slug === $plugin->get_slug() ) {
if ( ! $repo || $plugin->get_repo() === $repo ) {
return $plugin;
}
}
}
return null;
}
/**
* @param string $name
*
* @return null|OTGS_Installer_Plugin
*/
public function get_plugin_by_name( $name ) {
foreach ( $this->plugins as $plugin ) {
if ( $name === strip_tags( $plugin->get_name() ) ) {
return $plugin;
}
}
return null;
}
/**
* @param $slug
*
* @return null|string
*/
private function get_installed_plugin_id_by_slug( $slug ) {
foreach ( $this->get_installed_plugins() as $plugin_id => $plugin ) {
$plugin_slug = explode( '/', $plugin_id );
if ( $slug === $plugin_slug[0] ) {
return $plugin_id;
}
}
return null;
}
private function get_installed_plugins() {
if ( ! $this->installed_plugins ) {
$this->installed_plugins = get_plugins();
}
return $this->installed_plugins;
}
/**
* @param string $name
*
* @return string|null
*/
private function get_installed_plugin_id_by_name( $name ) {
$plugin_id = array_keys( wp_list_filter( $this->get_installed_plugins(), array( 'Name' => $name ) ) );
return current( $plugin_id );
}
}