Files
wyczarujprezent.pl/modules/leoelements/includes/embed.php
2024-10-28 22:14:22 +01:00

200 lines
5.5 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* 2007-2022 Leotheme
*
* NOTICE OF LICENSE
*
* LeoElements is module help you can build content for your shop
*
* DISCLAIMER
*
* @author Leotheme <leotheme@gmail.com>
* @copyright 2007-2022 Leotheme
* @license http://leotheme.com - prestashop template provider
*/
namespace LeoElements;
use LeoElements\Leo_Helper;
if ( ! defined( '_PS_VERSION_' ) ) {
exit; // Exit if accessed directly.
}
/**
* Elementor embed.
*
* Elementor embed handler class is responsible for Elementor embed functionality.
* The class holds the supported providers with their embed patters, and handles
* their custom properties to create custom HTML with the embeded content.
*
* @since 1.5.0
*/
class Embed {
/**
* Provider match masks.
*
* Holds a list of supported providers with their URL structure in a regex format.
*
* @since 1.5.0
* @access private
* @static
*
* @var array Provider URL structure regex.
*/
private static $provider_match_masks = [
'youtube' => '/^.*(?:youtu\.be\/|youtube(?:-nocookie)?\.com\/(?:(?:watch)?\?(?:.*&)?vi?=|(?:embed|v|vi|user)\/))([^\?&\"\'>]+)/',
'vimeo' => '/^.*vimeo\.com\/(?:[a-z]*\/)*([0-9]{6,11})[?]?.*/',
'dailymotion' => '/^.*dailymotion.com\/(?:video|hub)\/([^_]+)[^#]*(#video=([^_&]+))?/',
];
/**
* Embed patterns.
*
* Holds a list of supported providers with their embed patters.
*
* @since 1.5.0
* @access private
* @static
*
* @var array Embed patters.
*/
private static $embed_patterns = [
'youtube' => 'https://www.youtube{NO_COOKIE}.com/embed/{VIDEO_ID}?feature=oembed',
'vimeo' => 'https://player.vimeo.com/video/{VIDEO_ID}#t={TIME}',
'dailymotion' => 'https://dailymotion.com/embed/video/{VIDEO_ID}',
];
/**
* Get video properties.
*
* Retrieve the video properties for a given video URL.
*
* @since 1.5.0
* @access public
* @static
*
* @param string $video_url Video URL.
*
* @return null|array The video properties, or null.
*/
public static function get_video_properties( $video_url ) {
foreach ( self::$provider_match_masks as $provider => $match_mask ) {
preg_match( $match_mask, $video_url, $matches );
if ( $matches ) {
return [
'provider' => $provider,
'video_id' => $matches[1],
];
}
}
return null;
}
/**
* Get embed URL.
*
* Retrieve the embed URL for a given video.
*
* @since 1.5.0
* @access public
* @static
*
* @param string $video_url Video URL.
* @param array $embed_url_params Optional. Embed parameters. Default is an
* empty array.
* @param array $options Optional. Embed options. Default is an
* empty array.
*
* @return null|array The video properties, or null.
*/
public static function get_embed_url( $video_url, array $embed_url_params = [], array $options = [] ) {
$video_properties = self::get_video_properties( $video_url );
if ( ! $video_properties ) {
return null;
}
$embed_pattern = self::$embed_patterns[ $video_properties['provider'] ];
$replacements = [
'{VIDEO_ID}' => $video_properties['video_id'],
];
if ( 'youtube' === $video_properties['provider'] ) {
$replacements['{NO_COOKIE}'] = ! empty( $options['privacy'] ) ? '-nocookie' : '';
} elseif ( 'vimeo' === $video_properties['provider'] ) {
$time_text = '';
if ( ! empty( $options['start'] ) ) {
$time_text = date( 'H\hi\ms\s', $options['start'] );
}
$replacements['{TIME}'] = $time_text;
}
$embed_pattern = str_replace( array_keys( $replacements ), $replacements, $embed_pattern );
return Leo_Helper::add_query_arg( $embed_url_params, $embed_pattern );
}
/**
* Get embed HTML.
*
* Retrieve the final HTML of the embedded URL.
*
* @since 1.5.0
* @access public
* @static
*
* @param string $video_url Video URL.
* @param array $embed_url_params Optional. Embed parameters. Default is an
* empty array.
* @param array $options Optional. Embed options. Default is an
* empty array.
* @param array $frame_attributes Optional. IFrame attributes. Default is an
* empty array.
*
* @return string The embed HTML.
*/
public static function get_embed_html( $video_url, array $embed_url_params = [], array $options = [], array $frame_attributes = [] ) {
$default_frame_attributes = [
'class' => 'elementor-video-iframe',
'allowfullscreen',
];
$video_embed_url = self::get_embed_url( $video_url, $embed_url_params, $options );
if ( ! $video_embed_url ) {
return null;
}
if ( ! $options['lazy_load'] ) {
$default_frame_attributes['src'] = $video_embed_url;
} else {
$default_frame_attributes['data-lazy-load'] = $video_embed_url;
}
$frame_attributes = array_merge( $default_frame_attributes, $frame_attributes );
$attributes_for_print = [];
foreach ( $frame_attributes as $attribute_key => $attribute_value ) {
$attribute_value = Leo_Helper::esc_attr( $attribute_value );
if ( is_numeric( $attribute_key ) ) {
$attributes_for_print[] = $attribute_value;
} else {
$attributes_for_print[] = sprintf( '%1$s="%2$s"', $attribute_key, $attribute_value );
}
}
$attributes_for_print = implode( ' ', $attributes_for_print );
$iframe_html = "<iframe $attributes_for_print></iframe>";
/** This filter is documented in wp-includes/class-oembed.php */
return Leo_Helper::apply_filters( 'oembed_result', $iframe_html, $video_url, $frame_attributes );
}
}