|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wordlift\Videoobject\Provider\Client; |
|
4
|
|
|
|
|
5
|
|
|
use Wordlift\Common\Singleton; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @since 3.31.0 |
|
9
|
|
|
* @author Naveen Muthusamy <[email protected]> |
|
10
|
|
|
* This class acts as api client for vimeo. |
|
11
|
|
|
*/ |
|
12
|
|
|
class Vimeo_Client extends Singleton implements Client { |
|
13
|
|
|
|
|
14
|
|
|
static $requests_sent = 0; |
|
15
|
|
|
|
|
16
|
|
|
const VIMEO_URL_REGEX = '/https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)/'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param $url string Vimeo url |
|
20
|
|
|
*/ |
|
21
|
|
|
public function vimeo_url_to_id( $url ) { |
|
22
|
|
|
if ( ! $url ) { |
|
23
|
|
|
return false; |
|
24
|
|
|
} |
|
25
|
|
|
preg_match( self::VIMEO_URL_REGEX, $url, $matches ); |
|
26
|
|
|
|
|
27
|
|
|
if ( ! array_key_exists( 3, $matches ) ) { |
|
28
|
|
|
return false; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return '/videos/' . $matches[3]; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param $vimeo_urls |
|
36
|
|
|
* @param $post_id |
|
37
|
|
|
* |
|
38
|
|
|
* @return string[] |
|
39
|
|
|
*/ |
|
40
|
|
|
public function get_video_ids_for_api( $vimeo_urls ) { |
|
41
|
|
|
|
|
42
|
|
|
return array_filter( array_map( array( $this, 'vimeo_url_to_id' ), $vimeo_urls ) ); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
public function get_data( $video_urls ) { |
|
47
|
|
|
$ids = join( ",", $this->get_video_ids_for_api( $video_urls ) ); |
|
48
|
|
|
|
|
49
|
|
|
if ( ! $ids ) { |
|
50
|
|
|
return array(); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$api_url = $this->get_api_url() . "/videos/"; |
|
54
|
|
|
$api_url = add_query_arg( array( |
|
55
|
|
|
'uris' => $ids, |
|
56
|
|
|
'fields' => 'name,description,link,uri,duration,release_time,pictures,stats' |
|
57
|
|
|
), $api_url ); |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
$response = wp_remote_get( $api_url, array( |
|
61
|
|
|
'headers' => array( |
|
62
|
|
|
'Authorization' => 'bearer ' . $this->get_api_key() |
|
63
|
|
|
) |
|
64
|
|
|
) ); |
|
65
|
|
|
|
|
66
|
|
|
self::$requests_sent += 1; |
|
67
|
|
|
|
|
68
|
|
|
return wp_remote_retrieve_body( $response ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public static function get_api_key() { |
|
72
|
|
|
return get_option( self::get_api_key_option_name(), false ); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public static function get_api_key_option_name() { |
|
76
|
|
|
return '_wl_videoobject_vimeo_api_key'; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function get_api_url() { |
|
80
|
|
|
return 'https://api.vimeo.com'; |
|
81
|
|
|
} |
|
82
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.