Merge pull request #1347 from ultimatemember/fix/youtube_field_subdomain

YouTube Video field does not support all YouTube links
This commit is contained in:
Mykyta Synelnikov
2023-11-21 02:08:19 +02:00
committed by GitHub
3 changed files with 31 additions and 19 deletions
+1 -1
View File
@@ -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'] ) );
}
+5 -4
View File
@@ -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 = '<div class="um-youtube">
<iframe width="600" height="450" src="https://www.youtube.com/embed/' . $value . '" frameborder="0" allowfullscreen></iframe>
</div>';
if ( false !== $value ) {
$value = '<div class="um-youtube">'
. '<iframe width="600" height="450" src="https://www.youtube.com/embed/' . $value . '" frameborder="0" allowfullscreen></iframe>'
. '</div>';
}
return $value;
}
add_filter( 'um_profile_field_filter_hook__youtube_video', 'um_profile_field_filter_hook__youtube_video', 99, 2 );
+25 -14
View File
@@ -1918,20 +1918,31 @@ 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
(?: # 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
(?: # 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 );