Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
232 changes: 196 additions & 36 deletions revision-history/revision_history.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/*
Plugin Name: Show Revision History
Plugin URI: http://keyes.ie/wordpress/show-revision-history
Plugin Name: Revision History
Plugin URI: http://keyes.ie/wordpress/revision-history
Description: Allow any visitor to your site to view the revisions of posts and/or pages.
Version: 0.1
Version: 0.9.2
Author: John Keyes
Author URI: http://keyes.ie
*/
Expand Down Expand Up @@ -33,33 +33,89 @@
add_action('wp', 'check_for_revision');

function check_for_revision($wp) {
$adjust_title = get_option( 'rh_adjust_title' );

if ( $adjust_title ) {
$title_class = get_option( 'rh_title_class' );
$rev_date = rh_revision_str();

if (valid_revision_id()) {
$revision = $_GET['revision'];
$post = get_post($revision);
if ($post != null) {
# reset posts so the revision is displayed instead of the original
global $posts;
$posts = array($post);
}
}
else {
$post = get_post(get_the_ID());
}

if ($title_class) {
$post->post_title .= "<span class=\"$title_class\">";
}
$post->post_title .= " (Revision: " . $rev_date . ")";
if ($title_class) {
$post->post_title .= "</span>";
}
}
}

function rh_get_the_revision_post() {
if (valid_revision_id()) {
$revision = $_GET['revision'];
$post = get_post($revision);
if ($post != null) {
# reset posts so the revision is displayed instead of the original
global $posts;
$posts = array($post);
} else {
$post = get_post(get_the_ID());
}
return $post;
}

function rh_revision_str($post = null) {
if ($post == null) {
if (valid_revision_id()) {
$post = rh_get_the_revision_post($revision);
}
}
if ($post != null) {
$modified = strtotime($post->post_modified_gmt . ' +0000');
return sprintf('%s at %s', date(get_option('date_format'), $modified), date(get_option('time_format'), $modified));
} else {
return "Latest";
}
}

add_filter('the_content', 'display_post_revisions');
function rh_the_revision($before = '', $after = '') {
echo $before . rh_revision_str() . $after;
}


if (get_option( 'rh_use_shortcode' ) == '0'){
add_filter('the_content', 'display_post_revisions');
};


add_shortcode("revisions", "display_post_revisions");

function display_post_revisions($content) {
$post = get_post(get_the_ID());

// if we are visiting a revision we need to adjust the post
// so we can still get the revision history.
if ( $post && $post->post_type == "revision" ) {
$post = get_post($post->post_parent);
}
$page_name = 'show_page_revisions';
$post_name = 'show_post_revisions';
$page_name = 'rh_show_page_revisions';
$post_name = 'rh_show_post_revisions';
$shortcode_name = 'rh_use_shortcode';
$page_val = get_option( $page_name );
$post_val = get_option( $post_name );

if ( $post && (( $post->post_type == "post" && $post_val) ||
$shortcode_val = get_option( $shortcode_name );



if ($post && (( $post->post_type == "post" && $post_val) ||
($post->post_type == "page" && $page_val)) ) {
$revisions = list_post_revisions($post);
if ( $revisions ) {
Expand All @@ -73,19 +129,38 @@ function display_post_revisions($content) {
}





function list_post_revisions( $post ) {
if ( $revisions = wp_get_post_revisions( $post->ID ) ) {
$items = '';
$autosave_name = 'rh_show_autosaves';
$show_autosaves = get_option( $autosave_name );
$revision_id = (valid_revision_id()) ? $revision_id = $_GET['revision'] : $post->ID;
$current_revision_date = rh_revision_str($post);
$current_revision_author = get_author_name($post->post_author);
$current_revision_url = get_permalink($post);
if ($revision_id == $post->ID) {
$items .= "<li>$current_revision_date by $current_revision_author (<em>displayed above</em>)</li>";
} else {
$items .= "<li><a href=\"$current_revision_url\">$current_revision_date</a> by $current_revision_author</li>";
}
foreach ( $revisions as $revision ) {
$date = wp_post_revision_title( $revision, 0 );
$is_autosave = wp_is_post_autosave($revision);
if ( !$show_autosaves && $is_autosave ) {
continue;
}
$rev_date = rh_revision_str($revision);
$name = get_author_name( $revision->post_author );
$query_string = get_query_string($revision);
$items .= "<li>";
if ($revision_id == $revision->ID) {
$items .= "$date by $name (<em>displayed above</em>)";
$items .= "$rev_date by $name (<em>displayed above</em>)";
} else {
$items .= "<a href=\"$query_string\">$date</a> by $name";
$items .= "<a href=\"$query_string\">$rev_date</a> by $name";
}
if ($is_autosave) {
$items .= " [autosave]";
}
$items .= "</li>";
}
Expand All @@ -107,8 +182,12 @@ function get_query_string($revision) {
return $query_string;
}

add_option('show_page_revisions', '0');
add_option('show_post_revisions', '0');
add_option('rh_show_page_revisions', '0');
add_option('rh_show_post_revisions', '0');
add_option('rh_show_autosaves', '0');
add_option('rh_adjust_title', '0');
add_option('rh_title_class', 'rh-title');
add_option('rh_use_shortcode', '0');

// Hook for adding admin menus
add_action('admin_menu', 'add_revision_history');
Expand All @@ -121,22 +200,33 @@ function add_revision_history() {

function add_revision_history_options_page() {
// variables for the field and option names
$page_name = 'show_page_revisions';
$post_name = 'show_post_revisions';
$page_field_name = 'show_page_revisions';
$post_field_name = 'show_post_revisions';
$page_name = 'rh_show_page_revisions';
$post_name = 'rh_show_post_revisions';
$shortcode_name = 'rh_use_shortcode';
$autosaves_name = 'rh_show_autosaves';
$adjust_title_name = 'rh_adjust_title';
$title_class_name = 'rh_title_class';
$hidden_field_name = 'submit_hidden';

// get the current values
$page_val = get_option( $page_name );
$post_val = get_option( $post_name );
$shortcode_val = get_option( $shortcode_name );
$autosaves_val = get_option( $autosaves_name );
$adjust_title_val = get_option( $adjust_title_name );
$title_class_val = get_option( $title_class_name );

// See if the user has posted us some information
// If they did, this hidden field will be set to 'Y'
if( $_POST[ $hidden_field_name ] == 'Y' ) {
// get the values from the POST
$new_page_val = ($_POST[ $page_field_name ] == "on") ? "1" : "0";
$new_post_val = ($_POST[ $post_field_name ] == "on") ? "1" : "0";
$new_page_val = ($_POST[ $page_name ] == "on") ? "1" : "0";
$new_post_val = ($_POST[ $post_name ] == "on") ? "1" : "0";
$new_shortcode_val = ($_POST[ $shortcode_name ] == "on") ? "1" : "0";
$new_autosaves_val = ($_POST[ $autosaves_name ] == "on") ? "1" : "0";
$new_adjust_title_val = ($_POST[ $adjust_title_name ] == "on") ? "1" : "0";
$new_title_class_val = ($_POST[ $title_class_name ] == "") ? "" : $_POST[ $title_class_name ];

// save the new values
if ( $new_page_val != $page_val ) {
update_option( $page_name, $new_page_val );
Expand All @@ -146,27 +236,97 @@ function add_revision_history_options_page() {
update_option( $post_name, $new_post_val );
$post_val = $new_post_val;
}
if ( $new_shortcode_val != $shortcode_val ) {
update_option( $shortcode_name, $new_shortcode_val );
$shortcode_val = $new_shortcode_val;
}
if ( $new_autosaves_val != $autosaves_val ) {
update_option( $autosaves_name, $new_autosaves_val );
$autosaves_val = $new_autosaves_val;
}
if ( $new_adjust_title_val != $adjust_title_val ) {
update_option( $adjust_title_name, $new_adjust_title_val );
$adjust_title_val = $new_adjust_title_val;
}
if ( $new_title_class_val != $title_class_val ) {
update_option( $title_class_name, $new_title_class_val );
$title_class_val = $new_title_class_val;
}
// Feedback that we've updated the options
?>
<div class="updated"><p><strong><?php _e('Options saved.', 'mt_trans_domain' ); ?></strong></p></div>
<?php
} // END CHECKING POST
?>
<div class="wrap">
<?php echo "<h2>" . __( 'Revision History Options', 'mt_trans_domain' ) . "</h2>"; ?>
<?php echo "<h2>" . __( 'Revision History Settings', 'mt_trans_domain' ) . "</h2>"; ?>

<form name="show_revision_history" method="post" action="">
<input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y">
<p>
<input id="show_on_pages" type="checkbox" name="<?php echo $page_field_name; ?>"
<?php checked('1', $page_val); ?> />
<label for="show_on_pages"><?php _e("Show revision history on pages.", 'mt_trans_domain' ); ?></label>
</p>
<p>
<input id="show_on_posts" type="checkbox" name="<?php echo $post_field_name; ?>"
<?php checked('1', $post_val); ?> />
<label for="show_on_posts"><?php _e("Show revision history on posts.", 'mt_trans_domain' ); ?></label>
</p>
<table class="form-table">
<tbody>
<tr valign="top">
<th scope="row">
<span>Page revisions</span>
</th>
<td>
<input id="rh_show_on_pages" type="checkbox" name="<?php echo $page_name; ?>"
<?php checked('1', $page_val); ?> />
<label for="rh_show_on_pages"><?php _e("Show revision history on pages.", 'mt_trans_domain' ); ?></label>
</td>
</tr>
<tr valign="top">
<th scope="row">
<span>Post revisions</span>
</th>
<td>
<input id="rh_show_on_posts" type="checkbox" name="<?php echo $post_name; ?>"
<?php checked('1', $post_val); ?> />
<label for="rh_show_on_posts"><?php _e("Show revision history on posts.", 'mt_trans_domain' ); ?></label>
</td>
</tr>
<tr valign="top">
<th scope="row">
<span>Autosave revisions</span>
</th>
<td>
<input id="rh_show_autosaves" type="checkbox" name="<?php echo $autosaves_name; ?>"
<?php checked('1', $autosaves_val); ?> />
<label for="rh_show_autosaves"><?php _e("Show autosaves.", 'mt_trans_domain' ); ?></label>
</td>
</tr>
<tr valign="top">
<th scope="row">
<span>Modify post title</span>
</th>
<td>
<input id="rh_adjust_title" type="checkbox" name="<?php echo $adjust_title_name; ?>"
<?php checked('1', $adjust_title_val); ?> />
<label for="rh_adjust_title"><?php _e("Show revision date in post title of revisions.", 'mt_trans_domain' ); ?></label>
<span class="description">&nbsp;If you need more control use <code>rh_the_revision</code> in your theme.</span>
</td>
</tr>
<tr valign="top">
<th scope="row">
<label for="rh_title_class"><?php _e("Title Class", 'mt_trans_domain' ); ?></label>
</th>
<td>
<input id="rh_title_class" type="text" name="<?php echo $title_class_name; ?>" value="<?php echo $title_class_val; ?>" class="regular-text code"/>
<span class="description">A child span is added to the post header, with the specified class.</span>
</td>
</tr>
<tr valign="top">
<th scope="row">
<span>Use Shortcode</span>
</th>
<td>
<input id="rh_use_shortcode" type="checkbox" name="<?php echo $shortcode_name; ?>"
<?php checked('1', $shortcode_val); ?> />
<label for="rh_use_shortcode"><?php _e("Use Shortcode <code>[revisions]</code> to display revisions instead of auto adding to page or post.", 'mt_trans_domain' ); ?></label>
</td>
</tr>
</tbody>
</table>
<p class="submit">
<input type="submit" name="Submit" value="<?php _e('Update Options', 'mt_trans_domain' ) ?>" />
</p>
Expand Down