| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || die(); |
| 4 |
|
| 5 |
use Sellkit_Elementor_Optin_Ajaxhandler as AjaxHandler; |
| 6 |
|
| 7 |
/** |
| 8 |
* Initializing the WebHook action by extending Action_Base. |
| 9 |
* |
| 10 |
* @since 1.5.0 |
| 11 |
*/ |
| 12 |
class Sellkit_Elementor_Optin_Action_Webhook extends Sellkit_Elementor_Optin_Action_Base { |
| 13 |
|
| 14 |
private $exclude_fields = []; |
| 15 |
|
| 16 |
public static $instance; |
| 17 |
|
| 18 |
public static function get_instance() { |
| 19 |
if ( empty( static::$instance ) ) { |
| 20 |
static::$instance = new static(); |
| 21 |
} |
| 22 |
|
| 23 |
return static::$instance; |
| 24 |
} |
| 25 |
|
| 26 |
public function get_name() { |
| 27 |
return 'webhook'; |
| 28 |
} |
| 29 |
|
| 30 |
public function get_title() { |
| 31 |
return esc_html__( 'WebHook', 'sellkit' ); |
| 32 |
} |
| 33 |
|
| 34 |
public function add_controls( $widget ) { |
| 35 |
|
| 36 |
$description = sprintf( '%1$s<a href="https://zapier.com/apps/webhook/integrations" target="_blank">%2$s</a>.', |
| 37 |
esc_html__( 'Enter the webhook URL where you want to send your Form data after submit e.g. ', 'sellkit' ), |
| 38 |
esc_html__( 'Integrate with Zapier Webhook', 'sellkit' ) |
| 39 |
); |
| 40 |
|
| 41 |
$widget->add_control( 'webhook_url', |
| 42 |
[ |
| 43 |
'label' => esc_html__( 'Webhook URL', 'sellkit' ), |
| 44 |
'type' => 'text', |
| 45 |
'label_block' => true, |
| 46 |
'placeholder' => 'http://webhook-endpoint.com', |
| 47 |
'description' => $description, |
| 48 |
] |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
public function run( AjaxHandler $ajax_handler ) { |
| 53 |
$settings = $ajax_handler->form['settings']; |
| 54 |
|
| 55 |
if ( empty( $settings['webhook_url'] ) ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
$body = $this->get_form_data( $ajax_handler, $settings ); |
| 60 |
$args = [ 'body' => wp_json_encode( $body ) ]; |
| 61 |
|
| 62 |
$response = wp_remote_post( $settings['webhook_url'], $args ); |
| 63 |
|
| 64 |
$response_code = (int) wp_remote_retrieve_response_code( $response ); |
| 65 |
|
| 66 |
if ( $response_code < 200 || $response_code >= 300 ) { |
| 67 |
$ajax_handler->add_response( 'admin_errors', esc_html__( 'Webhook Action: Webhook Error.', 'sellkit' ) ); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
private function get_form_data( AjaxHandler $ajax_handler, $settings ) { |
| 72 |
$fields = []; |
| 73 |
|
| 74 |
foreach ( $settings['fields'] as $field ) { |
| 75 |
if ( in_array( $field['type'], $this->exclude_fields, true ) ) { |
| 76 |
continue; |
| 77 |
} |
| 78 |
|
| 79 |
if ( isset( $ajax_handler->form_data['fields'][ $field['_id'] ] ) ) { |
| 80 |
$field_value = $ajax_handler->form_data['fields'][ $field['_id'] ]; |
| 81 |
} |
| 82 |
|
| 83 |
if ( 'acceptance' === $field['type'] ) { |
| 84 |
$field_value = empty( $field_value ) ? esc_html__( 'No', 'sellkit' ) : esc_html__( 'Yes', 'sellkit' ); |
| 85 |
} |
| 86 |
|
| 87 |
if ( empty( $field['label'] ) ) { |
| 88 |
$fields[ esc_html__( 'No Label', 'sellkit' ) . ' ' . $field['_id'] ] = $field_value; |
| 89 |
} else { |
| 90 |
$fields[ $field['label'] ] = $field_value; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
return $fields; |
| 95 |
} |
| 96 |
} |
| 97 |
|