/var/www/html/wp-content/plugins/contact-form-7/modules/sendinblue/service.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php

if ( ! class_exists'WPCF7_Service' ) ) {
    return;
}

class 
WPCF7_Sendinblue extends WPCF7_Service {
    use 
WPCF7_Sendinblue_API;

    private static 
$instance;
    private 
$api_key;

    public static function 
get_instance() {
        if ( empty( 
self::$instance ) ) {
            
self::$instance = new self;
        }

        return 
self::$instance;
    }

    private function 
__construct() {
        
$option WPCF7::get_option'sendinblue' );

        if ( isset( 
$option['api_key'] ) ) {
            
$this->api_key $option['api_key'];
        }
    }

    public function 
get_title() {
        return 
__'Sendinblue''contact-form-7' );
    }

    public function 
is_active() {
        return (bool) 
$this->get_api_key();
    }

    public function 
get_api_key() {
        return 
$this->api_key;
    }

    public function 
get_categories() {
        return array( 
'email_marketing' );
    }

    public function 
icon() {
    }

    public function 
link() {
        echo 
wpcf7_link(
            
'https://www.sendinblue.com/?tap_a=30591-fb13f0&tap_s=1031580-b1bb1d',
            
'sendinblue.com'
        
);
    }

    protected function 
log$url$request$response ) {
        
wpcf7_log_remote_request$url$request$response );
    }

    protected function 
menu_page_url$args '' ) {
        
$args wp_parse_args$args, array() );

        
$url menu_page_url'wpcf7-integration'false );
        
$url add_query_arg( array( 'service' => 'sendinblue' ), $url );

        if ( ! empty( 
$args ) ) {
            
$url add_query_arg$args$url );
        }

        return 
$url;
    }

    protected function 
save_data() {
        
WPCF7::update_option'sendinblue', array(
            
'api_key' => $this->api_key,
        ) );
    }

    protected function 
reset_data() {
        
$this->api_key null;
        
$this->save_data();
    }

    public function 
load$action '' ) {
        if ( 
'setup' == $action and 'POST' == $_SERVER['REQUEST_METHOD'] ) {
            
check_admin_referer'wpcf7-sendinblue-setup' );

            if ( ! empty( 
$_POST['reset'] ) ) {
                
$this->reset_data();
                
$redirect_to $this->menu_page_url'action=setup' );
            } else {
                
$this->api_key = isset( $_POST['api_key'] )
                    ? 
trim$_POST['api_key'] )
                    : 
'';

                
$confirmed $this->confirm_key();

                if ( 
true === $confirmed ) {
                    
$redirect_to $this->menu_page_url( array(
                        
'message' => 'success',
                    ) );

                    
$this->save_data();
                } elseif ( 
false === $confirmed ) {
                    
$redirect_to $this->menu_page_url( array(
                        
'action' => 'setup',
                        
'message' => 'unauthorized',
                    ) );
                } else {
                    
$redirect_to $this->menu_page_url( array(
                        
'action' => 'setup',
                        
'message' => 'invalid',
                    ) );
                }
            }

            
wp_safe_redirect$redirect_to );
            exit();
        }
    }

    public function 
admin_notice$message '' ) {
        if ( 
'unauthorized' == $message ) {
            echo 
sprintf(
                
'<div class="notice notice-error"><p><strong>%1$s</strong>: %2$s</p></div>',
                
esc_html__"Error"'contact-form-7' ) ),
                
esc_html__"You have not been authenticated. Make sure the provided API key is correct."'contact-form-7' ) )
            );
        }

        if ( 
'invalid' == $message ) {
            echo 
sprintf(
                
'<div class="notice notice-error"><p><strong>%1$s</strong>: %2$s</p></div>',
                
esc_html__"Error"'contact-form-7' ) ),
                
esc_html__"Invalid key values."'contact-form-7' ) )
            );
        }

        if ( 
'success' == $message ) {
            echo 
sprintf(
                
'<div class="notice notice-success"><p>%s</p></div>',
                
esc_html__'Settings saved.''contact-form-7' ) )
            );
        }
    }

    public function 
display$action '' ) {
        echo 
'<p>' sprintf(
            
esc_html__"Store and organize your contacts while protecting user privacy on Sendinblue, the leading CRM & email marketing platform in Europe. Sendinblue offers unlimited contacts and advanced marketing features. For details, see %s."'contact-form-7' ) ),
            
wpcf7_link(
                
__'https://contactform7.com/sendinblue-integration/''contact-form-7' ),
                
__'Sendinblue integration''contact-form-7' )
            )
        ) . 
'</p>';

        if ( 
$this->is_active() ) {
            echo 
sprintf(
                
'<p class="dashicons-before dashicons-yes">%s</p>',
                
esc_html__"Sendinblue is active on this site."'contact-form-7' ) )
            );
        }

        if ( 
'setup' == $action ) {
            
$this->display_setup();
        } else {
            echo 
sprintf(
                
'<p><a href="%1$s" class="button">%2$s</a></p>',
                
esc_url$this->menu_page_url'action=setup' ) ),
                
esc_html__'Setup integration''contact-form-7' ) )
            );
        }
    }

    private function 
display_setup() {
        
$api_key $this->get_api_key();

?>
<form method="post" action="<?php echo esc_url$this->menu_page_url'action=setup' ) ); ?>">
<?php wp_nonce_field'wpcf7-sendinblue-setup' ); ?>
<table class="form-table">
<tbody>
<tr>
    <th scope="row"><label for="publishable"><?php echo esc_html__'API key''contact-form-7' ) ); ?></label></th>
    <td><?php
        
if ( $this->is_active() ) {
            echo 
esc_htmlwpcf7_mask_password$api_key4) );
            echo 
sprintf(
                
'<input type="hidden" value="%s" id="api_key" name="api_key" />',
                
esc_attr$api_key )
            );
        } else {
            echo 
sprintf(
                
'<input type="text" aria-required="true" value="%s" id="api_key" name="api_key" class="regular-text code" />',
                
esc_attr$api_key )
            );
        }
    
?></td>
</tr>
</tbody>
</table>
<?php
        
if ( $this->is_active() ) {
            
submit_button(
                
_x'Remove key''API keys''contact-form-7' ),
                
'small''reset'
            
);
        } else {
            
submit_button__'Save changes''contact-form-7' ) );
        }
?>
</form>
<?php
    
}
}


/**
 * Trait for the Sendinblue API (v3).
 *
 * @link https://developers.sendinblue.com/reference
 */
trait WPCF7_Sendinblue_API {


    public function 
confirm_key() {
        
$endpoint 'https://api.sendinblue.com/v3/account';

        
$request = array(
            
'headers' => array(
                
'Accept' => 'application/json',
                
'Content-Type' => 'application/json; charset=utf-8',
                
'API-Key' => $this->get_api_key(),
            ),
        );

        
$response wp_remote_get$endpoint$request );
        
$response_code = (int) wp_remote_retrieve_response_code$response );

        if ( 
200 === $response_code ) { // 200 OK
            
return true;
        } elseif ( 
401 === $response_code ) { // 401 Unauthorized
            
return false;
        } elseif ( 
400 <= $response_code ) {
            if ( 
WP_DEBUG ) {
                
$this->log$endpoint$request$response );
            }
        }
    }


    public function 
get_lists() {
        
$endpoint add_query_arg(
            array(
                
'limit' => 50,
                
'offset' => 0,
            ),
            
'https://api.sendinblue.com/v3/contacts/lists'
        
);

        
$request = array(
            
'headers' => array(
                
'Accept' => 'application/json',
                
'Content-Type' => 'application/json; charset=utf-8',
                
'API-Key' => $this->get_api_key(),
            ),
        );

        
$response wp_remote_get$endpoint$request );
        
$response_code = (int) wp_remote_retrieve_response_code$response );

        if ( 
200 === $response_code ) { // 200 OK
            
$response_body wp_remote_retrieve_body$response );
            
$response_body json_decode$response_bodytrue );

            if ( empty( 
$response_body['lists'] ) ) {
                return array();
            } else {
                return (array) 
$response_body['lists'];
            }
        } elseif ( 
400 <= $response_code ) {
            if ( 
WP_DEBUG ) {
                
$this->log$endpoint$request$response );
            }
        }
    }


    public function 
get_templates() {
        
$endpoint add_query_arg(
            array(
                
'templateStatus' => 'true',
                
'limit' => 100,
                
'offset' => 0,
            ),
            
'https://api.sendinblue.com/v3/smtp/templates'
        
);

        
$request = array(
            
'headers' => array(
                
'Accept' => 'application/json',
                
'Content-Type' => 'application/json; charset=utf-8',
                
'API-Key' => $this->get_api_key(),
            ),
        );

        
$response wp_remote_get$endpoint$request );
        
$response_code = (int) wp_remote_retrieve_response_code$response );

        if ( 
200 === $response_code ) { // 200 OK
            
$response_body wp_remote_retrieve_body$response );
            
$response_body json_decode$response_bodytrue );

            if ( empty( 
$response_body['templates'] ) ) {
                return array();
            } else {
                return (array) 
$response_body['templates'];
            }
        } elseif ( 
400 <= $response_code ) {
            if ( 
WP_DEBUG ) {
                
$this->log$endpoint$request$response );
            }
        }
    }


    public function 
create_contact$properties ) {
        
$endpoint 'https://api.sendinblue.com/v3/contacts';

        
$request = array(
            
'headers' => array(
                
'Accept' => 'application/json',
                
'Content-Type' => 'application/json; charset=utf-8',
                
'API-Key' => $this->get_api_key(),
            ),
            
'body' => json_encode$properties ),
        );

        
$response wp_remote_post$endpoint$request );
        
$response_code = (int) wp_remote_retrieve_response_code$response );

        if ( 
in_array$response_code, array( 201204 ), true ) ) {
            
$contact_id wp_remote_retrieve_body$response );
            return 
$contact_id;
        } elseif ( 
400 <= $response_code ) {
            if ( 
WP_DEBUG ) {
                
$this->log$endpoint$request$response );
            }
        }

        return 
false;
    }


    public function 
send_email$properties ) {
        
$endpoint 'https://api.sendinblue.com/v3/smtp/email';

        
$request = array(
            
'headers' => array(
                
'Accept' => 'application/json',
                
'Content-Type' => 'application/json; charset=utf-8',
                
'API-Key' => $this->get_api_key(),
            ),
            
'body' => json_encode$properties ),
        );

        
$response wp_remote_post$endpoint$request );
        
$response_code = (int) wp_remote_retrieve_response_code$response );

        if ( 
201 === $response_code ) { // 201 Transactional email sent
            
$message_id wp_remote_retrieve_body$response );
            return 
$message_id;
        } elseif ( 
400 <= $response_code ) {
            if ( 
WP_DEBUG ) {
                
$this->log$endpoint$request$response );
            }
        }

        return 
false;
    }


}