PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / trunk
Translate and Go multilingual – Automatic AI translation – wpLingua vtrunk
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 1.2.0 All 110 releases
wplingua / inc / parser / html.php

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

159 lines 2.8 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 HTML
11 *
12 * @param string|object $html string or $dom object
13 * @return array Texts
14 */
15 function wplng_parse_html( $html ) {
16
17 $texts = array();
18 $dom = wplng_sdh_str_get_html( $html );
19
20 if ( empty( $dom ) ) {
21 return array();
22 }
23
24 /**
25 * Parse Node text
26 */
27
28 $node_text_excluded = wplng_data_excluded_node_text();
29
30 foreach ( $dom->find( 'text' ) as $element ) {
31
32 if ( in_array( $element->parent->tag, $node_text_excluded ) ) {
33 continue;
34 }
35
36 $text = wplng_text_esc( $element->innertext );
37
38 if ( ! wplng_text_is_translatable( $text ) ) {
39 continue;
40 }
41
42 $texts[] = $text;
43 }
44
45 /**
46 * Parse texts in attributes
47 */
48
49 $attr_texts_to_translate = wplng_data_attr_text_to_translate();
50
51 foreach ( $attr_texts_to_translate as $attr ) {
52 foreach ( $dom->find( $attr['selector'] ) as $element ) {
53
54 if ( empty( $element->attr[ $attr['attr'] ] ) ) {
55 continue;
56 }
57
58 $text = wplng_text_esc( $element->attr[ $attr['attr'] ] );
59
60 if ( ! wplng_text_is_translatable( $text ) ) {
61 continue;
62 }
63
64 $texts[] = $text;
65 }
66 }
67
68 /**
69 * Parse HTML in attributes
70 */
71
72 $attr_html_to_translate = wplng_data_attr_html_to_translate();
73
74 foreach ( $attr_html_to_translate as $attr ) {
75 foreach ( $dom->find( $attr['selector'] ) as $element ) {
76
77 if ( empty( $element->attr[ $attr['attr'] ] ) ) {
78 continue;
79 }
80
81 $html = wp_specialchars_decode(
82 $element->attr[ $attr['attr'] ],
83 ENT_QUOTES | ENT_HTML5
84 );
85
86 $texts = array_merge(
87 $texts,
88 wplng_parse_html( $html )
89 );
90
91 }
92 }
93
94 /**
95 * Parse JSON in attributes
96 */
97
98 $attr_json_to_translate = wplng_data_attr_json_to_translate();
99
100 foreach ( $attr_json_to_translate as $attr ) {
101 foreach ( $dom->find( $attr['selector'] ) as $element ) {
102
103 if ( empty( $element->attr[ $attr['attr'] ] ) ) {
104 continue;
105 }
106
107 $json = wp_specialchars_decode(
108 $element->attr[ $attr['attr'] ],
109 ENT_QUOTES | ENT_HTML5
110 );
111
112 $texts = array_merge(
113 $texts,
114 wplng_parse_json(
115 $json,
116 array( $attr['attr'] )
117 )
118 );
119 }
120 }
121
122 /**
123 * Find and translate script
124 */
125
126 foreach ( $dom->find( 'script' ) as $element ) {
127
128 if ( ! empty( $element->attr['type'] )
129 && (
130 $element->attr['type'] === 'application/ld+json'
131 || $element->attr['type'] === 'application/json'
132 )
133 ) {
134
135 /**
136 * Find and parse JSON in scripts
137 */
138
139 $texts = array_merge(
140 $texts,
141 wplng_parse_json( $element->innertext )
142 );
143
144 } else {
145
146 /**
147 * Find and translate JS in scripts
148 */
149
150 $texts = array_merge(
151 $texts,
152 wplng_parse_js( $element->innertext )
153 );
154 }
155 }
156
157 return $texts;
158 }
159