Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save wpmudev-sls/de257c426e2c00247aa6c24874c3ed2e to your computer and use it in GitHub Desktop.
Save wpmudev-sls/de257c426e2c00247aa6c24874c3ed2e to your computer and use it in GitHub Desktop.
[Forminator] - Toggle PayPal button. Extend existing functionality of Forminator to include a conditional submit and PayPal button
<?php
/**
* Plugin Name: [Forminator] - Add submit button to an existing form with PayPal button
* Plugin URI: https://premium.wpmudev.org/
* Description: Extend existing functionality of Forminator to include a conditional submit and PayPal button (as of 1.9)
* Author: Alessandro Kaounas @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_Forminator_PayPal_Submit_Buttons' ) ) {
class WPMUDEV_Forminator_PayPal_Submit_Buttons {
private $module_id = 1135;
private $checkbox = 'checkbox-1';
private static $_instance = null;
public static function get_instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new WPMUDEV_Forminator_PayPal_Submit_Buttons();
}
return self::$_instance;
}
private function __construct() {
$this->init();
}
private function init() {
add_filter( 'forminator_render_form_markup', array( $this, 'wpmudev_add_submit_button' ), 10, 6 );
add_action( 'wp_enqueue_scripts', array( $this, 'wpmudev_theme_prefix_enqueue_script' ) );
}
public function wpmudev_add_submit_button( $html, $form_fields, $form_type, $form_settings, $form_design, $render_id ) {
if ( $form_settings['form_id'] != $this->module_id ) {
return $html;
}
$submit = '<div class="forminator-row"><div class="forminator-col"><div id="submit" class="forminator-field"><button class="forminator-button forminator-button-submit">Send Message</button></div></div></div>';
$html = str_replace( '</form>', $submit . '</form>', $html );
return $html;
}
public function wpmudev_theme_prefix_enqueue_script() {
if ( ! wp_script_is( 'jquery', 'done' ) ) {
wp_enqueue_script( 'jquery' );
}
wp_add_inline_script(
'jquery',
"
jQuery(document).ready(function($){
function __wpmudev_toogle_paypal_button() {
// Module setup
var form = $('#forminator-module-' + {$this->module_id});
var checkbox = form.find('.forminator-row .forminator-field input[name=\"{$this->checkbox}[]\"]');
// Initial values
var st_button = form.find('.forminator-row .forminator-field .forminator-button-submit');
// If submit button missing, return
if(!st_button.length) return;
var pp_button = form.find('.forminator-button-paypal').closest('.forminator-row');
var btn_status = checkbox.is(':checked');
// On load
forminator_{$this->module_id}_toggle_buttons(btn_status, pp_button, st_button);
// Event listener on change
checkbox.on('change', function(){
var pp_status = $(this).is(':checked');
forminator_{$this->module_id}_toggle_buttons(pp_status, pp_button, st_button);
});
}
// Function to handle show/hide
function forminator_{$this->module_id}_toggle_buttons(status, btn_p, btn_s){
if(status){
btn_p.show();
btn_s.hide();
}else{
btn_p.hide();
btn_s.show();
}
return;
}
$(document).ajaxComplete( function( event, xhr, settings ) {
__wpmudev_toogle_paypal_button();
});
__wpmudev_toogle_paypal_button();
});"
);
}
}
if ( ! function_exists( 'WPMUDEV_Forminator_PayPal_Submit_Buttons' ) ) {
function WPMUDEV_Forminator_PayPal_Submit_Buttons() {
return WPMUDEV_Forminator_PayPal_Submit_Buttons::get_instance();
};
add_action( 'plugins_loaded', 'WPMUDEV_Forminator_PayPal_Submit_Buttons', 99 );
}
}
@1aheed
Copy link

1aheed commented Jun 2, 2021

where do I write this code?

@panoslyrakis
Copy link

Hi @1aheed ,
Currently you probably don't need this snippet as Forminator has visibility conditions built in for PayPal button.

To answer your question though, this snippet can be used as a mu-plugin. If you are not familiar with mu-plugins you can read more about them here:
https://premium.wpmudev.org/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

But as mentioned you don't need this snippet anymore as you can toggle PayPal buttons visibility with built in visibility rules.

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment