diff --git a/src/Helpers/JSONHelper.php b/src/Helpers/JSONHelper.php index c235600c..6c8dfafd 100644 --- a/src/Helpers/JSONHelper.php +++ b/src/Helpers/JSONHelper.php @@ -4,14 +4,19 @@ class JSONHelper { - public static function clearStr(array $chars, string $to, string $jsonString): string + public static function clearJson($json): string { - return str_replace($chars, $to, $jsonString); - } + if (empty($json)) { + return ''; + } - public static function clearObj(array $chars, string $to, object $json): string - { - return str_replace($chars, $to, json_encode($json)); + if (is_object($json) || is_array($json)) { + $json = json_encode($json); + } + + $json = str_replace(['\n', '\t'], '', $json); + $json = str_replace(['\"', '"'], '\'', $json); + return str_replace(['\/'], '/', $json); } public static function compareStr(string $jsonString1, string $jsonString2): bool @@ -19,11 +24,6 @@ public static function compareStr(string $jsonString1, string $jsonString2): boo return json_encode(json_decode($jsonString1)) === json_encode(json_decode($jsonString2)); } - public static function compareObj(object $json1, object $json2): bool - { - return json_encode($json1) === json_encode($json2); - } - public static function compareArr(array $json1, array $json2): bool { return json_encode($json1) === json_encode($json2); diff --git a/src/Repositories/H5PRepository.php b/src/Repositories/H5PRepository.php index c342a258..39dcf41e 100644 --- a/src/Repositories/H5PRepository.php +++ b/src/Repositories/H5PRepository.php @@ -1003,9 +1003,9 @@ public function loadContent($id) } $content = $content->toArray(); $content['contentId'] = $content['id']; // : Identifier for the content - $content['parameters'] = JSONHelper::clearStr(['\"', '"'], '\'', $content['parameters']); // : json content as string - $content['params'] = JSONHelper::clearObj(['\"', '"'], '\'', $content['params']); // : json content as string - $content['filtered'] = isset($content['filtered']) ? JSONHelper::clearStr(['\"', '"'], '\'', $content['filtered']) : ''; // : json content as string + $content['parameters'] = JSONHelper::clearJson($content['parameters']); // : json content as string + $content['params'] = JSONHelper::clearJson($content['params']); // : json content as string + $content['filtered'] = JSONHelper::clearJson($content['filtered']); // : json content as string $content['embedType'] = \H5PCore::determineEmbedType($content['embed_type'] ?? 'div', $content['library']['embed_types']); // : csv of embed types //$content ['language'] // : Language code for the content $content['libraryId'] = $content['library_id']; // : Id for the main library diff --git a/src/Services/HeadlessH5PService.php b/src/Services/HeadlessH5PService.php index 64e43d7b..47d739de 100644 --- a/src/Services/HeadlessH5PService.php +++ b/src/Services/HeadlessH5PService.php @@ -3,6 +3,7 @@ namespace EscolaLms\HeadlessH5P\Services; use EscolaLms\HeadlessH5P\Exceptions\H5PException; +use EscolaLms\HeadlessH5P\Helpers\JSONHelper; use EscolaLms\HeadlessH5P\Helpers\MargeFiles; use EscolaLms\HeadlessH5P\Models\H5PLibrary; use EscolaLms\HeadlessH5P\Repositories\Contracts\H5PFrameworkInterface; @@ -427,10 +428,14 @@ public function getContentSettings($id, ?string $token = null): array $uberName = $library['name'] . ' ' . $library['majorVersion'] . '.' . $library['minorVersion']; + $jsonContent = empty($content['filtered']) + ? JSONHelper::clearJson($this->getCore()->filterParameters($content)) + : JSONHelper::clearJson($content['filtered']); + $settings['contents']["cid-$id"] = [ 'library' => $uberName, 'content' => $content, - 'jsonContent' => $content['filtered'], + 'jsonContent' => $jsonContent, 'fullScreen' => $content['library']['fullscreen'], // TODO check all of those endpointis are working fine 'exportUrl' => config('hh5p.h5p_export') && $token ? route('hh5p.content.export', [$content['id'], '_token' => $token]) : '', @@ -504,13 +509,17 @@ public function getSettingsForContent($id): array $uberName = $library['name'] . ' ' . $library['majorVersion'] . '.' . $library['minorVersion']; + $jsonContent = empty($content['filtered']) + ? JSONHelper::clearJson($this->getCore()->filterParameters($content)) + : JSONHelper::clearJson($content['filtered']); + $settings = [ 'library' => $uberName, 'content' => $content, 'jsonContent' => json_encode([ - 'params' => json_decode(str_replace(['\"', '"'], '\'', str_replace(['\n', '\t'], '', $content['filtered']))), + 'params' => json_decode($jsonContent), 'metadata' => $content['metadata'], - ], JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE), + ]), 'fullScreen' => $content['library']['fullscreen'], 'exportUrl' => config('hh5p.h5p_export') ? route('hh5p.content.export', [$content['id']]) : '', //'embedCode' => '', @@ -530,7 +539,6 @@ public function getSettingsForContent($id): array 'nonce' => $content['nonce'], ]; - return $settings; }