Created
August 5, 2019 16:39
-
-
Save Shelob9/75ebee38f2c198ca60d38ea137823455 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Test Add-on | |
*/ | |
add_action('wp_enqueue_scripts', function () { | |
wp_enqueue_script( | |
'cf-add-on-test', | |
esc_url_raw(plugins_url('test-add-on.js', __FILE__)), | |
[ | |
//'wp-blocks', 'wp-element', 'wp-editor' | |
'jquery', | |
], | |
md5_file(__DIR__ . '/' . 'test-add-on.js'), | |
true | |
); | |
}); | |
add_filter('-caldera_forms_render_get_form', function ($form) { | |
if ('CF5d4835db87edb' !== $form['ID']) { | |
return $form; | |
} | |
$id = 'stripe_token_' . $form['ID']; | |
$form['fields'][$id] = [ | |
'ID' => $id, | |
'label' => 'stripe-token', | |
'type' => 'hidden', | |
'slug' => $id, | |
'conditions' => ['type' => '',], | |
'caption' => '', | |
'config' => | |
[ | |
'custom_class' => '', 'placeholder' => '', 'default' => '', | |
] | |
]; | |
$form['layout_grid']['fields'][$id] = '1:1'; | |
return $form; | |
}); | |
/** | |
* On form submit, do a redirect to a diffrent site, then complete submission | |
*/ | |
add_action('caldera_forms_submit_process_start', function ( | |
$form, | |
$referrer, | |
$process_id | |
) { | |
//If we have a already done redirect, do nothing | |
if (isset($_GET['cf_tp'])) { | |
return; | |
} | |
//Get current URL | |
$url = caldera_forms_build_url($referrer); | |
//Append transient ID | |
$url = add_query_arg('cf_tp', $process_id, $url); | |
//Set CF to redirect to other site | |
add_filter('caldera_forms_redirect_url_complete', function () use ($url, $process_id) { | |
//Save transdata | |
//This should be automatic. See: https://github.com/CalderaWP/Caldera-Forms/issues/3312 | |
global $transdata; | |
Caldera_Forms_Transient::set_transient($process_id, $transdata, 5 * 60); | |
//create redirect URL. | |
return 'http://localhost:8080/?redirect=' . urlencode($url); | |
}, 9999); | |
}, 10, 3); | |
//@see https://github.com/CalderaWP/Caldera-Forms/issues/3313 | |
if (!function_exists('caldera_forms_build_url')) { | |
function caldera_forms_build_url(array $parts) | |
{ | |
if (function_exists('http_build_url')) { | |
return http_build_url($parts); | |
} | |
return add_query_arg( | |
$parts['query'], | |
(isset($parts['scheme']) ? "{$parts['scheme']}:" : '') | |
. ((isset($parts['user']) || isset($parts['host'])) ? '//' : '') | |
. (isset($parts['user']) ? "{$parts['user']}" : '') | |
. (isset($parts['pass']) ? ":{$parts['pass']}" : '') | |
. (isset($parts['user']) ? '@' : '') | |
. (isset($parts['host']) ? "{$parts['host']}" : '') | |
. (isset($parts['port']) ? ":{$parts['port']}" : '') | |
. (isset($parts['path']) ? "{$parts['path']}" : '') | |
. (isset($parts['fragment']) ? "#{$parts['fragment']}" : '') | |
); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3.1' | |
services: | |
wordpress: | |
image: wordpress | |
ports: | |
- 8228:80 | |
environment: | |
WORDPRESS_DB_PASSWORD: example | |
WORDPRESS_DEBUG: 1 | |
ABSPATH: /usr/src/wordpress/ | |
volumes: | |
- wordpress:/var/www/html | |
- .:/var/www/html/wp-content/plugins/caldera-forms | |
- ./wp-content/plugins/add-on:/var/www/html/wp-content/plugins/add-on | |
cli: | |
image: wordpress:cli | |
volumes: | |
- wordpress:/var/www/html | |
- .:/var/www/html/wp-content/plugins/caldera-forms | |
- ./wp-content/plugins/add-on:/var/www/html/wp-content/plugins/add-on | |
environment: | |
WORDPRESS_DB_PASSWORD: example | |
ABSPATH: /usr/src/wordpress/ | |
mysql: | |
image: mysql:5.7 | |
environment: | |
MYSQL_ROOT_PASSWORD: example | |
MYSQL_DATABASE: wordpress_test | |
wordpress_phpunit: | |
image: chriszarate/wordpress-phpunit | |
environment: | |
PHPUNIT_DB_HOST: mysql | |
volumes: | |
- .:/app | |
- testsuite:/tmp | |
composer: | |
image: composer | |
volumes: | |
- .:/app | |
volumes: | |
testsuite: | |
wordpress: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function($) { | |
//variable to prevent blocking all | |
var blockedOnce = false; | |
$(document).on("cf.ajax.request", function(e, obj) { | |
//only block submission once | |
if (!blockedOnce) { | |
//Make sure window.cf2 is set properly | |
if (!window.cf2) { | |
window.cf2 = {}; | |
} | |
var formId = obj.formIdAttr; | |
if (!window.cf2[formId]) { | |
window.cf2[formId] = {}; | |
} | |
if (!window.cf2[formId].hasOwnProperty("fieldsBlocking")) { | |
window.cf2[formId].fieldsBlocking = obj.fieldsBlocking || []; | |
} | |
//end hacks caused by not paying down this debt before CF Stripe update | |
//block submission based on a field | |
window.cf2[formId].fieldsBlocking.push("fld_6866745_1"); | |
//Set flag to prevent recurssion | |
blockedOnce = true; | |
//For testing purposes, re-submit in 2 seconds | |
setTimeout(function() { | |
//unblock | |
window.cf2[formId].fieldsBlocking = []; | |
//resubmit | |
obj.$form.submit(); | |
}, 2000); | |
} | |
}); | |
}); | |
var checkedout = false; | |
//submit form | |
//redirect back to same url with cf_tp query arg | |
//this triggers submit again, redirect to Stripe | |
//redirect back to client, triggers submit again | |
$( ).on( 'submit-or-whatever', () =>{ | |
if( 'cf_tp query args' ){ | |
if( 'has Stripe tokem' ){ | |
//Step 3 -> have saved transient, have stripe token. | |
//submit normal | |
}else{ | |
//step 2 -> have transient, not the stripe token | |
//redirect to stripe chekout using current url (with cf_tp query arg) as success_url for stripe.redirectToCheckout() | |
} | |
}else{ | |
//submit form -> step 1 -> no transient | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment