Skip to content

Commit 6e169a3

Browse files
authored
Keep percent signs in post revision diff output (#673)
The diff lines and post titles were passed to WP_CLI::colorize() as they are, so '%' sequences in the content were read as color tokens. For example 'my%20file.pdf' was printed as 'my0file.pdf' and a line ending in '100%' was printed as '100%n'. Escape '%' as '%%' before colorizing.
1 parent 35b0e2f commit 6e169a3

2 files changed

Lines changed: 50 additions & 6 deletions

File tree

‎features/post-revision.feature‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,40 @@ Feature: Manage WordPress post revisions
150150
When I run `wp post revision diff {REVISION_ID} --field=post_title`
151151
Then the return code should be 0
152152

153+
Scenario: Diff shows percent signs in the compared content as they are
154+
When I run `wp post create --post_title='Percent Test' --post_content='Start' --porcelain`
155+
Then STDOUT should be a number
156+
And save STDOUT as {POST_ID}
157+
158+
When I run `wp post update {POST_ID} --post_content='Progress: 100%'`
159+
Then STDOUT should contain:
160+
"""
161+
Success: Updated post {POST_ID}.
162+
"""
163+
164+
When I run `wp post update {POST_ID} --post_content='Progress: 100% of https://example.com/my%20file.pdf'`
165+
Then STDOUT should contain:
166+
"""
167+
Success: Updated post {POST_ID}.
168+
"""
169+
170+
When I run `wp post list --post_type=revision --post_parent={POST_ID} --fields=ID --format=ids --orderby=ID --order=ASC`
171+
Then STDOUT should not be empty
172+
And save STDOUT as {REVISION_IDS}
173+
174+
When I run `echo "{REVISION_IDS}" | awk '{print $1}'`
175+
Then save STDOUT as {REVISION_ID_1}
176+
177+
When I run `echo "{REVISION_IDS}" | awk '{print $2}'`
178+
Then save STDOUT as {REVISION_ID_2}
179+
180+
When I run `wp post revision diff {REVISION_ID_1} {REVISION_ID_2}`
181+
Then STDOUT should contain:
182+
"""
183+
- Progress: 100%
184+
+ Progress: 100% of https://example.com/my%20file.pdf
185+
"""
186+
153187
Scenario: Prune revisions keeping latest N
154188
When I run `wp post create --post_title='Prune Test' --post_content='Version 1' --porcelain`
155189
Then STDOUT should be a number

‎src/Post_Revision_Command.php‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public function diff( $args, $assoc_args ) {
204204
WP_CLI::colorize(
205205
sprintf(
206206
'%%y--- %s (%s) - ID %d%%n',
207-
$from_revision->post_title,
207+
$this->escape_color_tokens( $from_revision->post_title ),
208208
$from_revision->post_modified,
209209
$from_revision->ID
210210
)
@@ -214,7 +214,7 @@ public function diff( $args, $assoc_args ) {
214214
WP_CLI::colorize(
215215
sprintf(
216216
'%%y+++ %s (%s) - ID %d%%n',
217-
$to_revision->post_title,
217+
$this->escape_color_tokens( $to_revision->post_title ),
218218
$to_revision->post_modified,
219219
$to_revision->ID
220220
)
@@ -243,25 +243,35 @@ private function render_cli_diff( $diff ) {
243243
} elseif ( $edit instanceof \Text_Diff_Op_add ) {
244244
// Added lines - show in green
245245
foreach ( $edit->final as $line ) {
246-
WP_CLI::line( WP_CLI::colorize( '%g+ ' . $line . '%n' ) );
246+
WP_CLI::line( WP_CLI::colorize( '%g+ ' . $this->escape_color_tokens( $line ) . '%n' ) );
247247
}
248248
} elseif ( $edit instanceof \Text_Diff_Op_delete ) {
249249
// Deleted lines - show in red
250250
foreach ( $edit->orig as $line ) {
251-
WP_CLI::line( WP_CLI::colorize( '%r- ' . $line . '%n' ) );
251+
WP_CLI::line( WP_CLI::colorize( '%r- ' . $this->escape_color_tokens( $line ) . '%n' ) );
252252
}
253253
} elseif ( $edit instanceof \Text_Diff_Op_change ) {
254254
// Changed lines - show deletions in red, additions in green
255255
foreach ( $edit->orig as $line ) {
256-
WP_CLI::line( WP_CLI::colorize( '%r- ' . $line . '%n' ) );
256+
WP_CLI::line( WP_CLI::colorize( '%r- ' . $this->escape_color_tokens( $line ) . '%n' ) );
257257
}
258258
foreach ( $edit->final as $line ) {
259-
WP_CLI::line( WP_CLI::colorize( '%g+ ' . $line . '%n' ) );
259+
WP_CLI::line( WP_CLI::colorize( '%g+ ' . $this->escape_color_tokens( $line ) . '%n' ) );
260260
}
261261
}
262262
}
263263
}
264264

265+
/**
266+
* Escapes percent signs so that WP_CLI::colorize() does not read them as color tokens.
267+
*
268+
* @param string $text Text to print inside a colorized string.
269+
* @return string Text with every '%' doubled.
270+
*/
271+
private function escape_color_tokens( $text ) {
272+
return str_replace( '%', '%%', $text );
273+
}
274+
265275
/**
266276
* Deletes old post revisions.
267277
*

0 commit comments

Comments
 (0)