-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinylibraries.php
341 lines (338 loc) · 9.16 KB
/
tinylibraries.php
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
/**
* TinyLibraries
*
* @package TinyLibraries
*/
/*
Plugin Name: TinyLibraries
Plugin URI: http://arunas.co
Description: Create library plugins
Version: 0.1.0
Author: Arūnas Liuiza
Author URI: http://arunas.co
Text Domain: tinylibraries
*/
/**
* Plugin class instantioation function
*/
function TinyLibraries() {
if ( false === TinyLibrariesClass::$instance ) {
TinyLibrariesClass::$instance = new TinyLibrariesClass();
}
return TinyLibrariesClass::$instance;
}
add_action( 'plugins_loaded', 'TinyLibraries' );
/**
* Main olugin class.
*/
class TinyLibrariesClass {
/**
* Stores plugin class instance.
*
* @var object
*/
public static $instance = false;
/**
* Stores plugin full path
*
* @var string
*/
public $plugin_path = '';
/**
* Stores plugin filter
*
* @var string
*/
public $plugin_file = '';
/**
* Class constuctor function
*/
public function __construct() {
$this->plugin_path = plugin_dir_path( __FILE__ );
$this->plugin_file = __FILE__;
add_filter( 'views_plugins', array( $this, 'views' ) );
add_filter( 'show_advanced_plugins', array( $this, 'plugins' ), 10, 2 );
add_filter( 'activate_plugin', array( $this, 'activation' ), 10, 2 );
add_filter( 'deactivate_plugin', array( $this, 'deactivation' ), 10, 2 );
add_filter( 'plugin_action_links', array( $this, 'actions' ) );
add_action( 'wp_version_check', array( $this, 'updates' ) );
}
/**
* Load the library if needed
*
* @param string $library Library slug.
* @return bool SUccess/fail
*/
public function require_library( $library ) {
$file = WP_CONTENT_DIR . "/libraries/{$library}/{$library}.php";
if ( ! file_exists( $file ) ) {
return false;
}
require_once( $file );
return true;
}
/**
* Display Libraries View in Plugins page.
*
* @param array $views List of Plugin Views.
* @return array List of Plugin Views.
*/
public function views( $views ) {
global $status, $totals;
$id = 'tinylibraries';
$count = $totals[ $id ];
if ( 0 === $count ) {
return $views;
}
$current = $id === $status ? ' class="current"' : '';
$views[ $id ] = '<a href="plugins.php?plugin_status=' . $id . '"' . $current . '>';
$views[ $id ] .= __( 'Libraries', 'tinylibraries' );
$views[ $id ] .= ' <span class="count">(' . $count . ')</span></a>' . PHP_EOL;
return $views;
}
/**
* Display a list of Libraries in Plugins page.
*
* @param bool $bool Should advanced plugins be displayed.
* @param string $type Type of advanced plugin.
* @return bool Should advanced plugins be displayed.
*/
public function plugins( $bool, $type ) {
global $plugins, $status, $totals;
if ( 'dropins' !== $type ) {
return $bool;
}
$data = $this->_get_libraries();
foreach ( $data as $library => $file ) {
$data[ $library ] = $this->_get_library_info( $file );
}
// @codingStandardsIgnoreStart - I know accesing WP Globals is bad, but I need this hack, because we do not have a proper filter.
$plugins['tinylibraries'] = $data;
$totals['tinylibraries'] = count( $data );
if ( isset( $_REQUEST['plugin_status'] ) && ( 'tinylibraries' === $_REQUEST['plugin_status'] ) ) {
$status = 'tinylibraries';
}
// @codingStandardsIgnoreEnd
return $bool;
}
/**
* Check if plugin requires Libraries before activation.
*
* @param string $plugin Plugin path.
* @return bool Success/fail.
*/
public function activation( $plugin ) {
$libraries = $this->_get_required_libraries( $plugin );
if ( ! is_array( $libraries ) ) {
return false;
}
foreach ( $libraries as $library ) {
$this->_install( $library );
}
return true;
}
/**
* Remove unused libraries on plugin deactivation
*
* @param string $this_plugin plugin that is being deactivated.
* @return void
*/
public function deactivation( $this_plugin ) {
$plugins = get_option( 'active_plugins' );
$counts = array();
foreach ( $plugins as $plugin ) {
$libraries = $this->_get_required_libraries( $plugin );
if ( ! $libraries ) {
continue;
}
foreach ( $libraries as $library ) {
if ( ! isset( $counts[ $library ] ) ) {
$counts[ $library ] = 0;
}
++$counts[ $library ];
}
}
$libraries = $this->_get_required_libraries( $this_plugin );
foreach ( $libraries as $library ) {
if ( 1 < $counts[ $library ] ) {
continue;
}
$this->_delete( $library );
}
}
/**
* Check for updates
*
* @return void
*/
public function updates() {
$libraries = $this->_get_libraries();
foreach ( $libraries as $library => $library_file ) {
$current_version = $this->_get_library_info( $library_file );
$current_version = $current_version['Version'];
$latest_version = $this->_get_library_data( $library );
$latest_version = $latest_version['version'];
if ( -1 === version_compare( $current_version, $latest_version ) ) {
$this->_update( $library );
}
}
}
/**
* Hide action links for Libraries.
*
* @param array $actions Action links.
* @return array Action links.
*/
public function actions( $actions ) {
global $status;
if ( 'tinylibraries' !== $status ) {
return $actions;
}
return array();
}
/**
* Update a library
*
* @param string $library Library slug.
* @return void
*/
private function _update( $library ) {
$this->_delete( $library );
$this->_install( $library );
}
/**
* Install a Library.
*
* @param string $library Library slug.
* @return bool Success/fail.
*/
private function _install( $library ) {
global $wp_filesystem;
$data = $this->_get_library_data( $library );
if ( ! $data ) {
return false;
}
$dir = WP_CONTENT_DIR . "/libraries/{$library}/";
if ( file_exists( $dir ) ) {
return false;
}
$result = download_url( $data['download'] );
if ( ! $wp_filesystem ) {
WP_Filesystem();
}
$unzip = unzip_file( $result, $dir );
if ( is_wp_error( $unzip ) ) {
return false;
}
$dirlist = $wp_filesystem->dirlist( $dir );
$dirlist = array_keys( $dirlist );
$library_dir = array_pop( $dirlist );
$main_file = "{$dir}/{$library}.php";
$content = '<?php' . PHP_EOL;
$content .= '/*' . PHP_EOL;
$content .= "Library Name: {$data['name']}" . PHP_EOL;
$content .= "Description: {$data['description']}" . PHP_EOL;
$content .= "Version: {$data['version']}" . PHP_EOL;
$content .= "Class: {$data['main_class']}" . PHP_EOL;
$content .= "File: {$data['main_file']}" . PHP_EOL;
$content .= "Author: {$data['author']}" . PHP_EOL;
$content .= "Author URI: {$data['author_uri']}" . PHP_EOL;
$content .= "Library URI: {$data['uri']}" . PHP_EOL;
$content .= '*/' . PHP_EOL;
$content .= "if ( !class_exists( '{$data['main_class']}' ) ) {" . PHP_EOL;
$content .= " require_once( '{$dir}{$library_dir}/{$data['main_file']}');" . PHP_EOL;
$content .= '}' . PHP_EOL;
$wp_filesystem->put_contents( $main_file, $content );
return true;
}
/**
* Delete library
*
* @param string $library Library slug.
* @return bool Success/fail
*/
private function _delete( $library ) {
global $wp_filesystem;
if ( ! $wp_filesystem ) {
WP_Filesystem();
}
$libdir = WP_CONTENT_DIR . '/libraries/' . $library;
return $wp_filesystem->rmdir( $libdir, true );
}
/**
* Get list of requested Libraries
*
* @param strng $plugin Plugin path.
* @return array List of requested libraries.
*/
private function _get_required_libraries( $plugin ) {
$default_headers = array(
'Name' => 'Plugin Name',
'Libraries' => 'Libraries',
);
$data = get_file_data( WP_PLUGIN_DIR . '/' . $plugin, $default_headers, 'plugin' );
$libraries = $data['Libraries'];
$libraries = explode( ',', $libraries );
$none = array(
0 => '',
);
if ( $none === $libraries ) {
$libraries = false;
}
return $libraries;
}
/**
* Get list of installed Libraries.
*
* @return array List of installed libraries.
*/
private function _get_libraries() {
global $wp_filesystem;
if ( ! $wp_filesystem ) {
WP_Filesystem();
}
$dir = WP_CONTENT_DIR . '/libraries/';
$dirlist = $wp_filesystem->dirlist( $dir );
$libraries = array();
foreach ( $dirlist as $library => $library_data ) {
$libraries[ $library ] = $dir . $library . '/' . $library . '.php';
}
return $libraries;
}
/**
* Get current Library information.
*
* @param string $file Library init file.
* @return array Library info.
*/
private function _get_library_info( $file ) {
$default_headers = array(
'Name' => 'Library Name',
'Description' => 'Description',
'Version' => 'Version',
'Author' => 'Author',
'AuthorURI' => 'Author URI',
'PluginURI' => 'Library URI',
'TextDomain' => 'Text Domain',
);
$data = get_file_data( $file, $default_headers, 'plugin' );
return $data;
}
/**
* Get information about available Libraries.
*
* @param string $library Library slug.
* @return mixed array of library info or false if library not found.
*/
private function _get_library_data( $library ) {
$url = "https://api.aru.lt/json/tinylibraries/v1/library/{$library}";
$data = wp_remote_get( $url );
if ( is_wp_error( $data ) ) {
return false;
}
$data = $data['body'];
$data = json_decode( $data, true );
return $data;
}
}