1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Created by PhpStorm. |
5
|
|
|
* User: steve |
6
|
|
|
* Date: 4/17/2018 |
7
|
|
|
* Time: 10:09 PM |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
if ( ! class_exists( "FooGallery_Pro_Video_oEmbed" ) ) { |
11
|
|
|
|
12
|
|
|
require_once dirname( __FILE__ ) . '/class-foogallery-pro-video-base.php'; |
13
|
|
|
|
14
|
|
|
class FooGallery_Pro_Video_oEmbed extends FooGallery_Pro_Video_Base { |
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Takes a URL and attempts to return a result generated from its oEmbed data. |
18
|
|
|
* |
19
|
|
|
* @param string $url The url to fetch. |
20
|
|
|
* |
21
|
|
|
* @return array( |
|
|
|
|
22
|
|
|
* "mode" => "embed", |
23
|
|
|
* "videos" => array( |
24
|
|
|
* array( |
25
|
|
|
* "provider" => string, |
26
|
|
|
* "id" => string, |
27
|
|
|
* "url" => string, |
28
|
|
|
* "thumbnail" => string, |
29
|
|
|
* "title" => string, |
30
|
|
|
* "description" => string, |
31
|
|
|
* "html" => string |
32
|
|
|
* ) |
33
|
|
|
* ) |
34
|
|
|
* ) |
35
|
|
|
* @return array( |
|
|
|
|
36
|
|
|
* "mode" => "error", |
37
|
|
|
* "title" => string, |
38
|
|
|
* "message" => string |
39
|
|
|
* ) |
40
|
|
|
*/ |
41
|
|
|
function fetch( $url ) { |
|
|
|
|
42
|
|
|
// check if the url is not empty and is not false |
43
|
|
|
if ( empty( $url ) || $url === false ) { |
44
|
|
|
return $this->error_response( "Invalid url supplied." ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$data = foogallery_oembed_get_data( $url ); |
48
|
|
|
if ( $data === false ) { |
49
|
|
|
return $this->error_response( "Unable to retrieve any data from the supplied URL." ); |
50
|
|
|
} |
51
|
|
|
if ( strtolower( $data->type ) !== "video" ) { |
52
|
|
|
return $this->error_response( "The data returned for the supplied URL was not a video." ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$provider = sanitize_title($data->provider_name, "oembed"); |
56
|
|
|
|
57
|
|
|
$video = array( |
58
|
|
|
"provider" => $provider, |
59
|
|
|
"id" => $this->get_id($provider, $url), |
60
|
|
|
"url" => $url, |
61
|
|
|
"thumbnail" => $data->thumbnail_url, |
62
|
|
|
"title" => $data->title, |
63
|
|
|
"description" => !empty($data->description) ? $data->description : "" |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
if (empty($data->thumbnail_url) || empty($data->title)) { |
67
|
|
|
$video["mode"] = "oembed"; |
68
|
|
|
return $video; |
69
|
|
|
} |
70
|
|
|
return array( |
71
|
|
|
"mode" => "embed", |
72
|
|
|
"videos" => array($video) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Gets a unique ID for the supplied provider and url. |
78
|
|
|
* |
79
|
|
|
* @param string $provider The provider for the current URL. |
80
|
|
|
* @param string $url The URL to the oEmbed video. |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
private function get_id( $provider, $url ) { |
|
|
|
|
85
|
|
|
switch ( $provider ) { |
86
|
|
|
default: |
|
|
|
|
87
|
|
|
// this is setup so in the future we can handle different providers accordingly |
88
|
|
|
// but by default a ID is simply generated by hashing the url. This works fine as |
89
|
|
|
// the ID for oEmbed videos is just used to generate the uploaded filename and if |
90
|
|
|
// there is a duplicate WordPress will auto-append a count to the name in any case. |
91
|
|
|
return hash( 'crc32', $url, false ); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.