| 1 |
<?php // phpcs:ignore |
| 2 |
|
| 3 |
/** |
| 4 |
* Video class |
| 5 |
* |
| 6 |
* @package opitn/inludes/uitls |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace OPTN\Includes\Utils; |
| 10 |
|
| 11 |
/** |
| 12 |
* Video |
| 13 |
*/ |
| 14 |
class VideoHelper { |
| 15 |
|
| 16 |
/** |
| 17 |
* Youtube type |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
public static $youtube = 'youtube'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Vimeo type |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
public static $vimeo = 'vime0'; |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Other type |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
public static $other = 'other'; |
| 37 |
|
| 38 |
/** |
| 39 |
* Check if the given URL is a YouTube link. |
| 40 |
* |
| 41 |
* @param string $url url. |
| 42 |
* @return bool |
| 43 |
*/ |
| 44 |
private static function is_youtube_url( $url ) { |
| 45 |
return preg_match( '/(?:youtube\.com|youtu\.be)/', $url ) === 1; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Check if the given URL is a Vimeo link. |
| 50 |
* |
| 51 |
* @param string $url url. |
| 52 |
* @return bool |
| 53 |
*/ |
| 54 |
private static function is_vimeo_url( $url ) { |
| 55 |
return preg_match( '/vimeo\.com/', $url ) === 1; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Extract the YouTube video ID from a URL. |
| 60 |
* |
| 61 |
* @param string $url url. |
| 62 |
* @return string|null |
| 63 |
*/ |
| 64 |
private static function extract_youtube_id( $url ) { |
| 65 |
$pattern = '/(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/'; |
| 66 |
if ( preg_match( $pattern, $url, $matches ) ) { |
| 67 |
return $matches[1]; |
| 68 |
} |
| 69 |
return null; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Extract the Vimeo video ID from a URL. |
| 74 |
* |
| 75 |
* @param string $url url. |
| 76 |
* @return string|null |
| 77 |
*/ |
| 78 |
private static function extract_vimeo_id( $url ) { |
| 79 |
$pattern = '/vimeo\.com\/(?:.*?\/video\/|.*?\/|)(\d+)/'; |
| 80 |
if ( preg_match( $pattern, $url, $matches ) ) { |
| 81 |
return $matches[1]; |
| 82 |
} |
| 83 |
return null; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* Get the type of video src url. |
| 89 |
* |
| 90 |
* @param string $url url. |
| 91 |
* @return string |
| 92 |
*/ |
| 93 |
private static function get_video_url_type( $url ) { |
| 94 |
if ( self::is_youtube_url( $url ) ) { |
| 95 |
return self::$youtube; |
| 96 |
} |
| 97 |
|
| 98 |
if ( self::is_vimeo_url( $url ) ) { |
| 99 |
return self::$vimeo; |
| 100 |
} |
| 101 |
|
| 102 |
return self::$other; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Wraps content with thumbnail |
| 107 |
* |
| 108 |
* @param string $content content. |
| 109 |
* @param object $attr attributes. |
| 110 |
* @param bool $is_bg is background. |
| 111 |
* @return string |
| 112 |
*/ |
| 113 |
private static function wrap( $content, $attr, $is_bg ) { |
| 114 |
|
| 115 |
if ( $is_bg ) { |
| 116 |
$html = '<div class="optn-bg-video-thirdparty">'; |
| 117 |
$html .= '<div class="optn-bg-video-iframe-wrapper">'; |
| 118 |
|
| 119 |
$html .= $content; |
| 120 |
|
| 121 |
$html .= '</div></div>'; |
| 122 |
} else { |
| 123 |
$html = '<div class="optn-block-video-thirdparty">'; |
| 124 |
|
| 125 |
$src = isset( $attr->thumbSrc->id ) ? wp_get_attachment_image_url( $attr->thumbSrc->id, 'full' ) : $attr->thumbSrc->url; // phpcs:ignore |
| 126 |
|
| 127 |
if ( ! empty( $src ) && ! $attr->autoplay ) { |
| 128 |
ob_start(); |
| 129 |
?> |
| 130 |
<button |
| 131 |
style="background:black url('<?php echo esc_url( $src ); ?>') no-repeat center;background-size:cover;" |
| 132 |
class="optn-block-video-thumbnail" |
| 133 |
aria-label="Play Video" |
| 134 |
aria-title="Play Video" |
| 135 |
> |
| 136 |
</button> |
| 137 |
<?php |
| 138 |
$html .= ob_get_clean(); |
| 139 |
} |
| 140 |
|
| 141 |
$html .= $content; |
| 142 |
$html .= '</div>'; |
| 143 |
} |
| 144 |
|
| 145 |
return $html; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Generate youtube html |
| 150 |
* |
| 151 |
* @param object $attr attributes. |
| 152 |
* @param bool $is_bg is background. |
| 153 |
* @param array $extra_attrs extra attributes. |
| 154 |
* @return string |
| 155 |
*/ |
| 156 |
private static function get_youtube_html( &$attr, $is_bg, $extra_attrs ) { |
| 157 |
|
| 158 |
$vid_id = self::extract_youtube_id( $attr->url ); |
| 159 |
$tags = array(); |
| 160 |
|
| 161 |
$params = array( |
| 162 |
'enablejsapi' => 1, |
| 163 |
'loop' => (int) ( 'repeat' === $attr->repeat ), |
| 164 |
'playlist' => $vid_id, |
| 165 |
); |
| 166 |
|
| 167 |
if ( $is_bg ) { |
| 168 |
$params['autoplay'] = 1; |
| 169 |
$params['mute'] = 1; |
| 170 |
$params['disablekb'] = 1; |
| 171 |
$params['playsinline'] = 1; |
| 172 |
$params['rel'] = 0; |
| 173 |
$params['iv_load_policy'] = 3; |
| 174 |
$params['cc_load_policy'] = 0; |
| 175 |
$params['controls'] = 0; |
| 176 |
} else { |
| 177 |
$params['mute'] = (int) $attr->autoplay; |
| 178 |
$params['controls'] = (int) $attr->controls; |
| 179 |
} |
| 180 |
|
| 181 |
$tags['src'] = esc_url( |
| 182 |
add_query_arg( |
| 183 |
$params, |
| 184 |
'https://www.youtube.com/embed/' . $vid_id |
| 185 |
) |
| 186 |
); |
| 187 |
|
| 188 |
$tags['title'] = isset( $attr->desc ) ? esc_attr( $attr->desc ) : 'Youtube'; |
| 189 |
$tags['frameborder'] = '0'; |
| 190 |
$tags['allow'] = 'accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture;web-share'; |
| 191 |
$tags['referrerpolicy'] = 'strict-origin-when-cross-origin'; |
| 192 |
$tags['allowfullscreen'] = ''; |
| 193 |
|
| 194 |
if ( $is_bg ) { |
| 195 |
$tags['data-optn-youtube-autoplay'] = 'true'; |
| 196 |
} else { |
| 197 |
$tags['width'] = '100%'; |
| 198 |
$tags['height'] = '100%'; |
| 199 |
$tags['data-optn-youtube-autoplay'] = $attr->autoplay ? 'true' : 'false'; |
| 200 |
} |
| 201 |
|
| 202 |
foreach ( $extra_attrs as $key => $value ) { |
| 203 |
$tags[ $key ] = $value; |
| 204 |
} |
| 205 |
|
| 206 |
$attributes = Utils::build_attr_str( $tags ); |
| 207 |
|
| 208 |
return self::wrap( "<iframe {$attributes}></iframe>", $attr, $is_bg ); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Generate vimeo html |
| 213 |
* |
| 214 |
* @param object $attr attributes. |
| 215 |
* @param bool $is_bg is background. |
| 216 |
* @param array $extra_attrs extra attributes. |
| 217 |
* @return string |
| 218 |
*/ |
| 219 |
private static function get_vimeo_html( &$attr, $is_bg, $extra_attrs ) { |
| 220 |
$vid_id = self::extract_vimeo_id( $attr->url ); |
| 221 |
$tags = array(); |
| 222 |
|
| 223 |
$params = array( |
| 224 |
'autoplay' => $is_bg ? '1' : ( (int) $attr->autoplay ), |
| 225 |
'muted' => $is_bg ? '1' : ( (int) $attr->autoplay ), |
| 226 |
'loop' => (int) ( 'repeat' === $attr->repeat ), |
| 227 |
'controls' => $is_bg ? '0' : ( (int) $attr->controls ), |
| 228 |
|
| 229 |
); |
| 230 |
|
| 231 |
$tags['src'] = esc_url( |
| 232 |
add_query_arg( |
| 233 |
$params, |
| 234 |
'https://player.vimeo.com/video/' . $vid_id |
| 235 |
) |
| 236 |
); |
| 237 |
|
| 238 |
$tags['title'] = isset( $attr->desc ) ? esc_attr( $attr->desc ) : 'Vimeo'; |
| 239 |
$tags['frameborder'] = '0'; |
| 240 |
$tags['allow'] = 'autoplay; fullscreen; picture-in-picture'; |
| 241 |
$tags['allowfullscreen'] = ''; |
| 242 |
|
| 243 |
if ( $is_bg ) { |
| 244 |
$tags['data-optn-vimeo-autoplay'] = 'true'; |
| 245 |
} else { |
| 246 |
$tags['width'] = '100%'; |
| 247 |
$tags['height'] = '100%'; |
| 248 |
$tags['data-optn-vimeo-autoplay'] = $attr->autoplay ? 'true' : 'false'; |
| 249 |
} |
| 250 |
|
| 251 |
foreach ( $extra_attrs as $key => $value ) { |
| 252 |
$tags[ $key ] = $value; |
| 253 |
} |
| 254 |
|
| 255 |
$attributes = Utils::build_attr_str( $tags ); |
| 256 |
|
| 257 |
return self::wrap( "<iframe {$attributes}></iframe>", $attr, $is_bg ); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Generate video html |
| 262 |
* |
| 263 |
* @param object $attr attributes. |
| 264 |
* @param bool $is_bg is background. |
| 265 |
* @param array $extra_attrs extra attributes. |
| 266 |
* @return string |
| 267 |
*/ |
| 268 |
private static function get_video_tag_html( &$attr, $is_bg, $extra_attrs ) { |
| 269 |
|
| 270 |
$tags = array(); |
| 271 |
$attributes = ''; |
| 272 |
|
| 273 |
$tags['src'] = esc_url( $attr->url ); |
| 274 |
$tags['preload'] = 'true'; |
| 275 |
|
| 276 |
if ( 'repeat' === $attr->repeat ) { |
| 277 |
$tags['loop'] = 'true'; |
| 278 |
} |
| 279 |
|
| 280 |
if ( $is_bg ) { |
| 281 |
$tags['autoplay'] = ''; |
| 282 |
$tags['muted'] = ''; |
| 283 |
$tags['playsinline'] = ''; |
| 284 |
$tags['data-optn-autoplay'] = 'true'; |
| 285 |
} else { |
| 286 |
|
| 287 |
$thumb_url = isset( $attr->thumbSrc->id ) ? wp_get_attachment_image_url( $attr->thumbSrc->id, 'full' ) : $attr->thumbSrc->url; // phpcs:ignore |
| 288 |
|
| 289 |
if ( ! empty( $thumb_url ) ) { |
| 290 |
$tags['poster'] = esc_url( $thumb_url ); |
| 291 |
} |
| 292 |
|
| 293 |
$tags['data-optn-autoplay'] = 'false'; |
| 294 |
|
| 295 |
if ( $attr->autoplay ) { |
| 296 |
$tags['autoplay'] = ''; |
| 297 |
$tags['muted'] = ''; |
| 298 |
$tags['data-optn-autoplay'] = 'true'; |
| 299 |
} |
| 300 |
|
| 301 |
if ( $attr->controls ) { |
| 302 |
$tags['controls'] = ''; |
| 303 |
} |
| 304 |
|
| 305 |
$tags['aria-description'] = ! empty( $attr->desc ) ? esc_attr( $attr->desc ) : false; |
| 306 |
} |
| 307 |
|
| 308 |
foreach ( $extra_attrs as $key => $value ) { |
| 309 |
$tags[ $key ] = $value; |
| 310 |
} |
| 311 |
|
| 312 |
$attributes = Utils::build_attr_str( $tags ); |
| 313 |
|
| 314 |
return "<video {$attributes}></video>"; |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Extract the Vimeo video ID from a URL. |
| 319 |
* |
| 320 |
* @param object $attr attributes. |
| 321 |
* @param bool $is_bg is background. |
| 322 |
* @param array $extra_attrs extra attributes. |
| 323 |
* @return string |
| 324 |
*/ |
| 325 |
public static function get_video_html( &$attr, $is_bg = false, $extra_attrs = array() ) { |
| 326 |
$type = self::get_video_url_type( $attr->url ); |
| 327 |
|
| 328 |
if ( self::$youtube === $type ) { |
| 329 |
return self::get_youtube_html( $attr, $is_bg, $extra_attrs ); |
| 330 |
} |
| 331 |
|
| 332 |
if ( self::$vimeo === $type ) { |
| 333 |
return self::get_vimeo_html( $attr, $is_bg, $extra_attrs ); |
| 334 |
} |
| 335 |
|
| 336 |
return self::get_video_tag_html( $attr, $is_bg, $extra_attrs ); |
| 337 |
} |
| 338 |
} |
| 339 |
|