-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheasysuite-changelog.php
More file actions
76 lines (56 loc) · 1.83 KB
/
Copy patheasysuite-changelog.php
File metadata and controls
76 lines (56 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
* Plugin Name: EasySuite Changelog
*/
add_shortcode( 'changelog', function ( $atts ) {
$atts = shortcode_atts(
array(
'slug' => 'easycommerce',
'version' => 'trunk',
'cache' => DAY_IN_SECONDS,
'heading' => 'h4',
),
$atts,
'changelog'
);
$slug = $atts['slug'];
$version = $atts['version'] == 'trunk' ? 'trunk' : "tags/{$atts['version']}";
if ( false === $readme = get_transient( "easysuite-readme_{$slug}_{$version}" ) ) {
$remote = wp_remote_get( "https://plugins.svn.wordpress.org/{$slug}/{$version}/readme.txt" );
if ( 200 != wp_remote_retrieve_response_code( $remote ) ) {
return;
}
$readme = wp_remote_retrieve_body( $remote );
set_transient( "easysuite-readme_{$slug}_{$version}", $readme, $atts['cache'] );
}
if ( $readme ) {
return parse_changelog( $readme, $atts['heading'] );
}
}
);
function parse_changelog( $readme_content, $heading = 'h4' ) {
// Extract Changelog section only
if ( preg_match( '/== Changelog ==(.*?)(== [^=]+ ==|$)/s', $readme_content, $matches ) ) {
$changelog_raw = trim( $matches[1] );
// Match version blocks like "= 0.9.7-beta – 2025.03.19 ="
preg_match_all( '/= (.*?) =\s*(.*?)(?=\n= .*? =|\z)/s', $changelog_raw, $entries, PREG_SET_ORDER );
$html = '<div class="changelog">';
foreach ( $entries as $entry ) {
$version = htmlspecialchars( trim( $entry[1] ) );
$changes_raw = trim( $entry[2] );
$html .= "<{$heading}>{$version}</{$heading}>";
$html .= '<ul>';
// Convert each change line into <li>, ignoring empty lines
foreach ( preg_split( '/\r\n|\r|\n/', $changes_raw ) as $line ) {
$line = trim( $line );
if ( ! empty( $line ) ) {
$html .= '<li>' . htmlspecialchars( $line ) . '</li>';
}
}
$html .= '</ul>';
}
$html .= '</div>';
return $html;
}
return '';
}