File_Upload_Upgrader

Core class used for handling file uploads.

Defined (1)

The class is defined in the following location(s).

/wp-admin/includes/class-file-upload-upgrader.php  
  1. class File_Upload_Upgrader { 
  2.  
  3. /** 
  4. * The full path to the file package. 
  5. * @since 2.8.0 
  6. * @access public 
  7. * @var string $package 
  8. */ 
  9. public $package; 
  10.  
  11. /** 
  12. * The name of the file. 
  13. * @since 2.8.0 
  14. * @access public 
  15. * @var string $filename 
  16. */ 
  17. public $filename; 
  18.  
  19. /** 
  20. * The ID of the attachment post for this file. 
  21. * @since 3.3.0 
  22. * @access public 
  23. * @var int $id 
  24. */ 
  25. public $id = 0; 
  26.  
  27. /** 
  28. * Construct the upgrader for a form. 
  29. * @since 2.8.0 
  30. * @access public 
  31. * @param string $form The name of the form the file was uploaded from. 
  32. * @param string $urlholder The name of the `GET` parameter that holds the filename. 
  33. */ 
  34. public function __construct( $form, $urlholder ) { 
  35.  
  36. if ( empty($_FILES[$form]['name']) && empty($_GET[$urlholder]) ) 
  37. wp_die(__('Please select a file')); 
  38.  
  39. //Handle a newly uploaded file, Else assume it's already been uploaded 
  40. if ( ! empty($_FILES) ) { 
  41. $overrides = array( 'test_form' => false, 'test_type' => false ); 
  42. $file = wp_handle_upload( $_FILES[$form], $overrides ); 
  43.  
  44. if ( isset( $file['error'] ) ) 
  45. wp_die( $file['error'] ); 
  46.  
  47. $this->filename = $_FILES[$form]['name']; 
  48. $this->package = $file['file']; 
  49.  
  50. // Construct the object array 
  51. $object = array( 
  52. 'post_title' => $this->filename,  
  53. 'post_content' => $file['url'],  
  54. 'post_mime_type' => $file['type'],  
  55. 'guid' => $file['url'],  
  56. 'context' => 'upgrader',  
  57. 'post_status' => 'private' 
  58. ); 
  59.  
  60. // Save the data. 
  61. $this->id = wp_insert_attachment( $object, $file['file'] ); 
  62.  
  63. // Schedule a cleanup for 2 hours from now in case of failed install. 
  64. wp_schedule_single_event( time() + 2 * HOUR_IN_SECONDS, 'upgrader_scheduled_cleanup', array( $this->id ) ); 
  65.  
  66. } elseif ( is_numeric( $_GET[$urlholder] ) ) { 
  67. // Numeric Package = previously uploaded file, see above. 
  68. $this->id = (int) $_GET[$urlholder]; 
  69. $attachment = get_post( $this->id ); 
  70. if ( empty($attachment) ) 
  71. wp_die(__('Please select a file')); 
  72.  
  73. $this->filename = $attachment->post_title; 
  74. $this->package = get_attached_file( $attachment->ID ); 
  75. } else { 
  76. // Else, It's set to something, Back compat for plugins using the old (pre-3.3) File_Uploader handler. 
  77. if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) ) 
  78. wp_die( $uploads['error'] ); 
  79.  
  80. $this->filename = sanitize_file_name( $_GET[ $urlholder ] ); 
  81. $this->package = $uploads['basedir'] . '/' . $this->filename; 
  82.  
  83. if ( 0 !== strpos( realpath( $this->package ), realpath( $uploads['basedir'] ) ) ) { 
  84. wp_die( __( 'Please select a file' ) ); 
  85.  
  86. /** 
  87. * Delete the attachment/uploaded file. 
  88. * @since 3.2.2 
  89. * @access public 
  90. * @return bool Whether the cleanup was successful. 
  91. */ 
  92. public function cleanup() { 
  93. if ( $this->id ) 
  94. wp_delete_attachment( $this->id ); 
  95.  
  96. elseif ( file_exists( $this->package ) ) 
  97. return @unlink( $this->package ); 
  98.  
  99. return true;