1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* This file is a readme.txt builder used to publish updates on WordPress repository. |
4
|
|
|
* It avoid duplicating README.md files and ensure simplicity on Github. |
5
|
|
|
* |
6
|
|
|
* @package wp-mautic |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
// ----------------------------------------------------------------------------- |
10
|
|
|
/** |
11
|
|
|
* Markdown parser helper |
12
|
|
|
* |
13
|
|
|
* @param string $file |
14
|
|
|
* @return array |
15
|
|
|
*/ |
16
|
|
|
function parse_markdown( string $file ) :array { |
17
|
|
|
$source = fopen( $file, 'r' ); |
18
|
|
|
$content = ['default' => '']; |
19
|
|
|
$current = 'default'; |
20
|
|
|
$complement = ''; |
21
|
|
|
$level = 0; |
22
|
|
|
$sourceCode = false; |
23
|
|
|
while ( ! feof( $source ) ) { |
24
|
|
|
$line = rtrim(fgets( $source )); |
25
|
|
|
if ( '' === $line ) { |
26
|
|
|
$content[ $current ] .= PHP_EOL; |
27
|
|
|
continue; |
28
|
|
|
} |
29
|
|
|
if ( strpos( $line, '#' ) === 0 ) { |
30
|
|
|
$tmp = strpos( $line, ' ' ); |
31
|
|
|
if ( $level > 1 ) { |
32
|
|
|
if( $tmp > $level ) { |
33
|
|
|
$complement = $current . '{}'; |
34
|
|
|
} elseif($tmp < $level) { |
35
|
|
|
$exploded = explode('{}', $current); |
36
|
|
|
$complement = count($exploded) <= 2 |
37
|
|
|
? '' |
38
|
|
|
: implode('{}', array_slice($exploded, 0, count($exploded)-2)).'{}'; |
39
|
|
|
|
40
|
|
|
} |
41
|
|
|
} else { |
42
|
|
|
$complement = ''; |
43
|
|
|
} |
44
|
|
|
$current = $complement . trim( substr( $line, $tmp ) ); |
45
|
|
|
$content[ $current ] = ''; |
46
|
|
|
$level = $tmp; |
47
|
|
|
} else { |
48
|
|
|
if (0 === strpos($line, '`')) { |
49
|
|
|
$sourceCode = !$sourceCode; |
50
|
|
|
continue; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$content[ $current ] .= (true === $sourceCode?' ':'').$line . PHP_EOL; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $content; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// ----------------------------------------------------------------------------- |
61
|
|
|
// readme.txt template |
62
|
|
|
$template = <<<TXT |
63
|
|
|
=== WP Mautic === |
64
|
|
|
Author: mautic |
65
|
|
|
Tags: marketing, automation |
66
|
|
|
%tags% |
67
|
|
|
Donate link: http://mautic.org/ |
68
|
|
|
|
69
|
|
|
== Description == |
70
|
|
|
|
71
|
|
|
%heading% |
72
|
|
|
|
73
|
|
|
## Key features |
74
|
|
|
%key-features% |
75
|
|
|
|
76
|
|
|
## Configuration |
77
|
|
|
|
78
|
|
|
%configuration% |
79
|
|
|
|
80
|
|
|
## Usage |
81
|
|
|
|
82
|
|
|
%documentation% |
83
|
|
|
|
84
|
|
|
== Installation == |
85
|
|
|
|
86
|
|
|
%installation% |
87
|
|
|
|
88
|
|
|
== Upgrade Notice == |
89
|
|
|
|
90
|
|
|
%upgrade%== Changelog == |
91
|
|
|
|
92
|
|
|
%changelog% |
93
|
|
|
TXT; |
94
|
|
|
|
95
|
|
|
// ----------------------------------------------------------------------------- |
96
|
|
|
// Extract package tag |
97
|
|
|
$source = fopen( __DIR__ . '/../wpmautic.php', 'r' ); |
98
|
|
|
$tags = []; |
99
|
|
|
while ( ! feof( $source ) ) { |
100
|
|
|
$line = fgets( $source ); |
101
|
|
|
if ( " * @package wp-mautic\n" === $line ) { |
102
|
|
|
break; |
103
|
|
|
} |
104
|
|
|
if ( |
105
|
|
|
0 === strpos( $line, ' * ' ) && |
106
|
|
|
false === strpos( $line, 'Author:' ) && |
107
|
|
|
false === strpos( $line, 'Plugin ' ) |
108
|
|
|
) { |
109
|
|
|
$tags[] = str_replace( 'Version:', 'Stable tag:', trim( substr( $line, 3 ) ) ); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
$tags = implode( PHP_EOL, $tags ); |
113
|
|
|
|
114
|
|
|
// ----------------------------------------------------------------------------- |
115
|
|
|
// Extract upgrade notices |
116
|
|
|
$notices = []; |
117
|
|
|
foreach ( parse_markdown( __DIR__ . '/../UPGRADE.md' ) as $key => $notice ) { |
118
|
|
|
if ( ! preg_match( '/^v.*/', $key ) ) { |
119
|
|
|
continue; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$notices[] = "= $key =\n$notice"; |
123
|
|
|
} |
124
|
|
|
$notices = implode( $notices ); |
125
|
|
|
|
126
|
|
|
// ----------------------------------------------------------------------------- |
127
|
|
|
// Extract changelog |
128
|
|
|
$changelog = ''; |
129
|
|
|
$title = ''; |
130
|
|
|
foreach ( parse_markdown( __DIR__ . '/../CHANGELOG.md' ) as $key => $notice ) { |
131
|
|
|
if ( ! preg_match( '/^\[v.*/', $key ) ) { |
132
|
|
|
continue; |
133
|
|
|
} |
134
|
|
|
if ( false === strpos( $key, '{}' ) ) { |
135
|
|
|
preg_match( '/\[(.*)\] - (.*)/', $key, $matches ); |
136
|
|
|
$changelog .= "= {$matches[1]} =\n\nRelease date: {$matches[2]}\n\n"; |
137
|
|
|
} else { |
138
|
|
|
$tmp = explode( '{}', $key ); |
139
|
|
|
$changelog .= "* {$tmp[1]}\n" . str_replace( '- ', ' * ', $notice ); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// ----------------------------------------------------------------------------- |
144
|
|
|
// Extract README details |
145
|
|
|
$sections = []; |
146
|
|
|
|
147
|
|
|
$readme = parse_markdown( __DIR__ . '/../README.md' ); |
148
|
|
|
|
149
|
|
|
$header = explode('=======================', $readme['default'] ?? ''); |
150
|
|
|
$heading = end($header); |
151
|
|
|
$keyFeature = $readme['Key features'] ?? ''; |
152
|
|
|
$configuration = $readme['Configuration'] ?? ''; |
153
|
|
|
$documentation = ''; |
154
|
|
|
$installation = ''; |
155
|
|
|
foreach ( $readme as $key => $bloc ) { |
156
|
|
|
$tmp = []; |
157
|
|
|
if (false !== strpos( $key, '{}' )) { |
158
|
|
|
$tmp = explode( '{}', $key ); |
159
|
|
|
} |
160
|
|
View Code Duplication |
if (0 === strpos($key, 'Usage')) { |
|
|
|
|
161
|
|
|
$documentation .= (count($tmp) > 1 ? str_repeat('#', count($tmp)+1).' '.end($tmp).PHP_EOL : '').$bloc; |
162
|
|
|
} |
163
|
|
View Code Duplication |
if (0 === strpos($key, 'Installation')) { |
|
|
|
|
164
|
|
|
$installation .= (count($tmp) > 1 ? str_repeat('#', count($tmp)+1).' '.end($tmp).PHP_EOL : '').$bloc; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
// ----------------------------------------------------------------------------- |
169
|
|
|
// Export template |
170
|
|
|
echo str_replace([ |
171
|
|
|
'%tags%', |
172
|
|
|
'%heading%', |
173
|
|
|
'%key-features%', |
174
|
|
|
'%configuration%', |
175
|
|
|
'%documentation%', |
176
|
|
|
'%installation%', |
177
|
|
|
'%changelog%', |
178
|
|
|
'%upgrade%', |
179
|
|
|
'%upgrade%', |
180
|
|
|
], [ |
181
|
|
|
$tags, |
182
|
|
|
trim($heading), |
183
|
|
|
trim($keyFeature), |
184
|
|
|
trim($configuration), |
185
|
|
|
trim($documentation), |
186
|
|
|
trim($installation), |
187
|
|
|
$changelog, |
188
|
|
|
$notices |
189
|
|
|
], $template); |
190
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.