File: /var/www/kevin-demo/wp-content/plugins/wpforms-sendinblue/src/Api/Connection.php
<?php
namespace WPFormsSendinblue\Api;
use WPFormsSendinblue\Api\Http\Request;
use WPFormsSendinblue\Api\Http\Response;
/**
* Class Connection communicates with the Sendinblue API.
*
* @since 1.0.0
*/
class Connection {
/**
* Request instance.
*
* @since 1.0.0
*
* @var Request
*/
private $request;
/**
* Client constructor.
*
* @since 1.0.0
*
* @param Request $request Request.
*/
public function __construct( Request $request ) {
$this->request = $request;
}
/**
* Create a new contact.
*
* @since 1.0.0
*
* @param string $email Email address.
* @param int $list_id List ID.
* @param array $attributes List of attributes.
*
* @return Response
*/
public function create_contact( $email, $list_id, $attributes = [] ): Response {
return $this->request->post(
'/contacts/',
[
'email' => sanitize_email( $email ),
'attributes' => (object) $attributes,
'listIds' => [ absint( $list_id ) ],
'updateEnabled' => true,
'emailBlacklisted' => false,
'smsBlacklisted' => false,
]
);
}
/**
* Create a new contact.
*
* @since 1.1.0
*
* @param string $email Email address.
* @param int $list_id List ID.
* @param int $template_id Email template ID.
* @param string $redirect_url Redirect URL for an opt-in button.
* @param array $attributes List of attributes.
*
* @return Response
*/
public function create_opt_in_contact( $email, $list_id, $template_id, $redirect_url, $attributes = [] ): Response {
return $this->request->post(
'/contacts/doubleOptinConfirmation/',
[
'email' => sanitize_email( $email ),
'attributes' => (object) $attributes,
'includeListIds' => [ absint( $list_id ) ],
'redirectionUrl' => esc_url_raw( $redirect_url ),
'templateId' => absint( $template_id ),
]
);
}
/**
* Delete a contact.
*
* @since 1.0.0
*
* @param string $email Email address.
*
* @return Response
*/
public function delete_contact( $email ): Response {
return $this->request->delete(
sprintf(
'/contacts/%s',
rawurlencode( sanitize_email( $email ) )
)
);
}
/**
* Delete a contact from a list.
*
* @since 1.0.0
*
* @param string $email Email address.
* @param int $list_id List ID.
*
* @return Response
*/
public function delete_contact_from_list( $email, $list_id ): Response {
return $this->request->post(
sprintf( '/contacts/lists/%d/contacts/remove', absint( $list_id ) ),
[
'emails' => [ sanitize_email( $email ) ],
'all' => false,
]
);
}
/**
* Retrieve account information.
*
* @since 1.0.0
*
* @return Response
*/
public function get_account(): Response {
return $this->request->get( '/account/' );
}
/**
* Get a list of registered attributes (fields).
*
* @since 1.0.0
*
* @return Response
*/
public function get_attributes(): Response {
return $this->request->get( '/contacts/attributes/' );
}
/**
* Get a list of Sendinblue lists.
*
* @since 1.0.0
*
* @param int $limit Limit of records.
* @param int $offset Offset for lists select.
*
* @return Response
*/
public function get_lists( $limit, $offset = 0 ): Response {
return $this->request->get(
'/contacts/lists/',
[
'limit' => absint( $limit ),
'offset' => absint( $offset ),
]
);
}
/**
* Get a list of Sendinblue active templates.
*
* @since 1.1.0
*
* @param int $limit Limit of records.
* @param int $offset Offset for templates select.
*
* @return Response
*/
public function get_active_templates( $limit, $offset = 0 ): Response {
return $this->request->get(
'/smtp/templates/',
[
'templateStatus' => 'true', // Crucial pass the true value as a string.
'limit' => absint( $limit ),
'offset' => absint( $offset ),
]
);
}
/**
* Rename the contact email.
*
* @since 1.0.0
*
* @param string $old_email Old email address.
* @param string $new_email New email address.
*
* @return Response
*/
public function rename_contact( $old_email, $new_email ): Response {
return $this->request->put(
sprintf( '/contacts/%s/', rawurlencode( sanitize_email( $old_email ) ) ),
[
'attributes' => [
'EMAIL' => sanitize_email( $new_email ),
],
]
);
}
/**
* Track an event.
*
* @since 1.0.0
*
* @param string $email Email address.
* @param string $event_name Event name.
* @param array $properties List of attributes.
* @param array $form_data List of form data.
*
* @return Response
*/
public function track_event( $email, $event_name, $properties = [], $form_data = [] ): Response {
return $this->request->post(
'/trackEvent',
[
'email' => sanitize_email( $email ),
'event' => wpforms_sanitize_text_deeply( $event_name ),
'properties' => (object) $properties,
'eventdata' => [
'data' => (object) $form_data,
],
]
);
}
}