Skip to content

Commit

Permalink
Move redirects from old theme to plugin
Browse files Browse the repository at this point in the history
This means they work with both old and new theme activated

See #2496
  • Loading branch information
adamwoodnz committed Jun 19, 2024
1 parent 19cca2e commit fbafe66
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 61 deletions.
65 changes: 65 additions & 0 deletions wp-content/plugins/wporg-learn/inc/redirects.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace WPOrg_Learn\Redirects;

add_action( 'template_redirect', __NAMESPACE__ . '\wporg_learn_redirect_meetings' );
add_action( 'template_redirect', __NAMESPACE__ . '\wporg_learn_redirect_old_urls' );

/**
* Redirect meeting posts to associated link
*
* @return void
*/
function wporg_learn_redirect_meetings() {
global $post;

if ( is_singular( array( 'meeting' ) ) ) {

if ( ! empty( $post->ID ) ) {

$redirect = wp_http_validate_url( get_post_meta( $post->ID, 'link', true ) );

if ( $redirect && wp_redirect( $redirect ) ) {
exit;
}
}
}

}

/**
* Redirect old pages to their new homes.
*
* @return void
*/
function wporg_learn_redirect_old_urls() {
if ( ! is_404() ) {
return;
}

$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',
);

// Use `REQUEST_URI` rather than `$wp->request`, to get the entire source URI including url parameters.
$request = $_SERVER['REQUEST_URI'] ?? '';

foreach ( $redirects as $source => $destination ) {
if ( str_starts_with( $request, $source ) ) {
$redirect = $destination;

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

wp_safe_redirect( $redirect );
die();
}
}
}
1 change: 1 addition & 0 deletions wp-content/plugins/wporg-learn/wporg-learn.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ function load_files() {
require_once get_includes_path() . 'post-meta.php';
require_once get_includes_path() . 'post-type.php';
require_once get_includes_path() . 'profiles.php';
require_once get_includes_path() . 'redirects.php';
require_once get_includes_path() . 'sensei.php';
require_once get_includes_path() . 'taxonomy.php';
require_once get_includes_path() . 'export.php';
Expand Down
61 changes: 0 additions & 61 deletions wp-content/themes/pub/wporg-learn-2020/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1140,67 +1140,6 @@ function wporg_learn_return_default_image( $default_image ) {
*/
add_filter( 'jetpack_news_sitemap_generate', '__return_false' );

/**
* Redirect meeting posts to associated link
*
* @return void
*/
function wporg_learn_redirect_meetings() {
global $post;

if ( is_singular( array( 'meeting' ) ) ) {

if ( ! empty( $post->ID ) ) {

$redirect = wp_http_validate_url( get_post_meta( $post->ID, 'link', true ) );

if ( $redirect && wp_redirect( $redirect ) ) {
exit;
}
}
}

}
add_action( 'template_redirect', 'wporg_learn_redirect_meetings' );

/**
* Redirect old pages to their new homes.
*
* @return void
*/
function wporg_learn_redirect_old_urls() {
if ( ! is_404() ) {
return;
}

$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',
);

// Use `REQUEST_URI` rather than `$wp->request`, to get the entire source URI including url parameters.
$request = $_SERVER['REQUEST_URI'] ?? '';

foreach ( $redirects as $source => $destination ) {
if ( str_starts_with( $request, $source ) ) {
$redirect = $destination;

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

wp_safe_redirect( $redirect );
die();
}
}
}
add_action( 'template_redirect', 'wporg_learn_redirect_old_urls' );

/**
* Add file MIME types for upload.
*
Expand Down

0 comments on commit fbafe66

Please sign in to comment.