Skip to content

Commit 98d8590

Browse files
Copilotswissspidy
andcommitted
Address code review feedback: validate fields properly and remove unused property
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 8f2b422 commit 98d8590

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

src/Post_Revision_Command.php

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
use WP_CLI\Fetchers\Post as PostFetcher;
43
use WP_CLI\Utils;
54

65
/**
@@ -19,11 +18,28 @@
1918
*/
2019
class Post_Revision_Command {
2120

22-
private $fetcher;
23-
24-
public function __construct() {
25-
$this->fetcher = new PostFetcher();
26-
}
21+
/**
22+
* Valid post fields that can be compared.
23+
*
24+
* @var array<string>
25+
*/
26+
private $valid_fields = [
27+
'post_title',
28+
'post_content',
29+
'post_excerpt',
30+
'post_name',
31+
'post_status',
32+
'post_type',
33+
'post_author',
34+
'post_date',
35+
'post_date_gmt',
36+
'post_modified',
37+
'post_modified_gmt',
38+
'post_parent',
39+
'menu_order',
40+
'comment_status',
41+
'ping_status',
42+
];
2743

2844
/**
2945
* Restores a post revision.
@@ -122,11 +138,18 @@ public function diff( $args, $assoc_args ) {
122138
}
123139

124140
// Validate field
125-
if ( ! property_exists( $from_revision, $field ) || ! property_exists( $to_revision, $field ) ) {
126-
WP_CLI::error( "Invalid field '{$field}'." );
141+
if ( ! in_array( $field, $this->valid_fields, true ) ) {
142+
WP_CLI::error( "Invalid field '{$field}'. Valid fields: " . implode( ', ', $this->valid_fields ) );
143+
}
144+
145+
// Get the field values - use isset to check if field exists on the object
146+
if ( ! isset( $from_revision->{$field} ) ) {
147+
WP_CLI::error( "Field '{$field}' not found on revision {$from_id}." );
148+
}
149+
if ( ! isset( $to_revision->{$field} ) ) {
150+
WP_CLI::error( "Field '{$field}' not found on revision/post {$to_id}." );
127151
}
128152

129-
// Get the field values
130153
$left_string = $from_revision->{$field};
131154
$right_string = $to_revision->{$field};
132155

0 commit comments

Comments
 (0)