From cc75a22fe17d07caaed9fc80a0d1df0c06be005c Mon Sep 17 00:00:00 2001 From: Adam Wood <1017872+adamwoodnz@users.noreply.github.com> Date: Mon, 26 Aug 2024 17:30:05 +1200 Subject: [PATCH] Redirect deprecated tutorials to wordpress.tv (#2860) * Redirect deprecated tutorials to wordpress.tv See https://github.com/WordPress/Learn/issues/2496 * Add wordpress.tv to the allowed redirect hosts --- .../plugins/wporg-learn/inc/redirects.php | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/wp-content/plugins/wporg-learn/inc/redirects.php b/wp-content/plugins/wporg-learn/inc/redirects.php index 67635f862..2cca1a39f 100644 --- a/wp-content/plugins/wporg-learn/inc/redirects.php +++ b/wp-content/plugins/wporg-learn/inc/redirects.php @@ -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 * @@ -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. @@ -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; } } }