Move revisions/autosave and post format functions from wp-includes/post.php into revision.php and post-formats.php.

git-svn-id: http://core.svn.wordpress.org/trunk@23466 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Nacin 2013-02-21 21:24:34 +00:00
parent 46a93b902f
commit b88b1c1f53
5 changed files with 853 additions and 837 deletions

View File

@ -1798,181 +1798,6 @@ function convert_smilies($text) {
return $output;
}
/**
* Return the class for a post format content wrapper
*
* @since 3.6.0
*
* @param string $format
*/
function get_post_format_content_class( $format ) {
return apply_filters( 'post_format_content_class', 'post-format-content', $format );
}
/**
* Ouput the class for a post format content wrapper
*
* @since 3.6.0
*
* @param string $format
*/
function post_format_content_class( $format ) {
echo get_post_format_content_class( $format );
}
/**
* Provide fallback behavior for Posts that have associated post format
*
* @since 3.6.0
*
* @param string $content
*/
function post_formats_compat( $content, $id = 0 ) {
$post = empty( $id ) ? get_post() : get_post( $id );
if ( empty( $post ) )
return $content;
$format = get_post_format( $post );
if ( empty( $format ) || in_array( $format, array( 'status', 'aside', 'chat' ) ) )
return $content;
if ( current_theme_supports( 'post-formats', $format ) )
return $content;
$defaults = array(
'position' => 'after',
'tag' => 'div',
'class' => get_post_format_content_class( $format ),
'link_class' => '',
'image_class' => '',
'gallery' => '[gallery]',
'audio' => '',
'video' => ''
);
$args = apply_filters( 'post_format_compat', array() );
$compat = wp_parse_args( $args, $defaults );
$show_content = true;
$format_output = '';
$meta = get_post_format_meta( $post->ID );
switch ( $format ) {
case 'link':
$compat['tag'] = '';
if ( ! empty( $meta['url'] ) ) {
$esc_url = preg_quote( $meta['url'], '#' );
// Make sure the same URL isn't in the post (modified/extended versions allowed)
if ( ! preg_match( '#' . $esc_url . '[^/&\?]#', $content ) ) {
$format_output .= sprintf(
'<a %shref="%s">%s</a>',
empty( $compat['link_class'] ) ? '' : sprintf( 'class="%s" ', esc_attr( $compat['link_class'] ) ),
esc_url( $meta['url'] ),
empty( $post->post_title ) ? esc_url( $meta['url'] ) : apply_filters( 'the_title', $post->post_title )
);
}
}
break;
case 'quote':
if ( ! empty( $meta['quote'] ) && ! stristr( $content, $meta['quote'] ) ) {
$format_output .= sprintf( '<blockquote>%s</blockquote>', $meta['quote'] );
if ( ! empty( $meta['quote_source'] ) ) {
$format_output .= sprintf(
'<cite>%s</cite>',
! empty( $meta['url'] ) ?
sprintf( '<a href="%s">%s</a>', esc_url( $meta['url'] ), $meta['quote_source'] ) :
$meta['quote_source']
);
}
}
break;
case 'image':
if ( ! empty( $meta['image'] ) ) {
$image = is_numeric( $meta['image'] ) ? wp_get_attachment_url( $meta['image'] ) : $meta['image'];
if ( ! empty( $image ) && ! stristr( $content, $image ) ) {
$image_html = sprintf(
'<img %ssrc="%s" alt="" />',
empty( $compat['image_class'] ) ? '' : sprintf( 'class="%s" ', esc_attr( $compat['image_class'] ) ),
$image
);
if ( empty( $meta['url'] ) ) {
$format_output .= $image_html;
} else {
$format_output .= sprintf(
'<a href="%s">%s</a>',
esc_url( $meta['url'] ),
$image_html
);
}
}
}
break;
case 'gallery':
preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches );
if ( ! empty( $matches ) && isset( $matches[2] ) ) {
foreach ( (array) $matches[2] as $match ) {
if ( 'gallery' === $match )
break 2; // foreach + case
}
}
if ( empty( $meta['gallery'] ) && ! empty( $compat['gallery'] ) ) {
$format_output .= $compat['gallery'];
} elseif ( ! empty( $meta['gallery'] ) ) {
$format_output .= $meta['gallery'];
}
break;
case 'video':
case 'audio':
$shortcode_regex = '/' . get_shortcode_regex() . '/s';
$matches = preg_match( $shortcode_regex, $content );
if ( ! $matches || $format !== $matches[2] ) {
if ( empty( $meta['media'] ) && ! empty( $compat[$format] ) ) {
$format_output .= $compat[$format];
} elseif ( ! empty( $meta['media'] ) ) {
// the metadata is a shortcode or an embed code
if ( preg_match( $shortcode_regex, $meta['media'] ) || preg_match( '#<[^>]+>#', $meta['media'] ) ) {
$format_output .= $meta['media'];
} elseif ( ! stristr( $content, $meta['media'] ) ) {
// attempt to embed the URL
$format_output .= sprintf( '[embed]%s[/embed]', $meta['media'] );
}
}
}
break;
default:
return $content;
break;
}
if ( empty( $format_output ) )
return $content;
$output = '';
if ( ! empty( $content ) && $show_content && 'before' !== $compat['position'] )
$output .= $content . PHP_EOL . PHP_EOL;
if ( ! empty( $compat['tag'] ) )
$output .= sprintf( '<%s class="%s">', tag_escape( $compat['tag'] ), esc_attr( $compat['class'] ) );
$output .= $format_output;
if ( ! empty( $compat['tag'] ) )
$output .= sprintf( '</%s>', tag_escape( $compat['tag'] ) );
if ( ! empty( $content ) && $show_content && 'before' === $compat['position'] )
$output .= PHP_EOL . PHP_EOL . $content;
return $output;
}
/**
* Verifies that an email is valid.
*
@ -3531,7 +3356,7 @@ function sanitize_trackback_urls( $to_ping ) {
* @return string|array Slashed $value
*/
function wp_slash( $value ) {
if ( is_array( $value ) ) {
if ( is_array( $value ) ) {
foreach ( $value as $k => $v ) {
if ( is_array( $v ) ) {
$value[$k] = wp_slash( $v );
@ -3540,10 +3365,10 @@ function wp_slash( $value ) {
}
}
} else {
$value = addslashes( $value );
}
$value = addslashes( $value );
}
return $value;
return $value;
}
/**
@ -3562,5 +3387,5 @@ function wp_slash( $value ) {
* @return string|array Unslashed $value
*/
function wp_unslash( $value ) {
return stripslashes_deep( $value );
return stripslashes_deep( $value );
}

View File

@ -0,0 +1,429 @@
<?php
/**
* Post format functions.
*
* @package WordPress
* @subpackage Post
*/
/**
* Retrieve the format slug for a post
*
* @since 3.1.0
*
* @param int|object $post A post
*
* @return mixed The format if successful. False if no format is set. WP_Error if errors.
*/
function get_post_format( $post = null ) {
$post = get_post($post);
if ( ! post_type_supports( $post->post_type, 'post-formats' ) )
return false;
$_format = get_the_terms( $post->ID, 'post_format' );
if ( empty( $_format ) )
return false;
$format = array_shift( $_format );
return ( str_replace('post-format-', '', $format->slug ) );
}
/**
* Check if a post has a particular format
*
* @since 3.1.0
* @uses has_term()
*
* @param string $format The format to check for
* @param object|id $post The post to check. If not supplied, defaults to the current post if used in the loop.
* @return bool True if the post has the format, false otherwise.
*/
function has_post_format( $format, $post = null ) {
return has_term('post-format-' . sanitize_key($format), 'post_format', $post);
}
/**
* Assign a format to a post
*
* @since 3.1.0
*
* @param int|object $post The post for which to assign a format
* @param string $format A format to assign. Use an empty string or array to remove all formats from the post.
* @return mixed WP_Error on error. Array of affected term IDs on success.
*/
function set_post_format( $post, $format ) {
$post = get_post($post);
if ( empty($post) )
return new WP_Error('invalid_post', __('Invalid post'));
if ( !empty($format) ) {
$format = sanitize_key($format);
if ( 'standard' == $format || !in_array( $format, array_keys( get_post_format_slugs() ) ) )
$format = '';
else
$format = 'post-format-' . $format;
}
return wp_set_post_terms($post->ID, $format, 'post_format');
}
/**
* Retrieve post format metadata for a post
*
* @since 3.6.0
*
* @param int $post_id
* @return null
*/
function get_post_format_meta( $post_id = 0 ) {
$values = array(
'quote' => '',
'quote_source' => '',
'image' => '',
'url' => '',
'gallery' => '',
'media' => '',
);
foreach ( $values as $key => $value )
$values[$key] = get_post_meta( $post_id, '_wp_format_' . $key, true );
return $values;
}
/**
* Returns an array of post format slugs to their translated and pretty display versions
*
* @since 3.1.0
*
* @return array The array of translations
*/
function get_post_format_strings() {
$strings = array(
'standard' => _x( 'Standard', 'Post format' ), // Special case. any value that evals to false will be considered standard
'aside' => _x( 'Aside', 'Post format' ),
'chat' => _x( 'Chat', 'Post format' ),
'gallery' => _x( 'Gallery', 'Post format' ),
'link' => _x( 'Link', 'Post format' ),
'image' => _x( 'Image', 'Post format' ),
'quote' => _x( 'Quote', 'Post format' ),
'status' => _x( 'Status', 'Post format' ),
'video' => _x( 'Video', 'Post format' ),
'audio' => _x( 'Audio', 'Post format' ),
);
return $strings;
}
/**
* Retrieves an array of post format slugs.
*
* @since 3.1.0
*
* @return array The array of post format slugs.
*/
function get_post_format_slugs() {
$slugs = array_keys( get_post_format_strings() );
return array_combine( $slugs, $slugs );
}
/**
* Returns a pretty, translated version of a post format slug
*
* @since 3.1.0
*
* @param string $slug A post format slug
* @return string The translated post format name
*/
function get_post_format_string( $slug ) {
$strings = get_post_format_strings();
if ( !$slug )
return $strings['standard'];
else
return ( isset( $strings[$slug] ) ) ? $strings[$slug] : '';
}
/**
* Returns a link to a post format index.
*
* @since 3.1.0
*
* @param string $format Post format
* @return string Link
*/
function get_post_format_link( $format ) {
$term = get_term_by('slug', 'post-format-' . $format, 'post_format' );
if ( ! $term || is_wp_error( $term ) )
return false;
return get_term_link( $term );
}
/**
* Filters the request to allow for the format prefix.
*
* @access private
* @since 3.1.0
*/
function _post_format_request( $qvs ) {
if ( ! isset( $qvs['post_format'] ) )
return $qvs;
$slugs = get_post_format_slugs();
if ( isset( $slugs[ $qvs['post_format'] ] ) )
$qvs['post_format'] = 'post-format-' . $slugs[ $qvs['post_format'] ];
$tax = get_taxonomy( 'post_format' );
if ( ! is_admin() )
$qvs['post_type'] = $tax->object_type;
return $qvs;
}
add_filter( 'request', '_post_format_request' );
/**
* Filters the post format term link to remove the format prefix.
*
* @access private
* @since 3.1.0
*/
function _post_format_link( $link, $term, $taxonomy ) {
global $wp_rewrite;
if ( 'post_format' != $taxonomy )
return $link;
if ( $wp_rewrite->get_extra_permastruct( $taxonomy ) ) {
return str_replace( "/{$term->slug}", '/' . str_replace( 'post-format-', '', $term->slug ), $link );
} else {
$link = remove_query_arg( 'post_format', $link );
return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link );
}
}
add_filter( 'term_link', '_post_format_link', 10, 3 );
/**
* Remove the post format prefix from the name property of the term object created by get_term().
*
* @access private
* @since 3.1.0
*/
function _post_format_get_term( $term ) {
if ( isset( $term->slug ) ) {
$term->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
}
return $term;
}
add_filter( 'get_post_format', '_post_format_get_term' );
/**
* Remove the post format prefix from the name property of the term objects created by get_terms().
*
* @access private
* @since 3.1.0
*/
function _post_format_get_terms( $terms, $taxonomies, $args ) {
if ( in_array( 'post_format', (array) $taxonomies ) ) {
if ( isset( $args['fields'] ) && 'names' == $args['fields'] ) {
foreach( $terms as $order => $name ) {
$terms[$order] = get_post_format_string( str_replace( 'post-format-', '', $name ) );
}
} else {
foreach ( (array) $terms as $order => $term ) {
if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
$terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
}
}
}
}
return $terms;
}
add_filter( 'get_terms', '_post_format_get_terms', 10, 3 );
/**
* Remove the post format prefix from the name property of the term objects created by wp_get_object_terms().
*
* @access private
* @since 3.1.0
*/
function _post_format_wp_get_object_terms( $terms ) {
foreach ( (array) $terms as $order => $term ) {
if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
$terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
}
}
return $terms;
}
add_filter( 'wp_get_object_terms', '_post_format_wp_get_object_terms' );
/**
* Return the class for a post format content wrapper
*
* @since 3.6.0
*
* @param string $format
*/
function get_post_format_content_class( $format ) {
return apply_filters( 'post_format_content_class', 'post-format-content', $format );
}
/**
* Ouput the class for a post format content wrapper
*
* @since 3.6.0
*
* @param string $format
*/
function post_format_content_class( $format ) {
echo get_post_format_content_class( $format );
}
/**
* Provide fallback behavior for Posts that have associated post format
*
* @since 3.6.0
*
* @param string $content
*/
function post_formats_compat( $content, $id = 0 ) {
$post = empty( $id ) ? get_post() : get_post( $id );
if ( empty( $post ) )
return $content;
$format = get_post_format( $post );
if ( empty( $format ) || in_array( $format, array( 'status', 'aside', 'chat' ) ) )
return $content;
if ( current_theme_supports( 'post-formats', $format ) )
return $content;
$defaults = array(
'position' => 'after',
'tag' => 'div',
'class' => get_post_format_content_class( $format ),
'link_class' => '',
'image_class' => '',
'gallery' => '[gallery]',
'audio' => '',
'video' => ''
);
$args = apply_filters( 'post_format_compat', array() );
$compat = wp_parse_args( $args, $defaults );
$show_content = true;
$format_output = '';
$meta = get_post_format_meta( $post->ID );
switch ( $format ) {
case 'link':
$compat['tag'] = '';
if ( ! empty( $meta['url'] ) ) {
$esc_url = preg_quote( $meta['url'], '#' );
// Make sure the same URL isn't in the post (modified/extended versions allowed)
if ( ! preg_match( '#' . $esc_url . '[^/&\?]#', $content ) ) {
$format_output .= sprintf(
'<a %shref="%s">%s</a>',
empty( $compat['link_class'] ) ? '' : sprintf( 'class="%s" ', esc_attr( $compat['link_class'] ) ),
esc_url( $meta['url'] ),
empty( $post->post_title ) ? esc_url( $meta['url'] ) : apply_filters( 'the_title', $post->post_title )
);
}
}
break;
case 'quote':
if ( ! empty( $meta['quote'] ) && ! stristr( $content, $meta['quote'] ) ) {
$format_output .= sprintf( '<blockquote>%s</blockquote>', $meta['quote'] );
if ( ! empty( $meta['quote_source'] ) ) {
$format_output .= sprintf(
'<cite>%s</cite>',
! empty( $meta['url'] ) ?
sprintf( '<a href="%s">%s</a>', esc_url( $meta['url'] ), $meta['quote_source'] ) :
$meta['quote_source']
);
}
}
break;
case 'image':
if ( ! empty( $meta['image'] ) ) {
$image = is_numeric( $meta['image'] ) ? wp_get_attachment_url( $meta['image'] ) : $meta['image'];
if ( ! empty( $image ) && ! stristr( $content, $image ) ) {
$image_html = sprintf(
'<img %ssrc="%s" alt="" />',
empty( $compat['image_class'] ) ? '' : sprintf( 'class="%s" ', esc_attr( $compat['image_class'] ) ),
$image
);
if ( empty( $meta['url'] ) ) {
$format_output .= $image_html;
} else {
$format_output .= sprintf(
'<a href="%s">%s</a>',
esc_url( $meta['url'] ),
$image_html
);
}
}
}
break;
case 'gallery':
preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches );
if ( ! empty( $matches ) && isset( $matches[2] ) ) {
foreach ( (array) $matches[2] as $match ) {
if ( 'gallery' === $match )
break 2; // foreach + case
}
}
if ( empty( $meta['gallery'] ) && ! empty( $compat['gallery'] ) ) {
$format_output .= $compat['gallery'];
} elseif ( ! empty( $meta['gallery'] ) ) {
$format_output .= $meta['gallery'];
}
break;
case 'video':
case 'audio':
$shortcode_regex = '/' . get_shortcode_regex() . '/s';
$matches = preg_match( $shortcode_regex, $content );
if ( ! $matches || $format !== $matches[2] ) {
if ( empty( $meta['media'] ) && ! empty( $compat[$format] ) ) {
$format_output .= $compat[$format];
} elseif ( ! empty( $meta['media'] ) ) {
// the metadata is a shortcode or an embed code
if ( preg_match( $shortcode_regex, $meta['media'] ) || preg_match( '#<[^>]+>#', $meta['media'] ) ) {
$format_output .= $meta['media'];
} elseif ( ! stristr( $content, $meta['media'] ) ) {
// attempt to embed the URL
$format_output .= sprintf( '[embed]%s[/embed]', $meta['media'] );
}
}
}
break;
default:
return $content;
break;
}
if ( empty( $format_output ) )
return $content;
$output = '';
if ( ! empty( $content ) && $show_content && 'before' !== $compat['position'] )
$output .= $content . PHP_EOL . PHP_EOL;
if ( ! empty( $compat['tag'] ) )
$output .= sprintf( '<%s class="%s">', tag_escape( $compat['tag'] ), esc_attr( $compat['class'] ) );
$output .= $format_output;
if ( ! empty( $compat['tag'] ) )
$output .= sprintf( '</%s>', tag_escape( $compat['tag'] ) );
if ( ! empty( $content ) && $show_content && 'before' === $compat['position'] )
$output .= PHP_EOL . PHP_EOL . $content;
return $output;
}

View File

@ -753,71 +753,6 @@ function get_post_mime_type($ID = '') {
return false;
}
/**
* Retrieve the format slug for a post
*
* @since 3.1.0
*
* @param int|object $post A post
*
* @return mixed The format if successful. False if no format is set. WP_Error if errors.
*/
function get_post_format( $post = null ) {
$post = get_post($post);
if ( ! post_type_supports( $post->post_type, 'post-formats' ) )
return false;
$_format = get_the_terms( $post->ID, 'post_format' );
if ( empty( $_format ) )
return false;
$format = array_shift( $_format );
return ( str_replace('post-format-', '', $format->slug ) );
}
/**
* Check if a post has a particular format
*
* @since 3.1.0
* @uses has_term()
*
* @param string $format The format to check for
* @param object|id $post The post to check. If not supplied, defaults to the current post if used in the loop.
* @return bool True if the post has the format, false otherwise.
*/
function has_post_format( $format, $post = null ) {
return has_term('post-format-' . sanitize_key($format), 'post_format', $post);
}
/**
* Assign a format to a post
*
* @since 3.1.0
*
* @param int|object $post The post for which to assign a format
* @param string $format A format to assign. Use an empty string or array to remove all formats from the post.
* @return mixed WP_Error on error. Array of affected term IDs on success.
*/
function set_post_format( $post, $format ) {
$post = get_post($post);
if ( empty($post) )
return new WP_Error('invalid_post', __('Invalid post'));
if ( !empty($format) ) {
$format = sanitize_key($format);
if ( 'standard' == $format || !in_array( $format, array_keys( get_post_format_slugs() ) ) )
$format = '';
else
$format = 'post-format-' . $format;
}
return wp_set_post_terms($post->ID, $format, 'post_format');
}
/**
* Retrieve the post status based on the Post ID.
*
@ -1950,30 +1885,6 @@ function get_post_custom_values( $key = '', $post_id = 0 ) {
return isset($custom[$key]) ? $custom[$key] : null;
}
/**
* Retrieve post format metadata for a post
*
* @since 3.6.0
*
* @param int $post_id
* @return null
*/
function get_post_format_meta( $post_id = 0 ) {
$values = array(
'quote' => '',
'quote_source' => '',
'image' => '',
'url' => '',
'gallery' => '',
'media' => '',
);
foreach ( $values as $key => $value )
$values[$key] = get_post_meta( $post_id, '_wp_format_' . $key, true );
return $values;
}
/**
* Check if post is sticky.
*
@ -4949,416 +4860,6 @@ function _publish_post_hook($post_id) {
wp_schedule_single_event(time(), 'do_pings');
}
/**
* Determines which fields of posts are to be saved in revisions.
*
* Does two things. If passed a post *array*, it will return a post array ready
* to be inserted into the posts table as a post revision. Otherwise, returns
* an array whose keys are the post fields to be saved for post revisions.
*
* @package WordPress
* @subpackage Post_Revisions
* @since 2.6.0
* @access private
* @uses apply_filters() Calls '_wp_post_revision_fields' on 'title', 'content' and 'excerpt' fields.
*
* @param array $post Optional a post array to be processed for insertion as a post revision.
* @param bool $autosave optional Is the revision an autosave?
* @return array Post array ready to be inserted as a post revision or array of fields that can be versioned.
*/
function _wp_post_revision_fields( $post = null, $autosave = false ) {
static $fields = false;
if ( !$fields ) {
// Allow these to be versioned
$fields = array(
'post_title' => __( 'Title' ),
'post_content' => __( 'Content' ),
'post_excerpt' => __( 'Excerpt' ),
);
// Runs only once
$fields = apply_filters( '_wp_post_revision_fields', $fields );
// WP uses these internally either in versioning or elsewhere - they cannot be versioned
foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect )
unset( $fields[$protect] );
}
if ( !is_array($post) )
return $fields;
$return = array();
foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field )
$return[$field] = $post[$field];
$return['post_parent'] = $post['ID'];
$return['post_status'] = 'inherit';
$return['post_type'] = 'revision';
$return['post_name'] = $autosave ? "$post[ID]-autosave" : "$post[ID]-revision";
$return['post_date'] = isset($post['post_modified']) ? $post['post_modified'] : '';
$return['post_date_gmt'] = isset($post['post_modified_gmt']) ? $post['post_modified_gmt'] : '';
return $return;
}
/**
* Saves an already existing post as a post revision.
*
* Typically used immediately prior to post updates.
*
* @package WordPress
* @subpackage Post_Revisions
* @since 2.6.0
*
* @uses _wp_put_post_revision()
*
* @param int $post_id The ID of the post to save as a revision.
* @return mixed Null or 0 if error, new revision ID, if success.
*/
function wp_save_post_revision( $post_id, $new_data = null ) {
// We do autosaves manually with wp_create_post_autosave()
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// WP_POST_REVISIONS = 0, false
if ( ! WP_POST_REVISIONS )
return;
if ( !$post = get_post( $post_id, ARRAY_A ) )
return;
if ( 'auto-draft' == $post['post_status'] )
return;
if ( !post_type_supports($post['post_type'], 'revisions') )
return;
// if new data is supplied, check that it is different from last saved revision, unless a plugin tells us to always save regardless
if ( apply_filters( 'wp_save_post_revision_check_for_changes', true, $post, $new_data ) && is_array( $new_data ) ) {
$post_has_changed = false;
foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
if ( normalize_whitespace( $new_data[ $field ] ) != normalize_whitespace( $post[ $field ] ) ) {
$post_has_changed = true;
break;
}
}
//don't save revision if post unchanged
if( ! $post_has_changed )
return;
}
$return = _wp_put_post_revision( $post );
// WP_POST_REVISIONS = true (default), -1
if ( !is_numeric( WP_POST_REVISIONS ) || WP_POST_REVISIONS < 0 )
return $return;
// all revisions and (possibly) one autosave
$revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) );
// WP_POST_REVISIONS = (int) (# of autosaves to save)
$delete = count($revisions) - WP_POST_REVISIONS;
if ( $delete < 1 )
return $return;
$revisions = array_slice( $revisions, 0, $delete );
for ( $i = 0; isset($revisions[$i]); $i++ ) {
if ( false !== strpos( $revisions[$i]->post_name, 'autosave' ) )
continue;
wp_delete_post_revision( $revisions[$i]->ID );
}
return $return;
}
/**
* Retrieve the autosaved data of the specified post.
*
* Returns a post object containing the information that was autosaved for the
* specified post.
*
* @package WordPress
* @subpackage Post_Revisions
* @since 2.6.0
*
* @param int $post_id The post ID.
* @return object|bool The autosaved data or false on failure or when no autosave exists.
*/
function wp_get_post_autosave( $post_id ) {
if ( !$post = get_post( $post_id ) )
return false;
$q = array(
'name' => "{$post->ID}-autosave",
'post_parent' => $post->ID,
'post_type' => 'revision',
'post_status' => 'inherit'
);
// Use WP_Query so that the result gets cached
$autosave_query = new WP_Query;
add_action( 'parse_query', '_wp_get_post_autosave_hack' );
$autosave = $autosave_query->query( $q );
remove_action( 'parse_query', '_wp_get_post_autosave_hack' );
if ( $autosave && is_array($autosave) && is_object($autosave[0]) )
return $autosave[0];
return false;
}
/**
* Internally used to hack WP_Query into submission.
*
* @package WordPress
* @subpackage Post_Revisions
* @since 2.6.0
*
* @param object $query WP_Query object
*/
function _wp_get_post_autosave_hack( $query ) {
$query->is_single = false;
}
/**
* Determines if the specified post is a revision.
*
* @package WordPress
* @subpackage Post_Revisions
* @since 2.6.0
*
* @param int|object $post Post ID or post object.
* @return bool|int False if not a revision, ID of revision's parent otherwise.
*/
function wp_is_post_revision( $post ) {
if ( !$post = wp_get_post_revision( $post ) )
return false;
return (int) $post->post_parent;
}
/**
* Determines if the specified post is an autosave.
*
* @package WordPress
* @subpackage Post_Revisions
* @since 2.6.0
*
* @param int|object $post Post ID or post object.
* @return bool|int False if not a revision, ID of autosave's parent otherwise
*/
function wp_is_post_autosave( $post ) {
if ( !$post = wp_get_post_revision( $post ) )
return false;
if ( "{$post->post_parent}-autosave" !== $post->post_name )
return false;
return (int) $post->post_parent;
}
/**
* Inserts post data into the posts table as a post revision.
*
* @package WordPress
* @subpackage Post_Revisions
* @since 2.6.0
*
* @uses wp_insert_post()
*
* @param int|object|array $post Post ID, post object OR post array.
* @param bool $autosave Optional. Is the revision an autosave?
* @return mixed Null or 0 if error, new revision ID if success.
*/
function _wp_put_post_revision( $post = null, $autosave = false ) {
if ( is_object($post) )
$post = get_object_vars( $post );
elseif ( !is_array($post) )
$post = get_post($post, ARRAY_A);
if ( !$post || empty($post['ID']) )
return;
if ( isset($post['post_type']) && 'revision' == $post['post_type'] )
return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
$post = _wp_post_revision_fields( $post, $autosave );
$revision_id = wp_insert_post( $post );
if ( is_wp_error($revision_id) )
return $revision_id;
if ( $revision_id )
do_action( '_wp_put_post_revision', $revision_id );
return $revision_id;
}
/**
* Gets a post revision.
*
* @package WordPress
* @subpackage Post_Revisions
* @since 2.6.0
*
* @uses get_post()
*
* @param int|object $post Post ID or post object
* @param string $output Optional. OBJECT, ARRAY_A, or ARRAY_N.
* @param string $filter Optional sanitation filter. @see sanitize_post()
* @return mixed Null if error or post object if success
*/
function wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') {
$null = null;
if ( !$revision = get_post( $post, OBJECT, $filter ) )
return $revision;
if ( 'revision' !== $revision->post_type )
return $null;
if ( $output == OBJECT ) {
return $revision;
} elseif ( $output == ARRAY_A ) {
$_revision = get_object_vars($revision);
return $_revision;
} elseif ( $output == ARRAY_N ) {
$_revision = array_values(get_object_vars($revision));
return $_revision;
}
return $revision;
}
/**
* Restores a post to the specified revision.
*
* Can restore a past revision using all fields of the post revision, or only selected fields.
*
* @package WordPress
* @subpackage Post_Revisions
* @since 2.6.0
*
* @uses wp_get_post_revision()
* @uses wp_update_post()
* @uses do_action() Calls 'wp_restore_post_revision' on post ID and revision ID if wp_update_post()
* is successful.
*
* @param int|object $revision_id Revision ID or revision object.
* @param array $fields Optional. What fields to restore from. Defaults to all.
* @return mixed Null if error, false if no fields to restore, (int) post ID if success.
*/
function wp_restore_post_revision( $revision_id, $fields = null ) {
if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) )
return $revision;
if ( !is_array( $fields ) )
$fields = array_keys( _wp_post_revision_fields() );
$update = array();
foreach( array_intersect( array_keys( $revision ), $fields ) as $field )
$update[$field] = $revision[$field];
if ( !$update )
return false;
$update['ID'] = $revision['post_parent'];
$post_id = wp_update_post( $update );
if ( is_wp_error( $post_id ) )
return $post_id;
if ( $post_id )
do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] );
return $post_id;
}
/**
* Deletes a revision.
*
* Deletes the row from the posts table corresponding to the specified revision.
*
* @package WordPress
* @subpackage Post_Revisions
* @since 2.6.0
*
* @uses wp_get_post_revision()
* @uses wp_delete_post()
*
* @param int|object $revision_id Revision ID or revision object.
* @return mixed Null or WP_Error if error, deleted post if success.
*/
function wp_delete_post_revision( $revision_id ) {
if ( !$revision = wp_get_post_revision( $revision_id ) )
return $revision;
$delete = wp_delete_post( $revision->ID );
if ( is_wp_error( $delete ) )
return $delete;
if ( $delete )
do_action( 'wp_delete_post_revision', $revision->ID, $revision );
return $delete;
}
/**
* Returns all revisions of specified post.
*
* @package WordPress
* @subpackage Post_Revisions
* @since 2.6.0
*
* @uses get_children()
*
* @param int|object $post_id Post ID or post object
* @return array empty if no revisions
*/
function wp_get_post_revisions( $post_id = 0, $args = null ) {
if ( ! WP_POST_REVISIONS )
return array();
if ( ( !$post = get_post( $post_id ) ) || empty( $post->ID ) )
return array();
$defaults = array( 'order' => 'DESC', 'orderby' => 'date' );
$args = wp_parse_args( $args, $defaults );
$args = array_merge( $args, array( 'post_parent' => $post->ID, 'post_type' => 'revision', 'post_status' => 'inherit' ) );
if ( !$revisions = get_children( $args ) )
return array();
return $revisions;
}
function _set_preview($post) {
if ( ! is_object($post) )
return $post;
$preview = wp_get_post_autosave($post->ID);
if ( ! is_object($preview) )
return $post;
$preview = sanitize_post($preview);
$post->post_content = $preview->post_content;
$post->post_title = $preview->post_title;
$post->post_excerpt = $preview->post_excerpt;
return $post;
}
function _show_post_preview() {
if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) {
$id = (int) $_GET['preview_id'];
if ( false == wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) )
wp_die( __('You do not have permission to preview drafts.') );
add_filter('the_preview', '_set_preview');
}
}
/**
* Returns the post's parent's post_ID
*
@ -5418,57 +4919,6 @@ function wp_check_post_hierarchy_for_loops( $post_parent, $post_ID ) {
return $post_parent;
}
/**
* Returns an array of post format slugs to their translated and pretty display versions
*
* @since 3.1.0
*
* @return array The array of translations
*/
function get_post_format_strings() {
$strings = array(
'standard' => _x( 'Standard', 'Post format' ), // Special case. any value that evals to false will be considered standard
'aside' => _x( 'Aside', 'Post format' ),
'chat' => _x( 'Chat', 'Post format' ),
'gallery' => _x( 'Gallery', 'Post format' ),
'link' => _x( 'Link', 'Post format' ),
'image' => _x( 'Image', 'Post format' ),
'quote' => _x( 'Quote', 'Post format' ),
'status' => _x( 'Status', 'Post format' ),
'video' => _x( 'Video', 'Post format' ),
'audio' => _x( 'Audio', 'Post format' ),
);
return $strings;
}
/**
* Retrieves an array of post format slugs.
*
* @since 3.1.0
*
* @return array The array of post format slugs.
*/
function get_post_format_slugs() {
$slugs = array_keys( get_post_format_strings() );
return array_combine( $slugs, $slugs );
}
/**
* Returns a pretty, translated version of a post format slug
*
* @since 3.1.0
*
* @param string $slug A post format slug
* @return string The translated post format name
*/
function get_post_format_string( $slug ) {
$strings = get_post_format_strings();
if ( !$slug )
return $strings['standard'];
else
return ( isset( $strings[$slug] ) ) ? $strings[$slug] : '';
}
/**
* Sets a post thumbnail.
*
@ -5505,21 +4955,6 @@ function delete_post_thumbnail( $post ) {
return false;
}
/**
* Returns a link to a post format index.
*
* @since 3.1.0
*
* @param string $format Post format
* @return string Link
*/
function get_post_format_link( $format ) {
$term = get_term_by('slug', 'post-format-' . $format, 'post_format' );
if ( ! $term || is_wp_error( $term ) )
return false;
return get_term_link( $term );
}
/**
* Deletes auto-drafts for new posts that are > 7 days old
*
@ -5534,98 +4969,6 @@ function wp_delete_auto_drafts() {
wp_delete_post( $delete, true ); // Force delete
}
/**
* Filters the request to allow for the format prefix.
*
* @access private
* @since 3.1.0
*/
function _post_format_request( $qvs ) {
if ( ! isset( $qvs['post_format'] ) )
return $qvs;
$slugs = get_post_format_slugs();
if ( isset( $slugs[ $qvs['post_format'] ] ) )
$qvs['post_format'] = 'post-format-' . $slugs[ $qvs['post_format'] ];
$tax = get_taxonomy( 'post_format' );
if ( ! is_admin() )
$qvs['post_type'] = $tax->object_type;
return $qvs;
}
add_filter( 'request', '_post_format_request' );
/**
* Filters the post format term link to remove the format prefix.
*
* @access private
* @since 3.1.0
*/
function _post_format_link( $link, $term, $taxonomy ) {
global $wp_rewrite;
if ( 'post_format' != $taxonomy )
return $link;
if ( $wp_rewrite->get_extra_permastruct( $taxonomy ) ) {
return str_replace( "/{$term->slug}", '/' . str_replace( 'post-format-', '', $term->slug ), $link );
} else {
$link = remove_query_arg( 'post_format', $link );
return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link );
}
}
add_filter( 'term_link', '_post_format_link', 10, 3 );
/**
* Remove the post format prefix from the name property of the term object created by get_term().
*
* @access private
* @since 3.1.0
*/
function _post_format_get_term( $term ) {
if ( isset( $term->slug ) ) {
$term->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
}
return $term;
}
add_filter( 'get_post_format', '_post_format_get_term' );
/**
* Remove the post format prefix from the name property of the term objects created by get_terms().
*
* @access private
* @since 3.1.0
*/
function _post_format_get_terms( $terms, $taxonomies, $args ) {
if ( in_array( 'post_format', (array) $taxonomies ) ) {
if ( isset( $args['fields'] ) && 'names' == $args['fields'] ) {
foreach( $terms as $order => $name ) {
$terms[$order] = get_post_format_string( str_replace( 'post-format-', '', $name ) );
}
} else {
foreach ( (array) $terms as $order => $term ) {
if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
$terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
}
}
}
}
return $terms;
}
add_filter( 'get_terms', '_post_format_get_terms', 10, 3 );
/**
* Remove the post format prefix from the name property of the term objects created by wp_get_object_terms().
*
* @access private
* @since 3.1.0
*/
function _post_format_wp_get_object_terms( $terms ) {
foreach ( (array) $terms as $order => $term ) {
if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
$terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
}
}
return $terms;
}
add_filter( 'wp_get_object_terms', '_post_format_wp_get_object_terms' );
/**
* Update the custom taxonomies' term counts when a post's status is changed. For example, default posts term counts (for custom taxonomies) don't include private / draft posts.
*

417
wp-includes/revision.php Normal file
View File

@ -0,0 +1,417 @@
<?php
/**
* Post revision functions.
*
* @package WordPress
* @subpackage Post_Revisions
*/
/**
* Determines which fields of posts are to be saved in revisions.
*
* Does two things. If passed a post *array*, it will return a post array ready
* to be inserted into the posts table as a post revision. Otherwise, returns
* an array whose keys are the post fields to be saved for post revisions.
*
* @package WordPress
* @subpackage Post_Revisions
* @since 2.6.0
* @access private
* @uses apply_filters() Calls '_wp_post_revision_fields' on 'title', 'content' and 'excerpt' fields.
*
* @param array $post Optional a post array to be processed for insertion as a post revision.
* @param bool $autosave optional Is the revision an autosave?
* @return array Post array ready to be inserted as a post revision or array of fields that can be versioned.
*/
function _wp_post_revision_fields( $post = null, $autosave = false ) {
static $fields = false;
if ( !$fields ) {
// Allow these to be versioned
$fields = array(
'post_title' => __( 'Title' ),
'post_content' => __( 'Content' ),
'post_excerpt' => __( 'Excerpt' ),
);
// Runs only once
$fields = apply_filters( '_wp_post_revision_fields', $fields );
// WP uses these internally either in versioning or elsewhere - they cannot be versioned
foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect )
unset( $fields[$protect] );
}
if ( !is_array($post) )
return $fields;
$return = array();
foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field )
$return[$field] = $post[$field];
$return['post_parent'] = $post['ID'];
$return['post_status'] = 'inherit';
$return['post_type'] = 'revision';
$return['post_name'] = $autosave ? "$post[ID]-autosave" : "$post[ID]-revision";
$return['post_date'] = isset($post['post_modified']) ? $post['post_modified'] : '';
$return['post_date_gmt'] = isset($post['post_modified_gmt']) ? $post['post_modified_gmt'] : '';
return $return;
}
/**
* Saves an already existing post as a post revision.
*
* Typically used immediately prior to post updates.
*
* @package WordPress
* @subpackage Post_Revisions
* @since 2.6.0
*
* @uses _wp_put_post_revision()
*
* @param int $post_id The ID of the post to save as a revision.
* @return mixed Null or 0 if error, new revision ID, if success.
*/
function wp_save_post_revision( $post_id, $new_data = null ) {
// We do autosaves manually with wp_create_post_autosave()
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// WP_POST_REVISIONS = 0, false
if ( ! WP_POST_REVISIONS )
return;
if ( !$post = get_post( $post_id, ARRAY_A ) )
return;
if ( 'auto-draft' == $post['post_status'] )
return;
if ( !post_type_supports($post['post_type'], 'revisions') )
return;
// if new data is supplied, check that it is different from last saved revision, unless a plugin tells us to always save regardless
if ( apply_filters( 'wp_save_post_revision_check_for_changes', true, $post, $new_data ) && is_array( $new_data ) ) {
$post_has_changed = false;
foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
if ( normalize_whitespace( $new_data[ $field ] ) != normalize_whitespace( $post[ $field ] ) ) {
$post_has_changed = true;
break;
}
}
//don't save revision if post unchanged
if( ! $post_has_changed )
return;
}
$return = _wp_put_post_revision( $post );
// WP_POST_REVISIONS = true (default), -1
if ( !is_numeric( WP_POST_REVISIONS ) || WP_POST_REVISIONS < 0 )
return $return;
// all revisions and (possibly) one autosave
$revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) );
// WP_POST_REVISIONS = (int) (# of autosaves to save)
$delete = count($revisions) - WP_POST_REVISIONS;
if ( $delete < 1 )
return $return;
$revisions = array_slice( $revisions, 0, $delete );
for ( $i = 0; isset($revisions[$i]); $i++ ) {
if ( false !== strpos( $revisions[$i]->post_name, 'autosave' ) )
continue;
wp_delete_post_revision( $revisions[$i]->ID );
}
return $return;
}
/**
* Retrieve the autosaved data of the specified post.
*
* Returns a post object containing the information that was autosaved for the
* specified post.
*
* @package WordPress
* @subpackage Post_Revisions
* @since 2.6.0
*
* @param int $post_id The post ID.
* @return object|bool The autosaved data or false on failure or when no autosave exists.
*/
function wp_get_post_autosave( $post_id ) {
if ( !$post = get_post( $post_id ) )
return false;
$q = array(
'name' => "{$post->ID}-autosave",
'post_parent' => $post->ID,
'post_type' => 'revision',
'post_status' => 'inherit'
);
// Use WP_Query so that the result gets cached
$autosave_query = new WP_Query;
add_action( 'parse_query', '_wp_get_post_autosave_hack' );
$autosave = $autosave_query->query( $q );
remove_action( 'parse_query', '_wp_get_post_autosave_hack' );
if ( $autosave && is_array($autosave) && is_object($autosave[0]) )
return $autosave[0];
return false;
}
/**
* Internally used to hack WP_Query into submission.
*
* @package WordPress
* @subpackage Post_Revisions
* @since 2.6.0
*
* @param object $query WP_Query object
*/
function _wp_get_post_autosave_hack( $query ) {
$query->is_single = false;
}
/**
* Determines if the specified post is a revision.
*
* @package WordPress
* @subpackage Post_Revisions
* @since 2.6.0
*
* @param int|object $post Post ID or post object.
* @return bool|int False if not a revision, ID of revision's parent otherwise.
*/
function wp_is_post_revision( $post ) {
if ( !$post = wp_get_post_revision( $post ) )
return false;
return (int) $post->post_parent;
}
/**
* Determines if the specified post is an autosave.
*
* @package WordPress
* @subpackage Post_Revisions
* @since 2.6.0
*
* @param int|object $post Post ID or post object.
* @return bool|int False if not a revision, ID of autosave's parent otherwise
*/
function wp_is_post_autosave( $post ) {
if ( !$post = wp_get_post_revision( $post ) )
return false;
if ( "{$post->post_parent}-autosave" !== $post->post_name )
return false;
return (int) $post->post_parent;
}
/**
* Inserts post data into the posts table as a post revision.
*
* @package WordPress
* @subpackage Post_Revisions
* @since 2.6.0
*
* @uses wp_insert_post()
*
* @param int|object|array $post Post ID, post object OR post array.
* @param bool $autosave Optional. Is the revision an autosave?
* @return mixed Null or 0 if error, new revision ID if success.
*/
function _wp_put_post_revision( $post = null, $autosave = false ) {
if ( is_object($post) )
$post = get_object_vars( $post );
elseif ( !is_array($post) )
$post = get_post($post, ARRAY_A);
if ( !$post || empty($post['ID']) )
return;
if ( isset($post['post_type']) && 'revision' == $post['post_type'] )
return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
$post = _wp_post_revision_fields( $post, $autosave );
$revision_id = wp_insert_post( $post );
if ( is_wp_error($revision_id) )
return $revision_id;
if ( $revision_id )
do_action( '_wp_put_post_revision', $revision_id );
return $revision_id;
}
/**
* Gets a post revision.
*
* @package WordPress
* @subpackage Post_Revisions
* @since 2.6.0
*
* @uses get_post()
*
* @param int|object $post Post ID or post object
* @param string $output Optional. OBJECT, ARRAY_A, or ARRAY_N.
* @param string $filter Optional sanitation filter. @see sanitize_post()
* @return mixed Null if error or post object if success
*/
function wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') {
$null = null;
if ( !$revision = get_post( $post, OBJECT, $filter ) )
return $revision;
if ( 'revision' !== $revision->post_type )
return $null;
if ( $output == OBJECT ) {
return $revision;
} elseif ( $output == ARRAY_A ) {
$_revision = get_object_vars($revision);
return $_revision;
} elseif ( $output == ARRAY_N ) {
$_revision = array_values(get_object_vars($revision));
return $_revision;
}
return $revision;
}
/**
* Restores a post to the specified revision.
*
* Can restore a past revision using all fields of the post revision, or only selected fields.
*
* @package WordPress
* @subpackage Post_Revisions
* @since 2.6.0
*
* @uses wp_get_post_revision()
* @uses wp_update_post()
* @uses do_action() Calls 'wp_restore_post_revision' on post ID and revision ID if wp_update_post()
* is successful.
*
* @param int|object $revision_id Revision ID or revision object.
* @param array $fields Optional. What fields to restore from. Defaults to all.
* @return mixed Null if error, false if no fields to restore, (int) post ID if success.
*/
function wp_restore_post_revision( $revision_id, $fields = null ) {
if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) )
return $revision;
if ( !is_array( $fields ) )
$fields = array_keys( _wp_post_revision_fields() );
$update = array();
foreach( array_intersect( array_keys( $revision ), $fields ) as $field )
$update[$field] = $revision[$field];
if ( !$update )
return false;
$update['ID'] = $revision['post_parent'];
$post_id = wp_update_post( $update );
if ( is_wp_error( $post_id ) )
return $post_id;
if ( $post_id )
do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] );
return $post_id;
}
/**
* Deletes a revision.
*
* Deletes the row from the posts table corresponding to the specified revision.
*
* @package WordPress
* @subpackage Post_Revisions
* @since 2.6.0
*
* @uses wp_get_post_revision()
* @uses wp_delete_post()
*
* @param int|object $revision_id Revision ID or revision object.
* @return mixed Null or WP_Error if error, deleted post if success.
*/
function wp_delete_post_revision( $revision_id ) {
if ( !$revision = wp_get_post_revision( $revision_id ) )
return $revision;
$delete = wp_delete_post( $revision->ID );
if ( is_wp_error( $delete ) )
return $delete;
if ( $delete )
do_action( 'wp_delete_post_revision', $revision->ID, $revision );
return $delete;
}
/**
* Returns all revisions of specified post.
*
* @package WordPress
* @subpackage Post_Revisions
* @since 2.6.0
*
* @uses get_children()
*
* @param int|object $post_id Post ID or post object
* @return array empty if no revisions
*/
function wp_get_post_revisions( $post_id = 0, $args = null ) {
if ( ! WP_POST_REVISIONS )
return array();
if ( ( !$post = get_post( $post_id ) ) || empty( $post->ID ) )
return array();
$defaults = array( 'order' => 'DESC', 'orderby' => 'date' );
$args = wp_parse_args( $args, $defaults );
$args = array_merge( $args, array( 'post_parent' => $post->ID, 'post_type' => 'revision', 'post_status' => 'inherit' ) );
if ( !$revisions = get_children( $args ) )
return array();
return $revisions;
}
function _set_preview($post) {
if ( ! is_object($post) )
return $post;
$preview = wp_get_post_autosave($post->ID);
if ( ! is_object($preview) )
return $post;
$preview = sanitize_post($preview);
$post->post_content = $preview->post_content;
$post->post_title = $preview->post_title;
$post->post_excerpt = $preview->post_excerpt;
return $post;
}
function _show_post_preview() {
if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) {
$id = (int) $_GET['preview_id'];
if ( false == wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) )
wp_die( __('You do not have permission to preview drafts.') );
add_filter('the_preview', '_set_preview');
}
}

View File

@ -117,6 +117,8 @@ require( ABSPATH . WPINC . '/link-template.php' );
require( ABSPATH . WPINC . '/author-template.php' );
require( ABSPATH . WPINC . '/post.php' );
require( ABSPATH . WPINC . '/post-template.php' );
require( ABSPATH . WPINC . '/revision.php' );
require( ABSPATH . WPINC . '/post-formats.php' );
require( ABSPATH . WPINC . '/post-thumbnail-template.php' );
require( ABSPATH . WPINC . '/category.php' );
require( ABSPATH . WPINC . '/category-template.php' );