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

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