PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.16.6
Translate and Go multilingual – Automatic AI translation – wpLingua v2.16.6
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 / search.php

search.php in Translate and Go multilingual – Automatic AI translation – wpLingua 2.16.6, at inc/search.php

176 lines 4.0 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 * Replace the search query value by a placeholder tag.
11 *
12 * Used before page translation to prevent the search query
13 * from being translated as part of the page content.
14 *
15 * @param string $search The current search query string.
16 * @return string The placeholder tag, or the original search string if no translation is pending.
17 */
18 function wplng_search_put_tag( $search ) {
19
20 global $wplng_translate_search_query;
21
22 if ( $wplng_translate_search_query === null ) {
23 return $search;
24 }
25
26 return '%wplng-search-query%';
27 }
28
29
30
31 /**
32 * Restore the translated search query in the page HTML.
33 *
34 * Replaces the placeholder tag previously inserted by wplng_search_put_tag()
35 * with the actual (translated) search query string.
36 *
37 * @param string $html The page HTML content containing the placeholder tag.
38 * @return string The HTML with the placeholder replaced by the translated search query.
39 */
40 function wplng_search_replace_tag( $html ) {
41
42 global $wplng_translate_search_query;
43
44 if ( $wplng_translate_search_query === null ) {
45 return $html;
46 }
47
48 if ( current_user_can( 'edit_posts' )
49 && ! empty( $_GET['wplng-mode'] )
50 && ( $_GET['wplng-mode'] === 'editor'
51 || $_GET['wplng-mode'] === 'list'
52 )
53 ) {
54 return $html;
55 }
56
57 $html = str_replace(
58 '%wplng-search-query%',
59 wplng_text_esc(
60 $wplng_translate_search_query
61 ),
62 $html
63 );
64
65 return $html;
66 }
67
68
69 /**
70 * Translate search query if page is translated and e
71 * enable visitors to search on your website in their own language.
72 *
73 * Example: if your website is translated from English to French and
74 * you have a post named "Hello". When a French visitor searches for
75 * the term "Bonjour" on the website, this feature will translate it
76 * on the fly to "Hello" before launching the search. This will allow
77 * WordPress to find the post named "Hello" when your visitor has
78 * searched for "Bonjour".
79 *
80 * @param object $query
81 * @return void
82 */
83 function wplng_translate_search_query( $query ) {
84
85 global $wplng_translate_search_query;
86
87 /**
88 * Check if it's a search query
89 */
90
91 if ( ! $query->is_search()
92 || ! isset( $query->query['s'] )
93 || ! is_string( $query->query['s'] )
94 ) {
95 return;
96 }
97
98 /**
99 * Get current and website languages
100 * Ckeck if search need to be translate
101 */
102
103 $language_current = wplng_get_language_current_id();
104 $language_website = wplng_get_language_website_id();
105
106 if ( $language_website === $language_current ) {
107 return;
108 }
109
110 /**
111 * Sanitize search query and check if is translatable
112 */
113
114 $search_string = sanitize_text_field( $query->query['s'] );
115 $search_string = wplng_text_esc( $search_string );
116
117 if ( ! wplng_text_is_translatable( $search_string ) ) {
118 return;
119 }
120
121 if ( wplng_str_is_malicious( $search_string ) ) {
122 $query->set( 's', '' );
123 return;
124 }
125
126 $wplng_translate_search_query = $search_string;
127
128 /**
129 * Call API to get the translation
130 */
131
132 $translated_search = wplng_api_call_translate(
133 array( $search_string ),
134 $language_current,
135 $language_website
136 );
137
138 if ( ! isset( $translated_search[0] ) ) {
139 return;
140 }
141
142 $translated_search = $translated_search[0];
143
144 /**
145 * Check and clear the translation
146 */
147
148 // Remove leading/trailing punctuation added by translation API
149 $translated_search = trim( $translated_search, " \t\n\r\0\x0B.,;:!?\"'()[]{}…" );
150
151 // Clear translation
152 $translated_search = wplng_text_esc( $translated_search );
153
154 // Check if translation is not empty after cleaning
155 if ( '' === $translated_search ) {
156 return;
157 }
158
159 /**
160 * Replace the search text by the translation
161 */
162
163 $query->set( 's', $translated_search );
164 }
165
166
167 /**
168 * Make search untranslated
169 *
170 * @param bool $is_translatable
171 * @return void
172 */
173 function wplng_exclude_search( $is_translatable ) {
174 return $is_translatable && ! ( is_search() || isset( $_GET['s'] ) );
175 }
176