class-file-upload-upgrader.php 3.3 KB

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