WordPress.org

Make WordPress Core

Changeset 48619


Ignore:
Timestamp:
07/26/2020 01:12:51 AM (2 months ago)
Author:
SergeyBiryukov
Message:

Posts, Post Types: Make get_post_states() available on frontend.

This allows special pages to be denoted as such when editing a menu in the Customizer.

This applies to the Front Page, Posts Page, and Privacy Policy Page.

Follow-up to [47211], [47213], [47763].

Props dlh, whyisjake, SergeyBiryukov.
Fixes #46829. See #49374.

Location:
trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/template.php

    r48576 r48619  
    21502150
    21512151/**
    2152  * Retrieves an array of post states from a post.
    2153  *
    2154  * @since 5.3.0
    2155  *
    2156  * @param WP_Post $post The post to retrieve states for.
    2157  * @return string[] Array of post state labels keyed by their state.
    2158  */
    2159 function get_post_states( $post ) {
    2160     $post_states = array();
    2161     if ( isset( $_REQUEST['post_status'] ) ) {
    2162         $post_status = $_REQUEST['post_status'];
    2163     } else {
    2164         $post_status = '';
    2165     }
    2166 
    2167     if ( ! empty( $post->post_password ) ) {
    2168         $post_states['protected'] = _x( 'Password protected', 'post status' );
    2169     }
    2170 
    2171     if ( 'private' === $post->post_status && 'private' !== $post_status ) {
    2172         $post_states['private'] = _x( 'Private', 'post status' );
    2173     }
    2174 
    2175     if ( 'draft' === $post->post_status ) {
    2176         if ( get_post_meta( $post->ID, '_customize_changeset_uuid', true ) ) {
    2177             $post_states[] = __( 'Customization Draft' );
    2178         } elseif ( 'draft' !== $post_status ) {
    2179             $post_states['draft'] = _x( 'Draft', 'post status' );
    2180         }
    2181     } elseif ( 'trash' === $post->post_status && get_post_meta( $post->ID, '_customize_changeset_uuid', true ) ) {
    2182         $post_states[] = _x( 'Customization Draft', 'post status' );
    2183     }
    2184 
    2185     if ( 'pending' === $post->post_status && 'pending' !== $post_status ) {
    2186         $post_states['pending'] = _x( 'Pending', 'post status' );
    2187     }
    2188 
    2189     if ( is_sticky( $post->ID ) ) {
    2190         $post_states['sticky'] = _x( 'Sticky', 'post status' );
    2191     }
    2192 
    2193     if ( 'future' === $post->post_status ) {
    2194         $post_states['scheduled'] = _x( 'Scheduled', 'post status' );
    2195     }
    2196 
    2197     if ( 'page' === get_option( 'show_on_front' ) ) {
    2198         if ( intval( get_option( 'page_on_front' ) ) === $post->ID ) {
    2199             $post_states['page_on_front'] = _x( 'Front Page', 'page label' );
    2200         }
    2201 
    2202         if ( intval( get_option( 'page_for_posts' ) ) === $post->ID ) {
    2203             $post_states['page_for_posts'] = _x( 'Posts Page', 'page label' );
    2204         }
    2205     }
    2206 
    2207     if ( intval( get_option( 'wp_page_for_privacy_policy' ) ) === $post->ID ) {
    2208         $post_states['page_for_privacy_policy'] = _x( 'Privacy Policy Page', 'page label' );
    2209     }
    2210 
    2211     /**
    2212      * Filters the default post display states used in the posts list table.
    2213      *
    2214      * @since 2.8.0
    2215      * @since 3.6.0 Added the `$post` parameter.
    2216      *
    2217      * @param string[] $post_states An array of post display states.
    2218      * @param WP_Post  $post        The current post object.
    2219      */
    2220     return apply_filters( 'display_post_states', $post_states, $post );
    2221 }
    2222 
    2223 /**
    22242152 * Outputs the attachment media states as HTML.
    22252153 *
  • trunk/src/wp-includes/nav-menu.php

    r48439 r48619  
    817817                if ( $object ) {
    818818                    $menu_item->type_label = $object->labels->singular_name;
    819                     // Use post states for special pages (only in the admin).
    820                     if ( function_exists( 'get_post_states' ) ) {
    821                         $menu_post   = get_post( $menu_item->object_id );
    822                         $post_states = get_post_states( $menu_post );
    823                         if ( $post_states ) {
    824                             $menu_item->type_label = wp_strip_all_tags( implode( ', ', $post_states ) );
    825                         }
     819                    // Use post states for special pages.
     820                    $menu_post   = get_post( $menu_item->object_id );
     821                    $post_states = get_post_states( $menu_post );
     822                    if ( $post_states ) {
     823                        $menu_item->type_label = wp_strip_all_tags( implode( ', ', $post_states ) );
    826824                    }
    827825                } else {
  • trunk/src/wp-includes/post.php

    r48591 r48619  
    932932     */
    933933    return apply_filters( 'get_post_status', $post->post_status, $post );
     934}
     935
     936/**
     937 * Retrieves an array of post states from a post.
     938 *
     939 * @since 5.3.0
     940 *
     941 * @param WP_Post $post The post to retrieve states for.
     942 * @return string[] Array of post state labels keyed by their state.
     943 */
     944function get_post_states( $post ) {
     945    $post_states = array();
     946    if ( isset( $_REQUEST['post_status'] ) ) {
     947        $post_status = $_REQUEST['post_status'];
     948    } else {
     949        $post_status = '';
     950    }
     951
     952    if ( ! empty( $post->post_password ) ) {
     953        $post_states['protected'] = _x( 'Password protected', 'post status' );
     954    }
     955
     956    if ( 'private' === $post->post_status && 'private' !== $post_status ) {
     957        $post_states['private'] = _x( 'Private', 'post status' );
     958    }
     959
     960    if ( 'draft' === $post->post_status ) {
     961        if ( get_post_meta( $post->ID, '_customize_changeset_uuid', true ) ) {
     962            $post_states[] = __( 'Customization Draft' );
     963        } elseif ( 'draft' !== $post_status ) {
     964            $post_states['draft'] = _x( 'Draft', 'post status' );
     965        }
     966    } elseif ( 'trash' === $post->post_status && get_post_meta( $post->ID, '_customize_changeset_uuid', true ) ) {
     967        $post_states[] = _x( 'Customization Draft', 'post status' );
     968    }
     969
     970    if ( 'pending' === $post->post_status && 'pending' !== $post_status ) {
     971        $post_states['pending'] = _x( 'Pending', 'post status' );
     972    }
     973
     974    if ( is_sticky( $post->ID ) ) {
     975        $post_states['sticky'] = _x( 'Sticky', 'post status' );
     976    }
     977
     978    if ( 'future' === $post->post_status ) {
     979        $post_states['scheduled'] = _x( 'Scheduled', 'post status' );
     980    }
     981
     982    if ( 'page' === get_option( 'show_on_front' ) ) {
     983        if ( intval( get_option( 'page_on_front' ) ) === $post->ID ) {
     984            $post_states['page_on_front'] = _x( 'Front Page', 'page label' );
     985        }
     986
     987        if ( intval( get_option( 'page_for_posts' ) ) === $post->ID ) {
     988            $post_states['page_for_posts'] = _x( 'Posts Page', 'page label' );
     989        }
     990    }
     991
     992    if ( intval( get_option( 'wp_page_for_privacy_policy' ) ) === $post->ID ) {
     993        $post_states['page_for_privacy_policy'] = _x( 'Privacy Policy Page', 'page label' );
     994    }
     995
     996    /**
     997     * Filters the default post display states used in the posts list table.
     998     *
     999     * @since 2.8.0
     1000     * @since 3.6.0 Added the `$post` parameter.
     1001     *
     1002     * @param string[] $post_states An array of post display states.
     1003     * @param WP_Post  $post        The current post object.
     1004     */
     1005    return apply_filters( 'display_post_states', $post_states, $post );
    9341006}
    9351007
Note: See TracChangeset for help on using the changeset viewer.