| 1 |
<?php |
| 2 |
|
| 3 |
namespace JFB_Modules\Actions_V2\Call_Webhook; |
| 4 |
|
| 5 |
// If this file is called directly, abort. |
| 6 |
use Jet_Form_Builder\Actions\Action_Handler; |
| 7 |
use Jet_Form_Builder\Actions\Types\Base; |
| 8 |
use Jet_Form_Builder\Exceptions\Action_Exception; |
| 9 |
use JFB_Modules\Rich_Content; |
| 10 |
|
| 11 |
if ( ! defined( 'WPINC' ) ) { |
| 12 |
die; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Define Base_Type class |
| 17 |
*/ |
| 18 |
class Call_Webhook_Action extends Base { |
| 19 |
|
| 20 |
public function get_name() { |
| 21 |
return __( 'Call Webhook', 'jet-form-builder' ); |
| 22 |
} |
| 23 |
|
| 24 |
public function get_id() { |
| 25 |
return 'call_webhook'; |
| 26 |
} |
| 27 |
|
| 28 |
public function action_attributes() { |
| 29 |
return array( |
| 30 |
'webhook_url' => array( |
| 31 |
'default' => '', |
| 32 |
), |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
public function do_action( array $request, Action_Handler $handler ) { |
| 37 |
$webhook_url = ! empty( $this->settings['webhook_url'] ) ? trim( $this->settings['webhook_url'] ) : false; |
| 38 |
$webhook_timeout = ! empty( $this->settings['webhook_timeout'] ) ? (int) $this->settings['webhook_timeout'] : false; |
| 39 |
|
| 40 |
if ( ! $webhook_url ) { |
| 41 |
throw new Action_Exception( |
| 42 |
'failed', |
| 43 |
esc_html__( 'Empty webhook url', 'jet-form-builder' ) |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
$args = array( |
| 48 |
'body' => $request, |
| 49 |
); |
| 50 |
|
| 51 |
if ( false !== $webhook_timeout ) { |
| 52 |
$args['timeout'] = $webhook_timeout; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Filter webhook arguments |
| 57 |
*/ |
| 58 |
$args = apply_filters( |
| 59 |
'jet-form-builder/action/webhook/request-args', |
| 60 |
$args, |
| 61 |
$this->settings, |
| 62 |
$this |
| 63 |
); |
| 64 |
|
| 65 |
$webhook_url = Rich_Content\Module::rich( $webhook_url ); |
| 66 |
|
| 67 |
$webhook_url = apply_filters( |
| 68 |
'jet-form-builder/action/webhook/request-url', |
| 69 |
$webhook_url, |
| 70 |
$this |
| 71 |
); |
| 72 |
|
| 73 |
$webhook_url = esc_url_raw( $webhook_url ); |
| 74 |
|
| 75 |
if ( ! $webhook_url ) { |
| 76 |
throw new Action_Exception( 'failed', 'empty_webhook_url' ); |
| 77 |
} |
| 78 |
|
| 79 |
$response = wp_remote_post( $webhook_url, $args ); |
| 80 |
|
| 81 |
if ( $response instanceof \WP_Error ) { |
| 82 |
throw new Action_Exception( |
| 83 |
'failed', |
| 84 |
esc_html__( 'Remote request returned error', 'jet-form-builder' ), |
| 85 |
$response // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 86 |
); |
| 87 |
} |
| 88 |
/** |
| 89 |
* Fires whe webhook response received |
| 90 |
*/ |
| 91 |
do_action( 'jet-form-builder/action/webhook/response', $response, $this->settings, $this ); |
| 92 |
} |
| 93 |
|
| 94 |
} |
| 95 |
|