-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathgw-feed-specific-submit-button.php
127 lines (96 loc) · 3.83 KB
/
gw-feed-specific-submit-button.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* Gravity Wiz // Gravity Forms // Feed-specific Submit Button
* https://gravitywiz.com/gravity-forms-feed-specific-submit-button/
*
* Instruction Video: https://www.loom.com/share/c75fcfa9e9d2453f839dd483b25147a8
*
* Change the label of the submit button depending on which payment feed will be used to process the order. This can help
* set user expectation when conditionally redirecting to Stripe Checkout. Currently, this plugin is limited to Stripe.
*
* Usage:
*
* 1. Install and activate the plugin.
* 2. Create a payment feed.
* 3. Specify the submit button label if this feed is activated: https://gwiz.io/2MblCb0
*
* Whenever this feed is set as the active feed, the submit button label will automatically be set to your custom label.
*
* Plugin Name: Gravity Forms - Feed-specific Submit Button
* Plugin URI: https://gravitywiz.com/gravity-forms-feed-specific-submit-button/
* Description: Change the label of the submit button depending on which payment feed will be used to process the order.
* Author: Gravity Wiz.
* Version: 1.3
* Author URI: https://gravitywiz.com
*/
class GW_Feed_Specific_Submit_Button {
private static $instance = null;
public static function get_instance() {
if ( self::$instance === null ) {
self::$instance = new self;
}
return self::$instance;
}
private function __construct() {
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
add_filter( 'gform_gravityformsstripe_feed_settings_fields', array( $this, 'add_submit_button_setting' ) );
add_filter( 'gform_gravityformsstripe_frontend_feed', array( $this, 'add_submit_button_setting_to_frontend_feed' ), 10, 3 );
add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 );
}
public function add_submit_button_setting( $settings ) {
$submit_button_setting = array(
'name' => 'submitButtonLabel',
'label' => esc_html__( 'Submit Button Label' ),
'type' => 'text',
'tooltip' => '<h6>' . esc_html__( 'Submit Button Label' ) . '</h6>' . esc_html__( 'Specify a custom label for the submit button that will be used if this feed will be used to process the payment.' ),
);
$stripe = GFStripe::get_instance();
$settings = $stripe->add_field_after( 'conditionalLogic', $submit_button_setting, $settings );
return $settings;
}
public function add_submit_button_setting_to_frontend_feed( $feed, $form, $raw_feed ) {
$feed['submitButtonLabel'] = rgars( $raw_feed, 'meta/submitButtonLabel', rgars( $form, 'button/text' ) );
return $feed;
}
public function load_form_script( $form, $is_ajax_enabled ) {
if ( $this->is_applicable_form( $form ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) {
add_action( 'wp_footer', array( $this, 'output_script' ), 99 );
add_action( 'gform_preview_footer', array( $this, 'output_script' ), 99 );
}
return $form;
}
public function output_script() {
?>
<script type="text/javascript">
( function( $ ) {
gform.addAction( 'gform_frontend_feeds_evaluated', function( feeds, formId ) {
var $submitButton = $( '#gform_submit_button_{0}'.gformFormat( formId ) ),
originalLabel = $submitButton.data( 'default-label' );
if( originalLabel ) {
$submitButton.val( originalLabel );
}
for( var i = 0; i < feeds.length; i++ ) {
if( feeds[i].isActivated ) {
$submitButton
.data( 'default-label', $submitButton.val() )
.val( feeds[i].submitButtonLabel );
break;
}
}
}, 11 );
} )( jQuery );
</script>
<?php
}
public function is_applicable_form( $form ) {
/* @var GFStripe $stripe */
$stripe = GFStripe::get_instance();
return $stripe->has_frontend_feeds( $form );
}
}
function gw_feed_specific_submit_button() {
return GW_Feed_Specific_Submit_Button::get_instance();
}
gw_feed_specific_submit_button();