Make WordPress Core

Changeset 34170


Ignore:
Timestamp:
09/15/2015 04:14:23 AM (9 years ago)
Author:
wonderboymusic
Message:

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.

Location:
trunk/src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/edit-form-advanced.php

    r34016 r34170  
    8282
    8383$messages = array();
    84 /** This filter is documented in wp-admin/includes/meta-boxes.php */
    85 $post_preview_url = apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $permalink ), $post );
     84$post_preview_url = get_preview_post_link( $post );
    8685
    8786$preview_link_html = $scheduled_link_html = $view_post_html = '';
     
    126125);
    127126
    128 /** This filter is documented in wp-admin/includes/meta-boxes.php */
    129 $page_preview_url = apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $permalink ), $post );
     127$page_preview_url = get_preview_post_link( $post );
    130128
    131129$messages['page'] = array(
  • trunk/src/wp-admin/includes/ajax-actions.php

    r34163 r34170  
    15281528    check_ajax_referer( 'getpermalink', 'getpermalinknonce' );
    15291529    $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
    1530     wp_die( add_query_arg( array( 'preview' => 'true' ), get_permalink( $post_id ) ) );
     1530    wp_die( get_preview_post_link( $post_id ) );
    15311531}
    15321532
  • trunk/src/wp-admin/includes/class-wp-posts-list-table.php

    r34084 r34170  
    11581158            if ( in_array( $post->post_status, array( 'pending', 'draft', 'future' ) ) ) {
    11591159                if ( $can_edit_post ) {
    1160                     $preview_link = set_url_scheme( get_permalink( $post->ID ) );
     1160                    $unpublished_link = set_url_scheme( get_permalink( $post ) );
    11611161                    /** This filter is documented in wp-admin/includes/meta-boxes.php */
    1162                     $preview_link = apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $preview_link ), $post );
     1162                    $preview_link = get_preview_post_link( $post, array(), $unpublished_link );
    11631163                    $actions['view'] = '<a href="' . esc_url( $preview_link ) . '" title="' . esc_attr( sprintf( __( 'Preview &#8220;%s&#8221;' ), $title ) ) . '" rel="permalink">' . __( 'Preview' ) . '</a>';
    11641164                }
  • trunk/src/wp-admin/includes/meta-boxes.php

    r34069 r34170  
    4141<div id="preview-action">
    4242<?php
     43$preview_link = esc_url( get_preview_post_link( $post ) );
    4344if ( 'publish' == $post->post_status ) {
    44     $preview_link = esc_url( get_permalink( $post->ID ) );
    4545    $preview_button = __( 'Preview Changes' );
    4646} else {
    47     $preview_link = set_url_scheme( get_permalink( $post->ID ) );
    48 
    49     /**
    50      * Filter the URI of a post preview in the post submit box.
    51      *
    52      * @since 2.0.5
    53      * @since 4.0.0 $post parameter was added.
    54      *
    55      * @param string  $preview_link URI the user will be directed to for a post preview.
    56      * @param WP_Post $post         Post object.
    57      */
    58     $preview_link = esc_url( apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $preview_link ), $post ) );
    5947    $preview_button = __( 'Preview' );
    6048}
  • trunk/src/wp-admin/includes/post.php

    r34020 r34170  
    13191319    if ( isset( $view_post ) ) {
    13201320        if ( 'draft' == $post->post_status ) {
    1321             $preview_link = set_url_scheme( get_permalink( $post->ID ) );
     1321            $draft_link = set_url_scheme( get_permalink( $post->ID ) );
    13221322            /** This filter is documented in wp-admin/includes/meta-boxes.php */
    1323             $preview_link = apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $preview_link ), $post );
     1323            $preview_link = get_preview_post_link( $post, array(), $draft_link );
    13241324            $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";
    13251325        } else {
     
    15121512
    15131513    if ( $locked ) {
     1514        $query_args = array();
    15141515        if ( get_post_type_object( $post->post_type )->public ) {
    1515             $preview_link = set_url_scheme( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) );
    1516 
    15171516            if ( 'publish' == $post->post_status || $user->ID != $post->post_author ) {
    15181517                // Latest content is in autosave
    15191518                $nonce = wp_create_nonce( 'post_preview_' . $post->ID );
    1520                 $preview_link = add_query_arg( array( 'preview_id' => $post->ID, 'preview_nonce' => $nonce ), $preview_link );
     1519                $query_args['preview_id'] = $post->ID;
     1520                $query_args['preview_nonce'] = $nonce;
    15211521            }
    1522         } else {
    1523             $preview_link = '';
    1524         }
    1525 
    1526         /** This filter is documented in wp-admin/includes/meta-boxes.php */
    1527         $preview_link = apply_filters( 'preview_post_link', $preview_link, $post );
     1522        }
     1523
     1524        $preview_link = get_preview_post_link( $post->ID, $query_args );
    15281525
    15291526        /**
     
    17111708        wp_die( $saved_post_id->get_error_message() );
    17121709
    1713     $query_args = array( 'preview' => 'true' );
     1710    $query_args = array();
    17141711
    17151712    if ( $is_autosave && $saved_post_id ) {
     
    17211718    }
    17221719
    1723     $url = add_query_arg( $query_args, get_permalink( $post->ID ) );
    1724 
    1725     /** This filter is documented in wp-admin/includes/meta-boxes.php */
    1726     return apply_filters( 'preview_post_link', $url, $post );
     1720    return get_preview_post_link( $post, $query_args );
    17271721}
    17281722
  • trunk/src/wp-includes/admin-bar.php

    r34122 r34170  
    551551        {
    552552            if ( 'draft' == $post->post_status ) {
    553                 $preview_link = set_url_scheme( get_permalink( $post->ID ) );
    554                 /** This filter is documented in wp-admin/includes/meta-boxes.php */
    555                 $preview_link = apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $preview_link ), $post );
     553                $draft_link = set_url_scheme( get_permalink( $post->ID ) );
     554                $preview_link = get_preview_post_link( $post, array(), $draft_link );
    556555                $wp_admin_bar->add_menu( array(
    557556                    'id' => 'preview',
  • trunk/src/wp-includes/link-template.php

    r34160 r34170  
    11601160
    11611161/**
     1162 * Retrieve preview post link.
     1163 *
     1164 * Get the preview post URL. Allow any number of query args to be appended.
     1165 *
     1166 * @since 4.4.0
     1167 *
     1168 * @param int    $post         Optional. Post ID or WP_Post object. Defaults to global post.
     1169 * @param array  $query_args   Optional. If preview query arg should be added. Or array of query args to be added.
     1170 * @param string $preview_link Optional. If a link other than the permalink should be used. Used by _wp_link_page.
     1171 * @return string
     1172 */
     1173function get_preview_post_link( $post = null, $query_args = array(), $preview_link = '' ) {
     1174    $post = get_post( $post );
     1175    if ( ! $post ) {
     1176        return;
     1177    }
     1178
     1179    $post_type_object = get_post_type_object( $post->post_type );
     1180    if ( is_post_type_viewable( $post_type_object ) ) {
     1181        if ( ! $preview_link ) {
     1182            $preview_link = get_permalink( $post );
     1183        }
     1184
     1185        $query_args['preview'] = true;
     1186        $preview_link = add_query_arg( $query_args, $preview_link );
     1187    }
     1188
     1189    /**
     1190     * Filter the URI of a post preview in the post submit box.
     1191     *
     1192     * @since 2.0.5
     1193     * @since 4.4.0 $post parameter was added.
     1194     *
     1195     * @param string  $preview_link URI the user will be directed to for a post preview.
     1196     * @param WP_Post $post         Post object.
     1197     */
     1198    return apply_filters( 'preview_post_link', $preview_link, $post );
     1199}
     1200
     1201/**
    11621202 * Retrieve edit posts link for post.
    11631203 *
  • trunk/src/wp-includes/post-template.php

    r34109 r34170  
    900900    global $wp_rewrite;
    901901    $post = get_post();
     902    $query_args = array();
    902903
    903904    if ( 1 == $i ) {
     
    913914
    914915    if ( is_preview() ) {
    915         $url = add_query_arg( array(
    916             'preview' => 'true'
    917         ), $url );
    918916
    919917        if ( ( 'draft' !== $post->post_status ) && isset( $_GET['preview_id'], $_GET['preview_nonce'] ) ) {
    920             $url = add_query_arg( array(
    921                 'preview_id'    => wp_unslash( $_GET['preview_id'] ),
    922                 'preview_nonce' => wp_unslash( $_GET['preview_nonce'] )
    923             ), $url );
    924         }
     918            $query_args['preview_id'] = wp_unslash( $_GET['preview_id'] );
     919            $query_args['preview_nonce'] = wp_unslash( $_GET['preview_nonce'] );
     920        }
     921
     922        $url = get_preview_post_link( $post, $query_args, $url );
    925923    }
    926924
Note: See TracChangeset for help on using the changeset viewer.