From c0d850878acb9c712e6fa97206e8ac9bb22bab95 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Tue, 15 Sep 2015 04:14:23 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@34170 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/edit-form-advanced.php | 6 +-- src/wp-admin/includes/ajax-actions.php | 2 +- .../includes/class-wp-posts-list-table.php | 4 +- src/wp-admin/includes/meta-boxes.php | 14 +------ src/wp-admin/includes/post.php | 22 ++++------ src/wp-includes/admin-bar.php | 5 +-- src/wp-includes/link-template.php | 40 +++++++++++++++++++ src/wp-includes/post-template.php | 12 +++--- 8 files changed, 61 insertions(+), 44 deletions(-) diff --git a/src/wp-admin/edit-form-advanced.php b/src/wp-admin/edit-form-advanced.php index bcf1144c24..e650a4a7bf 100644 --- a/src/wp-admin/edit-form-advanced.php +++ b/src/wp-admin/edit-form-advanced.php @@ -81,8 +81,7 @@ if ( ! $permalink ) { } $messages = array(); -/** This filter is documented in wp-admin/includes/meta-boxes.php */ -$post_preview_url = apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $permalink ), $post ); +$post_preview_url = get_preview_post_link( $post ); $preview_link_html = $scheduled_link_html = $view_post_html = ''; @@ -125,8 +124,7 @@ $messages['post'] = array( 10 => __( 'Post draft updated.' ) . $preview_link_html, ); -/** This filter is documented in wp-admin/includes/meta-boxes.php */ -$page_preview_url = apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $permalink ), $post ); +$page_preview_url = get_preview_post_link( $post ); $messages['page'] = array( 0 => '', // Unused. Messages start at index 1. diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index 1d527aaf1a..5f8c01f875 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -1527,7 +1527,7 @@ function wp_ajax_menu_quick_search() { function wp_ajax_get_permalink() { check_ajax_referer( 'getpermalink', 'getpermalinknonce' ); $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 ) ); } /** diff --git a/src/wp-admin/includes/class-wp-posts-list-table.php b/src/wp-admin/includes/class-wp-posts-list-table.php index adc79230a4..c4883c54be 100644 --- a/src/wp-admin/includes/class-wp-posts-list-table.php +++ b/src/wp-admin/includes/class-wp-posts-list-table.php @@ -1157,9 +1157,9 @@ class WP_Posts_List_Table extends WP_List_Table { $title = _draft_or_post_title(); if ( in_array( $post->post_status, array( 'pending', 'draft', 'future' ) ) ) { 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 */ - $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'] = '' . __( 'Preview' ) . ''; } } elseif ( 'trash' != $post->post_status ) { diff --git a/src/wp-admin/includes/meta-boxes.php b/src/wp-admin/includes/meta-boxes.php index c4f318927b..cc4d0b2e1e 100644 --- a/src/wp-admin/includes/meta-boxes.php +++ b/src/wp-admin/includes/meta-boxes.php @@ -40,22 +40,10 @@ function post_submit_meta_box($post, $args = array() ) {
post_status ) { - $preview_link = esc_url( get_permalink( $post->ID ) ); $preview_button = __( 'Preview Changes' ); } 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' ); } ?> diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index ac92244987..66d72cb264 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -1318,9 +1318,9 @@ function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) { if ( isset( $view_post ) ) { 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 = apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $preview_link ), $post ); + $preview_link = get_preview_post_link( $post, array(), $draft_link ); $return .= "$view_post\n"; } else { if ( 'publish' === $post->post_status ) { @@ -1511,20 +1511,17 @@ function _admin_notice_post_locked() { 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 ) { // Latest content is in autosave $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 = apply_filters( 'preview_post_link', $preview_link, $post ); + $preview_link = get_preview_post_link( $post->ID, $query_args ); /** * Filter whether to allow the post lock to be overridden. @@ -1710,7 +1707,7 @@ function post_preview() { if ( is_wp_error( $saved_post_id ) ) wp_die( $saved_post_id->get_error_message() ); - $query_args = array( 'preview' => 'true' ); + $query_args = array(); if ( $is_autosave && $saved_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'] ); } - $url = add_query_arg( $query_args, get_permalink( $post->ID ) ); - - /** This filter is documented in wp-admin/includes/meta-boxes.php */ - return apply_filters( 'preview_post_link', $url, $post ); + return get_preview_post_link( $post, $query_args ); } /** diff --git a/src/wp-includes/admin-bar.php b/src/wp-includes/admin-bar.php index 70dcecea73..8a1a2be301 100644 --- a/src/wp-includes/admin-bar.php +++ b/src/wp-includes/admin-bar.php @@ -550,9 +550,8 @@ function wp_admin_bar_edit_menu( $wp_admin_bar ) { && ( $post_type_object->show_in_admin_bar ) ) { if ( 'draft' == $post->post_status ) { - $preview_link = set_url_scheme( get_permalink( $post->ID ) ); - /** 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 ); + $draft_link = set_url_scheme( get_permalink( $post->ID ) ); + $preview_link = get_preview_post_link( $post, array(), $draft_link ); $wp_admin_bar->add_menu( array( 'id' => 'preview', 'title' => $post_type_object->labels->view_item, diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index baba9abf21..f35ae0613d 100644 --- a/src/wp-includes/link-template.php +++ b/src/wp-includes/link-template.php @@ -1158,6 +1158,46 @@ function get_post_type_archive_feed_link( $post_type, $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. * diff --git a/src/wp-includes/post-template.php b/src/wp-includes/post-template.php index f2354bc37e..237d07a520 100644 --- a/src/wp-includes/post-template.php +++ b/src/wp-includes/post-template.php @@ -899,6 +899,7 @@ function wp_link_pages( $args = '' ) { function _wp_link_page( $i ) { global $wp_rewrite; $post = get_post(); + $query_args = array(); if ( 1 == $i ) { $url = get_permalink(); @@ -912,16 +913,13 @@ function _wp_link_page( $i ) { } if ( is_preview() ) { - $url = add_query_arg( array( - 'preview' => 'true' - ), $url ); if ( ( 'draft' !== $post->post_status ) && isset( $_GET['preview_id'], $_GET['preview_nonce'] ) ) { - $url = add_query_arg( array( - 'preview_id' => wp_unslash( $_GET['preview_id'] ), - 'preview_nonce' => wp_unslash( $_GET['preview_nonce'] ) - ), $url ); + $query_args['preview_id'] = wp_unslash( $_GET['preview_id'] ); + $query_args['preview_nonce'] = wp_unslash( $_GET['preview_nonce'] ); } + + $url = get_preview_post_link( $post, $query_args, $url ); } return '';