• Resolved freelaxed

    (@freelaxed)


    Hi,

    I’m looking for a way to alter posted data before submit.

    Reason: I want to store uploaded pictures in a custom folder, to save them for later. The URL to the custom location should be reported in the mail that wil be send.

    I managed to retrieve the data with the wpcf7_before_send_mail hook and the images are copied to the right location.
    So far so good, but how to return the altered data so it would be send? En maybe even possible store this value to Flamingo as well.

    My code so far:

    function cf7_save_custom_attachments($data) {
     $form_id = $data->id;
     if ($form_id == '267'){
      $form_to_DB = WPCF7_Submission::get_instance();
      if ($form_to_DB) {
       $formData = $form_to_DB->get_posted_data();
       $uploaded_files = $form_to_DB->uploaded_files();
       if(!empty($uploaded_files['uploaded_file_1'])){
        $sCurrentDate = date('U');
        $sUserName = !empty($formData['contact_voorletters']) ? $formData['contact_voorletters'] : 'John';
        $sUserName .= !empty($formData['contact_achternaam']) ? ' '.$formData['contact_achternaam'] : ' Doe';
        $sUserName .= !empty($formData['contact_email']) ? ' '.$formData['contact_email'] : ' do-not-mail-me';
        $sUserName .= !empty($formData['contact_telefoon']) ? ' '.$formData['contact_telefoon'] : ' do-not-call-me';
    					
        $sUniquUser = md5($sCurrentDate.$sUserName);
        $sWpUploadDir = wp_upload_dir();
        $sWpUploadDirPath = $sWpUploadDir['basedir'].'/archive/';
        $sWpUploadDirUrl = $sWpUploadDir['baseurl'].'/archive/';
        
        $aImagesToParse = array();
    
        if(!empty($uploaded_files)){
         foreach($uploaded_files as $aFile){
          if(!empty($aFile[0])){
           $aImagesToParse[] = $aFile[0];
          }
         }
        }
    
        if(!empty($aImagesToParse)){		
         if(!is_dir($sWpUploadDirPath.$sUniquUser)) {
          mkdir($sWpUploadDirPath.$sUniquUser , 0777);
         }
    	
         $iCount = 0;
    			
         foreach($aImagesToParse as $aFile){	
    				
          $iCount++;
    
          if(copy( $aFile, $sWpUploadDirPath.$sUniquUser.'/'.$sFileName )){															
           $sFileName = basename($aFile);
           $formData['uploaded_file_'.$iCount] = '<a href="'.$sWpUploadDirUrl.$sUniquUser.'/'.$sFileName.'">'.$sFileName.'</a>';
          }
         }					
        }
       }				
      }
      return;
     }
    }
    add_action('wpcf7_before_send_mail', 'cf7_save_custom_attachments',1);
Viewing 1 replies (of 1 total)
  • Thread Starter freelaxed

    (@freelaxed)

    I managed to get it work the way I need it!

    I combined ‘wpcf7_mail_tag_replaced’ with ‘wpcf7_after_flamingo’

    add_filter( 'wpcf7_mail_tag_replaced',
     function($replaced, $submitted, $html, $mail_tag) {
    
      $aRewriteTheseFields = array(
       'file_1',
       'file_2',
       'file_3,
       'file_4',
       'file_5',
       'file_6',
       'file_7',
       'file_8',
       'file_9',
       'file_10'
      );
    
      $sFieldName = $mail_tag->field_name();
    
      if(in_array($sFieldName,$aRewriteTheseFields)){
       if(!empty($submitted)){
    
       $form_to_DB = WPCF7_Submission::get_instance();
    
       if($form_to_DB){
    
        $formData = $form_to_DB->get_posted_data();
        $uploaded_files = $form_to_DB->uploaded_files();
        $data_hash = $form_to_DB->get_posted_data_hash();
    
        if(!empty($uploaded_files[$sFieldName])){
    
         $sCurrentDate = date('Y-m-d');
         $sUserName = !empty($data_hash) ? ' '.$data_hash : ' no-hash';
    
         $sUniquUser = md5($sCurrentDate.$sUserName);
         $sWpUploadDir = wp_upload_dir();
         $sWpUploadDirPath = $sWpUploadDir['basedir'].'/archive/';
         $sWpUploadDirUrl = $sWpUploadDir['baseurl'].'/archive/';
    
         $aImagesToParse = false;
    
         if(!empty($uploaded_files[$sFieldName][0])){
          $aImagesToParse = $uploaded_files[$sFieldName][0];
         }
    
         if(!empty($aImagesToParse)){		
    
          if(!is_dir($sWpUploadDirPath.$sUniquUser)) {
           mkdir($sWpUploadDirPath.$sUniquUser , 0777);
          }
    
          $sFileName = basename($aImagesToParse);
          if(copy( $aImagesToParse, $sWpUploadDirPath.$sUniquUser.'/'.$sFileName )){
           global $sFlamingoSavePaths;
    
           if(!is_array($sFlamingoSavePaths)){
            $sFlamingoSavePaths = array();
           }
    
           $sFlamingoKey = str_ireplace('file_', '',$sFieldName);
     
           $sFlamingoSavePaths[$sFlamingoKey] = $sWpUploadDirUrl.$sUniquUser.'/'.$sFileName;
    
           $replaced = '<a href="'.$sWpUploadDirUrl.$sUniquUser.'/'.$sFileName.'">'.$sFileName.'</a>';								
          }					
         }
        }				
       }
      }
     }
    
     return $replaced;
    }, 10, 4);
    function my_add_filepath_to_flamingo($result){
     // get our global var
     global $sFlamingoSavePaths;
    
     $flamingoEntryID = !empty($result['flamingo_inbound_id']) ? $result['flamingo_inbound_id'] : false;
    
     if($flamingoEntryID){
      if(!empty($sFlamingoSavePaths) && is_array($sFlamingoSavePaths)){
       foreach($sFlamingoSavePaths as $key=>$value){	
        update_post_meta( $flamingoEntryID, '_field_file_'.$key, $value);
       }
      }
     }
    }
    
    add_action('wpcf7_after_flamingo', 'my_add_filepath_to_flamingo');
Viewing 1 replies (of 1 total)

The topic ‘Alter posted data’ is closed to new replies.