Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alfredo-wpmudev/cd9144aca11ae87c6e5d00d6ef80784f to your computer and use it in GitHub Desktop.
Save alfredo-wpmudev/cd9144aca11ae87c6e5d00d6ef80784f to your computer and use it in GitHub Desktop.
Dummy example of how to use Forminator Pro to send fields as an Array to external API
<?php
/**
* Plugin Name: [Forminator Pro] - AWS Price Prediction
* Plugin URI: https://wpmudev.com/
* Description: AWS Price Prediction
* 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_AWS_Price_Prediction')) {
class WPMUDEV_Forminator_AWS_Price_Prediction
{
/**
* $forms used to define the form and the hidden field that will be used. It's possible use the feature in several forms
* $forms = array(
* 1048231 => 'hidden-1',
* 235 => 'hidden-2',
* 1257 => 'hidden-1',
* );
*/
private $forms = array(
1049266 => 'hidden-1',
);
//List of fields and what they represent in the AWS server
private $fields_relations = array(
'number-1' => 'Price',
'date-1' => 'Date',
'number-3' => 'Unemployment_Rate_Next',
'number-2'=> 'RPI_Rate_Next',
'number-10' => 'FTSE100_Rate_Next',
'number-5' => 'Exchange_Rate_Next',
'number-4'=> 'Employment_Rate_Next' ,
'number-11' => 'CPIH_Rate_Next',
'number-6' => 'Bank_Rate_Next',
'number-7'=> 'Yearly_GDP_Rate_Next',
'number-8' => 'Quarterly_GDP_CVM_Rate_Next',
'number-9' => 'Quarterly_GDP_Rate_Next'
);
private $input_data = [];
private $price_prediction;
private static $_instance = null;
public static function get_instance()
{
if (is_null(self::$_instance)) {
self::$_instance = new WPMUDEV_Forminator_AWS_Price_Prediction();
}
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_custom_form_submit_field_data', array($this, 'wpmudev_set_field_data'), 10, 2);
add_filter( 'forminator_replace_form_data',array($this, 'wpmudev_replace_form_data') , 10, 3 );
}
//Working with the submitted Data
public function wpmudev_set_field_data($field_data_array, $form_id)
{
if (!in_array($form_id, array_keys($this->forms))) {
return $field_data_array;
}
foreach ($field_data_array as $key => $field){
//Removing the Hidden Field
if ($field['name'] === $this->forms[$form_id]) {
unset($field_data_array[$key]);
}else{
//Filling the Data that will be sended to the AWS server
$this->input_data[$this->fields_relations[$field['name']]] = $field['value'];
}
}
//Getting the PRICE from the AWS site
$resp = $this->external_api_request($this->input_data);
//Checking if the price was calculated OK
if (isset($resp['prediction'])) {
$this->price_prediction = $resp['prediction']; //Show the price if everything going well.
}else{
$this->price_prediction = $resp['message']; //Show error message is something goest wrong.
}
//Recreating the Hidden Field
$field_data_array[] = array(
'name' => $this->forms[$form_id],
'value' => $this->price_prediction,
'field_array' => array(
'default_value' => 'custom_value',
'custom_value' => $this->price_prediction
)
);
return $field_data_array;
}
//This is what you need to customize to get the data from AWS
function external_api_request($input_data){
// Convert the input data to JSON format
$input_json = json_encode($input_data);
// Set the URL of the Flask API endpoint running on your EC2 instance
$api_url = 'https://myblank.tempurl.host/price-calculator.php'; // Replace with your EC2 public IP address
// Set up the POST request options
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/json',
'content' => $input_json
)
);
// Create a stream context to send the POST request
$context = stream_context_create($options);
// Send the POST request to the Flask API endpoint
$response = file_get_contents($api_url, false, $context);
// Check if the response is received successfully
if ($response === false) {
// Handle error if the response is not received
return array(
'success' => false,
'message' => 'Error: Unable to connect to the Flask API endpoint.'
);
} else {
// Decode the JSON response
$result = json_decode($response, true);
// Check if the predicted price is returned in the response
if (isset($result['predicted_price'])) {
// Extract the predicted price from the response
$predicted_price = $result['predicted_price'];
// Update the response array with the predicted price
$resp['success'] = true;
$resp['message'] = 'Prediction successful';
$resp['prediction'] = $predicted_price;
// Return the updated response
return $resp;
} else {
// Handle error if the predicted price is not returned
return array(
'success' => false,
'message' => 'Error: Predicted price not found in the response.'
);
}
}
}
//To replace the price prediction in the form where is used {price_predict}
function wpmudev_replace_form_data( $content, $data, $original_content ){
if (!in_array(intval($data['form_id']), array_keys($this->forms))) { // Please change the form ID
return $content;
}
if ( strpos( $content, '{price_predict}' ) !== false ) {
$content = str_replace( '{price_predict}', $this->price_prediction, $content );
}
return $content;
}
}
add_action('plugins_loaded', function () {
return WPMUDEV_Forminator_AWS_Price_Prediction::get_instance();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment