Created
April 30, 2024 17:18
-
-
Save alfredo-wpmudev/d11745ae73c824822a5c9a7c7c1b4f2d to your computer and use it in GitHub Desktop.
A way to modify the values of data that will be send in Webhook, in this specific case the submission ID field is not sending the Entry ID, it's sending a default text
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] - Replace Submission ID before send Webook request | |
* Plugin URI: https://wpmudev.com/ | |
* Description: Replace Submission ID before send Webook request | |
* Author: Alfredo Galano Loyola @ WPMUDEV | |
* Author URI: https://wpmudev.com/ | |
* 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_SubmissionID_Webhook_Fix')) { | |
class WPMUDEV_Forminator_SubmissionID_Webhook_Fix | |
{ | |
/** | |
* $forms used to define the form and the hidden field that will be used. | |
* It's possible use the feature in several forms using the format | |
* formID =>Hidden_Field_Name | |
* The Hidden Field is the one set to use the Submission ID value | |
*/ | |
private $forms = array( | |
"1049461" => 'hidden-1' | |
); | |
private $form_fields; | |
private static $_instance = null; | |
public static function get_instance() | |
{ | |
if (is_null(self::$_instance)) { | |
self::$_instance = new WPMUDEV_Forminator_SubmissionID_Webhook_Fix(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() | |
{ | |
$this->init(); | |
} | |
public function init() | |
{ | |
// Do some checks here | |
if (!defined('FORMINATOR_VERSION') || FORMINATOR_VERSION < '1.12' || !class_exists('Forminator_API')) { | |
return; | |
} | |
add_filter('forminator_addon_webhook_api_request_data', array($this, 'replace_webhook_arg_data'), 1, 3); | |
} | |
public function replace_webhook_arg_data($args, $verb, $path){ | |
if(isset($this->forms[$_REQUEST["form_id"]])){ | |
$this->form_fields = $this->forms[$_REQUEST["form_id"]]; | |
if(isset($args[$this->form_fields]) && $args[$this->form_fields]==="submission_id") | |
$entry = Forminator_Form_Entry_Model::get_latest_entry_by_form_id( $_REQUEST["form_id"]); | |
$args[$this->form_fields]=$entry->entry_id; | |
} | |
return $args; | |
} | |
} | |
add_action('plugins_loaded', function () { | |
return WPMUDEV_Forminator_SubmissionID_Webhook_Fix::get_instance(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
var_dump($arg) looks like this:
array(16) { ["name-1"]=> string(9) "Alfredito" ["hidden-1"]=> string(13) "submission_id" ["hidden-2"]=> string(7) "1049461" ["referer_url"]=> string(0) "" ["_wp_http_referer"]=> string(26) "/submission-id-to-zappier/" ["page_id"]=> string(7) "1049462" ["form_type"]=> string(7) "default" ["current_url"]=> string(54) "https://myblank.tempurl.host/submission-id-to-zappier/" ["render_id"]=> string(1) "0" ["forminator_current_location"]=> string(22) "11.9668736 -86.1011968" ["forminator_current_address"]=> string(0) "" ["forminator_current_location_error"]=> string(0) "" ["_forminator_user_ip"]=> NULL ["geolocation"]=> NULL ["form-title"]=> string(24) "Submission ID To Zappier" ["entry-time"]=> string(19) "2024-04-30 10:28:51" }