forked from verygoodplugins/edd-changelog-badges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedd-changelog.php
207 lines (170 loc) · 4.71 KB
/
edd-changelog.php
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* Plugin Name: EDD Changelog
* Plugin URI: https://wpfusion.com/
* Description: Beautiful changelog display for Easy Digital Downloads products
* Version: 1.0.0
* Author: Very Good Plugins
* Author URI: https://verygoodplugins.com/
* Text Domain: edd-changelog
*
* @package EDD_Changelog
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Register shortcode
*
* @param array $atts The shortcode attributes.
* @return string The changelog HTML.
*/
function edd_changelog_shortcode( $atts ) {
$defaults = array(
'id' => get_the_id(),
);
$atts = shortcode_atts( $defaults, $atts );
$changelog = get_post_meta( $atts['id'], '_edd_sl_changelog', true );
if ( empty( $changelog ) ) {
return '';
}
// Sanitize the HTML from the change log field.
$changelog = balanceTags( wp_kses_post( $changelog ), true );
$changelog = stripslashes( $changelog );
// Make sure we're using h4s.
$changelog = str_replace( 'h3', 'h4', $changelog );
// Define default badge configurations
$badge_configs = apply_filters(
'edd_changelog_badge_configs',
array(
'added' => array(
'prefix' => 'Added',
'badge_text' => 'New',
'emoji' => '✨',
'class' => 'new',
'ltrim' => false,
),
'improved' => array(
'prefix' => 'Improved',
'badge_text' => 'Improved',
'emoji' => '⚡️',
'class' => 'improved',
'ltrim' => 'Improved - ',
),
'fix' => array(
'prefix' => 'Fix',
'badge_text' => 'Fixed',
'emoji' => '🔧',
'class' => 'fixed',
'ltrim' => false,
),
'developer' => array(
'prefix' => 'Developer',
'badge_text' => 'Dev',
'emoji' => '🛠️',
'class' => 'dev',
'ltrim' => 'Developers: ',
),
)
);
// Add badges to list items
$changelog = preg_replace_callback(
'/<li>(.*?)<\/li>/s',
function ( $matches ) use ( $badge_configs ) {
$content = $matches[1];
$badge = '';
foreach ( $badge_configs as $config ) {
if ( preg_match( '/^' . $config['prefix'] . '/i', $content ) ) {
if ( $config['ltrim'] ) {
$content = str_replace( $config['ltrim'], '', $content );
}
$badge = sprintf(
'<span class="changelog-badge %s">%s %s</span> ',
$config['class'],
$config['emoji'],
$config['badge_text']
);
break;
}
}
return "<li>{$badge}{$content}</li>";
},
$changelog
);
$changelog = edd_changelog_add_ids_to_headings( $changelog );
$html = '<style type="text/css">
.edd_changelog-content ul {
margin-left: 0;
}
.edd_changelog-content ul li {
list-style-type: none;
position: relative;
padding-left: 86px;
}
.changelog-badge {
display: inline-block;
padding: 1px 6px;
border-radius: 8px;
font-size: 12px;
font-weight: 600;
margin-right: 8px;
line-height: 2;
width: 86px;
text-align: center;
position: absolute;
left: -10px;
top: 5px;
word-spacing: 2px;
}
.changelog-badge.new {
background-color: #22aa45;
color: white;
}
.changelog-badge.improved {
background-color: #2271b1;
color: white;
}
.changelog-badge.fixed {
background-color: #E55B10;
color: white;
}
.changelog-badge.dev {
background-color: #3c434a;
color: white;
}
</style>';
$html .= sprintf(
'<div id="edd_changelog_content-%1$d" class="edd_changelog-content">%2$s</div>',
$atts['id'],
$changelog
);
return $html;
}
add_shortcode( 'edd_changelog', 'edd_changelog_shortcode' );
/**
* Add IDs to headings for anchor links.
*
* @param string $content The content to add IDs to.
* @return string The content with IDs added to headings.
*/
function edd_changelog_add_ids_to_headings( $content ) {
$depth = 4;
$pattern = '/<h[2-' . $depth . ']*[^>]*>.*?<\/h[2-' . $depth . ']>/';
$whocares = preg_match_all( $pattern, $content, $winners );
foreach ( $winners[0] as $i => $winner ) {
// If it's a changelog we'll take the date off the slug.
$slug = preg_replace( '/\s-\s\d+\/\d+\/\d+/', '', $winner );
$slug = sanitize_title( $slug );
if ( false !== strpos( $winner, 'class="' ) ) {
// Has a class.
$winners[0][ $i ] = str_replace( '">', ' anchoring" id="' . $slug . '"><a class="anchor" href="#' . $slug . '">#</a>', $winners[0][ $i ] );
} else {
// Straight up <h3> or <h4> tag.
$winners[0][ $i ] = str_replace( '<h3>', '<h3 class="anchoring" id="' . $slug . '"><a class="anchor" href="#' . $slug . '">#</a>', $winners[0][ $i ] );
$winners[0][ $i ] = str_replace( '<h4>', '<h4 class="anchoring" id="' . $slug . '"><a class="anchor" href="#' . $slug . '">#</a>', $winners[0][ $i ] );
}
$content = str_replace( $winner, $winners[0][ $i ], $content );
}
return $content;
}