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