Skip to content

Instantly share code, notes, and snippets.

@goranefbl
Created May 19, 2026 22:06
Show Gist options
  • Select an option

  • Save goranefbl/05c68e4e99b0ecee9ed5a79e461ed031 to your computer and use it in GitHub Desktop.

Select an option

Save goranefbl/05c68e4e99b0ecee9ed5a79e461ed031 to your computer and use it in GitHub Desktop.
Translatepress support for Loyalty Widget
<?php
/**
* WPGens Loyalty: hardcoded TranslatePress fallback (ID-based).
*/
// ID-based action translations — id matches the row in the loyalty admin
// (Earning Actions list). Update as needed.
function wpgl_tp_id_map() {
return [
'earning' => [
1 => 'Registration',
2 => 'Place an order',
3 => 'Product review',
4 => 'Add birthday',
],
'redeem' => [
// '1' => 'Whatever',
],
];
}
// Title-based map for points label (still title-keyed; these are short).
function wpgl_tp_title_map() {
return [
'точка' => 'point',
'точки' => 'points',
];
}
function wpgl_tp_should_translate() {
if (!empty($GLOBALS['TRP_LANGUAGE']) && $GLOBALS['TRP_LANGUAGE'] === 'en_GB') return true;
if (defined('REST_REQUEST') && REST_REQUEST) return true; // assume single non-default language
return false;
}
add_filter('wpgens_loyalty_widget_config', function ($config) {
if (!wpgl_tp_should_translate()) return $config;
$id_map = wpgl_tp_id_map();
$title_map = wpgl_tp_title_map();
$translated = [];
foreach (['pointsName', 'pointsNameSingular'] as $k) {
if (!empty($config[$k]) && isset($title_map[$config[$k]])) {
$config[$k] = $title_map[$config[$k]];
$translated[] = $k;
}
}
foreach (['earningActions' => 'earning', 'redeemActions' => 'redeem'] as $listKey => $mapKey) {
if (!empty($config[$listKey]) && is_array($config[$listKey])) {
foreach ($config[$listKey] as $i => $a) {
$id = isset($a['id']) ? (int) $a['id'] : 0;
if ($id && isset($id_map[$mapKey][$id])) {
$config[$listKey][$i]['title'] = $id_map[$mapKey][$id];
$translated[] = $listKey . '[' . $i . '] id=' . $id;
}
}
}
}
$config['_debug'] = [
'filter' => 'wpgens_loyalty_widget_config',
'TRP_LANGUAGE' => $GLOBALS['TRP_LANGUAGE'] ?? null,
'is_rest' => defined('REST_REQUEST') && REST_REQUEST,
'translated' => $translated,
];
return $config;
}, 20);
add_filter('rest_post_dispatch', function ($response, $server, $request) {
if (strpos($request->get_route(), '/wpgens-loyalty/') === false) return $response;
if (!wpgl_tp_should_translate()) return $response;
$data = $response->get_data();
if (!is_array($data)) return $response;
$id_map = wpgl_tp_id_map();
$translated = [];
// Translate pointsProgram.earningActions[*].title and redeemActions[*].title
if (!empty($data['pointsProgram']) && is_array($data['pointsProgram'])) {
foreach (['earningActions' => 'earning', 'redeemActions' => 'redeem'] as $listKey => $mapKey) {
if (!empty($data['pointsProgram'][$listKey]) && is_array($data['pointsProgram'][$listKey])) {
foreach ($data['pointsProgram'][$listKey] as $i => $a) {
$id = isset($a['id']) ? (int) $a['id'] : 0;
if ($id && isset($id_map[$mapKey][$id])) {
$data['pointsProgram'][$listKey][$i]['title'] = $id_map[$mapKey][$id];
$translated[] = 'pointsProgram.' . $listKey . '[' . $i . '] id=' . $id;
}
}
}
}
}
// Same for top-level earningActions/redeemActions (used by /widget/config)
foreach (['earningActions' => 'earning', 'redeemActions' => 'redeem'] as $listKey => $mapKey) {
if (!empty($data[$listKey]) && is_array($data[$listKey])) {
foreach ($data[$listKey] as $i => $a) {
$id = isset($a['id']) ? (int) $a['id'] : 0;
if ($id && isset($id_map[$mapKey][$id])) {
$data[$listKey][$i]['title'] = $id_map[$mapKey][$id];
$translated[] = $listKey . '[' . $i . '] id=' . $id;
}
}
}
}
$data['_debug'] = [
'filter' => 'rest_post_dispatch',
'route' => $request->get_route(),
'TRP_LANGUAGE' => $GLOBALS['TRP_LANGUAGE'] ?? null,
'translated' => $translated,
];
$response->set_data($data);
return $response;
}, 99, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment