Making WordPress.org

Changeset 9084


Ignore:
Timestamp:
07/29/2019 07:00:42 PM (6 years ago)
Author:
coffee2code
Message:

Developer: Handle redirects for Block Editor handbook pages that have been renamed.

Ported over code for similar handling from Gutenberg site theme.

Props mkaz, Soean, coffee2code.
Fixes #4582, #4619.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/import-block-editor.php

    r8803 r9084  
    1212        );
    1313
     14        add_filter( 'template_redirect',               array( $this, 'redirects' ), 1 );
    1415        add_filter( 'handbook_label', array( $this, 'change_handbook_label' ), 10, 2 );
    1516        add_filter( 'handbook_display_toc',            array( $this, 'disable_toc' ) );
     
    2930            remove_filter( 'wp_kses_allowed_html', array( __CLASS__, 'allow_extra_tags' ), 10, 1 );
    3031        } );
     32    }
     33
     34    /**
     35     * Handles redirects for renamed/removed handbook pages.
     36     */
     37    public function redirects() {
     38        if ( 0 !== strpos( $_SERVER['REQUEST_URI'], '/block-editor/' ) ) {
     39            return;
     40        }
     41
     42        $handbook_path = explode( '/', trailingslashit( $_SERVER['REQUEST_URI'] ), 3 );
     43        $handbook_path = $handbook_path[2] ?? null;
     44
     45        if ( is_null( $handbook_path ) ) {
     46            return;
     47        }
     48
     49        // Any handbook pages where the slug changes should be listed here.
     50        $redirects = [
     51            'tutorials/block-tutorial/block-controls-toolbars-and-inspector' => 'tutorials/block-tutorial/block-controls-toolbar-and-sidebar/',
     52        ];
     53
     54        // General path redirects. (More specific path first.)
     55        $path_redirects = [
     56            // 'some-path/' => 'new-path/',
     57        ];
     58
     59        $new_handbook_path = '';
     60        if ( ! empty( $redirects[ untrailingslashit( $handbook_path ) ] ) ) {
     61            $new_handbook_path = $redirects[ untrailingslashit( $handbook_path ) ];
     62        } else {
     63            foreach ( $path_redirects as $old_path => $new_path ) {
     64                if ( 0 === strpos( $handbook_path, $old_path ) ) {
     65                    $new_handbook_path = str_replace( $old_path, $new_path, $handbook_path );
     66                    break;
     67                }
     68            }
     69        }
     70
     71        if ( $new_handbook_path ) {
     72            $redirect_to = get_post_type_archive_link( $this->get_post_type() ) . $new_handbook_path;
     73
     74            wp_safe_redirect( $redirect_to, 301 );
     75            exit;
     76        }
    3177    }
    3278
Note: See TracChangeset for help on using the changeset viewer.