@@ -26,24 +26,7 @@ public function Field($properties = [])
26
26
$ this ->setAttribute ('data-apikey ' , $ api_key );
27
27
Requirements::javascript ('https://apis.google.com/js/client.js?onload=googleApiClientReady ' );
28
28
} elseif (!empty ($ this ->value ) && self ::url_parser ($ this ->value )) {
29
- $ client = new Client ();
30
- try {
31
- $ res = $ client ->get ('https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v= ' . $ this ->value . '&format=json ' );
32
- if ($ res ->getStatusCode () == '200 ' && $ data = json_decode ($ res ->getBody ())) {
33
- $ api_data = new \stdClass ();
34
- $ api_data ->id = $ this ->value ;
35
- $ api_data ->snippet = new \stdClass ();
36
- $ api_data ->snippet ->title = $ data ->title ;
37
- if (preg_match ('/user\/([\w-]+)/ ' , $ data ->author_url , $ author )) {
38
- $ api_data ->snippet ->channelTitle = $ author [1 ];
39
- }
40
- $ api_data ->snippet ->thumbnails = new \stdClass ();
41
- $ api_data ->snippet ->thumbnails ->default = new \stdClass ();
42
- $ api_data ->snippet ->thumbnails ->default ->url = $ data ->thumbnail_url ;
43
-
44
- $ this ->setAttribute ('data-apidata ' , json_encode ($ api_data ));
45
- }
46
- } catch (ClientException $ e ) {}
29
+ $ this ->setAttribute ('data-apidata ' , json_encode (static ::get_video_information ($ this ->value )));
47
30
}
48
31
49
32
return parent ::Field ($ properties );
@@ -73,6 +56,28 @@ public function Type()
73
56
return 'text youtube ' ;
74
57
}
75
58
59
+ public static function get_video_information ($ videoId )
60
+ {
61
+ $ client = new Client ();
62
+ try {
63
+ $ res = $ client ->get ('https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v= ' . $ videoId . '&format=json ' );
64
+ if ($ res ->getStatusCode () == '200 ' && $ data = json_decode ($ res ->getBody ())) {
65
+ $ api_data = new \stdClass ();
66
+ $ api_data ->id = $ videoId ;
67
+ $ api_data ->snippet = new \stdClass ();
68
+ $ api_data ->snippet ->title = $ data ->title ;
69
+ if (preg_match ('/user\/([\w-]+)/ ' , $ data ->author_url , $ author )) {
70
+ $ api_data ->snippet ->channelTitle = $ author [1 ];
71
+ }
72
+ $ api_data ->snippet ->thumbnails = new \stdClass ();
73
+ $ api_data ->snippet ->thumbnails ->default = new \stdClass ();
74
+ $ api_data ->snippet ->thumbnails ->default ->url = $ data ->thumbnail_url ;
75
+
76
+ return $ api_data ;
77
+ }
78
+ } catch (ClientException $ e ) {}
79
+ }
80
+
76
81
/**
77
82
* Parse YouTube URL into a valid 11-character ID.
78
83
*
0 commit comments