| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Util\Translation\Provider; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Util\Translation\Support\AbstractStringRegistrationProvider; |
| 6 |
use BitCode\BitForm\Core\Util\Translation\TranslationManager; |
| 7 |
use BitCode\BitForm\Core\Util\Translation\TranslationStringName; |
| 8 |
|
| 9 |
/** |
| 10 |
* Polylang adapter; the free version suffices. |
| 11 |
* |
| 12 |
* pll_register_string() keeps its registry in memory per request, so strings |
| 13 |
* appear in Languages → Translations only if registered during that request. |
| 14 |
* Registration is gated to page=mlang_strings (render and save postback) and |
| 15 |
* stays off every other request. Frontend/AJAX translate by source lookup. |
| 16 |
*/ |
| 17 |
final class PolylangProvider extends AbstractStringRegistrationProvider |
| 18 |
{ |
| 19 |
public const GROUP = 'Bit Form'; |
| 20 |
|
| 21 |
public function registerStrings($formId, array $contextStrings) |
| 22 |
{ |
| 23 |
if (!function_exists('pll_register_string')) { |
| 24 |
return; |
| 25 |
} |
| 26 |
foreach ($contextStrings as $context => $string) { |
| 27 |
$context = (string) $context; |
| 28 |
// Multiline editor for HTML bodies: messages, mail body, content props. |
| 29 |
$multiline = (bool) preg_match('/^(?:msg-content-|mail-body-)|(?:^|-)content(?:-|$)/', $context); |
| 30 |
pll_register_string(TranslationStringName::forForm($context, $formId), $string, self::GROUP, $multiline); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
public function onAdminInit() |
| 35 |
{ |
| 36 |
add_action('admin_init', [$this, 'maybeRegisterAllForms']); |
| 37 |
} |
| 38 |
|
| 39 |
public function onAjaxInit($requestLang) |
| 40 |
{ |
| 41 |
$slug = $this->getCurrentLanguage(); |
| 42 |
if ('' === $slug || !function_exists('PLL')) { |
| 43 |
return; |
| 44 |
} |
| 45 |
// PLL() is $GLOBALS['polylang'], null until Polylang bootstraps. Blind |
| 46 |
// dereference fatals the public submit endpoint. |
| 47 |
$pll = PLL(); |
| 48 |
if (!is_object($pll) || !isset($pll->model) || !is_object($pll->model) || !method_exists($pll->model, 'get_language')) { |
| 49 |
return; |
| 50 |
} |
| 51 |
$language = $pll->model->get_language($slug); |
| 52 |
if (!is_object($language) || empty($language->locale)) { |
| 53 |
return; |
| 54 |
} |
| 55 |
// Adopt the submitter's locale so gettext defaults localize too. |
| 56 |
if (switch_to_locale($language->locale)) { |
| 57 |
add_action('shutdown', [__CLASS__, 'restoreLocale'], 1); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* `shutdown` callback — wrapper so the action returns nothing. |
| 63 |
*/ |
| 64 |
public static function restoreLocale() |
| 65 |
{ |
| 66 |
restore_previous_locale(); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Polylang's registry is per-request memory, so every form re-registers on |
| 71 |
* every screen load — the base default is right and nothing is overridden. |
| 72 |
* |
| 73 |
* @param string $page |
| 74 |
* |
| 75 |
* @return bool |
| 76 |
*/ |
| 77 |
protected function isTranslationScreen($page) |
| 78 |
{ |
| 79 |
return 'mlang_strings' === $page; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Validated bf_lang → Polylang's documented `lang` var → pll_current_language. |
| 84 |
* |
| 85 |
* @return string |
| 86 |
*/ |
| 87 |
protected function detectLanguage() |
| 88 |
{ |
| 89 |
$candidates = [TranslationManager::getRequestLang()]; |
| 90 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Polylang's documented AJAX language var, read-only. |
| 91 |
$candidates[] = TranslationManager::sanitizeLang(isset($_REQUEST['lang']) ? wp_unslash($_REQUEST['lang']) : ''); |
| 92 |
|
| 93 |
if (function_exists('pll_languages_list')) { |
| 94 |
$known = pll_languages_list(['fields' => 'slug']); |
| 95 |
if (is_array($known)) { |
| 96 |
foreach ($candidates as $candidate) { |
| 97 |
if ('' !== $candidate && in_array($candidate, $known, true)) { |
| 98 |
return $candidate; |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
if (function_exists('pll_current_language')) { |
| 104 |
$current = pll_current_language('slug'); |
| 105 |
return is_string($current) ? $current : ''; |
| 106 |
} |
| 107 |
return ''; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* @param string $string |
| 112 |
* @param string $context |
| 113 |
* @param int $formId |
| 114 |
* |
| 115 |
* @return mixed |
| 116 |
*/ |
| 117 |
protected function lookup($string, $context, $formId) |
| 118 |
{ |
| 119 |
if (!function_exists('pll_translate_string')) { |
| 120 |
return $string; |
| 121 |
} |
| 122 |
$lang = $this->getCurrentLanguage(); |
| 123 |
return '' === $lang ? $string : pll_translate_string($string, $lang); |
| 124 |
} |
| 125 |
} |
| 126 |
|