Changeset 2912244
- Timestamp:
- 05/14/2023 11:49:38 PM (3 years ago)
- Location:
- teamgate-crm-forms/trunk
- Files:
-
- 2 edited
-
readme.txt (modified) (3 diffs)
-
teamgate.php (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
teamgate-crm-forms/trunk/readme.txt
r2893573 r2912244 6 6 Requires PHP: 7.4 7 7 Tested up to: 6.2 8 Stable tag: 1. 58 Stable tag: 1.6 9 9 License: MIT 10 10 License URI: https://www.teamgate.com/terms-of-service/ … … 53 53 Same applies to urls and phones. 54 54 55 5. 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 55 57 Contact us for more for additional features. 56 58 … … 78 80 79 81 == Changelog == 82 **1.6** 83 * Added file attachments support 84 80 85 **1.5** 81 86 * 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 25 25 } 26 26 27 /* Capture post data on form submit in Contact Form 7 */28 add_action( 'wpcf7_mail_sent', 'teamgate_wpcf7_mail_sent' );29 30 27 function handleListValueFields( $name, $fieldGroup, $groupIndex, $value, &$dataset ) { 31 28 $fieldId = preg_replace( '/teamgate-' . $fieldGroup . '-?(\d+)?-?/', '', $name ); … … 51 48 $dataset[ $fieldGroup ][ $groupIndex ]['value'] = $value; 52 49 } else { 53 $dataset[ $fieldGroup ][ $groupIndex ]['value'][ $fieldId ] = $value;54 }50 $dataset[ $fieldGroup ][ $groupIndex ]['value'][ $fieldId ] = $value; 51 } 55 52 } else if ( ! empty( $fieldId ) && $fieldId === 'type' ) { 56 53 $dataset[ $fieldGroup ][0]['type'] = $value; … … 61 58 } 62 59 } 60 61 /* Capture post data on form submit in Contact Form 7 */ 62 add_action( 'wpcf7_mail_sent', 'teamgate_wpcf7_mail_sent' ); 63 63 64 64 /** … … 70 70 $title = $contact_form->title(); 71 71 $submission = WPCF7_Submission::get_instance(); 72 $appKey = esc_attr( get_option( 'teamgate-app-key' ) ); 73 $authToken = esc_attr( get_option( 'teamgate-auth-token' ) ); 72 74 $teamgatePostData = array(); 73 75 74 76 if ( $submission ) { 75 77 $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); 76 81 } 77 82 … … 83 88 require __DIR__ . '/php-sdk/vendor/autoload.php'; 84 89 $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 87 92 ] ); 88 93 // reformat custom fields … … 113 118 } 114 119 $method = $posted_data['teamgate-entry-handler']; 115 $ api->$method->create( $teamgatePostData );120 $data = $api->$method->create( $teamgatePostData ); 116 121 } catch ( Exception $e ) { 122 error_log( $e->getMessage() ); 123 117 124 return; 118 125 } 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; 119 200 } 120 201 }
Note: See TracChangeset
for help on using the changeset viewer.