spivurno
4/7/2015 - 3:24 PM

Gravity Perks // GP Preview Submission // Adds Support for Gravity Forms Advanced File Uploader

Gravity Perks // GP Preview Submission // Adds Support for Gravity Forms Advanced File Uploader

<?php
/**
 * Gravity Perks // GP Preview Submission // Adds Support for Gravity Forms Advanced File Uploader
 *
 * By default, AFU does not provide any merge tags for displaying the files in Notifications and Confirmations. This
 * snippet provides these merge tags and also makes them available prior to submission.
 *
 * @version	  1.0
 * @author    David Smith <david@gravitywiz.com>
 * @license   GPL-2.0+
 * @link      http://gravitywiz.com/...
 */
class GW_Advanced_File_Uploader_Integration {

    public function __construct( $args = array() ) {

        // set our default arguments, parse against the provided arguments, and store for use throughout the class
        $this->_args = wp_parse_args( $args, array(
            'form_id'  => false,
            'field_id' => false
        ) );

        // do version check in the init to make sure if GF is going to be loaded, it is already loaded
        add_action( 'init', array( $this, 'init' ) );

    }

    public function init() {

        // make sure we're running the required minimum version of Gravity Forms
        if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.9', '>=' ) ) {
            return;
        }

        // carry on
		add_filter( 'gform_merge_tag_filter', array( $this, 'maybe_add_filter' ), 9, 4 );

    }

	public function maybe_add_filter( $value, $input_id, $options, $field ) {

		if( ! has_filter( 'gform_merge_tag_filter', array( 'GWPreviewConfirmation', 'preview_special_merge_tags' ) ) || $field['type'] != 'prso_gform_pluploader' ) {
			return $value;
		}

		$count = rgpost( sprintf( 'pluploader_%d_count', $field->id ) );
		if( $count < 1 ) {
			return $value;
		}

		$wp_uploads = wp_upload_dir();
		if( ! isset( $wp_uploads['baseurl'] ) ) {
			return $value;
		}

		$wp_uploads_url = $wp_uploads['baseurl'];
		$tmp_upload_url = $wp_uploads_url . '/prso-pluploader-tmp'; // hardcoded from: self::$prso_pluploader_tmp_dir_name;
		$files          = array();

		for( $i = 0; $i < $count; $i++ ) {

			$base     = sprintf( 'pluploader_%d_%d_', $field->id, $i );
			$tmp_name = $this->name_decrypt( rgars( $_POST, "plupload/{$field->id}/{$i}" ) );

			$files[]  = array(
				'name' => rgpost( $base . 'name' ),
				'url'  => sprintf( '%s/%s', $tmp_upload_url, $tmp_name )
			);

		}

		$values = array();
		foreach( $files as $file ) {
			$values[] = sprintf( '<a href="%s">%s</a>', $file['url'], $file['name'] );
		}

		$value = sprintf( '<ul><li>%s</li></ul>', implode( '</li><li>', $values ) );

		return $value;
	}

	private function name_decrypt( $file_name = NULL ) {

		$key = '4ddRp]4X5}R-WU'; // hardcoded from: self::$encrypt_key;

		if( isset($file_name, $key) && function_exists('mcrypt_decrypt') ) {
			$file_name = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($file_name), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
		}

		return $file_name;
	}

}

# Configuration

new GW_Advanced_File_Uploader_Integration();