Making WordPress.org

Changeset 3681


Ignore:
Timestamp:
07/15/2016 06:50:59 PM (8 years ago)
Author:
coffee2code
Message:

Handbook plugin: Better support a handbook page acting as the handbook's root/landing page.

  • Override the permalink for such a page to be the handbook post type's archive URL.
  • Redirect the original permalink for such a page to the handbook post type archive URL.
File:
1 edited

Legend:

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

    r3484 r3681  
    121121        add_action( 'widgets_init',                       array( $this, 'register_post_type' ) );
    122122        add_filter( 'post_type_link',                     array( $this, 'post_type_link' ), 10, 2 );
     123        add_action( 'template_redirect',                  array( $this, 'redirect_handbook_root_page' ) );
    123124        add_filter( 'pre_get_posts',                      array( $this, 'pre_get_posts' ) );
    124125        add_action( 'widgets_init',                       array( $this, 'handbook_sidebar' ), 11 ); // After P2
     
    138139     */
    139140    function add_body_class( $classes ) {
    140         if ( is_post_type_archive( $this->post_type )  ) {
    141             $classes[] = 'post-type-archive-handbook';
    142         } elseif ( is_singular( $this->post_type ) ) {
     141        if ( wporg_is_handbook( $this->post_type ) ) {
    143142            $classes[] = 'single-handbook';
    144143        }
     
    237236    }
    238237
    239     function post_type_link( $link, $post ) {
    240         if ( $post->post_type === $this->post_type && $post->post_name === $this->post_type ) {
    241             return get_post_type_archive_link( $this->post_type );
    242         }
    243 
    244         return $link;
     238    /**
     239     * For a handbook page acting as the root page for the handbook, change its
     240     * permalink to be the equivalent of the post type archive link.
     241     *
     242     * @param string  $post_link The post's permalink.
     243     * @param WP_Post $post      The post in question.
     244     */
     245    function post_type_link( $post_link, $post ) {
     246        $post_type = get_post_type( $post );
     247
     248        // Only change links for this handbook's post type.
     249        if ( $post_type === $this->post_type ) {
     250            // Verify post is not a child page and that its slug matches the criteria to
     251            // be a handbook root page.
     252            $post_slug = get_post_field( 'post_name', $post );
     253            if ( ( $post_slug === $post_type || "{$post_slug}-handbook" === $post_type ) && ! wp_get_post_parent_id( $post ) ) {
     254                $post_link = get_post_type_archive_link( $post_type );
     255            }
     256        }
     257
     258        return $post_link;
     259    }
     260
     261    /**
     262     * For a handbook page acting as the root page for the handbook, redirect to the
     263     * post type archive link for the handbook.
     264     */
     265    function redirect_handbook_root_page() {
     266        if ( is_singular( $this->post_type )
     267            &&
     268            ! is_post_type_archive( $this->post_type )
     269            &&
     270            in_array( get_query_var( 'name' ), array( $this->post_type, substr( $this->post_type, 0, -9 ) ) )
     271        ) {
     272            wp_safe_redirect( get_post_type_archive_link( $this->post_type ) );
     273            exit;
     274        }
    245275    }
    246276
     
    256286                $query->set( 'p', $page->ID );
    257287                $query->is_singular = true;
    258                 $query->is_post_type_archive = false;
    259288            }
    260289            $query->set( 'handbook', $this->post_type );
Note: See TracChangeset for help on using the changeset viewer.