PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.10.0
Translate and Go multilingual – Automatic AI translation – wpLingua v2.10.0
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 / html.php

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

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