Skip to content

Commit 923023f

Browse files
authored
Sunrise: Redirect /uganda/2024/wordpress-showcase/ to masaka (#1076)
1 parent 5849491 commit 923023f

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed

public_html/wp-content/mu-plugins/tests/bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function manually_load_plugins() {
2525
ms_upload_constants();
2626

2727
require_once dirname( dirname( __DIR__ ) ) . '/sunrise.php';
28+
require_once dirname( dirname( __DIR__ ) ) . '/sunrise-events.php';
2829

2930
require_once dirname( __DIR__ ) . '/0-error-handling.php';
3031
require_once dirname( __DIR__ ) . '/wordcamp/lets-encrypt-helper.php';
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/*
3+
* This isn't technically an mu-plugin, but it's easier to just put this here than creating a whole new suite.
4+
*
5+
* @todo `sunrise-events.php` isn't watched by phpunit-watcher because it can only watch entire folders, and
6+
* `wp-content` is too big to monitor without significant performance impacts. You'll have to modify this file
7+
* to automatically re-run tests.
8+
*
9+
* See https://github.com/spatie/phpunit-watcher/issues/113
10+
*/
11+
12+
13+
namespace WordCamp\Sunrise\Events;
14+
use WP_UnitTestCase;
15+
16+
defined( 'WPINC' ) || die();
17+
18+
/**
19+
* @group sunrise
20+
* @group mu-plugins
21+
*/
22+
class Test_Sunrise_Events extends WP_UnitTestCase {
23+
/**
24+
* @covers WordCamp\Sunrise\get_redirect_url
25+
*
26+
* @dataProvider data_get_redirect_url
27+
*/
28+
public function test_get_redirect_url( $request_uri, $expected_url ) {
29+
$actual_url = get_redirect_url( $request_uri );
30+
31+
$this->assertSame( $expected_url, $actual_url );
32+
}
33+
34+
/**
35+
* Test cases for test_get_redirect_url().
36+
*
37+
* @return array
38+
*/
39+
public function data_get_redirect_url() {
40+
return array(
41+
'no redirect' => array(
42+
'request_uri' => '/foo/2024/bar/',
43+
'expected_url' => '',
44+
),
45+
46+
'without subpath or query vars' => array(
47+
'request_uri' => '/uganda/2024/wordpress-showcase/',
48+
'expected_url' => 'https://events.wordpress.test/masaka/2024/wordpress-showcase/',
49+
),
50+
51+
'with subpath and query vars' => array(
52+
'request_uri' => '/uganda/2024/wordpress-showcase/schedule/?foo=bar',
53+
'expected_url' => 'https://events.wordpress.test/masaka/2024/wordpress-showcase/schedule/?foo=bar',
54+
),
55+
);
56+
}
57+
}

public_html/wp-content/sunrise-events.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,56 @@
22

33
namespace WordCamp\Sunrise\Events;
44
use WP_Network, WP_Site;
5+
use function WordCamp\Sunrise\{ get_top_level_domain };
56

67
defined( 'WPINC' ) || die();
78
use const WordCamp\Sunrise\PATTERN_CITY_YEAR_TYPE_PATH;
89

9-
set_network_and_site();
10+
main();
1011

1112

13+
/**
14+
* Controller for this file.
15+
*/
16+
function main() {
17+
// Redirecting would interfere with bin scripts, unit tests, etc.
18+
if ( php_sapi_name() !== 'cli' ) {
19+
$redirect_url = get_redirect_url( $_SERVER['REQUEST_URI'] );
20+
21+
if ( $redirect_url ) {
22+
header( 'Location: ' . $redirect_url, true, 301 );
23+
die();
24+
}
25+
}
26+
27+
set_network_and_site();
28+
}
29+
30+
/**
31+
* Get the URL to redirect to, if any.
32+
*/
33+
function get_redirect_url( string $request_uri ): string {
34+
$domain = 'events.wordpress.' . get_top_level_domain();
35+
$old_full_url = sprintf(
36+
'https://%s/%s',
37+
$domain,
38+
ltrim( $request_uri, '/' )
39+
);
40+
41+
$renamed_sites = array(
42+
'/uganda/2024/wordpress-showcase/' => '/masaka/2024/wordpress-showcase/',
43+
);
44+
45+
foreach ( $renamed_sites as $old_site_path => $new_site_path ) {
46+
if ( str_starts_with( $request_uri, $old_site_path ) ) {
47+
$new_full_url = str_replace( $old_site_path, $new_site_path, $old_full_url );
48+
return $new_full_url;
49+
}
50+
}
51+
52+
return '';
53+
}
54+
1255
/**
1356
* Determine the current network and site.
1457
*

public_html/wp-content/sunrise.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555

5656
/*
5757
* Matches a URL path like '/vancouver/2023/diversity-day/`.
58+
*
59+
* These are used by the `events.wordpress.org` network.
5860
*/
5961
const PATTERN_CITY_YEAR_TYPE_PATH = '
6062
@ ^

0 commit comments

Comments
 (0)