Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Added: Files can be sent as base64encoded #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cf7-to-zapier.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Plugin Name: CF7 to Webhook
* Plugin URI: https://github.com/Vizir/cf7-to-zapier
* Description: Use Contact Form 7 as a trigger to Zapier!
* Version: 2.2.5
* Version: 2.2.6
* Author: Mário Valney, Vizir Software Studio
* Author URI: http://vizir.com.br/en
* Text Domain: cf7-to-webhook
Expand Down
19 changes: 19 additions & 0 deletions modules/cf7/admin/webhook-panel-html.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
$activate = '0';
$hook_url = '';
$send_mail = '0';
$base64_encode_files = '0'; //in seconds
$special_mail_tags = '';

if ( is_a( $contactform, 'WPCF7_ContactForm' ) ) {
Expand All @@ -27,6 +28,9 @@
$send_mail = $properties['send_mail'];
}

if ( isset( $properties['base64_encode_files'] ) ) {
$base64_encode_files = $properties['base64_encode_files'];
}
if ( isset( $properties['special_mail_tags'] ) ) {
$special_mail_tags = $properties['special_mail_tags'];
}
Expand Down Expand Up @@ -96,6 +100,21 @@
</p>
</td>
</tr>
<tr>
<th scope="row">
<label>
<?php _e( 'Base64Encode Files', CFTZ_TEXTDOMAIN ) ?>
</label>
</th>
<td>
<p>
<label for="ctz-webhook-base64encode-files">
<input type="checkbox" id="ctz-webhook-base64encode-files" name="ctz-webhook-base64encode-files" value="1" <?php checked( $base64_encode_files, "1" ) ?>>
<?php _e( 'Sends file inputs as base64encoded urls rather than uploading into wordpress and generating fileurl', CFTZ_TEXTDOMAIN ) ?>
</label>
</p>
</td>
</tr>
</tbody>
</table>
</fieldset>
Expand Down
41 changes: 32 additions & 9 deletions modules/cf7/class-module-cf7.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ public function wpcf7_save_contact_form( $contact_form ) {
$new_properties[ 'send_mail' ] = '0';
}

if ( isset( $_POST['ctz-webhook-base64encode-files'] ) && $_POST['ctz-webhook-base64encode-files'] == '1' ) {
$new_properties[ 'base64_encode_files' ] = '1';
} else {
$new_properties[ 'base64_encode_files' ] = '0';
}

if ( isset( $_POST['ctz-special-mail-tags'] ) ) {
$new_properties[ 'special_mail_tags' ] = sanitize_textarea_field( $_POST['ctz-special-mail-tags'] );
}
Expand All @@ -173,6 +179,7 @@ public function wpcf7_contact_form_properties( $properties, $instance ) {
'activate' => '0',
'hook_url' => '',
'send_mail' => '0',
'base64_encode_files' => '0',
'special_mail_tags' => '',
);
}
Expand Down Expand Up @@ -257,6 +264,7 @@ public function wpcf7_mail_sent( $contact_form ) {
* @param obj $contact_form ContactForm Obj
*/
private function get_data_from_contact_form( $contact_form ) {
$properties = $contact_form->prop( self::METADATA );
$data = [];

// Submission
Expand Down Expand Up @@ -296,16 +304,29 @@ private function get_data_from_contact_form( $contact_form ) {
wp_mkdir_p( $upload_dir );

$filename = wp_unique_filename( $upload_dir, $tag->name . '-' . basename( $file ) );

if ( ! copy( $file, $upload_dir . '/' . $filename ) ) {
$submission = WPCF7_Submission::get_instance();
$submission->set_status( 'mail_failed' );
$submission->set_response( $contact_form->message( 'upload_failed' ) );

continue;

if($properties['base64_encode_files'] === '1') {

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$file_content_type = finfo_file($finfo, $file);
finfo_close($finfo);

$file_content = file_get_contents($file);
$file__base64_content = base64_encode($file_content);
$file_content = $file_content_type . ';base64,' . $file__base64_content;

$copied_files[] = $file_content;

} else{
if ( ! copy( $file, $upload_dir . '/' . $filename ) ) {
$submission = WPCF7_Submission::get_instance();
$submission->set_status( 'mail_failed' );
$submission->set_response( $contact_form->message( 'upload_failed' ) );

continue;
}
$copied_files[] = $upload_url . '/' . $filename;
}

$copied_files[] = $upload_url . '/' . $filename;
}

$value = $copied_files;
Expand Down Expand Up @@ -362,6 +383,8 @@ private function get_data_from_contact_form( $contact_form ) {

$data[ $key ] = $value;
}
//add contact form id to data
$data['contact_form_id'] = $contact_form->id();

/**
* You can filter data retrieved from Contact Form tags with 'ctz_get_data_from_contact_form'
Expand Down