WebOrbiton
v3.0.0.2

FlatlyPage

49 lines · 1.2 KB
  1. <?php
  2. ​
  3. function get_page_content($page_id, $lang = 'pl')
  4. {
  5. $basePath = __DIR__ . "/../data/{$page_id}.php";
  6. ​
  7. if (!file_exists($basePath)) return [];
  8. ​
  9. $sourceData = include $basePath;
  10. ​
  11. if ($lang === 'pl') {
  12. return $sourceData;
  13. }
  14. ​
  15. $langPath = __DIR__ . "/../data/{$page_id}_{$lang}.php";
  16. ​
  17. if (!file_exists($langPath)) {
  18. save_page_content($page_id, $sourceData, $lang);
  19. return $sourceData;
  20. }
  21. ​
  22. $langData = include $langPath;
  23. ​
  24. return merge_recursive_sync($sourceData, $langData);
  25. }
  26. ​
  27. function merge_recursive_sync($source, $translation)
  28. {
  29. foreach ($source as $key => $value) {
  30. if (is_array($value)) {
  31. $source[$key] = merge_recursive_sync($value, $translation[$key] ?? []);
  32. } else {
  33. if (isset($translation[$key]) && !empty($translation[$key]) && $key !== 'id' && $key !== 'type') {
  34. $source[$key] = $translation[$key];
  35. }
  36. }
  37. }
  38. return $source;
  39. }
  40. ​
  41. function save_page_content($page_id, $data, $lang = 'pl')
  42. {
  43. $suffix = ($lang === 'pl') ? '' : '_' . $lang;
  44. $path = __DIR__ . "/../data/{$page_id}{$suffix}.php";
  45. ​
  46. $content = "<?php\nreturn " . var_export($data, true) . ";";
  47. return file_put_contents($path, $content);
  48. }
  49. ​