source: trunk/www.guidonia.net/wp/wp-content/plugins/webtv/webtv_Actions.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 15 years ago
File size: 18.8 KB
Line 
1<?php
2
3/*
4Author: Edgar de Le&oacute;n - edeleon
5Author URI: http://www.webstratega.com/about/
6Description: Lets you attach a video to a post and upload to most popular video distribution sites like YouTube, Vimeo and Blip.tv, after video is uploaded and processed get and inserts the embed code from the sites into custom fields on the post. The plugin has de possibility to extend any other distribution site creating extra drivers
7License: GPL (http://www.fsf.org/licensing/licenses/info/GPLv2.html)
8*/
9
10function webtv_status_box() {
11 if( function_exists( 'add_meta_box' )) {
12 add_meta_box('webtv-status-info','WebTV Status','webtv_display_status','post','normal','high');
13 }
14}
15
16
17
18function webtv_getembed_video() {
19 $webtvembedqueue = (array)get_option( 'webtv_embed_queue' );
20 if (empty($webtvembedqueue)) return;
21 $reschedule = false;
22 foreach ($webtvembedqueue as $service => $ids) {
23 //Load Settings
24 $settings = (array)get_option("webtv_".$service);
25 foreach ($ids as $post_id) {
26 //GetEmbedCode
27 $resp = webtv_getembed_code($post_id,$service,$settings);
28 if ($resp) unset($ids[$post_id]);
29 }
30 $webtvembedqueue[$service] = $ids;
31 if (!empty($ids)) $reschedule = true;
32 }
33 update_option( 'webtv_embed_queue', $webtvembedqueue );
34 //Si existen elementos en la cola de getEmbed programa el cron para dentro de 5 minutos
35 if ($reschedule) {
36 wp_clear_scheduled_hook( 'webtv_getembed' );
37 wp_schedule_single_event(time()+300, 'webtv_getembed');
38 }
39}
40
41function webtv_getembed_code($post_id,$service,$settings) {
42 $poststatus = (array)get_post_meta($post_id, '_webtv_status', true);
43 $uploadstatus = $poststatus[$service]['upload'];
44 $embedstatus = $poststatus[$service]['embed'];
45 $videodata = $poststatus[$service]['data'];
46 if (function_exists('getembed_'.$service)) {
47 $response = call_user_func('getembed_'.$service,$settings,$videodata);
48 if ($response['retry'] == 0) {
49 $return = true; //sacarlo de la cola
50 } else {
51 $return = false; //intentar de nuevo publicar
52 }
53 if(empty($response)) {
54 $embedstatus['status'] = 'error';
55 $embedstatus['status_msg'] = _("No response from $service");
56 }
57 if($response['error']) {
58 $embedstatus['status'] = 'error';
59 $embedstatus['status_msg'] = $response['error_description'] . " " . $response['status_description'];
60 $uploadstatus['status_msg'] = $response['error_description'] . " " . $response['status_description'];
61 } else {
62 $embedstatus['status'] = 'queued';
63 $embedstatus['status_msg'] = $response['error_description'] . " " . $response['status_description'];
64 }
65 if(!empty($response['embed'])) {
66 $embedstatus['status'] = 'completed';
67 $embedstatus['status_msg'] = _("check custom fields");
68 $uploadstatus['status_msg'] = _("completed");
69 //unset basic data
70 unset($response['service']);
71 unset($response['status']);
72 unset($response['status_description']);
73 unset($response['error']);
74 unset($response['error_description']);
75 unset($response['retry']);
76 //Insert into custom fields
77 foreach ($response as $customfield => $data) {
78 add_post_meta($post_id,'webtv_'.$service.'_'.$customfield,$data,true);
79 $videodata[$customfield] = $data;
80 }
81 //Autopublish post if enabled
82 $postoptions = get_post_meta($post_id, '_webtv_post_options',true);
83 $autopublish = false;
84 if (!empty($postoptions)) {
85 $globalopts = $postoptions['global'];
86 if ((isset($globalopts['autopost'])) && ($globalopts['autopost'] == 'on'))
87 $autopublish = true;
88 }
89 if (($autopublish) && (get_post_status($post_id) === 'draft')) {
90 wp_publish_post($post_id);
91 }
92 }
93 $poststatus[$service]['upload'] = $uploadstatus;
94 $poststatus[$service]['embed'] = $embedstatus;
95 $poststatus[$service]['data'] = $videodata;
96 add_post_meta($post_id, '_webtv_status', $poststatus, true) or
97 update_post_meta($post_id, '_webtv_status', $poststatus);
98 return $return;
99 }
100 return true; //Sacarlo de la cola
101}
102
103function webtv_publish_video($entry,$service,$settings,$options) {
104 $post_id = $entry['post_id'];
105 $publish = false;
106 if (isset($settings['enabled']) && ($settings['enabled'] == 1)) {
107 $publish = true;
108 }
109 if($publish) {
110 unset($settings['enabled']);
111 $poststatus = (array)get_post_meta($post_id, '_webtv_status', true);
112 $driver = $poststatus[$service]['upload'];
113 if ($driver['status'] == 'error') return true; //sacarlo de la cola
114 $driver['count']++;
115 if ($driver['count'] > $options['attemps']) {
116 $driver['status'] = 'error';
117 $driver['status_msg'] = sprintf(_('Impossible to upload video on %d attemps'),$options['attemps']);
118 $poststatus[$service]['upload'] = $driver;
119 add_post_meta($post_id, '_webtv_status', $poststatus, true) or
120 update_post_meta($post_id, '_webtv_status', $poststatus);
121 return true; //sacarlo de la cola
122 }
123 if (function_exists('upload_'.$service)) {
124 $response = call_user_func('upload_'.$service,$settings,$entry);
125 if ($response['retry'] == 0) {
126 $return = true; //sacarlo de la cola
127 } else {
128 $return = false; //intentar de nuevo publicar
129 }
130 if(empty($response)) {
131 $driver['status'] = 'error';
132 $driver['status_msg'] = _("No response from $service");
133 }
134 if($response['error']) {
135 $driver['status'] = 'error';
136 $driver['status_msg'] = $response['error_description'];
137 }
138 if(!empty($response['video_id'])) {
139 $driver['status'] = 'completed';
140 $driver['status_msg'] = $response['status'] . " " . $response['status_description'];
141 $poststatus[$service]['embed'] = array( 'status' => 'queued',
142 'status_msg' => 'scheduled to get the embed code');
143 //unset basic data
144 unset($response['service']);
145 unset($response['status']);
146 unset($response['status_description']);
147 unset($response['error']);
148 unset($response['error_description']);
149 unset($response['retry']);
150 //Insert into custom fields
151 foreach ($response as $customfield => $data) {
152 add_post_meta($post_id,'webtv_'.$service.'_'.$customfield,$data,true);
153 $poststatus[$service]['data'][$customfield] = $data;
154 }
155 //Insert into GetEmbed queue here if some upload gives a timeout, just to be sure
156 $webtvembedqueue = (array)get_option( 'webtv_embed_queue' );
157 if (empty($webtvembedqueue)) add_option('webtv_embed_queue', array());
158 $post_ids = (array)$webtvembedqueue[$service];
159 if( !array_key_exists( $post_id, $post_ids ) ) {
160 $post_ids[ $post_id ] = $post_id;
161 $webtvembedqueue[$service] = $post_ids;
162 }
163 update_option( 'webtv_embed_queue', $webtvembedqueue );
164 }
165 $poststatus[$service]['upload'] = $driver;
166 add_post_meta($post_id, '_webtv_status', $poststatus, true) or
167 update_post_meta($post_id, '_webtv_status', $poststatus);
168 return $return;
169 }
170 }
171 $poststatus = (array)get_post_meta($post_id, '_webtv_status', true);
172 $driver = $poststatus[$service]['upload'];
173 $driver['status'] = 'error';
174 $driver['status_msg'] = _("Please check webtv settings");
175 $poststatus[$service]['upload'] = $driver;
176 add_post_meta($post_id, '_webtv_status', $poststatus, true) or
177 update_post_meta($post_id, '_webtv_status', $poststatus);
178 return true; //sacarlo de la cola
179}
180
181function webtv_upload_video() {
182 $webtvqueue = (array)get_option( 'webtv_upload_queue' );
183 $reschedule = false;
184 if (!empty($webtvqueue)) {
185 $options = get_option("webtv");
186 foreach ($webtvqueue as $service => $ids) {
187 //Load Settings
188 $settings = (array)get_option("webtv_".$service);
189 foreach ($ids as $post_id) {
190 //Get Post info
191 $post = wp_get_single_post($post_id);
192 $entry['post_id'] = $post_id;
193 $entry['titulo'] = $post->post_title;
194 $entry['desc'] = $post->post_content;
195 $entry['tags'] = implode (',', $post->tags_input);
196 //Get File info
197 $fdetails = (array)get_post_meta($post_id,"_webtv_file_details",true);
198 $entry['fileinfo'] = $fdetails['full_path'];
199 $entry['content_type'] = $fdetails['content_type'];
200 $entry['filesize'] = $fdetails['size'];
201 //Upload file
202 $resp = webtv_publish_video($entry,$service,$settings,$options);
203 if ($resp) unset($ids[$post_id]); //descomentar al terminar pruebas
204 }
205 $webtvqueue[$service] = $ids;
206 if (!empty($ids)) $reschedule = true;
207 }
208 if (isset($webtvqueue[0])) unset($webtvqueue[0]);
209 update_option( 'webtv_upload_queue', $webtvqueue );
210 }
211 //Si existen elementos en la cola de subida programa el cron de subida para dentro de 5 minutos
212 if ($reschedule) {
213 wp_clear_scheduled_hook( 'webtv_upload' );
214 wp_schedule_single_event(time()+300, 'webtv_upload');
215 }
216 //Se programa el cron para obtener el embed code de los videos
217 if( !wp_next_scheduled( 'webtv_getembed' ) ) {
218 wp_clear_scheduled_hook( 'webtv_getembed' );
219 wp_schedule_single_event(time(), 'webtv_getembed');
220 }
221}
222
223function webtv_display_status() {
224 global $post_ID, $temp_ID;
225 $post_id = (int) (0 == $post_ID ? $temp_ID : $post_ID);
226
227 /*$url = get_settings('siteurl');
228 $url = 'http://webtv.webstratega.com/wp-content/plugins/'.$plugin = dirname(plugin_basename(__FILE__)).'/includes/';
229 $uploaddir = '/'.get_option( 'upload_path' );*/
230 //$uploaddir = '/'.get_option( 'upload_path' ).'/webtv'; //A decidir si guardar dentro de subdir
231 if ($post_id < 1) {
232 $message = _("You need to Save Draft before upload a video");
233 echo '<div id="draft">'.$message.'</div>';
234
235 } else {
236?>
237 <div id="uploaddiv" style="border: 1px solid rgb(127, 170, 255); padding: 2px; display: inline; background-color: rgb(197, 217, 255);">
238 <?php
239 $status = get_post_meta($post_id,"_webtv_upload_status",true);
240 if ((!empty($status) && $status == 'error') || (empty($status))) {
241 ?>
242 <span id="spanButtonPlaceHolder"><a href="#webtv-status-info" onclick="load_swfupload();" style="font-family: Helvetica, Arial, sans-serif; font-size: 10pt;">Upload Video</a></span>
243 <?php
244 } else {
245 echo _("Video Uploaded");
246 }
247 ?>
248 </div>
249 <br/>
250 <span>
251 <?php
252 $postoptions = get_post_meta($post_id, '_webtv_post_options',true);
253 $checked = 'checked="checked"';
254 if (!empty($postoptions)) {
255 $globalopts = $postoptions['global'];
256 if ((isset($globalopts['autopost'])) && ($globalopts['autopost'] == 'off'))
257 $checked = '';
258 }
259 ?>
260 <br/><input type="checkbox" name="autopost" id="autopost" <?=$checked?> /> <?php echo _("Automatically publish post after one successful upload?") ?>
261 </span>
262 <br/><br/>
263 <span><b><?php echo _("File Details")?>:</b></span><br/>
264 <div id="webtv-status-upload"></div>
265 <!--<div style="border: 1px solid rgb(127, 170, 255); padding: 2px; display: inline; background-color: rgb(197, 217, 255);">
266 <a href="#" id="btnCancel" onclick="swfu.cancelQueue();" disabled="disabled" >Cancel</a>
267 </div>
268 -->
269 <div id="eleList">
270 <?php
271
272
273 if (!empty($status)) {
274 switch ($status) {
275 case "error": $msg = get_post_meta($post_id,"_webtv_upload_status_msg",true);
276 $status_color = "#FF0000;";
277 break;
278 case "stored":
279 case "uploaded":$fdetails = get_post_meta($post_id,"_webtv_file_details",true);
280 $msg = $fdetails['local_file'].' '.$fdetails['content_type'].' '.file_size($fdetails['size']);
281 $status_color = "#009015;";
282 break;
283 }
284 echo '<span style="color: #6F6F6F;"> '.$msg.' </span><span style="color: '.$status_color.'">'.$status.'</span><br/>';
285 }
286 $poststatus = get_post_meta($post_id, '_webtv_post_status',true);
287 if (!empty($poststatus)) {
288 $status_color = "#FF0000;";
289 echo '<span style="color: #6F6F6F;"> '.$poststatus.'</span><span style="color: '.$status_color.'">error </span><br/>';
290 }
291 /*if ($programado = wp_next_scheduled( 'webtv_upload' )) {
292 $webtvqueue = get_option( 'webtv_upload_queue' );
293 ?>
294 <span style="color: #009015;"><?php echo _("scheduled ");?></span><span style="color: #6F6F6F;"><?php echo _("Next upload: "); echo date('d-m-Y h:i',$programado); ?></span>
295 <?php
296 $services = array();
297 if (!empty($webtvqueue)) {
298 foreach ($webtvqueue as $service => $ids) {
299 if (array_key_exists($post_id, $ids)) {
300 $services[] = $service;
301 }
302 }
303 }
304 ?><span style="color: #6F6F6F;"><?php if (!empty($services)) { echo _("to "); echo implode(",",$services); } ?></span><br/><?php
305 }*/
306 $poststatus = get_post_meta($post_id, '_webtv_status', true);
307 if (!empty($poststatus)) {
308 foreach ($poststatus as $driver => $type) {
309 ?><br/>
310 <span><b><?php echo ucfirst($driver) . " Status:"?></b></span><br/><?
311 foreach ($type as $process => $data) {
312 if ($process != 'data') {
313 ?>
314 <span><?=ucfirst($process)?></span>
315
316 <span style="color: #6F6F6F;"><?=$data['status_msg']?>
317 <?php
318 $queuename = ($process == 'upload') ? "webtv_upload" : "webtv_getembed";
319 if (($data['status'] == 'queued') && (wp_next_scheduled( $queuename )) ) {
320 echo _(" at ");
321 echo date('d-m-Y H:i',wp_next_scheduled( $queuename ));
322 }
323 echo "</span>";
324 $style_color = ($data['status'] == 'error') ? "#FF0000" : "#009015"; ?>
325 <span style="color: <?=$style_color?>;"><?=$data['status']?></span>
326 <?php
327 echo "<br/>";
328 } else {
329 ?>
330 <!--<span style="color: #6F6F6F;">Info</span><br/>-->
331 <span>VideoID</span>
332 <?php
333 if (isset($data['video_id'])) {
334 echo '<span style="color: #6F6F6F;">';
335 if (isset($data['watch_url'])) {
336 ?>
337 <a href="<?=$data['watch_url']?>" target="_blank"><?=$data['video_id']?></a>
338 <?php
339 } else {
340 echo $data['video_id'];
341 }
342 echo '</span><br/>';
343 }
344 echo '<span>CustomFields:</span><br/>';
345 foreach ($data as $customfield => $value) {
346 $message = sprintf(_("at %s custom field"),"webtv_".$driver."_".$customfield);
347 echo '<span style="color: #6F6F6F;">'.$customfield.' </span><span style="color: #6F6F6F;">'.$message.'</span><span style="color: #009015;"> done</span><br/>';
348 }
349 }
350 }
351 }
352 }
353 ?>
354
355 </div>
356 <?php $debug = false;
357 if ($debug) {
358 ?>
359 <div id="debug">
360 <p>Debug</p>
361 <?php
362 print "post_id ".$post_id;
363 echo "<BR/>";
364 $options = get_option("webtv");
365 foreach ($options['services'] as $driver) {
366 print_r($driver);
367 echo "<BR/>";
368 $settings = get_option("webtv_".$driver);
369 print_r($settings);
370 echo "<BR/>";
371 if ($settings['enabled']) {
372 print "enabled";
373 echo "<BR/>";
374 }
375 }
376 echo "<BR/>Upload Queue<BR/>";
377 $webtvqueue = get_option( 'webtv_upload_queue' );
378 $webtvembedqueue = get_option( 'webtv_embed_queue' );
379 //update_option( 'webtv_upload_queue',array());
380 //update_option( 'webtv_embed_queue',array());
381 //if (isset($webtvqueue[0])) unset($webtvqueue[0]);
382 //delete_post_meta($post_id, '_webtv_status');
383 print_r($webtvqueue);
384 echo "<BR/>Getembed Queue<BR/>";
385 print_r($webtvembedqueue);
386 echo "<BR/>";
387 /*$settings = get_option("webtv_youtube");
388 print_r($settings);*/
389 echo "<BR/>";
390 print "next upload cron ";
391 print date('Ymd h i',wp_next_scheduled( 'webtv_upload' ));
392 print date('Ymd h i',wp_next_scheduled( 'webtv_getembed' ));
393 echo "<BR/>";
394 print "Post Status <BR/> ";
395 print_r($poststatus = (array)get_post_meta($post_id, '_webtv_status', true));
396 ?>
397 </div>
398 <?php } ?>
399
400<?php
401 }
402}
403
404function webtv_save_and_schedule() {
405 global $post_ID, $temp_ID;
406 $post_id = (int) (0 == $post_ID ? $temp_ID : $post_ID);
407 //Check post options
408 $autopost = 'off';
409 if (isset($_POST['autopost']))
410 $autopost = 'on';
411 $postopts['global'] = array('autopost' => $autopost);
412 add_post_meta($post_id, '_webtv_post_options', $postopts, true) or
413 update_post_meta($post_id, '_webtv_post_options', $postopts);
414 //The _webtv_file_details and _webtv_upload_status custom fields are created on upload.php file using post data from swfupload
415 $status = get_post_meta($post_id,"_webtv_upload_status",true);
416 if ($status === "uploaded") {
417 //Get Post info
418 $post = wp_get_single_post($post_id);
419 if ( $post == false || $post == null ) {
420 return false;
421 }
422 $error_msg = "";
423 //print_r($post);
424 if (($post->post_type) != 'post') {
425 $error_msg .= _("This is not a post ");
426 $error = true;
427 }
428 if (empty($post->post_title)) {
429 $error_msg .= _(" Must have a post title ");
430 $error = true;
431 }
432 if (empty($post->post_content)) {
433 $error_msg .= _(" Must have post content ");
434 $error = true;
435 }
436 if (empty($post->tags_input)) {
437 $error_msg .= _(" Must have at least one tag ");
438 $error = true;
439 }
440 if ($error) {
441 add_post_meta($post_id, '_webtv_post_status', $error_msg, true) or
442 update_post_meta($post_id, '_webtv_post_status', $error_msg);
443 return false;
444 }
445 delete_post_meta($post_id, '_webtv_post_status');
446 $options = get_option("webtv");
447 $webtvqueue = (array)get_option( 'webtv_upload_queue' );
448 foreach ($options['services'] as $driver) {
449 $settings = get_option("webtv_".$driver);
450 if ($settings['enabled']) {
451 $poststatus[$driver]['upload'] = array( 'count' => 0,
452 'status' => 'queued',
453 'status_msg' => 'scheduled to upload');
454 add_post_meta($post_id, '_webtv_status', $poststatus, true) or
455 update_post_meta($post_id, '_webtv_status', $poststatus);
456 $post_ids = (array)$webtvqueue[$driver];
457 if( !array_key_exists( $post_id, $post_ids ) ) {
458 $post_ids[ $post_id ] = $post_id;
459 $webtvqueue[$driver] = $post_ids;
460 }
461 }
462 }
463 if (isset($webtvqueue[0])) unset($webtvqueue[0]);
464 update_option( 'webtv_upload_queue', $webtvqueue );
465 add_post_meta($post_id, '_webtv_upload_status', 'stored', true) or
466 update_post_meta($post_id, '_webtv_upload_status', 'stored');
467 } //$status === "uploaded"
468 //Iniciar el cron job
469 if( !wp_next_scheduled( 'webtv_upload' ) ) {
470 wp_clear_scheduled_hook( 'webtv_upload' );
471 wp_schedule_single_event(time(), 'webtv_upload');
472 }
473}
474
475function file_size($size)
476{
477 $filesizename = array("Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
478 return $size ? round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $filesizename[$i] : '0Bytes';
479}
480
481function webtv_content_type($filename) {
482 $pathinfo = pathinfo($filename);
483 $ext = strtolower($pathinfo['extension']);
484
485 $valid_video_format['mpeg'] = 'video/mpeg';
486 $valid_video_format['mpg'] = 'video/mpeg';
487 $valid_video_format['mpe'] = 'video/mpeg';
488 $valid_video_format['qt'] = 'video/quicktime';
489 $valid_video_format['mov'] = 'video/quicktime';
490 $valid_video_format['avi'] = 'video/x-msvideo';
491 $valid_video_format['wmv'] = 'video/x-ms-wmv';
492 $valid_video_format['3gp'] = 'video/3gpp';
493 $valid_video_format['m4v'] = 'video/x-m4v';
494 $valid_video_format['flv'] = 'video/x-flv';
495 $valid_video_format['mp4'] = 'video/mp4';
496 $valid_video_format['f4v'] = 'video/mp4';
497
498 return $valid_video_format[$ext];
499
500}
501
502/* Template Tags */
503function webtv_embedcode() {
504 global $post;
505 if (!isset($post->ID) || $post->ID == 0 || $post->ID == "") {
506 return;
507 } else {
508 $post_id = $post->ID;
509 }
510 $options = get_option("webtv");
511 $embed = '';
512 $orderlist = $options['order'];
513 foreach ($orderlist as $order => $service) {
514 $embed = get_post_meta($post_id,'webtv_'.$service.'_embed',true);
515 if (!empty($embed)) {
516 echo $embed;
517 return;
518 }
519 }
520 echo '';
521 return;
522}
523
524
525?>
Note: See TracBrowser for help on using the repository browser.