Plugin Directory

Changeset 1954303


Ignore:
Timestamp:
10/10/2018 04:00:49 AM (7 years ago)
Author:
zaus
Message:

v0.5.2 special GF attach by label instead; refactored interval keys

  • remember last service so we can get label setting
Location:
forms-3rdparty-files/trunk
Files:
2 edited

Legend:

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

    r1907973 r1954303  
    66Description: Add file upload processing to Forms 3rdparty Integration
    77Author: zaus, dominiceales
    8 Version: 0.5
     8Version: 0.5.2
    99Author URI: http://drzaus.com
    1010Changelog:
     
    1515    0.4.1 - fix for GF validation
    1616    0.5 - refactored support for GF single and multifile fields
     17    0.5.2 - special GF attach by label instead; refactored interval keys, remember last service
    1718*/
    1819
     
    2425     */
    2526    const OPTION_ATTACH_HOW = 'f3if_how';
     27    /**
     28     * Style for referencing GF fields
     29     */
     30    const OPTION_GF_STYLE = 'f3if_gf';
    2631
    2732    const VAL_ATTACH_PATH = 'path';
     
    3439     */
    3540    const SUBMISSION_ATTACH = '_FILES_';
     41
     42    public static $last_service;
    3643
    3744    function __construct() {
     
    5966        // 6. shortcodes would allow combinations of the above
    6067
     68        // save rather than pass so other hooks can access settings
     69        self::$last_service = $service;
     70
    6171        // TODO: this really needs to be part of something from base F3i plugin; maybe inject formid (gf_xxx or cf_xxx) into submission?
    6272        $plugin = is_object($form) ? get_class($form) : gettype($form);
     
    6575        $files = apply_filters(__CLASS__ . '_get_files', array(), $plugin, $form);
    6676
    67         ### _log('files', $files);
     77        ###_log('files after filter', $files);
    6878
    6979        foreach($files as $k => $meta) {
     
    7585
    7686            // just to make sure we have something
    77             if(empty($submission[$k])) $submission[$k] = basename($meta['path']);
     87            if(empty($submission[$k])) $submission[$k] = basename($meta[F3i_Files_Form_Plugin::FINAL_META_KEY]);
    7888        }
    7989
     
    8999    public static function Transform($meta, $how) {
    90100        switch($how) {
    91             case self::VAL_ATTACH_PATH: return $meta['path'];
     101            case self::VAL_ATTACH_PATH: return $meta[F3i_Files_Form_Plugin::FINAL_META_KEY];
    92102            case self::VAL_ATTACH_LINK:
    93103                // maybe strip wp_upload_dir()['basedir'] instead?
    94                 return site_url( str_replace(ABSPATH, '', $meta['path']) );
     104                return site_url( str_replace(ABSPATH, '', $meta[F3i_Files_Form_Plugin::FINAL_META_KEY]) );
    95105            case self::VAL_ATTACH_RAW:
    96106            case self::VAL_ATTACH_BAS64:
    97107                try {
    98                     $bytes = file_get_contents($meta['tmp']);
     108                    $bytes = file_get_contents($meta[F3i_Files_Form_Plugin::CURRENT_META_KEY]);
    99109                    // could throw an exception just to get the stack trace, but since we don't want to expose
    100110                    // all of that information return the 'message' instead
     
    114124        }
    115125        // unknown
    116         return $value;
     126        return $meta;
    117127    }
    118128   
    119129    // must add (known) stuff after we're ready; any new hooks can attach themselves to `__CLASS__ . '_get_files'`
    120130    public static function register() {
    121         if(is_plugin_active('contact-form-7/wp-contact-form-7.php') || class_exists('WPCF7_ContactForm') ) new F3i_CF7_Files;
     131        if(self::has_cf7()) new F3i_CF7_Files;
    122132        // TODO: is RGFormsModel deprecated?  according to http://stackoverflow.com/questions/26942558/rgformsmodel-questions-gravity-forms but it exists in code
    123         if(is_plugin_active('gravityforms/gravityforms.php') || class_exists('RGFormsModel') ) {
    124             F3i_Form_Files::as_gravity_form( new F3i_Form_Files('array') );
    125         }
    126         if(is_plugin_active('ninja-forms/ninja-forms.php') || class_exists('Ninja_Forms') ) new F3i_Form_Files('Ninja_Forms_Processing');
     133        if(self::has_gf()) F3i_Form_Files::as_gravity_form( new F3i_Form_Files('array') );
     134        if(self::has_ninja()) new F3i_Form_Files('Ninja_Forms_Processing');
     135    }
     136
     137    public static function has_cf7() {
     138        return is_plugin_active('contact-form-7/wp-contact-form-7.php') || class_exists('WPCF7_ContactForm');
     139    }
     140    public static function has_gf() {
     141        return is_plugin_active('gravityforms/gravityforms.php') || class_exists('RGFormsModel');
     142    }
     143    public static function has_ninja() {
     144        return is_plugin_active('ninja-forms/ninja-forms.php') || class_exists('Ninja_Forms');
    127145    }
    128146
     
    143161                        self::VAL_ATTACH_RAW => 'Raw Bytes'
    144162                    ) as $k => $v) { ?>
    145                         <option value="<?php echo esc_attr($k) ; ?>" <?php selected($entity[$field], $k);?>><?php echo $v; ?></option>
     163                        <option value="<?php echo esc_attr($k) ; ?>" <?php selected(isset($entity[$field]) ? $entity[$field] : null, $k);?>><?php echo $v; ?></option>
    146164                    <?php } ?>
    147165                    </select>
    148166                    <em class="description"><?php echo sprintf(__('How to include file attachments.  They\'ll be available to mapping as %s.', $P), '<i><code>YOUR_SOURCE_FIELD</code></i><code>_attach</code>'); ?></em>
    149167                </div>
     168
     169                <?php
     170                if(self::has_gf()) :
     171                $field = self::OPTION_GF_STYLE; ?>
     172                <div class="field">
     173                    <label for="<?php echo $field, '-', $eid ?>"><?php _e('Gravity Forms index style:', $P); ?></label>
     174                    <?php foreach(array('id'=>'ID', 'lbl' => 'Label') as $k => $label) : ?>
     175                        <label for="<?php echo $field, '-', $eid, $k ?>"><?php _e($label, $P); ?></label>
     176                        <input type="radio" id="<?php echo $field, '-', $eid, $k ?>" class="radio" name="<?php echo $P, '[', $eid, '][', $field, ']'?>" value="<?php echo $k ?>" <?php checked(isset($entity[$field]) ? $entity[$field] : '', $k) ?>/>
     177                    <?php endforeach; ?>
     178                    <em class="description"><?php echo sprintf(__('How to reference fields in Gravity Forms, either as %s for ID or "%s" for Label.', $P), '<i><code>input_4</code></i>', 'My Field Label'); ?></em>
     179                </div>
     180                <?php
     181                endif;
     182                ?>
    150183            </div>
    151184        </fieldset>
     
    162195 */
    163196abstract class F3i_Files_Form_Plugin {
     197    /**
     198     * meta key of where the current (temp) file lives
     199     */
     200    const CURRENT_META_KEY = 'curr';
     201    /**
     202     * meta key of where the final (permanent) file lives
     203     */
     204    const FINAL_META_KEY = 'final';
     205
    164206    function __construct() {
    165207        add_filter('F3i_Files_Plugin_get_files', array(&$this, 'get_files'), 10, 3);
     
    210252        add_filter(__CLASS__ . '_temp_path', array(&$instance, 'gravity_forms_temp_path'), 10, 2);
    211253        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);
     254        add_filter(__CLASS__ . '_list_files', array(&$instance, 'gravity_forms_list_files'), 10, 3);
    213255    }
    214256    /**
     
    240282     * Get all single and multiple file upload data, but standardize them both to same format
    241283     */
    242     public function gravity_forms_list_files($result, $fid) {
     284    public function gravity_forms_list_files($result, $fid, $form) {
    243285        // because of where we've hooked, we should have access to the raw files for single-upload fields
    244286        $singles = $_FILES;
    245287
    246         $result += GFFormsModel::$uploaded_files[$fid];
     288        $multi = GFFormsModel::$uploaded_files[$fid];
     289
     290        $use_label = !isset(F3i_Files_Plugin::$last_service[F3i_Files_Plugin::OPTION_GF_STYLE]) || F3i_Files_Plugin::$last_service[F3i_Files_Plugin::OPTION_GF_STYLE] != 'id';
     291
     292        ###_log(__FUNCTION__, $use_label ? 'use label' : 'use id', $form);
     293
     294        ###_log('comparing changed result before', $multi);
     295
     296        if($use_label) {
     297            foreach($multi as $field => $data) {
     298                if(!is_array($data)) continue; // because singles also appear in this list
     299                $lbl = $this->extract_field_label($field, $form['fields'], $data);
     300                $multi[$lbl] = $data;
     301                unset($multi[$field]);
     302            }
     303        }
     304        ###_log('comparing changed result after', $multi);
     305
     306        $result += $multi;
     307        ###_log(__FUNCTION__ . ' multi result', $result);
    247308
    248309        // merge singles if present
     
    258319                $data['temp_filename'] = $this->gravity_forms_temp_path($fid, trim($upload['temp_filename'], '/'));
    259320
     321            // extract field index to get name if desired
     322            if($use_label) $field = $this->extract_field_label($field, $form['fields'], $data);
     323
    260324            $result[$field] = array($data);
    261325        }
    262326
     327        ###_log(__FUNCTION__ . ' with singles result', $result);
    263328        return $result;
     329    }
     330    private function extract_field_label($name, $fields, $data) {
     331        $id = intval(substr($name, 6));
     332        ###_log(__FUNCTION__, $name, $id, $fields);
     333        // save this in case we need it later?
     334        // $data['field_id'] = $id;
     335
     336        // must scan the fields array for the matching id
     337        foreach($fields as $i => $field) {
     338            ###_log('comparing', $i, $id, $field->id, $field->label, $field->adminLabel);
     339
     340            if($id == $field->id)
     341                return !isset($field->adminLabel) || empty($field->adminLabel) ? $field->label : $field->adminLabel;
     342        }
     343
     344        // couldn't find it so return original
     345        return $name;
     346
    264347    }
    265348
     
    307390        // using the uploaded name, get the new upload path (which will autonumber for existing)
    308391        // 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'] );
     392        $current = $data['temp_filename'];
     393        if(!file_exists($current)) $current = apply_filters(__CLASS__ . '_temp_path', $fid, $current);
     394       
     395        // the file will *eventually* live here
     396        $final = apply_filters(__CLASS__ . '_actual_path', $fid, $meta['name'] );
     397
     398        // but where does the file actually live right now?
     399        if(!file_exists($current)) $current = $final;
     400
     401        // save both locations so we can get the actual bytes later too
     402        $meta[self::CURRENT_META_KEY] = $current;
     403        $meta[self::FINAL_META_KEY] = $final;
    313404
    314405        // other info we can get as long as we are here, if we don't have it already
     
    316407        else {
    317408            $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;
     409            $meta['mime'] = $finfo->file($current);
     410        }
     411        $meta['size'] = isset($data['size']) ? $data['size'] : filesize($current);
    324412
    325413        return $meta;
     
    333421        // );
    334422       
    335         $listed = apply_filters(__CLASS__ . '_list_files', array(), $form['id']);
    336         ### _log(__FUNCTION__, 'before', $listed);
     423        $listed = apply_filters(__CLASS__ . '_list_files', array(), $form['id'], $form);
     424        ###_log(__FUNCTION__, 'before', $listed);
    337425       
    338426        foreach($listed as $field => $fieldFiles)
    339         foreach($fieldFiles as $i => $data) {
     427        foreach((array)$fieldFiles as $i => $data) {
    340428            $meta = $this->parse_attachment($data, $form['id']);
    341429
    342             ### _log(__FUNCTION__ . '/loop', $field, $i, $data, $meta); ###
     430            ###_log(__FUNCTION__ . '/loop', $field, $i, $data, $meta); ###
    343431                       
    344432            // add the first multifile just like the old/single style
     
    346434        }
    347435       
    348         ### _log(__FUNCTION__, 'after', $files);
     436        ###_log(__FUNCTION__, 'after', $files);
    349437       
    350438        return $files;
     
    366454        // attach metadata while we're at it
    367455        foreach($files as $field => &$meta) {
    368             $meta = array('path' => $meta);
     456            $meta = array(
     457                self::FINAL_META_KEY => $meta,
     458                // self::CURRENT_META_KEY => $meta,
     459            );
    369460           
    370461            $finfo = new finfo(FILEINFO_MIME_TYPE);
    371             $meta['mime'] = $finfo->file($meta['path']);
    372             $meta['size'] = filesize($meta['path']);
     462            $meta['mime'] = $finfo->file($meta[self::FINAL_META_KEY]);
     463            $meta['size'] = filesize($meta[self::FINAL_META_KEY]);
    373464        }
    374465        return $files;
  • forms-3rdparty-files/trunk/readme.txt

    r1907973 r1954303  
    4343* `raw`
    4444
     45= Gravity Forms input ids cause conflicts =
     46
     47Use the 'label' option to instead reference file uploads via the field label.  You can also set the Advanced > Admin Field Label to use a shorter/non-display label than what users see on the form.
     48
     49ex) If your upload field `input_5` has a very long label "Choose one or more files blah blah blah" you would reference details like `Choose one or more files blah blah blah_attach` unless you set an admin label like "FileUpload", in which case you'd map to `FileUpload_attach`.  You can also use this to attach to different upload fields across forms -- normally they might not be able to share the input_id, but if you give them the same admin field label then you only need to map the service once to that admin label.
     50
    4551= This only works for GF or CF7, what about Ninja Forms or some other form plugin? =
    4652
     
    6066
    6167== Changelog ==
     68
     69= 0.5.2 =
     70* added special GF option to use field labels
     71* some under-the-hood refactoring for clarity, reuse
    6272
    6373= 0.5 =
Note: See TracChangeset for help on using the changeset viewer.