is_active() ) { return; } if ( $contact_form->in_demo_mode() ) { return; } $consented = true; $optional_consent_tag = false; foreach ( $contact_form->scan_form_tags( 'feature=name-attr' ) as $tag ) { if ( $tag->has_option( 'consent_for:airtable' ) ) { if ( $tag->has_option( 'optional' ) ) { $optional_consent_tag = $tag; } if ( null === $submission->get_posted_data( $tag->name ) ) { $consented = false; } break; } } if ( ! $consented ) { return; } $prop = wp_parse_args( $contact_form->prop( 'wpc_airtable' ), array( 'enabled' => true, 'app_id_selected' => '', 'table_selected' => '', 'mapping' => array(), 'map_types' => array(), ) ); if ( ! $prop['enabled'] || empty( $prop['app_id_selected'] ) || empty( $prop['table_selected'] ) ) { return; } $table_id = $prop['table_selected']; $data = (array) $submission->get_posted_data(); $files = $submission->uploaded_files(); $airtable_fields = array(); $field_mapper = WPCF7_Field_Mapper::get_instance(); $mapped_tags = Helpers\get_mapped_tags_from_contact_form( $contact_form ); foreach ( $mapped_tags as $wpcf7_field_name => $field ) { $column_name = $field['airtable_field_name']; $original_field_value = isset( $data[ $wpcf7_field_name ] ) ? $data[ $wpcf7_field_name ] : null; // Change value for special fields. if ( 'acceptance' === $field['type'] ) { $original_field_value = $original_field_value ? $field['content'] : __( 'No', 'add-on-cf7-for-airtable' ); } elseif ( 'file' === Helpers\normalize_cf7_field_type( $field['type'] ) ) { $original_field_value = get_uploaded_file_values( $files, $data, $wpcf7_field_name ); } $field_value = $field_mapper->get_formatted_field_value( $field['type'], $field['airtable_field_type'], $original_field_value ); if ( null !== $field_value ) { $airtable_fields[ $column_name ] = $field_value; } } $app_id = $prop['app_id_selected']; $service = WPCF7_Airtable_Service::get_instance(); $api_key = $service->get_api_key(); $api_client = new Airtable_API_Client( $api_key ); $records = array( array( 'fields' => $airtable_fields ) ); $response = $api_client->create_records( $app_id, $table_id, $records ); if ( is_wp_error( $response ) || $response->error ) { $error_message = $response->message; // Display error. $submission_response = __( 'Due to an error, your message could not be sent. Please try again later.', 'add-on-cf7-for-airtable' ); // If there is an optional consent tag, let the user know he can uncheck it. if ( $optional_consent_tag ) { /* translators: %s: consent checkbox text */ $submission_response = sprintf( __( 'Due to an error, your message could not be sent. Please try again later or uncheck.', 'add-on-cf7-for-airtable' ), $optional_consent_tag->content ); } $submission->set_response( $submission_response ); // Send email to administrator. $admin_email = get_option( 'admin_email' ); $subject = __( 'Error during Contact Form 7 submission', 'add-on-cf7-for-airtable' ); $message = __( 'An error occurred while saving the submission to Airtable.', 'add-on-cf7-for-airtable' ) . "\n\n"; /* translators: %s: error message */ $message .= sprintf( __( 'Error message: %s', 'add-on-cf7-for-airtable' ), $error_message ) . "\n"; wp_mail( $admin_email, $subject, $message ); $abort = true; return; } do_action( 'add-on-cf7-for-airtable/after-airtable-save' ); } /** * Collects file values for a CF7 field from uploaded files and fallback posted data. * * @param array $files Uploaded files by field. * @param array $data Posted data by field. * @param string $field_name CF7 field name. * @return string[] */ function get_uploaded_file_values( $files, $data, $field_name ) { $candidate_keys = array( $field_name, $field_name . '[]' ); $values = array(); foreach ( $candidate_keys as $key ) { if ( isset( $files[ $key ] ) ) { $values = array_merge( $values, normalize_uploaded_file_values( $files[ $key ] ) ); } } if ( empty( $values ) ) { foreach ( $candidate_keys as $key ) { if ( isset( $data[ $key ] ) ) { $values = array_merge( $values, normalize_uploaded_file_values( $data[ $key ] ) ); } } } return array_values( array_unique( $values ) ); } /** * Normalizes different uploaded file payload shapes to a flat list of strings. * * @param mixed $value Uploaded file payload. * @return string[] */ function normalize_uploaded_file_values( $value ) { if ( is_array( $value ) ) { $normalized = array(); foreach ( $value as $item ) { $normalized = array_merge( $normalized, normalize_uploaded_file_values( $item ) ); } return $normalized; } if ( ! is_string( $value ) ) { return array(); } $value = trim( $value ); if ( '' === $value ) { return array(); } return array_filter( array_map( 'trim', preg_split( '/[\r\n,]+/', $value ) ) ); } /** * Delete files uploaded ($filepaths) in `wpc_wpcf7_airtable_uploads` folder. * * @param string[] $filepaths File paths to be deleted. * @return void */ function delete_uploads( $filepaths ) { $upload_dir = wp_upload_dir(); $wpc_airtable_dirname = $upload_dir['basedir'] . '/wpc_wpcf7_airtable_uploads'; foreach ( $filepaths as $filepath ) { $filepath = realpath( $filepath ); if ( $filepath && strpos( $filepath, $wpc_airtable_dirname ) === 0 && is_file( $filepath ) ) { wp_delete_file( $filepath ); } } }