forked from OnTheGoSystems/wpml-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.php
78 lines (61 loc) · 1.71 KB
/
update.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
<?php
/**
* Parses the XML index file and generates the corresponding JSON file.
*
* The WPML plugin will read the JSON file to keep up to date with changes.
*/
error_reporting( E_ALL );
$config = new WPML_Config_Index;
$config->update();
exit;
class WPML_Config_Index {
var $configs;
var $errors;
function update() {
$this->load_xml();
if ( ! $this->validate_paths() ) {
die( implode( PHP_EOL, $this->errors ) . PHP_EOL );
}
$this->save_json();
}
function load_xml() {
$this->configs = simplexml_load_file( 'config-index.xml' );
}
function validate_paths() {
$this->errors = array();
foreach ( $this->configs->plugins->item as $config ) {
$slug = $config->attributes()['id'];
$path = $slug . '/wpml-config.xml';
if ( ! file_exists( $path ) ) {
$this->errors[] = "File <$path> not found.";
}
}
return empty( $this->errors );
}
function save_json() {
$data = new stdClass;
$data->plugins = $this->get_items( $this->configs->plugins->item );
$data->themes = $this->get_items( $this->configs->themes->item );
return file_put_contents( 'config-index.json', json_encode( $data ) );
}
function get_items( $items ) {
$data = array();
foreach ( $items as $item ) {
$data[] = $this->get_item( $item );
}
return $data;
}
function get_item( $item ) {
$name = (string) $item;
$slug = (string) $item->attributes()['id'];
$path = $slug . '/wpml-config.xml';
$override_local = (bool) $item->attributes()['override_local'];
$data = new stdClass;
$data->name = $name;
$data->override_local = $override_local;
$data->path = 'wpml-config/' . $path;
$data->updated = filemtime( $path );
$data->hash = md5( file_get_contents( $path ) );
return $data;
}
}