Skip to content

Commit 5efdc41

Browse files
Copilotswissspidy
andcommitted
Address code review feedback: handle null restore, extract helper, use instanceof
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent c0611b0 commit 5efdc41

File tree

1 file changed

+54
-60
lines changed

1 file changed

+54
-60
lines changed

src/Post_Revision_Command.php

Lines changed: 54 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,37 @@ public function restore( $args ) {
8080
// wp_restore_post_revision() returns post ID on success, false on failure, or null if revision is same as current
8181
if ( false === $restored_post_id ) {
8282
WP_CLI::error( "Failed to restore revision {$revision_id}." );
83+
} elseif ( null === $restored_post_id ) {
84+
WP_CLI::warning( "Revision {$revision_id} is the same as the current post. No action taken." );
85+
} else {
86+
WP_CLI::success( "Restored revision {$revision_id}." );
87+
}
88+
}
89+
90+
/**
91+
* Gets a post or revision object by ID.
92+
*
93+
* @param int $id The post or revision ID.
94+
* @param string $name The name to use in error messages ('from' or 'to').
95+
* @return \WP_Post The post or revision object.
96+
*/
97+
private function get_post_or_revision( $id, $name ) {
98+
$post = wp_get_post_revision( $id );
99+
100+
/**
101+
* Work around https://core.trac.wordpress.org/ticket/64643.
102+
* @var int $id
103+
*/
104+
105+
if ( ! $post instanceof \WP_Post ) {
106+
// Try as a regular post
107+
$post = get_post( $id );
108+
if ( ! $post instanceof \WP_Post ) {
109+
WP_CLI::error( "Invalid '{$name}' ID {$id}." );
110+
}
83111
}
84112

85-
WP_CLI::success( "Restored revision {$revision_id}." );
113+
return $post;
86114
}
87115

88116
/**
@@ -118,38 +146,12 @@ public function diff( $args, $assoc_args ) {
118146
$field = Utils\get_flag_value( $assoc_args, 'field', 'post_content' );
119147

120148
// Get the 'from' revision or post
121-
$from_revision = wp_get_post_revision( $from_id );
122-
123-
/**
124-
* Work around https://core.trac.wordpress.org/ticket/64643.
125-
* @var int $from_id
126-
*/
127-
128-
if ( ! $from_revision instanceof \WP_Post ) {
129-
// Try as a regular post
130-
$from_revision = get_post( $from_id );
131-
if ( ! $from_revision instanceof \WP_Post ) {
132-
WP_CLI::error( "Invalid 'from' ID {$from_id}." );
133-
}
134-
}
149+
$from_revision = $this->get_post_or_revision( $from_id, 'from' );
135150

136151
// Get the 'to' revision or post
137152
$to_revision = null;
138153
if ( $to_id ) {
139-
$to_revision = wp_get_post_revision( $to_id );
140-
141-
/**
142-
* Work around https://core.trac.wordpress.org/ticket/64643.
143-
* @var int $to_id
144-
*/
145-
146-
if ( ! $to_revision instanceof \WP_Post ) {
147-
// Try as a regular post
148-
$to_revision = get_post( $to_id );
149-
if ( ! $to_revision instanceof \WP_Post ) {
150-
WP_CLI::error( "Invalid 'to' ID {$to_id}." );
151-
}
152-
}
154+
$to_revision = $this->get_post_or_revision( $to_id, 'to' );
153155
} elseif ( 'revision' === $from_revision->post_type ) {
154156
// If no 'to' ID provided, use the parent post of the revision
155157
$to_revision = get_post( $from_revision->post_parent );
@@ -233,37 +235,29 @@ private function render_cli_diff( $diff ) {
233235
$edits = $diff->getDiff();
234236

235237
foreach ( $edits as $edit ) {
236-
switch ( get_class( $edit ) ) {
237-
case 'Text_Diff_Op_copy':
238-
// Unchanged lines - show in default color
239-
foreach ( $edit->orig as $line ) {
240-
WP_CLI::line( ' ' . $line );
241-
}
242-
break;
243-
244-
case 'Text_Diff_Op_add':
245-
// Added lines - show in green
246-
foreach ( $edit->final as $line ) {
247-
WP_CLI::line( WP_CLI::colorize( '%g+ ' . $line . '%n' ) );
248-
}
249-
break;
250-
251-
case 'Text_Diff_Op_delete':
252-
// Deleted lines - show in red
253-
foreach ( $edit->orig as $line ) {
254-
WP_CLI::line( WP_CLI::colorize( '%r- ' . $line . '%n' ) );
255-
}
256-
break;
257-
258-
case 'Text_Diff_Op_change':
259-
// Changed lines - show deletions in red, additions in green
260-
foreach ( $edit->orig as $line ) {
261-
WP_CLI::line( WP_CLI::colorize( '%r- ' . $line . '%n' ) );
262-
}
263-
foreach ( $edit->final as $line ) {
264-
WP_CLI::line( WP_CLI::colorize( '%g+ ' . $line . '%n' ) );
265-
}
266-
break;
238+
if ( $edit instanceof \Text_Diff_Op_copy ) {
239+
// Unchanged lines - show in default color
240+
foreach ( $edit->orig as $line ) {
241+
WP_CLI::line( ' ' . $line );
242+
}
243+
} elseif ( $edit instanceof \Text_Diff_Op_add ) {
244+
// Added lines - show in green
245+
foreach ( $edit->final as $line ) {
246+
WP_CLI::line( WP_CLI::colorize( '%g+ ' . $line . '%n' ) );
247+
}
248+
} elseif ( $edit instanceof \Text_Diff_Op_delete ) {
249+
// Deleted lines - show in red
250+
foreach ( $edit->orig as $line ) {
251+
WP_CLI::line( WP_CLI::colorize( '%r- ' . $line . '%n' ) );
252+
}
253+
} elseif ( $edit instanceof \Text_Diff_Op_change ) {
254+
// Changed lines - show deletions in red, additions in green
255+
foreach ( $edit->orig as $line ) {
256+
WP_CLI::line( WP_CLI::colorize( '%r- ' . $line . '%n' ) );
257+
}
258+
foreach ( $edit->final as $line ) {
259+
WP_CLI::line( WP_CLI::colorize( '%g+ ' . $line . '%n' ) );
260+
}
267261
}
268262
}
269263
}

0 commit comments

Comments
 (0)