1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* VideoPress Shortcode Handler |
5
|
|
|
* |
6
|
|
|
* This file may or may not be included from the Jetpack VideoPress module. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Translate a 'videopress' or 'wpvideo' shortcode and arguments into a video player display. |
11
|
|
|
* |
12
|
|
|
* Expected input formats: |
13
|
|
|
* |
14
|
|
|
* [videopress OcobLTqC] |
15
|
|
|
* [wpvideo OcobLTqC] |
16
|
|
|
* |
17
|
|
|
* @link http://codex.wordpress.org/Shortcode_API Shortcode API |
18
|
|
|
* @param array $attr shortcode attributes |
19
|
|
|
* @return string HTML markup or blank string on fail |
20
|
|
|
*/ |
21
|
|
|
function videopress_shortcode_callback( $attr ) { |
22
|
|
|
global $content_width; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* We only accept GUIDs as a first unnamed argument. |
26
|
|
|
*/ |
27
|
|
|
$guid = isset( $attr[0] ) ? $attr[0] : null; |
28
|
|
|
|
29
|
|
|
if ( isset( $attr['postid'] ) ) { |
30
|
|
|
$guid = get_post_meta( $attr['postid'], 'videopress_guid', true ); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Make sure the GUID passed in matches how actual GUIDs are formatted. |
35
|
|
|
*/ |
36
|
|
|
if ( ! videopress_is_valid_guid( $guid ) ) { |
37
|
|
|
return ''; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set the defaults |
42
|
|
|
*/ |
43
|
|
|
$defaults = array( |
44
|
|
|
'w' => 0, // Width of the video player, in pixels |
45
|
|
|
'at' => 0, // How many seconds in to initially seek to |
46
|
|
|
'hd' => true, // Whether to display a high definition version |
47
|
|
|
'loop' => false, // Whether to loop the video repeatedly |
48
|
|
|
'freedom' => false, // Whether to use only free/libre codecs |
49
|
|
|
'autoplay' => false, // Whether to autoplay the video on load |
50
|
|
|
'permalink' => true, // Whether to display the permalink to the video |
51
|
|
|
'flashonly' => false, // Whether to support the Flash player exclusively |
52
|
|
|
'defaultlangcode' => false, // Default language code |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$attr = shortcode_atts( $defaults, $attr, 'videopress' ); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Cast the attributes, post-input. |
59
|
|
|
*/ |
60
|
|
|
$attr['width'] = absint( $attr['w'] ); |
61
|
|
|
$attr['hd'] = (bool) $attr['hd']; |
62
|
|
|
$attr['freedom'] = (bool) $attr['freedom']; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* If the provided width is less than the minimum allowed |
66
|
|
|
* width, or greater than `$content_width` ignore. |
67
|
|
|
*/ |
68
|
|
|
if ( $attr['width'] < VIDEOPRESS_MIN_WIDTH ) { |
69
|
|
|
$attr['width'] = 0; |
70
|
|
View Code Duplication |
} elseif ( isset( $content_width ) && $content_width > VIDEOPRESS_MIN_WIDTH && $attr['width'] > $content_width ) { |
71
|
|
|
$attr['width'] = 0; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* If there was an invalid or unspecified width, set the width equal to the theme's `$content_width`. |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
if ( 0 === $attr['width'] && isset( $content_width ) && $content_width >= VIDEOPRESS_MIN_WIDTH ) { |
78
|
|
|
$attr['width'] = $content_width; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* If the width isn't an even number, reduce it by one (making it even). |
83
|
|
|
*/ |
84
|
|
|
if ( 1 === ( $attr['width'] % 2 ) ) { |
85
|
|
|
$attr['width'] --; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Filter the default VideoPress shortcode options. |
90
|
|
|
* |
91
|
|
|
* @module videopress |
92
|
|
|
* |
93
|
|
|
* @since 2.5.0 |
94
|
|
|
* |
95
|
|
|
* @param array $args Array of VideoPress shortcode options. |
96
|
|
|
*/ |
97
|
|
|
$options = apply_filters( 'videopress_shortcode_options', array( |
98
|
|
|
'at' => (int) $attr['at'], |
99
|
|
|
'hd' => $attr['hd'], |
100
|
|
|
'loop' => $attr['autoplay'] || $attr['loop'], |
101
|
|
|
'freedom' => $attr['freedom'], |
102
|
|
|
'autoplay' => $attr['autoplay'], |
103
|
|
|
'permalink' => $attr['permalink'], |
104
|
|
|
'force_flash' => (bool) $attr['flashonly'], |
105
|
|
|
'defaultlangcode' => $attr['defaultlangcode'], |
106
|
|
|
'forcestatic' => false, // This used to be a displayed option, but now is only |
107
|
|
|
// accessible via the `videopress_shortcode_options` filter. |
108
|
|
|
) ); |
109
|
|
|
|
110
|
|
|
// Register VideoPress scripts |
111
|
|
|
wp_register_script( 'videopress', 'https://v0.wordpress.com/js/videopress.js', array( 'jquery', 'swfobject' ), '1.09' ); |
112
|
|
|
|
113
|
|
|
require_once( dirname( __FILE__ ) . '/class.videopress-video.php' ); |
114
|
|
|
require_once( dirname( __FILE__ ) . '/class.videopress-player.php' ); |
115
|
|
|
|
116
|
|
|
$player = new VideoPress_Player( $guid, $attr['width'], $options ); |
117
|
|
|
|
118
|
|
|
if ( is_feed() ) { |
119
|
|
|
return $player->asXML(); |
120
|
|
|
} else { |
121
|
|
|
return $player->asHTML(); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
add_shortcode( 'videopress', 'videopress_shortcode_callback' ); |
125
|
|
|
add_shortcode( 'wpvideo', 'videopress_shortcode_callback' ); |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* By explicitly declaring the provider here, we can speed things up by not relying on oEmbed discovery. |
129
|
|
|
*/ |
130
|
|
|
wp_oembed_add_provider( '#^https?://videopress.com/v/.*#', 'http://public-api.wordpress.com/oembed/1.0/', true ); |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Adds a `for` query parameter to the oembed provider request URL. |
134
|
|
|
* @param String $oembed_provider |
135
|
|
|
* @return String $ehnanced_oembed_provider |
136
|
|
|
*/ |
137
|
|
|
function videopress_add_oembed_for_parameter( $oembed_provider ) { |
138
|
|
|
if ( false === stripos( $oembed_provider, 'videopress.com' ) ) { |
139
|
|
|
return $oembed_provider; |
140
|
|
|
} |
141
|
|
|
return add_query_arg( 'for', parse_url( home_url(), PHP_URL_HOST ), $oembed_provider ); |
142
|
|
|
} |
143
|
|
|
add_filter( 'oembed_fetch_url', 'videopress_add_oembed_for_parameter' ); |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Override the standard video short tag to also process videopress files as well. |
147
|
|
|
* |
148
|
|
|
* This will, parse the src given, and if it is a videopress file, it will parse as the |
149
|
|
|
* VideoPress shortcode instead. |
150
|
|
|
* |
151
|
|
|
* @param string $html Empty variable to be replaced with shortcode markup. |
152
|
|
|
* @param array $attr Attributes of the video shortcode. |
153
|
|
|
* @param string $content Video shortcode content. |
154
|
|
|
* @param int $instance Unique numeric ID of this video shortcode instance. |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
function videopress_code_shortcode_override($html, $attr, $content, $instance) { |
|
|
|
|
159
|
|
|
|
160
|
|
|
$videopress_guid = null; |
161
|
|
|
|
162
|
|
|
if ( isset( $attr['videopress_guid'] ) ) { |
163
|
|
|
$videopress_guid = $attr['videopress_guid']; |
164
|
|
|
|
165
|
|
|
} elseif ( isset ( $attr['src'] ) ) { |
166
|
|
|
$url = $attr['src']; |
167
|
|
|
|
168
|
|
|
if ( preg_match( '@videos.videopress.com/([a-z0-9]{8})/@i', $url, $matches ) ) { |
169
|
|
|
$videopress_guid = $matches[1]; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
} elseif ( isset( $attr['mp4'] ) ) { |
173
|
|
|
$url = $attr['mp4']; |
174
|
|
|
|
175
|
|
|
if ( preg_match( '@videos.videopress.com/([a-z0-9]{8})/@', $url, $matches ) ) { |
176
|
|
|
$videopress_guid = $matches[1]; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if ( $videopress_guid ) { |
181
|
|
|
$videopress_attr = array( $videopress_guid ); |
182
|
|
|
if ( isset( $attr['width'] ) ) { |
183
|
|
|
$videopress_attr['w'] = (int) $attr['width']; |
184
|
|
|
} |
185
|
|
|
if ( isset( $attr['autoplay'] ) ) { |
186
|
|
|
$videopress_attr['autoplay'] = $attr['autoplay']; |
187
|
|
|
} |
188
|
|
|
if ( isset( $attr['loop'] ) ) { |
189
|
|
|
$videopress_attr['loop'] = $attr['loop']; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
// Then display the VideoPress version of the stored GUID! |
193
|
|
|
return videopress_shortcode_callback( $videopress_attr ); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return ''; |
197
|
|
|
|
198
|
|
|
} |
199
|
|
|
add_filter('wp_video_shortcode_override', 'videopress_code_shortcode_override', 10, 4); |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.