WordPress.org

Make WordPress Core


Ignore:
Timestamp:
03/27/2013 11:56:28 AM (7 years ago)
Author:
markjaquith
Message:

Minor revisions PHP reorg, code cleanup, restores _post_restored_from functionality.

props adamsilverstein. see #23497

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/revision.php

    r23769 r23811  
    143143 * @since 2.6.0
    144144 * @uses wp_get_post_revisions()
    145  * 
     145 *
    146146 * @param int $post_id The post ID.
    147147 * @param int $user_id optional The post author ID.
     
    317317        do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] );
    318318
     319    $restore_details = array(
     320        'restored_revision_id' => $revision_id,
     321        'restored_by_user' => get_current_user_id(),
     322        'restored_time' => time()
     323    );
     324    update_post_meta( $post_id, '_post_restored_from', $restore_details );
     325
    319326    return $post_id;
    320327}
     
    418425function wp_first_revision_matches_current_version( $post ) {
    419426
    420         if ( ! $post = get_post( $post ) )
    421                 return false;
    422 
    423         if ( ! $revisions = wp_get_post_revisions( $post->ID ) )
    424                 return false;
    425 
    426         $last_revision = array_shift( $revisions );
    427 
    428         if ( ! ($last_revision->post_modified == $post->post_modified ) )
    429                 return false;
    430 
    431         return true;
    432 }
     427    if ( ! $post = get_post( $post ) )
     428        return false;
     429
     430    if ( ! $revisions = wp_get_post_revisions( $post->ID ) )
     431        return false;
     432
     433    $last_revision = array_shift( $revisions );
     434
     435    if ( ! ($last_revision->post_modified == $post->post_modified ) )
     436        return false;
     437
     438    return true;
     439}
     440
     441/**
     442 * Displays a human readable HTML representation of the difference between two strings.
     443 * similar to wp_text_diff, but tracks and returns could of lines added and removed
     444 *
     445 * @since 3.6
     446 * @see wp_parse_args() Used to change defaults to user defined settings.
     447 * @uses Text_Diff
     448 * @uses WP_Text_Diff_Renderer_Table
     449 *
     450 * @param string $left_string "old" (left) version of string
     451 * @param string $right_string "new" (right) version of string
     452 * @param string|array $args Optional. Change 'title', 'title_left', and 'title_right' defaults.
     453 * @return array contains html, linesadded & linesdeletd, empty string if strings are equivalent.
     454 */
     455function wp_text_diff_with_count( $left_string, $right_string, $args = null ) {
     456    $defaults = array( 'title' => '', 'title_left' => '', 'title_right' => '' );
     457    $args = wp_parse_args( $args, $defaults );
     458
     459    if ( !class_exists( 'WP_Text_Diff_Renderer_Table' ) )
     460            require( ABSPATH . WPINC . '/wp-diff.php' );
     461
     462    $left_string  = normalize_whitespace( $left_string );
     463    $right_string = normalize_whitespace( $right_string );
     464
     465    $left_lines  = explode( "\n", $left_string );
     466    $right_lines = explode( "\n", $right_string) ;
     467
     468    $text_diff = new Text_Diff($left_lines, $right_lines  );
     469    $linesadded = $text_diff->countAddedLines();
     470    $linesdeleted = $text_diff->countDeletedLines();
     471
     472    $renderer  = new WP_Text_Diff_Renderer_Table();
     473    $diff = $renderer->render( $text_diff );
     474
     475    if ( !$diff )
     476            return '';
     477
     478        $r  = "<table class='diff'>\n";
     479
     480    if ( ! empty( $args[ 'show_split_view' ] ) ) {
     481        $r .= "<col class='content diffsplit left' /><col class='content diffsplit middle' /><col class='content diffsplit right' />";
     482    } else {
     483        $r .= "<col class='content' />";
     484    }
     485
     486    if ( $args['title'] || $args['title_left'] || $args['title_right'] )
     487        $r .= "<thead>";
     488    if ( $args['title'] )
     489        $r .= "<tr class='diff-title'><th colspan='4'>$args[title]</th></tr>\n";
     490    if ( $args['title_left'] || $args['title_right'] ) {
     491        $r .= "<tr class='diff-sub-title'>\n";
     492        $r .= "\t<td></td><th>$args[title_left]</th>\n";
     493        $r .= "\t<td></td><th>$args[title_right]</th>\n";
     494        $r .= "</tr>\n";
     495    }
     496    if ( $args['title'] || $args['title_left'] || $args['title_right'] )
     497        $r .= "</thead>\n";
     498
     499    $r .= "<tbody>\n$diff\n</tbody>\n";
     500    $r .= "</table>";
     501
     502    return array( 'html' => $r, 'linesadded' => $linesadded, 'linesdeleted' => $linesdeleted );
     503    }
Note: See TracChangeset for help on using the changeset viewer.