WordPress.org

Making WordPress.org

Changeset 8844


Ignore:
Timestamp:
05/21/2019 02:28:28 AM (3 months ago)
Author:
coffee2code
Message:

Handbook plugin: Support use of 'handbook' as slug for page acting as handbook landing page.

Also extracts logic into post_is_landing_page() for consistent and non-duplicative checking if a given post is acting as the landing page for a handbook.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/handbook.php

    r8671 r8844  
    256256
    257257    /**
     258     * Determines if the given values correspond to a post that acts as the
     259     * landing page for this handbook.
     260     *
     261     * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
     262     * @return bool True if the given information would make such a post the
     263     *              handbook's landing page.
     264     */
     265    protected function post_is_landing_page( $post = null ) {
     266        $is_landing_page = false;
     267
     268        $post_type = get_post_type( $post );
     269        $slug      = get_post_field( 'post_name', $post );
     270
     271        if (
     272            $post_type === $this->post_type
     273        &&
     274            (
     275                $post_type === $slug
     276            ||
     277                $post_type === "{$slug}-handbook"
     278            ||
     279                'handbook' === $slug
     280            )
     281        &&
     282            ! wp_get_post_parent_id( $post )
     283        ) {
     284            $is_landing_page = true;
     285        }
     286
     287        return $is_landing_page;
     288    }
     289
     290    /**
    258291     * For a handbook page acting as the root page for the handbook, change its
    259292     * permalink to be the equivalent of the post type archive link.
     
    266299
    267300        // Only change links for this handbook's post type.
    268         if ( $post_type === $this->post_type ) {
    269             // Verify post is not a child page and that its slug matches the criteria to
    270             // be a handbook root page.
    271             $post_slug = get_post_field( 'post_name', $post );
    272             if ( ( $post_slug === $post_type || "{$post_slug}-handbook" === $post_type ) && ! wp_get_post_parent_id( $post ) ) {
    273                 $post_link = get_post_type_archive_link( $post_type );
    274             }
     301        if ( $this->post_is_landing_page( $post ) ) {
     302            $post_link = get_post_type_archive_link( $post_type );
    275303        }
    276304
     
    291319            ! $wp_query->is_handbook_root
    292320            &&
    293             in_array( get_query_var( 'name' ), array( $this->post_type, substr( $this->post_type, 0, -9 ) ) )
     321            $this->post_is_landing_page( get_queried_object_id() )
    294322        ) {
    295323            wp_safe_redirect( get_post_type_archive_link( $this->post_type ) );
Note: See TracChangeset for help on using the changeset viewer.