Themergency

WordPress Zip Generator Class

In yesterday’s post, WordPress Admin Color Scheme Generator, I mentioned that I borrowed some ideas and code from the underscores.me theme generator plugin. That plugin processes a source directory (the underscore theme files) and does text replacements in each file, and then adds them to a zip, which can then be downloaded. It works great, but it’s very specific to the underscore theme situation. I figured that the code and resulting logic could be extracted and made into a reusable PHP class, which can be used in different situations.

A description from the Github repo:

The WP Zip Generator automates a bunch of tasks related to creating a zip file that a user can download. The “source” directory is recursively iterated through and each file is processed. You can specify which files and directories to exclude from the final zip. When each file is processed, a regex replace is performed on the file contents using variables that you can specify.

Example Usage

I made this class a little differently to other classes I have done in the past. I pass in an array or arguments that the class will use, similar to how the WP_Query class works. Here is an example instantiation:

$zip_generator = new WP_Zip_Generator(array(
    'name'                 => 'admin-color-scheme-generator',
    'process_extensions'   => array('php', 'css', 'js', 'txt', 'md'),   
    'source_directory'     => dirname( __FILE__ ) . '/source/',
    'zip_root_directory'   => "color-scheme",
    'download_filename'    => "double-rainbow.zip",
    'filename_filter'      => array($this, 'process_zip_filename'),
    'file_contents_filter' => array($this, 'process_zip_file_contents'),
    'post_process_action'  => array($this, 'process_zip'),
    'variables'            => $variables
));

As you can see, there are lots of things you can change including the files to exclude, the name of the zip file, where the source directory is, and there are also a bunch of hooks where you can customize it for your unique situation.

Filters And Hooks

Based on the name you pass into the class, you can hook into filters and actions like you would normally in WordPress. (using the name ‘doublerainbow’)

add_filter( 'zip_generator_process_filename-doublerainbow', 'process_my_file_name', 10, 2 );
add_filter( 'zip_generator_process_file_contents-doublerainbow', 'process_file_contents', 10, 2 );
add_action( 'zip_generator_post_process-doublerainbow', 'add_stuff_to_my_zip', 10, 2 );

Or you can pass in your functions, and the class will hook up everything for you:

$zip_generator = new WP_Zip_Generator(array(
    'name'                 => 'doublerainbow',
    'filename_filter'      => array($this, 'process_zip_filename'),
    'file_contents_filter' => array($this, 'process_zip_file_contents'),
    'post_process_action'  => array($this, 'process_zip'),
    'variables'            => $variables
));

The 2 filters and 1 action are:

  • filename_filter - use this filter to change the filename of a file in the zip.
  • file_contents_filter - use this filter to change the contents of a file in the zip.
  • post_process_action - this action fires after all files in the source directory have been processed, so you can add extra files to the zip or remove existing files etc.

If you generate zip files in WordPress, then give the class a spin, and contribute to make it better.