├── .gitignore ├── 3rd-parties ├── listrak │ └── functions.listrak.php ├── mailchimp │ └── functions.mailchimp.php ├── multitouch │ └── functions.multitouch.php ├── salesforce │ └── salesforce setup cf7 - screenshot.png └── service_test.php ├── README.md ├── forms-3rdparty-integration.php ├── i_plugin.png ├── includes.php ├── plugin-ui.php ├── plugin.admin.css ├── plugin.admin.js ├── plugins ├── contactform7.php ├── fplugin_base.php ├── gravityforms.php └── ninjaforms.php ├── readme.txt ├── screenshot-1.png ├── screenshot-2.png ├── screenshot-3.png ├── sprites.png └── upgrade.php /.gitignore: -------------------------------------------------------------------------------- 1 | .htaccess 2 | wp-config.php 3 | wp-content/uploads/ 4 | wp-content/blogs.dir/ 5 | wp-content/upgrade/ 6 | wp-content/backup-db/ 7 | wp-content/advanced-cache.php 8 | wp-content/wp-cache-config.php 9 | sitemap.xml 10 | *.log 11 | wp-content/cache/ 12 | wp-content/backups/ 13 | sitemap.xml 14 | sitemap.xml.gz 15 | .svn 16 | .svn* -------------------------------------------------------------------------------- /3rd-parties/listrak/functions.listrak.php: -------------------------------------------------------------------------------- 1 | scanned_form_tags as $i => $tag){ 64 | if( 'interested' === $tag['name'] ){ 65 | foreach($tag['raw_values'] as $option){ 66 | //if we haven't already seen the option, then it was unchecked 67 | if( ! in_array($option, $selected_options) ){ 68 | $list[$field.$option] = 'off'; 69 | } 70 | }// loop raw_values 71 | } 72 | }// loop scanned_form_tags 73 | 74 | $values = array_merge($values, $list); 75 | endif; //isset checkbox.main profile 76 | 77 | return $values; 78 | }//-- function listrak_subscribe_filter 79 | 80 | 81 | }//--- class Cf73rdParty_ListrakCallbacks 82 | 83 | //start 'em up 84 | $cf73rdpartycallback_instance = new Cf73rdParty_ListrakCallbacks(); 85 | endif; //class-exists 86 | 87 | #endregion ------------------------------- CUSTOM FUNCTION CALLS --------------------------------- 88 | 89 | 90 | ?> -------------------------------------------------------------------------------- /3rd-parties/mailchimp/functions.mailchimp.php: -------------------------------------------------------------------------------- 1 | getMessage()); 39 | } 40 | 41 | #wp_mail( 'debug_address@email.com', 'Callback Hit', 'callback:'.__FUNCTION__."\n\ndata:\n".print_r($response,true)."\n\nresults:\n".print_r($results, true) ); 42 | }//-- function mailchimp_newsletter_action 43 | 44 | 45 | }//--- class Forms3rdpartyIntegration_MailchimpCallbacks 46 | 47 | //start 'em up 48 | $Forms3rdpartyIntegrationcallback_instance = new Forms3rdpartyIntegration_MailchimpCallbacks(); 49 | endif; //class-exists 50 | 51 | #endregion ------------------------------- CUSTOM FUNCTION CALLS --------------------------------- 52 | -------------------------------------------------------------------------------- /3rd-parties/multitouch/functions.multitouch.php: -------------------------------------------------------------------------------- 1 | Errors:'; 32 | $results['errors'] = $data['errors']; 33 | else: 34 | 35 | //easier output 36 | 37 | $columns = array( 38 | 'id' => 'Visit ID' 39 | , 'account' => 'Account' 40 | , 'date' => 'Date' 41 | , 'visit_url' => 'Page' 42 | , 'user_id' => 'User' 43 | , 'user_ip' => 'User IP' 44 | , 'action' => 'Action' 45 | , 'referer' => 'Referer' 46 | , 'referer_keywords' => 'Keywords' 47 | , 'ppc' => 'PPC' 48 | , 'var1' => 'Var1' 49 | , 'var2' => 'Var2' 50 | , 'var3' => 'Var3' 51 | ); 52 | 53 | ob_start(); 54 | ?> 55 |
56 |

Results returned for filters:

57 |
58 | $filter_detail){ 59 | ?>
60 |
61 | 63 |
64 |
65 | 66 | 67 | 68 | $label): 70 | ?> 72 | 73 | 74 | 75 | $entry): 77 | ?> 78 | > 79 | $label): 81 | ?> 83 | 84 | 87 | 88 |
89 | getMessage()); 98 | } 99 | 100 | #wp_mail( 'debug_address@email.com', 'Callback Hit', 'callback:'.__FUNCTION__."\n\ndata:\n".print_r($response,true)."\n\nresults:\n".print_r($results, true) ); 101 | }//-- function multitouch1 102 | 103 | /** 104 | * Apply filters to integration fields 105 | * @see http://codex.wordpress.org/Function_Reference/add_filter 106 | * 107 | * @param $values array of post values 108 | * @param $service reference to service detail array 109 | * @param $form reference to form object/array 110 | */ 111 | public function multitouch1_filter($values, $service, $form){ 112 | foreach($values as $field => &$value): 113 | //filter depending on field 114 | switch($field){ 115 | case 'filters': 116 | //look for placeholders, replace with stuff 117 | $orig = $value; 118 | if(strpos($value, '{IP}') !== false){ 119 | $headers = apache_request_headers(); 120 | $ip = isset($headers['X-Forwarded-For']) ? $headers['X-Forwarded-For'] : $_SERVER['REMOTE_ADDR']; 121 | $value = str_replace('{IP}', $ip, $value); 122 | } 123 | ### _log("changed from $orig to $value in $field"); 124 | break; 125 | } 126 | endforeach; 127 | 128 | return $values; 129 | }//-- function multitouch1_filter 130 | 131 | }//--- class Forms3rdpartyIntegrationCallbacks 132 | 133 | //start 'em up 134 | $Forms3rdpartyIntegrationcallback_instance = new Forms3rdpartyIntegrationCallbacks(); 135 | endif; //class-exists 136 | 137 | #endregion ------------------------------- CUSTOM FUNCTION CALLS --------------------------------- 138 | 139 | ?> -------------------------------------------------------------------------------- /3rd-parties/salesforce/salesforce setup cf7 - screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaus/forms-3rdparty-integration/59a2a42473bbc1eb9846115fcc5333df7cf692da/3rd-parties/salesforce/salesforce setup cf7 - screenshot.png -------------------------------------------------------------------------------- /3rd-parties/service_test.php: -------------------------------------------------------------------------------- 1 | 16 | --- POST (array) --- 17 | 18 | 19 | --- POST (raw) --- 20 | 21 | 22 | --- GET --- 23 | 24 | 25 | --- META --- 26 | $_SERVER['REQUEST_METHOD'] 29 | , 'QUERY_STRING' => $_SERVER['QUERY_STRING'] 30 | , 'HTTP_HOST' => $_SERVER['HTTP_HOST'] 31 | , 'HTTP_REFERER' => $_SERVER['HTTP_REFERER'] 32 | , 'HTTP_USER_AGENT' => $_SERVER['HTTP_USER_AGENT'] 33 | )); 34 | ?> 35 | 36 | --- HEADERS --- 37 | $value) { 56 | if (substr($name, 0, 5) == 'HTTP_') { 57 | $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; 58 | } 59 | } 60 | } 61 | print_r($headers); 62 | ?> -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Forms: 3rd-Party Integration # 2 | 3 | _(please note, the following was poorly copied from the Wordpress readme)_ 4 | 5 | ---- 6 | 7 | **Contributors:** zaus, atlanticbt, spkane 8 | 9 | **Donate link:** http://drzaus.com/donate 10 | 11 | **Tags:** contact form, form, contact form 7, CF7, gravity forms, GF, CRM, mapping, 3rd-party service, services, remote request 12 | 13 | **Requires at least:** 3.0 14 | 15 | **Tested up to:** 5.2.3 16 | 17 | **Stable tag:** trunk 18 | 19 | **License:** GPLv2 or later 20 | 21 | 22 | Send contact form submissions from other plugins to multiple external services e.g. CRM. Configurable, custom field mapping, pre/post processing. 23 | 24 | ## Description ## 25 | 26 | Send [Contact Form 7], [Gravity Forms], or [Ninja Forms] Submissions to a 3rd-party Service, like a CRM. Multiple configurable services, custom field mapping. Provides hooks and filters for pre/post processing of results. Allows you to send separate emails, or attach additional results to existing emails. Comes with a couple examples of hooks for common CRMs (listrak, mailchimp, salesforce). Check out the FAQ section for add-on plugins that extend this functionality, like sending XML/SOAP posts, setting headers, and dynamic fields. 27 | 28 | The plugin essentially makes a remote request (POST) to a service URL, passing along remapped form submission values. 29 | 30 | Based on idea by Alex Hager "[How to Integrate Salesforce in Contact Form 7]". 31 | 32 | Original plugin, [Contact Form 7: 3rdparty Integration] developed with the assistance of [AtlanticBT]. Current plugin sponsored by [Stephen P. Kane Consulting]. Please submit bugs / support requests to [GitHub issue tracker] in addition to the Wordpress Support Forums because the Forums do not send emails. 33 | 34 | [Ninja Forms]: http://ninjaforms.com/ "Ninja Forms" 35 | [Gravity Forms]: http://www.gravityforms.com/ "Gravity Forms" 36 | [Contact Form 7]: http://wordpress.org/extend/plugins/contact-form-7/ "Contact Form 7" 37 | [How to Integrate Salesforce in Contact Form 7]: http://www.alexhager.at/how-to-integrate-salesforce-in-contact-form-7/ "Original Inspiration" 38 | [Contact Form 7: 3rdparty Integration]: http://wordpress.org/extend/plugins/contact-form-7-3rd-party-integration/ "CF7 Integration" 39 | [AtlanticBT]: http://www.atlanticbt.com/ "Atlantic BT: Custom Website and Web-application Services" 40 | [Stephen P. Kane Consulting]: http://www.stephenpkane.com/ "Website Design and Internet Marketing Services" 41 | [GitHub issue tracker]: https://github.com/zaus/forms-3rdparty-integration/issues "GitHub issue tracker" 42 | 43 | 44 | ## Installation ## 45 | 46 | 1. Unzip, upload plugin folder to your plugins directory (`/wp-content/plugins/`) 47 | 2. Make sure at least one of [Contact Form 7], [Gravity Forms], or [Ninja Forms] is installed 48 | 3. Activate plugin 49 | 4. Go to new admin subpage _"3rdparty Services"_ under the CF7 "Contact" menu or Gravity Forms (or Ninja Forms) "Forms" menu and configure services + field mapping. 50 | 5. Turn on 'debug mode' to get emailed a copy of the submission+response data, until you're satisfied everything works, then turn it off 51 | 52 | [Contact Form 7]: http://wordpress.org/extend/plugins/contact-form-7/ "Contact Form 7" 53 | [Gravity Forms]: http://www.gravityforms.com/ "Gravity Forms" 54 | [Ninja Forms]: http://ninjaforms.com/ "Ninja Forms" 55 | 56 | ## Frequently Asked Questions ## 57 | 58 | ### I need help / My form isn't working ### 59 | 60 | Turn on 'debug mode' from the admin page to send you an email with: 61 | 62 | * the current plugin configuration, including field mappings 63 | * the user submission (as provided by the form plugin, CF7/GF/Ninja) 64 | * the post as sent to the service (applied mapping) 65 | * the response sent back from the service, which hopefully includes error codes or explanations (often is the raw HTML of a success/failure page) 66 | 67 | Submit an issue to the [GitHub issue tracker] in addition to / instead of the WP Support Forums. 68 | 69 | ### How do I add / configure a service? ### 70 | 71 | See [Screenshots](#screenshots) for visual examples. 72 | 73 | Essentially, 74 | 75 | 1. Name your service. 76 | 2. Enter the submission URL -- if your "service" provides an HTML form, you would use the form action here. 77 | 3. Choose which forms will submit to this service ("Attach to Forms"). 78 | 4. Set the default "success condition", or leave blank to ignore (or if using post processing, see [Hooks](#hooks) - this just looks for the provided text in the service response, and if present assumes "success" 79 | 4. Set an optional "failure message" to show if the remote request fails. Can include the "nice explanation" as well as the original message provided by the contact form plugin. 80 | 5. Allow hooks for further processing - unchecking it just saves minimal processing power, as it won't try to execute filters. 81 | 6. Map your form submission values (from the CF7/GF field tags) to expected fields for your service. 82 | * 1:1 mapping given as the _name_ (from the HTML input) of the CF7/GF field and the _name_ of the 3rdparty field 83 | * For GF and Ninja Forms, you may map either by the field name or the field label 84 | * You can also provide static values by checking the "Is Value?" checkbox and providing the value in the "Form Submission Field" column. 85 | * The "Label" column is optional, and just provided for administrative notes, i.e. so you can remind yourself what each mapping pertains to. 86 | 7. Add, remove, and rearrange mapping - basically just for visual clarity. 87 | 8. Use the provided hooks (as given in the bottom of the service block). 88 | 9. Add new services as needed, drag/drop mappings and service boxes. 89 | 90 | ### How can I pre/post process the request/results? ### 91 | 92 | See section [Hooks](#hooks). See plugin folder `/3rd-parties` for example code for some common CRMs, which you can either directly include or copy to your code. 93 | 94 | [GitHub issue tracker]: https://github.com/zaus/forms-3rdparty-integration/issues "GitHub issue tracker" 95 | 96 | ### I need to submit multiple values as... ### 97 | 98 | * By default, if more than one value appears in the post request for the same field/key, they will be joined by the 'separator' value like `&post-values=a,b,c`. 99 | * However, if you use `[]` as the separator it will instead create multiple keys like `&post-values[]=a&post-values[]=b&...`. 100 | * Use `[#]` to retain the numerical index: `&post-values[0]=a&post-values[1]=b&...` 101 | * Use `[%]` to place the numerical index at desired location; specifically useful with nested fields via Xpost below (and issues [#11](https://github.com/zaus/forms-3rdparty-xpost/issues/11) and [#7](https://github.com/zaus/forms-3rdparty-xpost/issues/7)). 102 | 103 | If you instead need to combine/nest fields, check out [Forms: 3rdparty Xpost](http://wordpress.org/plugins/forms-3rd-party-xpost/). 104 | 105 | ### How do I make a GET request instead of POST? ### 106 | 107 | Since v1.7.6, it's an admin setting for GET and POST, but for anything other than those two that you'd write a hook. 108 | 109 | _from http://wordpress.org/support/topic/method-get?replies=2#post-5996489_ 110 | 111 | See 'Hooks' section, #5 of http://wordpress.org/plugins/forms-3rdparty-integration/other_notes/ and the [source code](https://github.com/zaus/forms-3rdparty-integration/blob/master/forms-3rdparty-integration.php#L478). 112 | 113 | You'll need to perform `wp_remote_get` inside that filter and set `$post_args['response_bypass']` with the response, something like: 114 | 115 | function my_3rdparty_get_override($post_args, $service, $form) { 116 | $post_args['response_bypass'] = wp_remote_get($service['url'], $post_args); 117 | return $post_args; 118 | } 119 | 120 | #### How do I dynamically change the URL? #### 121 | 122 | Use the hook `Forms3rdPartyIntegration_service_filter_url`. 123 | 124 | ### What about Hidden Fields? ### 125 | 126 | Using hidden fields can provide an easier way to include arbitrary values on a per-form basis, rather than a single "Is Value?" in the Service mapping, as you can then put your form-specific value in the hidden field, and map the hidden field name generically. 127 | 128 | For convenience, you can install the [Contact Form 7 Modules: Hidden Fields]. This plugin originally included the relevant code, but it was causing issues on install, so is no longer bundled with it. 129 | 130 | [Contact Form 7 Modules: Hidden Fields]: http://wordpress.org/extend/plugins/contact-form-7-modules/ "Hidden Fields from CF7 Modules" 131 | 132 | ### How do I export/import settings? ### 133 | Use the "Forms 3rdparty Migration" plugin http://wordpress.org/plugins/forms-3rdparty-migrate/, which lets you export and import the raw settings as JSON. 134 | You can also export settings from the original plugin [Contact Form 7: 3rdparty Integration](http://wordpress.org/extend/plugins/contact-form-7-3rd-party-integration/) and "upgrade" them for this plugin (although > 1.6.1 you will need to reselect forms). 135 | Also at https://github.com/zaus/forms-3rdparty-migrate 136 | 137 | ### How do I map url parameters? ### 138 | Use the "Dynamic Fields" plugin: http://wordpress.org/plugins/forms-3rdparty-dynamic-fields/ 139 | Also at https://github.com/zaus/forms-3rdparty-dynamicfields 140 | 141 | ### How do I send XML/submit to SOAP? ### 142 | For simple xml containers try the "Forms 3rdparty Xpost" plugin: http://wordpress.org/plugins/forms-3rd-party-xpost/ 143 | Also at https://github.com/zaus/forms-3rdparty-xpost 144 | 145 | ### How do I set headers? ### 146 | You can also set headers with "Forms 3rdparty Xpost" plugin: http://wordpress.org/plugins/forms-3rd-party-xpost/ 147 | Also at https://github.com/zaus/forms-3rdparty-xpost 148 | 149 | ### How do I show a custom message on the confirmation screen? ### 150 | The failure message is shown by default if the 3rdparty post did not succeed. You can add custom messaging to the plugin's (GF, CF7, Ninja) email or success screen response with something like: 151 | 152 | class MyPlugin { 153 | public function MyPlugin() { 154 | add_filter('Forms3rdPartyIntegration_service', array(&$this, 'adjust_response'), 10, 2); 155 | } 156 | 157 | public function adjust_response($body, $refs) { 158 | // use 'attach' to inject to regular email 159 | // use 'message' to inject to page 160 | $refs['attach'] = 'custom message in email'; 161 | $refs['message'] = 'custom message on page'; 162 | } 163 | } 164 | new MyPlugin(); // attach hook 165 | 166 | ### How do I conditionally submit? (if field == ...) ### 167 | Use hook '...use_submission' to check the form submission (pre-mapping), making sure to pick the appropriate scenario, like: 168 | 169 | add_filter('Forms3rdPartyIntegration_use_submission', 'f3i_conditional_submit', 10, 3); 170 | function f3i_conditional_submit($use_this_form, $submission, $sid) { 171 | // if there was a specific value -- skip 172 | if(isset($submission['maybe-send']) && 'no' == $submission['maybe-send']) return false; 173 | // if there was a specific value -- use 174 | if(isset($submission['maybe-send']) && 'yes' == $submission['maybe-send']) return $use_this_form; // or true, i guess 175 | // if there was a value for it (e.g. for checkboxes) -- skip 176 | if(isset($submission['if-not-send'])) return false; 177 | // if there was a value for it (e.g. for checkboxes) -- use 178 | if(isset($submission['if-send']) && !empty($submission['if-send'])) return $use_this_form; // or true, i guess 179 | 180 | return $use_this_form; // or `false`, depending on your desired default 181 | } 182 | 183 | If you want to check _after_ the fields have been mapped, you can "reuse" the hook '...service_filter_args' and return `false` to skip, rather than bypass: 184 | 185 | add_filter('Forms3rdPartyIntegration_service_filter_args', 'f3i_conditional_post', 10, 3); 186 | function f3i_conditional_post($post_args, $service, $form) { 187 | // your skip scenarios, checking `body` subarray instead 188 | if(isset($post_args['body']['maybe-send']) && ...) return false; 189 | 190 | // regular behavior 191 | return $post_args; 192 | } 193 | 194 | ### How do I resend a service call? ### 195 | 196 | Using public instance functions `send` and `handle_results`: 197 | 198 | $f3p = Forms3rdPartyIntegration::$instance; 199 | $debug = $f3p->get_settings(); 200 | // $sid - maybe get it from the current filter 201 | // $form - maybe get it from the current filter 202 | // $submission - probably save it somewhere, or rebuilt it from a database entry, etc 203 | // $service = $f3p->get_services()[$sid]; 204 | 205 | $sendResult = $f3p->send($submission, $form, $service, $sid, $debug); 206 | if($sendResult === Forms3rdPartyIntegration::RET_SEND_STOP || $sendResult === Forms3rdPartyIntegration::RET_SEND_SKIP) return; 207 | 208 | $response = $sendResult['response']; 209 | $post_args = $sendResult['post_args']; 210 | 211 | return $f3p->handle_results($submission, $response, $post_args, $form, $service, $sid, $debug); 212 | 213 | ### How do I include part of the response in another service call? ### 214 | 215 | See "Forms: 3rdparty Post Again" 216 | - Github https://github.com/zaus/forms-3rdparty-postagain 217 | - Wordpress https://wordpress.org/plugins/forms-3rdparty-post-again/ 218 | 219 | ### How do I include part of the response the contact form results? ### 220 | 221 | See "Forms: 3rdparty Inject Results" 222 | - Github https://github.com/zaus/forms-3rdparty-inject-results 223 | - Wordpress https://wordpress.org/plugins/forms-3rdparty-inject-results/ 224 | 225 | However, currently only confirmed working with Gravity Forms. 226 | 227 | 228 | ## Screenshots ## 229 | 230 | __Please note these screenshots are from the previous plugin incarnation, but are still essentially valid.__ 231 | 232 | 1. Admin page - create multiple services, set up debugging/notice emails, example code 233 | ![Admin page - create multiple services, set up debugging/notice emails, example code](http://s.w.org/plugins/forms-3rdparty-integration/screenshot-1.png) 234 | 235 | 2. Sample service - mailchimp integration, with static and mapped values 236 | ![Sample service - mailchimp integration, with static and mapped values](http://s.w.org/plugins/forms-3rdparty-integration/screenshot-2.png) 237 | 238 | 3. Sample service - salesforce integration, with static and mapped values 239 | ![Sample service - salesforce integration, with static and mapped values](http://s.w.org/plugins/forms-3rdparty-integration/screenshot-3.png) 240 | 241 | 242 | ## Changelog ## 243 | 244 | ### 1.8 ### 245 | * copy service button 246 | * Ninja Forms are back! Can use Ninja Forms plugin v3.0+ again. 247 | 248 | ### 1.7.9 ### 249 | * debug message truncation with configure hooks 250 | * use hook `...debug_truncation` to set field length limits for each section 251 | 252 | ### 1.7.8 ### 253 | * adding per-service delimiter, supports newlines 254 | * 'add new service' button after metaboxes 255 | * minor ui fixes 256 | 257 | ### 1.7.7 ### 258 | * destination mapping is textarea to make other plugins easier (e.g. newlines in xpost formatting), hooks `..._service_mappings_headers` and `..._service_mappings_values` to add more columns 259 | 260 | ### 1.7.6 ### 261 | * exposing http method (get/post); result redirection 262 | 263 | ### 1.7.5 ### 264 | * late-bind GF confirmation for script tags 265 | 266 | ### 1.7.4 ### 267 | * another slight fix to make GF Resend do submission hooks too (so Reformat will work with it as well) 268 | 269 | ### 1.7.3 ### 270 | * slight refactor of `before_send` to make reposting GF submissions easier 271 | 272 | ### 1.7.2.1 ### 273 | * fix minor conflict between Inject Results and Post-Again 274 | 275 | ### 1.7.2 ### 276 | * added injection hooks for [Forms: 3rdparty Inject Results](https://github.com/zaus/forms-3rdparty-inject-results) -- use via `$form = apply_filters(Forms3rdPartyIntegration::$instance->N('inject'), $form, $values_to_inject);` 277 | 278 | ### 1.7 ### 279 | * refactored internal methods to make them reusable externally, specifically for 'forms-3rdparty-postagain' plugin 280 | 281 | ### 1.6.6.5 ### 282 | * added URL filter to allow customizing GET requests with post body arguments, or shortcodes 283 | * filter added to fplugin_base to allow multiple attachments per [github #62](https://github.com/zaus/forms-3rdparty-integration/issues/62) 284 | * fix [github #68](https://github.com/zaus/forms-3rdparty-integration/issues/68) 285 | * 'secret' debugging feature for error dump if logging after unable to send email: add a truthy value to post mapping with 3rdparty key `_json` 286 | 287 | ### 1.6.6.4 ### 288 | * fix array value without index placeholder bug introduced in [github #43](https://github.com/zaus/forms-3rdparty-integration/issues/43) 289 | * final bugfix to #55 (default options `mode` array) 290 | * tried to address [Xpost issue #7](https://github.com/zaus/forms-3rdparty-xpost/issues/7), but not the right place for it 291 | 292 | ### 1.6.6.3 ### 293 | * bugfixes [#53](https://github.com/zaus/forms-3rdparty-integration/issues/53) and [#55](https://github.com/zaus/forms-3rdparty-integration/issues/55) 294 | 295 | ### 1.6.6.1 ### 296 | * debug logging hook 297 | * fixed #52 - some hosting providers rejected arbitrary sender addresses 298 | * more options for debug mail failure logging 299 | 300 | ### 1.6.6 ### 301 | * Can now map GF and Ninja Forms by field label as well as id per issue #35 ([map by name](https://github.com/zaus/forms-3rdparty-integration/issues/35)) 302 | 303 | ### 1.6.5.1 ### 304 | * fix Github issue #43 ([valid success response codes](https://github.com/zaus/forms-3rdparty-integration/issues/43)) 305 | * fix Github issue #27 ([admin label](https://github.com/zaus/forms-3rdparty-integration/issues/27)) 306 | * exposed `$service` to hook `get_submission` to make extensions easier 307 | 308 | ### 1.6.4.3 ### 309 | * fix escaped slashes for gravity forms submissions, see [GitHub issue #42](https://github.com/zaus/forms-3rdparty-integration/issues/42) 310 | 311 | ### 1.6.4.2 ### 312 | * including original `$submission` in `service_filter_post` hook for [dynamicfields calc](https://wordpress.org/plugins/forms-3rdparty-dynamic-fields/) 313 | 314 | ### 1.6.4.1 ### 315 | * quick fix for global section toggle bug 316 | 317 | ### 1.6.4 ### 318 | * conditional submission hooks (see FAQ) 319 | * removed somewhat useless 'can-hook' setting, since I assume everybody wants success processing. Comment via github or author website contact form if you really need it. 320 | 321 | ### 1.6.3.1 ### 322 | * Fix for longstanding (?) Firefox admin bug (issue #36) preventing field editing/input selection 323 | 324 | ### 1.6.3 ### 325 | * fix form plugin checking when multiple contact form plugins used at same time 326 | 327 | ### 1.6.1 ### 328 | * integration with [Ninja Forms](http://www.ninjaforms.com) 329 | * refactored CF7 and GF integrations to take advantage of new FPLUGIN base (to make future integrations easier) 330 | * defined upgrade path 331 | 332 | Due to the new common form extension base, the way forms are identified in the settings has been changed. 333 | Deactivating and reactivating the plugin (which happens automatically on upgrade, but not FTP or other direct file changes) should correct your existing settings. 334 | 335 | Depending on how many services you have configured, the upgrade path may DESELECT your form selections in each service or otherwise break some configurations. 336 | If you are concerned this may affect you, please [export](https://github.com/zaus/forms-3rdparty-migrate) the settings so you can reapply your selections. 337 | 338 | 339 | ### 1.4.9 ### 340 | Updated cf7 plugin to match [their latest changes](http://contactform7.com/2014/07/02/contact-form-7-39-beta/). 341 | * using new way to access properties 342 | * removed remaining support for all older versions of CF7 (it was just getting complicated) 343 | 344 | ### 1.4.8.1 ### 345 | Trying to add some clarity to the admin pages 346 | 347 | ### 1.4.8 ### 348 | * multiple values treated differently depending on separator: 'char', `[]`, or `[#]` 349 | * static values treated the same as dynamic (so they get above processing) 350 | * fix: php5 constructor re: https://github.com/zaus/forms-3rdparty-integration/issues/6 351 | 352 | ### 1.4.7 ### 353 | * totally removing hidden field plugin -- seems like even though it wasn't referenced, it may have caused the "invalid header" error during install 354 | * admin ui - js fixes (configurable section icons via `data-icon`; entire metabox title now toggles accordion) 355 | * stripslashes on submission to fix apostrophes in 'failure response' textarea 356 | 357 | ### 1.4.6 ### 358 | * hook `...service_filter_args` to allow altering post headers, etc 359 | * fix: removed more args-by-reference (for PHP 5.4 issues, see support forum requests) 360 | * tested with WP 3.8, CF7 3.6 361 | 362 | ### 1.4.5 ### 363 | * fix: failure response attaches to 'onscreen message' for Gravity Forms 364 | * fix: (actually part of the next feature) failure response shows onscreen for Contact Form 7 365 | * customize the failure response shown onscreen -- new admin setting per service (see description) 366 | 367 | ### 1.4.4 ### 368 | * protecting against unattached forms 369 | * Github link 370 | * global post filter `Forms3rdPartyIntegration_service_filter_post` in addition to service-specific with suffix `_0`; accepts params `$post`, `$service`, `$form`, `$sid` 371 | * admin options hook `Forms3rdPartyIntegration_service_settings`, `..._metabox` 372 | * fix: gravityforms empty 'notification' field 373 | * fix: admin ui -- 'hooks' toggle on metabox clone, row clone fieldname 374 | * fix: service hooks not fired multiple times when both GF and CF7 plugins are active 375 | * fix: Gravityforms correctly updates $form array 376 | 377 | ### 1.4.3 ### 378 | * Fixed "plugin missing valid header" caused by some PHP versions rejecting passing variable by reference (?) as reported on Forum support topics ["Error on install"](http://wordpress.org/support/topic/error-on-install-6) and ["The plugin does not have a valid header"](http://wordpress.org/support/topic/the-plugin-does-not-have-a-valid-header-34), among others 379 | * Rewrote admin javascript to address style bug as reported on forum post ["fields on mapping maintenance screen misaligned"](http://wordpress.org/support/topic/fields-on-mapping-maintenance-screen-misaligned) and via direct contact. Really needed to be cleaned up anyway, I've learned more jQuery since then ;) 380 | * Dealt with weird issue where clicking a label also triggers its checkbox click 381 | * Other general ui fixes 382 | * More "verbose" endpoint-test script (headers, metadata as well as get/post) 383 | 384 | ### 1.4.2 ### 385 | 386 | * Bugfixes 387 | * cleaned up admin JS using delegate binding 388 | * added "empty" checking for 3rdparty entries to avoid dreaded "I've deleted my mappings and can't do anything" error 389 | * timeout option 390 | * fixed CF7 form selection bug 391 | * conditionally load CF7 or GF only if active/present; note that this plugin must `init` normally to check CF7 392 | 393 | ### 1.4.1 ### 394 | 395 | * Bugfixes 396 | * Added "Label" column for administrative notes 397 | 398 | ### 1.4.0 ### 399 | 400 | * Forked from [Contact Form 7: 3rdparty Integration]. 401 | * Removed 'hidden field plugin' from 1.3.0, as it's specific to CF7. 402 | 403 | ### 1.3.2 ### 404 | 405 | * Added failure hook - if your service fails for some reason, you now have the opportunity to alter the CF7 object and prevent it from mailing. 406 | 407 | ### 1.3.1 ### 408 | 409 | * Added check for old version of CF7, so it plays nice with changes in newer version (using custom post type to store forms instead, lost original function for retrieval) 410 | * see original error report http://wordpress.org/support/topic/plugin-forms-3rd-party-integration-undefined-function-wpcf7_contact_forms?replies=2#post-2646633 411 | 412 | ### 1.3.0 ### 413 | moved "external" includes (hidden-field plugin) to later hook to avoid conflicts when plugin already called 414 | 415 | ### 1.2.3 ### 416 | changed filter callback to operate on entire post set, changed name 417 | 418 | ### 1.2.2 ### 419 | fixed weird looping problem; removed some debugging code; added default service to test file 420 | 421 | ### 1.2 ### 422 | moved filter to include dynamic and static values; icons 423 | 424 | ### 1.1 ### 425 | added configuration options, multiple services 426 | 427 | ### 1.0 ### 428 | base version, just directly submits values 429 | 430 | 431 | [Contact Form 7: 3rdparty Integration]: http://wordpress.org/extend/plugins/contact-form-7-3rd-party-integration/ "CF7 Integration" 432 | 433 | ## Upgrade Notice ## 434 | 435 | ### 1.8 ### 436 | Now that Ninja Forms works again with this plugin there may be conflicts with the related sub-plugin "Forms 3rdparty File Attachments". Please deactivate the "File Attachments" plugin if you are unable to submit your contact form. 437 | 438 | ### 1.6.1 ### 439 | Due to the new common form extension base, the way forms are identified in the settings has been changed. 440 | Deactivating and reactivating the plugin (which happens automatically on upgrade, but not FTP or other direct file changes) should correct your existing settings. 441 | See Changelog for more details. 442 | 443 | ### 1.4.6 ### 444 | * PHP 5.4 errors with (deprecated) passing arguments by reference should be fixed. 445 | * Behavior change when reporting `$post` args in `on_response_failure` and similar -- now returns `$post_args`, which contains the header+body array as sent to new hook `...service_filter_args` 446 | * Please submit a [GitHub issue](https://github.com/zaus/forms-3rdparty-integration/issues) in addition to making a support forum request if something is broken. 447 | 448 | ### 1.4.5 ### 449 | You may need to configure the 'failure message', or at least refresh and save the admin settings, to avoid PHP 'empty index' warnings. 450 | 451 | ### 1.4.0 ### 452 | Accommodates Gravity Forms. Complete plugin rewrite, namespace since 1.3.2 incarnation as CF7 3rdparty Integration. Incompatible with previous versions. 453 | 454 | ### 1.3.1 ### 455 | See 1.3.0 notice 456 | 457 | ### 1.3.0 ### 458 | Fixes should accomodate CF7 < v1.2 and changes to >= v1.2 -- please test and check when upgrading, and report any errors to the plugin forum. 459 | 460 | ## Hooks ## 461 | 462 | _Please note that this documentation is in flux, and may not be accurate for latest rewrite 1.4.0_ 463 | 464 | 1. `add_action('Forms3rdPartyIntegration_service_a#', $response, $param_ref);` 465 | * hook for each service, indicated by the `#` - _this is given in the 'Hooks' section of each service_ 466 | * provide a function which takes `$response, &$results` as arguments 467 | * allows you to perform further processing on the service response, and directly alter the processing results, provided as `array('success'=>false, 'errors'=>false, 'attach'=>'', 'message' => '');` 468 | * *success* = `true` or `false` - change whether the service request is treated as "correct" or not 469 | * *errors* = an array of error messages to return to the form 470 | * *attach* = text to attach to the end of the email body 471 | * *message* = the message notification shown (from CF7 ajax response) below the form 472 | * note that the basic "success condition" may be augmented here by post processing 473 | 2. `add_action('Forms3rdPartyIntegration_service', $response, $param_ref, $sid);` 474 | * same as previous hook, but not tied to a specific service 475 | 3. `add_filter('Forms3rdPartyIntegration_service_filter_post_#, ...` 476 | * hook for each service, indicated by the `#` - _this is given in the 'Hooks' section of each service_ 477 | * allows you to programmatically alter the request parameters sent to the service 478 | * should return updated `$post` array 479 | 4. `add_filter('Forms3rdPartyIntegration_service_filter_post', 'YOUR_HOOK', 10, 4);` 480 | * in addition to service-specific with suffix `_a#`; accepts params `$post`, `$service`, `$form`, `$sid` 481 | 5. `add_filter('Forms3rdPartyIntegration_service_filter_args', 'YOUR_HOOK', 10, 3);` 482 | * alter the [args array](http://codex.wordpress.org/Function_Reference/wp_remote_post#Parameters) sent to `wp_remote_post` 483 | * allows you to add headers or override the existing settings (timeout, body) 484 | * if you return an array containing the key `response_bypass`, it will skip the normal POST and instead use that value as the 3rdparty response; note that it must match the format of a regular `wp_remote_post` response. 485 | * Note: if using `response_bypass` you should consider including the original arguments in the callback result for debugging purposes. 486 | 6. `add_action('Forms3rdPartyIntegration_remote_failure', 'mycf7_fail', 10, 5);` 487 | * hook to modify the Form (CF7 or GF) object if service failure of any kind occurs -- use like: 488 | 489 | function mycf7_fail(&$cf7, $debug, $service, $post, $response) { 490 | $cf7->skip_mail = true; // stop email from being sent 491 | // hijack message to notify user 492 | ///TODO: how to modify the "mail_sent" variable so the message isn't green? on_sent_ok hack? 493 | $cf7->messages['mail_sent_ok'] = 'Could not complete mail request:** ' . $response['safe_message']; 494 | } 495 | 496 | * needs some way to alter the `mail_sent` return variable in CF7 to better indicate an error - no way currently to access it directly. 497 | 7. `add_action('Forms3rdPartyIntegration_service_settings', 'YOUR_HOOK', 10, 3)` 498 | * accepts params `$eid`, `$P`, `$entity` corresponding to the index of each service entity and this plugin's namespace, and the `$entity` settings array 499 | * allows you to add a section to each service admin settings 500 | * name form fields with plugin namespace to automatically save: `$P[$eid][YOUR_CUSTOM_FIELD]` $rarr; `Forms3rdPartyIntegration[0][YOUR_CUSTOM_FIELD]` 501 | 8. `add_action('Forms3rdPartyIntegration_service_metabox', 'YOUR_HOOK', 10, 2)` 502 | * accepts params `$P`, `$entity` corresponding to the index of each service entity and this plugin's namespace, and the `$options` settings array (representing the full plugin settings) 503 | * allows you to append a metabox (or anything else) to the plugin admin settings page 504 | * name form fields with plugin namespace to automatically save: `$P[YOUR_CUSTOM_FIELD]` $rarr; `Forms3rdPartyIntegration[YOUR_CUSTOM_FIELD]` 505 | 9. `add_filter('Forms3rdPartyIntegration_debug_message', 'YOUR_HOOK', 10, 5);` 506 | * bypass/alternate debug logging 507 | 10. `add_filter('Forms3rdPartyIntegration_plugin_hooks', 'YOUR_HOOK', 10, 1);` 508 | * Accepts an array of contact form plugin hooks to attach F3p to, and returns that array. Modify result to attach to additional plugin hooks, like GF edit. 509 | 11. `add_filter('Forms3rdPartyIntegration_service_filter_url', 'YOUR_HOOK', 10, 2);` 510 | * hook a function that takes the `$service_url, $post_args` and returns the endpoint `$url` 511 | * used to modify the submission url based on mappings or other information 512 | * `$post_args` contains the `body` and other `wp_remote_post` details 513 | 514 | 515 | Basic examples provided for service hooks directly on plugin Admin page (collapsed box "Examples of callback hooks"). Code samples for common CRMS included in the `/3rd-parties` plugin folder. 516 | 517 | ## Stephen P. Kane Consulting ## 518 | 519 | From [the website] and [Handpicked Tomatoes]: 520 | 521 | **Transparent and Holistic Approach** 522 | 523 | > Transparency is good. It's amazing how many web design sites hide who they are. There are lots of reasons, none of which are good for the customer. We don't do that. I'm Stephen Kane, principal web craftsman at HandpickedTomatoes, and I'm an Orange County based freelancer who occasionally works with other local freelancers and agencies to deliver quality web solutions at very affordable prices. 524 | > We work to earn the right to be a trusted partner. One that you can turn to for professional help in strategizing, developing, executing, and maintaining your Internet presence. 525 | > We take a holistic view. Even if a project is small, our work should integrate into the big picture. We craft web architecture and designs that become winning websites that are easy to use and to share. We custom build social network footprints on sites like linkedin, facebook, twitter, youtube, flickr, yelp!, and google places and integrate them into your website to leverage social marketing. We help you set up and execute email campaigns, with search engine marketing, with photography, with site copy and content and anything else that you need in order to have a successful Internet presence. 526 | > Through this holistic approach, we work with clients to grow their sales, improve their brand recognition, and manage their online reputation. 527 | 528 | [the website]: http://www.stephenpkane.com/ "Wordpress, Online Marketing, Social Media, SEO" 529 | [Handpicked Tomatoes]: http://handpickedtomatoes.com/ "Website Design & Internet Marketing Services" -------------------------------------------------------------------------------- /forms-3rdparty-integration.php: -------------------------------------------------------------------------------- 1 | register(__FILE__); 52 | 53 | class Forms3rdPartyIntegration { 54 | 55 | #region =============== CONSTANTS AND VARIABLE NAMES =============== 56 | 57 | const pluginPageTitle = 'Forms: 3rd Party Integration'; 58 | 59 | const pluginPageShortTitle = '3rdparty Services'; 60 | 61 | /** 62 | * Admin - role capability to view the options page 63 | * @var string 64 | */ 65 | const adminOptionsCapability = 'manage_options'; 66 | 67 | /** 68 | * Version of current plugin -- match it to the comment 69 | * @var string 70 | */ 71 | const pluginVersion = '1.8'; 72 | 73 | 74 | /** 75 | * Self-reference to plugin name 76 | * @var string 77 | */ 78 | private $N; 79 | 80 | /** 81 | * Namespace the given key 82 | * @param string $key the key to namespace 83 | * @return the namespaced key 84 | */ 85 | public function N($key = false) { 86 | // nothing provided, return namespace 87 | if( ! $key || empty($key) ) { return $this->N; } 88 | return sprintf('%s_%s', $this->N, $key); 89 | } 90 | 91 | /** 92 | * Parameter index for mapping - administrative label (reminder) 93 | */ 94 | const PARAM_LBL = 'lbl'; 95 | /** 96 | * Parameter index for mapping - source plugin (i.e. GravityForms, CF7, etc) 97 | */ 98 | const PARAM_SRC = 'src'; 99 | /** 100 | * Parameter index for mapping - 3rdparty destination 101 | */ 102 | const PARAM_3RD = '3rd'; 103 | 104 | /** 105 | * How long (seconds) before considering timeout 106 | */ 107 | const DEFAULT_TIMEOUT = 10; 108 | 109 | /** 110 | * Singleton 111 | * @var object 112 | */ 113 | public static $instance = null; 114 | 115 | #endregion =============== CONSTANTS AND VARIABLE NAMES =============== 116 | 117 | 118 | #region =============== CONSTRUCTOR and INIT (admin, regular) =============== 119 | 120 | // php5 constructor must come first for 'strict standards' -- http://wordpress.org/support/topic/redefining-already-defined-constructor-for-class-wp_widget 121 | 122 | function __construct() { 123 | $this->N = __CLASS__; 124 | 125 | add_action( 'admin_menu', array( &$this, 'admin_init' ), 20 ); // late, so it'll attach menus farther down 126 | add_action( 'init', array( &$this, 'init' ) ); // want to run late, but can't because it misses CF7 onsend? 127 | } // function 128 | 129 | function Forms3rdPartyIntegration() { 130 | $this->__construct(); 131 | } // function 132 | 133 | 134 | function admin_init() { 135 | # perform your code here 136 | //add_action('admin_menu', array(&$this, 'config_page')); 137 | 138 | //add plugin entry settings link 139 | add_filter( 'plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 ); 140 | 141 | //needs a registered page in order for the above link to work? 142 | #$pageName = add_options_page("Custom Shortcodes - ABT Options", "Shortcodes -ABT", self::adminOptionsCapability, 'abt-shortcodes-config', array(&$this, 'submenu_config')); 143 | if ( function_exists('add_submenu_page') ){ 144 | 145 | // autoattach to cf7, gravityforms 146 | $subpagesOf = apply_filters($this->N('declare_subpages'), array()); 147 | foreach($subpagesOf as $plugin) { 148 | $page = add_submenu_page(/*'plugins.php'*/$plugin, __(self::pluginPageTitle), __(self::pluginPageShortTitle), self::adminOptionsCapability, basename(__FILE__,'.php').'-config', array(&$this, 'submenu_config')); 149 | //add admin stylesheet, etc 150 | add_action('admin_print_styles-' . $page, array(&$this, 'add_admin_headers')); 151 | } 152 | 153 | 154 | //register options 155 | $default_options = array( 156 | 'debug' => array('email'=>get_bloginfo('admin_email'), 'separator'=>', ', 'mode' => array(), 'sender' => '') 157 | , 0 => array( 158 | 'name'=>'Service 1' 159 | , 'url'=>plugins_url('3rd-parties/service_test.php', __FILE__) 160 | , 'success'=>'' 161 | , 'failure'=>'' 162 | , 'forms' => array() 163 | , 'timeout' => self::DEFAULT_TIMEOUT // timeout in seconds 164 | , 'mapping' => array( 165 | array(self::PARAM_LBL=>'The submitter name',self::PARAM_SRC=>'your-name', self::PARAM_3RD=>'name') 166 | , array(self::PARAM_LBL=>'The email address', self::PARAM_SRC=>'your-email', self::PARAM_3RD=>'email') 167 | ) 168 | ) 169 | ); 170 | add_option( $this->N('settings'), $default_options ); 171 | } 172 | 173 | } // function 174 | 175 | 176 | /** 177 | * General init 178 | * Add scripts and styles 179 | * but save the enqueue for when the shortcode actually called? 180 | */ 181 | function init(){ 182 | // needed here because both admin and before-send functions require v() 183 | /// TODO: more intelligently include... 184 | include_once('includes.php'); 185 | 186 | #wp_register_script('jquery-flip', plugins_url('jquery.flip.min.js', __FILE__), array('jquery'), self::pluginVersion, true); 187 | #wp_register_style('sponsor-flip', plugins_url('styles.css', __FILE__), array(), self::pluginVersion, 'all'); 188 | # 189 | #if( !is_admin() ){ 190 | # /* 191 | # add_action('wp_print_header_scripts', array(&$this, 'add_headers'), 1); 192 | # add_action('wp_print_footer_scripts', array(&$this, 'add_footers'), 1); 193 | # */ 194 | # wp_enqueue_script('jquery-flip'); 195 | # wp_enqueue_script('sponsor-flip-init'); 196 | # wp_enqueue_style('sponsor-flip'); 197 | #} 198 | 199 | 200 | // allow extensions; remember to check !is_admin 201 | do_action($this->N('init'), false); 202 | 203 | if(!is_admin()){ 204 | //add_action('wp_footer', array(&$this, 'shortcode_post_slider_add_script')); //jedi way to add shortcode scripts 205 | } 206 | 207 | } 208 | 209 | #endregion =============== CONSTRUCTOR and INIT (admin, regular) =============== 210 | 211 | #region =============== HEADER/FOOTER -- scripts and styles =============== 212 | 213 | /** 214 | * Add admin header stuff 215 | * @see http://codex.wordpress.org/Function_Reference/wp_enqueue_script#Load_scripts_only_on_plugin_pages 216 | */ 217 | function add_admin_headers(){ 218 | 219 | wp_enqueue_script($this->N('admin'), plugins_url('plugin.admin.js', __FILE__), array('jquery', 'jquery-ui-sortable'), self::pluginVersion, true); 220 | wp_localize_script($this->N('admin'), $this->N('admin'), array( 221 | 'N' => $this->N() 222 | )); 223 | 224 | $stylesToAdd = array( 225 | basename(__FILE__,'.php') => 'plugin.admin.css' //add a stylesheet with the key matching the filename 226 | ); 227 | 228 | // Have to manually add to in_footer 229 | // Check if script is done, if not, then add to footer 230 | foreach($stylesToAdd as $handle => $stylesheet){ 231 | wp_enqueue_style( 232 | $handle //id 233 | , plugins_url($stylesheet, __FILE__) //file 234 | , array() //dependencies 235 | , self::pluginVersion //version 236 | , 'all' //media 237 | ); 238 | } 239 | }//--- function add_admin_headers 240 | 241 | /** 242 | * Only add scripts and stuff if shortcode found on page 243 | * TODO: figure out how this works -- global $wpdb not correct 244 | * @source http://shibashake.com/wordpress-theme/wp_enqueue_script-after-wp_head 245 | * @source http://old.nabble.com/wp-_-enqueue-_-script%28%29-not-working-while-in-the-Loop-td26818198.html 246 | */ 247 | function add_headers() { 248 | //ignore the examples below 249 | return; 250 | 251 | if(is_admin()) return; 252 | 253 | $stylesToAdd = array(); 254 | 255 | // Have to manually add to in_footer 256 | // Check if script is done, if not, then add to footer 257 | foreach($stylesToAdd as $style){ 258 | if (!in_array($style, $wp_styles->done) && !in_array($style, $wp_styles->in_footer)) { 259 | $wp_styles->in_header[] = $style; 260 | } 261 | } 262 | }//-- function add_headers 263 | 264 | /** 265 | * Only add scripts and stuff if shortcode found on page 266 | * @see http://scribu.net/wordpress/optimal-script-loading.html 267 | */ 268 | function add_footers() { 269 | if(is_admin()){ 270 | wp_enqueue_script($this->N('admin')); 271 | return; 272 | } 273 | 274 | $scriptsToAdd = array( ); 275 | 276 | // Have to manually add to in_footer 277 | // Check if script is done, if not, then add to footer 278 | foreach($scriptsToAdd as $script){ 279 | if (!in_array($script, $wp_scripts->done) && !in_array($script, $wp_scripts->in_footer)) { 280 | $wp_scripts->in_footer[] = $script; 281 | } 282 | } 283 | } 284 | 285 | #endregion =============== HEADER/FOOTER -- scripts and styles =============== 286 | 287 | #region =============== Administrative Settings ======== 288 | 289 | private $_settings; 290 | private $_services; 291 | /** 292 | * Return the plugin settings 293 | */ 294 | function get_settings($stashed = true){ 295 | // TODO: if this ever changes, make sure to correspondingly fix 'upgrade.php' 296 | 297 | if( $stashed && isset($this->_settings) ) return $this->_settings; 298 | 299 | $this->_settings = get_option($this->N('settings')); 300 | // but we only want the actual settings, not the services 301 | $this->_settings = $this->_settings['debug']; 302 | 303 | return $this->_settings; 304 | }//--- get_settings 305 | /** 306 | * Return the service configurations 307 | */ 308 | function get_services($stashed = true) { 309 | if( $stashed && isset($this->_services) ) return $this->_services; 310 | 311 | $this->_services = get_option($this->N('settings')); 312 | // but we only want service listing, not the settings 313 | // TODO: this will go away once we move to custom post type like CF7 314 | unset($this->_services['debug']); 315 | 316 | return $this->_services; 317 | } 318 | function save_services($services) { 319 | $settings = $this->get_settings(false); 320 | $merged = array('debug' => $settings) + (array)$services; 321 | update_option($this->N('settings'), $merged); 322 | $this->_services = $services; // replace stash 323 | } 324 | function save_settings($settings) { 325 | $services = $this->get_services(false); 326 | $merged = array('debug' => $settings) + (array)$services; 327 | update_option($this->N('settings'), $merged); 328 | $this->_settings = $settings; // replace stash 329 | } 330 | 331 | /** 332 | * The submenu page 333 | */ 334 | function submenu_config(){ 335 | wp_enqueue_script($this->N('admin')); 336 | include_once('plugin-ui.php'); 337 | } 338 | 339 | /** 340 | * HOOK - Add the "Settings" link to the plugin list entry 341 | * @param $links 342 | * @param $file 343 | */ 344 | function plugin_action_links( $links, $file ) { 345 | if ( $file != plugin_basename( __FILE__ ) ) 346 | return $links; 347 | 348 | $url = $this->plugin_admin_url( array( 'page' => basename(__FILE__, '.php').'-config' ) ); 349 | 350 | $settings_link = '' 351 | . esc_html( __( 'Settings', $this->N ) ) . ''; 352 | 353 | array_unshift( $links, $settings_link ); 354 | 355 | return $links; 356 | } 357 | 358 | /** 359 | * Copied from Contact Form 7, for adding the plugin link 360 | * @param unknown_type $query 361 | */ 362 | function plugin_admin_url( $query = array() ) { 363 | global $plugin_page; 364 | 365 | if ( ! isset( $query['page'] ) ) 366 | $query['page'] = $plugin_page; 367 | 368 | $path = 'admin.php'; 369 | 370 | if ( $query = build_query( $query ) ) 371 | $path .= '?' . $query; 372 | 373 | $url = admin_url( $path ); 374 | 375 | return esc_url_raw( $url ); 376 | } 377 | 378 | /** 379 | * Helper to render a select list of available forms 380 | * @param array $forms list of forms from functions like wpcf7_contact_forms() 381 | * @param array $eid entry id - for multiple lists on page 382 | * @param array $selected ids of selected fields 383 | * @param string $field optionally specify the field name 384 | */ 385 | public function form_select_input($forms, $eid, $selected, $field = 'forms'){ 386 | ?> 387 | 400 | selected_input($array[$currentKey], $expected, $type) 416 | : $this->selected_input(array(), $expected, $type); 417 | } 418 | #endregion =============== Administrative Settings ======== 419 | 420 | 421 | /** 422 | * Prepare the service post with numerical placeholder, see github issue #43 423 | * @param $post the service submission 424 | * 425 | * @see https://github.com/zaus/forms-3rdparty-integration/issues/43 426 | */ 427 | function placeholder_separator($post) { 428 | $new = array(); // add results to new so we don't pollute the enumerator 429 | // find the arrays and reformat keys with index 430 | 431 | ###_log(__FUNCTION__ . '@' . __LINE__, $post); 432 | 433 | foreach($post as $f => $v) { 434 | // do we have a placeholder to fix for an array (iss #43) 435 | if(is_array($v) && false !== strpos($f, '%i')) { 436 | // for each item in the submission array, 437 | // get its numerical index and replace the 438 | // placeholder in the destination field 439 | 440 | foreach($v as $i => $p) { 441 | $k = str_replace('%i', $i, $f); 442 | $new[$k] = $p; 443 | } 444 | 445 | unset($post[$f]); // now remove original, since we need to reattach under a different key 446 | } 447 | } 448 | 449 | ###_log(__FUNCTION__ . '@' . __LINE__, $new); 450 | 451 | return array_merge($post, $new); 452 | } 453 | 454 | /** 455 | * Callback to perform before Form (i.e. Contact-Form-7, Gravity Forms) fires 456 | * @param $form the plugin form 457 | * @param $submission alias to submission data - in GF it's $_POST, in CF7 it's $cf7->posted_data; initially a flag (if not provided) to only formulate the submission once 458 | * 459 | * @see http://www.alexhager.at/how-to-integrate-salesforce-in-contact-form-7/ 460 | */ 461 | function before_send($form, $submission = array()){ 462 | ###_log(__LINE__.':'.__FILE__, ' begin before_send', $form); 463 | $submissionInit = false; // only get the submission once when that we know we're going to use this service/form 464 | 465 | //get field mappings and settings 466 | $services = $this->get_services(); 467 | 468 | // unlikely, but skip handling if nothing to do 469 | if(empty($services)) return $form; 470 | 471 | $debug = $this->get_settings(); 472 | 473 | //loop services 474 | foreach($services as $sid => $service): 475 | $use_this_form = $this->use_form($form, $service, $sid); 476 | if(!$use_this_form) continue; 477 | 478 | // only get the submission once, now that we know we're going to use this service/form 479 | if(false === $submissionInit) { 480 | $submission = apply_filters($this->N('get_submission'), $submission, $form, $service); 481 | $submissionInit = true; 482 | } 483 | 484 | // now we can conditionally check whether use the service based upon submission data 485 | $use_this_form = apply_filters($this->N('use_submission'), $use_this_form, $submission, $sid); 486 | if( !$use_this_form ) continue; 487 | 488 | 489 | // populate the 3rdparty post args 490 | $sendResult = $this->send($submission, $form, $service, $sid, $debug); 491 | if($sendResult === self::RET_SEND_STOP) break; 492 | elseif($sendResult === self::RET_SEND_SKIP) continue; 493 | 494 | $response = $sendResult['response']; 495 | $post_args = $sendResult['post_args']; 496 | 497 | $form = $this->handle_results($submission, $response, $post_args, $form, $service, $sid, $debug); 498 | endforeach; //-- loop services 499 | 500 | ###_log(__LINE__.':'.__FILE__, ' finished before_send', $form); 501 | 502 | // some plugins expected usage is as filter, so return (modified?) form 503 | return $form; 504 | }//--- end function before_send 505 | 506 | const RET_SEND_SKIP = -1; 507 | const RET_SEND_STOP = -2; 508 | const RET_SEND_OKAY = 1; 509 | 510 | /** 511 | * Check for the given service if we're supposed to use it with this form 512 | * @param $form 513 | * @param $service 514 | * @param $sid 515 | * @return bool whether to skip or not 516 | */ 517 | public function use_form($form, $service, $sid) { 518 | //check if we're supposed to use this service 519 | if( !isset($service['forms']) || empty($service['forms']) ) return false; // nothing provided 520 | 521 | // it's more like "use_this_service", actually... 522 | $use_this_form = apply_filters($this->N('use_form'), false, $form, $sid, $service['forms']); 523 | 524 | ###_log('are we using this form?', $use_this_form ? "YES" : "NO", $sid, $service); 525 | 526 | return $use_this_form; 527 | } 528 | 529 | /** 530 | * Create and perform the 3rdparty submission 531 | * @param $submission user input submission 532 | * @param $form the plugin form source 533 | * @param $service current service being sent 534 | * @param $sid service id 535 | * @param $debug debug settings 536 | * @return array|int either [response, post_args] or an interrupt value like @see RET_SEND_SKIP 537 | */ 538 | public function send($submission, $form, $service, $sid, $debug) { 539 | $post = array(); 540 | 541 | if(!isset($service['delim']) || empty($service['delim'])) 542 | $service['delim'] = $debug['separator']; // alias here for reporting and using default 543 | 544 | //find mapping 545 | foreach($service['mapping'] as $mid => $mapping){ 546 | $third = $mapping[self::PARAM_3RD]; 547 | 548 | //is this static or dynamic (userinput)? 549 | if(v($mapping['val'])){ 550 | $input = $mapping[self::PARAM_SRC]; 551 | } 552 | else { 553 | //check if we have that field in post data 554 | if( !isset($submission[ $mapping[self::PARAM_SRC] ]) ) continue; 555 | 556 | $input = $submission[ $mapping[self::PARAM_SRC] ]; 557 | } 558 | 559 | //allow multiple values to attach to same entry 560 | if( isset( $post[ $third ] ) ){ 561 | ###echo "multiple @$mid - $fsrc, $third :=\n"; 562 | 563 | if(!is_array($post[$third])) { 564 | $post[$third] = array($post[$third]); 565 | } 566 | $post[$third] []= $input; 567 | } 568 | else { 569 | $post[$third] = $input; 570 | } 571 | }// foreach mapping 572 | 573 | //extract special tags; 574 | $post = apply_filters($this->N('service_filter_post_'.$sid), $post, $service, $form, $submission); 575 | $post = apply_filters($this->N('service_filter_post'), $post, $service, $form, $sid, $submission); 576 | 577 | // fix for multiple values 578 | switch($service['delim']) { 579 | case '[#]': 580 | // don't do anything to include numerical index (default behavior of `http_build_query`) 581 | break; 582 | case '[%]': 583 | // see github issue #43 584 | $post = $this->placeholder_separator($post); 585 | break; 586 | case '[]': 587 | // must build as querystring then strip `#` out of `[#]=` 588 | $post = http_build_query($post); 589 | $post = preg_replace('/%5B[0-9]+%5D=/', '%5B%5D=', $post); 590 | break; 591 | default: 592 | // special case: newlines were escaped 593 | if($service['delim'] == '\\r\\n') $service['delim'] = "\r\n"; 594 | elseif($service['delim'] == '\\n') $service['delim'] = "\n"; 595 | 596 | // otherwise, find the arrays and implode 597 | foreach($post as $f => &$v) { 598 | ###_log('checking array', $f, $v, is_array($v) ? 'array' : 'notarray'); 599 | 600 | if(is_array($v)) $v = implode($service['delim'], $v); 601 | } 602 | break; 603 | } 604 | 605 | ###_log(__LINE__.':'.__FILE__, ' sending post to '.$service['url'], $post); 606 | 607 | // change args sent to remote post -- add headers, etc: http://codex.wordpress.org/Function_Reference/wp_remote_post 608 | // optionally, return an array with 'response_bypass' set to skip the wp_remote_post in favor of whatever you did in the hook 609 | $post_args = apply_filters($this->N('service_filter_args') 610 | , array( 611 | 'timeout' => empty($service['timeout']) ? self::DEFAULT_TIMEOUT : $service['timeout'] 612 | ,'body'=>$post 613 | ) 614 | , $service 615 | , $form 616 | ); 617 | 618 | //remote call 619 | 620 | // once more conditionally check whether use the service based upon (mapped) submission data 621 | if(false === $post_args) return self::RET_SEND_SKIP; 622 | // optional bypass -- replace with a SOAP call, etc 623 | elseif(isset($post_args['response_bypass'])) { 624 | $response = $post_args['response_bypass']; 625 | } 626 | else { 627 | //@see http://planetozh.com/blog/2009/08/how-to-make-http-requests-with-wordpress/ 628 | 629 | // allow hooks to modify the URL with submission, like send as url-encoded XML, etc 630 | $url = apply_filters($this->N('service_filter_url'), $service['url'], $post_args); 631 | 632 | $response = isset($service['method']) && $service['method'] == 'get' 633 | ? wp_remote_get($url, $post_args) 634 | : wp_remote_post($url, $post_args) 635 | ; 636 | } 637 | 638 | ###pbug(__LINE__.':'.__FILE__, ' response from '.$service['url'], $response); 639 | ### _log(__LINE__.':'.__FILE__, ' response from '.$service['url'], $submission, $post_args, $response); 640 | 641 | return array('response' => $response, 'post_args' => $post_args); 642 | }//-- fn send 643 | 644 | /** 645 | * Interpret and respond accordingly to the post results 646 | * 647 | * @param $submission 648 | * @param $response 649 | * @param $post_args 650 | * @param $form 651 | * @param $service 652 | * @param $sid 653 | * @param $debug 654 | */ 655 | public function handle_results($submission, $response, $post_args, $form, $service, $sid, $debug) { 656 | $can_hook = true; 657 | //if something went wrong with the remote-request "physically", warn 658 | if (!is_array($response)) { //new occurrence of WP_Error????? 659 | $response_array = array('safe_message'=>'error object', 'object'=>$response); 660 | $form = $this->on_response_failure($form, $debug, $service, $post_args, $response_array); 661 | $can_hook = false; 662 | } 663 | elseif(!$response 664 | || !isset($response['response']) 665 | || !isset($response['response']['code']) 666 | || ! apply_filters($this->N('is_success'), 200 <= $response['response']['code'] && $response['response']['code'] < 400, $response, $service) 667 | ) { 668 | $response['safe_message'] = 'physical request failure'; 669 | $form = $this->on_response_failure($form, $debug, $service, $post_args, $response); 670 | $can_hook = false; 671 | } 672 | //otherwise, check for a success "condition" if given 673 | elseif(!empty($service['success'])) { 674 | if(strpos($response['body'], $service['success']) === false){ 675 | $failMessage = array( 676 | 'reason'=>'Could not locate success clause within response' 677 | , 'safe_message' => 'Success Clause not found' 678 | , 'clause'=>$service['success'] 679 | , 'response'=>$response['body'] 680 | ); 681 | $form = $this->on_response_failure($form, $debug, $service, $post_args, $failMessage); 682 | $can_hook = false; 683 | } 684 | } 685 | 686 | if($can_hook){ 687 | ###_log('performing hooks for:', $this->N.'_service_'.$sid); 688 | 689 | //hack for pass-by-reference 690 | //holder for callback return results 691 | $callback_results = array('success'=>false, 'errors'=>false, 'attach'=>'', 'message' => '', 'redirect' => ''); 692 | // TODO: use object? 693 | $param_ref = array(); foreach($callback_results as $k => &$v){ $param_ref[$k] = &$v; } 694 | 695 | //allow hooks 696 | do_action($this->N('service_a'.$sid), $response['body'], $param_ref); 697 | do_action($this->N('service'), $response['body'], $param_ref, $sid); 698 | 699 | ###_log('after success', $form); 700 | 701 | //check for callback errors; if none, then attach stuff to message if requested 702 | if(!empty($callback_results['errors'])){ 703 | $failMessage = array( 704 | 'reason'=>'Service Callback Failure' 705 | , 'safe_message' => 'Service Callback Failure' 706 | , 'errors'=>$callback_results['errors']); 707 | $form = $this->on_response_failure($form, $debug, $service, $post_args, $failMessage); 708 | } 709 | else { 710 | ###_log('before remote_success', $callback_results, $sid); 711 | $form = apply_filters($this->N('remote_success'), $form, $callback_results, $service); 712 | } 713 | }// can hook 714 | 715 | ### _log(__FUNCTION__, $debug, strpos($debug['mode'], 'debug')); 716 | 717 | //forced debug contact; support legacy setting too 718 | if(isset($debug['mode']) && ($debug['mode'] == 'debug' || in_array('debug', $debug['mode'])) ) { 719 | // TMI with new WP_HTTP_Requests_Response object 720 | if( !is_a($response, 'WP_Error') && isset($response['http_response']) && is_object($response['http_response']) ) $response = $response['http_response']; 721 | 722 | $this->send_debug_message($debug, $service, $post_args, $response, $submission); 723 | } 724 | 725 | return $form; 726 | } 727 | 728 | /** 729 | * How to send the debug message 730 | * @param string $debug debug options -- 'email' and 'sender' 731 | * @param array $service service options 732 | * @param array $post details sent to 3rdparty 733 | * @param array|object $response the response object 734 | * @param object $submission the form submission 735 | * @return void n/a 736 | */ 737 | private function send_debug_message($debug, $service, $post, $response, $submission){ 738 | // allow hooks to bypass, if for example we're not getting debug emails or we want to use some fancy logging service 739 | $passthrough = apply_filters($this->N('debug_message'), true, $service, $post, $submission, $response, $debug); 740 | 741 | // not all hosting services allow arbitrary emails 742 | $sendAs = isset($debug['sender']) && !empty($debug['sender']) 743 | ? $debug['sender'] 744 | : get_bloginfo('admin_email'); //'From: "'.self::pluginPageTitle.' Debug" <'.$this->N.'-debug@' . str_replace('www.', '', $_SERVER['HTTP_HOST']) . '>'; 745 | $recipients = isset($debug['email']) && !empty($debug['email']) 746 | ? str_replace(';', ',', $debug['email']) 747 | : get_bloginfo('admin_email'); 748 | 749 | // allow hook to alter dump truncation 750 | $debug_truncation = apply_filters($this->N('debug_truncation'), array( 751 | 'service' => 5000, 752 | 'submission' => 200, 753 | 'post' => 2000, 754 | 'response' => 1000 755 | )); 756 | 757 | // did the debug message send? 758 | if( !$passthrough || !wp_mail( $recipients 759 | , self::pluginPageTitle . " Debug: {$service['name']}" 760 | , "*** Service ***\n".self::dump($service, $debug_truncation['service'])."\n*** Post (Form) ***\n" . get_bloginfo('url') . $_SERVER['REQUEST_URI'] . "\n".self::dump($submission, $debug_truncation['submission'])."\n*** Post (to Service) ***\n".self::dump($post, $debug_truncation['post'])."\n*** Response ***\n".self::dump($response, $debug_truncation['response']) 761 | , array($sendAs) 762 | ) ) { 763 | ///TODO: log? another email? what? 764 | error_log( sprintf("%s:%s could not send F3P debug email (to: %s) for service %s", __LINE__, __FILE__, $recipients, $service['url']) ); 765 | 766 | if(in_array('log', $debug['mode'])) { 767 | $log = array( 768 | 'sendAs' => $sendAs, 769 | 'recipients' => $recipients, 770 | 'submission' => $submission, 771 | 'post' => $post, 772 | 'response' => $response 773 | ); 774 | 775 | if(in_array('full', $debug['mode'])) $log['service'] = $service; 776 | 777 | $as_json = is_array($post['body']) && isset($post['body']['_json']) && $post['body']['_json']; 778 | $dump = $as_json 779 | ? wp_json_encode($log, defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0) 780 | : print_r($log, true); 781 | error_log( __CLASS__ . ':: ' . $dump ); 782 | } 783 | } 784 | } 785 | 786 | /** 787 | * Pretty-print something without dumping too much 788 | */ 789 | public static function dump($array, $length_limit = 200, $ignores = array(), $depth = '', $isBuffer = true) { 790 | // maybe catch (if not recursive) 791 | if($isBuffer) ob_start(); 792 | 793 | // trick loop 794 | $array = (array) $array; 795 | 796 | foreach($array as $k => $v) { 797 | // maybe ignore? 798 | if(in_array($k, $ignores)) continue; 799 | 800 | // dump key 801 | echo "{$depth}[{$k}] => "; 802 | 803 | // dump value or recurse 804 | if(is_array($v) || is_object($v)) { 805 | echo is_array($v) ? 'Array (' : 'Object ('; 806 | echo "\n"; 807 | 808 | self::dump($v, $length_limit, $ignores, $depth . "\t", false); 809 | 810 | echo "$depth)"; 811 | } 812 | else if(is_string($v) && strlen($v) > $length_limit) 813 | echo substr($v, 0, $length_limit), '...'; 814 | else 815 | echo $v; 816 | 817 | echo "\n"; 818 | } 819 | 820 | // maybe uncatch (if not recursive) 821 | if($isBuffer) { 822 | $output = ob_get_contents(); 823 | ob_end_clean(); 824 | return $output; 825 | } 826 | } 827 | 828 | 829 | /** 830 | * Add a javascript warning for failures; also send an email to debugging recipient with details 831 | * parameters passed by reference mostly for efficiency, not actually changed (with the exception of $form) 832 | * 833 | * @param $form object CF7 plugin object - contains mail details etc 834 | * @param $debug array this plugin "debug" option array 835 | * @param $service array service settings 836 | * @param $post_args array service post data 837 | * @param $response mixed|object remote-request response 838 | * @return mixed|object|void the updated $form 839 | */ 840 | private function on_response_failure($form, $debug, $service, $post_args, $response){ 841 | // failure hooks; pass-by-value 842 | 843 | $form = apply_filters($this->N('remote_failure'), $form, $debug, $service, $post_args, $response); 844 | 845 | return $form; 846 | }//--- end function on_response_failure 847 | 848 | 849 | /** 850 | * Email helper for `remote_failure` hooks 851 | * @param array $service service configuration 852 | * @param array $debug the debug settings (to get email) 853 | * @param array $post details sent to 3rdparty 854 | * @param array $response remote-request response 855 | * @param string $form_title name of the form used 856 | * @param string $form_recipient email of the original form recipient, so we know who to follow up with 857 | * @param debug_from_id short identifier of the Form plugin which failed (like 'CF7' or 'GF', etc) 858 | * 859 | * @return true if the warning email sent, false otherwise 860 | */ 861 | public function send_service_error(&$service, &$debug, &$post, &$response, $form_title, $form_recipient, $debug_from_id) { 862 | $body = sprintf('There was an error when trying to integrate with the 3rd party service {%2$s} (%3$s).%1$s%1$s**FORM**%1$sTitle: %6$s%1$sIntended Recipient: %7$s%1$sSource: %8$s%1$s%1$s**SUBMISSION**%1$s%4$s%1$s%1$s**RAW RESPONSE**%1$s%5$s' 863 | , "\n" 864 | , $service['name'] 865 | , $service['url'] 866 | , print_r($post, true) 867 | , print_r($response, true) 868 | , $form_title 869 | , $form_recipient 870 | , get_bloginfo('url') . $_SERVER['REQUEST_URI'] 871 | ); 872 | $subject = sprintf('%s-3rdParty Integration Failure: %s' 873 | , $debug_from_id 874 | , $service['name'] 875 | ); 876 | $headers = array( 877 | sprintf('From: "%1$s-3rdparty Debug" <%1$s-3rdparty-debug@%2$s>' 878 | , $debug_from_id 879 | , str_replace('www.', '', $_SERVER['HTTP_HOST']) 880 | ) 881 | ); 882 | 883 | //log if couldn't send debug email 884 | if(wp_mail( $debug['email'], $subject, $body, $headers )) return true; 885 | 886 | ###$form->additional_settings .= "\n".'on_sent_ok: \'alert("Could not send debug warning '.$service['name'].'");\''; 887 | error_log(__LINE__.':'.__FILE__ .' response failed from '.$service['url'].', could not send warning email: ' . print_r($response, true)); 888 | return false; 889 | }//-- fn send_service_error 890 | 891 | 892 | /** 893 | * Format the configured service failure message using the $response "safe message" and the plugin's original error message 894 | * @param array $service service configuration 895 | * @param array $response remote-request response 896 | * @param string $original_message the plugin's original failure message 897 | * @return the newly formatted failure message 898 | */ 899 | public function format_failure_message(&$service, &$response, $original_message) { 900 | return sprintf( 901 | __($service['failure'], $this->N()) 902 | , $original_message 903 | , __($response['safe_message'], $this->N()) 904 | ); 905 | } 906 | }//end class 907 | 908 | /* 909 | // some servers need at least one 'sacrificial' `error_log` call to make _log call work??? 910 | 911 | error_log('f3p-after-declare:' . $_SERVER["REQUEST_URI"]); 912 | 913 | if(!function_exists('_log')) { 914 | function _log($args) { 915 | $args = func_get_args(); 916 | error_log( print_r($args, true) ); 917 | } 918 | } 919 | */ 920 | -------------------------------------------------------------------------------- /i_plugin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaus/forms-3rdparty-integration/59a2a42473bbc1eb9846115fcc5333df7cf692da/i_plugin.png -------------------------------------------------------------------------------- /includes.php: -------------------------------------------------------------------------------- 1 | N('init') i.e. Forms3rdPartyIntegration::$instance->N('init') 23 | // IMPORTANT: protective checking - do the related modules exist? - http://codex.wordpress.org/Function_Reference/is_plugin_active 24 | if( ! function_exists('is_plugin_active') ) { include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); } 25 | 26 | // specific forms plugins 27 | 28 | // TODO: maybe make this optional via filter? 29 | // I'm assuming the overhead of checking if plugins exist is low, 30 | // but this could cause headaches if you have multiple form 31 | // plugins active, since 'use-form' checking isn't the best for differentiating 32 | 33 | if(is_plugin_active('contact-form-7/wp-contact-form-7.php') || class_exists('WPCF7_ContactForm') ) 34 | include('plugins/contactform7.php'); 35 | 36 | if(is_plugin_active('gravityforms/gravityforms.php') || class_exists('RGFormsModel') ) 37 | include('plugins/gravityforms.php'); 38 | 39 | if(is_plugin_active('ninja-forms/ninja-forms.php') || class_exists('Ninja_Forms') ) 40 | include('plugins/ninjaforms.php'); 41 | 42 | /* to add others, use something like: 43 | 44 | add_action( 'plugins_loaded', array('Forms3rdpartyIntegration_YOUR_FORM_PLUGIN', 'init') ); 45 | 46 | class Forms3rdpartyIntegration_YOUR_FORM_PLUGIN { 47 | // after plugins ready 48 | public static function init() { 49 | // hook way early 50 | add_action(Forms3rdPartyIntegration::$instance->N('init'), array(__CLASS__, '_include'), 1); 51 | } 52 | 53 | 54 | // actually start the class once Forms3rdparty is ready to include it 55 | public static function _include() { 56 | include('DERIVED_INSTANCE_OF_FPLUGIN_BASE.php'); 57 | } 58 | } 59 | 60 | */ -------------------------------------------------------------------------------- /plugin-ui.php: -------------------------------------------------------------------------------- 1 | N; 6 | 7 | if( isset($_POST[$P]) && check_admin_referer($P, $P.'_nonce') ) { 8 | $options = stripslashes_deep($_POST[$P]); 9 | 10 | // expected fields not really used... 11 | $expectedFields = array( 12 | 'name' 13 | , 'url' 14 | ,'mapping' 15 | , 'success' 16 | , 'failure' 17 | , 'forms' 18 | , 'timeout' 19 | ,self::PARAM_LBL 20 | ,self::PARAM_SRC 21 | ,self::PARAM_3RD 22 | ); 23 | #pbug($options); 24 | 25 | // update_option( $this->N('settings'), $options); 26 | $this->save_settings($options['debug']); 27 | $this->save_services($options); // technically, this will overwrite the settings section anyway... 28 | 29 | echo '

' . __('Settings saved.') . '

'; 30 | } 31 | else { 32 | $options = array('debug' => $this->get_settings()) + $this->get_services(); 33 | // get_option( $this->N('settings') 34 | } 35 | 36 | 37 | //prepare list of contact forms -- 38 | $forms = apply_filters($this->N('select_forms'), array()); 39 | 40 | $debugUrl = plugins_url('3rd-parties/service_test.php', __FILE__); 41 | 42 | ?> 43 |
44 | 45 |

46 |
47 |

.

48 |

.

49 |

Form plugin Field column', $P); ?>.

50 |
51 | 52 |
53 | 54 | 55 | 56 |
Global Values
57 | 62 |
63 | 64 | 65 | 66 |
67 |
68 | 69 | 70 | 71 |
72 |
73 | 74 | selected_input_array($debugOptions, 'mode', 'debug', 'checked'); ?> /> 75 | . 76 | 77 |
78 |
79 | 80 | selected_input_array($debugOptions, 'mode', 'log', 'checked'); ?> /> 81 | . 82 |
83 |
84 | 85 | selected_input_array($debugOptions, 'mode', 'full', 'checked'); ?> /> 86 | . 87 |
88 |
89 | 90 | 91 | . (?) 92 |
93 |
94 | 95 |
96 | '' 101 | , 'url'=>'' 102 | , 'success'=>'' 103 | , 'failure' => '' 104 | , 'forms' => array() 105 | , 'timeout' => self::DEFAULT_TIMEOUT 106 | , 'mapping' => array() 107 | )); 108 | } 109 | 110 | $eid = -1; // always increment to correct for weirdness 111 | foreach($options as $ekey => $entity): 112 | $eid++; 113 | ?> 114 |
115 |
116 |

3rd-Party Service:

117 | 118 |
119 | 120 |
Service 121 |
122 |
123 | 124 | 125 |
126 | 127 |
128 | 129 | 130 | action attribute of the 3rd-party form. See Debug Mode setting for a working test url.', $P), '#dbg-debugmode', $debugUrl);?> 131 |
132 | 133 |
134 | N('submit_methods'), array( 136 | 'post' => 'Post', 137 | 'get' => 'Get' 138 | ), $eid, $P, $entity); 139 | if(empty($entity['method'])) $entity['method'] = 'post'; 140 | foreach($methods as $mkey => $mlabel): 141 | ?> 142 | /> 143 | 144 | 145 | 146 |
147 | 148 |
149 | 150 | form_select_input($forms, $eid, isset($entity['forms']) ? $entity['forms'] : ''); 153 | ?> 154 | CTRL to (un)select more than one');?>. 155 |
156 | 157 |
158 | 159 | 160 | . . 161 | . 162 |
163 |
164 | 165 | 166 | service fails', $P); ?>. 167 | %2$s) and/or the original plugin message (%1$s)', $P);?>. . 168 | sprintf($mask, $original_message, $nice_message)', $P);?>. 169 |
170 |
171 | 172 | 173 | . 174 |
175 | 176 |
177 | 178 | 179 | . 180 |
181 |
182 |
183 | 184 | N('service_settings'), $eid, $P, $entity); 186 | ?> 187 | 188 |
Mapping 189 | 190 | 193 | 194 | 195 | 196 | 200 | 204 | 208 | N('service_mappings_headers'), $eid, $P, $entity); 210 | ?> 211 | 212 | 213 | 214 | 215 | '' 223 | , self::PARAM_LBL => '' 224 | , self::PARAM_SRC => '' 225 | , self::PARAM_3RD => '' 226 | )); 227 | } 228 | 229 | $pairNum = 0; //always increments correctly? 230 | foreach($entity['mapping'] as $k => $pair): 231 | ?> 232 | 233 | 237 | 241 | 245 | 249 | N('service_mappings_values'), $eid, $pairNum, $P, $entity); 251 | ?> 252 | 263 | 264 | 267 | 268 |
191 | * Note that the label is just for you to remind yourself what the mapping is for, and does not do anything.
192 | Also, if you accidentally delete all of the mapping fields, try deleting the Service entry and refreshing the page, then adding another Service.', $P);?>
197 | * 198 |

199 |
201 | 202 |

or the value to submit if \'Is Value\' is checked', $P);?>

203 |
205 | 206 |

207 |
234 | 235 | /> 236 | 238 | 239 | 240 | 242 | 243 | 244 | 246 | 247 | 248 | 253 | 254 | 258 | 259 | 262 |
269 |
270 | 271 |
272 |
Hooks 273 |
274 |
275 |

The following are examples of action callbacks and content filters you can use to customize this service.

276 |

Add them to your functions.php or another plugin.

277 |
278 |
279 |
283 |
284 |
288 |
289 |
290 |
291 | 292 | Delete Service 293 | Copy Service 294 | Add Another Service 295 | 296 | 297 |
298 | 299 |
300 |
301 | N('service_metabox'), $P, $options); 305 | ?> 306 | 307 |
308 | 309 |
310 | 311 | 312 | Add Another Service 313 |
314 | 315 |
316 | 317 | N('service_metabox_after'), $P, $options); 319 | ?> 320 | 321 | 322 |
323 |
324 |

Examples of callback hooks.

325 |
326 | 327 |
328 |

You can also see examples in the plugin folder 3rd-Parties.

329 |

Action

330 |
331 | /**
332 |  * Callback hook for 3rd-party service XYZ
333 |  * @param $response the remote-request response (in this case, it's a serialized string)
334 |  * @param $results the callback return results (passed by reference since function can't return a value; also must be "constructed by reference"; see plugin)
335 |  */
336 | public function service1_action_callback($response, $results){
337 | 	try {
338 | 		// do something with $response
339 | 		
340 | 		// set return results - text to attach to the end of the email
341 | 		$results['attach'] = $output;
342 | 		
343 | 		///add_filter('wpcf7_mail_components', (&$this, 'filter_'.__FUNCTION__));
344 | 	} catch(Exception $ex){
345 | 		// indicate failure by adding errors as an array
346 | 		$results['errors'] = array($ex->getMessage());
347 | 	}
348 | }//--	function service1_action_callback
349 | 				
350 | 351 |

Filter

352 |
353 | /**
354 |  * Apply filters to integration fields
355 |  * so that you could say "current_visitor={IP}" and dynamically retrieve the visitor IP
356 |  * @see http://codex.wordpress.org/Function_Reference/add_filter
357 |  * 
358 |  * @param $values array of post values
359 |  * @param $service reference to service detail array
360 |  * @param $cf7 reference to Contact Form 7 object
361 |  */
362 | public function service1_filter_callback($values, $service, $cf7){
363 | 	foreach($values as $field => &$value):
364 | 		//filter depending on field
365 | 		switch($field){
366 | 			case 'filters':
367 | 				//look for placeholders, replace with stuff
368 | 				$orig = $value;
369 | 				if(strpos($value, '{IP}') !== false){
370 | 					$headers = apache_request_headers(); 
371 | 					$ip = isset($headers['X-Forwarded-For']) ? $headers['X-Forwarded-For'] : $_SERVER['REMOTE_ADDR'];
372 | 					$value = str_replace('{IP}', $ip, $value);
373 | 				}
374 | 				break;
375 | 		}
376 | 	endforeach;
377 | 	
378 | 	return $values;
379 | }//--	function multitouch1_filter
380 | 				
381 |
382 | 383 |
384 |
385 |
386 | 387 | 397 | 398 |
-------------------------------------------------------------------------------- /plugin.admin.css: -------------------------------------------------------------------------------- 1 | .invisible { position:absolute; left:-9999px; } 2 | 3 | form { margin-bottom: 2em; } 4 | 5 | div.shortcode-description { border:1px solid #DFDFDF; border-radius:5px; background:#fff; } 6 | 7 | .collapsed .inside { display:none; } 8 | div.meta-box .collapsed { position:relative; } 9 | div.meta-box h4 { padding-left:1em; } 10 | div.meta-box.collapsed h4, div.meta-box .collapsed h4 { position:absolute; top:-1em; right:20%; } 11 | 12 | pre { white-space:pre-wrap; padding:2px; margin:0.5em; border:1px dotted #ddd; border-radius:4px; background-color:#FCFCFC; } 13 | 14 | .shortcode-description legend { font-weight:bold; } 15 | .shortcode-description fieldset { border:1px dotted #ccc; clear:both; padding:1em; margin-bottom:1em; background-color:rgb(234, 239, 245); } 16 | 17 | div.field { padding-bottom:0.75em; } 18 | div.field .text { width:50%; } 19 | div.field em { display:block; margin-left:4em; margin-top:0.5em; } 20 | 21 | table.mappings { width:100%; margin:1em 0.5em; border-bottom:1px solid #ccc; border-collapse:collapse; } 22 | table.mappings td, table.mappings th { border:1px solid #ccc; } 23 | table.mappings th .descr { font-weight:normal; font-size:75%; } 24 | table.mappings caption { font-style:italic; color:#888; margin-bottom:0.2em; } 25 | table.mappings input, table.mappings textarea, table.mappings select { width:100%; } 26 | table.mappings .thin { width:4em; } 27 | table.mappings .drag-handle { padding:0.5em;} 28 | /* sortable */ 29 | .ui-state-highlight { height: 2.5em; line-height: 1.2em; } 30 | 31 | a.actn { text-decoration: none; } 32 | 33 | td.drag-handle { cursor:move; } 34 | 35 | .icon a { height:16px; width:16px; margin:0px; padding:0px; display:inline-block; text-indent:-9999px; background:url(sprites.png) no-repeat 0 0; } 36 | .icon a.minus { background-position:-16px -80px; } .icon a.minus:hover { background-position:0px -80px; } 37 | .icon a.plus { background-position:-16px -64px; } .icon a.plus:hover { background-position:0px -64px; } 38 | .icon a.trash { background-position:-16px -112px; } .icon a.trash:hover { background-position:0px -112px; } 39 | 40 | 41 | /* overrides */ 42 | #wpcontent select.multiple { height: 6em; } 43 | -------------------------------------------------------------------------------- /plugin.admin.js: -------------------------------------------------------------------------------- 1 | (function($){ 2 | var lib = { 3 | id: function() { 4 | /// generate a "unique" id 5 | 6 | // "GUID" (Math.random()*16|0).toString(16) // http://stackoverflow.com/a/2117523/1037948 7 | return (Date.now || function() { return +new Date; })(); // http://stackoverflow.com/a/221357/1037948 8 | }//-- fn id 9 | , 10 | action: function(e) { 11 | /// Do what the button says, usually involving a target and an after-effect 12 | 13 | var $o = $(this) 14 | , target = $o.data('target') 15 | , $target = target ? $(target) : $o.closest( $o.data('rel') ) //get the target off the link "rel", and find it from the parent-chain 16 | , after = $o.data('after') 17 | , action = $o.data('actn') 18 | ; 19 | 20 | // toggling not handled by link, so allow...not the cleanest, maybe add another data- ? 21 | if(action && action.indexOf('toggle') < 0) e.preventDefault(); 22 | 23 | switch(action) { 24 | case 'add': lib.clone($target, after, true); break; 25 | case 'clone': lib.clone($target, after); break; 26 | case 'remove': lib.remove($target, after); break; 27 | case 'toggle': 28 | case 'toggle-sibling': lib.toggle($target, after, action); break; 29 | } 30 | 31 | }//-- fn action 32 | , 33 | clone: function($target, after, clear) { 34 | var $clone = $target.clone(); //clone the target so we can add it later 35 | 36 | // perform requested post-operation 37 | lib.afterClone[after]($target, $clone, lib.id(), clear); 38 | 39 | //some more row properties 40 | $clone.toggleClass('alt'); 41 | 42 | //add the clone after the target 43 | $target.after( $clone ); 44 | }//-- fn clone 45 | , 46 | remove: function($target, after) { 47 | $target.empty().remove(); 48 | //lib.afterRemove[after]($target); 49 | }//-- fn remove 50 | , 51 | toggle: function($container, after, action) { 52 | /// toggle the given container, or maybe a sibling, depending on the action 53 | var $target = action == 'toggle' ? $container : $container.find(after); 54 | 55 | $target.toggleClass('collapsed'); 56 | } 57 | , 58 | afterClone: { 59 | row: function($target, $clone, newid) { 60 | lib.updateClonedRow(newid, $clone, /(mapping\]\[)([\d]+)/, true); 61 | }//-- fn afterClone.row 62 | , 63 | metabox: function($target, $clone, newid, clear) { 64 | //delete extra rows, fix title 65 | if(clear) $clone.find('tr.fields').slice(1).empty().remove(); // only save the first row 66 | var $serviceName = $clone.find('input:first'); 67 | $serviceName.val($serviceName.val() + ' Copy'); 68 | var $title = $clone.find('h3 span:last'); 69 | $title.html( clear ? $title.html().split(':')[0] : $title.html() + ' Copy' ) 70 | // and reapply data 71 | .parent() 72 | .data('actn', "toggle") 73 | .data('rel', ".postbox"); 74 | // also reapply toggle behavior to each subsection 75 | $clone.find('legend.hndle') 76 | .data('actn', "toggle") 77 | .data('rel', ".postbox"); 78 | 79 | // toggle hooks appropriately -- since the next call will 'reset' all fields, force collapsed 80 | $clone.find('.hook-example').addClass('collapsed'); 81 | 82 | //reset clone values and update indices 83 | lib.updateClonedRow(newid, $clone, /(\[)([\d])/, clear); 84 | }//-- fn afterClone.metabox 85 | }//-- afterClone 86 | , 87 | updateClonedRow: function(newid, $clone, regex, clear) { 88 | //reset clone values and update indices 89 | $clone.find('input,select,textarea').each(function(i, o){ 90 | var $o = $(o) 91 | , id = $o.attr('id').split('-') 92 | , name = $o.attr('name') 93 | ; 94 | 95 | $o.attr('id', id[0]+'-'+newid); 96 | $o.attr('name', name.replace(regex, '$1' + newid)); 97 | 98 | //reset values 99 | if(clear) { 100 | if( $o.attr('type') != 'checkbox' ){ 101 | $o.val(''); 102 | } 103 | else { 104 | $o.removeAttr('checked'); 105 | } 106 | } 107 | }); 108 | $clone.find('label').each(function(i, o){ 109 | var $o = $(o); 110 | 111 | //set the for equal to its closest input's id 112 | $o.attr('for', $o.siblings('input').attr('id')); 113 | }); 114 | }//-- fn lib.updateClonedRow 115 | }; 116 | 117 | $(function() { 118 | // setup elements 119 | var $plugin = $('#' + Forms3rdPartyIntegration_admin.N) 120 | , $metaboxes = $plugin.find('.meta-box') 121 | ; 122 | 123 | // clone / delete row or metabox, toggle container or sibling, etc 124 | $plugin.on('click', '.actn', lib.action); 125 | // checkbox 126 | $plugin.on('change', '.change-actn', lib.action); // custom target to avoid checkbox pitfall...ugh 127 | 128 | //collapse all metabox sections initially 129 | var $postbox = $plugin.find('.postbox').not(function(i) { return i < 1 }); 130 | 131 | $postbox 132 | .each(function(i,o) { 133 | var $me = $(o); 134 | if(! $me.hasClass('open') ) $me.addClass('collapsed'); 135 | 136 | $me.find('.hndle').first() 137 | .prepend('[' + ($(o).data('icon') || '+') + '] ') 138 | .addClass('actn') 139 | .data('actn', "toggle") 140 | .data('rel', ".postbox") 141 | ; 142 | }) 143 | ; 144 | //$postbox.first().removeClass('collapsed'); // not the debug section 145 | 146 | // sortable 147 | $plugin.find('table.mappings tbody').sortable({ 148 | // fix width -- http://www.foliotek.com/devblog/make-table-rows-sortable-using-jquery-ui-sortable/ 149 | helper: function(e, ui) { 150 | ui.children().each(function() { 151 | $(this).width($(this).width()); 152 | }); 153 | return ui; 154 | } 155 | , placeholder: "ui-state-highlight" 156 | }) 157 | .end() 158 | .find('div.meta-box-sortables').sortable({distance:30, tolerance:'pointer'}); 159 | 160 | }); 161 | })(jQuery); -------------------------------------------------------------------------------- /plugins/contactform7.php: -------------------------------------------------------------------------------- 1 | ID; /* WP Post */ } 38 | /** 39 | * Get the title/name from the plugin's form listing 40 | */ 41 | protected function GET_FORM_LIST_TITLE($list_entry) { return $list_entry->post_title; /* WP Post */ } 42 | 43 | /** 44 | * Get the ID from the form "object" 45 | */ 46 | protected function GET_FORM_ID($form) { return $form->id(); } 47 | /** 48 | * Get the title from the form "object" 49 | */ 50 | protected function GET_FORM_TITLE($form) { return $form->title(); } 51 | 52 | 53 | /** 54 | * Returns an array of the plugin's forms, loosely as ID => NAME; 55 | * will be reformatted into ID => NAME by @see GET_FORM_LIST_ID and @see GET_FORM_LIST_TITLE 56 | */ 57 | protected function GET_PLUGIN_FORMS() { 58 | // since they're stored as a custom post type 59 | return get_posts( array( 60 | 'numberposts' => -1, 61 | 'orderby' => 'ID', 62 | 'order' => 'ASC', 63 | 'post_type' => 'wpcf7_contact_form' ) ); 64 | } 65 | 66 | 67 | 68 | /** 69 | * Determine if the form "object" is from the expected plugin (i.e. check its type) 70 | */ 71 | protected function IS_PLUGIN_FORM($form) { 72 | return is_object($form) && 'WPCF7_ContactForm' == get_class($form); 73 | } 74 | 75 | /** 76 | * Get the posted data from the form (or POST, wherever it is) 77 | */ 78 | protected function GET_FORM_SUBMISSION($form) { 79 | $submission = WPCF7_Submission::get_instance(); 80 | if($submission) return $submission->get_posted_data(); 81 | return array('no submission'); 82 | } 83 | 84 | /** 85 | * How to attach the callback attachment for the indicated service (using `$this->attachment_heading` or `$this->attachment_heading_html` as appropriate) 86 | * @param $form the form "object" 87 | * @param $to_attach the content to attach 88 | * @param $service_name the name of the service to report in the header 89 | * @return $form, altered to contain the attachment 90 | */ 91 | protected function ATTACH($form, $to_attach, $service_name) { 92 | $mail = $form->prop('mail'); // previous style: $form->mail 93 | $mail['body'] .= "\n\n" . ( 94 | $mail['use_html'] 95 | ? $this->attachment_heading_html($service_name) 96 | : $this->attachment_heading($service_name) 97 | ) 98 | . $to_attach; 99 | $form->set_properties(array('mail'=>$mail)); 100 | 101 | return $form; // yes this is redundant when it's an object, but need it for compatibility with GF 102 | } 103 | 104 | /** 105 | * Insert new fields into the form's submission 106 | * @param $form the original form "object" 107 | * @param $newfields key/value pairs to inject 108 | * @return $form, altered to contain the new fields 109 | */ 110 | public function INJECT($form, $newfields) { 111 | 112 | // by the time we've hooked to CF7 it's too late to modify $_POST (within the Submission class) or use hook `wpcf7_posted_data` 113 | 114 | $submission = WPCF7_Submission::get_instance(); 115 | ### _log(__CLASS__, __FUNCTION__, $newfields, $submission); 116 | 117 | // TODO: how to merge? can't assign non-variable by reference? (using `&`) 118 | $data = $submission->get_posted_data(); 119 | // don't overwrite with empty values (but is that always appropriate?), see forms-3rdparty-inject-results#1 120 | # $data = $newfields + $data; 121 | foreach($newfields as $k => $v) { 122 | if(!empty($v)) $data[$k] = $v; 123 | } 124 | 125 | 126 | return $form; 127 | } 128 | 129 | /** 130 | * How to update the confirmation message for a successful result 131 | * @param $form the form "object" 132 | * @param $message the content to report 133 | * @return $form, altered to contain the message 134 | */ 135 | protected function SET_OKAY_MESSAGE($form, $message) { 136 | $messages = $form->prop('messages'); 137 | $messages['mail_sent_ok'] = $message; 138 | $form->set_properties(array('messages'=>$messages)); 139 | 140 | return $form; 141 | } 142 | 143 | /** 144 | * How to update the confirmation redirect for a successful result 145 | * @param $form the form "object" 146 | * @param $redirect the url to redirect to 147 | * @return $form, altered to contain the message 148 | */ 149 | protected function SET_OKAY_REDIRECT($form, $redirect) { 150 | $messages = $form->prop('messages'); 151 | $url = esc_url_raw( $redirect ); 152 | $messages['mail_sent_ok'] .= ""; 153 | $form->set_properties(array('messages'=>$messages)); 154 | 155 | return $form; 156 | } 157 | 158 | 159 | /** 160 | * Fetch the original error message for the form 161 | */ 162 | protected function GET_ORIGINAL_ERROR_MESSAGE($form) { 163 | // cheat -- because we're going to deal with multiple confirmation messages, 164 | // we'll use a placeholder here, and correctly format it later via sprintf if it's present 165 | return '%s'; 166 | // $messages = $form->prop('messages'); 167 | // return $messages['mail_sent_ng']; 168 | } 169 | 170 | /** 171 | * How to update the confirmation message for a failure/error 172 | * @param $form the form "object" 173 | * @param $message the content to report 174 | * @param $safe_message a short, sanitized error message, which may already be part of the $message 175 | * @return $form, altered to contain the message 176 | */ 177 | protected function SET_BAD_MESSAGE($form, $message, $safe_message) { 178 | 179 | // make sure $safe_message doesn't have any newlines... 180 | $additional = $form->prop('additional_settings') . 181 | sprintf("\non_sent_ok: 'if(window.console && console.warn){ console.warn(\"Failed cf7 integration: %s\"); }'" 182 | , addslashes($safe_message)); 183 | 184 | // recreate property array to submit 185 | $result = array('additional_settings' => $additional); 186 | 187 | // do we always report, or just pretend it worked, because the original contact plugin may be fine... 188 | 189 | // kind of a hack -- override the success and fail messages, just in case one or other is displayed 190 | $messages = $form->prop('messages'); 191 | foreach(array('mail_sent_ok', 'mail_sent_ng') as $msg) { 192 | $messages[$msg] = sprintf($message, $messages[$msg]); 193 | } 194 | $result['messages'] = $messages; 195 | 196 | $form->set_properties($result); 197 | 198 | return $form; 199 | } 200 | 201 | /** 202 | * Return the regularly intended confirmation email recipient 203 | */ 204 | protected function GET_RECIPIENT($form) { 205 | $mail = $form->prop('mail'); 206 | return isset($mail['recipient']) ? $mail['recipient'] : '--na--'; 207 | } 208 | 209 | }///--- class Forms3rdpartyIntegration_CF7 210 | 211 | 212 | // engage! 213 | new Forms3rdpartyIntegration_CF7; -------------------------------------------------------------------------------- /plugins/fplugin_base.php: -------------------------------------------------------------------------------- 1 | FPLUGIN(); 18 | } 19 | 20 | abstract protected function BEFORE_SEND_FILTER(); 21 | 22 | /** 23 | * Used to identify form in select box, differentiating them from other plugins' forms 24 | */ 25 | abstract protected function FORM_ID_PREFIX(); 26 | 27 | /** 28 | * Returns an array of the plugin's forms as ID => NAME 29 | */ 30 | abstract protected function GET_PLUGIN_FORMS(); 31 | 32 | /** 33 | * Get the ID from the plugin's form listing 34 | */ 35 | abstract protected function GET_FORM_LIST_ID($list_entry); 36 | abstract protected function GET_FORM_LIST_TITLE($list_entry); 37 | 38 | /** 39 | * Get the ID from the form "object" 40 | */ 41 | abstract protected function GET_FORM_ID($form); 42 | /** 43 | * Get the title from the form "object" 44 | */ 45 | abstract protected function GET_FORM_TITLE($form); 46 | 47 | /** 48 | * Determine if the form "object" is from the expected plugin (i.e. check its type) 49 | */ 50 | abstract protected function IS_PLUGIN_FORM($form); 51 | 52 | /** 53 | * Get the posted data from the form (or POST, wherever it is) 54 | */ 55 | abstract protected function GET_FORM_SUBMISSION($form); 56 | 57 | /** 58 | * How to attach the callback attachment for the indicated service (using `$this->attachment_heading` or `$this->attachment_heading_html` as appropriate) 59 | * @param $form the form "object" 60 | * @param $to_attach the content to attach 61 | * @param $service_name the name of the service to report in the header 62 | * @return $form, altered to contain the attachment 63 | */ 64 | abstract protected function ATTACH($form, $to_attach, $service_name); 65 | /* EXAMPLE 66 | if(isset($form['notification'])) 67 | $form['notification']['message'] .= "\n\n" . 68 | ( 69 | isset($form['notification']['disableAutoformat']) && $form['notification']['disableAutoformat'] 70 | ? $this->attachment_heading_html($service_name) 71 | : $this->attachment_heading($service_name) 72 | ) 73 | . $to_attach; 74 | */ 75 | 76 | /** 77 | * Insert new fields into the form's submission 78 | * @param $form the original form "object" 79 | * @param $newfields key/value pairs to inject 80 | * @return $form, altered to contain the new fields 81 | */ 82 | public function INJECT($form, $newfields) { 83 | return $form; 84 | } 85 | 86 | 87 | /** 88 | * How to update the confirmation message for a successful result 89 | * @param $form the form "object" 90 | * @param $message the content to report 91 | * @return $form, altered to contain the message 92 | */ 93 | abstract protected function SET_OKAY_MESSAGE($form, $message); 94 | 95 | /** 96 | * How to update the confirmation redirect for a successful result 97 | * @param $form the form "object" 98 | * @param $redirect the url to redirect to 99 | * @return $form, altered to contain the message 100 | */ 101 | abstract protected function SET_OKAY_REDIRECT($form, $redirect); 102 | 103 | /** 104 | * How to update the confirmation message for a failure/error 105 | * @param $form the form "object" 106 | * @param $message the content to report 107 | * @param $safe_message a short, sanitized error message, which may already be part of the $message 108 | * @return $form, altered to contain the message 109 | */ 110 | abstract protected function SET_BAD_MESSAGE($form, $message, $safe_message); 111 | 112 | /** 113 | * Return the regularly intended confirmation email recipient from the form "object" 114 | */ 115 | abstract protected function GET_RECIPIENT($form); 116 | 117 | /** 118 | * Fetch the original error message for the form 119 | */ 120 | abstract protected function GET_ORIGINAL_ERROR_MESSAGE($form); 121 | 122 | 123 | 124 | function __construct() { 125 | add_action(Forms3rdPartyIntegration::$instance->N('init'), array(&$this, 'init')); 126 | add_filter(Forms3rdPartyIntegration::$instance->N('declare_subpages'), array(&$this, 'add_subpage')); 127 | add_filter(Forms3rdPartyIntegration::$instance->N('use_form'), array(&$this, 'use_form'), 10, 4); 128 | add_filter(Forms3rdPartyIntegration::$instance->N('select_forms'), array(&$this, 'select_forms'), 10, 1); 129 | add_filter(Forms3rdPartyIntegration::$instance->N('get_submission'), array(&$this, 'get_submission'), 10, 2); 130 | } 131 | 132 | /** 133 | * Register all plugin hooks; override in form-specific plugins if necessary 134 | */ 135 | public function init() { 136 | if( !is_admin() ) { 137 | $filter = apply_filters(Forms3rdPartyIntegration::$instance->N('plugin_hooks'), (array) $this->BEFORE_SEND_FILTER()); 138 | ### _log(__CLASS__, $filter); 139 | 140 | foreach($filter as $f) add_filter( $f, array(&Forms3rdPartyIntegration::$instance, 'before_send') ); 141 | } 142 | 143 | //add_action( 'init', array( &$this, 'other_includes' ), 20 ); 144 | } 145 | 146 | /** 147 | * Register plugin as a subpage of the given pages 148 | * @param array $subpagesOf list of pages to be a subpage of -- add your target here 149 | * @return the modified list of subpages 150 | */ 151 | public function add_subpage($subpagesOf) { 152 | $subpagesOf []= $this->FPLUGIN(); 153 | return $subpagesOf; 154 | } 155 | 156 | 157 | /** 158 | * Helper to render a select list of available FPLUGIN forms 159 | * @param array $forms list of FPLUGIN forms 160 | * @param array $eid entry id - for multiple lists on page 161 | * @param array $selected ids of selected fields 162 | */ 163 | public function select_forms($forms){ 164 | // from http://ninjaforms.com/documentation/developer-api/functions/ninja_forms_get_all_forms/ 165 | // use like https://github.com/wpninjas/ninja-forms/blob/e4bc7d40c6e91ce0eee7c5f50a8a4c88d449d5f8/includes/admin/post-metabox.php#L43 166 | $plugin_forms = $this->GET_PLUGIN_FORMS(); 167 | foreach($plugin_forms as $f) { 168 | $form = array( 169 | 'id' => $this->FORM_ID_PREFIX() . $this->GET_FORM_LIST_ID($f) 170 | , 'title' => sprintf('(%s) %s', $this->REPORTING_NAME(), $this->GET_FORM_LIST_TITLE($f)) 171 | ); 172 | // add to list 173 | $forms []= $form; 174 | } 175 | 176 | return $forms; 177 | }//-- end function select_forms 178 | 179 | 180 | private $_use_form; 181 | protected function set_in_use() { 182 | $this->_use_form = $this->FPLUGIN(); 183 | } 184 | protected function in_use() { 185 | return $this->_use_form == $this->FPLUGIN(); 186 | } 187 | 188 | /** 189 | * How do decide whether the form is being used 190 | * @param bool $result the cascading result: true to use this form 191 | * @param object $form the FPLUGIN form object 192 | * @param int $service_id service identifier (from hook, option setting) 193 | * @param array $service_forms list of forms attached to this service 194 | * @return bool whether or not to use this form with this service 195 | */ 196 | public function use_form($result, $form, $service_id, $service_forms) { 197 | // protect against accidental binding between multiple plugins 198 | $this->_use_form = $result; 199 | 200 | // nothing to check against if nothing selected 201 | if( empty($service_forms) ) return $this->_use_form; 202 | 203 | if(!$this->IS_PLUGIN_FORM($form)) return $this->_use_form; 204 | 205 | // did we choose this form? 206 | if( in_array($this->FORM_ID_PREFIX() . $this->GET_FORM_ID($form), $service_forms) ) { 207 | ###_log('fplugin-int using form? ' . ($result ? 'Yes' : 'No'), $service_id, $form['id']); 208 | $this->set_in_use(); 209 | 210 | // also add subsequent hooks 211 | add_filter(Forms3rdPartyIntegration::$instance->N('remote_success'), array(&$this, 'remote_success'), 10, 3); 212 | add_filter(Forms3rdPartyIntegration::$instance->N('remote_failure'), array(&$this, 'remote_failure'), 10, 5); 213 | // expose injection point for other plugins 214 | add_filter(Forms3rdPartyIntegration::$instance->N('inject'), array(&$this, 'INJECT'), 10, 2); 215 | } 216 | 217 | return $this->_use_form; 218 | } 219 | 220 | /** 221 | * Get the posted submission for the form 222 | * @param array $submission initial values for submission; may have been provided by hooks 223 | * @param object $form the form object 224 | * @return array list of posted submission values to manipulate and map 225 | */ 226 | public function get_submission($submission, $form){ 227 | if(!$this->in_use()) return $submission; // return existing data, which is probably from another plugin's hook 228 | 229 | // interacting with user submission example -- http://ninjaforms.com/documentation/developer-api/actions/ninja_forms_process/ 230 | $all_fields = $this->GET_FORM_SUBMISSION($form); 231 | 232 | // http://php.net/manual/en/language.operators.array.php 233 | // rather than `array_merge`, since we may have numeric indices 234 | // `+` returns the union of two arrays, preserving left hand side and ignoring duplicate keys from right 235 | $result = (array)$all_fields + (array)$submission; 236 | 237 | return $result; 238 | } 239 | 240 | protected function attachment_heading($service_name) { 241 | return "Service \"$service_name\" Results:\n"; 242 | } 243 | protected function attachment_heading_html($service_name) { 244 | return "
Service "$service_name" Results:
\n"; 245 | } 246 | 247 | /** 248 | * What to do when the remote request succeeds 249 | * @param array $callback_results list of 'success' (did it work), 'errors' (list of validation errors), 'attach' (email body attachment), 'message' (when failed) 250 | * @param object $form the form object 251 | * @param array $service associative array of the service options 252 | * @return void n/a 253 | */ 254 | public function remote_success($form, $callback_results, $service) { 255 | ###_log(__FUNCTION__, __CLASS__, $form, $callback_results); 256 | 257 | //if requested, attach results to message 258 | if(!empty($callback_results['attach'])) { 259 | // html -- leave it up to the instance to format properly via `attachment_heading`, etc 260 | $form = $this->ATTACH($form, $callback_results['attach'], $service['name']); 261 | } 262 | 263 | //if requested, attach message to success notification 264 | if( !empty($callback_results['message']) ) { 265 | $form = $this->SET_OKAY_MESSAGE($form, $callback_results['message']); 266 | } 267 | 268 | //if requested, attach redirect to success notification 269 | if( !empty($callback_results['redirect']) ) { 270 | $form = $this->SET_OKAY_REDIRECT($form, $callback_results['redirect']); 271 | } 272 | 273 | ###_log(__FUNCTION__, $form); 274 | 275 | return $form; 276 | } 277 | 278 | /** 279 | * Formats the confirmation message based on service settings 280 | * @param $confirmation the original confirmation message 281 | * @param $response the service response data 282 | * @param $service service configuration 283 | * @return $confirmation updated 284 | */ 285 | protected function update_failure_confirmation($confirmation, $response, $service) { 286 | 287 | if(empty($service['failure'])) { 288 | $failure = empty($confirmation) 289 | ? $response['safe_message'] 290 | : $confirmation; 291 | } 292 | else $failure = 293 | Forms3rdPartyIntegration::$instance->format_failure_message($service, $response, $confirmation); 294 | 295 | ###_log(__FUNCTION__, $failure, $confirmation, $response['safe_message']); 296 | 297 | return $failure; 298 | } 299 | 300 | /** 301 | * Add a warning for failures; also send an email to debugging recipient with details 302 | * parameters passed by reference mostly for efficiency, not actually changed (with the exception of $form) 303 | * 304 | * @param $form reference to plugin object - contains mail details etc 305 | * @param $debug reference to this plugin "debug" option array 306 | * @param $service reference to service settings 307 | * @param $post reference to service post data 308 | * @param $response reference to remote-request response 309 | * @return the updated form reference 310 | */ 311 | public function remote_failure($form, $debug, $service, $post, $response){ 312 | ###_log(__FUNCTION__, __CLASS__, $form); 313 | 314 | //notify frontend 315 | 316 | //$form->get_form_setting($conf_setting); 317 | $confirmation = $this->update_failure_confirmation($this->GET_ORIGINAL_ERROR_MESSAGE($form), $response, $service); 318 | 319 | // TODO: do we add an error, or overwrite the confirmation message? 320 | 321 | // NOTE: assumes PHP > 5.3.14 in order to use `get_class`, which is already used by 322 | // ninjaforms and cf7 extensions, so...maybe overkill... 323 | $hook = function_exists('get_class') ? get_class($this) : __CLASS__; 324 | if(apply_filters($hook . '_show_warning', true)) { 325 | $form = $this->SET_BAD_MESSAGE($form, $confirmation, $response['safe_message']); 326 | } 327 | 328 | 329 | ###_log(__FUNCTION__, __CLASS__, __LINE__); 330 | 331 | //notify admin 332 | Forms3rdPartyIntegration::$instance->send_service_error( 333 | $service, 334 | $debug, 335 | $post, 336 | $response, 337 | $this->GET_FORM_TITLE($form), 338 | $this->GET_RECIPIENT($form), 339 | $this->REPORTING_NAME() 340 | ); 341 | 342 | 343 | return $form; 344 | }//--- end function on_response_failure 345 | 346 | }///--- class Forms3rdpartyIntegration_FPLUGIN -------------------------------------------------------------------------------- /plugins/gravityforms.php: -------------------------------------------------------------------------------- 1 | id; } 39 | /** 40 | * Get the title/name from the plugin's form listing 41 | */ 42 | protected function GET_FORM_LIST_TITLE($list_entry) { return $list_entry->title; } 43 | 44 | /** 45 | * Get the ID from the form "object" 46 | */ 47 | protected function GET_FORM_ID($form) { return $form['id']; } 48 | /** 49 | * Get the title from the form "object" 50 | */ 51 | protected function GET_FORM_TITLE($form) { return $form['title']; } 52 | 53 | 54 | /** 55 | * Returns an array of the plugin's forms, loosely as ID => NAME; 56 | * will be reformatted into ID => NAME by @see GET_FORM_LIST_ID and @see GET_FORM_LIST_TITLE 57 | */ 58 | protected function GET_PLUGIN_FORMS() { 59 | // from /wp-content/plugins/gravityforms/form_list.php, ~line 51 60 | return RGFormsModel::get_forms(true, "title"); 61 | } 62 | 63 | 64 | 65 | /** 66 | * Determine if the form "object" is from the expected plugin (i.e. check its type) 67 | */ 68 | protected function IS_PLUGIN_FORM($form) { 69 | // TODO: figure out a more bulletproof way to confirm it's a GF form 70 | return is_array($form) && isset($form['id']) && !empty($form['id']); 71 | } 72 | 73 | /** 74 | * Get the posted data from the form (or POST, wherever it is) 75 | */ 76 | protected function GET_FORM_SUBMISSION($form) { 77 | $submission = stripslashes_deep($_POST); // fix issue #42 78 | 79 | ### _log('gf-sub', $submission, $form['fields']); 80 | 81 | // per issue #35 also include by name 82 | foreach($submission as $id => $val) { 83 | // find the field by id -- bonus, this handles checkbox 'input_4_3' -> '4'? 84 | $fid = intval( str_replace('input_', '', $id) ); 85 | if($fid == 0) continue; // not a mappable input 86 | 87 | $field = $this->findfield($form['fields'], $fid); 88 | 89 | if($field !== false) { 90 | if(isset($submission[ $field->label ])) 91 | // preserve indexes 92 | $submission[ $field->label ] = array_merge((array) $submission[ $field->label ], array($val)); 93 | else 94 | $submission[ $field->label ] = $val; 95 | } 96 | } 97 | 98 | return $submission; 99 | } 100 | 101 | private function findfield($fields, $fid) { 102 | foreach($fields as $i => $field) { 103 | if($field->id == $fid) return $field; 104 | } 105 | return false; 106 | } 107 | 108 | /** 109 | * How to attach the callback attachment for the indicated service (using `$this->attachment_heading` or `$this->attachment_heading_html` as appropriate) 110 | * @param $form the form "object" 111 | * @param $to_attach the content to attach 112 | * @param $service_name the name of the service to report in the header 113 | * @return $form, altered to contain the attachment 114 | */ 115 | protected function ATTACH($form, $to_attach, $service_name) { 116 | // http://www.gravityhelp.com/documentation/page/Notification 117 | ###_log('attaching to mail body', print_r($cf7->mail, true)); 118 | if(isset($form['notification'])) 119 | $form['notification']['message'] .= "\n\n" 120 | . ( 121 | isset($form['notification']['disableAutoformat']) && $form['notification']['disableAutoformat'] 122 | ? $this->attachment_heading_html($service_name) 123 | : $this->attachment_heading($service_name) 124 | ) 125 | . $to_attach; 126 | 127 | return $form; 128 | } 129 | 130 | /** 131 | * Insert new fields into the form's submission 132 | * @param $form the original form "object" 133 | * @param $newfields key/value pairs to inject 134 | * @return $form, altered to contain the new fields 135 | */ 136 | public function INJECT($form, $newfields) { 137 | ### _log(__CLASS__, __FUNCTION__, array('oldPOST' => $_POST)); 138 | 139 | // inject into 'submission' -- ultimately, GF use rgpost and more to pull from the $_POST array 140 | // but the new fields must match up to an existing GF field 141 | // array union op (+) preserves keys from first array, so start with new fields to overwrite 142 | // but be aware that union op will overwrite with an empty value, particularly problematic when used with #postagain plugin 143 | # $_POST = $newfields + $_POST; 144 | foreach($newfields as $k => $v) { 145 | // don't overwrite with empty values (but is that always appropriate?), see forms-3rdparty-inject-results#1 146 | if(!empty($v)) $_POST[$k] = $v; 147 | } 148 | 149 | ### note: calling `get_current_lead` sets it within singleton, preventing our inject modification from being relevant 150 | ### $lead = GFFormsModel::get_current_lead(); 151 | 152 | ### _log(__CLASS__, __FUNCTION__, array('newPOST' => $_POST, 'newfields' => $newfields)); 153 | 154 | // https://www.gravityhelp.com/documentation/article/gform_field_value_parameter_name/ 155 | // but this is too late for injection so don't try it 156 | //$this->_newfields = $newfields; // save for next hook 157 | //add_filter( 'gform_field_value', array(&$this, 'populate_fields'), 10, 3 ); 158 | 159 | return $form; 160 | } 161 | 162 | /** 163 | * How to update the confirmation message for a successful result 164 | * @param $form the form "object" 165 | * @param $message the content to report 166 | * @return $form, altered to contain the message 167 | */ 168 | protected function SET_OKAY_MESSAGE($form, $message) { 169 | $this->set_confirmation($form['confirmation'], $message); 170 | 171 | return $form; 172 | } 173 | 174 | private $confirmationMessage; 175 | 176 | private function set_confirmation(&$confirmation, $message) { 177 | // http://www.gravityhelp.com/documentation/page/Confirmation 178 | switch($confirmation['type']) { 179 | case 'message': 180 | // need to hang on to the message and 'properly' hook to it if you want script tags 181 | $this->confirmationMessage = $message; 182 | add_filter('gform_confirmation', array(&$this, 'bind_confirmation'), 10, 3); // to bypass sanitization, or maybe version changed and this is how it should happen 183 | $confirmation['message'] = $message; // already contains confirmation message, don't append 184 | break; 185 | case 'redirect': 186 | $confirmation['queryString'] .= '&response_message=' . urlencode($message); 187 | break; 188 | case 'page': 189 | /// ??? 190 | break; 191 | } 192 | } 193 | 194 | /** 195 | * Must late-bind confirmation message if you want to preserve javascript 196 | */ 197 | public function bind_confirmation($message, $form, $entry) { 198 | ###_log(__FUNCTION__, $message); 199 | 200 | return $this->confirmationMessage; 201 | } 202 | 203 | /** 204 | * How to update the confirmation redirect for a successful result 205 | * @param $form the form "object" 206 | * @param $redirect the url to redirect to 207 | * @return $form, altered to contain the message 208 | */ 209 | protected function SET_OKAY_REDIRECT($form, $redirect) { 210 | // https://docs.gravityforms.com/gform_confirmation/#4-open-the-page-or-redirect-in-a-new-tab 211 | 212 | add_filter( 'gform_confirmation', array(&$this, 'gform_confirmation'), 10, 4 ); 213 | 214 | return $form; 215 | } 216 | 217 | public function gform_confirmation($confirmation, $form, $entry, $ajax) { 218 | if ( isset( $confirmation['redirect'] ) ) { 219 | $url = esc_url_raw( $redirect ); 220 | $confirmation .= ""; 221 | } 222 | 223 | return $confirmation; 224 | } 225 | 226 | /** 227 | * Fetch the original error message for the form 228 | */ 229 | protected function GET_ORIGINAL_ERROR_MESSAGE($form) { 230 | // cheat -- because we're going to deal with multiple confirmation messages, 231 | // we'll use a placeholder here, and correctly format it later via sprintf if it's present 232 | return '%s'; //$form['confirmation']; 233 | } 234 | 235 | /** 236 | * How to update the confirmation message for a failure/error 237 | * @param $form the form "object" 238 | * @param $message the content to report 239 | * @param $safe_message a short, sanitized error message, which may already be part of the $message 240 | * @return $form, altered to contain the message 241 | */ 242 | protected function SET_BAD_MESSAGE($form, $message, $safe_message) { 243 | // what confirmation do we update? try them all to be safe? 244 | $this->set_confirmation($form['confirmation'], sprintf($message, $form['confirmation']['message'])); 245 | foreach($form['confirmations'] as $conf => &$confirmation) { 246 | $this->set_confirmation($confirmation, sprintf($message, $confirmation['message'])); 247 | } 248 | 249 | return $form; 250 | } 251 | 252 | /** 253 | * Return the regularly intended confirmation email recipient 254 | */ 255 | protected function GET_RECIPIENT($form) { 256 | return isset($form['notification']) ? $form['notification']['to'] : '--na--'; 257 | } 258 | 259 | }///--- class Forms3rdpartyIntegration_Gf 260 | 261 | 262 | // engage! 263 | new Forms3rdpartyIntegration_Gf; 264 | -------------------------------------------------------------------------------- /plugins/ninjaforms.php: -------------------------------------------------------------------------------- 1 | NAME; 27 | * will be reformatted into ID => NAME by @see GET_FORM_LIST_ID and @see GET_FORM_LIST_TITLE 28 | */ 29 | protected function GET_PLUGIN_FORMS() { return ninja_forms_get_all_forms(); } 30 | 31 | /** 32 | * Get the ID from the plugin's form listing 33 | */ 34 | protected function GET_FORM_LIST_ID($list_entry) { return $list_entry['id']; } 35 | protected function GET_FORM_LIST_TITLE($list_entry) { return $list_entry['data']['form_title']; } 36 | 37 | /** 38 | * Get the ID from the form "object" 39 | */ 40 | protected function GET_FORM_ID($form) { return $form['form_id']; } 41 | /** 42 | * Get the title from the form "object" 43 | */ 44 | protected function GET_FORM_TITLE($form) { 45 | return $form['settings']['title']; 46 | } 47 | 48 | 49 | /** 50 | * Determine if the form "object" is from the expected plugin (i.e. check its type) 51 | */ 52 | protected function IS_PLUGIN_FORM($form) { 53 | ###_log(__CLASS__, __FUNCTION__, substr(print_r($form, true), 0, 100) . '...'); 54 | 55 | // pick some things that seem unique to ninjaforms form array (particularly after v3) 56 | return is_array($form) 57 | && isset($form['form_id']) 58 | && isset($form['settings']) 59 | && isset($form['fields']) 60 | && isset($form['fields_by_key']) 61 | ; 62 | } 63 | 64 | /** 65 | * Get the posted data from the form (or POST, wherever it is) 66 | */ 67 | protected function GET_FORM_SUBMISSION($form) { 68 | // interacting with user submission example -- http://ninjaforms.com/documentation/developer-api/actions/ninja_forms_process/ 69 | $submission = array(); 70 | 71 | // per issue #35 also include by name 72 | foreach($form['fields'] as $id => $field) { 73 | ### _log('nja-fld ' . $id, $field); 74 | $val = $field['value']; 75 | $submission[ $field['id'] ] = $val; 76 | $submission[ $field['key'] ] = $val; 77 | } 78 | 79 | return $submission; 80 | } 81 | 82 | /** 83 | * How to attach the callback attachment for the indicated service (using `$this->attachment_heading` or `$this->attachment_heading_html` as appropriate) 84 | * @param $form the form "object" 85 | * @param $to_attach the content to attach 86 | * @param $service_name the name of the service to report in the header 87 | * @return $form, altered to contain the attachment 88 | */ 89 | protected function ATTACH($form, $to_attach, $service_name) { 90 | // http://ninjaforms.com/documentation/developer-api/code-examples/modifying-form-settings-and-behavior/ 91 | // need to hook before `ninja_form_process`? 92 | // although may be able to use hook `ninja_forms_user_email` in post_process -- https://github.com/wpninjas/ninja-forms/blob/e4bc7d40c6e91ce0eee7c5f50a8a4c88d449d5f8/includes/display/processing/filter-msgs.php 93 | $body = &$form['actions']['email']['sent']; 94 | 95 | 96 | if(isset($body)) { 97 | $body .= "\n\n" . 98 | ( 99 | substr(ltrim($body), 0, 1) === '<' 100 | ? $this->attachment_heading_html($service_name) 101 | : $this->attachment_heading($service_name) 102 | ) 103 | . $to_attach; 104 | } 105 | 106 | return $form; // just to match expectation 107 | } 108 | 109 | /** 110 | * Insert new fields into the form's submission 111 | * @param $form the original form "object" 112 | * @param $newfields key/value pairs to inject 113 | * @return $form, altered to contain the new fields 114 | */ 115 | public function INJECT($form, $newfields) { 116 | // TODO: TBD -- maybe this works like gravity forms? 117 | foreach($newfields as $k => $v) { 118 | // don't overwrite with empty values (but is that always appropriate?), see forms-3rdparty-inject-results#1 119 | // not sure how to actually inject, but at least we can overwrite 120 | if(!empty($v) && isset($form['fields_by_key'][$k])) { 121 | $_POST[$k] = $v; 122 | $field = &$form['fields_by_key'][$k]; 123 | $field['value'] = $v; 124 | // also overwrite the 'regular' field list (by id) 125 | $form['fields'][$field['id']]['value'] = $v; 126 | } 127 | } 128 | return $form; 129 | } 130 | 131 | 132 | /** 133 | * How to update the confirmation message for a successful result 134 | * @param $form the form "object" 135 | * @param $message the content to report 136 | * @return $form, altered to contain the message 137 | */ 138 | protected function SET_OKAY_MESSAGE($form, $message) { 139 | 140 | // like https://github.com/wpninjas/ninja-forms/blob/e4bc7d40c6e91ce0eee7c5f50a8a4c88d449d5f8/includes/display/processing/filter-msgs.php 141 | 142 | $form['actions']['success_message'] = wpautop($message); 143 | 144 | return $form; // just to match expectation 145 | } 146 | 147 | /** 148 | * How to update the confirmation redirect for a successful result 149 | * @param $form the form "object" 150 | * @param $redirect the url to redirect to 151 | * @return $form, altered to contain the message 152 | */ 153 | protected function SET_OKAY_REDIRECT($form, $redirect) { 154 | $url = esc_url_raw( $redirect ); 155 | $form['actions']['success_message'] .= wpautop(""); 156 | 157 | return $form; // just to match expectation 158 | } 159 | 160 | 161 | /** 162 | * How to update the confirmation message for a failure/error 163 | * @param $form the form "object" 164 | * @param $message the content to report 165 | * @param $safe_message a short, sanitized error message, which may already be part of the $message 166 | * @return $form, altered to contain the message 167 | */ 168 | protected function SET_BAD_MESSAGE($form, $message, $safe_message) { 169 | return $this->SET_OKAY_MESSAGE($form, $message); 170 | } 171 | 172 | /** 173 | * Return the regularly intended confirmation email recipient 174 | */ 175 | protected function GET_RECIPIENT($form) { 176 | return $form['actions']['email']['to']; 177 | } 178 | 179 | /** 180 | * Fetch the original error message for the form 181 | */ 182 | protected function GET_ORIGINAL_ERROR_MESSAGE($form) { 183 | ### TODO: not sure what the original failure message would be... 184 | return $form['actions']['success_message']; 185 | } 186 | 187 | 188 | /** 189 | * Register all plugin hooks; override in form-specific plugins if necessary 190 | */ 191 | public function init() { 192 | // because ninja forms submits via ajax, can't check for `is_admin` anymore (> 3.0) 193 | // if( !is_admin() ) { 194 | $filter = apply_filters(Forms3rdPartyIntegration::$instance->N('plugin_hooks'), (array) $this->BEFORE_SEND_FILTER()); 195 | ###_log(__CLASS__, $filter); 196 | foreach($filter as $f) add_filter( $f, array(&Forms3rdPartyIntegration::$instance, 'before_send') ); 197 | // } 198 | 199 | //add_action( 'init', array( &$this, 'other_includes' ), 20 ); 200 | } 201 | 202 | }///--- class Forms3rdpartyIntegration_Ninja 203 | 204 | 205 | // engage! 206 | new Forms3rdpartyIntegration_Ninja; -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | === Forms: 3rd-Party Integration === 2 | Contributors: zaus, atlanticbt, spkane 3 | Donate link: http://drzaus.com/donate 4 | Tags: contact form, form, contact form 7, CF7, gravity forms, GF, CRM, mapping, 3rd-party service, services, remote request 5 | Requires at least: 3.0 6 | Tested up to: 5.2.3 7 | Stable tag: trunk 8 | License: GPLv2 or later 9 | 10 | Send contact form submissions from other plugins to multiple external services e.g. CRM. Configurable, custom field mapping, pre/post processing. 11 | 12 | == Description == 13 | 14 | Send [Contact Form 7][], [Gravity Forms][], or [Ninja Forms][] Submissions to a 3rd-party Service, like a CRM. Multiple configurable services, custom field mapping. Provides hooks and filters for pre/post processing of results. Allows you to send separate emails, or attach additional results to existing emails. Comes with a couple examples of hooks for common CRMs (listrak, mailchimp, salesforce). Check out the FAQ section for add-on plugins that extend this functionality, like sending XML/SOAP posts, setting headers, and dynamic fields. 15 | 16 | The plugin essentially makes a remote request (POST) to a service URL, passing along remapped form submission values. 17 | 18 | Based on idea by Alex Hager "[How to Integrate Salesforce in Contact Form 7][]". 19 | 20 | Original plugin, [Contact Form 7: 3rdparty Integration][] developed with the assistance of [AtlanticBT][]. Current plugin sponsored by [Stephen P. Kane Consulting][]. Please submit bugs / support requests to [GitHub issue tracker][] in addition to the Wordpress Support Forums because the Forums do not send emails. 21 | 22 | [Ninja Forms]: http://ninjaforms.com/ "Ninja Forms" 23 | [Gravity Forms]: http://www.gravityforms.com/ "Gravity Forms" 24 | [Contact Form 7]: http://wordpress.org/extend/plugins/contact-form-7/ "Contact Form 7" 25 | [How to Integrate Salesforce in Contact Form 7]: http://www.alexhager.at/how-to-integrate-salesforce-in-contact-form-7/ "Original Inspiration" 26 | [Contact Form 7: 3rdparty Integration]: http://wordpress.org/extend/plugins/contact-form-7-3rd-party-integration/ "CF7 Integration" 27 | [AtlanticBT]: http://www.atlanticbt.com/ "Atlantic BT: Custom Website and Web-application Services" 28 | [Stephen P. Kane Consulting]: http://www.stephenpkane.com/ "Website Design and Internet Marketing Services" 29 | [GitHub issue tracker]: https://github.com/zaus/forms-3rdparty-integration/issues "GitHub issue tracker" 30 | 31 | 32 | == Installation == 33 | 34 | 1. Unzip, upload plugin folder to your plugins directory (`/wp-content/plugins/`) 35 | 2. Make sure at least one of [Contact Form 7][], [Gravity Forms][], or [Ninja Forms][] is installed 36 | 3. Activate plugin 37 | 4. Go to new admin subpage _"3rdparty Services"_ under the CF7 "Contact" menu or Gravity Forms "Forms" menu and configure services + field mapping. 38 | 5. Turn on 'debug mode' to get emailed a copy of the submission+response data, until you're satisfied everything works, then turn it off 39 | 40 | [Contact Form 7]: http://wordpress.org/extend/plugins/contact-form-7/ "Contact Form 7" 41 | [Gravity Forms]: http://www.gravityforms.com/ "Gravity Forms" 42 | [Ninja Forms]: http://ninjaforms.com/ "Ninja Forms" 43 | 44 | == Frequently Asked Questions == 45 | 46 | = I need help / My form isn't working = 47 | 48 | Turn on 'debug mode' from the admin page to send you an email with: 49 | 50 | * the current plugin configuration, including field mappings 51 | * the user submission (as provided by CF7/GF/Ninja) 52 | * the post as sent to the service (applied mapping) 53 | * the response sent back from the service, which hopefully includes error codes or explanations (often is the raw HTML of a success/failure page) 54 | 55 | Submit an issue to the [GitHub issue tracker][] in addition to / instead of the WP Support Forums. 56 | 57 | = How do I add / configure a service? = 58 | 59 | See [Screenshots][] for visual examples. 60 | 61 | Essentially, 62 | 63 | 1. Name your service. 64 | 2. Enter the submission URL -- if your "service" provides an HTML form, you would use the form action here. 65 | 3. Choose which forms will submit to this service ("Attach to Forms"). 66 | 4. Set the default "success condition", or leave blank to ignore (or if using post processing, see [Hooks][] - this just looks for the provided text in the service response, and if present assumes "success" 67 | 4. Set an optional "failure message" to show if the remote request fails. Can include the "nice explanation" as well as the original message provided by the contact form plugin. 68 | 5. Allow hooks for further processing - unchecking it just saves minimal processing power, as it won't try to execute filters. 69 | 6. Map your form submission values (from the CF7/GF field tags) to expected fields for your service. 70 | * 1:1 mapping given as the _name_ (from the HTML input) of the CF7/GF field and the _name_ of the 3rdparty field 71 | * For GF and Ninja Forms, you may map either by the field name or the field label 72 | * You can also provide static values by checking the "Is Value?" checkbox and providing the value in the "Form Submission Field" column. 73 | * The "Label" column is optional, and just provided for administrative notes, i.e. so you can remind yourself what each mapping pertains to. 74 | 7. Add, remove, and rearrange mapping - basically just for visual clarity. 75 | 8. Use the provided hooks (as given in the bottom of the service block). 76 | 9. Add new services as needed, drag/drop mappings and service boxes. 77 | 78 | = How can I pre/post process the request/results? = 79 | 80 | See section [Hooks][]. See plugin folder `/3rd-parties` for example code for some common CRMs, which you can either directly include or copy to your code. 81 | 82 | [Hooks]: /extend/plugins/forms-3rd-party-integration/other_notes#Hooks 83 | [Screenshots]: /extend/plugins/forms-3rd-party-integration/screenshots 84 | [GitHub issue tracker]: https://github.com/zaus/forms-3rdparty-integration/issues "GitHub issue tracker" 85 | 86 | = I need to submit multiple values as... = 87 | 88 | * By default, if more than one value appears in the post request for the same field/key, they will be joined by the 'separator' value like `&post-values=a,b,c`. 89 | * However, if you use `[]` as the separator it will instead create multiple keys like `&post-values[]=a&post-values[]=b&...`. 90 | * Use `[#]` to retain the numerical index: `&post-values[0]=a&post-values[1]=b&...` 91 | * Use `[%]` to place the numerical index at desired location; specifically useful with nested fields via Xpost below (and issues [#11](https://github.com/zaus/forms-3rdparty-xpost/issues/11) and [#7](https://github.com/zaus/forms-3rdparty-xpost/issues/7)). 92 | 93 | If you instead need to combine/nest fields, check out [Forms: 3rdparty Xpost][]. 94 | 95 | [Forms: 3rdparty Xpost]: http://wordpress.org/plugins/forms-3rd-party-xpost/ 96 | 97 | = How do I make a GET request instead of POST? = 98 | 99 | Since v1.7.6, it's an admin setting for GET and POST, but for anything other than those two that you'd write a hook. 100 | 101 | _from http://wordpress.org/support/topic/method-get?replies=2#post-5996489_ 102 | 103 | See 'Hooks' section, #5 of http://wordpress.org/plugins/forms-3rdparty-integration/other_notes/ and the [source code](https://github.com/zaus/forms-3rdparty-integration/blob/master/forms-3rdparty-integration.php#L478). 104 | 105 | You'll need to perform `wp_remote_get` inside that filter and set `$post_args['response_bypass']` with the response, something like: 106 | 107 | function my_3rdparty_get_override($post_args, $service, $form) { 108 | $post_args['response_bypass'] = wp_remote_get($service['url'], $post_args); 109 | return $post_args; 110 | } 111 | 112 | = How do I dynamically change the URL? = 113 | 114 | Use the hook `Forms3rdPartyIntegration_service_filter_url`. (see "Hooks" section) 115 | 116 | = What about Hidden Fields? = 117 | 118 | Using hidden fields can provide an easier way to include arbitrary values on a per-form basis, rather than a single "Is Value?" in the Service mapping, as you can then put your form-specific value in the hidden field, and map the hidden field name generically. 119 | 120 | For convenience, you can install the [Contact Form 7 Modules: Hidden Fields][]. This plugin originally included the relevant code, but it was causing issues on install, so is no longer bundled with it. 121 | 122 | [Contact Form 7 Modules: Hidden Fields]: http://wordpress.org/extend/plugins/contact-form-7-modules/ "Hidden Fields from CF7 Modules" 123 | 124 | = How do I export/import settings? = 125 | Use the "Forms 3rdparty Migration" plugin http://wordpress.org/plugins/forms-3rdparty-migrate/, which lets you export and import the raw settings as JSON. 126 | You can also export settings from the original plugin [Contact Form 7: 3rdparty Integration][] and "upgrade" them for this plugin (although > 1.6.1 you will need to reselect forms). 127 | Also at https://github.com/zaus/forms-3rdparty-migrate 128 | 129 | = How do I map url parameters? = 130 | Use the "Dynamic Fields" plugin: http://wordpress.org/plugins/forms-3rdparty-dynamic-fields/ 131 | Also at https://github.com/zaus/forms-3rdparty-dynamicfields 132 | 133 | = How do I send XML/submit to SOAP? = 134 | For simple xml containers try the "Forms 3rdparty Xpost" plugin: http://wordpress.org/plugins/forms-3rd-party-xpost/ 135 | Also at https://github.com/zaus/forms-3rdparty-xpost 136 | 137 | = How do I set headers? = 138 | You can also set headers with "Forms 3rdparty Xpost" plugin: http://wordpress.org/plugins/forms-3rd-party-xpost/ 139 | Also at https://github.com/zaus/forms-3rdparty-xpost 140 | 141 | = How do I show a custom message on the confirmation screen? = 142 | The failure message is shown by default if the 3rdparty post did not succeed. You can add custom messaging to the plugin's (GF, CF7, Ninja) email or success screen response with something like: 143 | 144 | class MyPlugin { 145 | public function MyPlugin() { 146 | add_filter('Forms3rdPartyIntegration_service', array(&$this, 'adjust_response'), 10, 2); 147 | } 148 | 149 | public function adjust_response($body, $refs) { 150 | // use 'attach' to inject to regular email 151 | // use 'message' to inject to page 152 | $refs['attach'] = 'custom message in email'; 153 | $refs['message'] = 'custom message on page'; 154 | } 155 | } 156 | new MyPlugin(); // attach hook 157 | 158 | = How do I conditionally submit? (if field == ...) = 159 | Use hook '...use_submission' to check the form submission (pre-mapping), making sure to pick the appropriate scenario, like: 160 | 161 | add_filter('Forms3rdPartyIntegration_use_submission', 'f3i_conditional_submit', 10, 3); 162 | function f3i_conditional_submit($use_this_form, $submission, $sid) { 163 | // if there was a specific value -- skip 164 | if(isset($submission['maybe-send']) && 'no' == $submission['maybe-send']) return false; 165 | // if there was a specific value -- use 166 | if(isset($submission['maybe-send']) && 'yes' == $submission['maybe-send']) return $use_this_form; // or true, i guess 167 | // if there was a value for it (e.g. for checkboxes) -- skip 168 | if(isset($submission['if-not-send'])) return false; 169 | // if there was a value for it (e.g. for checkboxes) -- use 170 | if(isset($submission['if-send']) && !empty($submission['if-send'])) return $use_this_form; // or true, i guess 171 | 172 | return $use_this_form; // or `false`, depending on your desired default 173 | } 174 | 175 | If you want to check _after_ the fields have been mapped, you can "reuse" the hook '...service_filter_args' and return `false` to skip, rather than bypass: 176 | 177 | add_filter('Forms3rdPartyIntegration_service_filter_args', 'f3i_conditional_post', 10, 3); 178 | function f3i_conditional_post($post_args, $service, $form) { 179 | // your skip scenarios, checking `body` subarray instead 180 | if(isset($post_args['body']['maybe-send']) && ...) return false; 181 | 182 | // regular behavior 183 | return $post_args; 184 | } 185 | 186 | 187 | [Contact Form 7: 3rdparty Integration]: http://wordpress.org/extend/plugins/contact-form-7-3rd-party-integration/ "CF7 Integration" 188 | 189 | = How do I resend a service call? = 190 | 191 | Using public instance functions `send` and `handle_results`: 192 | 193 | $f3p = Forms3rdPartyIntegration::$instance; 194 | $debug = $f3p->get_settings(); 195 | // $sid - maybe get it from the current filter 196 | // $form - maybe get it from the current filter 197 | // $submission - probably save it somewhere, or rebuilt it from a database entry, etc 198 | // $service = $f3p->get_services()[$sid]; 199 | 200 | $sendResult = $f3p->send($submission, $form, $service, $sid, $debug); 201 | if($sendResult === Forms3rdPartyIntegration::RET_SEND_STOP || $sendResult === Forms3rdPartyIntegration::RET_SEND_SKIP) return; 202 | 203 | $response = $sendResult['response']; 204 | $post_args = $sendResult['post_args']; 205 | 206 | return $f3p->handle_results($submission, $response, $post_args, $form, $service, $sid, $debug); 207 | 208 | 209 | = How do I include part of the response in another service call? = 210 | 211 | See "Forms: 3rdparty Post Again" 212 | 213 | - Github https://github.com/zaus/forms-3rdparty-postagain 214 | - Wordpress https://wordpress.org/plugins/forms-3rdparty-post-again/ 215 | 216 | = How do I include part of the response the contact form results? = 217 | 218 | See "Forms: 3rdparty Inject Results" 219 | 220 | - Github https://github.com/zaus/forms-3rdparty-inject-results 221 | - Wordpress https://wordpress.org/plugins/forms-3rdparty-inject-results/ 222 | 223 | However, currently only confirmed working with Gravity Forms. 224 | 225 | 226 | == Screenshots == 227 | 228 | __Please note these screenshots are from the previous plugin incarnation, but are still essentially valid.__ 229 | 230 | 1. Admin page - create multiple services, set up debugging/notice emails, example code 231 | 2. Sample service - mailchimp integration, with static and mapped values 232 | 3. Sample service - salesforce integration, with static and mapped values 233 | 234 | 235 | == Changelog == 236 | 237 | = 1.8 = 238 | * copy service button 239 | * Ninja Forms are back! Can use Ninja Forms plugin v3.0+ again. 240 | 241 | = 1.7.9 = 242 | * debug message truncation with configure hooks 243 | * use hook `...debug_truncation` to set field length limits for each section 244 | 245 | = 1.7.8 = 246 | * adding per-service delimiter, supports newlines 247 | * 'add new service' button after metaboxes 248 | * minor ui fixes 249 | 250 | = 1.7.7 = 251 | * destination mapping is textarea to make other plugins easier (e.g. newlines in xpost formatting), hooks `..._service_mappings_headers` and `..._service_mappings_values` to add more columns 252 | 253 | = 1.7.6 = 254 | * exposing http method (get/post); result redirection 255 | 256 | = 1.7.5 = 257 | * late-bind GF confirmation for script tags 258 | 259 | = 1.7.4 = 260 | * another slight fix to make GF Resend do submission hooks too (so Reformat will work with it as well) 261 | 262 | = 1.7.3 = 263 | * slight refactor of `before_send` to make reposting GF submissions easier 264 | 265 | = 1.7.2.1 = 266 | * fix minor conflict between Inject Results and Post-Again 267 | 268 | = 1.7.2 = 269 | * added injection hooks for [Forms: 3rdparty Inject Results](https://github.com/zaus/forms-3rdparty-inject-results) -- use via `$form = apply_filters(Forms3rdPartyIntegration::$instance->N('inject'), $form, $values_to_inject);` 270 | 271 | = 1.7 = 272 | * refactored internal methods to make them reusable externally, specifically for 'forms-3rdparty-postagain' plugin 273 | 274 | = 1.6.6.5 = 275 | * added URL filter to allow customizing GET requests with post body arguments, or shortcodes 276 | * filter added to fplugin_base to allow multiple attachments per [github #62](https://github.com/zaus/forms-3rdparty-integration/issues/62) 277 | * fix [github #68](https://github.com/zaus/forms-3rdparty-integration/issues/68) 278 | * 'secret' debugging feature for error dump if logging after unable to send email: add a truthy value to post mapping with 3rdparty key `_json` 279 | 280 | = 1.6.6.4 = 281 | * fix array value without index placeholder bug introduced in [github #43](https://github.com/zaus/forms-3rdparty-integration/issues/43) 282 | * final bugfix to #55 (default options `mode` array) 283 | * tried to address [Xpost issue #7](https://github.com/zaus/forms-3rdparty-xpost/issues/7), but not the right place for it 284 | 285 | = 1.6.6.3 = 286 | * bugfixes [#53](https://github.com/zaus/forms-3rdparty-integration/issues/53) and [#55](https://github.com/zaus/forms-3rdparty-integration/issues/55) 287 | 288 | = 1.6.6.1 = 289 | * debug logging hook 290 | * fixed #52 - some hosting providers rejected arbitrary sender addresses 291 | * more options for debug mail failure logging 292 | 293 | = 1.6.6 = 294 | * Can now map GF and Ninja Forms by field label as well as id per issue #35 ([map by name](https://github.com/zaus/forms-3rdparty-integration/issues/35)) 295 | 296 | = 1.6.5.1 = 297 | * fix Github issue #43 ([valid success response codes](https://github.com/zaus/forms-3rdparty-integration/issues/43)) 298 | * fix Github issue #27 ([admin label](https://github.com/zaus/forms-3rdparty-integration/issues/27)) 299 | * exposed `$service` to hook `get_submission` to make extensions easier 300 | 301 | = 1.6.4.3 = 302 | * fix escaped slashes for gravity forms submissions, see [GitHub issue #42](https://github.com/zaus/forms-3rdparty-integration/issues/42) 303 | 304 | = 1.6.4.2 = 305 | * including original `$submission` in `service_filter_post` hook for [dynamicfields calc](https://wordpress.org/plugins/forms-3rdparty-dynamic-fields/) 306 | 307 | = 1.6.4.1 = 308 | * quick fix for global section toggle bug 309 | 310 | = 1.6.4 = 311 | * conditional submission hooks (see FAQ) 312 | * removed somewhat useless 'can-hook' setting, since I assume everybody wants success processing. Comment via github or author website contact form if you really need it. 313 | 314 | = 1.6.3.1 = 315 | * Fix for longstanding (?) Firefox admin bug (issue #36) preventing field editing/input selection 316 | 317 | = 1.6.3 = 318 | * fix form plugin checking when multiple contact form plugins used at same time 319 | 320 | = 1.6.1 = 321 | * integration with [Ninja Forms](http://www.ninjaforms.com) 322 | * refactored CF7 and GF integrations to take advantage of new FPLUGIN base (to make future integrations easier) 323 | * defined upgrade path 324 | 325 | Due to the new common form extension base, the way forms are identified in the settings has been changed. 326 | Deactivating and reactivating the plugin (which happens automatically on upgrade, but not FTP or other direct file changes) should correct your existing settings. 327 | 328 | Depending on how many services you have configured, the upgrade path may DESELECT your form selections in each service or otherwise break some configurations. 329 | If you are concerned this may affect you, please [export](https://github.com/zaus/forms-3rdparty-migrate) the settings so you can reapply your selections. 330 | 331 | = 1.4.9 = 332 | * Updated cf7 plugin to match [their latest changes](http://contactform7.com/2014/07/02/contact-form-7-39-beta/). 333 | * using new way to access properties 334 | * removed remaining support for all older versions of CF7 (it was just getting complicated) 335 | 336 | = 1.4.8.1 = 337 | Trying to add some clarity to the admin pages 338 | 339 | = 1.4.8 = 340 | * multiple values treated differently depending on separator: 'char', `[]`, or `[#]` 341 | * static values treated the same as dynamic (so they get above processing) 342 | * fix: php5 constructor re: https://github.com/zaus/forms-3rdparty-integration/issues/6 343 | 344 | = 1.4.7 = 345 | * totally removing hidden field plugin -- seems like even though it wasn't referenced, it may have caused the "invalid header" error during install 346 | * admin ui - js fixes (configurable section icons via `data-icon`; entire metabox title now toggles accordion) 347 | * stripslashes on submission to fix apostrophes in 'failure response' textarea 348 | 349 | = 1.4.6 = 350 | * hook `...service_filter_args` to allow altering post headers, etc 351 | * fix: removed more args-by-reference (for PHP 5.4 issues, see support forum requests) 352 | * tested with WP 3.8, CF7 3.6 353 | 354 | = 1.4.5 = 355 | * fix: failure response attaches to 'onscreen message' for Gravity Forms 356 | * fix: (actually part of the next feature) failure response shows onscreen for Contact Form 7 357 | * customize the failure response shown onscreen -- new admin setting per service (see description) 358 | 359 | = 1.4.4 = 360 | * protecting against unattached forms 361 | * Github link 362 | * global post filter `Forms3rdPartyIntegration_service_filter_post` in addition to service-specific with suffix `_0`; accepts params `$post`, `$service`, `$form`, `$sid` 363 | * admin options hook `Forms3rdPartyIntegration_service_settings`, `..._metabox` 364 | * fix: gravityforms empty 'notification' field 365 | * fix: admin ui -- 'hooks' toggle on metabox clone, row clone fieldname 366 | * fix: service hooks not fired multiple times when both GF and CF7 plugins are active 367 | * fix: Gravityforms correctly updates $form array 368 | 369 | = 1.4.3 = 370 | * Fixed "plugin missing valid header" caused by some PHP versions rejecting passing variable by reference (?) as reported on Forum support topics ["Error on install"](http://wordpress.org/support/topic/error-on-install-6) and ["The plugin does not have a valid header"](http://wordpress.org/support/topic/the-plugin-does-not-have-a-valid-header-34), among others 371 | * Rewrote admin javascript to address style bug as reported on forum post ["fields on mapping maintenance screen misaligned"](http://wordpress.org/support/topic/fields-on-mapping-maintenance-screen-misaligned) and via direct contact. Really needed to be cleaned up anyway, I've learned more jQuery since then ;) 372 | * Dealt with weird issue where clicking a label also triggers its checkbox click 373 | * Other general ui fixes 374 | * More "verbose" endpoint-test script (headers, metadata as well as get/post) 375 | 376 | = 1.4.2 = 377 | 378 | * Bugfixes 379 | * cleaned up admin JS using delegate binding 380 | * added "empty" checking for 3rdparty entries to avoid dreaded "I've deleted my mappings and can't do anything" error 381 | * timeout option 382 | * fixed CF7 form selection bug 383 | * conditionally load CF7 or GF only if active/present; note that this plugin must `init` normally to check CF7 384 | 385 | = 1.4.1 = 386 | 387 | * Bugfixes 388 | * Added "Label" column for administrative notes 389 | 390 | = 1.4.0 = 391 | 392 | * Forked from [Contact Form 7: 3rdparty Integration][]. 393 | * Removed 'hidden field plugin' from 1.3.0, as it's specific to CF7. 394 | 395 | = 1.3.2 = 396 | 397 | * Added failure hook - if your service fails for some reason, you now have the opportunity to alter the CF7 object and prevent it from mailing. 398 | 399 | = 1.3.1 = 400 | 401 | * Added check for old version of CF7, so it plays nice with changes in newer version (using custom post type to store forms instead, lost original function for retrieval) 402 | * see original error report http://wordpress.org/support/topic/plugin-forms-3rd-party-integration-undefined-function-wpcf7_contact_forms?replies=2#post-2646633 403 | 404 | = 1.3.0 = 405 | moved "external" includes (hidden-field plugin) to later hook to avoid conflicts when plugin already called 406 | 407 | = 1.2.3 = 408 | changed filter callback to operate on entire post set, changed name 409 | 410 | = 1.2.2 = 411 | fixed weird looping problem; removed some debugging code; added default service to test file 412 | 413 | = 1.2 = 414 | moved filter to include dynamic and static values; icons 415 | 416 | = 1.1 = 417 | added configuration options, multiple services 418 | 419 | = 1.0 = 420 | base version, just directly submits values 421 | 422 | 423 | [Contact Form 7: 3rdparty Integration]: http://wordpress.org/extend/plugins/contact-form-7-3rd-party-integration/ "CF7 Integration" 424 | [export]: https://github.com/zaus/forms-3rdparty-migrate 425 | [GitHub issue]: https://github.com/zaus/forms-3rdparty-integration/issues 426 | 427 | 428 | == Upgrade Notice == 429 | 430 | = 1.8 = 431 | Now that Ninja Forms works again with this plugin there may be conflicts with the related sub-plugin "Forms 3rdparty File Attachments". Please deactivate the "File Attachments" plugin if you are unable to submit your contact form. 432 | 433 | = 1.6.1 = 434 | Due to the new common form extension base, the way forms are identified in the settings has been changed. 435 | Deactivating and reactivating the plugin (which happens automatically on upgrade, but not FTP or other direct file changes) should correct your existing settings. 436 | See Changelog for more details. 437 | 438 | = 1.4.6 = 439 | * PHP 5.4 errors with (deprecated) passing arguments by reference should be fixed. 440 | * Behavior change when reporting `$post` args in `on_response_failure` and similar -- now returns `$post_args`, which contains the header+body array as sent to new hook `...service_filter_args` 441 | * Please submit a [GitHub issue][] in addition to making a support forum request if something is broken. 442 | 443 | = 1.4.5 = 444 | You may need to configure the 'failure message', or at least refresh and save the admin settings, to avoid PHP 'empty index' warnings. 445 | 446 | = 1.4.0 = 447 | Accommodates Gravity Forms. Complete plugin rewrite, namespace since 1.3.2 incarnation as CF7 3rdparty Integration. Incompatible with previous versions. 448 | 449 | = 1.3.1 = 450 | See 1.3.0 notice 451 | 452 | = 1.3.0 = 453 | Fixes should accomodate CF7 < v1.2 and changes to >= v1.2 -- please test and check when upgrading, and report any errors to the plugin forum. 454 | 455 | [export]: https://github.com/zaus/forms-3rdparty-migrate 456 | [GitHub issue]: https://github.com/zaus/forms-3rdparty-integration/issues 457 | 458 | == Hooks == 459 | 460 | _Please note that this documentation is in flux, and may not be accurate for latest rewrite 1.4.0_ 461 | 462 | 1. `add_action('Forms3rdPartyIntegration_service_a#', $response, $param_ref);` 463 | * hook for each service, indicated by the `#` - _this is given in the 'Hooks' section of each service_ 464 | * provide a function which takes `$response, &$results` as arguments 465 | * allows you to perform further processing on the service response, and directly alter the processing results, provided as `array('success'=>false, 'errors'=>false, 'attach'=>'', 'message' => '');` 466 | * *success* = `true` or `false` - change whether the service request is treated as "correct" or not 467 | * *errors* = an array of error messages to return to the form 468 | * *attach* = text to attach to the end of the email body 469 | * *message* = the message notification shown (from CF7 ajax response) below the form 470 | * note that the basic "success condition" may be augmented here by post processing 471 | 2. `add_action('Forms3rdPartyIntegration_service', $response, $param_ref, $sid);` 472 | * same as previous hook, but not tied to a specific service 473 | 3. `add_filter('Forms3rdPartyIntegration_service_filter_post_#, ...` 474 | * hook for each service, indicated by the `#` - _this is given in the 'Hooks' section of each service_ 475 | * allows you to programmatically alter the request parameters sent to the service 476 | * should return updated `$post` array 477 | 4. `add_filter('Forms3rdPartyIntegration_service_filter_post', 'YOUR_HOOK', 10, 4);` 478 | * in addition to service-specific with suffix `_a#`; accepts params `$post`, `$service`, `$form`, `$sid` 479 | 5. `add_filter('Forms3rdPartyIntegration_service_filter_args', 'YOUR_HOOK', 10, 3);` 480 | * alter the [args array](http://codex.wordpress.org/Function_Reference/wp_remote_post#Parameters) sent to `wp_remote_post` 481 | * allows you to add headers or override the existing settings (timeout, body) 482 | * if you return an array containing the key `response_bypass`, it will skip the normal POST and instead use that value as the 3rdparty response; note that it must match the format of a regular `wp_remote_post` response. 483 | * Note: if using `response_bypass` you should consider including the original arguments in the callback result for debugging purposes. 484 | 6. `add_action('Forms3rdPartyIntegration_remote_failure', 'mycf7_fail', 10, 5);` 485 | * hook to modify the Form (CF7 or GF) object if service failure of any kind occurs -- use like: 486 | 487 | function mycf7_fail(&$cf7, $debug, $service, $post, $response) { 488 | $cf7->skip_mail = true; // stop email from being sent 489 | // hijack message to notify user 490 | ///TODO: how to modify the "mail_sent" variable so the message isn't green? on_sent_ok hack? 491 | $cf7->messages['mail_sent_ok'] = 'Could not complete mail request:** ' . $response['safe_message']; 492 | } 493 | 494 | * needs some way to alter the `mail_sent` return variable in CF7 to better indicate an error - no way currently to access it directly. 495 | 7. `add_action('Forms3rdPartyIntegration_service_settings', 'YOUR_HOOK', 10, 3)` 496 | * accepts params `$eid`, `$P`, `$entity` corresponding to the index of each service entity and this plugin's namespace, and the `$entity` settings array 497 | * allows you to add a section to each service admin settings 498 | * name form fields with plugin namespace to automatically save: `$P[$eid][YOUR_CUSTOM_FIELD]` $rarr; `Forms3rdPartyIntegration[0][YOUR_CUSTOM_FIELD]` 499 | 8. `add_action('Forms3rdPartyIntegration_service_metabox', 'YOUR_HOOK', 10, 2)` 500 | * accepts params `$P`, `$entity` corresponding to the index of each service entity and this plugin's namespace, and the `$options` settings array (representing the full plugin settings) 501 | * allows you to append a metabox (or anything else) to the plugin admin settings page 502 | * name form fields with plugin namespace to automatically save: `$P[YOUR_CUSTOM_FIELD]` $rarr; `Forms3rdPartyIntegration[YOUR_CUSTOM_FIELD]` 503 | 9. `add_filter('Forms3rdPartyIntegration_debug_message', 'YOUR_HOOK', 10, 5);` 504 | * bypass/alternate debug logging 505 | 10. `add_filter('Forms3rdPartyIntegration_plugin_hooks', 'YOUR_HOOK', 10, 1);` 506 | * Accepts an array of contact form plugin hooks to attach F3p to, and returns that array. Modify result to attach to additional plugin hooks, like GF edit. 507 | 11. `add_filter('Forms3rdPartyIntegration_service_filter_url', 'YOUR_HOOK', 10, 2);` 508 | * hook a function that takes the `$service_url, $post_args` and returns the endpoint `$url` 509 | * used to modify the submission url based on mappings or other information 510 | * `$post_args` contains the `body` and other `wp_remote_post` details 511 | 512 | 513 | Basic examples provided for service hooks directly on plugin Admin page (collapsed box "Examples of callback hooks"). Code samples for common CRMS included in the `/3rd-parties` plugin folder. 514 | 515 | == Stephen P. Kane Consulting == 516 | 517 | From [the website][] and [Handpicked Tomatoes][]: 518 | 519 | **Transparent and Holistic Approach** 520 | 521 | > Transparency is good. It's amazing how many web design sites hide who they are. There are lots of reasons, none of which are good for the customer. We don't do that. I'm Stephen Kane, principal web craftsman at HandpickedTomatoes, and I'm an Orange County based freelancer who occasionally works with other local freelancers and agencies to deliver quality web solutions at very affordable prices. 522 | > We work to earn the right to be a trusted partner. One that you can turn to for professional help in strategizing, developing, executing, and maintaining your Internet presence. 523 | > We take a holistic view. Even if a project is small, our work should integrate into the big picture. We craft web architecture and designs that become winning websites that are easy to use and to share. We custom build social network footprints on sites like linkedin, facebook, twitter, youtube, flickr, yelp!, and google places and integrate them into your website to leverage social marketing. We help you set up and execute email campaigns, with search engine marketing, with photography, with site copy and content and anything else that you need in order to have a successful Internet presence. 524 | > Through this holistic approach, we work with clients to grow their sales, improve their brand recognition, and manage their online reputation. 525 | 526 | [the website]: http://www.stephenpkane.com/ "Wordpress, Online Marketing, Social Media, SEO" 527 | [Handpicked Tomatoes]: http://handpickedtomatoes.com/ "Website Design & Internet Marketing Services" -------------------------------------------------------------------------------- /screenshot-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaus/forms-3rdparty-integration/59a2a42473bbc1eb9846115fcc5333df7cf692da/screenshot-1.png -------------------------------------------------------------------------------- /screenshot-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaus/forms-3rdparty-integration/59a2a42473bbc1eb9846115fcc5333df7cf692da/screenshot-2.png -------------------------------------------------------------------------------- /screenshot-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaus/forms-3rdparty-integration/59a2a42473bbc1eb9846115fcc5333df7cf692da/screenshot-3.png -------------------------------------------------------------------------------- /sprites.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaus/forms-3rdparty-integration/59a2a42473bbc1eb9846115fcc5333df7cf692da/sprites.png -------------------------------------------------------------------------------- /upgrade.php: -------------------------------------------------------------------------------- 1 | N(self::HOOK_LOADED); } 26 | public function activated_hook_name() { return $this->N(self::HOOK_ACTIVATED); } 27 | 28 | public function register($original_file) { 29 | register_activation_hook( $original_file, array( &$this, 'activate' ) ); 30 | } 31 | 32 | public function activate() { 33 | // delay doing anything until plugins are actually ready 34 | // see http://codex.wordpress.org/Function_Reference/register_activation_hook#Process_Flow 35 | add_option( $this->N(), $this->N() ); 36 | 37 | /* other non-plugin-dependent activation code here */ 38 | do_action( $this->activated_hook_name(), 'activated' ); 39 | } 40 | 41 | public function load_plugin() { 42 | 43 | if ( is_admin() && get_option( $this->N() ) == $this->N() ) { 44 | 45 | // clear the 'run once' flag 46 | delete_option( $this->N() ); 47 | 48 | /* do stuff once right after activation */ 49 | do_action( $this->loaded_hook_name(), 'loaded' ); 50 | } 51 | } 52 | }//--- class WpPluginUpgradeBase 53 | 54 | 55 | endif; // class_exists 56 | 57 | class Forms3rdPartyIntegrationUpgrade extends WpPluginUpgradeBase { 58 | 59 | function __construct() { 60 | parent::__construct(); 61 | 62 | add_action($this->loaded_hook_name(), array(&$this, 'loaded')); 63 | 64 | ## test 65 | ### add_action($this->loaded_hook_name(), array(&$this, 'test')); 66 | ### add_action($this->activated_hook_name(), array(&$this, 'test')); 67 | } 68 | 69 | public function test($action) { 70 | // just prove it was called 71 | error_log(print_r(array(__FILE__, __CLASS__, __FUNCTION__, $action), true)); 72 | } 73 | 74 | /** 75 | * List of important upgrade steps 76 | */ 77 | private $upgrades = array(self::VERSION_FPLUGINBASE); 78 | const VERSION_FPLUGINBASE = '1.6.0'; 79 | 80 | public function loaded($action) { 81 | // check current plugin version 82 | $current = Forms3rdPartyIntegration::pluginVersion; 83 | 84 | // compare against prev version and do stuff 85 | $prev = get_option( Forms3rdPartyIntegration::$instance->N('version') ); 86 | 87 | ### error_log('prev version ' . $prev . ', current ' . $current); 88 | 89 | // special case: we've never set the version before; not all plugins will need to upgrade in that case 90 | if(empty($prev) || version_compare($prev, $current) < 0) { 91 | // are there upgrade steps depending on how out-of-date? 92 | foreach($this->upgrades as $next_version) { 93 | if(version_compare($prev, $next_version) < 0) $this->do_upgrade($prev, $next_version); 94 | 95 | $prev = $next_version; 96 | } 97 | } 98 | 99 | // update stored plugin version for next time 100 | update_option(Forms3rdPartyIntegration::$instance->N('version'), $current); 101 | } 102 | 103 | function do_upgrade($prev, $next) { 104 | ## error_log('upgrade from ' . $prev . ' to ' . $next); 105 | switch($next) { 106 | case self::VERSION_FPLUGINBASE: 107 | ## error_log('doing fpluginbase upgrade...'); 108 | 109 | // check the attached forms, and guess what prefixes should be added based on the 110 | // currently activated form plugins. 111 | // should be okay to "overdo it" and add multiple prefixes, 112 | // because they'll get selected and then corrected on next admin save 113 | 114 | $services = Forms3rdPartyIntegration::$instance->get_services(); 115 | 116 | // only add prefix if corresponding plugin is active 117 | $has_cf7 = is_plugin_active('contact-form-7/wp-contact-form-7.php'); 118 | $has_gf = is_plugin_active('gravityforms/gravityforms.php'); 119 | // $has_ninja = is_plugin_active('ninja-forms/ninja-forms.php'); // don't need this for < 1.6.0 120 | 121 | $prefixes = array(); 122 | if($has_cf7) $prefixes []= Forms3rdpartyIntegration_CF7::FORM_ID_PREFIX; 123 | if($has_gf) $prefixes []= Forms3rdpartyIntegration_Gf::FORM_ID_PREFIX; 124 | 125 | // nothing to do? quit 126 | if(empty($prefixes)) break; // return? 127 | 128 | foreach($services as &$service) { 129 | if( !isset($service['forms']) || empty($service['forms']) ) continue; // nothing attached 130 | 131 | $new_forms_list = array(); 132 | foreach($service['forms'] as &$form_id) { 133 | // old style, no prefix? 134 | if( ! is_numeric($form_id) ) { 135 | // don't really need to preserve this, since this shouldn't ever happen 136 | // unless someone migrated and messed with settings on purpose 137 | // any any invalid values will disappear the next time the plugin settings are saved 138 | $new_forms_list []= $form_id; 139 | continue; 140 | } 141 | 142 | foreach($prefixes as $prefix) { 143 | $new_forms_list []= $prefix . $form_id; 144 | } 145 | } 146 | $service['forms'] = $new_forms_list; 147 | } // foreach service 148 | 149 | // now save the service changes 150 | Forms3rdPartyIntegration::$instance->save_services($services); 151 | 152 | break; 153 | } 154 | } 155 | } 156 | --------------------------------------------------------------------------------