WordPress.org

Make WordPress Core

Changeset 33666


Ignore:
Timestamp:
08/20/2015 07:39:57 PM (6 years ago)
Author:
wonderboymusic
Message:

Custom Post Types:

  • Introduce is_post_type_viewable( $post_type_object )
  • Separate the HTML bits from the translatable bits in the post messages array in edit-form-advanced.php
  • Don't show certain UI pieces when a post is not viewable on the front end

When a custom post type item is not viewable on the front end, we don't want to show links to View it (on the front end) all over the admin. We also want to hide the Preview link, et al. We also want our admin messages to not contain said links.

Custom post types with public_queryable set to false are not viewable on the front end.
'page' is viewable on the front end, but 'page' is a _builtin type, and public_queryable is set to false when it is registered - see WP::parse_request() for when public_queryable gets used.

This is confusing, but also somewhat straightforward: to determine if a post type is viewable on the front end, we can check one way for _builtin => true and another way for _builtin => false:

$post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public )

If a post type is publicly_queryable, it's viewable. If that value is false, it is viewable if it's a _builtin type that is also public.

I am in search of edge cases, so this shall land.

Props wonderboymusic, DrewAPicture.
See #17609.

Location:
trunk/src
Files:
4 edited

Legend:

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

    r33641 r33666  
    8585$post_preview_url = apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $permalink ), $post );
    8686
     87$preview_link_html = $scheduled_link_html = $view_post_html = '';
     88
     89$viewable = is_post_type_viewable( $post_type_object );
     90
     91if ( $viewable ) {
     92    // Preview link.
     93    $preview_link_html = sprintf( ' <a target="_blank" href="%s">%s</a>',
     94        esc_url( $post_preview_url ),
     95        __( 'Preview post' )
     96    );
     97
     98    // Scheduled preview link.
     99    $scheduled_link_html = sprintf( ' <a target="_blank" href="%s">%s</a>',
     100        esc_url( $permalink ),
     101        __( 'Preview post' )
     102    );
     103
     104    // View post link.
     105    $view_post_html = sprintf( ' <a href="%s">">%</a>',
     106        esc_url( $permalink ),
     107        __( 'View post' )
     108    );
     109}
     110
     111/* translators: Publish box date format, see http://php.net/date */
     112$scheduled_date = date_i18n( __( 'M j, Y @ H:i' ), strtotime( $post->post_date ) );
    87113$messages['post'] = array(
    88114     0 => '', // Unused. Messages start at index 1.
    89      1 => sprintf( __('Post updated. <a href="%s">View post</a>'), esc_url( $permalink ) ),
     115     1 => __( 'Post updated.' ) . $view_post_html,
    90116     2 => __('Custom field updated.'),
    91117     3 => __('Custom field deleted.'),
     
    93119    /* translators: %s: date and time of the revision */
    94120     5 => isset($_GET['revision']) ? sprintf( __('Post restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
    95      6 => sprintf( __('Post published. <a href="%s">View post</a>'), esc_url( $permalink ) ),
     121     6 => __( 'Post published.' ) . $view_post_html,
    96122     7 => __('Post saved.'),
    97      8 => sprintf( __('Post submitted. <a target="_blank" href="%s">Preview post</a>'), esc_url( $post_preview_url ) ),
    98      9 => sprintf( __('Post scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview post</a>'),
    99         /* translators: Publish box date format, see http://php.net/date */
    100         date_i18n( __( 'M j, Y @ H:i' ), strtotime( $post->post_date ) ), esc_url( $permalink ) ),
    101     10 => sprintf( __('Post draft updated. <a target="_blank" href="%s">Preview post</a>'), esc_url( $post_preview_url ) ),
     123     8 => __( 'Post submitted.' ) . $preview_link_html,
     124     9 => sprintf( __( 'Post scheduled for: <strong>%1$s</strong>' ), $scheduled_date ) . $scheduled_link_html,
     125    10 => __( 'Post draft updated.' ) . $preview_link_html,
    102126);
    103127
     
    509533<div class="inside">
    510534<?php
     535if ( $viewable ) :
    511536$sample_permalink_html = $post_type_object->public ? get_sample_permalink_html($post->ID) : '';
    512537$shortlink = wp_get_shortlink($post->ID, 'post');
     
    526551<?php
    527552}
     553endif;
    528554?>
    529555</div>
  • trunk/src/wp-admin/includes/class-wp-posts-list-table.php

    r33644 r33666  
    10731073        }
    10741074
    1075         if ( $post_type_object->public ) {
     1075        if ( is_post_type_viewable( $post_type_object ) ) {
    10761076            $title = _draft_or_post_title();
    10771077            if ( in_array( $post->post_status, array( 'pending', 'draft', 'future' ) ) ) {
  • trunk/src/wp-admin/includes/meta-boxes.php

    r33433 r33666  
    3232<?php if ( 'publish' != $post->post_status && 'future' != $post->post_status && 'pending' != $post->post_status ) { ?>
    3333<input <?php if ( 'private' == $post->post_status ) { ?>style="display:none"<?php } ?> type="submit" name="save" id="save-post" value="<?php esc_attr_e('Save Draft'); ?>" class="button" />
     34<span class="spinner"></span>
    3435<?php } elseif ( 'pending' == $post->post_status && $can_publish ) { ?>
    3536<input type="submit" name="save" id="save-post" value="<?php esc_attr_e('Save as Pending'); ?>" class="button" />
     37<span class="spinner"></span>
    3638<?php } ?>
    37 <span class="spinner"></span>
    38 </div>
    39 <?php if ( $post_type_object->public ) : ?>
     39</div>
     40<?php if ( is_post_type_viewable( $post_type_object ) ) : ?>
    4041<div id="preview-action">
    4142<?php
  • trunk/src/wp-includes/post.php

    r33637 r33666  
    18451845
    18461846/**
     1847 * Determines whether a post type is considered "viewable".
     1848 *
     1849 * For built-in post types such as posts and pages, the 'public' value will be evaluated.
     1850 * For all others, the 'publicly_queryable' value will be used.
     1851 *
     1852 * @since 4.4.0
     1853 *
     1854 * @param object $post_type_object Post type object.
     1855 * @return bool Whether the post type should be considered viewable.
     1856 */
     1857function is_post_type_viewable( $post_type_object ) {
     1858    return $post_type_object->publicly_queryable || ( $post_type_object->_builtin && $post_type_object->public );
     1859}
     1860
     1861/**
    18471862 * Retrieve list of latest posts or posts matching criteria.
    18481863 *
Note: See TracChangeset for help on using the changeset viewer.