Created
March 30, 2020 19:36
-
-
Save wpmudev-sls/a42d7a0fa2021953dcfa276516aa02f2 to your computer and use it in GitHub Desktop.
[Forminator Pro] - Custom submission lookup
This file contains 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 Pro] - Custom submission lookup | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Generate a unique code and access form submission using it (as of 1.12.1) | |
* Author: Alessandro Kaounas @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* Task: 0/11289012348292/1168805940889796 | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
// No need to do anything if the request is via WP-CLI. | |
if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
return; | |
} | |
if ( ! class_exists( 'WPMUDEV_Forminator_Custom_Submission_Lookup' ) ) { | |
class WPMUDEV_Forminator_Custom_Submission_Lookup { | |
// Unique code length | |
private $length = 6; | |
// Source Form | |
private $s_form_id = 492; | |
private $s_form_field = 'hidden-1'; | |
// Target Form | |
private $t_form_id = 494; | |
private $t_form_field = 'text-1'; | |
private $generations = 0; | |
private static $_instance = null; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_Forminator_Custom_Submission_Lookup(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
$this->init(); | |
} | |
public function init(){ | |
add_filter( 'forminator_custom_form_submit_field_data', array( $this, 'wpmudev_randomize_hidden_input' ), 10, 2 ); | |
add_filter( 'forminator_custom_form_submit_errors', array( $this, 'wpmudev_forminator_custom_form_submit_errors' ), 10, 3 ); | |
add_action( 'forminator_custom_form_after_save_entry', array( $this, 'wpmudev_forminator_custom_form_after_save_entry' ) ); | |
} | |
public function wpmudev_forminator_custom_form_after_save_entry( $form_id ){ | |
if( $this->s_form_id != $form_id ){ | |
return; | |
} | |
$entries = Forminator_API::get_entries( $form_id ); | |
if( is_array( $entries ) ){ | |
$entry = $entries[0]; | |
if( $entry instanceof Forminator_Form_Entry_Model ){ | |
$code = $entry->get_meta( $this->s_form_field ); | |
} | |
} | |
if( ! isset( $code ) ){ | |
return; | |
} | |
wp_send_json_success( | |
array( | |
'success' => true, | |
'message' => sprintf( __( "Form submitted with unique code: <strong>%s</strong>", Forminator::DOMAIN ), $code ), | |
'behav' => 'behaviour-thankyou' | |
) | |
); | |
} | |
public function wpmudev_forminator_custom_form_submit_errors( $errors, $form_id, $field_data_array ){ | |
if( $form_id != $this->t_form_id ){ | |
return; | |
} | |
global $wpdb; | |
foreach( $field_data_array as $key => $field ){ | |
if( $field['name'] === $this->t_form_field ){ | |
$code = trim( $field['value'] ); | |
} | |
} | |
if( ! isset( $code ) ){ | |
return; | |
} | |
$query = "SELECT entry_id FROM {$wpdb->prefix}frmt_form_entry_meta | |
WHERE meta_key LIKE '{$this->s_form_field}' AND `meta_value` LIKE '{$code}';"; | |
$entry_id = $wpdb->get_var( $query ); | |
$entry = Forminator_API::get_entry( $form_id, $entry_id ); | |
$form = Forminator_API::get_form( $this->s_form_id ); | |
$html = ''; | |
foreach( $form->get_fields() as $key => $field ){ | |
if( $field->slug == $this->s_form_field ){ | |
continue; | |
} | |
$html .= '<label for="forminator-field-text-' . $key . '" class="forminator-label">' . $field->field_label. '</label>'; | |
$html .= '<p>' . $entry->get_meta( (string) $field->slug ) . '</p>'; | |
} | |
wp_send_json_success( array( | |
'success' => true, | |
'message' => $html, | |
'behav' => 'behaviour-thankyou' | |
)); | |
} | |
public function wpmudev_randomize_hidden_input( $field_data_array, $form_id ) { | |
if( (int) $form_id !== (int) $this->s_form_id ) { | |
return $field_data_array; | |
} | |
foreach( $field_data_array as $key => $field ){ | |
if( $field['name'] === $this->s_form_field ){ | |
unset( $field_data_array[$key] ); | |
} | |
} | |
$field_data_array[] = array( | |
'name' => $this->s_form_field, | |
'value' => $this->wpmudev_randomizer() | |
); | |
return $field_data_array; | |
} | |
public function wpmudev_is_unique( $num = '' ) { | |
global $wpdb; | |
$query = "SELECT COUNT(*) FROM {$wpdb->prefix}frmt_form_entry_meta | |
WHERE meta_key LIKE '{$this->s_form_field}' | |
AND meta_value LIKE '{$num}' | |
AND entry_id IN( SELECT entry_id | |
FROM {$wpdb->prefix}frmt_form_entry WHERE form_id = {$this->s_form_id} );"; | |
if( 0 === (int) $wpdb->get_var( $query ) ){ | |
return true; | |
}else{ | |
$this->$generations++; | |
return false; | |
} | |
} | |
public function wpmudev_randomizer() { | |
$num = ''; | |
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
for ($i = 0; $i < $this->length; $i++) { | |
$num .= $characters[rand(0, strlen( $characters ) - 1)]; | |
} | |
if( $this->$generations > pow( strlen( $characters ), $this->length ) ){ | |
return null; | |
} | |
if( (int) $num === 0 ){ | |
return $this->wpmudev_randomizer(); | |
} | |
if( $this->wpmudev_is_unique( $num ) ) { | |
return $num; | |
} else { | |
return $this->wpmudev_randomizer(); | |
} | |
} | |
} | |
add_action( 'plugins_loaded', function(){ | |
return WPMUDEV_Forminator_Custom_Submission_Lookup::get_instance(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment