Last active
January 25, 2021 07:45
-
-
Save ohid/4648be6071b66945ac2bab546fbb1656 to your computer and use it in GitHub Desktop.
[Forminator] - Custom Form Redirect Based On Submitted Form Data
This file contains hidden or 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: [Forminator] - Custom Form Redirect Based On Submitted Form Data | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Adds custom redirect to Forms based on submitted data | |
* Task: SLS-1147 | |
* Version: 1.0.0 | |
* Author: Panos Lyrakis & Ohid @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
return; | |
} | |
if ( ! class_exists( 'WPMUDEV_Forminator_CustomForm_Redirect' ) ) { | |
class WPMUDEV_Forminator_CustomForm_Redirect { | |
private $form_id = [32, 39]; | |
private $initial_consultation = 'http://superdev.test/redirect-url-one'; | |
private $spiritual_guidance = 'http://superdev.test/redirect-url-two'; | |
private $intuitive_guidance_coaching_pryzma_clarity_session = 'http://superdev.test/redirect-url-three'; | |
private $pryzma_life = 'http://superdev.test/redirect-url-four'; | |
private $pryzma_pathway = 'http://superdev.test/redirect-url-five'; | |
private $power_session = 'http://superdev.test/redirect-url-six'; | |
private $monthly_power_sessions = 'http://superdev.test/redirect-url-seven'; | |
private $pilot_true_ii_mentorship_programme = 'http://superdev.test/redirect-url-eight'; | |
private $redirect_url = ''; | |
private static $_instance = null; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_Forminator_CustomForm_Redirect(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
add_filter( 'forminator_custom_form_submit_response', array( $this, 'handle_response' ), 20, 2 ); | |
add_filter( 'forminator_custom_form_ajax_submit_response', array( $this, 'handle_response' ), 20, 2 ); | |
} | |
public function handle_response( $response, $form_id ) { | |
if ( ! $this->is_valid_form( $form_id ) ) { | |
return $response; | |
} | |
$response['url'] = $this->calc_url(); | |
return $response; | |
} | |
private function calc_url( $data = null ) { | |
$data = ! is_null( $data ) ? $data : $_POST; | |
$fieldname_part = 'select'; | |
foreach ( $data as $field_name => $field_data ) { | |
if ( substr( $field_name, 0, strlen( $fieldname_part ) ) === $fieldname_part ) { | |
switch ( $field_data ) { | |
case 'initial-consultation-29-for-30-minutes' : | |
$this->redirect_url = $this->initial_consultation; | |
break; | |
case 'spiritual-guidance-200-for-60-minutes' : | |
$this->redirect_url = $this->spiritual_guidance; | |
break; | |
case 'intuitive-guidance-coaching-pryzma-clarity-session-50000-2-hours' : | |
$this->redirect_url = $this->intuitive_guidance_coaching_pryzma_clarity_session; | |
break; | |
case 'pryzma-life-mapping-50000-2-hours' : | |
$this->redirect_url = $this->pryzma_life; | |
break; | |
case 'pryzma-pathway-plan-97500-2-x-2hours-sessions' : | |
$this->redirect_url = $this->pryzma_pathway; | |
break; | |
case '30minute-power-session-with-keda-6000' : | |
$this->redirect_url = $this->power_session; | |
break; | |
case 'monthly-power-sessions-annual-19900' : | |
$this->redirect_url = $this->monthly_power_sessions; | |
break; | |
case 'pilot-true-ii-mentorship-programme-250000' : | |
$this->redirect_url = $this->pilot_true_ii_mentorship_programme; | |
break; | |
} | |
} | |
} | |
return $this->redirect_url; | |
} | |
private function is_valid_form( $form_id ) { | |
if ( is_array( $this->form_id ) ) { | |
return in_array( (int) $form_id, $this->form_id ); | |
} else { | |
return (int) $form_id === $this->form_id; | |
} | |
} | |
} | |
if ( ! function_exists( 'wpmudev_forminator_customform_redirect' ) ) { | |
function wpmudev_forminator_customform_redirect(){ | |
return WPMUDEV_Forminator_CustomForm_Redirect::get_instance(); | |
}; | |
add_action( 'plugins_loaded', 'wpmudev_forminator_customform_redirect', 99 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment