-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
option-array-to-wpml-config.php
50 lines (39 loc) · 1.41 KB
/
option-array-to-wpml-config.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
<?php
/*
Snippet Name: wpml-config.xml admin-texts generator
Version: 0.1.0
Description: Convert an option to wpml-config.xml keys
Snippet URI: https://github.com/szepeviktor/wordpress-plugin-construction
Author: Viktor Szépe <viktor@szepe.net>
License: The MIT License (MIT)
Depends: WP-CLI
Usage: WPOPTION="OPTION-NAME" wp eval-file option-array-to-wpml-config.php > wpml-config.xml
*/
if ( empty( $_SERVER['WPOPTION'] ) || ! function_exists( 'get_option' ) ) {
exit( 1 );
}
$option_name = $_SERVER['WPOPTION'];
$option_value = get_option( $option_name );
print "<wpml-config>\n <admin-texts>\n";
recursive_process_wpml( array( $option_name => $option_value ), 2 );
print " </admin-texts>\n</wpml-config>\n";
function recursive_process_wpml( $array, $level = 0 ) {
foreach ( $array as $key => $value ) {
if ( is_array( $value ) ) {
output_wpml_key( $key, $level, false );
recursive_process_wpml( $value, $level + 1 );
output_wpml_key_close( $key, $level );
} else {
output_wpml_key( $key, $level );
}
}
}
function output_wpml_key( $name, $level, $self_closing = true ) {
$padding = $level * 4;
$close = $self_closing ? ' /' : '';
printf( "%{$padding}s<key name=\"%s\"%s>\n", '', $name, $close );
}
function output_wpml_key_close( $name, $level ) {
$padding = $level * 4;
printf( "%{$padding}s</key>\n", ' ', $name );
}