1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
<?php
/** * This class handles video related functionality. * * Class SiteOrigin_Video */
class SiteOrigin_Video { /** * Check whether it's possible to oEmbed by testing if a provider URL can be obtained. * * @param string $url The URL of the video to be embedded. * * @return bool Whether it's possible to embed this video. */ function can_oembed( $url ) { $wp_oembed = new WP_oEmbed(); $provider = $wp_oembed->get_provider( $url, array( 'discover' => false ) ); return ! empty( $provider ); } /** * Gets a video source embed * * @param string $src The URL of the video. * @param bool $autoplay Whether to start playing the video automatically once loaded. ( YouTube only ) * @param bool $related_videos Whether to show related videos after the video has finished playing. ( YouTube only ) * * @return false|mixed|null|string|string[] */ function get_video_oembed( $src, $autoplay = false, $related_videos = true ) { if ( empty( $src ) ) { return ''; } global $content_width; $video_width = ! empty( $content_width ) ? $content_width : 640; $hash = md5( serialize( array( 'src' => $src, 'width' => $video_width, 'autoplay' => $autoplay, ) ) ); // Convert embed format to standard format to be compatible with wp_oembed_get $src = preg_replace('/https?:\/\/www.youtube.com\/embed\/([^\/]+)/', 'https://www.youtube.com/watch?v=$1', $src);
$html = get_transient( 'sow-vid-embed[' . $hash . ']' ); if ( empty( $html ) ) { $html = wp_oembed_get( $src, array( 'width' => $video_width ) ); if ( $autoplay ) { $html = preg_replace_callback( '/src=["\'](http[^"\']*)["\']/', array( $this, 'autoplay_callback' ), $html ); } if ( empty( $related_videos ) ) { $html = preg_replace_callback( '/src=["\'](http[^"\']*)["\']/', array( $this, 'remove_related_videos' ), $html ); } if ( ! empty( $html ) ) { set_transient( 'sow-vid-embed[' . $hash . ']', $html, 30 * 86400 ); } } return $html; } /** * The preg_replace callback that adds autoplay. * * @param $match * * @return mixed */ function autoplay_callback( $match ) { return str_replace( $match[1], add_query_arg( 'autoplay', 1, $match[1] ), $match[0] ); } /** * The preg_replace callback that adds the rel param for YouTube videos. * * @param $match * * @return mixed */ function remove_related_videos( $match ) { return str_replace( $match[1], add_query_arg( 'rel', 0, $match[1] ), $match[0] ); } }
|