79194999

Date: 2024-11-16 11:05:58
Score: 0.5
Natty:
Report link

A more structured way (wordpress way) of doing the web hook basing on @oscar's amazing answer, would be to utilize a plug that can help manage web hooks such as Bit Integrations.

First a modification need to be done to the following path as @oscar mentioned.

plugins/elementor-pro/modules/forms/submissions/actions/save-to-database.php

the modification is by simply adding the following line

$record->set('submission_id', $this->submission_id);

exactly after

$this->submission_id = Query::get_instance()->add_submission( [
            'main_meta_id' => 0,
            'post_id' => $post_id,
            'referer' => remove_query_arg(
                [ 'preview_id', 'preview_nonce', 'preview' ],
                $meta['page_url']['value']
            ),
            'referer_title' => $meta['page_title']['value'],
            'element_id' => $element_id,
            'form_name' => $form_name,
            'campaign_id' => 0,
            'user_id' => get_current_user_id(),
            'user_ip' => $meta['remote_ip']['value'],
            'user_agent' => $meta['user_agent']['value'],
            'actions_count' => $actions_count,
            'actions_succeeded_count' => 0,
            'meta' => wp_json_encode( [
                // TODO: Should be removed if there is an ability to edit "global widgets"
                'edit_post_id' => $record->get_form_settings( 'edit_post_id' ),
            ] ),
        ], $record->get_field( null ) );

now instead of calling the hook customly through function.php n the theme which could risk in breaking the theme if was edited incorrectly. We shall use the bit integration plugin. After installing the plugin, we go to the plugin code files to allow it to read the added piece of data which is the submission id.

First we go to this file

plugins/bit-integrations/includes/Triggers/Elementor/t ElementorHelper.php

we modify the following functions :

public static function extractRecordData($record)
    {
        return [
            'id'           => $record->get_form_settings('id'),
            'form_post_id' => $record->get_form_settings('form_post_id'),
            'edit_post_id' => $record->get_form_settings('edit_post_id'),
            'submission_id' => $record->get('submission_id'),
            'fields'       => $record->get('fields'),
            'files'        => $record->get('files'),
        ];
    }

and scroll down a bit to modify the second function that handles the retrieved data and passes it to the UI to be seen by the user while setting up the hook in bit integrations plugin.

public static function setFields($formData)
    {
        $allFields = [
            ['name' => 'id', 'type' => 'text', 'label' => wp_sprintf(__('Form Id (%s)', 'bit-integrations'), $formData['id']), 'value' => $formData['id']],
            ['name' => 'form_post_id', 'type' => 'text', 'label' => wp_sprintf(__('Form Post Id (%s)', 'bit-integrations'), $formData['form_post_id']), 'value' => $formData['form_post_id']],
            ['name' => 'edit_post_id', 'type' => 'text', 'label' => wp_sprintf(__('Edit Post Id (%s)', 'bit-integrations'), $formData['edit_post_id']), 'value' => $formData['edit_post_id']],
            ['name' => 'submission_id', 'type' => 'text', 'label' => wp_sprintf(__('Submission Id (%s)', 'bit-integrations'), $formData['submission_id']), 'value' => $formData['submission_id']],
        ];

//... rest of the code

To understand where those functions are called we can check the controller PHP file as follows:

plugins/bit-integrations/includes/Triggers/Elementor/t ElementorController.php

public static function handle_elementor_submit($record)
    {
        $recordData = ElementorHelper::extractRecordData($record);
        $formData = ElementorHelper::setFields($recordData);
        $reOrganizeId = $recordData['id'] . $recordData['form_post_id'];

        if (get_option('btcbi_elementor_test') !== false) {
            update_option('btcbi_elementor_test', [
                'formData'   => $formData,
                'primaryKey' => [(object) ['key' => 'id', 'value' => $recordData['id']]]
            ]);
        }

        $flows = ElementorHelper::fetchFlows($recordData['id'], $reOrganizeId);
        if (!$flows) {
            return;
        }

        foreach ($flows as $flow) {
            $flowDetails = static::parseFlowDetails($flow->flow_details);

            if (!isset($flowDetails->primaryKey) && ($flow->triggered_entity_id == $recordData['id'] || $flow->triggered_entity_id == $reOrganizeId)) {
                $data = ElementorHelper::prepareDataForFlow($record);
                Flow::execute('Elementor', $flow->triggered_entity_id, $data, [$flow]);

                continue;
            }

            if (\is_array($flowDetails->primaryKey) && ElementorHelper::isPrimaryKeysMatch($recordData, $flowDetails)) {
                $data = array_column($formData, 'value', 'name');
                Flow::execute('Elementor', $flow->triggered_entity_id, $data, [$flow]);
            }
        }

        return ['type' => 'success'];
    }

Example of the hooked data: enter image description here

voilla, works like a charm.

special thanks to @oscar for his insight and ope it saved someone's time.

Reasons:
  • Blacklisted phrase (0.5): thanks
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @oscar's
  • User mentioned (0): @oscar
  • User mentioned (0): @oscar
  • Low reputation (0.5):
Posted by: Osama Hussein