| @@ -6,15 +6,85 @@ | ||
| 6 | 6 | } |
| 7 | 7 | |
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | - * Translate search query if page is translated | |
| 10 | + * Replace the search query value by a placeholder tag. | |
| 11 | 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 | + * | |
| 12 | 80 | * @param object $query |
| 13 | 81 | * @return void |
| 14 | 82 | */ |
| 15 | 83 | function wplng_translate_search_query( $query ) { |
| 16 | 84 | |
| 85 | + global $wplng_translate_search_query; | |
| 86 | + | |
| 17 | 87 | /** |
| 18 | 88 | * Check if it's a search query |
| 19 | 89 | */ |
| 20 | 90 | |
| @@ -37,17 +107,25 @@ | ||
| 37 | 107 | return; |
| 38 | 108 | } |
| 39 | 109 | |
| 40 | 110 | /** |
| 41 | - * Sanitize search query and check if is translatale | |
| 111 | + * Sanitize search query and check if is translatable | |
| 42 | 112 | */ |
| 43 | 113 | |
| 44 | 114 | $search_string = sanitize_text_field( $query->query['s'] ); |
| 115 | + $search_string = wplng_text_esc( $search_string ); | |
| 45 | 116 | |
| 46 | 117 | if ( ! wplng_text_is_translatable( $search_string ) ) { |
| 47 | 118 | return; |
| 48 | 119 | } |
| 49 | 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 | + | |
| 50 | 128 | /** |
| 51 | 129 | * Call API to get the translation |
| 52 | 130 | */ |
| 53 | 131 | |
| @@ -66,17 +144,13 @@ | ||
| 66 | 144 | /** |
| 67 | 145 | * Check and clear the translation |
| 68 | 146 | */ |
| 69 | 147 | |
| 70 | - // Remove added ponctuation | |
| 71 | - $translated_search = preg_replace( | |
| 72 | - '#[^A-Za-z0-9]#', | |
| 73 | - '', | |
| 74 | - $translated_search | |
| 75 | - ); | |
| 148 | + // Remove leading/trailing punctuation added by translation API | |
| 149 | + $translated_search = trim( $translated_search, " \t\n\r\0\x0B.,;:!?\"'()[]{}…" ); | |
| 76 | 150 | |
| 77 | 151 | // Clear translation |
| 78 | - $translated_search = trim( esc_attr( $translated_search ) ); | |
| 152 | + $translated_search = wplng_text_esc( $translated_search ); | |
| 79 | 153 | |
| 80 | 154 | // Check if translation is not empty after cleaning |
| 81 | 155 | if ( '' === $translated_search ) { |
| 82 | 156 | return; |
| @@ -85,9 +159,9 @@ | ||
| 85 | 159 | /** |
| 86 | 160 | * Replace the search text by the translation |
| 87 | 161 | */ |
| 88 | 162 | |
| 89 | - $query->set( 's', $translated_search ); | |
| 163 | + $query->set( 's', $translated_search ); | |
| 90 | 164 | } |
| 91 | 165 | |
| 92 | 166 | |
| 93 | 167 | /** |