Completed
Pull Request — develop (#1350)
by Naveen
03:15
created

Vimeo::get_video_from_video_data()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 1
dl 0
loc 26
rs 9.1928
c 0
b 0
f 0
1
<?php
2
/**
3
 * @since 3.31.0
4
 * @author Naveen Muthusamy <[email protected]>
5
 */
6
7
namespace Wordlift\Videoobject\Provider;
8
9
use Wordlift\Videoobject\Data\Video\Video;
10
11
/**
12
 * Class Vimeo
13
 * @package Wordlift\Videoobject\Provider
14
 */
15
class Vimeo extends Api_Provider {
16
17
	const API_FIELD_NAME = '_wl_videoobject_vimeo_api_key';
18
19
	public static function get_api_key() {
20
		return get_option( self::API_FIELD_NAME, false );
21
	}
22
23
	public function get_videos_data( $videos ) {
24
25
		$key = $this->get_api_key();
26
27
		if ( ! $key ) {
28
			return array();
29
		}
30
31
		$urls = array_map( function ( $video ) {
32
			return $video->get_url();
33
		}, $videos );
34
35
36
		$response_body = $this->api_client->get_data( $urls );
37
38
		if ( ! is_string( $response_body ) || ! $response_body ) {
39
			return array();
40
		}
41
42
		$response = json_decode( $response_body, true );
43
44
		$video_list = $response['data'];
45
46
47
		if ( ! is_array( $video_list ) ) {
48
			// Return if we cant parse the response.
49
			return array();
50
		}
51
52
53
		return array_filter( array_map( array( $this, 'get_video_from_video_data' ), $video_list ) );
54
55
	}
56
57
	public function get_video_from_video_data( $vimeo_video_data ) {
58
59
		if ( ! $vimeo_video_data ) {
60
			// If valid data not supplied dont init the object.
61
			return false;
62
		}
63
		$video              = new Video();
64
		$video->name        = $vimeo_video_data['name'];
65
		$video->description = $vimeo_video_data['description'];
66
67
		$video->content_url = $vimeo_video_data['link'];
68
		$video->embed_url   = "https://player.vimeo.com/video/" . $this->get_id( $vimeo_video_data );
69
		if ( is_numeric( $vimeo_video_data['duration'] ) ) {
70
			$video->duration = "PT" . $vimeo_video_data['duration'] . "S";
71
		}
72
		$video->upload_date    = $vimeo_video_data['release_time'];
73
		$video->thumbnail_urls = $this->set_thumbnail_urls( $vimeo_video_data );
74
		$video->id             = $video->content_url;
75
76
		if ( array_key_exists( 'stats', $vimeo_video_data )
77
		     && array_key_exists( 'plays', $vimeo_video_data['stats'] ) ) {
78
			$video->views = (int) $vimeo_video_data['stats']['plays'];
79
		}
80
81
		return $video;
82
	}
83
84
	private function get_id( $api_response_data ) {
85
		return str_replace( "/videos/", "", $api_response_data['uri'] );
86
	}
87
88
89
	private function set_thumbnail_urls( $api_response_data ) {
90
91
		if ( ! array_key_exists( 'pictures', $api_response_data ) || ! array_key_exists( 'sizes',
92
				$api_response_data['pictures'] ) ) {
93
			return array();
94
		}
95
		if ( ! is_array( $api_response_data['pictures']['sizes'] ) ) {
96
			return array();
97
		}
98
		$pictures = $api_response_data['pictures']['sizes'];
99
100
		return array_map( function ( $picture_data ) {
101
			return $picture_data['link'];
102
		}, $pictures );
103
104
	}
105
106
107
}