v3.0.0.2
FlatlyPage
- <?php
-
- function get_page_content($page_id, $lang = 'pl')
- {
- $basePath = __DIR__ . "/../data/{$page_id}.php";
-
- if (!file_exists($basePath)) return [];
-
- $sourceData = include $basePath;
-
- if ($lang === 'pl') {
- return $sourceData;
- }
-
- $langPath = __DIR__ . "/../data/{$page_id}_{$lang}.php";
-
- if (!file_exists($langPath)) {
- save_page_content($page_id, $sourceData, $lang);
- return $sourceData;
- }
-
- $langData = include $langPath;
-
- return merge_recursive_sync($sourceData, $langData);
- }
-
- function merge_recursive_sync($source, $translation)
- {
- foreach ($source as $key => $value) {
- if (is_array($value)) {
- $source[$key] = merge_recursive_sync($value, $translation[$key] ?? []);
- } else {
- if (isset($translation[$key]) && !empty($translation[$key]) && $key !== 'id' && $key !== 'type') {
- $source[$key] = $translation[$key];
- }
- }
- }
- return $source;
- }
-
- function save_page_content($page_id, $data, $lang = 'pl')
- {
- $suffix = ($lang === 'pl') ? '' : '_' . $lang;
- $path = __DIR__ . "/../data/{$page_id}{$suffix}.php";
-
- $content = "<?php\nreturn " . var_export($data, true) . ";";
- return file_put_contents($path, $content);
- }
-