| 1 |
<?php |
| 2 |
|
| 3 |
namespace Linguise\WordPress; |
| 4 |
|
| 5 |
use Linguise\WordPress\FragmentBase; |
| 6 |
use Linguise\WordPress\FragmentHandler; |
| 7 |
use Linguise\WordPress\HTMLHelper; |
| 8 |
use Linguise\Vendor\JsonPath\JsonObject; |
| 9 |
use Linguise\Vendor\Linguise\Script\Core\Debug; |
| 10 |
|
| 11 |
defined('ABSPATH') || die(''); |
| 12 |
|
| 13 |
/** |
| 14 |
* Fragment override mode handler. |
| 15 |
*/ |
| 16 |
class FragmentOverrideHandler extends FragmentBase |
| 17 |
{ |
| 18 |
/** |
| 19 |
* Try to match the script with the override list. |
| 20 |
* |
| 21 |
* @param \DOMNode|\DOMElement $script The script to be matched |
| 22 |
* @param string $html_data The raw HTML data to be checked |
| 23 |
* |
| 24 |
* @return array|null |
| 25 |
*/ |
| 26 |
public static function tryMatchWithOverride($script, $html_data) |
| 27 |
{ |
| 28 |
$script_content = $script->textContent; |
| 29 |
$script_id = $script->getAttribute('id'); |
| 30 |
|
| 31 |
$override_list = self::getJSONOverrideMatcher($html_data); |
| 32 |
|
| 33 |
$multi_matched = []; |
| 34 |
foreach ($override_list as $override_item) { |
| 35 |
$script_content = HTMLHelper::unclobberCdataInternal($script_content); |
| 36 |
if (isset($override_item['mode']) && $override_item['mode'] === 'app_json') { |
| 37 |
// If mode is app_json and key is the same |
| 38 |
if (isset($override_item['key']) && $override_item['key'] === $script_id) { |
| 39 |
$json_data = json_decode($script_content, true); |
| 40 |
$collected_temp = FragmentHandler::collectFragmentFromJson($json_data); |
| 41 |
if (!empty($collected_temp)) { |
| 42 |
return [ |
| 43 |
'name' => $override_item['name'], |
| 44 |
'fragments' => $collected_temp, |
| 45 |
]; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
continue; |
| 50 |
} |
| 51 |
|
| 52 |
if (isset($override_item['key']) && $override_item['key'] !== $script_id) { |
| 53 |
// If the key is set and it's not the same, then we skip |
| 54 |
continue; |
| 55 |
} |
| 56 |
|
| 57 |
$match_res = preg_match('/' . $override_item['match'] . '/s', $script_content, $match); |
| 58 |
if ($match_res === false || $match_res === 0) { |
| 59 |
continue; |
| 60 |
} |
| 61 |
|
| 62 |
// since it matches, get the JSON value. |
| 63 |
$match_index = 1; |
| 64 |
if (isset($override_item['position'])) { |
| 65 |
$match_index = $override_item['position']; |
| 66 |
} |
| 67 |
|
| 68 |
$matched = $match[$match_index]; |
| 69 |
|
| 70 |
if (isset($override_item['encode']) && $override_item['encode']) { |
| 71 |
// decode the matched string |
| 72 |
$matched = urldecode($matched); |
| 73 |
} |
| 74 |
|
| 75 |
$json_data = json_decode($matched, true); |
| 76 |
|
| 77 |
// collect the fragment |
| 78 |
$is_strict = false; |
| 79 |
if (isset($override_item['strict']) && $override_item['strict']) { |
| 80 |
$is_strict = true; |
| 81 |
} |
| 82 |
|
| 83 |
$collected_temp = FragmentHandler::collectFragmentFromJson($json_data, $is_strict); |
| 84 |
if (!empty($collected_temp)) { |
| 85 |
$multi_matched[] = [ |
| 86 |
'name' => $override_item['name'], |
| 87 |
'fragments' => $collected_temp, |
| 88 |
]; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
if (empty($multi_matched)) { |
| 93 |
return null; |
| 94 |
} |
| 95 |
|
| 96 |
return $multi_matched; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Apply the translated fragments for the override. |
| 101 |
* |
| 102 |
* @param string $html_data The HTML data to be injected |
| 103 |
* @param string $fragment_name The name of the fragment, e.g. 'woocommerce' |
| 104 |
* @param string $fragment_param The param of the fragment, e.g. 'woocommerce$$attr-1' |
| 105 |
* @param array $fragment_info The array of fragments to be injected, from intoJSONFragments |
| 106 |
* |
| 107 |
* @return string |
| 108 |
*/ |
| 109 |
public static function applyTranslatedFragmentsForOverride($html_data, $fragment_name, $fragment_param, $fragment_info) |
| 110 |
{ |
| 111 |
$fragment_matcher = self::getJSONOverrideMatcher($html_data); |
| 112 |
|
| 113 |
// Find the one that match $fragment_name |
| 114 |
$matched_fragment = null; |
| 115 |
foreach ($fragment_matcher as $fragment_match) { |
| 116 |
if ($fragment_match['name'] === $fragment_name) { |
| 117 |
$matched_fragment = $fragment_match; |
| 118 |
break; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
if (is_null($matched_fragment)) { |
| 123 |
return $html_data; |
| 124 |
} |
| 125 |
|
| 126 |
if (isset($matched_fragment['mode']) && $matched_fragment['mode'] === 'app_json') { |
| 127 |
if (!isset($matched_fragment['key'])) { |
| 128 |
return $html_data; |
| 129 |
} |
| 130 |
|
| 131 |
$match_res = preg_match('/<script.*? id=["\']' . $matched_fragment['key'] . '["\'].+?>{(.*)}<\/script>/s', $html_data, $html_matches); |
| 132 |
|
| 133 |
if ($match_res === false || $match_res === 0) { |
| 134 |
return $html_data; |
| 135 |
} |
| 136 |
|
| 137 |
$match_data = $html_matches[1]; |
| 138 |
$json_data = new JsonObject(json_decode('{' . $match_data . '}', true)); |
| 139 |
foreach ($fragment_info['fragments'] as $fragment) { |
| 140 |
if (isset($fragment['skip']) && $fragment['skip']) { |
| 141 |
// If skip is true, then don't replace this fragment (but remove it) |
| 142 |
continue; |
| 143 |
} |
| 144 |
$decoded_key = self::unwrapKey($fragment['key']); |
| 145 |
$merged_key = strpos($decoded_key, '[') === 0 ? '$' . $decoded_key : '$.' . $decoded_key; |
| 146 |
try { |
| 147 |
$json_data->set($merged_key, $fragment['value']); |
| 148 |
} catch (\Linguise\Vendor\JsonPath\InvalidJsonPathException $e) { |
| 149 |
Debug::log('Failed to set key in override: ' . $merged_key . ' -> ' . $e->getMessage()); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
$replaced_json = $json_data->getJson(); |
| 154 |
|
| 155 |
$html_data = str_replace('{' . $match_data . '}', $replaced_json, $html_data); |
| 156 |
} else { |
| 157 |
$match_res = preg_match('/' . $matched_fragment['match'] . '/s', $html_data, $html_matches); |
| 158 |
if ($match_res === false || $match_res === 0) { |
| 159 |
return $html_data; |
| 160 |
} |
| 161 |
|
| 162 |
$index_match = 1; |
| 163 |
if (isset($matched_fragment['position'])) { |
| 164 |
$index_match = $matched_fragment['position']; |
| 165 |
} |
| 166 |
|
| 167 |
$before_match = $html_matches[$index_match]; |
| 168 |
$should_encode = isset($matched_fragment['encode']) && $matched_fragment['encode']; |
| 169 |
if ($should_encode) { |
| 170 |
$before_match = urldecode($before_match); |
| 171 |
} |
| 172 |
|
| 173 |
$json_data = new JsonObject(json_decode($before_match, true)); |
| 174 |
foreach ($fragment_info['fragments'] as $fragment) { |
| 175 |
if (isset($fragment['skip']) && $fragment['skip']) { |
| 176 |
// If skip is true, then don't replace this fragment (but remove it) |
| 177 |
continue; |
| 178 |
} |
| 179 |
$decoded_key = self::unwrapKey($fragment['key']); |
| 180 |
$merged_key = strpos($decoded_key, '[') === 0 ? '$' . $decoded_key : '$.' . $decoded_key; |
| 181 |
try { |
| 182 |
$json_data->set($merged_key, $fragment['value']); |
| 183 |
} catch (\Linguise\Vendor\JsonPath\InvalidJsonPathException $e) { |
| 184 |
Debug::log('Failed to set key in override: ' . $merged_key . ' -> ' . $e->getMessage()); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
$replaced_json = $json_data->getJson(); |
| 189 |
|
| 190 |
$replaced_json = apply_filters('linguise_after_apply_translated_fragments_override', $replaced_json, $fragment_name); |
| 191 |
|
| 192 |
if ($should_encode) { |
| 193 |
$replaced_json = rawurlencode($replaced_json); |
| 194 |
} |
| 195 |
$subst_ptrn = $matched_fragment['replacement']; |
| 196 |
$subst_ptrn = str_replace('$$JSON_DATA$$', $replaced_json, $subst_ptrn); |
| 197 |
|
| 198 |
$replacement = preg_replace('/' . $matched_fragment['match'] . '/', $subst_ptrn, $html_data, 1, $count); |
| 199 |
if ($count) { |
| 200 |
$html_data = $replacement; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
return $html_data; |
| 205 |
} |
| 206 |
} |
| 207 |
|