[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-admin/includes/ -> revision.php (source)

   1  <?php
   2  /**
   3   * WordPress Administration Revisions API
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   * @since 3.6.0
   8   */
   9  
  10  /**
  11   * Get the revision UI diff.
  12   *
  13   * @since 3.6.0
  14   *
  15   * @param WP_Post|int $post         The post object or post ID.
  16   * @param int         $compare_from The revision ID to compare from.
  17   * @param int         $compare_to   The revision ID to come to.
  18   * @return array|false Associative array of a post's revisioned fields and their diffs.
  19   *                     Or, false on failure.
  20   */
  21  function wp_get_revision_ui_diff( $post, $compare_from, $compare_to ) {
  22      $post = get_post( $post );
  23      if ( ! $post ) {
  24          return false;
  25      }
  26  
  27      if ( $compare_from ) {
  28          $compare_from = get_post( $compare_from );
  29          if ( ! $compare_from ) {
  30              return false;
  31          }
  32      } else {
  33          // If we're dealing with the first revision...
  34          $compare_from = false;
  35      }
  36  
  37      $compare_to = get_post( $compare_to );
  38      if ( ! $compare_to ) {
  39          return false;
  40      }
  41  
  42      // If comparing revisions, make sure we're dealing with the right post parent.
  43      // The parent post may be a 'revision' when revisions are disabled and we're looking at autosaves.
  44      if ( $compare_from && $compare_from->post_parent !== $post->ID && $compare_from->ID !== $post->ID ) {
  45          return false;
  46      }
  47      if ( $compare_to->post_parent !== $post->ID && $compare_to->ID !== $post->ID ) {
  48          return false;
  49      }
  50  
  51      if ( $compare_from && strtotime( $compare_from->post_date_gmt ) > strtotime( $compare_to->post_date_gmt ) ) {
  52          $temp         = $compare_from;
  53          $compare_from = $compare_to;
  54          $compare_to   = $temp;
  55      }
  56  
  57      // Add default title if title field is empty.
  58      if ( $compare_from && empty( $compare_from->post_title ) ) {
  59          $compare_from->post_title = __( '(no title)' );
  60      }
  61      if ( empty( $compare_to->post_title ) ) {
  62          $compare_to->post_title = __( '(no title)' );
  63      }
  64  
  65      $return = array();
  66  
  67      foreach ( _wp_post_revision_fields( $post ) as $field => $name ) {
  68          /**
  69           * Contextually filter a post revision field.
  70           *
  71           * The dynamic portion of the hook name, `$field`, corresponds to a name of a
  72           * field of the revision object.
  73           *
  74           * Possible hook names include:
  75           *
  76           *  - `_wp_post_revision_field_post_title`
  77           *  - `_wp_post_revision_field_post_content`
  78           *  - `_wp_post_revision_field_post_excerpt`
  79           *
  80           * @since 3.6.0
  81           *
  82           * @param string  $revision_field The current revision field to compare to or from.
  83           * @param string  $field          The current revision field.
  84           * @param WP_Post $compare_from   The revision post object to compare to or from.
  85           * @param string  $context        The context of whether the current revision is the old
  86           *                                or the new one. Values are 'to' or 'from'.
  87           */
  88          $content_from = $compare_from ? apply_filters( "_wp_post_revision_field_{$field}", $compare_from->$field, $field, $compare_from, 'from' ) : '';
  89  
  90          /** This filter is documented in wp-admin/includes/revision.php */
  91          $content_to = apply_filters( "_wp_post_revision_field_{$field}", $compare_to->$field, $field, $compare_to, 'to' );
  92  
  93          $args = array(
  94              'show_split_view' => true,
  95              'title_left'      => __( 'Removed' ),
  96              'title_right'     => __( 'Added' ),
  97          );
  98  
  99          /**
 100           * Filters revisions text diff options.
 101           *
 102           * Filters the options passed to wp_text_diff() when viewing a post revision.
 103           *
 104           * @since 4.1.0
 105           *
 106           * @param array   $args {
 107           *     Associative array of options to pass to wp_text_diff().
 108           *
 109           *     @type bool $show_split_view True for split view (two columns), false for
 110           *                                 un-split view (single column). Default true.
 111           * }
 112           * @param string  $field        The current revision field.
 113           * @param WP_Post $compare_from The revision post to compare from.
 114           * @param WP_Post $compare_to   The revision post to compare to.
 115           */
 116          $args = apply_filters( 'revision_text_diff_options', $args, $field, $compare_from, $compare_to );
 117  
 118          $diff = wp_text_diff( $content_from, $content_to, $args );
 119  
 120          if ( ! $diff && 'post_title' === $field ) {
 121              // It's a better user experience to still show the Title, even if it didn't change.
 122              // No, you didn't see this.
 123              $diff = '<table class="diff"><colgroup><col class="content diffsplit left"><col class="content diffsplit middle"><col class="content diffsplit right"></colgroup><tbody><tr>';
 124  
 125              // In split screen mode, show the title before/after side by side.
 126              if ( true === $args['show_split_view'] ) {
 127                  $diff .= '<td>' . esc_html( $compare_from->post_title ) . '</td><td></td><td>' . esc_html( $compare_to->post_title ) . '</td>';
 128              } else {
 129                  $diff .= '<td>' . esc_html( $compare_from->post_title ) . '</td>';
 130  
 131                  // In single column mode, only show the title once if unchanged.
 132                  if ( $compare_from->post_title !== $compare_to->post_title ) {
 133                      $diff .= '</tr><tr><td>' . esc_html( $compare_to->post_title ) . '</td>';
 134                  }
 135              }
 136  
 137              $diff .= '</tr></tbody>';
 138              $diff .= '</table>';
 139          }
 140  
 141          if ( $diff ) {
 142              $return[] = array(
 143                  'id'   => $field,
 144                  'name' => $name,
 145                  'diff' => $diff,
 146              );
 147          }
 148      }
 149  
 150      /**
 151       * Filters the fields displayed in the post revision diff UI.
 152       *
 153       * @since 4.1.0
 154       *
 155       * @param array[] $return       Array of revision UI fields. Each item is an array of id, name, and diff.
 156       * @param WP_Post $compare_from The revision post to compare from.
 157       * @param WP_Post $compare_to   The revision post to compare to.
 158       */
 159      return apply_filters( 'wp_get_revision_ui_diff', $return, $compare_from, $compare_to );
 160  
 161  }
 162  
 163  /**
 164   * Prepare revisions for JavaScript.
 165   *
 166   * @since 3.6.0
 167   *
 168   * @param WP_Post|int $post                 The post object or post ID.
 169   * @param int         $selected_revision_id The selected revision ID.
 170   * @param int         $from                 Optional. The revision ID to compare from.
 171   * @return array An associative array of revision data and related settings.
 172   */
 173  function wp_prepare_revisions_for_js( $post, $selected_revision_id, $from = null ) {
 174      $post    = get_post( $post );
 175      $authors = array();
 176      $now_gmt = time();
 177  
 178      $revisions = wp_get_post_revisions(
 179          $post->ID,
 180          array(
 181              'order'         => 'ASC',
 182              'check_enabled' => false,
 183          )
 184      );
 185      // If revisions are disabled, we only want autosaves and the current post.
 186      if ( ! wp_revisions_enabled( $post ) ) {
 187          foreach ( $revisions as $revision_id => $revision ) {
 188              if ( ! wp_is_post_autosave( $revision ) ) {
 189                  unset( $revisions[ $revision_id ] );
 190              }
 191          }
 192          $revisions = array( $post->ID => $post ) + $revisions;
 193      }
 194  
 195      $show_avatars = get_option( 'show_avatars' );
 196  
 197      cache_users( wp_list_pluck( $revisions, 'post_author' ) );
 198  
 199      $can_restore = current_user_can( 'edit_post', $post->ID );
 200      $current_id  = false;
 201  
 202      foreach ( $revisions as $revision ) {
 203          $modified     = strtotime( $revision->post_modified );
 204          $modified_gmt = strtotime( $revision->post_modified_gmt . ' +0000' );
 205          if ( $can_restore ) {
 206              $restore_link = str_replace(
 207                  '&amp;',
 208                  '&',
 209                  wp_nonce_url(
 210                      add_query_arg(
 211                          array(
 212                              'revision' => $revision->ID,
 213                              'action'   => 'restore',
 214                          ),
 215                          admin_url( 'revision.php' )
 216                      ),
 217                      "restore-post_{$revision->ID}"
 218                  )
 219              );
 220          }
 221  
 222          if ( ! isset( $authors[ $revision->post_author ] ) ) {
 223              $authors[ $revision->post_author ] = array(
 224                  'id'     => (int) $revision->post_author,
 225                  'avatar' => $show_avatars ? get_avatar( $revision->post_author, 32 ) : '',
 226                  'name'   => get_the_author_meta( 'display_name', $revision->post_author ),
 227              );
 228          }
 229  
 230          $autosave = (bool) wp_is_post_autosave( $revision );
 231          $current  = ! $autosave && $revision->post_modified_gmt === $post->post_modified_gmt;
 232          if ( $current && ! empty( $current_id ) ) {
 233              // If multiple revisions have the same post_modified_gmt, highest ID is current.
 234              if ( $current_id < $revision->ID ) {
 235                  $revisions[ $current_id ]['current'] = false;
 236                  $current_id                          = $revision->ID;
 237              } else {
 238                  $current = false;
 239              }
 240          } elseif ( $current ) {
 241              $current_id = $revision->ID;
 242          }
 243  
 244          $revisions_data = array(
 245              'id'         => $revision->ID,
 246              'title'      => get_the_title( $post->ID ),
 247              'author'     => $authors[ $revision->post_author ],
 248              'date'       => date_i18n( __( 'M j, Y @ H:i' ), $modified ),
 249              'dateShort'  => date_i18n( _x( 'j M @ H:i', 'revision date short format' ), $modified ),
 250              /* translators: %s: Human-readable time difference. */
 251              'timeAgo'    => sprintf( __( '%s ago' ), human_time_diff( $modified_gmt, $now_gmt ) ),
 252              'autosave'   => $autosave,
 253              'current'    => $current,
 254              'restoreUrl' => $can_restore ? $restore_link : false,
 255          );
 256  
 257          /**
 258           * Filters the array of revisions used on the revisions screen.
 259           *
 260           * @since 4.4.0
 261           *
 262           * @param array   $revisions_data {
 263           *     The bootstrapped data for the revisions screen.
 264           *
 265           *     @type int        $id         Revision ID.
 266           *     @type string     $title      Title for the revision's parent WP_Post object.
 267           *     @type int        $author     Revision post author ID.
 268           *     @type string     $date       Date the revision was modified.
 269           *     @type string     $dateShort  Short-form version of the date the revision was modified.
 270           *     @type string     $timeAgo    GMT-aware amount of time ago the revision was modified.
 271           *     @type bool       $autosave   Whether the revision is an autosave.
 272           *     @type bool       $current    Whether the revision is both not an autosave and the post
 273           *                                  modified date matches the revision modified date (GMT-aware).
 274           *     @type bool|false $restoreUrl URL if the revision can be restored, false otherwise.
 275           * }
 276           * @param WP_Post $revision       The revision's WP_Post object.
 277           * @param WP_Post $post           The revision's parent WP_Post object.
 278           */
 279          $revisions[ $revision->ID ] = apply_filters( 'wp_prepare_revision_for_js', $revisions_data, $revision, $post );
 280      }
 281  
 282      /**
 283       * If we only have one revision, the initial revision is missing; This happens
 284       * when we have an autsosave and the user has clicked 'View the Autosave'
 285       */
 286      if ( 1 === count( $revisions ) ) {
 287          $revisions[ $post->ID ] = array(
 288              'id'         => $post->ID,
 289              'title'      => get_the_title( $post->ID ),
 290              'author'     => $authors[ $revision->post_author ],
 291              'date'       => date_i18n( __( 'M j, Y @ H:i' ), strtotime( $post->post_modified ) ),
 292              'dateShort'  => date_i18n( _x( 'j M @ H:i', 'revision date short format' ), strtotime( $post->post_modified ) ),
 293              /* translators: %s: Human-readable time difference. */
 294              'timeAgo'    => sprintf( __( '%s ago' ), human_time_diff( strtotime( $post->post_modified_gmt ), $now_gmt ) ),
 295              'autosave'   => false,
 296              'current'    => true,
 297              'restoreUrl' => false,
 298          );
 299          $current_id             = $post->ID;
 300      }
 301  
 302      /*
 303       * If a post has been saved since the last revision (no revisioned fields
 304       * were changed), we may not have a "current" revision. Mark the latest
 305       * revision as "current".
 306       */
 307      if ( empty( $current_id ) ) {
 308          if ( $revisions[ $revision->ID ]['autosave'] ) {
 309              $revision = end( $revisions );
 310              while ( $revision['autosave'] ) {
 311                  $revision = prev( $revisions );
 312              }
 313              $current_id = $revision['id'];
 314          } else {
 315              $current_id = $revision->ID;
 316          }
 317          $revisions[ $current_id ]['current'] = true;
 318      }
 319  
 320      // Now, grab the initial diff.
 321      $compare_two_mode = is_numeric( $from );
 322      if ( ! $compare_two_mode ) {
 323          $found = array_search( $selected_revision_id, array_keys( $revisions ), true );
 324          if ( $found ) {
 325              $from = array_keys( array_slice( $revisions, $found - 1, 1, true ) );
 326              $from = reset( $from );
 327          } else {
 328              $from = 0;
 329          }
 330      }
 331  
 332      $from = absint( $from );
 333  
 334      $diffs = array(
 335          array(
 336              'id'     => $from . ':' . $selected_revision_id,
 337              'fields' => wp_get_revision_ui_diff( $post->ID, $from, $selected_revision_id ),
 338          ),
 339      );
 340  
 341      return array(
 342          'postId'         => $post->ID,
 343          'nonce'          => wp_create_nonce( 'revisions-ajax-nonce' ),
 344          'revisionData'   => array_values( $revisions ),
 345          'to'             => $selected_revision_id,
 346          'from'           => $from,
 347          'diffData'       => $diffs,
 348          'baseUrl'        => parse_url( admin_url( 'revision.php' ), PHP_URL_PATH ),
 349          'compareTwoMode' => absint( $compare_two_mode ), // Apparently booleans are not allowed.
 350          'revisionIds'    => array_keys( $revisions ),
 351      );
 352  }
 353  
 354  /**
 355   * Print JavaScript templates required for the revisions experience.
 356   *
 357   * @since 4.1.0
 358   *
 359   * @global WP_Post $post Global post object.
 360   */
 361  function wp_print_revision_templates() {
 362      global $post;
 363      ?><script id="tmpl-revisions-frame" type="text/html">
 364          <div class="revisions-control-frame"></div>
 365          <div class="revisions-diff-frame"></div>
 366      </script>
 367  
 368      <script id="tmpl-revisions-buttons" type="text/html">
 369          <div class="revisions-previous">
 370              <input class="button" type="button" value="<?php echo esc_attr_x( 'Previous', 'Button label for a previous revision' ); ?>" />
 371          </div>
 372  
 373          <div class="revisions-next">
 374              <input class="button" type="button" value="<?php echo esc_attr_x( 'Next', 'Button label for a next revision' ); ?>" />
 375          </div>
 376      </script>
 377  
 378      <script id="tmpl-revisions-checkbox" type="text/html">
 379          <div class="revision-toggle-compare-mode">
 380              <label>
 381                  <input type="checkbox" class="compare-two-revisions"
 382                  <#
 383                  if ( 'undefined' !== typeof data && data.model.attributes.compareTwoMode ) {
 384                      #> checked="checked"<#
 385                  }
 386                  #>
 387                  />
 388                  <?php esc_html_e( 'Compare any two revisions' ); ?>
 389              </label>
 390          </div>
 391      </script>
 392  
 393      <script id="tmpl-revisions-meta" type="text/html">
 394          <# if ( ! _.isUndefined( data.attributes ) ) { #>
 395              <div class="diff-title">
 396                  <# if ( 'from' === data.type ) { #>
 397                      <strong><?php _ex( 'From:', 'Followed by post revision info' ); ?></strong>
 398                  <# } else if ( 'to' === data.type ) { #>
 399                      <strong><?php _ex( 'To:', 'Followed by post revision info' ); ?></strong>
 400                  <# } #>
 401                  <div class="author-card<# if ( data.attributes.autosave ) { #> autosave<# } #>">
 402                      {{{ data.attributes.author.avatar }}}
 403                      <div class="author-info">
 404                      <# if ( data.attributes.autosave ) { #>
 405                          <span class="byline">
 406                          <?php
 407                          printf(
 408                              /* translators: %s: User's display name. */
 409                              __( 'Autosave by %s' ),
 410                              '<span class="author-name">{{ data.attributes.author.name }}</span>'
 411                          );
 412                          ?>
 413                              </span>
 414                      <# } else if ( data.attributes.current ) { #>
 415                          <span class="byline">
 416                          <?php
 417                          printf(
 418                              /* translators: %s: User's display name. */
 419                              __( 'Current Revision by %s' ),
 420                              '<span class="author-name">{{ data.attributes.author.name }}</span>'
 421                          );
 422                          ?>
 423                              </span>
 424                      <# } else { #>
 425                          <span class="byline">
 426                          <?php
 427                          printf(
 428                              /* translators: %s: User's display name. */
 429                              __( 'Revision by %s' ),
 430                              '<span class="author-name">{{ data.attributes.author.name }}</span>'
 431                          );
 432                          ?>
 433                              </span>
 434                      <# } #>
 435                          <span class="time-ago">{{ data.attributes.timeAgo }}</span>
 436                          <span class="date">({{ data.attributes.dateShort }})</span>
 437                      </div>
 438                  <# if ( 'to' === data.type && data.attributes.restoreUrl ) { #>
 439                      <input  <?php if ( wp_check_post_lock( $post->ID ) ) { ?>
 440                          disabled="disabled"
 441                      <?php } else { ?>
 442                          <# if ( data.attributes.current ) { #>
 443                              disabled="disabled"
 444                          <# } #>
 445                      <?php } ?>
 446                      <# if ( data.attributes.autosave ) { #>
 447                          type="button" class="restore-revision button button-primary" value="<?php esc_attr_e( 'Restore This Autosave' ); ?>" />
 448                      <# } else { #>
 449                          type="button" class="restore-revision button button-primary" value="<?php esc_attr_e( 'Restore This Revision' ); ?>" />
 450                      <# } #>
 451                  <# } #>
 452              </div>
 453          <# if ( 'tooltip' === data.type ) { #>
 454              <div class="revisions-tooltip-arrow"><span></span></div>
 455          <# } #>
 456      <# } #>
 457      </script>
 458  
 459      <script id="tmpl-revisions-diff" type="text/html">
 460          <div class="loading-indicator"><span class="spinner"></span></div>
 461          <div class="diff-error"><?php _e( 'Sorry, something went wrong. The requested comparison could not be loaded.' ); ?></div>
 462          <div class="diff">
 463          <# _.each( data.fields, function( field ) { #>
 464              <h3>{{ field.name }}</h3>
 465              {{{ field.diff }}}
 466          <# }); #>
 467          </div>
 468      </script>
 469      <?php
 470  }


Generated: Sat Nov 23 01:00:02 2024 Cross-referenced by PHPXref 0.7.1