Skip to content

Commit

Permalink
Add a new filter for custom redirect validations (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
diksha-nts authored Aug 20, 2024
1 parent b538ca7 commit eeb5536
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions includes/class-wpcom-legacy-redirector.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,29 +187,39 @@ public static function validate_urls( $from_url, $redirect_to ) {
$message = __( 'If you are doing an external redirect, make sure you safelist the domain using the "allowed_redirect_hosts" filter.', 'wpcom-legacy-redirector' );
return new WP_Error( 'external-url-not-allowed', $message );
}
return array( $from_url, $redirect_to );
}
if ( false === self::validate( $from_url, $redirect_to ) ) {
$message = __( '"Redirect From" and "Redirect To" values are required and should not match.', 'wpcom-legacy-redirector' );
return new WP_Error( 'invalid-values', $message );
}
if ( 404 !== absint( self::check_if_404( home_url() . $from_url ) ) ) {
$message = __( 'Redirects need to be from URLs that have a 404 status.', 'wpcom-legacy-redirector' );
return new WP_Error( 'non-404', $message );
}
if ( 'private' === self::vip_legacy_redirect_check_if_public( $from_url ) ) {
$message = __( 'You are trying to redirect from a URL that is currently private.', 'wpcom-legacy-redirector' );
return new WP_Error( 'private-url', $message );
}
if ( 'private' === self::vip_legacy_redirect_check_if_public( $redirect_to ) && '/' !== $redirect_to ) {
$message = __( 'You are trying to redirect to a URL that is currently not public.', 'wpcom-legacy-redirector' );
return new WP_Error( 'non-public', $message );
}
if ( 'null' === self::vip_legacy_redirect_check_if_public( $redirect_to ) && '/' !== $redirect_to ) {
$message = __( 'You are trying to redirect to a URL that does not exist.', 'wpcom-legacy-redirector' );
return new WP_Error( 'invalid', $message );
} else {
if ( false === self::validate( $from_url, $redirect_to ) ) {
$message = __( '"Redirect From" and "Redirect To" values are required and should not match.', 'wpcom-legacy-redirector' );
return new WP_Error( 'invalid-values', $message );
}
if ( 404 !== absint( self::check_if_404( home_url() . $from_url ) ) ) {
$message = __( 'Redirects need to be from URLs that have a 404 status.', 'wpcom-legacy-redirector' );
return new WP_Error( 'non-404', $message );
}
if ( 'private' === self::vip_legacy_redirect_check_if_public( $from_url ) ) {
$message = __( 'You are trying to redirect from a URL that is currently private.', 'wpcom-legacy-redirector' );
return new WP_Error( 'private-url', $message );
}
if ( 'private' === self::vip_legacy_redirect_check_if_public( $redirect_to ) && '/' !== $redirect_to ) {
$message = __( 'You are trying to redirect to a URL that is currently not public.', 'wpcom-legacy-redirector' );
return new WP_Error( 'non-public', $message );
}
if ( 'null' === self::vip_legacy_redirect_check_if_public( $redirect_to ) && '/' !== $redirect_to ) {
$message = __( 'You are trying to redirect to a URL that does not exist.', 'wpcom-legacy-redirector' );
return new WP_Error( 'invalid', $message );
}
}
return array( $from_url, $redirect_to );
/**
* Filter the result of the redirect validation.
*
* @param array $params {
* Array containing the URLs to validate.
*
* @type string $from_url URL to redirect (source).
* @type string $redirect_to URL to redirect to (destination).
* }
*/
return apply_filters( 'wpcom_legacy_redirector_validate_urls', array( $from_url, $redirect_to ) );
}

/**
Expand Down

0 comments on commit eeb5536

Please sign in to comment.