選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

130 行
3.3 KiB

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