|
| 1 | +<?php |
| 2 | + |
| 3 | +use WP_CLI\Fetchers\Post as PostFetcher; |
| 4 | +use WP_CLI\Utils; |
| 5 | + |
| 6 | +/** |
| 7 | + * Manages post revisions. |
| 8 | + * |
| 9 | + * ## EXAMPLES |
| 10 | + * |
| 11 | + * # Restore a post revision |
| 12 | + * $ wp post revision restore 123 |
| 13 | + * Success: Restored revision 123. |
| 14 | + * |
| 15 | + * # Show diff between two revisions |
| 16 | + * $ wp post revision diff 123 456 |
| 17 | + * |
| 18 | + * @package wp-cli |
| 19 | + */ |
| 20 | +class Post_Revision_Command { |
| 21 | + |
| 22 | + private $fetcher; |
| 23 | + |
| 24 | + public function __construct() { |
| 25 | + $this->fetcher = new PostFetcher(); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Restores a post revision. |
| 30 | + * |
| 31 | + * ## OPTIONS |
| 32 | + * |
| 33 | + * <revision_id> |
| 34 | + * : The revision ID to restore. |
| 35 | + * |
| 36 | + * ## EXAMPLES |
| 37 | + * |
| 38 | + * # Restore a post revision |
| 39 | + * $ wp post revision restore 123 |
| 40 | + * Success: Restored revision 123. |
| 41 | + * |
| 42 | + * @subcommand restore |
| 43 | + */ |
| 44 | + public function restore( $args ) { |
| 45 | + $revision_id = (int) $args[0]; |
| 46 | + |
| 47 | + // Get the revision post |
| 48 | + $revision = wp_get_post_revision( $revision_id ); |
| 49 | + |
| 50 | + if ( ! $revision ) { |
| 51 | + WP_CLI::error( "Invalid revision ID {$revision_id}." ); |
| 52 | + } |
| 53 | + |
| 54 | + // Restore the revision |
| 55 | + $restored_post_id = wp_restore_post_revision( $revision_id ); |
| 56 | + |
| 57 | + if ( false === $restored_post_id || null === $restored_post_id ) { |
| 58 | + WP_CLI::error( "Failed to restore revision {$revision_id}." ); |
| 59 | + } |
| 60 | + |
| 61 | + WP_CLI::success( "Restored revision {$revision_id}." ); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Shows the difference between two revisions. |
| 66 | + * |
| 67 | + * ## OPTIONS |
| 68 | + * |
| 69 | + * <from> |
| 70 | + * : The 'from' revision ID or post ID. |
| 71 | + * |
| 72 | + * [<to>] |
| 73 | + * : The 'to' revision ID. If not provided, compares with the current post. |
| 74 | + * |
| 75 | + * [--field=<field>] |
| 76 | + * : Compare specific field(s). Default: post_content |
| 77 | + * |
| 78 | + * ## EXAMPLES |
| 79 | + * |
| 80 | + * # Show diff between two revisions |
| 81 | + * $ wp post revision diff 123 456 |
| 82 | + * |
| 83 | + * # Show diff between a revision and the current post |
| 84 | + * $ wp post revision diff 123 |
| 85 | + * |
| 86 | + * @subcommand diff |
| 87 | + */ |
| 88 | + public function diff( $args, $assoc_args ) { |
| 89 | + $from_id = (int) $args[0]; |
| 90 | + $to_id = isset( $args[1] ) ? (int) $args[1] : null; |
| 91 | + $field = Utils\get_flag_value( $assoc_args, 'field', 'post_content' ); |
| 92 | + |
| 93 | + // Get the 'from' revision or post |
| 94 | + $from_revision = wp_get_post_revision( $from_id ); |
| 95 | + if ( ! $from_revision instanceof \WP_Post ) { |
| 96 | + // Try as a regular post |
| 97 | + $from_revision = get_post( $from_id ); |
| 98 | + if ( ! $from_revision instanceof \WP_Post ) { |
| 99 | + WP_CLI::error( "Invalid 'from' ID {$from_id}." ); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Get the 'to' revision or post |
| 104 | + $to_revision = null; |
| 105 | + if ( $to_id ) { |
| 106 | + $to_revision = wp_get_post_revision( $to_id ); |
| 107 | + if ( ! $to_revision instanceof \WP_Post ) { |
| 108 | + // Try as a regular post |
| 109 | + $to_revision = get_post( $to_id ); |
| 110 | + if ( ! $to_revision instanceof \WP_Post ) { |
| 111 | + WP_CLI::error( "Invalid 'to' ID {$to_id}." ); |
| 112 | + } |
| 113 | + } |
| 114 | + } elseif ( 'revision' === $from_revision->post_type ) { |
| 115 | + // If no 'to' ID provided, use the parent post of the revision |
| 116 | + $to_revision = get_post( $from_revision->post_parent ); |
| 117 | + if ( ! $to_revision instanceof \WP_Post ) { |
| 118 | + WP_CLI::error( "Could not find parent post for revision {$from_id}." ); |
| 119 | + } |
| 120 | + } else { |
| 121 | + WP_CLI::error( "Please provide a 'to' revision ID when comparing posts." ); |
| 122 | + } |
| 123 | + |
| 124 | + // Validate field |
| 125 | + if ( ! property_exists( $from_revision, $field ) || ! property_exists( $to_revision, $field ) ) { |
| 126 | + WP_CLI::error( "Invalid field '{$field}'." ); |
| 127 | + } |
| 128 | + |
| 129 | + // Get the field values |
| 130 | + $left_string = $from_revision->{$field}; |
| 131 | + $right_string = $to_revision->{$field}; |
| 132 | + |
| 133 | + // Generate the diff |
| 134 | + $diff_args = [ |
| 135 | + 'title_left' => sprintf( |
| 136 | + '%s (%s) - ID %d', |
| 137 | + $from_revision->post_title, |
| 138 | + $from_revision->post_modified, |
| 139 | + $from_revision->ID |
| 140 | + ), |
| 141 | + 'title_right' => sprintf( |
| 142 | + '%s (%s) - ID %d', |
| 143 | + $to_revision->post_title, |
| 144 | + $to_revision->post_modified, |
| 145 | + $to_revision->ID |
| 146 | + ), |
| 147 | + ]; |
| 148 | + |
| 149 | + $diff = wp_text_diff( $left_string, $right_string, $diff_args ); |
| 150 | + |
| 151 | + if ( ! $diff ) { |
| 152 | + WP_CLI::success( 'No difference found.' ); |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + // Output the diff |
| 157 | + WP_CLI::line( $diff ); |
| 158 | + } |
| 159 | +} |
0 commit comments