Introduce Revisioning for custom Post Types, Introduce 'supports' argument to register_post_type() to wrap add_post_type_support(), Whitespace additions to create_initial_post_types(). See #9674. Fixes #11703

git-svn-id: http://svn.automattic.com/wordpress/trunk@12751 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
dd32 2010-01-18 11:44:51 +00:00
parent e0872e840c
commit 7f34057330
4 changed files with 101 additions and 44 deletions

View File

@ -135,7 +135,7 @@ if ( $post->post_author && !in_array($post->post_author, $authors) )
if ( $authors && count( $authors ) > 1 )
add_meta_box('authordiv', __('Author'), 'post_author_meta_box', $post_type, 'normal', 'core');
if ( 0 < $post_ID && wp_get_post_revisions( $post_ID ) )
if ( post_type_supports($post_type, 'revisions') && 0 < $post_ID && wp_get_post_revisions( $post_ID ) )
add_meta_box('revisionsdiv', __('Revisions'), 'post_revisions_meta_box', $post_type, 'normal', 'core');
do_action('do_meta_boxes', $post_type, 'normal', $post);

View File

@ -12,21 +12,15 @@ require_once('admin.php');
wp_enqueue_script('list-revisions');
wp_reset_vars(array('revision', 'left', 'right', 'diff', 'action'));
$revision_id = absint($revision);
$diff = absint($diff);
$left = absint($left);
$right = absint($right);
$parent_file = $redirect = 'edit.php';
$redirect = 'edit.php';
switch ( $action ) :
case 'delete' : // stubs
case 'edit' :
if ( constant('WP_POST_REVISIONS') ) // stub
$redirect = remove_query_arg( 'action' );
else // Revisions disabled
$redirect = 'edit.php';
break;
case 'restore' :
if ( !$revision = wp_get_post_revision( $revision_id ) )
break;
@ -115,9 +109,12 @@ default :
if ( !constant('WP_POST_REVISIONS') && !wp_is_post_autosave( $revision ) ) // Revisions disabled and we're not looking at an autosave
break;
$post_type_object = get_post_type_object($post->post_type);
$post_title = '<a href="' . get_edit_post_link() . '">' . get_the_title() . '</a>';
$revision_title = wp_post_revision_title( $revision, false );
$h2 = sprintf( __( 'Post Revision for &#8220;%1$s&#8221; created on %2$s' ), $post_title, $revision_title );
$h2 = sprintf( __( 'Revision for &#8220;%1$s&#8221; created on %2$s' ), $post_title, $revision_title );
$title = __( 'Revisions' );
// Sets up the diff radio buttons
$left = $revision->ID;
@ -127,21 +124,23 @@ default :
break;
endswitch;
if ( !$redirect && !in_array( $post->post_type, array( 'post', 'page' ) ) )
$redirect = 'edit.php';
if ( !$redirect ) {
if ( empty($post->post_type) ) // Empty post_type means either malformed object found, or no valid parent was found.
$redirect = 'edit.php';
elseif ( !post_type_supports($post->post_type, 'revisions') )
$redirect = 'edit.php?post_type=' . $post->post_type;
}
if ( $redirect ) {
if ( !empty($redirect) ) {
wp_redirect( $redirect );
exit;
}
if ( 'page' == $post->post_type ) {
$submenu_file = 'edit-pages.php';
$title = __( 'Page Revisions' );
} else {
$submenu_file = 'edit.php';
$title = __( 'Post Revisions' );
}
// This is so that the correct "Edit" menu item is selected.
if ( !empty($post->post_type) && 'post' != $post->post_type )
$parent_file = $submenu_file = 'edit.php?post_type=' . $post->post_type;
else
$parent_file = $submenu_file = 'edit.php';
require_once( 'admin-header.php' );

View File

@ -1260,17 +1260,17 @@ function wp_list_post_revisions( $post_id = 0, $args = null ) {
extract( wp_parse_args( $args, $defaults ), EXTR_SKIP );
switch ( $type ) {
case 'autosave' :
if ( !$autosave = wp_get_post_autosave( $post->ID ) )
return;
$revisions = array( $autosave );
break;
case 'revision' : // just revisions - remove autosave later
case 'all' :
default :
if ( !$revisions = wp_get_post_revisions( $post->ID ) )
return;
break;
case 'autosave' :
if ( !$autosave = wp_get_post_autosave( $post->ID ) )
return;
$revisions = array( $autosave );
break;
case 'revision' : // just revisions - remove autosave later
case 'all' :
default :
if ( !$revisions = wp_get_post_revisions( $post->ID ) )
return;
break;
}
/* translators: post revision: 1: when, 2: author name */
@ -1326,6 +1326,7 @@ function wp_list_post_revisions( $post_id = 0, $args = null ) {
<div class="alignleft">
<input type="submit" class="button-secondary" value="<?php esc_attr_e( 'Compare Revisions' ); ?>" />
<input type="hidden" name="action" value="diff" />
<input type="hidden" name="post_type" value="<?php echo esc_attr($GLOBALS['post_type']); ?>" />
</div>
</div>

View File

@ -15,18 +15,69 @@
* Creates the initial post types when 'init' action is fired.
*/
function create_initial_post_types() {
register_post_type( 'post', array('label' => __('Posts'), 'exclude_from_search' => false, '_builtin' => true, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false) );
register_post_type( 'page', array('label' => __('Pages'),'exclude_from_search' => false, '_builtin' => true, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'page', 'hierarchical' => true) );
register_post_type( 'attachment', array('label' => __('Media'), 'exclude_from_search' => false, '_builtin' => true, '_edit_link' => 'media.php?attachment_id=%d', 'capability_type' => 'post', 'hierarchical' => false) );
register_post_type( 'revision', array('label' => __('Revisions'),'exclude_from_search' => true, '_builtin' => true, '_edit_link' => 'revision.php?revision=%d', 'capability_type' => 'post', 'hierarchical' => false) );
add_post_type_support('post', array('post-thumbnails', 'excerpts', 'trackbacks', 'custom-fields', 'comments') );
add_post_type_support('page', array('post-thumbnails', 'page-attributes', 'custom-fields', 'comments') );
register_post_type( 'post', array( 'label' => __('Posts'),
'exclude_from_search' => false,
'_builtin' => true,
'_edit_link' => 'post.php?post=%d',
'capability_type' => 'post',
'hierarchical' => false,
'supports' => array('post-thumbnails', 'excerpts', 'trackbacks', 'custom-fields', 'comments', 'revisions')
) );
register_post_status( 'publish', array('label' => _x('Published', 'post'), 'exclude_from_search' => false, '_builtin' => true, 'label_count' => _n_noop('Published <span class="count">(%s)</span>', 'Published <span class="count">(%s)</span>')) );
register_post_status( 'future', array('label' => _x('Scheduled', 'post'), 'exclude_from_search' => false, '_builtin' => true, 'label_count' => _n_noop('Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>')) );
register_post_status( 'draft', array('label' => _x('Draft', 'post'), 'exclude_from_search' => false, '_builtin' => true, 'label_count' => _n_noop('Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>')) );
register_post_status( 'private', array('label' => _x('Private', 'post'), 'exclude_from_search' => false, '_builtin' => true, 'label_count' => _n_noop('Private <span class="count">(%s)</span>', 'Private <span class="count">(%s)</span>')) );
register_post_status( 'trash', array('label' => _x('Trash', 'post'), 'exclude_from_search' => false, '_builtin' => true, 'label_count' => _n_noop('Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>')) );
register_post_type( 'page', array( 'label' => __('Pages'),
'exclude_from_search' => false,
'_builtin' => true,
'_edit_link' => 'post.php?post=%d',
'capability_type' => 'page',
'hierarchical' => true,
'supports' => array('post-thumbnails', 'page-attributes', 'custom-fields', 'comments', 'revisions')
) );
register_post_type( 'attachment', array('label' => __('Media'),
'exclude_from_search' => false,
'_builtin' => true,
'_edit_link' => 'media.php?attachment_id=%d',
'capability_type' => 'post',
'hierarchical' => false
) );
register_post_type( 'revision', array( 'label' => __('Revisions'),
'exclude_from_search' => true,
'_builtin' => true,
'_edit_link' => 'revision.php?revision=%d',
'capability_type' => 'post',
'hierarchical' => false
) );
register_post_status( 'publish', array( 'label' => _x('Published', 'post'),
'exclude_from_search' => false,
'_builtin' => true,
'label_count' => _n_noop('Published <span class="count">(%s)</span>', 'Published <span class="count">(%s)</span>')
) );
register_post_status( 'future', array( 'label' => _x('Scheduled', 'post'),
'exclude_from_search' => false,
'_builtin' => true,
'label_count' => _n_noop('Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>')
) );
register_post_status( 'draft', array( 'label' => _x('Draft', 'post'),
'exclude_from_search' => false,
'_builtin' => true,
'label_count' => _n_noop('Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>')
) );
register_post_status( 'private', array( 'label' => _x('Private', 'post'),
'exclude_from_search' => false,
'_builtin' => true,
'label_count' => _n_noop('Private <span class="count">(%s)</span>', 'Private <span class="count">(%s)</span>')
) );
register_post_status( 'trash', array( 'label' => _x('Trash', 'post'),
'exclude_from_search' => false,
'_builtin' => true,
'label_count' => _n_noop('Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>')
) );
}
add_action( 'init', 'create_initial_post_types', 0 ); // highest priority
@ -639,6 +690,7 @@ function get_post_types( $args = array(), $output = 'names' ) {
* inherit_type - The post type from which to inherit the edit link and capability type. Defaults to none.
* capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to "post".
* hierarchical - Whether the post type is hierarchical. Defaults to false.
* supports - An alias for calling add_post_type_support() directly. See add_post_type_support() for Documentation. Defaults to none.
*
* @package WordPress
* @subpackage Post
@ -655,7 +707,7 @@ function register_post_type($post_type, $args = array()) {
$wp_post_types = array();
// Args prefixed with an underscore are reserved for internal use.
$defaults = array('label' => false, 'exclude_from_search' => true, '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false, 'public' => false, '_show' => false);
$defaults = array('label' => false, 'exclude_from_search' => true, '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false, 'public' => false, '_show' => false, 'supports' => array());
$args = wp_parse_args($args, $defaults);
$args = (object) $args;
@ -676,6 +728,11 @@ function register_post_type($post_type, $args = array()) {
if ( !$args->_builtin && $args->public )
$args->_show = true;
if ( ! empty($args->supports) ) {
add_post_type_support($post_type, $args->supports);
unset($args->supports);
}
$wp_post_types[$post_type] = $args;
return $args;
@ -3940,7 +3997,7 @@ function wp_save_post_revision( $post_id ) {
if ( !$post = get_post( $post_id, ARRAY_A ) )
return;
if ( !in_array( $post['post_type'], array( 'post', 'page' ) ) )
if ( !post_type_supports($post['post_type'], 'revisions') )
return;
$return = _wp_put_post_revision( $post );