Guest User

Untitled

a guest
Jul 16th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. register_shutdown_function(array($this, 'downloadVideomailToMediaLibrary'), $videomail);
  2.  
  3. public function downloadVideomailToMediaLibrary($videomail) {
  4. if ($videomail['webm']) {
  5. $videoUrl = $videomail['webm'];
  6. } else if ($videomail['mp4']) {
  7. $videoUrl = $videomail['mp4'];
  8. }
  9.  
  10. if ($videoUrl) {
  11. $tempFile = download_url($videoUrl, 300);
  12.  
  13. if (is_wp_error($tempFile)) {
  14. @unlink($tempFile);
  15. return $tempFile;
  16. } else {
  17. // Need to require these files
  18. if (!function_exists('media_handle_upload')) {
  19. require_once(ABSPATH . "wp-admin" . '/includes/image.php');
  20. require_once(ABSPATH . "wp-admin" . '/includes/file.php');
  21. require_once(ABSPATH . "wp-admin" . '/includes/media.php');
  22. }
  23.  
  24. $videoType = $videomail['recordingStats']['videoType'];
  25. $subject = 'some subject'
  26.  
  27. // Array based on $_FILE as seen in PHP file uploads
  28. $file = array(
  29. 'name' => $subject . '.' . $videoType,
  30. 'type' => wp_check_filetype($tempFile),
  31. 'tmp_name' => $tempFile,
  32. 'error' => 0,
  33. 'size' => filesize($tempFile),
  34. );
  35.  
  36. // Move the temporary file into the uploads directory
  37. // https://codex.wordpress.org/Function_Reference/media_handle_sideload
  38. $results = media_handle_sideload($file, 0, $subject);
  39.  
  40. // If error storing permanently, unlink
  41. if (is_wp_error($results)) {
  42. @unlink($tempFile);
  43. return $results;
  44. }
  45. }
  46. }
  47. }
Add Comment
Please, Sign In to add comment