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
|
<?php /** * Video Playlist Class * */
if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly
/*-----------------------------------------------------------------------------------*/ # Get Youtube Video data /*-----------------------------------------------------------------------------------*/ if ( ! class_exists( 'Penci_Video_List' ) ) {
class Penci_Video_List {
static $youtube_key = 'AIzaSyAPRocQxSO9mFJE9sbLDXXk_xaS201r50c'; static $youtube_api_base = 'https://www.googleapis.com/youtube/v3/videos'; static $vimeo_api_base = 'https://vimeo.com/api/v2/video/';
public function __construct() { if( is_admin() ){ add_action( 'wp_ajax_nopriv_penci_save_video_playlist', array( __CLASS__, 'save_video_playlist' ) ); add_action( 'wp_ajax_penci_save_video_playlist', array( __CLASS__, 'save_video_playlist' ) );
add_action( 'wp_ajax_nopriv_penci_remove_video_playlist', array( __CLASS__, 'remove_video_playlist' ) ); add_action( 'wp_ajax_penci_remove_video_playlist', array( __CLASS__, 'remove_video_playlist' ) ); } }
/** * Save Videos list */ public static function save_video_playlist() {
if ( empty( $_POST['nonce'] ) ) { wp_send_json_error(); }
$videos = isset( $_POST['videoList'] ) ? $_POST['videoList'] : ''; $shortcode_id = isset( $_POST['shortcodeId'] ) ? $_POST['shortcodeId'] : '';
$video_infos = self::get_video_infos( $videos );
$option_video = get_option( 'penci-shortcode-playlist-' . $shortcode_id );
if ( $option_video != $video_infos ) { update_option( 'penci-shortcode-playlist-' . $shortcode_id, $video_infos ); }
wp_send_json_success();
}
/** * Remove Videos list */ public static function remove_video_playlist() {
if ( empty( $_POST['nonce'] ) ) { wp_send_json_error(); }
$shortcode_id = isset( $_POST['shortcodeId'] ) ? $_POST['shortcodeId'] : '';
delete_option( 'penci-shortcode-playlist-' . $shortcode_id );
wp_send_json_success( 'penci-shortcode-playlist-' . $shortcode_id );
}
public static function get_video_infos( $videos ) { $videos_list = array(); if ( empty( $videos ) ) { return $videos_list; }
$videos_data = self::get_video_info( explode( ',', $videos ) );
if ( empty( $videos_data ) ) { return $videos_list; }
$youtube_thumb_base = 'https://i.ytimg.com/vi/'; $youtube_player_base = 'https://www.youtube.com/embed/'; $vimeo_thumb_base = 'https://i.vimeocdn.com/video/'; $vimeo_player_base = 'https://player.vimeo.com/video/';
foreach ( $videos_data as $video ) {
if ( empty( $video['id'] ) ) { continue; }
if ( 'youtube' == $video['type'] ) { $video['thumb'] = $youtube_thumb_base . $video['id'] . '/default.jpg'; $video['id'] = $youtube_player_base . $video['id'] . '?enablejsapi=1&rel=0&showinfo=0'; } elseif ( $video['type'] == 'vimeo' ) { $video['thumb'] = $vimeo_thumb_base . $video['thumb']; $video['id'] = $vimeo_player_base . $video['id'] . '?api=1&title=0&byline=0'; }
$videos_list[] = $video; }
return $videos_list; }
/** * Get Videos List data * * @param $videos_list * * @return array */ public static function get_video_info( $videos_list ) {
$videos_list = array_filter( $videos_list ); $videos_ids = array(); foreach ( $videos_list as $video ) {
// Youtube if ( preg_match( "#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $video, $matches ) ) {
$video_id = preg_replace( '/\s+/', '', $matches[0] );
if ( ! isset( $youtube_videos[ $video_id ] ) ) { $videos_ids[] = self::get_youtube_info( $video_id ); } } // Vimeo elseif ( preg_match( "/(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/([a-z]*\/)*([0-9]{6,11})[?]?.*/", $video, $matches ) ) { $video_id = preg_replace( '/\s+/', '', $matches[5] );
if ( ! isset( $vimeo_videos[ $video_id ] ) ) { $videos_ids[] = self::get_vimeo_info( $video_id ); } }
}
return $videos_ids; }
/** * Get Youtube Video data * * @param $vid * * @return null */ private static function get_youtube_info( $video_id ) { $video = array(); // Build the Api request $params = array( 'part' => 'snippet,contentDetails', 'id' => $video_id, 'key' => get_theme_mod( 'penci_youtube_api_key' ), );
$api_url = self::$youtube_api_base . '?' . http_build_query( $params );
$request = wp_remote_get( $api_url );
// Check if there are errors if ( is_wp_error( $request ) ) { return null; }
// Prepare the data $result = json_decode( wp_remote_retrieve_body( $request ), true );
// Check if the video title is exists if ( empty( $result['items'][0]['snippet']['title'] ) ) { return null; }
// Prepare the Video duration $video_info = $result['items'][0]['contentDetails'];
if ( ! empty( $video_info['duration'] ) ) { $interval = new DateInterval( $video_info['duration'] ); $duration_sec = $interval->h * 3600 + $interval->i * 60 + $interval->s; $time_format = ( $duration_sec >= 3600 ) ? 'H:i:s' : 'i:s'; $video['duration'] = gmdate( $time_format, $duration_sec ); }
// Video data $video['title'] = $result['items'][0]['snippet']['title']; $video['id'] = $video_id; $video['type'] = 'youtube';
return $video; }
/** * Get Vimeo Video data * * @param $vid * * @return null */ private static function get_vimeo_info( $video_id ) {
$video = array(); // Build the Api request $api_url = self::$vimeo_api_base . $video_id . '.json'; $request = wp_remote_get( $api_url );
// Check if there is no any errors if ( is_wp_error( $request ) ) { return null; }
// Prepare the data $result = json_decode( wp_remote_retrieve_body( $request ), true );
// Check if the video title is exists - if ( empty( $result[0]['title'] ) ) { return null; }
// Prepare the Video duration if ( ! empty( $result[0]['duration'] ) ) {
$duration_sec = $result[0]['duration']; $time_format = ( $duration_sec >= 3600 ) ? 'H:i:s' : 'i:s'; $video['duration'] = gmdate( $time_format, $duration_sec ); }
// Prepare the Video thumbnail if ( ! empty( $result[0]['thumbnail_small'] ) ) { $video_thumb = @parse_url( $result[0]['thumbnail_small'] ); $video_thumb = str_replace( '/video/', '', $video_thumb['path'] ); $video['thumb'] = $video_thumb; }
// Video data $video['title'] = $result[0]['title']; $video['id'] = $video_id; $video['type'] = 'vimeo';
return $video; }
} }
new Penci_Video_List;
|