Can't add the URL of saved attachment from contact form 7 to flamingo inbound messages

3.3k Views Asked by At

I’m saving the contact form submission with flamingo plugin. And I also save the form uploaded files as WordPress attachments using something this code:

//Save CF7 data
function cf7_create_post($WPCF7_ContactForm) {
    //In case you wanna check for a especific form
    /* $form_id = $data->id;
      if (224 == $WPCF7_ContactForm->id()) { */

    //Get current form
    $wpcf7      = WPCF7_ContactForm::get_current();
    // get current SUBMISSION instance
    $submission = WPCF7_Submission::get_instance();

    if ($submission) {
        $formData       = $submission->get_posted_data(); // Get all data from the posted form
        $uploaded_files = $submission->uploaded_files(); // this allows you access to the upload file in the temp location
    }

    // We need to get the CF7 field name from FILE
    $cf7_file_field_name = 'file-cv'; // [file uploadyourfile]

    //Do the magic the same as the refer link above
    $image_name     = $formData[$cf7_file_field_name];
    $image_location = $uploaded_files[$cf7_file_field_name];
    $image_content  = file_get_contents($image_location);
    $wud            = wp_upload_dir();
    $upload         = wp_upload_bits($image_name, null, $image_content);
    $chemin_final   = $upload['url'];
    $filename       = $upload['file'];
    if ($filename > '') {
        require_once(ABSPATH . 'wp-admin/includes/admin.php');
        $wp_filetype = wp_check_filetype(basename($filename), null);
        $attachment  = array(
            'post_mime_type' => $wp_filetype['type'],
            'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
            'post_content' => '',
            'post_status' => 'inherit'
        );
        $attach_id = wp_insert_attachment($attachment, $filename); // $newpostid optional
        require_once(ABSPATH . 'wp-admin/includes/image.php');
        $attach_data = wp_generate_attachment_metadata($attach_id, $filename);
        wp_update_attachment_metadata($attach_id, $attach_data);
        $attach_url = get_attached_file( $attach_id );
    }
}
add_action('wpcf7_before_send_mail', 'cf7_create_post', 10);

I want to save the URL of the attachment in flamingo (add it to the posted data), when I use ‘wpcf7_before_send_mail’ filter, the ‘wpcf7_posted_data’ filter is already fired and can’t modify the posted data anymore.

I tried to save the file as attachment using ‘wpcf7_posted_data’ filter itself (where I can add a field with the URL and it will show on flamingo post), but when ‘wpcf7_posted_data’ filter is fired, the uploaded files are not available yet in the posted data or uploaded_files() method.

I guess I can achieve that buy modifying the flamingo post later, but sure how to get its info and if it will be a good solution. Thanks

2

There are 2 best solutions below

0
TheShah On

The only way i found to do the task was to add the filepath/filename to a global variable and use one of the actionhooks of the flamingo plugin to insert the filepath.

in contact form 7 under modules/flamingo.php there is a hook "wpcf7_after_flamingo":

do_action( 'wpcf7_after_flamingo', $result );

we can use it to update the fields.

'wpcf7_before_send_mail' is triggered before the flamingo-hook, save the filepath in a global variable.

function my_before_cf7_send_mail(\WPCF7_ContactForm $contactForm){
    $submission = WPCF7_Submission::get_instance();
    //$contactForm;
    if($submission && ($contactForm->id() == 'FORM_ID')) {
        // get the name of the user
       
        $uploaded_files = $submission->uploaded_files();

        if ($uploaded_files) {
            // get the temp path of the file 
            $tempFilepath = $uploaded_files["FIELDNAME"];
            
            // Get the path to the upload directory.
            $wp_upload_dir = WP_CONTENT_DIR;

            //get original filename 
            $origFilename = sanitize_title(basename($tempFilepath));
            $newDir = $wp_upload_dir . '/uploads/customfolder/';
            $newFilepath = $newDir . $originFilename;


            copy($tempFilepath, $newFilepath);
            global $FieldnameFilePath;
            $FieldnameFilePath= $newFilepath;
        }
    }
}
add_action('wpcf7_before_send_mail', 'my_before_cf7_send_mail');

function my_add_filepath_to_flamingo($result){
    // get our global var
    global $FieldnameFilePath;

    $filepath = $FieldnameFilePath;
    
    // get the id from $result
    $flamingoEntryID = $result['flamingo_inbound_id'];

    if($flamingoEntryID){
        update_post_meta( $flamingoEntryID, '_field_FIELDNAME', $filepath );
        
        // also update the serialized post_meta with the same ID
        // i did not need that one!
    }

}
add_action('wpcf7_after_flamingo', 'my_add_filepath_to_flamingo');
1
Hossein Karimi On

Only Change:

$image_location = $uploaded_files[$cf7_file_field_name][0];
$image_name     = pathinfo( $image_location, PATHINFO_BASENAME );