Forked from wpmudev-sls/forminator-entry-id-in-response.php
Created
June 13, 2021 17:50
-
-
Save patrickfreitasdev/d106756e7ce0c2916849fe508f8fe34b to your computer and use it in GitHub Desktop.
[Forminator] - Submission id in response messag. This snippet allows to use the submission id in response messages. The following macro can be used in the content `{submission_id}`
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] - Submission id in response message | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: This snippet allows to use the submission id in response messages. The following macro can be used in the content `{submission_id}` | |
* Author: Panos Lyrakis @ 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_Submission_ID_In_Response' ) ) { | |
class WPMUDEV_Forminator_Submission_ID_In_Response { | |
private static $_instance = null; | |
private $macro = '{submission_id}'; | |
public static function get_instance() { | |
if ( is_null( self::$_instance ) ) { | |
self::$_instance = new WPMUDEV_Forminator_Submission_ID_In_Response(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
global $wpdb; | |
$this->forminator_entry_meta_tbl = "{$wpdb->prefix}frmt_form_entry_meta"; | |
add_action( 'forminator_custom_form_submit_before_set_fields', array( $this, 'temporize_entry_id' ), 20, 3 ); | |
add_filter( 'forminator_form_ajax_submit_response', array( $this, 'inject_entry_to_response' ), 20 ); | |
add_filter( 'forminator_form_submit_response', array( $this, 'inject_entry_to_response' ), 20 ); | |
} | |
/* | |
** Sets the entry id in a temporary $_POST key, _forminator_temp_entry_id | |
** By default it is not available. Setting it to the $_POST Global Var we can add it to ajax | |
** response on forminator_form_ajax_submit_response filter | |
*/ | |
public function temporize_entry_id( $entry, $form_id, $field_data_array ) { | |
$form = Forminator_Form_Model::model()->load( $form_id ); | |
$_POST['_forminator_temp_entry_id'] = $entry->entry_id; | |
$_POST['_forminator_temp_form_id'] = $form_id; | |
} | |
/* | |
** Inject the form id and entry id in ajax response | |
*/ | |
public function inject_entry_to_response( $response ) { | |
if ( isset( $_POST['_forminator_temp_entry_id'] ) && is_numeric( $_POST['_forminator_temp_entry_id'] ) && | |
isset( $_POST['_forminator_temp_form_id'] ) && is_numeric( $_POST['_forminator_temp_form_id'] ) | |
) { | |
$entry_id = (int) $_POST['_forminator_temp_entry_id']; | |
$response['entry_id'] = $entry_id; | |
$response['form_id'] = (int) $_POST['_forminator_temp_form_id']; | |
$response['message'] = str_replace( $this->macro, $entry_id, $response['message'] ); | |
} | |
return $response; | |
} | |
} | |
if ( ! function_exists( 'wpmudev_forminator_submission_id_in_response' ) ) { | |
function wpmudev_forminator_submission_id_in_response() { | |
return WPMUDEV_Forminator_Submission_ID_In_Response::get_instance(); | |
}; | |
add_action( 'plugins_loaded', 'wpmudev_forminator_submission_id_in_response', 10 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment