Create a function, `get_preview_post_link()`, to DRY the logic for applying the `'preview_post_link'` filter to a URL.

Props TomHarrigan, wonderboymusic.
Fixes #24345.

Built from https://develop.svn.wordpress.org/trunk@34170


git-svn-id: http://core.svn.wordpress.org/trunk@34138 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor 2015-09-15 04:15:25 +00:00
parent ee42803e21
commit 54829b6305
9 changed files with 62 additions and 45 deletions

View File

@ -81,8 +81,7 @@ if ( ! $permalink ) {
} }
$messages = array(); $messages = array();
/** This filter is documented in wp-admin/includes/meta-boxes.php */ $post_preview_url = get_preview_post_link( $post );
$post_preview_url = apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $permalink ), $post );
$preview_link_html = $scheduled_link_html = $view_post_html = ''; $preview_link_html = $scheduled_link_html = $view_post_html = '';
@ -125,8 +124,7 @@ $messages['post'] = array(
10 => __( 'Post draft updated.' ) . $preview_link_html, 10 => __( 'Post draft updated.' ) . $preview_link_html,
); );
/** This filter is documented in wp-admin/includes/meta-boxes.php */ $page_preview_url = get_preview_post_link( $post );
$page_preview_url = apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $permalink ), $post );
$messages['page'] = array( $messages['page'] = array(
0 => '', // Unused. Messages start at index 1. 0 => '', // Unused. Messages start at index 1.

View File

@ -1527,7 +1527,7 @@ function wp_ajax_menu_quick_search() {
function wp_ajax_get_permalink() { function wp_ajax_get_permalink() {
check_ajax_referer( 'getpermalink', 'getpermalinknonce' ); check_ajax_referer( 'getpermalink', 'getpermalinknonce' );
$post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0; $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
wp_die( add_query_arg( array( 'preview' => 'true' ), get_permalink( $post_id ) ) ); wp_die( get_preview_post_link( $post_id ) );
} }
/** /**

View File

@ -1157,9 +1157,9 @@ class WP_Posts_List_Table extends WP_List_Table {
$title = _draft_or_post_title(); $title = _draft_or_post_title();
if ( in_array( $post->post_status, array( 'pending', 'draft', 'future' ) ) ) { if ( in_array( $post->post_status, array( 'pending', 'draft', 'future' ) ) ) {
if ( $can_edit_post ) { if ( $can_edit_post ) {
$preview_link = set_url_scheme( get_permalink( $post->ID ) ); $unpublished_link = set_url_scheme( get_permalink( $post ) );
/** This filter is documented in wp-admin/includes/meta-boxes.php */ /** This filter is documented in wp-admin/includes/meta-boxes.php */
$preview_link = apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $preview_link ), $post ); $preview_link = get_preview_post_link( $post, array(), $unpublished_link );
$actions['view'] = '<a href="' . esc_url( $preview_link ) . '" title="' . esc_attr( sprintf( __( 'Preview &#8220;%s&#8221;' ), $title ) ) . '" rel="permalink">' . __( 'Preview' ) . '</a>'; $actions['view'] = '<a href="' . esc_url( $preview_link ) . '" title="' . esc_attr( sprintf( __( 'Preview &#8220;%s&#8221;' ), $title ) ) . '" rel="permalink">' . __( 'Preview' ) . '</a>';
} }
} elseif ( 'trash' != $post->post_status ) { } elseif ( 'trash' != $post->post_status ) {

View File

@ -40,22 +40,10 @@ function post_submit_meta_box($post, $args = array() ) {
<?php if ( is_post_type_viewable( $post_type_object ) ) : ?> <?php if ( is_post_type_viewable( $post_type_object ) ) : ?>
<div id="preview-action"> <div id="preview-action">
<?php <?php
$preview_link = esc_url( get_preview_post_link( $post ) );
if ( 'publish' == $post->post_status ) { if ( 'publish' == $post->post_status ) {
$preview_link = esc_url( get_permalink( $post->ID ) );
$preview_button = __( 'Preview Changes' ); $preview_button = __( 'Preview Changes' );
} else { } else {
$preview_link = set_url_scheme( get_permalink( $post->ID ) );
/**
* Filter the URI of a post preview in the post submit box.
*
* @since 2.0.5
* @since 4.0.0 $post parameter was added.
*
* @param string $preview_link URI the user will be directed to for a post preview.
* @param WP_Post $post Post object.
*/
$preview_link = esc_url( apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $preview_link ), $post ) );
$preview_button = __( 'Preview' ); $preview_button = __( 'Preview' );
} }
?> ?>

View File

@ -1318,9 +1318,9 @@ function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) {
if ( isset( $view_post ) ) { if ( isset( $view_post ) ) {
if ( 'draft' == $post->post_status ) { if ( 'draft' == $post->post_status ) {
$preview_link = set_url_scheme( get_permalink( $post->ID ) ); $draft_link = set_url_scheme( get_permalink( $post->ID ) );
/** This filter is documented in wp-admin/includes/meta-boxes.php */ /** This filter is documented in wp-admin/includes/meta-boxes.php */
$preview_link = apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $preview_link ), $post ); $preview_link = get_preview_post_link( $post, array(), $draft_link );
$return .= "<span id='view-post-btn'><a href='" . esc_url( $preview_link ) . "' class='button button-small' target='wp-preview-{$post->ID}'>$view_post</a></span>\n"; $return .= "<span id='view-post-btn'><a href='" . esc_url( $preview_link ) . "' class='button button-small' target='wp-preview-{$post->ID}'>$view_post</a></span>\n";
} else { } else {
if ( 'publish' === $post->post_status ) { if ( 'publish' === $post->post_status ) {
@ -1511,20 +1511,17 @@ function _admin_notice_post_locked() {
<?php <?php
if ( $locked ) { if ( $locked ) {
$query_args = array();
if ( get_post_type_object( $post->post_type )->public ) { if ( get_post_type_object( $post->post_type )->public ) {
$preview_link = set_url_scheme( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) );
if ( 'publish' == $post->post_status || $user->ID != $post->post_author ) { if ( 'publish' == $post->post_status || $user->ID != $post->post_author ) {
// Latest content is in autosave // Latest content is in autosave
$nonce = wp_create_nonce( 'post_preview_' . $post->ID ); $nonce = wp_create_nonce( 'post_preview_' . $post->ID );
$preview_link = add_query_arg( array( 'preview_id' => $post->ID, 'preview_nonce' => $nonce ), $preview_link ); $query_args['preview_id'] = $post->ID;
$query_args['preview_nonce'] = $nonce;
} }
} else {
$preview_link = '';
} }
/** This filter is documented in wp-admin/includes/meta-boxes.php */ $preview_link = get_preview_post_link( $post->ID, $query_args );
$preview_link = apply_filters( 'preview_post_link', $preview_link, $post );
/** /**
* Filter whether to allow the post lock to be overridden. * Filter whether to allow the post lock to be overridden.
@ -1710,7 +1707,7 @@ function post_preview() {
if ( is_wp_error( $saved_post_id ) ) if ( is_wp_error( $saved_post_id ) )
wp_die( $saved_post_id->get_error_message() ); wp_die( $saved_post_id->get_error_message() );
$query_args = array( 'preview' => 'true' ); $query_args = array();
if ( $is_autosave && $saved_post_id ) { if ( $is_autosave && $saved_post_id ) {
$query_args['preview_id'] = $post->ID; $query_args['preview_id'] = $post->ID;
@ -1720,10 +1717,7 @@ function post_preview() {
$query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] ); $query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
} }
$url = add_query_arg( $query_args, get_permalink( $post->ID ) ); return get_preview_post_link( $post, $query_args );
/** This filter is documented in wp-admin/includes/meta-boxes.php */
return apply_filters( 'preview_post_link', $url, $post );
} }
/** /**

View File

@ -550,9 +550,8 @@ function wp_admin_bar_edit_menu( $wp_admin_bar ) {
&& ( $post_type_object->show_in_admin_bar ) ) && ( $post_type_object->show_in_admin_bar ) )
{ {
if ( 'draft' == $post->post_status ) { if ( 'draft' == $post->post_status ) {
$preview_link = set_url_scheme( get_permalink( $post->ID ) ); $draft_link = set_url_scheme( get_permalink( $post->ID ) );
/** This filter is documented in wp-admin/includes/meta-boxes.php */ $preview_link = get_preview_post_link( $post, array(), $draft_link );
$preview_link = apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $preview_link ), $post );
$wp_admin_bar->add_menu( array( $wp_admin_bar->add_menu( array(
'id' => 'preview', 'id' => 'preview',
'title' => $post_type_object->labels->view_item, 'title' => $post_type_object->labels->view_item,

View File

@ -1158,6 +1158,46 @@ function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
return apply_filters( 'post_type_archive_feed_link', $link, $feed ); return apply_filters( 'post_type_archive_feed_link', $link, $feed );
} }
/**
* Retrieve preview post link.
*
* Get the preview post URL. Allow any number of query args to be appended.
*
* @since 4.4.0
*
* @param int $post Optional. Post ID or WP_Post object. Defaults to global post.
* @param array $query_args Optional. If preview query arg should be added. Or array of query args to be added.
* @param string $preview_link Optional. If a link other than the permalink should be used. Used by _wp_link_page.
* @return string
*/
function get_preview_post_link( $post = null, $query_args = array(), $preview_link = '' ) {
$post = get_post( $post );
if ( ! $post ) {
return;
}
$post_type_object = get_post_type_object( $post->post_type );
if ( is_post_type_viewable( $post_type_object ) ) {
if ( ! $preview_link ) {
$preview_link = get_permalink( $post );
}
$query_args['preview'] = true;
$preview_link = add_query_arg( $query_args, $preview_link );
}
/**
* Filter the URI of a post preview in the post submit box.
*
* @since 2.0.5
* @since 4.4.0 $post parameter was added.
*
* @param string $preview_link URI the user will be directed to for a post preview.
* @param WP_Post $post Post object.
*/
return apply_filters( 'preview_post_link', $preview_link, $post );
}
/** /**
* Retrieve edit posts link for post. * Retrieve edit posts link for post.
* *

View File

@ -899,6 +899,7 @@ function wp_link_pages( $args = '' ) {
function _wp_link_page( $i ) { function _wp_link_page( $i ) {
global $wp_rewrite; global $wp_rewrite;
$post = get_post(); $post = get_post();
$query_args = array();
if ( 1 == $i ) { if ( 1 == $i ) {
$url = get_permalink(); $url = get_permalink();
@ -912,16 +913,13 @@ function _wp_link_page( $i ) {
} }
if ( is_preview() ) { if ( is_preview() ) {
$url = add_query_arg( array(
'preview' => 'true'
), $url );
if ( ( 'draft' !== $post->post_status ) && isset( $_GET['preview_id'], $_GET['preview_nonce'] ) ) { if ( ( 'draft' !== $post->post_status ) && isset( $_GET['preview_id'], $_GET['preview_nonce'] ) ) {
$url = add_query_arg( array( $query_args['preview_id'] = wp_unslash( $_GET['preview_id'] );
'preview_id' => wp_unslash( $_GET['preview_id'] ), $query_args['preview_nonce'] = wp_unslash( $_GET['preview_nonce'] );
'preview_nonce' => wp_unslash( $_GET['preview_nonce'] )
), $url );
} }
$url = get_preview_post_link( $post, $query_args, $url );
} }
return '<a href="' . esc_url( $url ) . '">'; return '<a href="' . esc_url( $url ) . '">';

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.4-alpha-34169'; $wp_version = '4.4-alpha-34170';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.