Plugin Directory

Changeset 1907966


Ignore:
Timestamp:
07/12/2018 04:51:41 AM (8 years ago)
Author:
zaus
Message:

adjustments to handle GF multifile upload as well as single upload

  • consolidate php FILES with getting GF uploaded files
  • attempting to do the same for plain files, untested (ninja forms?)
Location:
forms-3rdparty-files/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • forms-3rdparty-files/trunk/forms-3rdparty-files.php

    r1507421 r1907966  
    66Description: Add file upload processing to Forms 3rdparty Integration
    77Author: zaus, dominiceales
    8 Version: 0.4.1
     8Version: 0.5
    99Author URI: http://drzaus.com
    1010Changelog:
     
    1414    0.4 - need to check for GF path (maybe different version than originally wrote against); return exception rather than throw it?
    1515    0.4.1 - fix for GF validation
     16    0.5 - refactored support for GF single and multifile fields
    1617*/
    1718
     
    6768
    6869        foreach($files as $k => $meta) {
    69             $submission[$k . '_attach'] = self::Transform($meta['path'], $service[self::OPTION_ATTACH_HOW]);
     70            $submission[$k . '_attach'] = self::Transform($meta, $service[self::OPTION_ATTACH_HOW]);
    7071            $submission[$k . '_size'] = $meta['size'];
    7172            $submission[$k . '_mime'] = $meta['mime'];
     
    8283    /**
    8384     * Apply appropriate transformation of the value based on the requested 'how' method
    84      * @param $value the original value
     85     * @param $meta the original metadata, including value
    8586     * @param $how how to transform it
    8687     * @return the converted value
    8788     */
    88     public static function Transform($value, $how) {
     89    public static function Transform($meta, $how) {
    8990        switch($how) {
    90             case self::VAL_ATTACH_PATH: return $value;
     91            case self::VAL_ATTACH_PATH: return $meta['path'];
    9192            case self::VAL_ATTACH_LINK:
    9293                // maybe strip wp_upload_dir()['basedir'] instead?
    93                 return site_url( str_replace(ABSPATH, '', $value) );
     94                return site_url( str_replace(ABSPATH, '', $meta['path']) );
    9495            case self::VAL_ATTACH_RAW:
    9596            case self::VAL_ATTACH_BAS64:
    9697                try {
    97                     $bytes = file_get_contents($value);
     98                    $bytes = file_get_contents($meta['tmp']);
    9899                    // could throw an exception just to get the stack trace, but since we don't want to expose
    99100                    // all of that information return the 'message' instead
     
    128129    public function service_settings($eid, $P, $entity) {
    129130    ?>
    130         <fieldset class="postbox"><legend class="hndle"><span><?php _e('File Attachments', $P); ?></span></legend>
     131        <fieldset><legend><span><?php _e('File Attachments', $P); ?></span></legend>
    131132            <div class="inside">
    132133                <em class="description">How to attach files to submission mappings.</em>
     
    185186    }
    186187
    187     static function as_gravity_form($instance) {
    188         add_filter(__CLASS__ . '_get_path', array(&$instance, 'gravity_form_path'), 10, 3);
    189     }
    190     function gravity_form_path($tmp, $field, $form) {
    191         $upload = GFFormsModel::get_temp_filename( $form['id'], $field );
    192         ### _log(__FUNCTION__, $tmp, $field, $upload);
    193 
    194         // see `forms_model.php:3684` in function `move_temp_file`
    195         // overkill path.combine
    196         return '/' . implode('/', array(trim(GFFormsModel::get_upload_path( $form['id'] ), '/'), 'tmp', trim($upload['temp_filename'], '/')));
    197     }
    198 
    199188    function get_files($files, $plugin, $form) {
    200189        /*
     
    215204    }
    216205
     206    /**
     207     * Attach gravity-forms specific hooks
     208     */
     209    public static function as_gravity_form($instance) {
     210        add_filter(__CLASS__ . '_temp_path', array(&$instance, 'gravity_forms_temp_path'), 10, 2);
     211        add_filter(__CLASS__ . '_actual_path', array(&$instance, 'gravity_forms_actual_path'), 10, 2);
     212        add_filter(__CLASS__ . '_list_files', array(&$instance, 'gravity_forms_list_files'), 10, 2);
     213    }
     214    /**
     215     * Attach gravity-forms specific hooks
     216     */
     217    public static function as_plain_files($instance) {
     218        add_filter(__CLASS__ . '_temp_path', array(&$instance, 'plain_files_temp_path'), 10, 2);
     219        add_filter(__CLASS__ . '_actual_path', array(&$instance, 'plain_files_actual_path'), 10, 2);
     220        add_filter(__CLASS__ . '_list_files', array(&$instance, 'plain_files_list_files'), 10, 2);
     221    }
     222
     223    /**
     224     * Get the full temporary path for the given form id and temp name
     225     */
     226    public function gravity_forms_temp_path($fid, $tmp) {
     227        return GFFormsModel::get_upload_path( $fid ) . '/tmp/' . $tmp;
     228    }
     229    /**
     230     * Get the actual path for the given form id and file name
     231     */
     232    public function gravity_forms_actual_path($fid, $name) {
     233        // see `forms_model.php` in function `move_temp_file` --
     234        $meta = GFFormsModel::get_file_upload_path( $fid, $name ); // gives url=>xxx, path=>xxx
     235                // ::get_upload_path only gives the base folder; `tmp` is the intermediate subfolder
     236
     237        return $meta['path']; // we'll reconstruct the url later via option
     238    }
     239    /**
     240     * Get all single and multiple file upload data, but standardize them both to same format
     241     */
     242    public function gravity_forms_list_files($result, $fid) {
     243        // because of where we've hooked, we should have access to the raw files for single-upload fields
     244        $singles = $_FILES;
     245
     246        $result += GFFormsModel::$uploaded_files[$fid];
     247
     248        // merge singles if present
     249        if(!isset($singles) || empty($singles)) return $multiples;
     250
     251        foreach($singles as $field => $data) {
     252            // reformat data to match uploaded_files
     253            $data['uploaded_filename'] = $data['name'];
     254            if(isset($data['type'])) $data['mime'] = $data['type'];
     255            // note that the php temp file `$data['tmp_name']` has already disappeared, so must get from GF
     256            $upload = GFFormsModel::get_temp_filename( $fid, $field );
     257            if(!empty($upload))
     258                $data['temp_filename'] = $this->gravity_forms_temp_path($fid, trim($upload['temp_filename'], '/'));
     259
     260            $result[$field] = array($data);
     261        }
     262
     263        return $result;
     264    }
     265
     266    /**
     267     * Get the full temporary path for the given form id and temp name
     268     */
     269    public function plain_files_temp_path($fid, $tmp) {
     270        return $tmp;
     271    }
     272    /**
     273     * Get the actual path for the given form id and file name
     274     */
     275    public function plain_files_actual_path($fid, $name) {
     276        return $name; // todo
     277    }
     278    /**
     279     * Get all single and multiple file upload data, but standardize them both to same format
     280     */
     281    public function plain_files_list_files($result, $fid) {
     282        // todo
     283       
     284        // because of where we've hooked, we should have access to the raw files for single-upload fields
     285        $singles = $_FILES;
     286
     287        // merge singles if present
     288        if(!isset($singles) || empty($singles)) return $result;
     289
     290        foreach($singles as $field => $data) {
     291            // reformat data to match uploaded_files
     292            $data['uploaded_filename'] = $data['name'];
     293            if(isset($data['type'])) $data['mime'] = $data['type'];
     294            $data['temp_filename'] = $data['tmp_name'];
     295
     296            $result[$field] = array($data);
     297        }
     298
     299        return $result;
     300    }
     301
     302    private function parse_attachment(&$data, $fid) {
     303        $meta = array(); // ready the result holder
     304
     305        $meta['name'] = $data['uploaded_filename'];
     306
     307        // using the uploaded name, get the new upload path (which will autonumber for existing)
     308        // but the current file lives in the temporary spot, maybe explicitly given or we have to find it from gf
     309        $tmp = $data['temp_filename'];
     310        if(!file_exists($tmp)) $tmp = apply_filters(__CLASS__ . '_temp_path', $fid, $tmp);
     311
     312        $meta['path'] = apply_filters(__CLASS__ . '_actual_path', $fid, $meta['name'] );
     313
     314        // other info we can get as long as we are here, if we don't have it already
     315        if(isset($data['mime'])) $meta['mime'] = $data['mime'];
     316        else {
     317            $finfo = new finfo(FILEINFO_MIME_TYPE);
     318            $meta['mime'] = $finfo->file($tmp);
     319        }
     320        $meta['size'] = isset($data['size']) ? $data['size'] : filesize($tmp);
     321       
     322        // save the temp location so we can get the actual bytes later
     323        $meta['tmp'] = $tmp;
     324
     325        return $meta;
     326    }
     327
    217328    protected function attach_files($files, $form) {
    218         /*
    219         ### _log('attach_gf_files', $files, $_FILES
    220             //, $form
    221             , GFFormsModel::$uploaded_files
    222             , GFFormsModel::get_upload_path( $form['id'] )
    223         );
    224         */
    225 
    226         foreach($_FILES as $field => $data) {
    227             $meta = array();
    228            
    229             $meta['path'] = apply_filters(__CLASS__ . '_get_path', $data['tmp_name'], $field, $form);
    230             // but GF doesn't provide the filename? toss on other stuff for fun while we're at it
    231             $meta['name'] = $data['name'];
    232             $finfo = new finfo(FILEINFO_MIME_TYPE);
    233             $meta['mime'] = $finfo->file($meta['path']);
    234             $meta['size'] = $data['size'];
    235            
    236             $files[$field] = $meta;
    237         }
    238 
    239         ### _log(__FUNCTION__, $files);
     329        // _log('attach_gf_files', $files, $_FILES ###
     330        //  //, $form
     331        //  , GFFormsModel::$uploaded_files
     332        //  , GFFormsModel::get_upload_path( $form['id'] )
     333        // );
     334       
     335        $listed = apply_filters(__CLASS__ . '_list_files', array(), $form['id']);
     336        ### _log(__FUNCTION__, 'before', $listed);
     337       
     338        foreach($listed as $field => $fieldFiles)
     339        foreach($fieldFiles as $i => $data) {
     340            $meta = $this->parse_attachment($data, $form['id']);
     341
     342            ### _log(__FUNCTION__ . '/loop', $field, $i, $data, $meta); ###
     343                       
     344            // add the first multifile just like the old/single style
     345            $files[$i < 1 ? $field : $field . '.' . (1+$i)] = $meta;
     346        }
     347       
     348        ### _log(__FUNCTION__, 'after', $files);
    240349       
    241350        return $files;
    242351    }
    243352}
    244 
    245353
    246354class F3i_CF7_Files extends F3i_Files_Form_Plugin {
  • forms-3rdparty-files/trunk/readme.txt

    r1505794 r1907966  
    44Tags: contact form, form, contact form 7, CF7, gravity forms, GF, CRM, mapping, 3rd-party service, services, remote request, file attachment, upload, file upload
    55Requires at least: 3.0
    6 Tested up to: 4.5.3
     6Tested up to: 4.9.6
    77Stable tag: trunk
    88License: GPLv2 or later
     
    6161== Changelog ==
    6262
     63= 0.5 =
     64* refactored support for GF single and multifile fields
     65
    6366= 0.4.1 =
    6467* fix #2 -- GF validation errors removes filename, fallback to path basename
Note: See TracChangeset for help on using the changeset viewer.