multilang.php
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | if(!class_exists('acfe_multilang')): |
| 7 | |
| 8 | class acfe_multilang{ |
| 9 | |
| 10 | var $is_wpml = false; |
| 11 | var $is_polylang = false; |
| 12 | var $is_multilang = false; |
| 13 | var $options_pages = false; |
| 14 | |
| 15 | function __construct(){ |
| 16 | |
| 17 | // WPML |
| 18 | if(defined('ICL_SITEPRESS_VERSION')){ |
| 19 | |
| 20 | $this->is_wpml = true; |
| 21 | $this->is_multilang = true; |
| 22 | |
| 23 | } |
| 24 | |
| 25 | // PolyLang |
| 26 | if(defined('POLYLANG_VERSION') && function_exists('pll_default_language')){ |
| 27 | |
| 28 | $this->is_polylang = true; |
| 29 | $this->is_multilang = true; |
| 30 | |
| 31 | } |
| 32 | |
| 33 | if($this->is_multilang){ |
| 34 | |
| 35 | add_action('acf/init', array($this, 'init'), 99); |
| 36 | |
| 37 | } |
| 38 | |
| 39 | } |
| 40 | |
| 41 | function init(){ |
| 42 | |
| 43 | // Check setting |
| 44 | if(!acf_get_setting('acfe/modules/multilang')) |
| 45 | return; |
| 46 | |
| 47 | // Polylang specific |
| 48 | if($this->is_polylang){ |
| 49 | |
| 50 | // Default/Current Language |
| 51 | $dl = pll_default_language('locale'); |
| 52 | $cl = pll_current_language('locale'); |
| 53 | |
| 54 | // Update settings |
| 55 | acf_update_setting('default_language', $dl); |
| 56 | acf_update_setting('current_language', $cl); |
| 57 | |
| 58 | } |
| 59 | |
| 60 | // ACF Options Post ID |
| 61 | add_filter('acf/validate_post_id', array($this, 'set_options_post_id'), 99, 2); |
| 62 | |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * WPML |
| 67 | * https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/ |
| 68 | */ |
| 69 | function wpml_get_languages($pluck, $type = 'all'){ |
| 70 | |
| 71 | // Pluck |
| 72 | $pluck_filter = $pluck; |
| 73 | |
| 74 | if($pluck === 'locale') |
| 75 | $pluck_filter = 'default_locale'; |
| 76 | |
| 77 | // Vars |
| 78 | $languages = array(); |
| 79 | |
| 80 | switch($type){ |
| 81 | |
| 82 | // Active |
| 83 | case 'active': |
| 84 | |
| 85 | // Active Languages |
| 86 | // https://wpml.org/wpml-hook/wpml_active_languages/ |
| 87 | $languages = apply_filters('wpml_active_languages', null, array('skip_missing' => 0)); |
| 88 | |
| 89 | $languages = wp_list_pluck($languages, $pluck_filter, true); |
| 90 | |
| 91 | return $languages; |
| 92 | |
| 93 | // All |
| 94 | case 'all': |
| 95 | |
| 96 | // Active Languages |
| 97 | // https://wpml.org/wpml-hook/wpml_active_languages/ |
| 98 | $languages = apply_filters('wpml_active_languages', null, array('skip_missing' => 0)); |
| 99 | |
| 100 | $languages = wp_list_pluck($languages, $pluck_filter, true); |
| 101 | |
| 102 | // Plugin Languages |
| 103 | $plugin_languages = icl_get_languages_locales(); |
| 104 | |
| 105 | if(!empty($plugin_languages)){ |
| 106 | |
| 107 | if($pluck === 'code'){ |
| 108 | |
| 109 | $plugin_languages = array_keys($plugin_languages); |
| 110 | |
| 111 | }elseif($pluck === 'locale'){ |
| 112 | |
| 113 | $plugin_languages = array_values($plugin_languages); |
| 114 | |
| 115 | } |
| 116 | |
| 117 | // Merge |
| 118 | $languages = array_merge($languages, $plugin_languages); |
| 119 | $languages = array_unique($languages); |
| 120 | |
| 121 | } |
| 122 | |
| 123 | return $languages; |
| 124 | |
| 125 | } |
| 126 | |
| 127 | return $languages; |
| 128 | |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * PolyLang |
| 133 | * https://polylang.pro/doc/filter-reference/ |
| 134 | * https://polylang.pro/doc/developpers-how-to/ |
| 135 | * https://polylang.wordpress.com/documentation/documentation-for-developers/general/ |
| 136 | * https://polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/ |
| 137 | */ |
| 138 | function polylang_get_languages($pluck, $type = 'all'){ |
| 139 | |
| 140 | // Vars |
| 141 | $languages = array(); |
| 142 | |
| 143 | switch($type){ |
| 144 | |
| 145 | // Active |
| 146 | case 'active': |
| 147 | |
| 148 | $pluck_filter = $pluck; |
| 149 | if($pluck === 'code') |
| 150 | $pluck_filter = 'slug'; |
| 151 | |
| 152 | // https://polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/ |
| 153 | $languages = pll_languages_list(array( |
| 154 | 'hide_empty' => false, |
| 155 | 'fields' => $pluck_filter |
| 156 | )); |
| 157 | |
| 158 | return $languages; |
| 159 | |
| 160 | // All |
| 161 | case 'all': |
| 162 | |
| 163 | // Copy from wp-content/plugins/polylang-pro/settings/settings.php:363 |
| 164 | require_once ABSPATH . 'wp-admin/includes/translation-install.php'; |
| 165 | |
| 166 | $languages = include PLL_SETTINGS_INC . '/languages.php'; |
| 167 | $translations = wp_get_available_translations(); |
| 168 | |
| 169 | if (!empty($translations)){ |
| 170 | |
| 171 | $translations['en_US'] = ''; |
| 172 | $languages = array_intersect_key($languages, $translations); |
| 173 | |
| 174 | } |
| 175 | |
| 176 | $languages = apply_filters('pll_predefined_languages', $languages); |
| 177 | |
| 178 | foreach($languages as $k => $lang){ |
| 179 | |
| 180 | if(isset($lang['code'], $lang['locale'], $lang['name'], $lang['dir'], $lang['flag'])) |
| 181 | continue; |
| 182 | |
| 183 | unset($languages[$k]); |
| 184 | |
| 185 | } |
| 186 | |
| 187 | $languages = wp_list_pluck($languages, $pluck, true); |
| 188 | |
| 189 | return $languages; |
| 190 | |
| 191 | } |
| 192 | |
| 193 | return $languages; |
| 194 | |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * ACF Options Post ID |
| 199 | */ |
| 200 | function set_options_post_id($post_id, $original_post_id){ |
| 201 | |
| 202 | // Bail early if original post id is 'options' ||'option' |
| 203 | if(!is_string($post_id) || in_array($original_post_id, array('options', 'option'))) |
| 204 | return $post_id; |
| 205 | |
| 206 | $data = acf_get_post_id_info($post_id); |
| 207 | |
| 208 | // Bail early if post id isn't an option type |
| 209 | if($data['type'] !== 'option') |
| 210 | return $post_id; |
| 211 | |
| 212 | // Bail early if no Options Page found with that post id |
| 213 | if(!$this->is_options_page($post_id)) |
| 214 | return $post_id; |
| 215 | |
| 216 | // Bail early if already localized: 'my-options_en_US' |
| 217 | if($this->is_localized($post_id)) |
| 218 | return $post_id; |
| 219 | |
| 220 | // Append current language to post id |
| 221 | $dl = acf_get_setting('default_language'); |
| 222 | $cl = acf_get_setting('current_language'); |
| 223 | |
| 224 | // Add Language |
| 225 | if($cl && $cl !== $dl){ |
| 226 | |
| 227 | $post_id .= '_' . $cl; |
| 228 | |
| 229 | } |
| 230 | |
| 231 | return $post_id; |
| 232 | |
| 233 | } |
| 234 | |
| 235 | function is_localized($post_id){ |
| 236 | |
| 237 | // Check if post id ends with '-en_US' || '_en_US' || '-en' || '_en' |
| 238 | // https://regex101.com/r/oMsyeL/4 |
| 239 | preg_match('/(?P<locale>[_\-][A-Za-z]{2}_[A-Za-z]{2})$|(?P<code>[_\-][A-Za-z]{2})$/', $post_id, $matches); |
| 240 | |
| 241 | if(empty($matches)) |
| 242 | return false; |
| 243 | |
| 244 | // Cleanup matches |
| 245 | $lang = array(); |
| 246 | |
| 247 | foreach($matches as $key => $val){ |
| 248 | |
| 249 | if(is_int($key) || empty($val)) |
| 250 | continue; |
| 251 | |
| 252 | $lang = array( |
| 253 | 'type' => $key, |
| 254 | 'lang' => strtolower(substr($val, 1)), // Lowercase + Remove the first '_' |
| 255 | ); |
| 256 | |
| 257 | } |
| 258 | |
| 259 | if(empty($lang)) |
| 260 | return false; |
| 261 | |
| 262 | // Get WPML/Polylang Languages List |
| 263 | $languages = $this->get_languages($lang['type']); |
| 264 | $languages = array_map('strtolower', $languages); |
| 265 | |
| 266 | // Compare Matches vs WPML/Polylang Languages List |
| 267 | return in_array($lang['lang'], $languages); |
| 268 | |
| 269 | } |
| 270 | |
| 271 | function is_options_page($post_id){ |
| 272 | |
| 273 | // Get Options Pages |
| 274 | if($this->options_pages === false){ |
| 275 | |
| 276 | // Get ACF Options Pages |
| 277 | $options_pages = acf_get_array(acf_get_options_pages()); |
| 278 | $list = wp_list_pluck($options_pages, 'post_id', true); |
| 279 | |
| 280 | // Add 'Post Types List' location |
| 281 | $post_types = acf_get_post_types(array( |
| 282 | 'show_ui' => 1, |
| 283 | 'exclude' => array('attachment') |
| 284 | )); |
| 285 | |
| 286 | if(!empty($post_types)){ |
| 287 | |
| 288 | foreach($post_types as $post_type){ |
| 289 | |
| 290 | $list[] = $post_type . '_options'; |
| 291 | |
| 292 | } |
| 293 | |
| 294 | } |
| 295 | |
| 296 | // Add 'Taxonomy List' location |
| 297 | $taxonomies = acf_get_taxonomies(); |
| 298 | |
| 299 | if(!empty($taxonomies)){ |
| 300 | |
| 301 | foreach($taxonomies as $taxonomy){ |
| 302 | |
| 303 | $list[] = 'tax_' . $taxonomy . '_options'; |
| 304 | |
| 305 | } |
| 306 | |
| 307 | } |
| 308 | |
| 309 | $this->options_pages = $list; |
| 310 | |
| 311 | } |
| 312 | |
| 313 | if(is_array($this->options_pages) && !empty($this->options_pages)){ |
| 314 | |
| 315 | return in_array($post_id, $this->options_pages); |
| 316 | |
| 317 | } |
| 318 | |
| 319 | return false; |
| 320 | |
| 321 | } |
| 322 | |
| 323 | function get_languages($pluck = 'code', $type = 'all', $plugin = false){ |
| 324 | |
| 325 | // Polylang |
| 326 | if($this->is_polylang || $plugin === 'polylang'){ |
| 327 | |
| 328 | return $this->polylang_get_languages($pluck, $type); |
| 329 | |
| 330 | // WPML |
| 331 | }elseif($this->is_wpml || $plugin === 'wpml'){ |
| 332 | |
| 333 | return $this->wpml_get_languages($pluck, $type); |
| 334 | |
| 335 | } |
| 336 | |
| 337 | return array(); |
| 338 | |
| 339 | } |
| 340 | |
| 341 | } |
| 342 | |
| 343 | acf_new_instance('acfe_multilang'); |
| 344 | |
| 345 | endif; |
| 346 | |
| 347 | function acfe_is_multilang(){ |
| 348 | |
| 349 | return acf_get_instance('acfe_multilang')->is_multilang; |
| 350 | |
| 351 | } |
| 352 | |
| 353 | function acfe_get_multilang(){ |
| 354 | |
| 355 | $wpml = acf_get_instance('acfe_multilang')->is_wpml; |
| 356 | $polylang = acf_get_instance('acfe_multilang')->is_polylang; |
| 357 | |
| 358 | $data = array( |
| 359 | 'dl' => acf_get_setting('default_language'), |
| 360 | 'cl' => acf_get_setting('current_language'), |
| 361 | 'wpml' => $wpml, |
| 362 | 'polylang' => $polylang, |
| 363 | ); |
| 364 | |
| 365 | return $data; |
| 366 | |
| 367 | } |
| 368 | |
| 369 | function acfe_get_languages($pluck = 'code', $type = 'all', $plugin = false){ |
| 370 | |
| 371 | return acf_get_instance('acfe_multilang')->get_languages($pluck, $type, $plugin); |
| 372 | |
| 373 | } |
| 374 | |
| 375 | function acfe_is_polylang(){ |
| 376 | |
| 377 | return acf_get_instance('acfe_multilang')->is_polylang; |
| 378 | |
| 379 | } |
| 380 | |
| 381 | function acfe_is_wpml(){ |
| 382 | |
| 383 | return acf_get_instance('acfe_multilang')->is_wpml; |
| 384 | |
| 385 | } |
| 386 | |
| 387 | function acfe_get_post_lang($post_id, $field = false){ |
| 388 | |
| 389 | // Polylang |
| 390 | if(acfe_is_polylang()){ |
| 391 | |
| 392 | // Default field |
| 393 | if(!$field) |
| 394 | $field = 'locale'; |
| 395 | |
| 396 | return pll_get_post_language($post_id, $field); |
| 397 | |
| 398 | // WPML |
| 399 | }elseif(acfe_is_wpml()){ |
| 400 | |
| 401 | $post_lang = apply_filters('wpml_post_language_details', NULL, $post_id); |
| 402 | |
| 403 | // Default field |
| 404 | if(!$field) |
| 405 | $field = 'slug'; |
| 406 | |
| 407 | if($field === 'locale'){ |
| 408 | |
| 409 | return $post_lang['locale']; |
| 410 | |
| 411 | }elseif($field === 'slug'){ |
| 412 | |
| 413 | return $post_lang['language_code']; |
| 414 | |
| 415 | }elseif($field === 'name'){ |
| 416 | |
| 417 | return $post_lang['display_name']; |
| 418 | |
| 419 | } |
| 420 | |
| 421 | return false; |
| 422 | |
| 423 | } |
| 424 | |
| 425 | return false; |
| 426 | |
| 427 | } |
| 428 | |
| 429 | function acfe__(&$string, $name = false, $textdomain = 'acfe'){ |
| 430 | |
| 431 | if(!acfe_is_multilang() || empty($string)) |
| 432 | return __($string, $textdomain); |
| 433 | |
| 434 | if(empty($name)) |
| 435 | $name = $string; |
| 436 | |
| 437 | // WPML |
| 438 | if(acfe_is_wpml()){ |
| 439 | |
| 440 | do_action( 'wpml_register_single_string', $textdomain, $name, $string); |
| 441 | |
| 442 | $string = __($string, $textdomain); |
| 443 | |
| 444 | return $string; |
| 445 | |
| 446 | } |
| 447 | |
| 448 | // PolyLang |
| 449 | if(acfe_is_polylang()){ |
| 450 | |
| 451 | pll_register_string($name, $string, $textdomain); |
| 452 | |
| 453 | $string = pll__($string); |
| 454 | |
| 455 | return $string; |
| 456 | |
| 457 | } |
| 458 | |
| 459 | $string = __($string, $textdomain); |
| 460 | |
| 461 | return $string; |
| 462 | |
| 463 | } |
| 464 | |
| 465 | function acfe__e($string, $name = false, $textdomain = 'acfe'){ |
| 466 | |
| 467 | echo acfe__($string, $name, $textdomain); |
| 468 | |
| 469 | } |