Skip to content

Instantly share code, notes, and snippets.

@dexit
Forked from pingram3541/functions.php
Created May 7, 2024 16:42
Show Gist options
  • Save dexit/edc2b7d83d51b0d16f1a898519c4359a to your computer and use it in GitHub Desktop.
Save dexit/edc2b7d83d51b0d16f1a898519c4359a to your computer and use it in GitHub Desktop.
Elementor Pro Forms Custom Action - Twilio
<?php
/**
* Twilio Elementor Pro form integration
* requirements:
* - php7.2 or greater
* - twilio php helper library - https://github.com/twilio/twilio-php/archive/main.zip
* - copy into theme folder /twilio and ref below on init
*/
add_action( 'elementor_pro/init', function() {
// Iinclude our action class file and twilio support library
include_once( get_stylesheet_directory() . '/twilio/twilio-php-main/src/Twilio/autoload.php' );
include_once( get_stylesheet_directory() . '/inc/twilio.php' );
// Instantiate the action class
$twilio_action = new Twilio_Action_After_Submit();
// Register the action with form widget
\ElementorPro\Plugin::instance()->modules_manager->get_modules( 'forms' )->add_form_action( $twilio_action->get_name(), $twilio_action );
});
<?php
use Twilio\Rest\Client; //see functions.php
/**
* Class Twilio_Action_After_Submit
* @see https://developers.elementor.com/custom-form-action/
* @see - https://www.twilio.com/docs/libraries/php
* Custom elementor form action after submit to send an SMS
* confirmation to number submitted in the form
*/
class Twilio_Action_After_Submit extends \ElementorPro\Modules\Forms\Classes\Action_Base {
/**
* Get Name
*
* Return the action name
*
* @access public
* @return string
*/
public function get_name() {
return 'twilio';
}
/**
* Get Label
*
* Returns the action label
*
* @access public
* @return string
*/
public function get_label() {
return __( 'Twilio', 'text-domain' );
}
/**
* Run
*
* Runs the action after submit
*
* @access public
* @param \ElementorPro\Modules\Forms\Classes\Form_Record $record
* @param \ElementorPro\Modules\Forms\Classes\Ajax_Handler $ajax_handler
*/
public function run( $record, $ajax_handler ) {
$settings = $record->get( 'form_settings' );
//make sure it's the right form
$form_name = $settings['form_name'];
if ( 'CTA Form' !== $form_name ) { //replace with actual form name
return;
}
// Make sure that there is a Twilio SID
if ( empty( $settings['twilio_sid'] ) ) {
return;
}
// Make sure that there is a Twilio auth token
if ( empty( $settings['twilio_auth_token'] ) ) {
return;
}
// Make sure that there is a Twilio sending number
if ( empty( $settings['twilio_subscriber'] ) ) {
return;
}
// Get submitetd Form data
$raw_fields = $record->get( 'fields' );
// Normalize the Form Data
$fields = [];
foreach ( $raw_fields as $id => $field ) {
$fields[ $id ] = $field['value'];
}
/**
* Twilio integration - sends SMS to applicant's phone number
*
**/
$twilio_enabled = true;
if ( true === $twilio_enabled )
{
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = $settings['twilio_sid'];
$token = $settings['twilio_auth_token'];
$client = new Client($sid, $token);
$twilio_number = $settings['twilio_subscriber']; //telephone number
//send our confirmation
$client->messages->create(
'+1' . $fields['cta_phone'], //replace with your field id
array(
'from' => $twilio_number,
'body' => 'The website says: Thank you '. $fields['cta_first_name'] .' ' . $fields['cta_last_name'] . ', we\'ve received your information, a representative will contact you shortly.'
) //replace fields above with your own field id's
);
}
}
/**
* Register Settings Section
*
* Registers the Action controls
*
* @access public
* @param \Elementor\Widget_Base $widget
*/
public function register_settings_section( $widget ) {
$widget->start_controls_section(
'section_twilio',
[
'label' => __( 'Twilio', 'text-domain' ),
'condition' => [
'submit_actions' => $this->get_name(),
],
]
);
$widget->add_control(
'twilio_sid',
[
'label' => __( 'Twilio SID', 'text-domain' ),
'type' => \Elementor\Controls_Manager::TEXT,
'placeholder' => 'you account sid',
'label_block' => true,
'separator' => 'before',
'description' => __( 'Enter the sid of your Twilio app', 'text-domain' ),
]
);
$widget->add_control(
'twilio_auth_token',
[
'label' => __( 'Twilio Auth Token', 'text-domain' ),
'type' => \Elementor\Controls_Manager::TEXT,
'separator' => 'before',
'description' => __( 'the auth token used to authorize the data transfer.', 'text-domain' ),
]
);
$widget->add_control(
'twilio_subscriber',
[
'label' => __( 'Twilio Subscriber Number', 'text-domain' ),
'type' => \Elementor\Controls_Manager::TEXT,
'separator' => 'before',
'description' => __( 'the sending telephone number of your Twilio app.', 'text-domain' ),
]
);
$widget->end_controls_section();
}
/**
* On Export
*
* Clears form settings on export
* @access Public
* @param array $element
*/
public function on_export( $element ) {
unset(
$element['twilio_sid'],
$element['twilio_auth_token'],
$element['twilio_subscriber']
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment