| 1 |
<?php |
| 2 |
|
| 3 |
namespace TableBuilder\Admin\Api; |
| 4 |
|
| 5 |
defined('ABSPATH') || exit; |
| 6 |
|
| 7 |
class ChangelogData { |
| 8 |
|
| 9 |
private const ROUTE = 'changelog'; |
| 10 |
|
| 11 |
private const SOURCES = [ |
| 12 |
'Free' => [ |
| 13 |
'file' => TABLE_BUILDER_BLOCK_PLUGIN_DIR . 'readme.txt', |
| 14 |
'regex' => '/= (?:TableKit|Table Builder Block)\s*:?\s*([^(=]+?)\s*\(([^)]+)\)\s*=\R(.*?)(?=\R= (?:TableKit|Table Builder Block)\s*:?\s*|\z)/s', |
| 15 |
], |
| 16 |
]; |
| 17 |
|
| 18 |
public function __construct() { |
| 19 |
add_action('rest_api_init', [$this, 'register_routes']); |
| 20 |
} |
| 21 |
|
| 22 |
public function register_routes(): void { |
| 23 |
register_rest_route('tablebuilder/v1', self::ROUTE, [ |
| 24 |
'methods' => \WP_REST_Server::READABLE, |
| 25 |
'callback' => [$this, 'get_changelog'], |
| 26 |
'permission_callback' => fn() => current_user_can('manage_options'), |
| 27 |
]); |
| 28 |
} |
| 29 |
|
| 30 |
public function get_changelog(\WP_REST_Request $request): \WP_REST_Response { |
| 31 |
if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) { |
| 32 |
return new \WP_REST_Response([], 403); |
| 33 |
} |
| 34 |
|
| 35 |
$items = array_merge( |
| 36 |
$this->parse_source('Free', self::SOURCES['Free']), |
| 37 |
$this->parse_source('Pro', $this->get_pro_source()), |
| 38 |
); |
| 39 |
|
| 40 |
usort($items, fn($a, $b) => strtotime($b['publish_date']) <=> strtotime($a['publish_date'])); |
| 41 |
|
| 42 |
return new \WP_REST_Response($items, 200); |
| 43 |
} |
| 44 |
|
| 45 |
private function get_pro_source(): array { |
| 46 |
$dir = defined('TABLE_BUILDER_BLOCK_PRO_PLUGIN_DIR') |
| 47 |
? TABLE_BUILDER_BLOCK_PRO_PLUGIN_DIR |
| 48 |
: dirname(rtrim(TABLE_BUILDER_BLOCK_PLUGIN_DIR, '/\\')) . '/table-builder-block-pro/'; |
| 49 |
|
| 50 |
return [ |
| 51 |
'file' => $dir . 'changelog.txt', |
| 52 |
'regex' => '/= Version:\s*([^(=]+?)\s*\(([^)]+)\)\s*=\R(.*?)(?=\R= Version:|\z)/s', |
| 53 |
]; |
| 54 |
} |
| 55 |
|
| 56 |
private function parse_source(string $type, array $source): array { |
| 57 |
if (!file_exists($source['file'])) { |
| 58 |
return []; |
| 59 |
} |
| 60 |
|
| 61 |
$content = file_get_contents($source['file']); |
| 62 |
|
| 63 |
if (!$content || !preg_match('/== Changelog ==\R(.*)$/s', $content, $m)) { |
| 64 |
return []; |
| 65 |
} |
| 66 |
|
| 67 |
preg_match_all($source['regex'], trim($m[1]), $entries, PREG_SET_ORDER); |
| 68 |
|
| 69 |
return array_filter(array_map( |
| 70 |
fn($entry) => $this->build_item($entry, $type), |
| 71 |
$entries |
| 72 |
)); |
| 73 |
} |
| 74 |
|
| 75 |
private function build_item(array $entry, string $type): ?array { |
| 76 |
$list_items = array_filter( |
| 77 |
array_map(fn($line) => $this->parse_list_line($line), preg_split('/\R/', trim($entry[3]))) |
| 78 |
); |
| 79 |
|
| 80 |
if (empty($list_items)) { |
| 81 |
return null; |
| 82 |
} |
| 83 |
|
| 84 |
return [ |
| 85 |
'title' => trim($entry[1]), |
| 86 |
'type' => $type, |
| 87 |
'publish_date' => $this->format_date(trim($entry[2])), |
| 88 |
'content' => '<ul>' . implode('', $list_items) . '</ul>', |
| 89 |
]; |
| 90 |
} |
| 91 |
|
| 92 |
private function parse_list_line(string $line): string { |
| 93 |
$line = trim($line); |
| 94 |
|
| 95 |
if (!str_starts_with($line, '*')) { |
| 96 |
return ''; |
| 97 |
} |
| 98 |
|
| 99 |
$text = trim(ltrim($line, '* ')); |
| 100 |
|
| 101 |
return $text !== '' ? '<li>' . esc_html($text) . '</li>' : ''; |
| 102 |
} |
| 103 |
|
| 104 |
private function format_date(string $date): string { |
| 105 |
$timestamp = strtotime($date); |
| 106 |
return $timestamp ? gmdate('F j, Y', $timestamp) : $date; |
| 107 |
} |
| 108 |
} |