| 1 |
<?php |
| 2 |
|
| 3 |
namespace Linguise\WordPress; |
| 4 |
|
| 5 |
use Linguise\WordPress\FragmentBase; |
| 6 |
use Linguise\Vendor\JsonPath\JsonObject; |
| 7 |
use Linguise\Vendor\Linguise\Script\Core\Debug; |
| 8 |
|
| 9 |
defined('ABSPATH') || die(''); |
| 10 |
|
| 11 |
/** |
| 12 |
* Fragment auto mode handler. |
| 13 |
*/ |
| 14 |
class FragmentAutoHandler extends FragmentBase |
| 15 |
{ |
| 16 |
/** |
| 17 |
* Apply the translated fragments for the auto mode. |
| 18 |
* |
| 19 |
* @param array $json_data The JSON data to be injected |
| 20 |
* @param array $fragments The array of fragments to be injected, from intoJSONFragments |
| 21 |
* |
| 22 |
* @return array |
| 23 |
*/ |
| 24 |
public static function applyTranslatedFragmentsForAuto($json_data, $fragments) |
| 25 |
{ |
| 26 |
$json_path = new JsonObject($json_data); |
| 27 |
// Warn if $json_data is empty |
| 28 |
if (empty($json_path->getValue())) { |
| 29 |
return false; |
| 30 |
} |
| 31 |
|
| 32 |
foreach ($fragments as $fragment) { |
| 33 |
if (isset($fragment['skip']) && $fragment['skip']) { |
| 34 |
// If skip is true, then don't replace this fragment (but remove it) |
| 35 |
continue; |
| 36 |
} |
| 37 |
// remove the html fragment from the translated page |
| 38 |
$decoded_key = self::unwrapKey($fragment['key']); |
| 39 |
try { |
| 40 |
$json_path->set('$.' . $decoded_key, $fragment['value']); |
| 41 |
} catch (\Linguise\Vendor\JsonPath\InvalidJsonPathException $e) { |
| 42 |
Debug::log('Failed to set key in auto: ' . $decoded_key . ' -> ' . $e->getMessage()); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
$replaced_json = $json_path->getValue(); |
| 47 |
return $replaced_json; |
| 48 |
} |
| 49 |
} |
| 50 |
|