| 1 |
<?php // phpcs:ignore |
| 2 |
|
| 3 |
namespace OPTN\Includes\Integrations\Implementations; |
| 4 |
|
| 5 |
use OPTN\Includes\Integrations\Base\BaseMailIntegration; |
| 6 |
use OPTN\Includes\Utils\Utils; |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* MailerLite Integration class. |
| 12 |
* |
| 13 |
* @link https://developers.mailerlite.com/docs/ |
| 14 |
*/ |
| 15 |
class Webhook extends BaseMailIntegration { |
| 16 |
|
| 17 |
/** |
| 18 |
* Get name |
| 19 |
* |
| 20 |
* @return string |
| 21 |
*/ |
| 22 |
public function get_name() { |
| 23 |
return 'webhook'; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Adds a subscribe to MailerLite |
| 28 |
* |
| 29 |
* @param array $data data. |
| 30 |
* @return bool |
| 31 |
*/ |
| 32 |
public function add_subscriber( $data ): bool { |
| 33 |
|
| 34 |
if ( empty( $data['link'] ) || empty( $data['reqType'] ) ) { |
| 35 |
return false; |
| 36 |
} |
| 37 |
|
| 38 |
$url = wp_http_validate_url( esc_url_raw( $data['link'] ) ); |
| 39 |
|
| 40 |
if ( false === $url ) { |
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
$res = null; |
| 45 |
|
| 46 |
if ( 'GET' === $data['reqType'] ) { |
| 47 |
$url = add_query_arg( is_array( $data['fields'] ) ? $data['fields'] : array(), $url ); |
| 48 |
|
| 49 |
if ( false === wp_http_validate_url( $url ) ) { |
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
$res = wp_safe_remote_get( |
| 54 |
$url, |
| 55 |
array( |
| 56 |
'timeout' => 45, |
| 57 |
) |
| 58 |
); |
| 59 |
} elseif ( 'POST' === $data['reqType'] ) { |
| 60 |
$res = wp_safe_remote_post( |
| 61 |
$url, |
| 62 |
array( |
| 63 |
'method' => 'POST', |
| 64 |
'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ), |
| 65 |
'body' => wp_json_encode( isset( $data['fields'] ) && is_array( $data['fields'] ) ? $data['fields'] : array() ), |
| 66 |
'timeout' => 45, |
| 67 |
) |
| 68 |
); |
| 69 |
} else { |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
if ( ! is_null( $res ) && ! is_wp_error( $res ) ) { |
| 74 |
$this->add_lead( $data ); |
| 75 |
return true; |
| 76 |
} |
| 77 |
|
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Adds to the leads table |
| 83 |
* |
| 84 |
* @param array $data data. |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
public function add_lead( $data ) { |
| 88 |
if ( isset( $data['conv_id'] ) && isset( $data['fields'] ) ) { |
| 89 |
$fields = $data['fields']; |
| 90 |
|
| 91 |
$leads_data = array( |
| 92 |
'conv_id' => intval( $data['conv_id'] ), |
| 93 |
'name' => Utils::extract_name( $fields ), |
| 94 |
'email' => isset( $fields['email'] ) ? sanitize_email( $fields['email'] ) : null, |
| 95 |
'data' => Utils::sanitize_json_array( $fields ), |
| 96 |
'integration' => 'webhook', |
| 97 |
); |
| 98 |
|
| 99 |
$this->db->add_lead( $leads_data ); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Gets the fields |
| 105 |
* |
| 106 |
* @param array $args receive data as array. |
| 107 |
* @return array |
| 108 |
*/ |
| 109 |
public function get_fields( $args = array() ): array { |
| 110 |
return array(); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Gets list |
| 115 |
* |
| 116 |
* @param array $args receive data as array. |
| 117 |
* @return array |
| 118 |
*/ |
| 119 |
public function get_lists( $args = array() ): array { |
| 120 |
return array(); |
| 121 |
} |
| 122 |
} |
| 123 |
|