Plugin Directory

Changeset 3407018


Ignore:
Timestamp:
12/01/2025 04:13:32 PM (3 months ago)
Author:
littlepackage
Message:

update to version 4.0.6

Location:
waterwoo-pdf/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • waterwoo-pdf/trunk/classes/wwpdf-file-handler.php

    r3400576 r3407018  
    1616     */
    1717    protected $email = '';
     18
     19    /**
     20     * @var string
     21     */
     22    protected $temp_folder = '';
    1823
    1924    /**
     
    3136        if ( isset( $_GET['eddfile'] ) || isset( $_GET['edd-all-access-download'] ) ) {
    3237            add_filter( 'edd_requested_file',                   [ $this, 'dispatch_edd' ], 15, 4 );
    33         }
    34 
    35         if ( apply_filters( 'wwpdf_do_cleanup', true ) ) {
    36             $this->do_cleanup();
    3738        }
    3839
     
    5051     * @return void|string $file_path
    5152     */
    52     public function dispatch_woo( $file_path, $email, $order, $product, $download ) {
     53    public function dispatch_woo( string $file_path, string $email, $order, $product, $download ) {
    5354
    5455        if ( apply_filters( 'wwpdf_abort_watermarking', false, $file_path, $email, $order, $product, $download ) ) {
     
    9394     * @return string
    9495     */
    95     public function dispatch_dlm( $file_path, $remote_file, $download ) {
     96    public function dispatch_dlm( string $file_path, bool $remote_file, object $download ) {
    9697
    9798        // Sorry, the free version of PDF Ink (pdfink.com) doesn't handle remote PDF files
     
    147148     * @return string
    148149     */
    149     public function dispatch_edd( $file_path, $download_files, $file_key, $args ) {
     150    public function dispatch_edd( string $file_path, $download_files, $file_key, $args ) {
    150151
    151152        if ( empty( $file_path ) ) {
     
    186187     * @return mixed|void
    187188     */
    188     protected function dispatch( $source, $file_path, $order_id, $product_id ) {
     189    protected function dispatch( string $source, string $file_path, $order_id, $product_id ) {
    189190
    190191        // Check if it's a PDF
     
    244245     * @deprecated in PDF Watermark v4.0
    245246     */
    246     public function pdf_filepath( $file_path, $email, $order, $product, $download ) {
     247    public function pdf_filepath( string $file_path, string $email, object $order, object $product, $download ) {
    247248        // Sorry guys
    248249    }
     
    253254     * @return string
    254255     */
    255     protected function get_requested_filename( $file_path ) {
     256    protected function get_requested_filename( string $file_path ) {
    256257
    257258        $name = basename( $file_path );
     
    270271     * @return boolean|string $content
    271272     */
    272     public function get_content( $source, $order_id, $product_id ) {
     273    public function get_content( string $source, $order_id, $product_id ) {
    273274
    274275        $email = '';
     
    456457
    457458        if ( ! empty( $order_id ) ) {
    458             $watermarked_path = PDFINK_LITE_UPLOADS_PATH . $source . DIRECTORY_SEPARATOR . $order_id;
     459            $temp_folder = PDFINK_LITE_UPLOADS_PATH . $source . DIRECTORY_SEPARATOR . $order_id;
    459460        } else {
    460             $watermarked_path = PDFINK_LITE_UPLOADS_PATH . $source . DIRECTORY_SEPARATOR . date( 'Y' ) . DIRECTORY_SEPARATOR . date( 'm' ) . DIRECTORY_SEPARATOR . date( 'd' ) . $order_id;
    461         }
    462 
    463         if ( ! wp_mkdir_p( $watermarked_path ) || ! is_writable( $watermarked_path ) ) {
    464             throw new Exception( __( 'The PDF destination folder, ' . $watermarked_path .' is not writable.', 'waterwoo-pdf' ) );
    465         }
     461            $temp_folder = PDFINK_LITE_UPLOADS_PATH . $source . DIRECTORY_SEPARATOR . date( 'Y' ) . DIRECTORY_SEPARATOR . date( 'm' ) . DIRECTORY_SEPARATOR . date( 'd' ) . $order_id;
     462        }
     463
     464        if ( ! wp_mkdir_p( $temp_folder ) || ! is_writable( $temp_folder ) ) {
     465            throw new Exception( __( 'The PDF destination folder, ' . $temp_folder .' is not writable.', 'waterwoo-pdf' ) );
     466        }
     467        $this->temp_folder = $temp_folder;
    466468
    467469        $_file_path = $parsed_file_path['file_path'];
     
    472474        // Attempt to watermark using the open source TCPDI/TCPDF libraries
    473475        // There are other better libraries available which you can use easily if you upgrade to PDF Ink (www.pdfink.com)
    474         $watermarker = new WWPDF_Watermark( $_file_path, $watermarked_path . DIRECTORY_SEPARATOR . $this->get_requested_filename( $_file_path ), $settings );
     476        $watermarker = new WWPDF_Watermark( $_file_path, $temp_folder . DIRECTORY_SEPARATOR . $this->get_requested_filename( $_file_path ), $settings );
    475477        $watermarker->do_watermark();
    476478
     
    481483            $watermarked_file = $watermarker->newfile;
    482484        }
    483         // @todo cleanup using $this->watermarked file
    484         $this->watermarked_file = $watermarked_file;
     485
     486        $this->watermarked_file = $watermarker->newfile;
     487
     488        if ( apply_filters( 'wwpdf_do_cleanup', true ) ) {
     489            $this->do_cleanup();
     490        }
    485491
    486492        // Send watermarked file back to WooCommerce
     
    608614    /**
    609615     * Check if there is a stamped file and maybe delete it
     616     * This only happens if download type is set to FORCE.
    610617     *
    611618     * @return void
     
    613620    public function cleanup_file() {
    614621
    615         // This only happens if download type is set to FORCE
    616         if ( isset( $this->watermarked_file ) && ! empty( $this->watermarked_file->newfile ) ) {
    617             unlink( $this->watermarked_file->newfile );
    618             $this->watermarked_file = '';
     622        if ( isset( $this->watermarked_file ) && ! empty( $this->watermarked_file ) ) {
     623            $file_path = wp_normalize_path( $this->watermarked_file );
     624        } else {
     625            wwpdf_debug_log( 'Could not establish which file to delete.', 'warning' );
     626            return;
     627        }
     628
     629        $temp_folder = wp_normalize_path( $this->temp_folder );
     630
     631        if ( ! is_file( $file_path ) ) {
     632            wwpdf_debug_log( 'Could not delete watermarked PDF: does not exist or is not a regular file.', 'warning' );
     633            return;
     634        }
     635
     636        require_once ABSPATH . 'wp-admin/includes/file.php';
     637
     638        if ( ! function_exists( 'WP_Filesystem' ) ) {
     639            wwpdf_debug_log( 'Unable to get WP_Filesystem on board to delete watermarked PDF.', 'warning' );
     640            return;
     641        }
     642
     643        $creds = request_filesystem_credentials( wp_nonce_url( site_url() ) );
     644
     645        // If credentials cannot be obtained (rare on uninstall), abort
     646        if ( ! $creds ) {
     647            wwpdf_debug_log( 'Credentials to manipulate WP Filesystem not obtained, so cannot delete watermarked PDF.', 'warning' );
     648            return;
     649        }
     650
     651        // Initialise the global $wp_filesystem object
     652        if ( ! WP_Filesystem( $creds ) ) {
     653            // Initialization failed (e.g., host disallows direct access).
     654            // You could fall back to PHP's native functions, but for safety we stop.
     655            return;
     656        }
     657
     658        global $wp_filesystem;
     659
     660        if ( ! $wp_filesystem->delete( $file_path )  ) {
     661            wwpdf_debug_log( 'Unable to delete watermarked PDF (filesystem error).', 'warning' );
     662        }
     663
     664        // Attempt to delete the parent directory – if empty
     665        $parent_dir = dirname( $file_path );
     666        // Safety check to make sure directories match
     667        if ( $parent_dir === $temp_folder && is_dir( $parent_dir ) ) {
     668            // is_dir() + is_readable() + is_writable() checks are implicit in $wp_filesystem->rmdir()
     669            $wp_filesystem->rmdir( $parent_dir, false ); // false = non‑recursive (only if empty)
     670        } else {
     671            wwpdf_debug_log( 'PDF Ink temp dir not removed, maybe because it is not empty.', 'notice' );
    619672        }
    620673
     
    629682    private function do_cleanup() {
    630683
    631         // We can only *try* to cleanup if we have a forced download.
    632         if ( 'force' === get_option( 'woocommerce_file_download_method' ) ) {
     684        // We can only *try* to clean up if we have a forced download, using PHP shutdown
     685        if ( ( 'force' === get_option( 'woocommerce_file_download_method' ) && doing_filter( 'woocommerce_download_product_filepath' ) )
     686            || ( 'direct' === edd_get_file_download_method() && doing_filter( 'edd_requested_file' ) )
     687        ) {
    633688            add_action( 'shutdown', [ $this, 'cleanup_file' ] ); // this will not work every time because we cannot know download is complete before PHP shutdown
    634689        }
     
    637692        // but adding a hook here just in case you have other plans. The upgraded version of this plugin
    638693        // includes automatic file cleanup, on a chosen schedule.
    639         do_action( 'wwpdf_file_cleanup', $this->watermarked_file );
     694        do_action( 'wwpdf_file_cleanup', $this->watermarked_file, $this->temp_folder );
    640695
    641696    }
  • waterwoo-pdf/trunk/readme.txt

    r3400576 r3407018  
    66Tested up to: 6.8
    77Requires PHP: 7.4
    8 Stable tag: 4.0.5
     8Stable tag: 4.0.6
    99License: GPLv3 or later
    1010License URI: https://www.gnu.org/licenses/gpl-3.0.html
     
    202202== Changelog ==
    203203
    204 = 4.0.5 - 21 November 2024 =
     204= 4.0.6 - 1 December 2025 =
     205* Fix - automatic temp file deletion (when used with WooCommerce forced and EDD forced file delivery)
     206
     207= 4.0.5 - 21 November 2025 =
    205208* Fix - move load_plugin_textdomain() to 'init' hook
    206209* Tweak - provide debug log feedback for people getting unexpected white bars on PDF (answer: upgrade)
  • waterwoo-pdf/trunk/waterwoo-pdf.php

    r3400576 r3407018  
    44 * Plugin URI: https://wordpress.org/plugins/waterwoo-pdf/
    55 * Description: Custom watermark your PDF files upon WooCommerce, Download Monitor, and Easy Digital Download customer download. Since 2014. FKA "WaterWoo"
    6  * Version: 4.0.5
     6 * Version: 4.0.6
    77 * Author: Little Package
    88 * Author URI: https://pdfink.com/
     
    5050
    5151if ( ! defined( 'WWPDF_FREE_VERSION' ) ) {
    52     define( 'WWPDF_FREE_VERSION', '4.0.5' );
     52    define( 'WWPDF_FREE_VERSION', '4.0.6' );
    5353}
    5454
Note: See TracChangeset for help on using the changeset viewer.