| 1 |
<?php |
| 2 |
|
| 3 |
namespace CryptX\Admin; |
| 4 |
|
| 5 |
use CryptX\Config; |
| 6 |
|
| 7 |
class ChangelogSettingsTab |
| 8 |
{ |
| 9 |
/** |
| 10 |
* @var Config Configuration instance |
| 11 |
*/ |
| 12 |
private Config $config; |
| 13 |
|
| 14 |
/** |
| 15 |
* Template path |
| 16 |
*/ |
| 17 |
private const TEMPLATE_PATH = CRYPTX_DIR_PATH . 'templates/admin/tabs/changelog.php'; |
| 18 |
|
| 19 |
/** |
| 20 |
* ChangelogSettingsTab constructor. |
| 21 |
* |
| 22 |
* @param Config $config Configuration instance |
| 23 |
*/ |
| 24 |
public function __construct(Config $config) |
| 25 |
{ |
| 26 |
$this->config = $config; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Render the changelog tab content |
| 31 |
*/ |
| 32 |
public function render(): void |
| 33 |
{ |
| 34 |
if ('changelog' !== $this->getActiveTab()) { |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
$changelogs = $this->getChangelogData(); |
| 39 |
$this->renderTemplate(self::TEMPLATE_PATH, ['changelogs' => $changelogs]); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Get changelog data |
| 44 |
*/ |
| 45 |
private function getChangelogData(): array |
| 46 |
{ |
| 47 |
$readmePath = CRYPTX_DIR_PATH . '/readme.txt'; |
| 48 |
if (!file_exists($readmePath)) { |
| 49 |
return []; |
| 50 |
} |
| 51 |
|
| 52 |
$fileContents = file_get_contents($readmePath); |
| 53 |
if ($fileContents === false) { |
| 54 |
return []; |
| 55 |
} |
| 56 |
|
| 57 |
return $this->parseChangelog($fileContents); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Render template with data |
| 62 |
*/ |
| 63 |
private function renderTemplate(string $path, array $data): void |
| 64 |
{ |
| 65 |
if (!file_exists($path)) { |
| 66 |
throw new \RuntimeException(sprintf('Template file not found: %s', $path)); |
| 67 |
} |
| 68 |
|
| 69 |
extract($data); |
| 70 |
include $path; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get active tab |
| 75 |
*/ |
| 76 |
private function getActiveTab(): string |
| 77 |
{ |
| 78 |
return sanitize_text_field($_GET['tab'] ?? 'general'); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Parse changelog content from readme.txt |
| 83 |
* |
| 84 |
* @param string $content |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
private function parseChangelog(string $content): array |
| 88 |
{ |
| 89 |
$content = str_replace(["\r\n", "\r"], "\n", $content); |
| 90 |
$content = trim($content); |
| 91 |
|
| 92 |
// Split into sections |
| 93 |
$sections = $this->parseSections($content); |
| 94 |
if (!isset($sections['changelog'])) { |
| 95 |
return []; |
| 96 |
} |
| 97 |
|
| 98 |
// Parse changelog entries |
| 99 |
return $this->parseChangelogEntries($sections['changelog']['content']); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Parse sections from readme content |
| 104 |
* |
| 105 |
* @param string $content |
| 106 |
* @return array |
| 107 |
*/ |
| 108 |
private function parseSections(string $content): array |
| 109 |
{ |
| 110 |
$_sections = preg_split('/^[\s]*==[\s]*(.+?)[\s]*==/m', $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); |
| 111 |
$sections = []; |
| 112 |
|
| 113 |
for ($i = 1; $i <= count($_sections); $i += 2) { |
| 114 |
if (!isset($_sections[$i - 1]) || !isset($_sections[$i])) { |
| 115 |
continue; |
| 116 |
} |
| 117 |
|
| 118 |
$title = $_sections[$i - 1]; |
| 119 |
$sections[str_replace(' ', '_', strtolower($title))] = [ |
| 120 |
'title' => $title, |
| 121 |
'content' => $_sections[$i] |
| 122 |
]; |
| 123 |
} |
| 124 |
|
| 125 |
return $sections; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Parse changelog entries |
| 130 |
* |
| 131 |
* @param string $content |
| 132 |
* @return array |
| 133 |
*/ |
| 134 |
private function parseChangelogEntries(string $content): array |
| 135 |
{ |
| 136 |
$_changelogs = preg_split('/^[\s]*=[\s]*(.+?)[\s]*=/m', $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); |
| 137 |
$changelogs = []; |
| 138 |
|
| 139 |
for ($i = 1; $i <= count($_changelogs); $i += 2) { |
| 140 |
if (!isset($_changelogs[$i - 1]) || !isset($_changelogs[$i])) { |
| 141 |
continue; |
| 142 |
} |
| 143 |
|
| 144 |
$version = $_changelogs[$i - 1]; |
| 145 |
$content = ltrim($_changelogs[$i], "\n"); |
| 146 |
|
| 147 |
// Parse changelog items |
| 148 |
$items = $this->parseChangelogItems($content); |
| 149 |
|
| 150 |
if (!empty($items)) { |
| 151 |
$changelogs[] = [ |
| 152 |
'version' => $version, |
| 153 |
'items' => $items |
| 154 |
]; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
return $changelogs; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Parse individual changelog items |
| 163 |
* |
| 164 |
* @param string $content |
| 165 |
* @return array |
| 166 |
*/ |
| 167 |
private function parseChangelogItems(string $content): array |
| 168 |
{ |
| 169 |
$lines = explode("\n", $content); |
| 170 |
$items = []; |
| 171 |
|
| 172 |
foreach ($lines as $line) { |
| 173 |
$line = trim($line); |
| 174 |
if (empty($line)) { |
| 175 |
continue; |
| 176 |
} |
| 177 |
|
| 178 |
// Remove leading asterisk and clean up |
| 179 |
if (strpos($line, '* ') === 0) { |
| 180 |
$line = substr($line, 2); |
| 181 |
} |
| 182 |
|
| 183 |
if (!empty($line)) { |
| 184 |
$items[] = trim($line); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
return $items; |
| 189 |
} |
| 190 |
} |