PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.11.7
Translate and Go multilingual – Automatic AI translation – wpLingua v2.11.7
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 1.2.1 All 109 releases
wplingua / wplingua.php

wplingua.php in Translate and Go multilingual – Automatic AI translation – wpLingua 2.11.7, at wplingua.php

421 lines 15.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: wpLingua
4 * Plugin URI: https://wplingua.com/
5 * Description: An all-in-one solution that makes your websites multilingual and translates them automatically, without word or page limits. The highlights: a free first language, an on-page visual editor for editing translations, a customizable language switcher, search engine optimization (SEO), self-hosted data and more!
6 * Author: wpLingua Translation Service
7 * Author URI: https://wplingua.com/
8 * Text Domain: wplingua
9 * Domain Path: /languages/
10 * Version: 2.11.7
11 * Requires PHP: 7.4
12 * License: GPL v2 or later
13 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
14 */
15
16
17 // If this file is called directly, abort.
18 if ( ! defined( 'WPINC' ) ) {
19 die;
20 }
21
22
23 // Define wpLingua constants
24 define( 'WPLNG_API_URL', 'https://api.wplingua.com' );
25 define( 'WPLNG_API_VERSION', '3.0' );
26 define( 'WPLNG_API_SSLVERIFY', true );
27 define( 'WPLNG_PLUGIN_VERSION', '2.11.7' );
28 define( 'WPLNG_PLUGIN_FILE', plugin_basename( __FILE__ ) );
29 define( 'WPLNG_PLUGIN_DIR', __DIR__ );
30 define( 'WPLNG_CACHE_DIR', WP_CONTENT_DIR . '/wplingua-cache' );
31 define( 'WPLNG_PHP_MIN_VERSION', '7.4' );
32 define( 'WPLNG_MAX_TRANSLATIONS_STR', 220 );
33 define( 'WPLNG_MAX_TRANSLATIONS_CHAR', 4200 );
34 define( 'WPLNG_MAX_FILE_SIZE', 5000000 );
35
36
37 // Define debug constants
38 defined( 'WPLNG_DEBUG_JSON' ) || define( 'WPLNG_DEBUG_JSON', false );
39 defined( 'WPLNG_DEBUG_AJAX' ) || define( 'WPLNG_DEBUG_AJAX', false );
40 defined( 'WPLNG_DEBUG_REST' ) || define( 'WPLNG_DEBUG_REST', false );
41 defined( 'WPLNG_DEBUG_BEAT' ) || define( 'WPLNG_DEBUG_BEAT', false );
42 defined( 'WPLNG_DEBUG_XML' ) || define( 'WPLNG_DEBUG_XML', false );
43
44
45 // Load all needed PHP files
46 require_once WPLNG_PLUGIN_DIR . '/loader.php';
47
48
49 /**
50 * Loads wpLingua plugin's translated strings.
51 *
52 * @return void
53 */
54 function wplng_load_plugin_textdomain() {
55 load_plugin_textdomain(
56 'wplingua',
57 false,
58 'wplingua/languages'
59 );
60 }
61
62
63 /**
64 * Register all wpLingua Hook
65 *
66 * @return void
67 */
68 function wplng_start() {
69
70 // Setup $wplng_class_reload
71 global $wplng_class_reload;
72 $wplng_class_reload = false;
73
74 // Setup $wplng_request_uri
75 if ( isset( $_SERVER['REQUEST_URI'] ) ) {
76
77 $request_uri = sanitize_url( $_SERVER['REQUEST_URI'] );
78 $decoded_uri = urldecode( $request_uri );
79
80 // Check if the request URI is clean
81 if ( strtolower( esc_url_raw( $request_uri ) ) !== strtolower( $request_uri )
82 || wplng_str_is_malicious( $request_uri )
83 || wplng_str_is_malicious( $decoded_uri )
84 ) {
85 return;
86 }
87
88 // Also check query string for double-encoded attacks
89 if ( isset( $_SERVER['QUERY_STRING'] ) ) {
90 $query_string = sanitize_text_field( $_SERVER['QUERY_STRING'] );
91 $decoded_query = urldecode( urldecode( $query_string ) );
92
93 if ( wplng_str_is_malicious( $decoded_query ) ) {
94 return;
95 }
96 }
97
98 global $wplng_request_uri;
99 $wplng_request_uri = $request_uri;
100 }
101
102 // The plugin version has changed
103 if ( get_option( 'wplng_version' ) !== WPLNG_PLUGIN_VERSION ) {
104 update_option( 'wplng_version', WPLNG_PLUGIN_VERSION, true );
105 wplng_clear_translations_cache();
106 wplng_clear_slugs_cache();
107 wplng_clear_folder_cache();
108 }
109
110 // Load plugin text domain /languages/
111 add_action( 'init', 'wplng_load_plugin_textdomain' );
112
113 // Do not cache page for connected editor
114 add_action( 'init', 'wplng_init_manage_cache', 1 );
115
116 // Display a notice if incompatibility is detected
117 add_action( 'admin_notices', 'wplng_admin_notice_incompatible_plugin', 1 );
118 add_action( 'admin_notices', 'wplng_admin_notice_incompatible_multisite', 1 );
119 add_action( 'admin_notices', 'wplng_admin_notice_incompatible_sub_folder', 1 );
120 add_action( 'admin_notices', 'wplng_admin_notice_incompatible_php_version', 1 );
121 add_action( 'admin_notices', 'wplng_admin_notice_incompatible_htaccess', 1 );
122 add_action( 'admin_notices', 'wplng_admin_notice_incompatible_plain_permalink', 1 );
123
124 // Return if incompatibility is detected
125 if ( ! empty( wplng_get_incompatible_plugins() )
126 || is_multisite()
127 || wplng_website_in_sub_folder()
128 || ( version_compare( PHP_VERSION, WPLNG_PHP_MIN_VERSION ) < 0 )
129 || ! wplng_htaccess_is_valid()
130 || empty( get_option( 'permalink_structure' ) )
131 ) {
132 return;
133 }
134
135 // Register plugin settings
136 add_action( 'admin_init', 'wplng_register_settings' );
137
138 // Add settings link in plugin list
139 add_filter( 'plugin_action_links_' . WPLNG_PLUGIN_FILE, 'wplng_settings_link' );
140
141 // Redirect to the settings page on plugin activation
142 add_action( 'activated_plugin', 'wplng_plugin_activation_redirect' );
143
144 // Set footer text for options pages
145 add_filter( 'admin_footer_text', 'wplng_admin_footer_text', 11 );
146 add_filter( 'update_footer', 'wplng_update_footer', 11 );
147
148 // Print head script (JSON with all languages informations)
149 add_action( 'toplevel_page_wplingua-settings', 'wplng_inline_script_languages' );
150
151 if ( empty( wplng_get_api_data() ) ) {
152
153 // Add Register option page in back office menu
154 add_action( 'admin_menu', 'wplng_create_menu_register' );
155
156 // Enqueue CSS and JS files for register option pages
157 add_action( 'admin_enqueue_scripts', 'wplng_option_page_register_assets' );
158
159 // Display a notice if the plugin is activate but not configured
160 add_action( 'admin_notices', 'wplng_admin_notice_no_key_set', 1 );
161
162 } else {
163
164 /**
165 * Back office
166 */
167
168 // Add menu in back office
169 add_action( 'admin_menu', 'wplng_create_menu' );
170
171 // Add admin Bar menu
172 add_action( 'admin_bar_menu', 'wplng_admin_bar_menu', 81 );
173 add_action( 'admin_bar_menu', 'wplng_admin_bar_edit', 81 );
174
175 // Switcher in nav menu options
176 add_action( 'admin_enqueue_scripts', 'wplng_switcher_nav_menu_inline_scripts' );
177 add_action( 'admin_head-nav-menus.php', 'wp_nav_menu_switcher_box_add_register' );
178 add_action( 'wp_nav_menu_item_custom_fields', 'wp_nav_menu_switcher_box_edit', 10, 2 );
179
180 // Enqueue CSS and JS files for option pages
181 add_action( 'admin_enqueue_scripts', 'wplng_option_page_settings_assets' );
182 add_action( 'admin_enqueue_scripts', 'wplng_option_page_switcher_assets' );
183 add_action( 'admin_enqueue_scripts', 'wplng_option_page_exclusions_assets' );
184 add_action( 'admin_enqueue_scripts', 'wplng_option_page_dictionary_assets' );
185 add_action( 'admin_enqueue_scripts', 'wplng_option_page_link_media_assets' );
186
187 // Update flags URL
188 add_action( 'update_option_wplng_flags_style', 'wplng_options_switcher_update_flags_style', 10, 2 );
189
190 // Reset API data on API key changing
191 add_action( 'update_option_wplng_api_key', 'wplng_on_update_option_wplng_api_key', 10, 2 );
192
193 // Add edit link on page and post list
194 add_filter( 'page_row_actions', 'wplng_row_edit_translation_link', 10, 2 );
195 add_filter( 'post_row_actions', 'wplng_row_edit_translation_link', 10, 2 );
196
197 /**
198 * wplng_translation : CPT, taxo, meta
199 */
200
201 // Register wplng_translation CPT
202 add_action( 'init', 'wplng_register_post_type_translation' );
203
204 // Add metabox for wplng_translation
205 add_action( 'add_meta_boxes_wplng_translation', 'wplng_translation_add_meta_box' );
206
207 // Save metabox on posts saving
208 add_action( 'save_post_wplng_translation', 'wplng_translation_save_meta_boxes_data', 10, 2 );
209
210 // Clear translation cache on trash / untrash
211 add_action( 'trashed_post', 'wplng_clear_translations_cache_trash_untrash' );
212 add_action( 'untrash_post', 'wplng_clear_translations_cache_trash_untrash' );
213 add_action( 'delete_post', 'wplng_clear_translations_cache_trash_untrash' );
214
215 // Enqueue Script for wplng_translation admin: Edit
216 add_action( 'admin_print_scripts-post.php', 'wplng_translation_edit_assets' );
217
218 // Enqueue Script for wplng_translation admin: List
219 add_action( 'admin_print_scripts-edit.php', 'wplng_translation_list_assets' );
220
221 // Only show translations for the website languages
222 add_action( 'pre_get_posts', 'wplng_filter_wplng_translation_posts' );
223
224 // Remove Quick edit from translations list
225 add_filter( 'post_row_actions', 'wplng_translation_remove_quick_edit', 10, 2 );
226
227 // Ajax function for regenerate translation on edit page
228 add_action( 'wp_ajax_wplng_ajax_translation', 'wplng_ajax_generate_translation' );
229
230 // Ajax function for edit modal: Get HTML modal
231 add_action( 'wp_ajax_wplng_ajax_edit_modal', 'wplng_ajax_edit_modal' );
232
233 // Ajax function for edit modal: Save modal
234 add_action( 'wp_ajax_wplng_ajax_save_modal', 'wplng_ajax_save_modal' );
235
236 // Ajax function for edit modal: HeartBeat
237 add_action( 'wp_ajax_wplng_ajax_heartbeat', 'wplng_ajax_heartbeat' );
238 add_action( 'wp_ajax_nopriv_wplng_ajax_heartbeat', 'wplng_ajax_heartbeat' );
239
240 // Display 100 translation in admin area by default
241 add_filter( 'get_user_option_edit_wplng_translation_per_page', 'wplng_translation_per_page' );
242
243 // Filter translations by status
244 add_action( 'restrict_manage_posts', 'wplng_restrict_manage_posts_translation_status' );
245 add_filter( 'parse_query', 'wplng_posts_filter_translation_status' );
246
247 // Translation status on website translations list
248 add_filter( 'post_class', 'wplng_post_class_translation_status', 10, 3 );
249 add_filter( 'post_row_actions', 'wplng_post_row_actions_translation_status', 10, 2 );
250 add_filter( 'manage_wplng_translation_posts_columns', 'wplng_translation_status_columns' );
251 add_action( 'manage_wplng_translation_posts_custom_column', 'wplng_translation_status_item', 10, 2 );
252
253 /**
254 * wplng_slug : CPT, taxo, meta
255 */
256
257 // Register wplng_translation CPT
258 add_action( 'init', 'wplng_register_post_type_slug' );
259
260 // Add metabox for wplng_slug
261 add_action( 'add_meta_boxes_wplng_slug', 'wplng_slug_add_meta_box' );
262
263 // Save metabox on posts saving
264 add_action( 'save_post_wplng_slug', 'wplng_slug_save_meta_boxes_data', 10, 2 );
265
266 // Clear slugs cache on trash / untrash
267 add_action( 'trashed_post', 'wplng_clear_slugs_cache_trash_untrash' );
268 add_action( 'untrash_post', 'wplng_clear_slugs_cache_trash_untrash' );
269 add_action( 'delete_post', 'wplng_clear_slugs_cache_trash_untrash' );
270
271 // Enqueue Script for wplng_slug admin: Edit
272 add_action( 'admin_print_scripts-post.php', 'wplng_slug_edit_assets' );
273
274 // Enqueue Script for wplng_slug admin: List
275 add_action( 'admin_print_scripts-edit.php', 'wplng_slug_list_assets' );
276
277 // Only show slugs for the website languages
278 add_action( 'pre_get_posts', 'wplng_filter_wplng_slug_posts' );
279
280 // Remove Quick edit from slugs list
281 add_filter( 'post_row_actions', 'wplng_slug_remove_quick_edit', 10, 2 );
282
283 // Ajax function for regenerate slug on edit page
284 add_action( 'wp_ajax_wplng_ajax_slug', 'wplng_ajax_generate_slug' );
285
286 // Display 100 translation in admin area by default
287 add_filter( 'get_user_option_edit_wplng_slug_per_page', 'wplng_slug_per_page' );
288
289 // Filter slugs by status
290 add_action( 'restrict_manage_posts', 'wplng_restrict_manage_posts_slug_status' );
291 add_filter( 'parse_query', 'wplng_posts_filter_slug_status' );
292
293 // Translation status on website slugs list
294 add_filter( 'post_class', 'wplng_post_class_slug_status', 10, 3 );
295 add_filter( 'post_row_actions', 'wplng_post_row_actions_slug_status', 10, 2 );
296 add_filter( 'manage_wplng_slug_posts_columns', 'wplng_slug_status_columns' );
297 add_action( 'manage_wplng_slug_posts_custom_column', 'wplng_slug_status_item', 10, 2 );
298
299 /**
300 * Front
301 */
302
303 // Enqueue CSS and JS files
304 add_action( 'wp_enqueue_scripts', 'wplng_register_assets' );
305
306 // Add custom inline styles (option page: switcher)
307 add_action( 'wp_head', 'wplng_add_custom_inline_styles', 50 );
308
309 // Script JS in page
310 add_action( 'wp_footer', 'wplng_on_page_script' );
311
312 // Add languages switcher before </body>
313 add_action( 'wp_footer', 'wplng_switcher_wp_footer' );
314
315 // Add languages switcher in nav menu
316 add_filter( 'wp_nav_menu_objects', 'wplng_switcher_nav_menu_replace_items' );
317 add_filter( 'nav_menu_link_attributes', 'wplng_add_nav_menu_link_attributes_atts', 10, 2 );
318
319 // Set alternate links with hreflang parametters
320 add_action( 'wp_head', 'wplng_link_alternate_hreflang', 1 );
321
322 // Disable web browser automatic translation
323 add_action( 'language_attributes', 'wplng_disable_web_browser_auto_translate' );
324
325 // Ajax function for Load in progress
326 add_action( 'wp_ajax_wplng_load_in_progress', 'wplng_ajax_load_in_progress' );
327
328 /**
329 * OB and REQUEST_URI
330 */
331
332 // Manage URL with REQUEST_URI and start OB
333 add_action( 'init', 'wplng_ob_start', 1 );
334
335 // Redirect page if is called with an untranslate slug
336 add_action( 'template_redirect', 'wplng_redirect_translated_slug' );
337
338 /**
339 * Generate JSON translation on the fly for wp-i18n.js
340 */
341
342 // Generate JSON translation
343 add_filter( 'load_script_translation_file', 'wplng_load_script_translation_file', 20, 3 );
344
345 /**
346 * Clear wpLingua cache on updates
347 */
348
349 // Clear wpLingua cache on WordPress core, plugins, themes or translations update
350 add_action( 'upgrader_process_complete', 'wplng_clear_cache_on_update', 10, 2 );
351
352 // Clear wpLingua cache on plugin activation/deactivation
353 add_action( 'activated_plugin', 'wplng_clear_folder_cache' );
354 add_action( 'deactivated_plugin', 'wplng_clear_folder_cache' );
355
356 // Clear wpLingua cache on theme switch
357 add_action( 'switch_theme', 'wplng_clear_folder_cache' );
358
359 // Clear wpLingua cache on site language change
360 add_action( 'update_option_WPLANG', 'wplng_clear_folder_cache' );
361
362 // Clear wpLingua cache on wpLingua settings change
363 add_action( 'update_option_wplng_website_language', 'wplng_clear_folder_cache' );
364 add_action( 'update_option_wplng_target_languages', 'wplng_clear_folder_cache' );
365
366 /**
367 * Features
368 */
369
370 // Make multilingua Sitemap XML for All In One SEO plugin
371 // Il other case, multilingual Sitemap is make by output buffering
372 if ( get_option( 'wplng_sitemap_xml', true )
373 && function_exists( 'aioseo' )
374 ) {
375 add_filter( 'aioseo_sitemap_post', 'wplng_aioseo_filter_sitemap_post', 10, 2 );
376 add_filter( 'aioseo_sitemap_term', 'wplng_aioseo_filter_sitemap_term', 10, 2 );
377 }
378
379 // Search from translated languages
380 if ( ! empty( get_option( 'wplng_translate_search' ) )
381 && wplng_api_feature_is_allow( 'search' )
382 ) {
383 add_action( 'parse_query', 'wplng_translate_search_query' );
384 } else {
385 add_filter( 'wplng_url_is_translatable', 'wplng_exclude_search', 20 );
386 }
387
388 // Redirect by browser language
389 $browser_language_redirect = get_option( 'wplng_browser_language_redirect' );
390 if ( $browser_language_redirect === 'js_only' ) {
391 add_action( 'wp_head', 'wplng_browser_language_redirect_js_only', 2 );
392 } elseif ( $browser_language_redirect === 'php_js' ) {
393 add_action( 'template_redirect', 'wplng_browser_language_redirect_php_js' );
394 }
395
396 /**
397 * Shortcode
398 */
399
400 add_shortcode( 'wplng_switcher', 'wplng_shortcode_switcher' );
401 add_shortcode( 'wplng_notranslate', 'wplng_shortcode_notranslate' );
402 add_shortcode( 'wplng_only', 'wplng_shortcode_only' );
403
404 /**
405 * Gutenberg
406 */
407
408 add_filter( 'block_categories_all', 'wplng_block_category' );
409 add_action( 'init', 'wplng_register_block' );
410 add_action( 'enqueue_block_editor_assets', 'wplng_register_block_assets' );
411
412 /**
413 * Do action after wpLingua fully initialized
414 */
415
416 do_action( 'wplng_initialized' );
417
418 }
419 }
420 wplng_start();
421