|
<?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($){ |
|
|
|
// 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; |
|
} |
|
|
|
});" ); |
|
|
|
} |
|
|
|
} |
|
|
|
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 ); |
|
|
|
} |
|
|
|
} |