Plugin Directory

Changeset 2912244


Ignore:
Timestamp:
05/14/2023 11:49:38 PM (3 years ago)
Author:
teamgate
Message:

feature: add file attachments support

Location:
teamgate-crm-forms/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • teamgate-crm-forms/trunk/readme.txt

    r2893573 r2912244  
    66Requires PHP: 7.4
    77Tested up to: 6.2
    8 Stable tag: 1.5
     8Stable tag: 1.6
    99License: MIT
    1010License URI: https://www.teamgate.com/terms-of-service/
     
    5353Same applies to urls and phones.
    5454
     555. Allow to attach files to entity, by adding file fields to form. If `teamgate-entry-handler` submit button is present all files will be forwarded to teamgate.
     56
    5557Contact us for more for additional features.
    5658
     
    7880
    7981== Changelog ==
     82**1.6**
     83* Added file attachments support
     84
    8085**1.5**
    8186* Fixed bug with selectable fields that posts to teamgate api value as array even when it is supposed to be single value.
  • teamgate-crm-forms/trunk/teamgate.php

    r2893573 r2912244  
    2525}
    2626
    27 /* Capture post data on form submit in Contact Form 7 */
    28 add_action( 'wpcf7_mail_sent', 'teamgate_wpcf7_mail_sent' );
    29 
    3027function handleListValueFields( $name, $fieldGroup, $groupIndex, $value, &$dataset ) {
    3128    $fieldId = preg_replace( '/teamgate-' . $fieldGroup . '-?(\d+)?-?/', '', $name );
     
    5148            $dataset[ $fieldGroup ][ $groupIndex ]['value'] = $value;
    5249        } else {
    53             $dataset[ $fieldGroup ][ $groupIndex ]['value'][ $fieldId ] = $value;
    54         }
     50            $dataset[ $fieldGroup ][ $groupIndex ]['value'][ $fieldId ] = $value;
     51        }
    5552    } else if ( ! empty( $fieldId ) && $fieldId === 'type' ) {
    5653        $dataset[ $fieldGroup ][0]['type'] = $value;
     
    6158    }
    6259}
     60
     61/* Capture post data on form submit in Contact Form 7 */
     62add_action( 'wpcf7_mail_sent', 'teamgate_wpcf7_mail_sent' );
    6363
    6464/**
     
    7070    $title            = $contact_form->title();
    7171    $submission       = WPCF7_Submission::get_instance();
     72    $appKey           = esc_attr( get_option( 'teamgate-app-key' ) );
     73    $authToken        = esc_attr( get_option( 'teamgate-auth-token' ) );
    7274    $teamgatePostData = array();
    7375
    7476    if ( $submission ) {
    7577        $posted_data = $submission->get_posted_data();
     78        $files       = array_filter($submission->uploaded_files(), static function ($nameOfInput) {
     79            return strpos( $nameOfInput, 'teamgate' ) === 0;
     80        }, ARRAY_FILTER_USE_KEY);
    7681    }
    7782
     
    8388            require __DIR__ . '/php-sdk/vendor/autoload.php';
    8489            $api = new \Teamgate\API( [
    85                 'apiKey'    => esc_attr( get_option( 'teamgate-app-key' ) ),
    86                 'authToken' => esc_attr( get_option( 'teamgate-auth-token' ) )
     90                'apiKey'    => $appKey,
     91                'authToken' => $authToken
    8792            ] );
    8893            // reformat custom fields
     
    113118            }
    114119            $method = $posted_data['teamgate-entry-handler'];
    115             $api->$method->create( $teamgatePostData );
     120            $data   = $api->$method->create( $teamgatePostData );
    116121        } catch ( Exception $e ) {
     122            error_log( $e->getMessage() );
     123
    117124            return;
    118125        }
     126    }
     127
     128    try {
     129        if ( ! empty( $posted_data['teamgate-entry-handler'] ) && ! empty( $files ) && ! empty( $data->data->id ) ) {
     130            $entityId      = $data->data->id;
     131            $headers       = [
     132                'X-App-Key'    => $appKey,
     133                'X-Auth-Token' => $authToken,
     134                'Content-Type' => 'application/json',
     135            ];
     136            $filesToAttach = [];
     137            foreach ( $files as $fileName => $filePath ) {
     138                // make wordpress request to upload file to Teamgate Files api
     139                $fileInfo = wp_check_filetype( $filePath[0] );
     140                $body     = wp_json_encode([
     141                    'name'        => trim(strrchr($filePath[0], '/'), '/') ?? $fileName . $fileInfo['ext'],
     142                    'type'        => $fileInfo['type'],
     143                    'size'        => filesize( $filePath[0] ),
     144                    'description' => null,
     145                    // think of the way to extract description from file name or from some CF7 input field or leave it empty or remove this field because it is optional
     146                    'content'     => base64_encode( file_get_contents( $filePath[0] ) ),
     147                    // add same tags that are submitted with the form or remove this field or add your own tag strings
     148                    'tags'        => ! empty( $teamgatePostData['tags'] ) ? $teamgatePostData['tags'] : [],
     149                ]);
     150
     151
     152                $params = [
     153                    'timeout' => 60,
     154                    'headers' => $headers,
     155                    'body'    => $body,
     156                ];
     157
     158                $response = wp_remote_post( 'https://api.teamgate.com/v4/files', $params );
     159
     160                if ( is_wp_error( $response ) ) {
     161                    error_log( $response->get_error_message() );
     162                } else {
     163                    if ( $response['response']['code'] !== 200 ) {
     164                        $body = json_decode($response['body'], true);
     165                        error_log( $response['response']['message'] . ' ' . $body['error'] . ' at ' . __FILE__ . ':' . __LINE__ );
     166                        return;
     167                    }
     168
     169                    // Process the response data
     170                    $fileData = json_decode( wp_remote_retrieve_body( $response ), true );
     171                    // Do something with the response data
     172                    if ( ! empty( $fileData['data']['id'] ) ) {
     173                        $filesToAttach[] = $fileData['data']['id'];
     174                    }
     175                }
     176            }
     177
     178            if ( ! empty( $filesToAttach ) ) {
     179                $response = wp_remote_request( 'https://api.teamgate.com/v4/' . $posted_data['teamgate-entry-handler'] . '/' . $entityId . '/files', [
     180                    'method' => 'PATCH',
     181                    'headers' => $headers,
     182                    'body'    => wp_json_encode([
     183                        'value' => $filesToAttach,
     184                    ]),
     185                ] );
     186
     187                if ( is_wp_error( $response ) ) {
     188                    error_log( $response->get_error_message() );
     189                } else {
     190                    if ( $response['response']['code'] !== 200 ) {
     191                        error_log( $response['response']['message'] . ' at ' . __FILE__ . ':' . __LINE__ );
     192                    }
     193                }
     194            }
     195        }
     196    } catch ( Exception $e ) {
     197        error_log( $e->getMessage() );
     198
     199        return;
    119200    }
    120201}
Note: See TracChangeset for help on using the changeset viewer.