Plugin Directory

Changeset 3071479


Ignore:
Timestamp:
04/16/2024 10:18:33 AM (22 months ago)
Author:
pauladamdavis
Message:

Delete prior exports and change how new exports are generated - Credit to Joshua Chan https://patchstack.com/database/researcher/7d630755-51bd-4c8b-8d77-a700aea26d1d

Location:
ghost/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • ghost/trunk/class-ghost.php

    r2995134 r3071479  
    2626     * @var  string
    2727     */
    28     protected $version = '1.4.0';
     28    protected $version = '1.5.0';
    2929
    3030    /**
     
    6262    protected $date_format = 'Y-m-d\TH:i:sP';
    6363    protected $ghost_image_base = 'content/images/wordpress';
     64    protected $json_file_name = 'wp_ghost_export.json';
     65    protected $zip_file_name = 'wp_ghost_export.zip';
    6466
    6567    /**
     
    7880        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) );
    7981        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
     82
     83        // Init deleting if old exports
     84        add_action( 'init', array( $this, 'deleteLocalExportFiles' ) );
    8085
    8186        if ( isset( $_GET['ghostexport'] ) ) {
     
    144149    public static function deactivate( $network_wide ) {
    145150        // TODO: Define deactivation functionality here
     151    }
     152   
     153    /**
     154     * Deleted locally saved export files, which prior versions saved in the uploads directory.
     155     *
     156     * @since 1.5.0
     157     */
     158    function deleteLocalExportFiles() {
     159        // Get the files in the directory, ignoring the . and ..
     160        $upload_dir = wp_upload_dir();
     161        $gfiledir = $upload_dir['basedir'] . '/ghost-exports';
     162
     163        // Return if directory does not exist
     164        if ( ! is_dir($gfiledir) ) {
     165            return;
     166        }
     167
     168        $filesInDir = scandir($gfiledir);
     169        $slicesFilesInDir = array_slice($filesInDir);
     170
     171        // Delete each file
     172        foreach ($slicesFilesInDir as $file) {
     173            unlink($gfiledir . '/' . $file);
     174        }
     175
     176        // And delete the directory
     177        rmdir($gfiledir);
    146178    }
    147179
     
    600632        $upload_dir = wp_upload_dir();
    601633        $filedir = $upload_dir['basedir'];
    602         $gfiledir = $upload_dir['basedir'] . '/ghost-exports';
    603         if ( ! file_exists( $gfiledir ) ) {
    604             wp_mkdir_p( $gfiledir );
    605         }
    606         $filename = 'wp_ghost_export.json';
    607 
    608         if ( ! is_writable( $gfiledir ) ) {
    609             wp_die( "<p>Uploads directory is not writable, can't save the Ghost json file :/</p><p>Generated by the Ghost plugin version {$this->version}.</p>", 'Directory not writable' );
    610         }
    611 
    612         $handle = fopen( $gfiledir . '/' . $filename, 'w' );
     634
    613635        $content = $this->get_json( $this->garray );
    614636
    615637        // Remove extra backslashes and quotes added due to double encoding and replace WordPress uploads URL with Ghost relative URL.
    616638        $cleaned_content = $this->clean_content_json($content);
    617 
    618         fwrite( $handle, $cleaned_content );
    619         fclose( $handle );
    620 
    621         // Generate a zip archive of images in a Ghost compatible directory structure as well as the JSON file.
    622639
    623640        // Ensure ZipArchive is installed.
     
    627644
    628645        // Initialise the archive object
    629         $gziparchivename = 'wp_ghost_export.zip';
    630646        $gziparchive = new ZipArchive();
    631         $gziparchive->open( $gfiledir . '/' . $gziparchivename, ZipArchive::CREATE | ZipArchive::OVERWRITE );
     647
     648        $tmp_file = tmpfile();
     649        $tmp_location = stream_get_meta_data($tmp_file)['uri'];
     650
     651        $res = $gziparchive->open($tmp_location, ZipArchive::CREATE);
    632652
    633653        // Create recursive directory iterator
     
    639659
    640660        // Set which files to exclude from being zipped.
    641         $gexclusions = array($gziparchivename);
     661        $gexclusions = array( $this->zip_file_name );
    642662        // Set which extensions to include.
    643         $gincludedextensions = array( "jpg", "jpeg", "gif", "png", "svg", "svgz", "ico" );
     663        $gincludedextensions = array( 'jpg', 'jpeg', 'gif', 'png', 'svg', 'svgz', 'ico', 'webp' );
    644664
    645665        foreach ( $files as $name => $file ) {
     
    653673                // Add current file to archive in dedicated WordPress folder within a Ghost compatible directory structure.
    654674                $gziparchive->addFile( $filePath, $this->ghost_image_base . '/' . $relativePath );
    655             } elseif ( $file->getFilename() == $filename ) {
    656                 // Get real and relative path for current file
    657                 $filePath = $file->getRealPath();
    658                 $relativePath = substr( $filePath, strlen($gfiledir) + 1 );
    659                 // Add current file to archive in dedicated WordPress folder within a Ghost compatible directory structure.
    660                 $gziparchive->addFile( $filePath, 'json/' . $relativePath );
    661675            }
    662676        }
    663677
    664         // Zip archive will be created only after closing object
     678        $gziparchive->addFromString( 'json/' . $this->json_file_name, $cleaned_content );
     679
    665680        $gziparchive->close();
    666681
    667         header( 'Content-Description: File Transfer' );
    668         header( 'Content-Type: application/octet-stream' );
    669         header( 'Content-Disposition: attachment; filename=' . $gziparchivename );
    670         header( 'Content-Transfer-Encoding: binary' );
    671         header( 'Expires: 0' );
    672         header( 'Cache-Control: must-revalidate' );
    673         header( 'Pragma: public' );
    674         header( 'Content-Length: ' . filesize( $gfiledir . '/' . $gziparchivename ) );
    675 
    676         flush();
    677 
    678         readfile( $gfiledir . '/' . $gziparchivename );
     682        header( 'Content-type: application/octet-stream' );
     683        header( 'Content-Disposition: attachment; filename=' . $this->zip_file_name );
     684
     685        echo( readfile( $tmp_location ) );
     686
    679687        exit;
    680688    }
     
    692700        $this->populate_data();
    693701
    694         $upload_dir = wp_upload_dir();
    695         $filedir = $upload_dir['basedir'];
    696         $gfiledir = $upload_dir['basedir'] . '/ghost-exports';
    697         if ( ! file_exists( $gfiledir ) ) {
    698             wp_mkdir_p( $gfiledir );
    699         }
    700         $filename = 'wp_ghost_export.json';
    701 
    702         if ( ! is_writable( $gfiledir ) ) {
    703             wp_die( "<p>Uploads directory is not writable, can't save the Ghost json file :/</p><p>Generated by the Ghost plugin version {$this->version}.</p>", 'Directory not writable' );
    704         }
    705 
    706         $handle = fopen( $gfiledir . '/' . $filename, 'w' );
    707702        $content = $this->get_json( $this->garray );
    708703
    709704        // Remove extra backslashes and quotes added due to double encoding and replace WordPress uploads URL with Ghost relative URL.
    710         $cleaned_content = $this->clean_content_json($content);
    711 
    712         fwrite( $handle, $cleaned_content );
    713         fclose( $handle );
    714 
    715         header( 'Content-Description: File Transfer' );
    716         header( 'Content-Type: application/octet-stream' );
    717         header( 'Content-Disposition: attachment; filename='.$filename );
    718         header( 'Content-Transfer-Encoding: binary' );
    719         header( 'Expires: 0' );
    720         header( 'Cache-Control: must-revalidate' );
    721         header( 'Pragma: public' );
    722         header( 'Content-Length: ' . filesize( $gfiledir . '/' . $filename ) );
    723 
    724         flush();
    725 
    726         readfile( $gfiledir . '/' . $filename );
     705        $cleaned_content = $this->clean_content_json( $content );
     706
     707        header ( 'Content-Type: application/octet-stream' );
     708        header ( 'Content-disposition: attachment; filename=' . $this->json_file_name );
     709        echo $cleaned_content;
    727710
    728711        exit;
  • ghost/trunk/ghost.php

    r2995134 r3071479  
    1313 * Plugin URI:  http://ghost.org
    1414 * Description: Plugin to export your WordPress blog so you can import it into your Ghost installation
    15  * Version:     1.4.0
     15 * Version:     1.5.0
    1616 * Author:      Ghost Foundation
    1717 * Author URI:  http://ghost.org
  • ghost/trunk/readme.txt

    r2995134 r3071479  
    44Tags: ghost, export, migrate, blogging, publishing
    55Requires at least: 4.2.0
    6 Tested up to: 6.4.1
    7 Stable tag: 1.4.0
     6Tested up to: 6.5
     7Stable tag: 1.5.0
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    6161
    6262== Changelog ==
     63
     64= 1.5.0 =
     65
     66* Delete prior exports and change how new exports are generated - Credit to [Joshua Chan](https://patchstack.com/database/researcher/7d630755-51bd-4c8b-8d77-a700aea26d1d)
     67* Test & ensure compatibility with WorePress 6.5
    6368
    6469= 1.4.0 =
Note: See TracChangeset for help on using the changeset viewer.