| 1 |
<?php |
| 2 |
|
| 3 |
namespace CryptX\Admin; |
| 4 |
|
| 5 |
use CryptX\Config; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class ChangelogSettingsTab |
| 9 |
* Handles the changelog tab functionality in the CryptX plugin admin interface |
| 10 |
*/ |
| 11 |
class ChangelogSettingsTab |
| 12 |
{ |
| 13 |
/** |
| 14 |
* @var Config Configuration instance |
| 15 |
*/ |
| 16 |
private Config $config; |
| 17 |
|
| 18 |
/** |
| 19 |
* ChangelogSettingsTab constructor. |
| 20 |
* |
| 21 |
* @param Config $config Configuration instance |
| 22 |
*/ |
| 23 |
public function __construct(Config $config) |
| 24 |
{ |
| 25 |
$this->config = $config; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Render the changelog tab content |
| 30 |
*/ |
| 31 |
public function render(): void |
| 32 |
{ |
| 33 |
echo '<h4>' . esc_html__('Changelog', 'cryptx') . '</h4>'; |
| 34 |
$this->renderChangelogContent(); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Parse and render changelog content from readme.txt |
| 39 |
*/ |
| 40 |
private function renderChangelogContent(): void |
| 41 |
{ |
| 42 |
$readmePath = CRYPTX_DIR_PATH . '/readme.txt'; |
| 43 |
if (!file_exists($readmePath)) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
$fileContents = file_get_contents($readmePath); |
| 48 |
if ($fileContents === false) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
$changelogs = $this->parseChangelog($fileContents); |
| 53 |
foreach ($changelogs as $log) { |
| 54 |
echo wp_kses_post("<dl>" . implode("", $log) . "</dl>"); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Parse changelog content from readme.txt |
| 60 |
* |
| 61 |
* @param string $content |
| 62 |
* @return array |
| 63 |
*/ |
| 64 |
private function parseChangelog(string $content): array |
| 65 |
{ |
| 66 |
$content = str_replace(["\r\n", "\r"], "\n", $content); |
| 67 |
$content = trim($content); |
| 68 |
|
| 69 |
// Split into sections |
| 70 |
$sections = $this->parseSections($content); |
| 71 |
if (!isset($sections['changelog'])) { |
| 72 |
return []; |
| 73 |
} |
| 74 |
|
| 75 |
// Parse changelog entries |
| 76 |
return $this->parseChangelogEntries($sections['changelog']['content']); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Parse sections from readme content |
| 81 |
* |
| 82 |
* @param string $content |
| 83 |
* @return array |
| 84 |
*/ |
| 85 |
private function parseSections(string $content): array |
| 86 |
{ |
| 87 |
$_sections = preg_split('/^[\s]*==[\s]*(.+?)[\s]*==/m', $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); |
| 88 |
$sections = []; |
| 89 |
|
| 90 |
for ($i = 1; $i <= count($_sections); $i += 2) { |
| 91 |
$title = $_sections[$i - 1]; |
| 92 |
$sections[str_replace(' ', '_', strtolower($title))] = [ |
| 93 |
'title' => $title, |
| 94 |
'content' => $_sections[$i] |
| 95 |
]; |
| 96 |
} |
| 97 |
|
| 98 |
return $sections; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Parse changelog entries |
| 103 |
* |
| 104 |
* @param string $content |
| 105 |
* @return array |
| 106 |
*/ |
| 107 |
private function parseChangelogEntries(string $content): array |
| 108 |
{ |
| 109 |
$_changelogs = preg_split('/^[\s]*=[\s]*(.+?)[\s]*=/m', $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); |
| 110 |
$changelogs = []; |
| 111 |
|
| 112 |
for ($i = 1; $i <= count($_changelogs); $i += 2) { |
| 113 |
$version = $_changelogs[$i - 1]; |
| 114 |
$content = ltrim($_changelogs[$i], "\n"); |
| 115 |
$content = str_replace("* ", "<li>", $content); |
| 116 |
$content = str_replace("\n", " </li>\n", $content); |
| 117 |
|
| 118 |
$changelogs[] = [ |
| 119 |
'version' => "<dt>" . esc_html($version) . "</dt>", |
| 120 |
'content' => "<dd><ul>" . wp_kses_post($content) . "</ul></dd>" |
| 121 |
]; |
| 122 |
} |
| 123 |
|
| 124 |
return $changelogs; |
| 125 |
} |
| 126 |
} |