Skip to content

Commit

Permalink
Merge pull request #93 from Automattic/bugfix/73-admin-redirect-save-fix
Browse files Browse the repository at this point in the history
Bugfix/73 admin redirect save fix
  • Loading branch information
GaryJones authored May 24, 2024
2 parents 58b9abe + 7d9693a commit 6fe6f71
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 8 deletions.
9 changes: 8 additions & 1 deletion includes/class-lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ public static function get_redirect_uri( $url ) {
return add_query_arg( $preservable_params, get_permalink( $redirect_post->post_parent ) );
} elseif ( ! empty( $redirect_post->post_excerpt ) ) {
// Add preserved params to the destination URL.
return add_query_arg( $preservable_params, esc_url_raw( $redirect_post->post_excerpt ) );
// We need to add here the home_url() if the target starts with /.
$redirect_url = esc_url_raw( $redirect_post->post_excerpt );

if ( strpos( $redirect_post->post_excerpt, '/' ) === 0 ) {
$redirect_url = home_url() . $redirect_url;
}

return add_query_arg( $preservable_params, $redirect_url );
}
}
return false;
Expand Down
16 changes: 16 additions & 0 deletions includes/class-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,20 @@ function ( $matches ) {

return $parts;
}

/**
* Get WP Home URL without path suffix.
*
* @return string
*/
public static function get_home_domain_without_path() {
$home_url_info = self::mb_parse_url( home_url() );
$return_url = $home_url_info['scheme'] . '://' . $home_url_info['host'];

if ( !empty( $home_url_info['port'] ) ) {
$return_url .= ':' . $home_url_info['port'];
}

return $return_url;
}
}
12 changes: 11 additions & 1 deletion includes/class-wpcom-legacy-redirector-ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Automattic\LegacyRedirector\Capability;
use Automattic\LegacyRedirector\Post_Type;
use Automattic\LegacyRedirector\Utils;

/**
* User interface additions.
Expand Down Expand Up @@ -154,11 +155,20 @@ public function add_redirect_validation() {
);
} else {
$redirect_from = sanitize_text_field( $_POST['redirect_from'] );

// We apply the home_url() prefix to $redirect_from.
$redirect_url_info = Utils::mb_parse_url( home_url() . $redirect_from);
$redirect_from = $redirect_url_info['path'];
if( !empty( $redirect_url_info['query'] ) ) {
$redirect_from .= '?' . $redirect_url_info['query'];
}

$redirect_to = sanitize_text_field( $_POST['redirect_to'] );
if ( WPCOM_Legacy_Redirector::validate( $redirect_from, $redirect_to ) ) {
$output = WPCOM_Legacy_Redirector::insert_legacy_redirect( $redirect_from, $redirect_to, true );
if ( true === $output ) {
$link = '<a href="' . esc_url( $redirect_from ) . '" target="_blank">' . esc_url( $redirect_from ) . '</a>';
$follow_home_domain = Utils::get_home_domain_without_path();
$link = '<a href="' . esc_url( $follow_home_domain . $redirect_from ) . '" target="_blank">' . esc_html( $redirect_from ) . '</a>';
$messages[] = __( 'The redirect was added successfully. Check Redirect: ', 'wpcom-legacy-redirector' ) . $link;
} elseif ( is_wp_error( $output ) ) {
foreach ( $output->get_error_messages() as $error ) {
Expand Down
2 changes: 1 addition & 1 deletion includes/class-wpcom-legacy-redirector.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static function wpcom_legacy_add_redirect_js( $hook ) {
}

wp_enqueue_script( 'wpcom-legacy-redirector', plugins_url( '/../js/admin-add-redirects.js', __FILE__ ), array(), WPCOM_LEGACY_REDIRECTOR_VERSION, true );
wp_localize_script( 'wpcom-legacy-redirector', 'wpcomLegacyRedirector', array( 'siteurl' => get_option( 'siteurl' ) ) );
wp_localize_script( 'wpcom-legacy-redirector', 'wpcomLegacyRedirector', array( 'siteurl' => home_url() ) );
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/Integration/LookupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ public function get_protected_redirect_data() {
return array(
'redirect unicode characters with querystring' => array(
'/فوتوغرافيا/?test=فوتوغرافيا',
'/some_other_page',
'http://example.com/some_other_page',
'301',
),
'redirect_simple' => array(
'/test',
'/',
'http://example.com/',
'301',
),
'redirect_unicode_no_query' => array(
'/فوتوغرافيا/',
'/',
'http://example.com/',
'301',
),
);
Expand Down
14 changes: 12 additions & 2 deletions tests/Integration/RedirectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ final class RedirectsTest extends TestCase {
*/
public function get_redirect_data() {
return array(
'redirect_relative_path' => array(
'/non-existing-page',
'/test2',
home_url() . '/test2',
),

'redirect_unicode_in_path' => array(
// https://www.w3.org/International/articles/idn-and-iri/ .
'/JP納豆',
Expand Down Expand Up @@ -64,12 +70,16 @@ public function get_redirect_data() {
* @param string $from From path.
* @param string $to Destination.
*/
public function test_redirect_is_inserted_successfully_and_returns_true( $from, $to ) {
public function test_redirect_is_inserted_successfully_and_returns_true( $from, $to, $expected = null ) {
$redirect = WPCOM_Legacy_Redirector::insert_legacy_redirect( $from, $to, false );
$this->assertTrue( $redirect, 'insert_legacy_redirect() and return true, failed' );

$redirect = Lookup::get_redirect_uri( $from );
$this->assertEquals( $redirect, $to, 'get_redirect_uri(), failed - got "' . $redirect . '", expected "' . $to . '"' );

if ( \is_null( $expected ) ) {
$expected = $to;
}
$this->assertEquals( $expected, $redirect, 'get_redirect_uri(), failed - got "' . $redirect . '", expected "' . $to . '"' );
}

/**
Expand Down

0 comments on commit 6fe6f71

Please sign in to comment.