wp/wp-admin/revision.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
     5  * Requires wp-admin/includes/revision.php.
     5  * Requires wp-admin/includes/revision.php.
     6  *
     6  *
     7  * @package WordPress
     7  * @package WordPress
     8  * @subpackage Administration
     8  * @subpackage Administration
     9  * @since 2.6.0
     9  * @since 2.6.0
    10  *
       
    11  * @param int    revision Optional. The revision ID.
       
    12  * @param string action   The action to take.
       
    13  *                        Accepts 'restore', 'view' or 'edit'.
       
    14  * @param int    from     The revision to compare from.
       
    15  * @param int    to       Optional, required if revision missing. The revision to compare to.
       
    16  */
    10  */
    17 
    11 
    18 /** WordPress Administration Bootstrap */
    12 /** WordPress Administration Bootstrap */
    19 require_once( dirname( __FILE__ ) . '/admin.php' );
    13 require_once( dirname( __FILE__ ) . '/admin.php' );
    20 
    14 
    21 require ABSPATH . 'wp-admin/includes/revision.php';
    15 require ABSPATH . 'wp-admin/includes/revision.php';
    22 
    16 
       
    17 /**
       
    18  * @global int    $revision Optional. The revision ID.
       
    19  * @global string $action   The action to take.
       
    20  *                          Accepts 'restore', 'view' or 'edit'.
       
    21  * @global int    $from     The revision to compare from.
       
    22  * @global int    $to       Optional, required if revision missing. The revision to compare to.
       
    23  */
    23 wp_reset_vars( array( 'revision', 'action', 'from', 'to' ) );
    24 wp_reset_vars( array( 'revision', 'action', 'from', 'to' ) );
    24 
    25 
    25 $revision_id = absint( $revision );
    26 $revision_id = absint( $revision );
    26 
    27 
    27 $from = is_numeric( $from ) ? absint( $from ) : null;
    28 $from = is_numeric( $from ) ? absint( $from ) : null;
    28 if ( ! $revision_id )
    29 if ( ! $revision_id ) {
    29 	$revision_id = absint( $to );
    30 	$revision_id = absint( $to );
       
    31 }
    30 $redirect = 'edit.php';
    32 $redirect = 'edit.php';
    31 
    33 
    32 switch ( $action ) {
    34 switch ( $action ) {
    33 case 'restore' :
    35 	case 'restore':
    34 	if ( ! $revision = wp_get_post_revision( $revision_id ) )
    36 		if ( ! $revision = wp_get_post_revision( $revision_id ) ) {
       
    37 			break;
       
    38 		}
       
    39 
       
    40 		if ( ! current_user_can( 'edit_post', $revision->post_parent ) ) {
       
    41 			break;
       
    42 		}
       
    43 
       
    44 		if ( ! $post = get_post( $revision->post_parent ) ) {
       
    45 			break;
       
    46 		}
       
    47 
       
    48 		// Restore if revisions are enabled or this is an autosave.
       
    49 		if ( ! wp_revisions_enabled( $post ) && ! wp_is_post_autosave( $revision ) ) {
       
    50 			$redirect = 'edit.php?post_type=' . $post->post_type;
       
    51 			break;
       
    52 		}
       
    53 
       
    54 		// Don't allow revision restore when post is locked
       
    55 		if ( wp_check_post_lock( $post->ID ) ) {
       
    56 			break;
       
    57 		}
       
    58 
       
    59 		check_admin_referer( "restore-post_{$revision->ID}" );
       
    60 
       
    61 		wp_restore_post_revision( $revision->ID );
       
    62 		$redirect = add_query_arg(
       
    63 			array(
       
    64 				'message'  => 5,
       
    65 				'revision' => $revision->ID,
       
    66 			),
       
    67 			get_edit_post_link( $post->ID, 'url' )
       
    68 		);
    35 		break;
    69 		break;
       
    70 	case 'view':
       
    71 	case 'edit':
       
    72 	default:
       
    73 		if ( ! $revision = wp_get_post_revision( $revision_id ) ) {
       
    74 			break;
       
    75 		}
       
    76 		if ( ! $post = get_post( $revision->post_parent ) ) {
       
    77 			break;
       
    78 		}
    36 
    79 
    37 	if ( ! current_user_can( 'edit_post', $revision->post_parent ) )
    80 		if ( ! current_user_can( 'read_post', $revision->ID ) || ! current_user_can( 'edit_post', $revision->post_parent ) ) {
       
    81 			break;
       
    82 		}
       
    83 
       
    84 		// Revisions disabled and we're not looking at an autosave
       
    85 		if ( ! wp_revisions_enabled( $post ) && ! wp_is_post_autosave( $revision ) ) {
       
    86 			$redirect = 'edit.php?post_type=' . $post->post_type;
       
    87 			break;
       
    88 		}
       
    89 
       
    90 		$post_edit_link = get_edit_post_link();
       
    91 		$post_title     = '<a href="' . $post_edit_link . '">' . _draft_or_post_title() . '</a>';
       
    92 		/* translators: %s: post title */
       
    93 		$h1             = sprintf( __( 'Compare Revisions of &#8220;%s&#8221;' ), $post_title );
       
    94 		$return_to_post = '<a href="' . $post_edit_link . '">' . __( '&larr; Return to editor' ) . '</a>';
       
    95 		$title          = __( 'Revisions' );
       
    96 
       
    97 		$redirect = false;
    38 		break;
    98 		break;
    39 
       
    40 	if ( ! $post = get_post( $revision->post_parent ) )
       
    41 		break;
       
    42 
       
    43 	// Restore if revisions are enabled or this is an autosave.
       
    44 	if ( ! wp_revisions_enabled( $post ) && ! wp_is_post_autosave( $revision ) ) {
       
    45 		$redirect = 'edit.php?post_type=' . $post->post_type;
       
    46 		break;
       
    47 	}
       
    48 
       
    49 	// Don't allow revision restore when post is locked
       
    50 	if ( wp_check_post_lock( $post->ID ) )
       
    51 		break;
       
    52 
       
    53 	check_admin_referer( "restore-post_{$revision->ID}" );
       
    54 
       
    55 	wp_restore_post_revision( $revision->ID );
       
    56 	$redirect = add_query_arg( array( 'message' => 5, 'revision' => $revision->ID ), get_edit_post_link( $post->ID, 'url' ) );
       
    57 	break;
       
    58 case 'view' :
       
    59 case 'edit' :
       
    60 default :
       
    61 	if ( ! $revision = wp_get_post_revision( $revision_id ) )
       
    62 		break;
       
    63 	if ( ! $post = get_post( $revision->post_parent ) )
       
    64 		break;
       
    65 
       
    66 	if ( ! current_user_can( 'read_post', $revision->ID ) || ! current_user_can( 'edit_post', $revision->post_parent ) )
       
    67 		break;
       
    68 
       
    69 	// Revisions disabled and we're not looking at an autosave
       
    70 	if ( ! wp_revisions_enabled( $post ) && ! wp_is_post_autosave( $revision ) ) {
       
    71 		$redirect = 'edit.php?post_type=' . $post->post_type;
       
    72 		break;
       
    73 	}
       
    74 
       
    75 	$post_edit_link = get_edit_post_link();
       
    76 	$post_title     = '<a href="' . $post_edit_link . '">' . _draft_or_post_title() . '</a>';
       
    77 	/* translators: 1: Post title */
       
    78 	$h1             = sprintf( __( 'Compare Revisions of &#8220;%1$s&#8221;' ), $post_title );
       
    79 	$return_to_post = '<a href="' . $post_edit_link . '">' . __( '&larr; Return to editor' ) . '</a>';
       
    80 	$title          = __( 'Revisions' );
       
    81 
       
    82 	$redirect = false;
       
    83 	break;
       
    84 }
    99 }
    85 
   100 
    86 // Empty post_type means either malformed object found, or no valid parent was found.
   101 // Empty post_type means either malformed object found, or no valid parent was found.
    87 if ( ! $redirect && empty( $post->post_type ) )
   102 if ( ! $redirect && empty( $post->post_type ) ) {
    88 	$redirect = 'edit.php';
   103 	$redirect = 'edit.php';
       
   104 }
    89 
   105 
    90 if ( ! empty( $redirect ) ) {
   106 if ( ! empty( $redirect ) ) {
    91 	wp_redirect( $redirect );
   107 	wp_redirect( $redirect );
    92 	exit;
   108 	exit;
    93 }
   109 }
    94 
   110 
    95 // This is so that the correct "Edit" menu item is selected.
   111 // This is so that the correct "Edit" menu item is selected.
    96 if ( ! empty( $post->post_type ) && 'post' != $post->post_type )
   112 if ( ! empty( $post->post_type ) && 'post' != $post->post_type ) {
    97 	$parent_file = $submenu_file = 'edit.php?post_type=' . $post->post_type;
   113 	$parent_file = $submenu_file = 'edit.php?post_type=' . $post->post_type;
    98 else
   114 } else {
    99 	$parent_file = $submenu_file = 'edit.php';
   115 	$parent_file = $submenu_file = 'edit.php';
       
   116 }
   100 
   117 
   101 wp_enqueue_script( 'revisions' );
   118 wp_enqueue_script( 'revisions' );
   102 wp_localize_script( 'revisions', '_wpRevisionsSettings', wp_prepare_revisions_for_js( $post, $revision_id, $from ) );
   119 wp_localize_script( 'revisions', '_wpRevisionsSettings', wp_prepare_revisions_for_js( $post, $revision_id, $from ) );
   103 
   120 
   104 /* Revisions Help Tab */
   121 /* Revisions Help Tab */
   108 $revisions_overview .= '<p>' . __( 'From this screen you can review, compare, and restore revisions:' ) . '</p>';
   125 $revisions_overview .= '<p>' . __( 'From this screen you can review, compare, and restore revisions:' ) . '</p>';
   109 $revisions_overview .= '<ul><li>' . __( 'To navigate between revisions, <strong>drag the slider handle left or right</strong> or <strong>use the Previous or Next buttons</strong>.' ) . '</li>';
   126 $revisions_overview .= '<ul><li>' . __( 'To navigate between revisions, <strong>drag the slider handle left or right</strong> or <strong>use the Previous or Next buttons</strong>.' ) . '</li>';
   110 $revisions_overview .= '<li>' . __( 'Compare two different revisions by <strong>selecting the &#8220;Compare any two revisions&#8221; box</strong> to the side.' ) . '</li>';
   127 $revisions_overview .= '<li>' . __( 'Compare two different revisions by <strong>selecting the &#8220;Compare any two revisions&#8221; box</strong> to the side.' ) . '</li>';
   111 $revisions_overview .= '<li>' . __( 'To restore a revision, <strong>click Restore This Revision</strong>.' ) . '</li></ul>';
   128 $revisions_overview .= '<li>' . __( 'To restore a revision, <strong>click Restore This Revision</strong>.' ) . '</li></ul>';
   112 
   129 
   113 get_current_screen()->add_help_tab( array(
   130 get_current_screen()->add_help_tab(
   114 	'id'      => 'revisions-overview',
   131 	array(
   115 	'title'   => __( 'Overview' ),
   132 		'id'      => 'revisions-overview',
   116 	'content' => $revisions_overview
   133 		'title'   => __( 'Overview' ),
   117 ) );
   134 		'content' => $revisions_overview,
       
   135 	)
       
   136 );
   118 
   137 
   119 $revisions_sidebar  = '<p><strong>' . __( 'For more information:' ) . '</strong></p>';
   138 $revisions_sidebar  = '<p><strong>' . __( 'For more information:' ) . '</strong></p>';
   120 $revisions_sidebar .= '<p>' . __( '<a href="https://codex.wordpress.org/Revision_Management">Revisions Management</a>' ) . '</p>';
   139 $revisions_sidebar .= '<p>' . __( '<a href="https://codex.wordpress.org/Revision_Management">Revisions Management</a>' ) . '</p>';
   121 $revisions_sidebar .= '<p>' . __( '<a href="https://wordpress.org/support/">Support Forums</a>' ) . '</p>';
   140 $revisions_sidebar .= '<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>';
   122 
   141 
   123 get_current_screen()->set_help_sidebar( $revisions_sidebar );
   142 get_current_screen()->set_help_sidebar( $revisions_sidebar );
   124 
   143 
   125 require_once( ABSPATH . 'wp-admin/admin-header.php' );
   144 require_once( ABSPATH . 'wp-admin/admin-header.php' );
   126 
   145