Changeset 1954303
- Timestamp:
- 10/10/2018 04:00:49 AM (7 years ago)
- Location:
- forms-3rdparty-files/trunk
- Files:
-
- 2 edited
-
forms-3rdparty-files.php (modified) (19 diffs)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
forms-3rdparty-files/trunk/forms-3rdparty-files.php
r1907973 r1954303 6 6 Description: Add file upload processing to Forms 3rdparty Integration 7 7 Author: zaus, dominiceales 8 Version: 0.5 8 Version: 0.5.2 9 9 Author URI: http://drzaus.com 10 10 Changelog: … … 15 15 0.4.1 - fix for GF validation 16 16 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 17 18 */ 18 19 … … 24 25 */ 25 26 const OPTION_ATTACH_HOW = 'f3if_how'; 27 /** 28 * Style for referencing GF fields 29 */ 30 const OPTION_GF_STYLE = 'f3if_gf'; 26 31 27 32 const VAL_ATTACH_PATH = 'path'; … … 34 39 */ 35 40 const SUBMISSION_ATTACH = '_FILES_'; 41 42 public static $last_service; 36 43 37 44 function __construct() { … … 59 66 // 6. shortcodes would allow combinations of the above 60 67 68 // save rather than pass so other hooks can access settings 69 self::$last_service = $service; 70 61 71 // TODO: this really needs to be part of something from base F3i plugin; maybe inject formid (gf_xxx or cf_xxx) into submission? 62 72 $plugin = is_object($form) ? get_class($form) : gettype($form); … … 65 75 $files = apply_filters(__CLASS__ . '_get_files', array(), $plugin, $form); 66 76 67 ### _log('files', $files);77 ###_log('files after filter', $files); 68 78 69 79 foreach($files as $k => $meta) { … … 75 85 76 86 // 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]); 78 88 } 79 89 … … 89 99 public static function Transform($meta, $how) { 90 100 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]; 92 102 case self::VAL_ATTACH_LINK: 93 103 // 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]) ); 95 105 case self::VAL_ATTACH_RAW: 96 106 case self::VAL_ATTACH_BAS64: 97 107 try { 98 $bytes = file_get_contents($meta[ 'tmp']);108 $bytes = file_get_contents($meta[F3i_Files_Form_Plugin::CURRENT_META_KEY]); 99 109 // could throw an exception just to get the stack trace, but since we don't want to expose 100 110 // all of that information return the 'message' instead … … 114 124 } 115 125 // unknown 116 return $ value;126 return $meta; 117 127 } 118 128 119 129 // must add (known) stuff after we're ready; any new hooks can attach themselves to `__CLASS__ . '_get_files'` 120 130 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; 122 132 // 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'); 127 145 } 128 146 … … 143 161 self::VAL_ATTACH_RAW => 'Raw Bytes' 144 162 ) 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> 146 164 <?php } ?> 147 165 </select> 148 166 <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> 149 167 </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 ?> 150 183 </div> 151 184 </fieldset> … … 162 195 */ 163 196 abstract 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 164 206 function __construct() { 165 207 add_filter('F3i_Files_Plugin_get_files', array(&$this, 'get_files'), 10, 3); … … 210 252 add_filter(__CLASS__ . '_temp_path', array(&$instance, 'gravity_forms_temp_path'), 10, 2); 211 253 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); 213 255 } 214 256 /** … … 240 282 * Get all single and multiple file upload data, but standardize them both to same format 241 283 */ 242 public function gravity_forms_list_files($result, $fid ) {284 public function gravity_forms_list_files($result, $fid, $form) { 243 285 // because of where we've hooked, we should have access to the raw files for single-upload fields 244 286 $singles = $_FILES; 245 287 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); 247 308 248 309 // merge singles if present … … 258 319 $data['temp_filename'] = $this->gravity_forms_temp_path($fid, trim($upload['temp_filename'], '/')); 259 320 321 // extract field index to get name if desired 322 if($use_label) $field = $this->extract_field_label($field, $form['fields'], $data); 323 260 324 $result[$field] = array($data); 261 325 } 262 326 327 ###_log(__FUNCTION__ . ' with singles result', $result); 263 328 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 264 347 } 265 348 … … 307 390 // using the uploaded name, get the new upload path (which will autonumber for existing) 308 391 // 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; 313 404 314 405 // other info we can get as long as we are here, if we don't have it already … … 316 407 else { 317 408 $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); 324 412 325 413 return $meta; … … 333 421 // ); 334 422 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); 337 425 338 426 foreach($listed as $field => $fieldFiles) 339 foreach( $fieldFiles as $i => $data) {427 foreach((array)$fieldFiles as $i => $data) { 340 428 $meta = $this->parse_attachment($data, $form['id']); 341 429 342 ### _log(__FUNCTION__ . '/loop', $field, $i, $data, $meta); ###430 ###_log(__FUNCTION__ . '/loop', $field, $i, $data, $meta); ### 343 431 344 432 // add the first multifile just like the old/single style … … 346 434 } 347 435 348 ### _log(__FUNCTION__, 'after', $files);436 ###_log(__FUNCTION__, 'after', $files); 349 437 350 438 return $files; … … 366 454 // attach metadata while we're at it 367 455 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 ); 369 460 370 461 $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]); 373 464 } 374 465 return $files; -
forms-3rdparty-files/trunk/readme.txt
r1907973 r1954303 43 43 * `raw` 44 44 45 = Gravity Forms input ids cause conflicts = 46 47 Use 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 49 ex) 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 45 51 = This only works for GF or CF7, what about Ninja Forms or some other form plugin? = 46 52 … … 60 66 61 67 == Changelog == 68 69 = 0.5.2 = 70 * added special GF option to use field labels 71 * some under-the-hood refactoring for clarity, reuse 62 72 63 73 = 0.5 =
Note: See TracChangeset
for help on using the changeset viewer.