From f6ff721e8f05054f36ac76736c8a857012753b8a Mon Sep 17 00:00:00 2001 From: Milan Chaudhary Date: Tue, 5 Sep 2023 10:17:57 +0545 Subject: [PATCH 1/3] Tweak - Redirection even if the redirect_url is page slug --- includes/functions-ur-core.php | 21 +++++++++++++++++++ .../class-ur-shortcode-my-account.php | 9 +++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/includes/functions-ur-core.php b/includes/functions-ur-core.php index f20e4c01e..88d897291 100644 --- a/includes/functions-ur-core.php +++ b/includes/functions-ur-core.php @@ -3921,3 +3921,24 @@ function user_registration_conditional_user_meta_filter( $valid_form_data, $user add_filter( 'user_registration_before_user_meta_update', 'user_registration_conditional_user_meta_filter', 10, 3 ); add_filter( 'user_registration_before_save_profile_details', 'user_registration_conditional_user_meta_filter', 10, 3 ); +if ( ! function_exists( 'ur_get_all_page_slugs' ) ) { + /** + * Get all the page slugs. + */ + function ur_get_all_page_slugs() { + $args = array( + 'post_type' => 'page', + 'posts_per_page' => -1, + ); + + $pages = get_pages( $args ); + + $slugs = array(); + + foreach ( $pages as $page ) { + $slugs[] = $page->post_name; + } + + return $slugs; + } +} diff --git a/includes/shortcodes/class-ur-shortcode-my-account.php b/includes/shortcodes/class-ur-shortcode-my-account.php index d43d0b9d5..b424b9e5a 100644 --- a/includes/shortcodes/class-ur-shortcode-my-account.php +++ b/includes/shortcodes/class-ur-shortcode-my-account.php @@ -63,7 +63,14 @@ public static function output( $atts ) { if ( ! is_user_logged_in() ) { - $redirect_url = isset( $atts['redirect_url'] ) ? esc_url( trim( $atts['redirect_url'] ) ) : ''; + $redirect_url = isset( $atts['redirect_url'] ) ? trim( $atts['redirect_url'] ) : ''; + if ( filter_var( $redirect_url, FILTER_VALIDATE_URL ) === false ) { + $all_page_slug = ur_get_all_page_slugs(); + if ( in_array( $redirect_url, $all_page_slug, true ) ) { + $redirect_url = site_url( $redirect_url ); + } + } + $redirect_url = esc_url( $redirect_url ); $redirect_url = ( isset( $_GET['redirect_to'] ) && empty( $redirect_url ) ) ? esc_url( wp_unslash( $_GET['redirect_to'] ) ) : $redirect_url; // @codingStandardsIgnoreLine $form_id = isset( $atts['form_id'] ) ? absint( $atts['form_id'] ) : 0; $message = apply_filters( 'user_registration_my_account_message', '' ); From eec90fa350ae4656dd392c2ae3ba330096377260 Mon Sep 17 00:00:00 2001 From: Milan Chaudhary Date: Tue, 5 Sep 2023 10:34:33 +0545 Subject: [PATCH 2/3] Added - Redirection even if we put the slug as redirect_url in login shortcode --- includes/class-ur-shortcodes.php | 14 ++++++++++++++ includes/shortcodes/class-ur-shortcode-login.php | 1 + .../shortcodes/class-ur-shortcode-my-account.php | 7 +------ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/includes/class-ur-shortcodes.php b/includes/class-ur-shortcodes.php index 51ba8418d..f5d03a2ec 100644 --- a/includes/class-ur-shortcodes.php +++ b/includes/class-ur-shortcodes.php @@ -311,4 +311,18 @@ private static function render_form( $form_id ) { ) ); } + /** + * Check the redirection url is valid url or slug. + * + * @param string $redirect_url redirection url. + */ + public static function check_is_valid_redirect_url( $redirect_url ) { + if ( filter_var( $redirect_url, FILTER_VALIDATE_URL ) === false ) { + $all_page_slug = ur_get_all_page_slugs(); + if ( in_array( $redirect_url, $all_page_slug, true ) ) { + $redirect_url = site_url( $redirect_url ); + } + } + return $redirect_url; + } } diff --git a/includes/shortcodes/class-ur-shortcode-login.php b/includes/shortcodes/class-ur-shortcode-login.php index fab233b19..df2714a60 100644 --- a/includes/shortcodes/class-ur-shortcode-login.php +++ b/includes/shortcodes/class-ur-shortcode-login.php @@ -37,6 +37,7 @@ public static function output( $atts ) { global $wp, $post; $redirect_url = isset( $atts['redirect_url'] ) ? trim( $atts['redirect_url'] ) : ''; + $redirect_url = UR_Shortcodes::check_is_valid_redirect_url( $redirect_url ); if ( ! is_user_logged_in() ) { // After password reset, add confirmation message. diff --git a/includes/shortcodes/class-ur-shortcode-my-account.php b/includes/shortcodes/class-ur-shortcode-my-account.php index b424b9e5a..758dc8d3a 100644 --- a/includes/shortcodes/class-ur-shortcode-my-account.php +++ b/includes/shortcodes/class-ur-shortcode-my-account.php @@ -64,12 +64,7 @@ public static function output( $atts ) { if ( ! is_user_logged_in() ) { $redirect_url = isset( $atts['redirect_url'] ) ? trim( $atts['redirect_url'] ) : ''; - if ( filter_var( $redirect_url, FILTER_VALIDATE_URL ) === false ) { - $all_page_slug = ur_get_all_page_slugs(); - if ( in_array( $redirect_url, $all_page_slug, true ) ) { - $redirect_url = site_url( $redirect_url ); - } - } + $redirect_url = UR_Shortcodes::check_is_valid_redirect_url( $redirect_url ); $redirect_url = esc_url( $redirect_url ); $redirect_url = ( isset( $_GET['redirect_to'] ) && empty( $redirect_url ) ) ? esc_url( wp_unslash( $_GET['redirect_to'] ) ) : $redirect_url; // @codingStandardsIgnoreLine $form_id = isset( $atts['form_id'] ) ? absint( $atts['form_id'] ) : 0; From 14681d80912ca33ec9b7f4adbbb7d34835770094 Mon Sep 17 00:00:00 2001 From: Milan Chaudhary Date: Mon, 18 Sep 2023 11:48:56 +0545 Subject: [PATCH 3/3] Fixed - The case if no valid url and page slug then redirect to home url --- includes/class-ur-shortcodes.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/includes/class-ur-shortcodes.php b/includes/class-ur-shortcodes.php index f5d03a2ec..b699caffa 100644 --- a/includes/class-ur-shortcodes.php +++ b/includes/class-ur-shortcodes.php @@ -321,6 +321,8 @@ public static function check_is_valid_redirect_url( $redirect_url ) { $all_page_slug = ur_get_all_page_slugs(); if ( in_array( $redirect_url, $all_page_slug, true ) ) { $redirect_url = site_url( $redirect_url ); + } else { + $redirect_url = home_url(); } } return $redirect_url;