api_key = $api_key; } public function get_subscriber( string $email ) { return null; } public function get_list() { if ( ! $this->api_key ) { return array(); } $response = wp_remote_get( 'https://api.brevo.com/v3/contacts/lists?limit=50&offset=0&sort=desc', array( 'headers' => array( 'api-key' => $this->api_key, ), ) ); if ( ! is_wp_error( $response ) ) { if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { return array(); } $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( empty( $body['lists'] ) ) { return array(); } return array_map( function( $list ) { return array( 'name' => $list['name'], 'id' => $list['id'], ); }, $body['lists'] ); } else { return array(); } } public function add_subscriber( array $args ) { $url = 'https://api.brevo.com/v3/contacts'; if ( ! empty( $args['name'] ) ) { $names = explode( ' ', $args['name'] ); $last_name = end( $names ); $first_name = implode( ' ', array_slice( $names, 0, -1 ) ); } $body = array( 'email' => $args['email'], 'attributes' => array( 'FIRSTNAME' => $first_name, 'LASTNAME' => $last_name, ), 'listIds' => array( intval( $args['list_id'] ) ), ); $res = wp_remote_request( $url, array( 'method' => 'POST', 'headers' => array( 'api-key' => $this->api_key, 'Content-Type' => 'application/json', ), 'body' => wp_json_encode( $body ), 'timeout' => 15, ) ); if ( is_wp_error( $res ) ) { return array( 'success' => false, 'message' => $res->get_error_message(), ); } $code = wp_remote_retrieve_response_code( $res ); $json = json_decode( wp_remote_retrieve_body( $res ), true ) ?? array(); if ( $code >= 200 && $code < 300 ) { return array_merge( $json, array( 'success' => true, ) ); } return array_merge( $json, array( 'success' => false, ) ); } }