Skip to content

Commit

Permalink
Redirect deprecated tutorials to wordpress.tv (#2860)
Browse files Browse the repository at this point in the history
* Redirect deprecated tutorials to wordpress.tv

See #2496

* Add wordpress.tv to the allowed redirect hosts
  • Loading branch information
adamwoodnz authored Aug 26, 2024
1 parent 124117f commit cc75a22
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions wp-content/plugins/wporg-learn/inc/redirects.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
add_action( 'template_redirect', __NAMESPACE__ . '\wporg_learn_redirect_meetings' );
add_action( 'template_redirect', __NAMESPACE__ . '\wporg_learn_redirect_old_urls' );

add_filter( 'allowed_redirect_hosts', __NAMESPACE__ . '\wporg_learn_allowed_redirect_hosts' );

/**
* Add allowed redirect hosts.
*
* @param array $hosts The array of allowed redirect hosts.
* @return array The updated array of allowed redirect hosts.
*/
function wporg_learn_allowed_redirect_hosts( $hosts ) {
return array_merge( $hosts, array( 'wordpress.tv' ) );
};

/**
* Redirect meeting posts to associated link
*
Expand Down Expand Up @@ -39,11 +51,14 @@ function wporg_learn_redirect_old_urls() {

$redirects = array(
// Source => Destination, any characters after the source will be appended to the destination.
'/workshop/' => '/tutorial/',
'/workshops' => '/tutorials',
'/social-learning' => '/online-workshops',
'/workshop-presenter-application' => '/tutorial-presenter-application',
'/report-content-errors' => '/report-content-feedback',
'/workshop/' => '/tutorial/',
'/workshops' => '/tutorials',
'/social-learning' => '/online-workshops',
'/workshop-presenter-application' => '/tutorial-presenter-application',
'/report-content-errors' => '/report-content-feedback',
'/tutorial/block-editor-01-basics/' => 'https://wordpress.tv/2021/06/18/shusei-toda-naoko-takano-block-editor-01-basics/',
'/tutorial/block-editor-02-text-blocks/' => 'https://wordpress.tv/2021/06/03/shusei-toda-block-editor-02-text-blocks/',
'/tutorial/ja-login-password-reset/' => 'https://wordpress.tv/2021/02/16/login-password-reset/',
);

// Use `REQUEST_URI` rather than `$wp->request`, to get the entire source URI including url parameters.
Expand All @@ -52,14 +67,15 @@ function wporg_learn_redirect_old_urls() {
foreach ( $redirects as $source => $destination ) {
if ( str_starts_with( $request, $source ) ) {
$redirect = $destination;
$code = 301;

// Append any extra request parameters.
if ( strlen( $request ) > strlen( $source ) ) {
$redirect .= substr( $request, strlen( $source ) );
}

wp_safe_redirect( $redirect );
die();
wp_safe_redirect( $redirect, $code, 'Learn WordPress' );
exit;
}
}
}

0 comments on commit cc75a22

Please sign in to comment.