Make WordPress Core


Ignore:
Timestamp:
08/28/2011 08:42:07 AM (15 years ago)
Author:
dd32
Message:

Store Plugin/Theme uploads in the Media Library properly. Add Scheduled cleanup +2hrs to clean up any aborted installation attempts. See #18182

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/class-wp-upgrader.php

    r18616 r18617  
    14351435        var $package;
    14361436        var $filename;
     1437        var $id = 0;
    14371438
    14381439        function __construct($form, $urlholder) {
    1439                 if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
    1440                         wp_die($uploads['error']);
    14411440
    14421441                if ( empty($_FILES[$form]['name']) && empty($_GET[$urlholder]) )
    14431442                        wp_die(__('Please select a file'));
    14441443
    1445                 if ( !empty($_FILES) )
     1444                //Handle a newly uploaded file, Else assume its already been uploaded
     1445                if ( ! empty($_FILES) ) {
     1446                        $overrides = array( 'test_form' => false, 'test_type' => false );
     1447                        $file = wp_handle_upload( $_FILES[$form], $overrides );
     1448
     1449                        if ( isset( $file['error'] ) )
     1450                                wp_die( $file['error'] );
     1451
    14461452                        $this->filename = $_FILES[$form]['name'];
    1447                 else if ( isset($_GET[$urlholder]) )
     1453                        $this->package = $file['file'];
     1454
     1455                        // Construct the object array
     1456                        $object = array(
     1457                                'post_title' => $this->filename,
     1458                                'post_content' => $file['url'],
     1459                                'post_mime_type' => $file['type'],
     1460                                'guid' => $file['url'],
     1461                                'context' => 'upgrader',
     1462                                'post_status' => 'private'
     1463                        );
     1464
     1465                        // Save the data
     1466                        $this->id = wp_insert_attachment( $object, $file['file'] );
     1467
     1468                        // schedule a cleanup for 2 hours from now in case of failed install
     1469                        wp_schedule_single_event( time() + 7200, 'upgrader_scheduled_cleanup', array( $this->id ) );
     1470
     1471                } elseif ( is_numeric( $_GET[$urlholder] ) ) {
     1472                        // Numeric Package = previously uploaded file, see above.
     1473                        $this->id = (int) $_GET[$urlholder];
     1474                        $attachment = get_post( $this->id );
     1475                        if ( empty($attachment) )
     1476                                wp_die(__('Please select a file'));     
     1477
     1478                        $this->filename = $attachment->post_title;
     1479                        $this->package = get_attached_file( $attachment->ID );
     1480                } else {
     1481                        // Else, It's set to something, Back compat for plugins using the old (pre-3.3) File_Uploader handler.
     1482                        if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
     1483                                wp_die( $uploads['error'] );
     1484
    14481485                        $this->filename = $_GET[$urlholder];
    1449 
    1450                 //Handle a newly uploaded file, Else assume its already been uploaded
    1451                 if ( !empty($_FILES) ) {
    1452                         $this->filename = wp_unique_filename( $uploads['basedir'], $this->filename );
    14531486                        $this->package = $uploads['basedir'] . '/' . $this->filename;
    1454 
    1455                         // Move the file to the uploads dir
    1456                         if ( false === @ move_uploaded_file( $_FILES[$form]['tmp_name'], $this->package) )
    1457                                 wp_die( sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path']));
    1458                 } else {
    1459                         $this->package = $uploads['basedir'] . '/' . $this->filename;
    14601487                }
    14611488        }
    14621489
    14631490        function cleanup() {
    1464                 if ( file_exists($this->package) )
    1465                         return @unlink($this->package);
     1491                if ( $this->id )
     1492                        wp_delete_attachment( $this->id );
     1493
     1494                elseif ( file_exists( $this->package ) )
     1495                        return @unlink( $this->package );
     1496
    14661497                return true;
    14671498        }
Note: See TracChangeset for help on using the changeset viewer.