From b85fe08bc50d933de8be7fb96242de4f3979cdc0 Mon Sep 17 00:00:00 2001 From: yuriinalivaiko Date: Sun, 29 Oct 2023 19:04:50 +0200 Subject: [PATCH 1/2] - fixed YouTube link validation --- includes/core/um-actions-form.php | 2 +- includes/core/um-filters-fields.php | 9 ++++---- includes/um-short-functions.php | 32 ++++++++++++++++------------- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/includes/core/um-actions-form.php b/includes/core/um-actions-form.php index 643f8f68..9f5496c3 100644 --- a/includes/core/um-actions-form.php +++ b/includes/core/um-actions-form.php @@ -792,7 +792,7 @@ function um_submit_form_errors_hook_( $submitted_data, $form_data ) { break; case 'youtube_video': - if ( ! UM()->validation()->is_url( $submitted_data[ $key ], 'youtube.com/watch?v=' ) && ! UM()->validation()->is_url( $submitted_data[ $key ], 'youtu.be' ) && ! UM()->validation()->is_url( $submitted_data[ $key ], 'youtube.com/shorts/' ) ) { + if ( ! UM()->validation()->is_url( $submitted_data[ $key ] ) || false === um_youtube_id_from_url( $submitted_data[ $key ] ) ) { // translators: %s: label. UM()->form()->add_error( $key, sprintf( __( 'Please enter a valid %s URL', 'ultimate-member' ), $array['label'] ) ); } diff --git a/includes/core/um-filters-fields.php b/includes/core/um-filters-fields.php index fcfa3679..da38c517 100644 --- a/includes/core/um-filters-fields.php +++ b/includes/core/um-filters-fields.php @@ -94,10 +94,11 @@ function um_profile_field_filter_hook__youtube_video( $value, $data ) { return ''; } $value = ( strstr( $value, 'http' ) || strstr( $value, '://' ) ) ? um_youtube_id_from_url( $value ) : $value; - $value = '
- -
'; - + if ( false !== $value ) { + $value = '
' + . '' + . '
'; + } return $value; } add_filter( 'um_profile_field_filter_hook__youtube_video', 'um_profile_field_filter_hook__youtube_video', 99, 2 ); diff --git a/includes/um-short-functions.php b/includes/um-short-functions.php index 0e92a89a..fc4dc466 100644 --- a/includes/um-short-functions.php +++ b/includes/um-short-functions.php @@ -1918,20 +1918,24 @@ function um_youtube_id_from_url( $url ) { $url = preg_replace( '/\?si=.*/', '', $url ); // referral attribute. $pattern = - '%^# Match any youtube URL - (?:https?://)? # Optional scheme. Either http or https - (?:www\.)? # Optional www subdomain - (?: # Group host alternatives - youtu\.be/ # Either youtu.be, - | youtube\.com # or youtube.com - (?: # Group path alternatives - /embed/ # Either /embed/ - | /v/ # or /v/ - | /watch\?v= # or /watch\?v= - | /shorts/ # or /shorts/ for short videos - ) # End path alternatives. - ) # End host alternatives. - ([\w-]{10,12}) # Allow 10-12 for 11 char youtube id. + '%^ # Match any youtube URL + (?:https?://)? # Optional scheme. Either http or https + (?:www\.)? # Optional www subdomain + (?: # Group host alternatives + youtu\.be/ # Either youtu.be, + | youtube\.com # or youtube.com + (?: # Group path alternatives + /embed/ # Either /embed/ + | /v/ # or /v/ + | /watch\?v= # or /watch\?v= + | /shorts/ # or /shorts/ for short videos + ) # End path alternatives. + ) # End host alternatives. + ([\w-]{10,12}) # Allow 10-12 for 11 char youtube id. + (?: # Additional parameters + (?:\?|\&) + \w+=[^&$]+ + )* $%x'; $result = preg_match( $pattern, $url, $matches ); From a4d97612bbdf55e662caf6b776387f294f183d23 Mon Sep 17 00:00:00 2001 From: yuriinalivaiko Date: Mon, 30 Oct 2023 13:59:38 +0200 Subject: [PATCH 2/2] - fixed YouTube link validation: sub-domain is allowed --- includes/um-short-functions.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/includes/um-short-functions.php b/includes/um-short-functions.php index fc4dc466..91a6d804 100644 --- a/includes/um-short-functions.php +++ b/includes/um-short-functions.php @@ -1920,7 +1920,14 @@ function um_youtube_id_from_url( $url ) { $pattern = '%^ # Match any youtube URL (?:https?://)? # Optional scheme. Either http or https - (?:www\.)? # Optional www subdomain + (?: # Optional subdomain, for example m or www. + [a-z0-9] # Subdomain begins with alpha-num. + (?: # Optionally more than one char. + [a-z0-9-]{0,61} # Middle part may have dashes. + [a-z0-9] # Starts and ends with alpha-num. + )? # Subdomain length from 1 to 63. + \. # Required dot separates subdomains. + )? # Subdomain is optional. (?: # Group host alternatives youtu\.be/ # Either youtu.be, | youtube\.com # or youtube.com