Making WordPress.org


Ignore:
Timestamp:
10/16/2020 07:02:54 AM (5 years ago)
Author:
dd32
Message:

Open-source a bunch of redirects that were in sunrise.php (and a handful of similarly related redirects that didn't exist).

This will hopefully allow simpler recording of some of the generic redirects that are active, and may allow others to easier add/patch them.

See #5345.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/mu-plugins/pub/wporg-redirects.php

    r9868 r10385  
    66if ( 1 === get_current_blog_id() ) {
    77    add_action( 'template_redirect', function() {
     8        // WordPress.org/feed/* should redirect to WordPress.org/news/feed/*
    89        if ( is_feed() ) {
    9             // WordPress.org/feed/* should redirect to WordPress.org/news/feed/*
    1010            wp_safe_redirect( '/news/feed/' . ( 'feed' !== get_query_var('feed') ? get_query_var('feed') : '' ), 301 );
    1111            exit;
     12
     13        // WordPress.org does not have a specific site search, only the global WordPress.org search
    1214        } elseif ( is_search() ) {
    1315            wp_safe_redirect( '/search/' . urlencode( get_query_var('s') ), 301 );
    1416            exit;
     17
     18        } elseif ( is_404() ) {
     19            $path_redirects = [
     20                // The news blog is often thought to be at /blog
     21                '/blog' => '/news/',
     22
     23                // new Downloads pages https://meta.trac.wordpress.org/ticket/3673
     24                '/download/beta'            => '/download/beta-nightly/',
     25                '/download/nightly'         => '/download/beta-nightly/',
     26                '/download/release-archive' => '/download/releases/',
     27                '/download/legacy'          => '/download/',
     28
     29                // Five for the Future site aliases
     30                '/5'                => '/five-for-the-future/',
     31                '/five'             => '/five-for-the-future/',
     32                '/5-for-the-future' => '/five-for-the-future/',
     33                '/5forthefuture'    => '/five-for-the-future/',
     34                '/fiveforthefuture' => '/five-for-the-future/',
     35            ];
     36
     37            foreach ( $path_redirects as $test => $redirect ) {
     38                if ( 0 === strpos( $_SERVER['REQUEST_URI'], $test ) ) {
     39
     40                    // override nocache_headers();
     41                    header_remove( 'expires' );
     42                    header_remove( 'cache-control' );
     43
     44                    wp_safe_redirect( $redirect, 301 );
     45                    exit;
     46                }
     47            }
    1548        }
    1649    }, 9 ); // Before redirect_canonical();
    1750}
     51
     52/**
     53 * Redirect some common urls to the proper location.
     54 */
     55add_action( 'template_redirect', function() {
     56    $host = $_SERVER['HTTP_HOST'];
     57    $path = $_SERVER['REQUEST_URI'];
     58
     59    if ( ! is_404() ) {
     60        return;
     61    }
     62
     63    $path_redirects = [
     64        // Singular => Plural for plugin/theme directories
     65        '/plugin/' => '/plugins/',
     66        '/theme/'  => '/themes/',
     67
     68        // The plugin directory was available at /plugins-wp/ during a beta-test, and is still linked to.
     69        '/plugins-wp/' => '/plugins/',
     70
     71        // Rosetta txt-download urls were changed to /download/.
     72        '/txt-download/' => '/downloads/',
     73    ];
     74
     75    if ( 'make.wordpress.org' === $host ) {
     76        // Slack invite url is /chat not /slack.
     77        $path_redirects['/slack'] = '/chat/';
     78    }
     79
     80    foreach ( $path_redirects as $test => $redirect ) {
     81        if ( 0 === strpos( $path, $test ) || 0 === strpos( $path . '/', $test ) ) {
     82
     83            // Include any extra path components. (eg. /plugin/hello-dolly/)
     84            $path = substr( $path, strlen( $test ) );
     85            if ( $path ) {
     86                $redirect .= $path;
     87            }
     88
     89            // override nocache_headers();
     90            header_remove( 'expires' );
     91            header_remove( 'cache-control' );
     92
     93            wp_safe_redirect( $redirect, 301 );
     94            exit;
     95        }
     96    }
     97
     98}, 9 );
     99
     100/**
     101 * Handle the domain-based redirects
     102 *
     103 * Called from sunrise.php on ms_site_not_found and ms_network_not_found actions.
     104 */
     105function wporg_redirect_site_not_found() {
     106    // Default location for a not-found site or network is the main WordPress.org homepage.
     107    $location = 'https://wordpress.org/';
     108    $host     = $_SERVER['HTTP_HOST'];
     109
     110    switch ( $host ) {
     111        // :earth_asia::earth_africa::earth_americas:.wordpress.org
     112        case 'xn--tg8hcb.wordpress.org':
     113            $location = 'https://emoji.wordpress.org/';
     114            break;
     115
     116        // Singular => Plural
     117        case 'profile.wordpress.org':
     118            $location = 'https://profiles.wordpress.org' . $_SERVER['REQUEST_URI'];
     119            break;
     120
     121        // WordPress.org => WordPress.net
     122        case 'wp15.wordpress.org':
     123        case '2017.wordpress.org':
     124        case '2019.wordpress.org':
     125        case '2020.wordpress.org':
     126        case '2021.wordpress.org':
     127            $location = 'https://' . explode( $host, '.' )[0] . '.wordpress.net/';
     128            break;
     129
     130        case 'slack.wordpress.org':
     131        case 'chat.wordpress.org':
     132            $location = 'https://make.wordpress.org/chat/';
     133            break;
     134
     135        // Plural => Singular
     136        case 'developers.wordpress.org':
     137            $location = 'https://developer.wordpress.org/';
     138            break;
     139    }
     140
     141    if ( ! headers_sent() ) {
     142        header( 'Location: ' . $location, true, 301 );
     143    } else {
     144        // Headers should not have been sent at this point in time.
     145        // On some pages, such as wp-cron.php the request has been terminated prior to WordPress loading, and so headers were "sent".
     146        echo "<a href='$location'>$location</a>";
     147    }
     148    exit;
     149}
Note: See TracChangeset for help on using the changeset viewer.