PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / trunk
Translate and Go multilingual – Automatic AI translation – wpLingua vtrunk
2.16.6 2.16.5 2.16.4 2.16.3 2.16.2 2.16.1 2.16.0 2.15.2 2.15.1 2.15.0 2.14.3 2.14.2 2.14.1 2.14.0 2.13.1 2.13.0 2.12.3 2.12.2 2.12.1 trunk 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 All 111 releases
wplingua / inc / parser / json.php

json.php in Translate and Go multilingual – Automatic AI translation – wpLingua trunk, at inc/parser/json.php

148 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // If this file is called directly, abort.
4 if ( ! defined( 'WPINC' ) ) {
5 die;
6 }
7
8
9 /**
10 * wpLingua Parser : Get texts in an JSON
11 *
12 * @param string $json
13 * @param array $parents
14 * @return array Texts
15 */
16 function wplng_parse_json( $json, $parents = array() ) {
17
18 if ( empty( $json ) ) {
19 return array();
20 }
21
22 $json_decoded = json_decode( $json, true );
23
24 if ( empty( $json_decoded ) || ! is_array( $json_decoded ) ) {
25 return array();
26 }
27
28 $texts = wplng_parse_json_array( $json_decoded, $parents );
29
30 return $texts;
31 }
32
33
34 /**
35 * wpLingua Parser : Get texts in an array decoded from a JSON
36 *
37 * @param array $json_decoded
38 * @param array $parents
39 * @return array Texts
40 */
41 function wplng_parse_json_array( $json_decoded, $parents = array() ) {
42
43 $texts = array();
44
45 /**
46 * Don't parse JSON if it's exclude
47 */
48
49 if ( wplng_json_element_is_excluded( $json_decoded, $parents ) ) {
50 return array();
51 }
52
53 /**
54 * Parse each JSON elements
55 */
56
57 foreach ( $json_decoded as $key => $value ) {
58
59 $current_parents = array_merge( $parents, array( $key ) );
60
61 /**
62 * Don't parse element if it's exclude
63 */
64
65 if ( wplng_json_element_is_excluded( $value, $current_parents ) ) {
66 continue;
67 }
68
69 /**
70 * Check the value
71 */
72
73 if ( is_array( $value ) ) {
74
75 /**
76 * If element is an array, parse it
77 */
78
79 $texts = array_merge(
80 $texts,
81 wplng_parse_json_array( $value, $current_parents )
82 );
83
84 } elseif ( is_string( $value ) ) {
85
86 /**
87 * If element is a string
88 */
89
90 // Ignore if is an URL or a local ID (fr_FR, fr, FR, ...)
91 if ( wplng_str_is_url( $value )
92 || wplng_str_is_locale_id( $value )
93 ) {
94 continue;
95 }
96
97 if ( wplng_str_is_html( $value ) ) {
98
99 /**
100 * If element is a HTML, parse it
101 */
102
103 $texts = array_merge(
104 $texts,
105 wplng_parse_html( $value )
106 );
107
108 } elseif ( wplng_str_is_json( $value ) ) {
109
110 /**
111 * If element is a JSON, parse it
112 */
113
114 $texts = array_merge(
115 $texts,
116 wplng_parse_json( $value, $current_parents )
117 );
118
119 } elseif ( wplng_str_is_script_i18n( $value ) ) {
120
121 /**
122 * If element is a i18n JSON, parse it
123 */
124
125 // Voluntarily don't pass $current_parents
126 $texts = array_merge(
127 $texts,
128 wplng_parse_js_json_in_i18n_script( $value )
129 );
130
131 } else {
132
133 /**
134 * Element is a unknow string, check if it's translatable
135 */
136
137 if ( wplng_text_is_translatable( $value )
138 && wplng_json_element_is_included( $value, $current_parents )
139 ) {
140 $texts[] = $value;
141 }
142 }
143 }
144 }
145
146 return $texts;
147 }
148