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

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