Cron for deleting EDDiMark watermarked files

If you are using EDDiMark to stamp PDFs, and especially if you are using the “redirect” download method* with Easy Digital Downloads, files will collect on your server. We do not assume responsibility for deleting these files for several reasons, and so you may want to set up automatic deletion using a cron job (a scheduled task). Ultimately, if you’re capable of setting up a cron job at the server level, that’d be optimal, but the WP cron system should suffice. Here’s the easiest way to set something up, something customizable and capable of being terminated when you desire.

  1. First, activate the WP Crontrol plugin.
  2. Visit the WP Crontrol settings by navigating to “Tools -> Cron Events.” That screen will have two tabs at the bottom portion, below a list of currently active cron jobs in your Wordpress system.
  3. Click the “Add PHP Cron Event” tab
  4. In the “PHP Code” section, you can use the code below. Edit it to suit your needs. Use at your own risk!
  5. Give your cron job a recognizable name like “EDDiMark file pruner”
  6. Tell it when to start running in the “Next Run” field
  7. Tell it how often to run in the “Recurrence” field. Keep in mind that the more frequently this cron runs, the harder your server has to work — it can slow things down.
  8. That’s it! Now your cron job — to delete old PDF files from your server — will run on your schedule. This might be the very easiest way to get this job done. When you need to change the schedule, you’ll need to delete this cron (maybe you titled it “EDDiMark file pruner”) from the list of Cron Events at the top of the “Tools -> Cron Events” page. You can then add a new one with a different schedule. The job with the old schedule should be deleted before a job with a new schedule is added. Also, you’d want to delete this cron job if you deactivate/delete the EDDiMark plugin, then you could deactivate/delete the WP Crontrol plugin if you’re not using it, just to keep things tidy.

    function eddimark_file_pruner() {
        	
        // Bail if not in WordPress cron
    	  if ( ! edd_doing_cron() ) {
    	    return;
        }
        $dir = get_temp_dir();
        $tmp = TRUE;
        if ( apply_filters( 'eddpdf_watermark_use_uploads_dir', FALSE ) || apply_filters( 'eddimark_use_uploads_dir', false ) ) {
            $dir = EDDIMARK_TMP_FILEPATH;
            $tmp = FALSE;
        }
        eddimark_scheduled_prune( $dir, $tmp, FALSE );
    
    }
    
    function eddimark_scheduled_prune( $dir, $tmp = TRUE, $recursive = FALSE ) {
    
        if ( apply_filters( 'eddpdf_watermark_use_uploads_dir', false ) ) {
            $stub = FALSE; // numeric (Payment ID)
        } else {
            $stub = 'eddimark_'; 
        }       
    
        if ( ! $recursive ) {
    
            foreach( glob( $dir . '*' ) as $file ) { // dir is already trailing-slashed
                if ( is_dir( $file ) // we are specifically looking for directories on this first run
                && ( ( $stub && strpos( $file, $stub ) !== FALSE ) || ( ! $stub && is_numeric( $file ) ) ) ) { 
                    eddimark_scheduled_prune( $file, $tmp, TRUE );
                }
            }
        }
    
        if ( $recursive ) { // now we are hunting for PDFs
    
            $eddimark_recycle = edd_get_option( 'eddimark_recycle' );
            if ( $eddimark_recycle !== FALSE ) {
                $now = time();
                // keep files on board until user's download expires (to save processing)
                $hours_til_expires = edd_get_option( 'download_link_expiration' );
                if ( empty( $hours_til_expires ) ) {
                    $hours_til_expires = 24;
                } 
            }        
            // get straight to business deleting PDFs
            // we do not expect to find other file types in these folders
            foreach( glob( $dir . "/*.[pP][dD][fF]" ) as $file ) {
    
                if ( is_file( $file ) ) {
                    if ( $eddimark_recycle === FALSE || ( $eddimark_recycle !== FALSE && $now - filemtime( $file ) >= 3600 * $hours_til_expires ) ) {
                        @unlink( $file );
                        // file is deleted, let's delete the directory      
                        if ( ! file_exists( $file ) ) {
                            rmdir( $dir );
                        }
                    } else if ( $eddimark_recycle !== FALSE && $now - filemtime( $file ) < 3600 * $hours_til_expires ) {
                        // A file left on server for recycling (for now)
                        continue;
                    }
                }
            }
        }   
    
    } 
    // must hook in after theme setup to be able to see if 'eddpdf_watermark_use_uploads_dir' hook in effect
    add_action( 'after_setup_theme', 'eddimark_file_pruner' );

    Please note this particular function (eddimark_scheduled_prune()) only deletes files that have been on the server longer than the Easy Digital Downloads “Download Link Expiration” (set at a default 24 hours in the EDD “Misc -> File Downloads” settings). You can see the conditional for that being set on line 26. It deletes any PDFs older than that found within the folder used by EDDiMark. Be careful with it! If you need help writing something more unique to your installation, I can be hired to help.


    * If you are using Easy Digital Download’s “force” download method, you can ask that EDDiMark set files to be deleted immediately after they are served by using the following hook:

    add_filter( 'eddimark_do_cleanup', '__return_true' );

    Add this line of code, on its own line, in a WP child theme functions.php file, or by using the Code Snippets plugin. The ‘eddimark_do_cleanup’ hook does not work with “Redirect” type downloads, because there is no way to ascertain when a download is complete using this method. A cron will need to be set up — and good thing you know how to do it now!