| 1 |
<?php |
| 2 |
|
| 3 |
namespace Extendify\QuickEdit\Services; |
| 4 |
|
| 5 |
defined('ABSPATH') || die('No direct access.'); |
| 6 |
|
| 7 |
// Whether the current front-end render shows a non-default-language |
| 8 |
// translation, across TranslatePress / WPML / Polylang. Quick Edit writes to |
| 9 |
// the source post_content, so on a translated render the on-screen text is the |
| 10 |
// translation while the save target is the source — editing text there would |
| 11 |
// overwrite the source with the wrong language. This is detection only; the |
| 12 |
// shape ships to the client as extQuickEditData.translatedContext and the |
| 13 |
// save-side guard lives in the SaveController (see the QE context-guards plan). |
| 14 |
class TranslatedContext |
| 15 |
{ |
| 16 |
// ['isTranslated' => bool, 'plugin' => 'translatepress'|'wpml'|'polylang'|null]. |
| 17 |
// First plugin reporting current-language != default-language wins; 'plugin' |
| 18 |
// is the name to surface in the "manage translations in <plugin>" message. |
| 19 |
public static function detect(): array |
| 20 |
{ |
| 21 |
if (self::translatePressIsTranslated()) { |
| 22 |
return ['isTranslated' => true, 'plugin' => 'translatepress']; |
| 23 |
} |
| 24 |
|
| 25 |
if (self::wpmlIsTranslated()) { |
| 26 |
return ['isTranslated' => true, 'plugin' => 'wpml']; |
| 27 |
} |
| 28 |
|
| 29 |
if (self::polylangIsTranslated()) { |
| 30 |
return ['isTranslated' => true, 'plugin' => 'polylang']; |
| 31 |
} |
| 32 |
|
| 33 |
return ['isTranslated' => false, 'plugin' => null]; |
| 34 |
} |
| 35 |
|
| 36 |
private static function translatePressIsTranslated(): bool |
| 37 |
{ |
| 38 |
$settings = get_option('trp_settings'); |
| 39 |
if (!is_array($settings) || empty($settings['default-language'])) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
// TranslatePress resolves the request's language into this global (a |
| 44 |
// locale like 'es_ES') by the time scripts enqueue; on the default |
| 45 |
// language it equals settings['default-language']. |
| 46 |
$current = isset($GLOBALS['TRP_LANGUAGE']) ? (string) $GLOBALS['TRP_LANGUAGE'] : ''; |
| 47 |
if ($current === '') { |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
return $current !== (string) $settings['default-language']; |
| 52 |
} |
| 53 |
|
| 54 |
private static function wpmlIsTranslated(): bool |
| 55 |
{ |
| 56 |
// WPML's own public API for the active/default language — these are its |
| 57 |
// hooks, not ones we register, so the plugin-prefix rule doesn't apply. |
| 58 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 59 |
$current = apply_filters('wpml_current_language', null); |
| 60 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 61 |
$default = apply_filters('wpml_default_language', null); |
| 62 |
if (!is_string($current) || !is_string($default) || $current === '' || $default === '') { |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
return $current !== $default; |
| 67 |
} |
| 68 |
|
| 69 |
private static function polylangIsTranslated(): bool |
| 70 |
{ |
| 71 |
if (!function_exists('pll_current_language') || !function_exists('pll_default_language')) { |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
$current = pll_current_language('slug'); |
| 76 |
$default = pll_default_language('slug'); |
| 77 |
if (!$current || !$default) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
return $current !== $default; |
| 82 |
} |
| 83 |
} |
| 84 |
|