WordPress.org

Making WordPress.org


Ignore:
Timestamp:
05/14/2019 08:28:41 PM (3 years ago)
Author:
coffee2code
Message:

Gutenberg theme: Redirect handbook pages to the new Block Editor handbook in DevHub.

Fixes #4388.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/gutenberg/functions.php

    r8775 r8793  
    916916 */
    917917function gutenbergtheme_handbook_redirects() {
    918     if ( ! is_404() || ! get_query_var( 'handbook' ) ) {
     918    if ( 0 !== strpos( $_SERVER['REQUEST_URI'], '/gutenberg/handbook' ) ) {
    919919        return;
    920920    }
    921921
     922    $handbook_path = explode( '/', trailingslashit( $_SERVER['REQUEST_URI'] ), 4 );
     923    $handbook_path = $handbook_path[3] ?? null;
     924
     925    if ( is_null( $handbook_path ) ) {
     926        return;
     927    }
     928
    922929    // Any handbook pages where the filename changes should be listed here.
    923     // If only the parent path changes, it's not needed and WordPress will handle it automatically.
    924930    $redirects = [
    925         'designers-developers/developers/backwards-compatibility' => 'designers-developers/developers/backward-compatibility/',
    926         'reference/deprecated'           => 'designers-developers/developers/backward-compatibility/deprecations/',
     931        'designers-developers/developers/backwards-compatibility' => 'developers/backward-compatibility/',
     932        'reference/deprecated'           => 'developers/backward-compatibility/deprecations/',
    927933        'reference/design-principles'    => 'contributors/design/',
    928         'extensibility/annotations'      => 'designers-developers/developers/block-api/block-annotations/',
    929         'block-api/attributes'           => 'designers-developers/developers/block-api/block-attributes/',
    930         'block-api/deprecated-blocks'    => 'designers-developers/developers/block-api/block-deprecation/',
    931         'block-api/rich-text-api'        => 'designers-developers/developers/tutorials/format-api/',
    932         'block-api'                      => 'designers-developers/developers/block-api/block-registration/',
    933         'templates'                      => 'designers-developers/developers/block-api/block-templates/',
    934         'extensibility'                  => 'designers-developers/developers/filters/',
    935         'extensibility/autocomplete'     => 'designers-developers/developers/filters/autocomplete-filters/',
    936         'extensibility/extending-blocks' => 'designers-developers/developers/filters/block-filters/',
    937         'extensibility/parser'           => 'designers-developers/developers/filters/parser-filters/',
    938         'packages/packages-api-request'  => 'designers-developers/developers/packages/packages-api-fetch/',
    939         'blocks'                         => 'designers-developers/developers/tutorials/block-tutorial/intro/',
    940         'language'                       => 'designers-developers/key-concepts/',
     934        'extensibility/annotations'      => 'developers/block-api/block-annotations/',
     935        'block-api/attributes'           => 'developers/block-api/block-attributes/',
     936        'block-api/deprecated-blocks'    => 'developers/block-api/block-deprecation/',
     937        'block-api/rich-text-api'        => 'developers/tutorials/format-api/',
     938        'block-api'                      => 'developers/block-api/block-registration/',
     939        'templates'                      => 'developers/block-api/block-templates/',
     940        'extensibility'                  => 'developers/filters/',
     941        'extensibility/autocomplete'     => 'developers/filters/autocomplete-filters/',
     942        'extensibility/extending-blocks' => 'developers/filters/block-filters/',
     943        'extensibility/parser'           => 'developers/filters/parser-filters/',
     944        'packages/packages-api-request'  => 'packages/packages-api-fetch/',
     945        'blocks'                         => 'tutorials/block-tutorial/',
     946        'language'                       => 'key-concepts/',
    941947
    942948        // Redirects for index pages where needed
    943         // These next two look like an infinite redirect, but one will exist so the `is_404()` check prevents it above. This is for an inpending changes
    944         'designers-developers/developers/tutorials/block-tutorial'       => 'designers-developers/developers/tutorials/block-tutorial/intro/',
    945         'designers-developers/developers/tutorials/block-tutorial/intro' => 'designers-developers/developers/tutorials/block-tutorial/',
     949        'designers-developers/developers/tutorials/block-tutorial'       => 'tutorials/block-tutorial/',
     950        'designers-developers/developers/tutorials/block-tutorial/intro' => 'tutorials/block-tutorial/',
    946951    ];
    947952
    948     if ( isset( $redirects[ get_query_var( 'handbook' ) ] ) ) {
    949         wp_redirect(
    950             home_url(
    951                 '/handbook/' .
    952                 $redirects[ get_query_var( 'handbook' ) ]
    953             ),
    954             301
    955         );
    956         exit;
    957     }
    958 }
    959 add_action( 'template_redirect', 'gutenbergtheme_handbook_redirects' );
     953    // General path redirects. (More specific path first.)
     954    $path_redirects = [
     955        'block-api/'                                  => 'developers/block-api/',
     956        'designers-developers/data/'                  => 'data/',
     957        'designers-developers/designers/'             => 'designers/',
     958        'designers-developers/developers/components/' => 'components/',
     959        'designers-developers/developers/packages/'   => 'packages/',
     960        'designers-developers/developers/tutorials/'  => 'tutorials/',
     961        'designers-developers/developers/'            => 'developers/',
     962    ];
     963
     964    if ( ! empty( $redirects[ untrailingslashit( $handbook_path ) ] ) ) {
     965        $handbook_path = $redirects[ untrailingslashit( $handbook_path ) ];
     966    } else {
     967        foreach ( $path_redirects as $old_path => $new_path ) {
     968            if ( 0 === strpos( $handbook_path, $old_path ) ) {
     969                $handbook_path = str_replace( $old_path, $new_path, $handbook_path );
     970                break;
     971            }
     972        }
     973    }
     974
     975    $redirect_to = 'https://developer.wordpress.org/block-editor/' . $handbook_path ;
     976
     977    wp_redirect( $redirect_to );
     978    exit;
     979}
     980add_action( 'template_redirect', 'gutenbergtheme_handbook_redirects', 1 );
    960981
    961982/**
Note: See TracChangeset for help on using the changeset viewer.