-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathPost_Revision_Command.php
More file actions
186 lines (166 loc) · 4.58 KB
/
Post_Revision_Command.php
File metadata and controls
186 lines (166 loc) · 4.58 KB
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
<?php
use WP_CLI\Utils;
/**
* Manages post revisions.
*
* ## EXAMPLES
*
* # Restore a post revision
* $ wp post revision restore 123
* Success: Restored revision 123.
*
* # Show diff between two revisions
* $ wp post revision diff 123 456
*
* @package wp-cli
*/
class Post_Revision_Command {
/**
* Valid post fields that can be compared.
*
* @var array<string>
*/
private $valid_fields = [
'post_title',
'post_content',
'post_excerpt',
'post_name',
'post_status',
'post_type',
'post_author',
'post_date',
'post_date_gmt',
'post_modified',
'post_modified_gmt',
'post_parent',
'menu_order',
'comment_status',
'ping_status',
];
/**
* Restores a post revision.
*
* ## OPTIONS
*
* <revision_id>
* : The revision ID to restore.
*
* ## EXAMPLES
*
* # Restore a post revision
* $ wp post revision restore 123
* Success: Restored revision 123.
*
* @subcommand restore
*/
public function restore( $args ) {
$revision_id = (int) $args[0];
// Get the revision post
$revision = wp_get_post_revision( $revision_id );
if ( ! $revision ) {
WP_CLI::error( "Invalid revision ID {$revision_id}." );
}
// Restore the revision
$restored_post_id = wp_restore_post_revision( $revision_id );
// wp_restore_post_revision() returns post ID on success, false on failure, or null if revision is same as current
if ( ! $restored_post_id ) {
WP_CLI::error( "Failed to restore revision {$revision_id}." );
}
WP_CLI::success( "Restored revision {$revision_id}." );
}
/**
* Shows the difference between two revisions.
*
* ## OPTIONS
*
* <from>
* : The 'from' revision ID or post ID.
*
* [<to>]
* : The 'to' revision ID. If not provided, compares with the current post.
*
* [--field=<field>]
* : Compare specific field(s). Default: post_content
*
* ## EXAMPLES
*
* # Show diff between two revisions
* $ wp post revision diff 123 456
*
* # Show diff between a revision and the current post
* $ wp post revision diff 123
*
* @subcommand diff
*/
public function diff( $args, $assoc_args ) {
$from_id = (int) $args[0];
$to_id = isset( $args[1] ) ? (int) $args[1] : null;
$field = Utils\get_flag_value( $assoc_args, 'field', 'post_content' );
// Get the 'from' revision or post
$from_revision = wp_get_post_revision( $from_id );
if ( ! $from_revision instanceof \WP_Post ) {
// Try as a regular post
$from_revision = get_post( $from_id );
if ( ! $from_revision instanceof \WP_Post ) {
WP_CLI::error( "Invalid 'from' ID {$from_id}." );
}
}
// Get the 'to' revision or post
$to_revision = null;
if ( $to_id ) {
$to_revision = wp_get_post_revision( $to_id );
if ( ! $to_revision instanceof \WP_Post ) {
// Try as a regular post
$to_revision = get_post( $to_id );
if ( ! $to_revision instanceof \WP_Post ) {
WP_CLI::error( "Invalid 'to' ID {$to_id}." );
}
}
} elseif ( 'revision' === $from_revision->post_type ) {
// If no 'to' ID provided, use the parent post of the revision
$to_revision = get_post( $from_revision->post_parent );
if ( ! $to_revision instanceof \WP_Post ) {
WP_CLI::error( "Could not find parent post for revision {$from_id}." );
}
} else {
WP_CLI::error( "Please provide a 'to' revision ID when comparing posts." );
}
// Validate field
if ( ! in_array( $field, $this->valid_fields, true ) ) {
WP_CLI::error( "Invalid field '{$field}'. Valid fields: " . implode( ', ', $this->valid_fields ) );
}
// Get the field values - use isset to check if field exists on the object
if ( ! isset( $from_revision->{$field} ) ) {
WP_CLI::error( "Field '{$field}' not found on revision {$from_id}." );
}
// $to_revision is guaranteed to be non-null at this point due to earlier validation
if ( ! isset( $to_revision->{$field} ) ) {
$to_error_id = $to_id ?? $to_revision->ID;
WP_CLI::error( "Field '{$field}' not found on revision/post {$to_error_id}." );
}
$left_string = $from_revision->{$field};
$right_string = $to_revision->{$field};
// Generate the diff
$diff_args = [
'title_left' => sprintf(
'%s (%s) - ID %d',
$from_revision->post_title,
$from_revision->post_modified,
$from_revision->ID
),
'title_right' => sprintf(
'%s (%s) - ID %d',
$to_revision->post_title,
$to_revision->post_modified,
$to_revision->ID
),
];
$diff = wp_text_diff( $left_string, $right_string, $diff_args );
if ( ! $diff ) {
WP_CLI::success( 'No difference found.' );
return;
}
// Output the diff
WP_CLI::line( $diff );
}
}