forked from findingsimple/acf-field-video
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video.php
executable file
·349 lines (254 loc) · 8.33 KB
/
video.php
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
class acf_field_video extends acf_field
{
// vars
var $settings, // will hold info such as dir / path
$defaults; // will hold default field options
/*
* __construct
*
*/
function __construct()
{
// vars
$this->name = 'video';
$this->label = __('Video');
$this->category = __("Content",'acf'); // Basic, Content, Choice, etc
$this->defaults = array(
// add default here to merge into your field.
// This makes life easy when creating the field options as you don't need to use any if( isset('') ) logic. eg:
'preview_type' => 'embed', //whether to display a preview for the video. Display video embed, thumbnail image or none
'return_value' => 'url', //return the url, embed code, thumbnail url or array or all
);
// do not delete!
parent::__construct();
// settings
$this->settings = array(
'path' => apply_filters('acf/helpers/get_path', __FILE__),
'dir' => apply_filters('acf/helpers/get_dir', __FILE__),
'version' => '1.0.0'
);
}
/*
* create_options()
*
*/
function create_options( $field )
{
// defaults?
$field = array_merge($this->defaults, $field);
// key is needed in the field names to correctly save the data
$key = $field['name'];
// Create Field Options HTML
?>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php _e("Preview",'acf'); ?></label>
<p class="description"><?php _e("Preview type to display in the dashboard.",'acf'); ?></p>
</td>
<td>
<?php
do_action('acf/create_field', array(
'type' => 'radio',
'name' => 'fields[' . $key . '][preview_type]',
'value' => $field['preview_type'],
'layout' => 'horizontal',
'choices' => array(
'embed' => __('Embed'),
'thumbnail' => __('Thumbnail'),
'none' => __('None')
)
));
?>
</td>
</tr>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php _e("Return Value",'acf'); ?></label>
<p class="description"><?php _e("Specify the returned value on front end.",'acf'); ?></p>
</td>
<td>
<?php
do_action('acf/create_field', array(
'type' => 'radio',
'name' => 'fields[' . $key . '][return_value]',
'value' => $field['return_value'],
'layout' => 'horizontal',
'choices' => array(
'url' => __('Video URL'),
'embed' => __('Embed Code'),
'thumbnail' => __('Thumbnail URL'),
'array' => __('Array')
)
));
?>
</td>
</tr>
<?php
}
/*
* create_field()
*
*/
function create_field( $field )
{
global $post;
// defaults?
$field = array_merge($this->defaults, $field);
if ( !empty( $field['value'] ) && ( $field['preview_type'] == 'thumbnail' ) )
$thumbnail = self::get_video_thumbnail_uri( $field['value'] );
if ( !empty ( $field['value'] ) && ( $field['preview_type'] == 'embed' ) )
$embed = wp_oembed_get( $field['value'] );
?>
<?php if ( !empty( $thumbnail ) ) { ?>
<a href="<?php echo $field['value']; ?>" title="" target="_blank" style="display:block;padding-top:4px;margin-bottom:10px;" ><img src='<?php echo $thumbnail; ?>' alt='' style="display:block;width:100%;" /></a>
<?php } ?>
<?php if ( !empty( $embed ) ) {
echo '<div class="acf-video-field-embed" style="margin-bottom:10px;">' . $embed . '</div>';
} ?>
<input type="text" id="<?php echo $field['key']; ?>" class="<?php echo $field['class']; ?>" name="<?php echo $field['name']; ?>" value="<?php echo $field['value']; ?>" />
<?php
}
/*
* format_value_for_api()
*
*/
function format_value_for_api($value, $post_id, $field)
{
if ( !empty( $value ) && ( $field['return_value'] == 'thumbnail' ) )
$updated_value = self::get_video_thumbnail_uri( $value );
if ( !empty ( $value ) && ( $field['return_value'] == 'embed' ) )
$updated_value = wp_oembed_get( $value );
if ( !empty ( $value ) && ( $field['return_value'] == 'array' ) ) {
$updated_value = array(
'url' => $value,
'embed' => wp_oembed_get( $value ),
'thumbnail' => self::get_video_thumbnail_uri( $value )
);
}
if ( !empty ( $updated_value ) )
return $updated_value;
else
return $value;
}
/*
* input_admin_enqueue_scripts()
*
*/
function input_admin_enqueue_scripts()
{
// register scripts
wp_register_script( 'jquery-fitvids', $this->settings['dir'] . 'js/jquery.fitvids.js', array('jquery'), $this->settings['version'] );
wp_register_script( 'acf-input-video', $this->settings['dir'] . 'js/input.js', array('acf-input', 'jquery-fitvids'), $this->settings['version'] );
// enqueu scripts
wp_enqueue_script(array(
'jquery-fitvids',
));
wp_enqueue_script(array(
'acf-input-video',
));
}
/**
* Get the video thumbnail
*/
function get_video_thumbnail_uri( $video_uri ) {
$thumbnail_uri = '';
// determine the type of video and the video id
$video = self::parse_video_uri( $video_uri );
// get youtube thumbnail
if ( $video['type'] == 'youtube' )
$thumbnail_uri = 'http://img.youtube.com/vi/' . $video['id'] . '/maxresdefault.jpg';
// get vimeo thumbnail
if( $video['type'] == 'vimeo' )
$thumbnail_uri = self::get_vimeo_thumbnail_uri( $video['id'] );
// get wistia thumbnail
if( $video['type'] == 'wistia' )
$thumbnail_uri = self::get_wistia_thumbnail_uri( $video_uri );
// get default/placeholder thumbnail
if( empty( $thumbnail_uri ) || is_wp_error( $thumbnail_uri ) )
$thumbnail_uri = '';
//return thumbnail uri
return $thumbnail_uri;
}
/**
* Parse the video uri/url to determine the video type/source and the video id
*/
function parse_video_uri( $url ) {
// Parse the url
$parse = parse_url( $url );
// Set blank variables
$video_type = '';
$video_id = '';
// Url is http://youtu.be/xxxx
if ( $parse['host'] == 'youtu.be' ) {
$video_type = 'youtube';
$video_id = ltrim( $parse['path'],'/' );
}
// Url is http://www.youtube.com/watch?v=xxxx
// or http://www.youtube.com/watch?feature=player_embedded&v=xxx
// or http://www.youtube.com/embed/xxxx
if ( ( $parse['host'] == 'youtube.com' ) || ( $parse['host'] == 'www.youtube.com' ) ) {
$video_type = 'youtube';
parse_str( $parse['query'] );
$video_id = $v;
if ( !empty( $feature ) )
$video_id = end( explode( 'v=', $parse['query'] ) );
if ( strpos( $parse['path'], 'embed' ) == 1 )
$video_id = end( explode( '/', $parse['path'] ) );
}
// Url is http://www.vimeo.com
if ( ( $parse['host'] == 'vimeo.com' ) || ( $parse['host'] == 'www.vimeo.com' ) ) {
$video_type = 'vimeo';
$video_id = ltrim( $parse['path'],'/' );
}
$host_names = explode(".", $parse['host'] );
$rebuild = ( ! empty( $host_names[1] ) ? $host_names[1] : '') . '.' . ( ! empty($host_names[2] ) ? $host_names[2] : '');
// Url is an oembed url wistia.com
if ( ( $rebuild == 'wistia.com' ) || ( $rebuild == 'wi.st.com' ) ) {
$video_type = 'wistia';
if ( strpos( $parse['path'], 'medias' ) == 1 )
$video_id = end( explode( '/', $parse['path'] ) );
}
// If recognised type return video array
if ( !empty( $video_type ) ) {
$video_array = array(
'type' => $video_type,
'id' => $video_id
);
return $video_array;
} else {
return false;
}
}
/**
* Takes a Vimeo video/clip ID and calls the Vimeo API v2 to get the large thumbnail URL.
*/
function get_vimeo_thumbnail_uri( $clip_id ) {
$vimeo_api_uri = 'http://vimeo.com/api/v2/video/' . $clip_id . '.php';
$vimeo_response = wp_remote_get( $vimeo_api_uri );
if( is_wp_error( $vimeo_response ) ) {
return $vimeo_response;
} else {
$vimeo_response = unserialize( $vimeo_response['body'] );
return $vimeo_response[0]['thumbnail_large'];
}
}
/**
* Takes a wistia oembed url and gets the video thumbnail url.
*/
function get_wistia_thumbnail_uri( $video_uri ) {
if ( empty($video_uri) )
return false;
$wistia_api_uri = 'http://fast.wistia.com/oembed?url=' . $video_uri;
$wistia_response = wp_remote_get( $wistia_api_uri );
if( is_wp_error( $wistia_response ) ) {
return $wistia_response;
} else {
$wistia_response = json_decode( $wistia_response['body'], true );
return $wistia_response['thumbnail_url'];
}
}
}
// create field
new acf_field_video();
?>