PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.9.4
Translate and Go multilingual – Automatic AI translation – wpLingua v2.9.4
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.9.4, at wplingua.php

350 lines 12.6 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.9.4
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.9.4' );
28 define( 'WPLNG_PLUGIN_FILE', plugin_basename( __FILE__ ) );
29 define( 'WPLNG_PLUGIN_PATH', __DIR__ );
30 define( 'WPLNG_PHP_MIN_VERSION', '7.4' );
31 define( 'WPLNG_MAX_TRANSLATIONS_STR', 220 );
32 define( 'WPLNG_MAX_TRANSLATIONS_CHAR', 4200 );
33 define( 'WPLNG_MAX_FILE_SIZE', 5000000 );
34
35
36 // Define debug constants
37 defined( 'WPLNG_DEBUG_JSON' ) || define( 'WPLNG_DEBUG_JSON', false );
38 defined( 'WPLNG_DEBUG_AJAX' ) || define( 'WPLNG_DEBUG_AJAX', false );
39 defined( 'WPLNG_DEBUG_BEAT' ) || define( 'WPLNG_DEBUG_BEAT', false );
40 defined( 'WPLNG_DEBUG_XML' ) || define( 'WPLNG_DEBUG_XML', false );
41
42
43 // Load all needed PHP files
44 require_once WPLNG_PLUGIN_PATH . '/loader.php';
45
46
47 /**
48 * Loads wpLingua plugin's translated strings.
49 *
50 * @return void
51 */
52 function wplng_load_plugin_textdomain() {
53 load_plugin_textdomain(
54 'wplingua',
55 false,
56 'wplingua/languages'
57 );
58 }
59
60
61 /**
62 * Register all wpLingua Hook
63 *
64 * @return void
65 */
66 function wplng_start() {
67
68 // Setup $wplng_class_reload
69 global $wplng_class_reload;
70 $wplng_class_reload = false;
71
72 // Setup $wplng_request_uri
73 if ( isset( $_SERVER['REQUEST_URI'] ) ) {
74
75 $request_uri = sanitize_url( $_SERVER['REQUEST_URI'] );
76
77 // Check if the referer is clean
78 if ( strtolower( esc_url_raw( $request_uri ) ) !== strtolower( $request_uri ) ) {
79 return;
80 }
81
82 global $wplng_request_uri;
83 $wplng_request_uri = $request_uri;
84 }
85
86 // The plugin version has changed
87 if ( get_option( 'wplng_version' ) !== WPLNG_PLUGIN_VERSION ) {
88 update_option( 'wplng_version', WPLNG_PLUGIN_VERSION, true );
89 wplng_clear_translations_cache();
90 wplng_clear_slugs_cache();
91 }
92
93 // Load plugin text domain /languages/
94 add_action( 'init', 'wplng_load_plugin_textdomain' );
95
96 // Display a notice if incompatibility is detected
97 add_action( 'admin_notices', 'wplng_admin_notice_incompatible_plugin', 1 );
98 add_action( 'admin_notices', 'wplng_admin_notice_incompatible_multisite', 1 );
99 add_action( 'admin_notices', 'wplng_admin_notice_incompatible_sub_folder', 1 );
100 add_action( 'admin_notices', 'wplng_admin_notice_incompatible_php_version', 1 );
101 add_action( 'admin_notices', 'wplng_admin_notice_incompatible_htaccess', 1 );
102
103 // Return if incompatibility is detected
104 if ( ! empty( wplng_get_incompatible_plugins() )
105 || is_multisite()
106 || wplng_website_in_sub_folder()
107 || ( version_compare( PHP_VERSION, WPLNG_PHP_MIN_VERSION ) < 0 )
108 || ! wplng_htaccess_is_valid()
109 ) {
110 return;
111 }
112
113 // Register plugin settings
114 add_action( 'admin_init', 'wplng_register_settings' );
115
116 // Add settings link in plugin list
117 add_filter( 'plugin_action_links_' . WPLNG_PLUGIN_FILE, 'wplng_settings_link' );
118
119 // Redirect to the settings page on plugin activation
120 add_action( 'activated_plugin', 'wplng_plugin_activation_redirect' );
121
122 // Set footer text for options pages
123 add_filter( 'admin_footer_text', 'wplng_admin_footer_text', 11 );
124 add_filter( 'update_footer', 'wplng_update_footer', 11 );
125
126 // Print head script (JSON with all languages informations)
127 add_action( 'toplevel_page_wplingua-settings', 'wplng_inline_script_languages' );
128
129 if ( empty( wplng_get_api_data() ) ) {
130
131 // Add Register option page in back office menu
132 add_action( 'admin_menu', 'wplng_create_menu_register' );
133
134 // Enqueue CSS and JS files for register option pages
135 add_action( 'admin_enqueue_scripts', 'wplng_option_page_register_assets' );
136
137 // Display a notice if the plugin is activate but not configured
138 add_action( 'admin_notices', 'wplng_admin_notice_no_key_set', 1 );
139
140 } else {
141
142 /**
143 * Back office
144 */
145
146 // Add menu in back office
147 add_action( 'admin_menu', 'wplng_create_menu' );
148
149 // Add admin Bar menu
150 add_action( 'admin_bar_menu', 'wplng_admin_bar_menu', 81 );
151 add_action( 'admin_bar_menu', 'wplng_admin_bar_edit', 81 );
152
153 // Switcher in nav menu options
154 add_action( 'admin_enqueue_scripts', 'wplng_switcher_nav_menu_inline_scripts' );
155 add_action( 'admin_head-nav-menus.php', 'wp_nav_menu_switcher_box_add_register' );
156 add_action( 'wp_nav_menu_item_custom_fields', 'wp_nav_menu_switcher_box_edit', 10, 2 );
157
158 // Enqueue CSS and JS files for option pages
159 add_action( 'admin_enqueue_scripts', 'wplng_option_page_settings_assets' );
160 add_action( 'admin_enqueue_scripts', 'wplng_option_page_switcher_assets' );
161 add_action( 'admin_enqueue_scripts', 'wplng_option_page_exclusions_assets' );
162 add_action( 'admin_enqueue_scripts', 'wplng_option_page_dictionary_assets' );
163 add_action( 'admin_enqueue_scripts', 'wplng_option_page_link_media_assets' );
164
165 // Update flags URL
166 add_action( 'update_option_wplng_flags_style', 'wplng_options_switcher_update_flags_style', 10, 2 );
167
168 // Reset API data on API key changing
169 add_action( 'update_option_wplng_api_key', 'wplng_on_update_option_wplng_api_key', 10, 2 );
170
171 // Add edit link on page and post list
172 add_filter( 'page_row_actions', 'wplng_row_edit_translation_link', 10, 2 );
173 add_filter( 'post_row_actions', 'wplng_row_edit_translation_link', 10, 2 );
174
175 /**
176 * wplng_translation : CPT, taxo, meta
177 */
178
179 // Register wplng_translation CPT
180 add_action( 'init', 'wplng_register_post_type_translation' );
181
182 // Add metabox for wplng_translation
183 add_action( 'add_meta_boxes_wplng_translation', 'wplng_translation_add_meta_box' );
184
185 // Save metabox on posts saving
186 add_action( 'save_post_wplng_translation', 'wplng_translation_save_meta_boxes_data', 10, 2 );
187
188 // Clear translation cache on trash / untrash
189 add_action( 'trashed_post', 'wplng_clear_translations_cache_trash_untrash' );
190 add_action( 'untrash_post', 'wplng_clear_translations_cache_trash_untrash' );
191 add_action( 'delete_post', 'wplng_clear_translations_cache_trash_untrash' );
192
193 // Enqueue Script for wplng_translation admin: Edit
194 add_action( 'admin_print_scripts-post.php', 'wplng_translation_edit_assets' );
195
196 // Enqueue Script for wplng_translation admin: List
197 add_action( 'admin_print_scripts-edit.php', 'wplng_translation_list_assets' );
198
199 // Only show translations for the website languages
200 add_action( 'pre_get_posts', 'wplng_filter_wplng_translation_posts' );
201
202 // Remove Quick edit from translations list
203 add_filter( 'post_row_actions', 'wplng_translation_remove_quick_edit', 10, 2 );
204
205 // Ajax function for regenerate translation on edit page
206 add_action( 'wp_ajax_wplng_ajax_translation', 'wplng_ajax_generate_translation' );
207
208 // Ajax function for edit modal: Get HTML modal
209 add_action( 'wp_ajax_wplng_ajax_edit_modal', 'wplng_ajax_edit_modal' );
210
211 // Ajax function for edit modal: Save modal
212 add_action( 'wp_ajax_wplng_ajax_save_modal', 'wplng_ajax_save_modal' );
213
214 // Ajax function for edit modal: HeartBeat
215 add_action( 'wp_ajax_wplng_ajax_heartbeat', 'wplng_ajax_heartbeat' );
216 add_action( 'wp_ajax_nopriv_wplng_ajax_heartbeat', 'wplng_ajax_heartbeat' );
217
218 // Display 100 translation in admin area by default
219 add_filter( 'get_user_option_edit_wplng_translation_per_page', 'wplng_translation_per_page' );
220
221 // Filter translations by status
222 add_action( 'restrict_manage_posts', 'wplng_restrict_manage_posts_translation_status' );
223 add_filter( 'parse_query', 'wplng_posts_filter_translation_status' );
224
225 // Translation status on website translations list
226 add_filter( 'post_class', 'wplng_post_class_translation_status', 10, 3 );
227 add_filter( 'post_row_actions', 'wplng_post_row_actions_translation_status', 10, 2 );
228 add_filter( 'manage_wplng_translation_posts_columns', 'wplng_translation_status_columns' );
229 add_action( 'manage_wplng_translation_posts_custom_column', 'wplng_translation_status_item', 10, 2 );
230
231 /**
232 * wplng_slug : CPT, taxo, meta
233 */
234
235 // Register wplng_translation CPT
236 add_action( 'init', 'wplng_register_post_type_slug' );
237
238 // Add metabox for wplng_slug
239 add_action( 'add_meta_boxes_wplng_slug', 'wplng_slug_add_meta_box' );
240
241 // Save metabox on posts saving
242 add_action( 'save_post_wplng_slug', 'wplng_slug_save_meta_boxes_data', 10, 2 );
243
244 // Clear slugs cache on trash / untrash
245 add_action( 'trashed_post', 'wplng_clear_slugs_cache_trash_untrash' );
246 add_action( 'untrash_post', 'wplng_clear_slugs_cache_trash_untrash' );
247 add_action( 'delete_post', 'wplng_clear_slugs_cache_trash_untrash' );
248
249 // Enqueue Script for wplng_slug admin: Edit
250 add_action( 'admin_print_scripts-post.php', 'wplng_slug_edit_assets' );
251
252 // Enqueue Script for wplng_slug admin: List
253 add_action( 'admin_print_scripts-edit.php', 'wplng_slug_list_assets' );
254
255 // Only show slugs for the website languages
256 add_action( 'pre_get_posts', 'wplng_filter_wplng_slug_posts' );
257
258 // Remove Quick edit from slugs list
259 add_filter( 'post_row_actions', 'wplng_slug_remove_quick_edit', 10, 2 );
260
261 // Ajax function for regenerate slug on edit page
262 add_action( 'wp_ajax_wplng_ajax_slug', 'wplng_ajax_generate_slug' );
263
264 // Display 100 translation in admin area by default
265 add_filter( 'get_user_option_edit_wplng_slug_per_page', 'wplng_slug_per_page' );
266
267 // Filter slugs by status
268 add_action( 'restrict_manage_posts', 'wplng_restrict_manage_posts_slug_status' );
269 add_filter( 'parse_query', 'wplng_posts_filter_slug_status' );
270
271 // Translation status on website slugs list
272 add_filter( 'post_class', 'wplng_post_class_slug_status', 10, 3 );
273 add_filter( 'post_row_actions', 'wplng_post_row_actions_slug_status', 10, 2 );
274 add_filter( 'manage_wplng_slug_posts_columns', 'wplng_slug_status_columns' );
275 add_action( 'manage_wplng_slug_posts_custom_column', 'wplng_slug_status_item', 10, 2 );
276
277 /**
278 * Front
279 */
280
281 // Enqueue CSS and JS files
282 add_action( 'wp_enqueue_scripts', 'wplng_register_assets' );
283
284 // Script JS in page
285 add_action( 'wp_footer', 'wplng_on_page_script' );
286
287 // Add languages switcher before </body>
288 add_action( 'wp_footer', 'wplng_switcher_wp_footer' );
289
290 // Add languages switcher in nav menu
291 add_filter( 'wp_nav_menu_objects', 'wplng_switcher_nav_menu_replace_items' );
292 add_filter( 'nav_menu_link_attributes', 'wplng_add_nav_menu_link_attributes_atts', 10, 2 );
293
294 // Set alternate links with hreflang parametters
295 add_action( 'wp_head', 'wplng_link_alternate_hreflang', 1 );
296
297 // Disable web browser automatic translation
298 add_action( 'language_attributes', 'wplng_disable_web_browser_auto_translate' );
299
300 /**
301 * OB and REQUEST_URI
302 */
303
304 // Manage URL with REQUEST_URI and start OB
305 add_action( 'init', 'wplng_ob_start', 1 );
306
307 // Redirect page if is called with an untranslate slug
308 add_action( 'template_redirect', 'wplng_redirect_translated_slug' );
309
310 /**
311 * Features
312 */
313
314 // Search from translated languages
315 if ( ! empty( get_option( 'wplng_translate_search' ) )
316 && wplng_api_feature_is_allow( 'search' )
317 ) {
318 add_action( 'parse_query', 'wplng_translate_search_query' );
319 } else {
320 add_filter( 'wplng_url_is_translatable', 'wplng_exclude_search', 20 );
321 }
322
323 // Redirect by browser language
324 $browser_language_redirect = get_option( 'wplng_browser_language_redirect' );
325 if ( $browser_language_redirect === 'js_only' ) {
326 add_action( 'wp_head', 'wplng_browser_language_redirect_js_only', 2 );
327 } elseif ( $browser_language_redirect === 'php_js' ) {
328 add_action( 'template_redirect', 'wplng_browser_language_redirect_php_js' );
329 }
330
331 /**
332 * Shortcode
333 */
334
335 add_shortcode( 'wplng_switcher', 'wplng_shortcode_switcher' );
336 add_shortcode( 'wplng_notranslate', 'wplng_shortcode_notranslate' );
337 add_shortcode( 'wplng_only', 'wplng_shortcode_only' );
338
339 /**
340 * Gutenberg
341 */
342
343 add_filter( 'block_categories_all', 'wplng_block_category' );
344 add_action( 'init', 'wplng_register_block' );
345 add_action( 'enqueue_block_editor_assets', 'wplng_register_block_assets' );
346
347 }
348 }
349 wplng_start();
350