PluginProbe
Elementor Website Builder – more than just a page builder / 3.7.8
Elementor Website Builder – more than just a page builder v3.7.8
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / includes / embed.php

embed.php in Elementor Website Builder – more than just a page builder 3.7.8, at includes/embed.php

280 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit; // Exit if accessed directly.
6 }
7
8 /**
9 * Elementor embed.
10 *
11 * Elementor embed handler class is responsible for Elementor embed functionality.
12 * The class holds the supported providers with their embed patters, and handles
13 * their custom properties to create custom HTML with the embeded content.
14 *
15 * @since 1.5.0
16 */
17 class Embed {
18
19 /**
20 * Provider match masks.
21 *
22 * Holds a list of supported providers with their URL structure in a regex format.
23 *
24 * @since 1.5.0
25 * @access private
26 * @static
27 *
28 * @var array Provider URL structure regex.
29 */
30 private static $provider_match_masks = [
31 'youtube' => '/^.*(?:youtu\.be\/|youtube(?:-nocookie)?\.com\/(?:(?:watch)?\?(?:.*&)?vi?=|(?:embed|v|vi|user)\/))([^\?&\"\'>]+)/',
32 'vimeo' => '/^.*vimeo\.com\/(?:[a-z]*\/)*([‌​0-9]{6,11})[?]?.*/',
33 'dailymotion' => '/^.*dailymotion.com\/(?:video|hub)\/([^_]+)[^#]*(#video=([^_&]+))?/',
34 ];
35
36 /**
37 * Embed patterns.
38 *
39 * Holds a list of supported providers with their embed patters.
40 *
41 * @since 1.5.0
42 * @access private
43 * @static
44 *
45 * @var array Embed patters.
46 */
47 private static $embed_patterns = [
48 'youtube' => 'https://www.youtube{NO_COOKIE}.com/embed/{VIDEO_ID}?feature=oembed',
49 'vimeo' => 'https://player.vimeo.com/video/{VIDEO_ID}#t={TIME}',
50 'dailymotion' => 'https://dailymotion.com/embed/video/{VIDEO_ID}',
51 ];
52
53 /**
54 * Get video properties.
55 *
56 * Retrieve the video properties for a given video URL.
57 *
58 * @since 1.5.0
59 * @access public
60 * @static
61 *
62 * @param string $video_url Video URL.
63 *
64 * @return null|array The video properties, or null.
65 */
66 public static function get_video_properties( $video_url ) {
67 foreach ( self::$provider_match_masks as $provider => $match_mask ) {
68 preg_match( $match_mask, $video_url, $matches );
69
70 if ( $matches ) {
71 return [
72 'provider' => $provider,
73 'video_id' => $matches[1],
74 ];
75 }
76 }
77
78 return null;
79 }
80
81 /**
82 * Get embed URL.
83 *
84 * Retrieve the embed URL for a given video.
85 *
86 * @since 1.5.0
87 * @access public
88 * @static
89 *
90 * @param string $video_url Video URL.
91 * @param array $embed_url_params Optional. Embed parameters. Default is an
92 * empty array.
93 * @param array $options Optional. Embed options. Default is an
94 * empty array.
95 *
96 * @return null|array The video properties, or null.
97 */
98 public static function get_embed_url( $video_url, array $embed_url_params = [], array $options = [] ) {
99 $video_properties = self::get_video_properties( $video_url );
100
101 if ( ! $video_properties ) {
102 return null;
103 }
104
105 $embed_pattern = self::$embed_patterns[ $video_properties['provider'] ];
106
107 $replacements = [
108 '{VIDEO_ID}' => $video_properties['video_id'],
109 ];
110
111 if ( 'youtube' === $video_properties['provider'] ) {
112 $replacements['{NO_COOKIE}'] = ! empty( $options['privacy'] ) ? '-nocookie' : '';
113 } elseif ( 'vimeo' === $video_properties['provider'] ) {
114 $time_text = '';
115
116 if ( ! empty( $options['start'] ) ) {
117 $time_text = date( 'H\hi\ms\s', $options['start'] ); // PHPCS:Ignore WordPress.DateTime.RestrictedFunctions.date_date
118 }
119
120 $replacements['{TIME}'] = $time_text;
121
122 /**
123 * Handle Vimeo private videos
124 *
125 * Vimeo requires an additional parameter when displaying private/unlisted videos. It has two ways of
126 * passing that parameter:
127 * * as an endpoint - vimeo.com/{video_id}/{privacy_token}
128 * OR
129 * * as a GET parameter named `h` - vimeo.com/{video_id}?h={privacy_token}
130 *
131 * The following regex match looks for either of these methods in the Vimeo URL, and if it finds a privacy
132 * token, it adds it to the embed params array as the `h` parameter (which is how Vimeo can receive it when
133 * using Oembed).
134 */
135 $h_param = [];
136 preg_match( '/(?|(?:[\?|\&]h={1})([\w]+)|\d\/([\w]+))/', $video_url, $h_param );
137
138 if ( ! empty( $h_param ) ) {
139 $embed_url_params['h'] = $h_param[1];
140 }
141 }
142
143 $embed_pattern = str_replace( array_keys( $replacements ), $replacements, $embed_pattern );
144
145 return add_query_arg( $embed_url_params, $embed_pattern );
146 }
147
148 /**
149 * Get embed HTML.
150 *
151 * Retrieve the final HTML of the embedded URL.
152 *
153 * @since 1.5.0
154 * @access public
155 * @static
156 *
157 * @param string $video_url Video URL.
158 * @param array $embed_url_params Optional. Embed parameters. Default is an
159 * empty array.
160 * @param array $options Optional. Embed options. Default is an
161 * empty array.
162 * @param array $frame_attributes Optional. IFrame attributes. Default is an
163 * empty array.
164 *
165 * @return string The embed HTML.
166 */
167 public static function get_embed_html( $video_url, array $embed_url_params = [], array $options = [], array $frame_attributes = [] ) {
168 $video_properties = self::get_video_properties( $video_url );
169
170 $default_frame_attributes = [
171 'class' => 'elementor-video-iframe',
172 'allowfullscreen',
173 'title' => sprintf(
174 /* translators: %s: Video provider */
175 __( '%s Video Player', 'elementor' ),
176 $video_properties['provider']
177 ),
178 ];
179
180 $video_embed_url = self::get_embed_url( $video_url, $embed_url_params, $options );
181 if ( ! $video_embed_url ) {
182 return null;
183 }
184 if ( ! $options['lazy_load'] ) {
185 $default_frame_attributes['src'] = $video_embed_url;
186 } else {
187 $default_frame_attributes['data-lazy-load'] = $video_embed_url;
188 }
189
190 $frame_attributes = array_merge( $default_frame_attributes, $frame_attributes );
191
192 $attributes_for_print = [];
193
194 foreach ( $frame_attributes as $attribute_key => $attribute_value ) {
195 $attribute_value = esc_attr( $attribute_value );
196
197 if ( is_numeric( $attribute_key ) ) {
198 $attributes_for_print[] = $attribute_value;
199 } else {
200 $attributes_for_print[] = sprintf( '%1$s="%2$s"', $attribute_key, $attribute_value );
201 }
202 }
203
204 $attributes_for_print = implode( ' ', $attributes_for_print );
205
206 $iframe_html = "<iframe $attributes_for_print></iframe>";
207
208 /** This filter is documented in wp-includes/class-oembed.php */
209 return apply_filters( 'oembed_result', $iframe_html, $video_url, $frame_attributes );
210 }
211
212 /**
213 * Get oembed data from the cache.
214 * if not exists in the cache it will fetch from provider and then save to the cache.
215 *
216 * @param $oembed_url
217 * @param $cached_post_id
218 *
219 * @return array|null
220 */
221 public static function get_oembed_data( $oembed_url, $cached_post_id ) {
222 $cached_oembed_data = json_decode( get_post_meta( $cached_post_id, '_elementor_oembed_cache', true ), true );
223
224 if ( isset( $cached_oembed_data[ $oembed_url ] ) ) {
225 return $cached_oembed_data[ $oembed_url ];
226 }
227
228 $normalize_oembed_data = self::fetch_oembed_data( $oembed_url );
229
230 if ( ! $cached_oembed_data ) {
231 $cached_oembed_data = [];
232 }
233
234 update_post_meta( $cached_post_id, '_elementor_oembed_cache', wp_json_encode( array_merge(
235 $cached_oembed_data,
236 [
237 $oembed_url => $normalize_oembed_data,
238 ]
239 ) ) );
240
241 return $normalize_oembed_data;
242 }
243
244 /**
245 * Fetch oembed data from oembed provider.
246 *
247 * @param $oembed_url
248 *
249 * @return array|null
250 */
251 public static function fetch_oembed_data( $oembed_url ) {
252 $oembed_data = _wp_oembed_get_object()->get_data( $oembed_url );
253
254 if ( ! $oembed_data ) {
255 return null;
256 }
257
258 return [
259 'thumbnail_url' => $oembed_data->thumbnail_url,
260 'title' => $oembed_data->title,
261 ];
262 }
263
264 /**
265 * @param $oembed_url
266 * @param null|string|int $cached_post_id
267 *
268 * @return string|null
269 */
270 public static function get_embed_thumbnail_html( $oembed_url, $cached_post_id = null ) {
271 $oembed_data = self::get_oembed_data( $oembed_url, $cached_post_id );
272
273 if ( ! $oembed_data ) {
274 return null;
275 }
276
277 return '<div class="elementor-image">' . sprintf( '<img src="%1$s" alt="%2$s" title="%2$s" width="%3$s" />', $oembed_data['thumbnail_url'], esc_attr( $oembed_data['title'] ), '100%' ) . '</div>';
278 }
279 }
280