PluginProbe ʕ •ᴥ•ʔ
Brevo – Email, SMS, Web Push, Chat, and more. / 3.2.9
Brevo – Email, SMS, Web Push, Chat, and more. v3.2.9
2.9.13 2.9.14 2.9.15 2.9.16 2.9.17 2.9.18 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8 2.9.9 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.9 3.1.0 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.2 3.1.20 3.1.21 3.1.22 3.1.23 3.1.24 3.1.25 3.1.26 3.1.27 3.1.28 3.1.29 3.1.3 3.1.30 3.1.31 3.1.32 3.1.33 3.1.34 3.1.35 3.1.36 3.1.37 3.1.38 3.1.39 3.1.4 3.1.40 3.1.41 3.1.42 3.1.43 3.1.44 3.1.45 3.1.46 3.1.47 3.1.48 3.1.49 3.1.5 3.1.50 3.1.51 3.1.52 3.1.53 3.1.54 3.1.55 3.1.56 3.1.57 3.1.58 3.1.59 3.1.6 3.1.60 3.1.61 3.1.62 3.1.63 3.1.64 3.1.65 3.1.66 3.1.67 3.1.68 3.1.69 3.1.7 3.1.70 3.1.71 3.1.72 3.1.73 3.1.74 3.1.75 3.1.76 3.1.77 3.1.78 3.1.79 3.1.8 3.1.80 3.1.81 3.1.82 3.1.83 3.1.84 3.1.85 3.1.86 3.1.87 3.1.88 3.1.89 3.1.9 3.1.90 3.1.91 3.1.92 3.1.93 3.1.94 3.1.95 3.1.96 3.1.97 3.1.98 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 trunk 1.0 1.5 2.0.8 2.9.10 2.9.11 2.9.12
mailin / sendinblue.php
mailin Last commit date
css 1 year ago form 1 year ago img 1 year ago inc 8 months ago js 10 months ago lang 8 months ago model 8 months ago page 8 months ago widget 4 years ago wonderpush-php-lib 10 months ago brevo-frame.html 1 year ago index.php 11 years ago readme.txt 8 months ago screenshot-1.png 2 years ago screenshot-2.png 2 years ago screenshot-3.png 2 years ago screenshot-4.png 2 years ago screenshot-5.png 2 years ago sendinblue.php 8 months ago sw.js 1 year ago wonderpush-worker-loader.min.js 1 year ago wonderpush.min.html 1 year ago
sendinblue.php
1902 lines
1 <?php
2 /**
3 * Plugin Name: Brevo - Email, SMS, Web Push, Chat, and more.
4 * Plugin URI: https://www.brevo.com/?r=wporg
5 * Description: Manage your contact lists, subscription forms and all email and marketing-related topics from your wp panel, within one single plugin
6 * Version: 3.2.9
7 * Author: Brevo
8 * Author URI: https://www.brevo.com/?r=wporg
9 * License: GPLv2 or later
10 *
11 * @package SIB
12 */
13
14 /*
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License
17 as published by the Free Software Foundation; either version 2
18 of the License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 */
27
28 /**
29 * Application entry point. Contains plugin startup class that loads on <i> sendinblue_init </i> action.
30 */
31 if ( ! class_exists( 'Mailin' ) ) {
32 require_once( 'inc/mailin.php' );
33 }
34 if ( ! class_exists( 'SendinblueApiClient' ) ) {
35 require_once( 'inc/SendinblueApiClient.php' );
36 }
37 if ( ! class_exists( 'SendinblueAccount' ) ) {
38 require_once( 'inc/SendinblueAccount.php' );
39 }
40 // For marketing automation.
41 if ( ! class_exists( 'Sendinblue' ) ) {
42 require_once( 'inc/sendinblue.php' );
43 }
44
45 if ( ! class_exists( 'WonderPush\WonderPush' )) {
46 require_once( 'wonderpush-php-lib/init.php' );
47 }
48
49 if ( ! class_exists( 'SIB_Push_Utils' ) ) {
50 require_once( 'inc/push-utils.php' );
51 }
52
53 if ( ! class_exists( 'SIB_Push_Settings' ) ) {
54 require_once( 'inc/push-settings.php' );
55 }
56
57 if ( ! class_exists( 'inc/push-httpclient.php' ) ) {
58 require_once( 'inc/push-httpclient.php' );
59 }
60
61 if ( ! class_exists( 'SIB_Push_API' ) ) {
62 require_once( 'inc/push-api.php' );
63 }
64
65 if ( ! class_exists( 'SIB_Push_Admin' ) ) {
66 require_once( 'inc/push-admin.php' );
67 }
68
69 if ( ! class_exists( 'SIB_Push_Public' ) ) {
70 require_once( 'inc/push-public.php' );
71 }
72
73 if ( ! class_exists( 'SIB_Push_WooCommerce' ) ) {
74 require_once( 'inc/push-woocommerce.php' );
75 }
76
77 if ( ! class_exists( 'SIB_Push_Amp' ) ) {
78 require_once( 'inc/push-amp.php' );
79 }
80
81 if (!function_exists('http_build_url')) {
82 require_once( 'inc/http-build-url.php' );
83 }
84
85 if ( ! class_exists( 'SIB_Manager' ) ) {
86 register_deactivation_hook( __FILE__, array( 'SIB_Manager', 'deactivate' ) );
87 register_activation_hook( __FILE__, array( 'SIB_Manager', 'install' ) );
88 register_uninstall_hook( __FILE__, array( 'SIB_Manager', 'uninstall' ) );
89
90 require_once( 'page/page-home.php' );
91 require_once( 'page/page-form.php' );
92 require_once( 'page/page-push.php' );
93 require_once( 'widget/widget_form.php' );
94 require_once( 'inc/table-forms.php' );
95 require_once( 'inc/sib-api-manager.php' );
96 require_once( 'inc/sib-sms-code.php' );
97 require_once( 'model/model-forms.php' );
98 require_once( 'model/model-users.php' );
99 require_once( 'model/model-lang.php' );
100 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
101 require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
102 /**
103 * Class SIB_Manager
104 */
105 class SIB_Manager {
106
107 private const ROUTE_METHODS = 'methods';
108 private const ROUTE_CALLBACK = 'callback';
109 private const ROUTE_PATH = 'path';
110 private const PERMISSION_CALLBACK = 'permission_callback';
111 private const API_NAMESPACE = "mailin/v1";
112
113 /** Main setting option name */
114 const MAIN_OPTION_NAME = 'sib_main_option';
115
116 /** Home setting option name */
117 const HOME_OPTION_NAME = 'sib_home_option';
118
119 /** Access token option name */
120 const ACCESS_TOKEN_OPTION_NAME = 'sib_token_store';
121
122 /** Plugin language notice option name */
123 const LANGUAGE_OPTION_NAME = 'sib_language_notice_option';
124
125 /** Form preview option name */
126 const PREVIEW_OPTION_NAME = 'sib_preview_form';
127
128 const API_KEY_V3_OPTION_NAME = 'sib_api_key_v3';
129
130 const PUSH_SETTINGS_OPTION_NAME = 'sib_push_settings';
131
132 const RECAPTCHA_API_TEMPLATE = 'https://www.google.com/recaptcha/api/siteverify?%s';
133
134 const TURNSTILE_SITE_VERIFY = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
135
136 /** Installation id option name */
137 const INSTALLATION_ID = 'sib_installation_id';
138 const BREVO_PLUGIN_VERSION = 'brevo_plugin_version';
139
140 const SDK_LOADER_PRODUCTION_URL = "https://cdn.brevo.com/js/sdk-loader.js";
141 const SDK_LOADER_STAGING_URL = "https://cdn.brevo.com/js/sdk-staging-loader.js";
142 const URL_CHECK_STAGING = "staging";
143 const SERVICE_WORKER_FILE_URL = "/js/service-worker.js";
144
145 const SIB_ATTRIBUTE = array(
146 'input' => array(
147 'type' => true,
148 'name' => true,
149 'value' => true,
150 'class' => true,
151 'id' => true,
152 'size' => true,
153 'min' => true,
154 'max' => true,
155 'pattern' => true,
156 'title' => true,
157 'placeholder' => true,
158 'required' => true,
159 ),
160 'p' => array(
161 'align' => true,
162 'id' => true,
163 'class' => true,
164 'dir' => true,
165 'lang' => true,
166 'style' => true,
167 'xml:lang' => true,
168 ),
169 'iframe' => array(
170 'name' => true,
171 'id' => true,
172 'class' => true,
173 'src' => true,
174 'width' => true,
175 'height' => true,
176 'style' => true,
177 'loading' => true,
178 'allow' => true,
179 'allowfullscreen' => true,
180 ),
181 'div' => array(
182 'id' => true,
183 'class' => true,
184 'dir' => true,
185 'lang' => true,
186 'style' => true,
187 'xml:lang' => true,
188 'data-require' => true,
189 'data-sitekey' => true,
190 'data-error-callback' => true,
191 'data-theme' => true,
192 ),
193 'a' => array(
194 'href' => true,
195 'id' => true,
196 'class' => true,
197 'rel' => true,
198 'rev' => true,
199 'name' => true,
200 'target' => true,
201 ),
202 'style' => array(),
203 'script' => array(
204 'src' => true,
205 ),
206 'link' => array(
207 'rel' => true,
208 'href' => true,
209 'type' => true,
210 ),
211 'select' => array(
212 'name' => true,
213 'multiple' => true,
214 'class' => true,
215 'id' => true,
216 'style' => true,
217 'required' => true,
218 ),
219 'option' => array(
220 'value' => true,
221 ),
222 'ul' => array(
223 'class' => true,
224 'style' => true,
225 ),
226 'center' => array(),
227 'download' => array(
228 'valueless' => 'y',
229 )
230 );
231
232 /**
233 * API key
234 *
235 * @var $access_key
236 */
237 public static $access_key;
238
239 /**
240 * Store instance
241 *
242 * @var $instance
243 */
244 public static $instance;
245
246 /**
247 * Plugin directory path value. set in constructor
248 *
249 * @var $plugin_dir
250 */
251 public static $plugin_dir;
252
253 /**
254 * Plugin url. set in constructor
255 *
256 * @var $plugin_url
257 */
258 public static $plugin_url;
259
260 /**
261 * Plugin name. set in constructor
262 *
263 * @var $plugin_name
264 */
265 public static $plugin_name;
266
267 /**
268 * Check if wp_mail is declared
269 *
270 * @var $wp_mail_conflict
271 */
272 static $wp_mail_conflict;
273
274 /**
275 * Class constructor
276 * Sets plugin url and directory and adds hooks to <i>init</i>. <i>admin_menu</i>
277 */
278 function __construct() {
279 // get basic info.
280 self::$plugin_dir = plugin_dir_path( __FILE__ );
281 self::$plugin_url = plugins_url( '', __FILE__ );
282 self::$plugin_name = plugin_basename( __FILE__ );
283
284 self::$wp_mail_conflict = false;
285
286 // api key for sendinblue.
287 $general_settings = get_option( self::MAIN_OPTION_NAME, array() );
288 self::$access_key = isset( $general_settings['access_key'] ) ? $general_settings['access_key'] : '';
289
290 self::$instance = $this;
291 add_action('plugins_loaded', array( &$this, 'brevo_wp_load' ) );
292 add_action( 'upgrader_process_complete', array( &$this, 'my_upgrade_function' ), 10, 2);
293 add_action( 'admin_init', array( &$this, 'admin_init' ), 9999 );
294 add_action( 'admin_menu', array( &$this, 'admin_menu' ), 9999 );
295 add_action('rest_api_init', array($this, 'create_brevo_rest_endpoints'));
296
297 add_action( 'wp_print_scripts', array( &$this, 'frontend_register_scripts' ), 9999 );
298 add_action( 'wp_enqueue_scripts', array( &$this, 'wp_head_ac' ), 999 );
299 add_action( 'wp_dashboard_setup', array( 'SIB_Push_Admin', 'add_dashboard_widget' ), 9999 );
300 add_action( 'admin_init', array( 'SIB_Push_Admin', 'add_post_options' ), 9999 );
301 add_action( 'admin_bar_menu', array( 'SIB_Push_Admin', 'add_admin_bar_menu_item' ), 100);
302
303 // create custom url for form preview.
304 add_filter( 'query_vars', array( &$this, 'sib_query_vars' ) );
305 add_action( 'parse_request', array( &$this, 'sib_parse_request' ) );
306
307 add_action( 'wp_ajax_sib_validate_process', array( 'SIB_Page_Home', 'ajax_validation_process' ) );
308 add_action( 'wp_ajax_sib_validate_ma', array( 'SIB_Page_Home', 'ajax_validate_ma' ) );
309 add_action( 'wp_ajax_sib_activate_email_change', array( 'SIB_Page_Home', 'ajax_activate_email_change' ) );
310 add_action( 'wp_ajax_sib_sender_change', array( 'SIB_Page_Home', 'ajax_sender_change' ) );
311 add_action( 'wp_ajax_sib_send_email', array( 'SIB_Page_Home', 'ajax_send_email' ) );
312 add_action( 'wp_ajax_sib_remove_cache', array( 'SIB_Page_Home', 'ajax_remove_cache' ) );
313 add_action( 'wp_ajax_sib_sync_users', array( 'SIB_Page_Home', 'ajax_sync_users' ) );
314
315 add_action( 'wp_ajax_sib_change_template', array( 'SIB_Page_Form', 'ajax_change_template' ) );
316 add_action( 'wp_ajax_sib_get_lists', array( 'SIB_Page_Form', 'ajax_get_lists' ) );
317 add_action( 'wp_ajax_sib_get_templates', array( 'SIB_Page_Form', 'ajax_get_templates' ) );
318 add_action( 'wp_ajax_sib_get_attributes', array( 'SIB_Page_Form', 'ajax_get_attributes' ) );
319 add_action( 'wp_ajax_sib_update_form_html', array( 'SIB_Page_Form', 'ajax_update_html' ) );
320 add_action( 'wp_ajax_sib_copy_origin_form', array( 'SIB_Page_Form', 'ajax_copy_origin_form' ) );
321
322 add_action( 'wp_ajax_sib_get_country_prefix', array( $this, 'ajax_get_country_prefix' ) );
323 add_action( 'wp_ajax_nopriv_sib_get_country_prefix', array( $this, 'ajax_get_country_prefix' ) );
324
325 add_action( 'init', array( 'SIB_Push_API', 'init' ) );
326 add_action( 'init', array( 'SIB_Push_Public', 'init' ) );
327 // NOTE: deactivate woocommerce
328 // add_action( 'init', array( 'SIB_Push_WooCommerce', 'init' ) );
329
330 add_action( 'init', array( &$this, 'init' ) );
331
332 add_action( 'wp_login', array( &$this, 'sib_wp_login_identify' ), 10, 2 );
333
334 // change sib tables name on prior(2.6.9) versions.
335 SIB_Model_Users::add_prefix();
336 SIB_Forms::add_prefix();
337 SIB_Forms::modify_datatype();
338
339 if ( self::is_api_key_set() ) {
340 add_shortcode( 'sibwp_form', array( &$this, 'sibwp_form_shortcode' ) );
341 // register widget.
342 add_action( 'widgets_init', array( &$this, 'sib_create_widget' ) );
343
344 // create forms tables and create default form.
345 SIB_Forms::createTable();
346 // create users table.
347 SIB_Model_Users::createTable();
348 // add columns for old versions
349 SIB_Forms::alterTable();
350 SIB_Model_Users::add_user_added_date_column();
351 SIB_Model_Users::add_flag_doi_sent();
352 }
353
354 $use_api_version = get_option( 'sib_use_apiv2', '0' );
355 if ( '0' === $use_api_version ) {
356 self::uninstall();
357 update_option( 'sib_use_apiv2', '1' );
358 }
359
360 // Wpml plugin part.
361 if ( ! function_exists( 'is_plugin_active_for_network' ) ) :
362 require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
363 endif;
364 if ( in_array( 'sitepress-multilingual-cms/sitepress.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) || is_plugin_active_for_network( 'sitepress-multilingual-cms/sitepress.php' ) ) {
365 SIB_Forms_Lang::createTable();
366 add_action( 'sib_language_sidebar', array( $this, 'sib_create_language_sidebar' ) );
367 }
368
369 /**
370 * Hook wp_mail to send transactional emails
371 */
372
373 // check if wp_mail function is already declared by others.
374 if ( function_exists( 'wp_mail' ) ) {
375 self::$wp_mail_conflict = true;
376 }
377 $home_settings = get_option( SIB_Manager::HOME_OPTION_NAME, array() );
378
379 if( 'yes' === $home_settings['activate_email'] )
380 {
381 if ( false === self::$wp_mail_conflict ) {
382 /**
383 * Declare wp_mail function for Sendinblue SMTP module
384 *
385 * @param string $to - receiption email.
386 * @param string $subject - subject of email.
387 * @param string $message - message content.
388 * @param string $headers - header of email.
389 * @param array $attachments - attachments.
390 * @return bool
391 */
392 function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
393 $message = str_replace( 'NF_SIB', '', $message );
394 $message = str_replace( 'WC_SIB', '', $message );
395 try {
396 $sent = SIB_Manager::sib_email( $to, $subject, $message, $headers, $attachments );
397 if ( is_wp_error( $sent ) || ! isset( $sent['code'] ) || 'success' !== $sent['code'] ) {
398 try{
399 return true;
400 }catch( Exception $e ){
401 return false;
402 }
403 }
404 return true;
405 } catch ( Exception $e ) {
406 return false;
407 }
408 }
409 } else {
410 add_action( 'admin_notices', array( &$this, 'wpMailNotices' ) );
411 return;
412 }
413 }
414 }
415
416 /**
417 * Is this wordpress instance connected to the staging environment?
418 * Note: don't forget to manually change mailin/sw.js to put the staging sdk loader
419 * @return bool
420 */
421 static function is_staging() {
422 return false;
423 }
424
425 /**
426 * Add identify tag for login users
427 *
428 * @param string $user_login - user login name.
429 * @param array $user - user.
430 */
431 function sib_wp_login_identify( $user_login, $user ) {
432
433 $userEmail = $user->user_email;
434 $data = array(
435 'email_id' => $userEmail,
436 'name' => $user_login,
437 );
438 SIB_API_Manager::identify_user( $data );
439 }
440
441 /**
442 * Initialize method. called on <i>init</i> action
443 */
444 function init() {
445 // Sign up process.
446 if ( isset( $_POST['sib_form_action'] ) && ( 'subscribe_form_submit' == sanitize_text_field($_POST['sib_form_action']) ) ) {
447 $this->signup_process();
448 }
449 // Subscribe.
450 if ( isset( $_GET['sib_action'] ) && ( 'subscribe' == sanitize_text_field($_GET['sib_action']) ) ) {
451 $code = isset( $_GET['code'] ) ? sanitize_text_field( $_GET['code'] ) : '';
452 $contact_info = SIB_Model_Users::get_data_by_code( $code );
453 $user_added_date = $contact_info['user_added_date'];
454 $current_date = gmdate( 'Y-m-d H:i:s' );
455 $date_diff = strtotime( $current_date ) - strtotime( $user_added_date );
456 if ( $date_diff > 5 ) {
457 SIB_API_Manager::subscribe( $contact_info );
458 } else {
459 $type = 'Bot Event';
460 SIB_API_Manager::template_subscribe( $type );
461 }
462 exit;
463 }
464 // Dismiss language notice.
465 if ( isset( $_GET['dismiss_admin_lang_notice'] ) && '1' == sanitize_text_field($_GET['dismiss_admin_lang_notice']) ) {
466 update_option( SIB_Manager::LANGUAGE_OPTION_NAME, true );
467 wp_safe_redirect( $_SERVER['HTTP_REFERER'] );
468 exit();
469 }
470
471 add_action( 'wp_head', array( &$this, 'install_ma_script' ) );
472 }
473
474 /**
475 * Hook admin_init
476 */
477 function admin_init() {
478 add_action( 'admin_action_sib_setting_subscription', array( 'SIB_Page_Form', 'save_setting_subscription' ) );
479 add_action( 'admin_action_nopriv_sib_setting_subscription', array( 'SIB_Page_Form', 'save_setting_subscription' ) );
480 SIB_Manager::LoadTextDomain();
481 $this->register_scripts();
482 $this->register_styles();
483 }
484
485 /**
486 * Hook admin_menu
487 */
488 function admin_menu() {
489 SIB_Manager::LoadTextDomain();
490 new SIB_Page_Home();
491 if ( SIB_Push_Utils::can_modify_settings() ) {
492 new SIB_Page_Push();
493 }
494 new SIB_Page_Form();
495
496 }
497
498 /**
499 * Register script for admin page
500 */
501 function register_scripts() {
502 wp_register_script( 'sib-bootstrap-js', self::$plugin_url . '/js/bootstrap/js/bootstrap.bundle.min.js', array( 'jquery' ), false );
503 wp_register_script( 'sib-admin-js', self::$plugin_url . '/js/admin.js', array( 'jquery' ), filemtime( self::$plugin_dir . '/js/admin.js' ) );
504 wp_register_script( 'sib-chosen-js', self::$plugin_url . '/js/chosen.jquery.min.js', array( 'jquery' ), false );
505 wp_register_script( 'sib-push-js', self::$plugin_url . '/js/push.bundle.js', array( 'wp-i18n', 'react', 'react-dom' ), filemtime(self::$plugin_dir . '/js/push.bundle.js'), true );
506 wp_register_script('sib-post-editor-js', self::$plugin_url . '/js/post-editor.bundle.js', array( 'jquery', 'wp-i18n' ), filemtime(self::$plugin_dir . '/js/post-editor.bundle.js'));
507 wp_register_script('sib-select2', self::$plugin_url . '/js/select2.full.min.js', array('jquery'), '4.0.13');
508 wp_set_script_translations( 'sib-push-js', 'push', self::$plugin_dir . 'lang' );
509 wp_set_script_translations( 'sib-post-editor-js', 'post-editor', self::$plugin_dir . 'lang' );
510 wp_enqueue_script('jquery-ui-datepicker');
511 wp_enqueue_script('jquery-ui-spinner');
512 }
513
514 /**
515 * Register stylesheet for admin page
516 */
517 function register_styles() {
518 wp_register_style( 'sib-bootstrap-css', self::$plugin_url . '/js/bootstrap/css/bootstrap.css', array(), false, 'all' );
519 wp_register_style( 'sib-fontawesome-css', self::$plugin_url . '/css/fontawesome/css/font-awesome.css', array(), false, 'all' );
520 wp_register_style( 'sib-chosen-css', self::$plugin_url . '/css/chosen.min.css' );
521 wp_register_style( 'sib-admin-css', self::$plugin_url . '/css/admin.css', array(), filemtime( self::$plugin_dir . '/css/admin.css' ), 'all' );
522 wp_register_style('sib-push-admin-css', self::$plugin_url . '/css/push-admin.css', array(), filemtime( self::$plugin_dir . '/css/push-admin.css' ));
523 wp_register_style('sib-select2', self::$plugin_url . '/css/select2.min.css', false, '4.0.13');
524 wp_register_style('sib-font-face', 'https://designsystem.brevo.com/designsystem/component/face/ba05798091ad5d7562c3.css', false, 'ba05798091ad5d7562c3');
525 }
526
527 /**
528 * Registers scripts for frontend
529 */
530 function frontend_register_scripts() {
531
532 }
533
534 /**
535 * Enqueue script on front page
536 */
537 function wp_head_ac() {
538 wp_enqueue_script( 'sib-front-js', self::$plugin_url . '/js/mailin-front.js', array( 'jquery' ), filemtime( self::$plugin_dir . '/js/mailin-front.js' ), false );
539 wp_enqueue_style( 'sib-front-css', self::$plugin_url.'/css/mailin-front.css', array(), array(), 'all');
540 wp_localize_script(
541 'sib-front-js', 'sibErrMsg', array(
542 'invalidMail' => __( 'Please fill out valid email address', 'mailin' ),
543 'requiredField' => __( 'Please fill out required fields', 'mailin' ),
544 'invalidDateFormat' => __( 'Please fill out valid date format', 'mailin' ),
545 'invalidSMSFormat' => __( 'Please fill out valid phone number', 'mailin' ),
546 )
547 );
548 wp_localize_script(
549 'sib-front-js', 'ajax_sib_front_object',
550 array(
551 'ajax_url' => admin_url( 'admin-ajax.php' ),
552 'ajax_nonce' => wp_create_nonce( 'sib_front_ajax_nonce' ),
553 'flag_url' => plugins_url('img/flags/', __FILE__ ),
554 )
555 );
556 }
557
558 /**
559 * Install method is called once install this plugin.
560 * create tables, default option ...
561 */
562 static function install() {
563 $general_settings = get_option( self::MAIN_OPTION_NAME, array() );
564 $access_key = isset( $general_settings['access_key'] ) ? $general_settings['access_key'] : '';
565 if ( '' === $access_key ) {
566 // Default option when activate.
567 $home_settings = array(
568 'activate_email' => 'no',
569 'activate_ma' => 'default',
570 );
571 update_option( self::HOME_OPTION_NAME, $home_settings );
572 }
573
574 self::activate_brevo_connection();
575 }
576
577 /**
578 * Uninstall method is called once uninstall this plugin
579 * delete tables, options that used in plugin
580 */
581 static function uninstall() {
582 $setting = array();
583 update_option( SIB_Manager::MAIN_OPTION_NAME, $setting );
584
585 $home_settings = array(
586 'activate_email' => 'no',
587 'activate_ma' => 'default',
588 );
589 update_option( SIB_Manager::HOME_OPTION_NAME, $home_settings );
590
591 // Delete access_token.
592 $token_settings = array();
593 update_option( SIB_Manager::ACCESS_TOKEN_OPTION_NAME, $token_settings );
594
595 //Deactivate the connection on Brevo
596 self::deactivate_brevo_connection();
597
598 //Then delete the api key in our plugin
599 delete_option(SIB_Manager::API_KEY_V3_OPTION_NAME);
600 // Empty tables.
601 SIB_Model_Users::removeTable();
602 SIB_Forms::removeTable();
603 SIB_Forms_Lang::removeTable();
604
605 // Remove all transient.
606 SIB_API_Manager::remove_transients();
607 }
608
609 static function deactivate_brevo_connection()
610 {
611 $installationId = get_option( SIB_Manager::INSTALLATION_ID );
612 if(!empty($installationId))
613 {
614 $apiClient = new SendinblueApiClient();
615 $params["connection"] = 27;
616 $params["plugin_version"] = SendinblueApiClient::PLUGIN_VERSION;
617 $params["active"] = false;
618 $params["deactivated_at"] = gmdate("Y-m-d\TH:i:s\Z");
619 $apiClient->updateInstallationInfo($installationId, $params);
620 }
621 }
622
623 static function activate_brevo_connection()
624 {
625 $installationId = get_option( SIB_Manager::INSTALLATION_ID );
626 if(!empty($installationId))
627 {
628 $apiClient = new SendinblueApiClient();
629 $params["connection"] = 27;
630 $params["plugin_version"] = SendinblueApiClient::PLUGIN_VERSION;
631 $params["active"] = true;
632 $params["activated_at"] = gmdate("Y-m-d\TH:i:s\Z");
633 $apiClient->updateInstallationInfo($installationId, $params);
634 }
635 }
636
637 /**
638 * Deactivate method is called once deactivate this plugin
639 */
640 static function deactivate() {
641 update_option( SIB_Manager::LANGUAGE_OPTION_NAME, false );
642 self::uninstall_service_worker_script();
643 // Remove sync users option.
644 delete_option( 'sib_sync_users' );
645 // Remove all transient.
646 SIB_API_Manager::remove_transients();
647
648 //Also deactivate the connection on Brevo
649 self::deactivate_brevo_connection();
650 }
651
652 /**
653 * Check if plugin is logged in.
654 *
655 * @param bool $redirect
656 * @return bool
657 */
658 static function is_done_validation($redirect = true) {
659 if (self::is_api_key_set()) {
660 $apiClient = new SendinblueApiClient();
661 $apiClient->getAccount();
662 if ( SendinblueApiClient::RESPONSE_CODE_OK === $apiClient->getLastResponseCode() ) {
663 //This is only for those users who have an active connection but no installation id could be
664 //saved on their shop
665 $installationId = get_option( SIB_Manager::INSTALLATION_ID );
666 if(empty($installationId))
667 {
668 self::fetch_and_save_installation_id();
669 }
670 return true;
671 } elseif (SendinblueApiClient::RESPONSE_CODE_UNAUTHORIZED === $apiClient->getLastResponseCode()) {
672 delete_option(SIB_Manager::API_KEY_V3_OPTION_NAME);
673 }
674 }
675
676 if ($redirect) {
677 self::redirect_to_sib_plugin_homepage();
678 }
679
680 return false;
681 }
682
683 static function redirect_to_sib_plugin_homepage() {
684 wp_safe_redirect(add_query_arg('page', SIB_Page_Home::PAGE_ID, admin_url('admin.php')));
685 }
686
687 /**
688 * @return bool
689 */
690 static function is_api_key_set() {
691 $api_key = get_option(SIB_Manager::API_KEY_V3_OPTION_NAME);
692 return !empty($api_key);
693 }
694
695 static function is_ma_active() {
696 $general_settings = get_option( SIB_Manager::MAIN_OPTION_NAME, array() );
697 $ma_key = isset( $general_settings['ma_key'] ) ? sanitize_text_field($general_settings['ma_key']) : null;
698 if ( $ma_key === null || strlen($ma_key) === 0 ) {
699 return false;
700 }
701 $home_settings = get_option( SIB_Manager::HOME_OPTION_NAME, array() );
702 $activate_ma = isset( $home_settings['activate_ma'] ) ? $home_settings['activate_ma'] : 'default';
703 return 'no' !== $activate_ma;
704 }
705
706 static function fetch_and_save_installation_id()
707 {
708 $apiClient = new SendinblueApiClient();
709
710 $params["partnerName"] = "WORDPRESS";
711 $params["plugin_version"] = SendinblueApiClient::PLUGIN_VERSION;
712 $params["shop_url"] = get_home_url();
713 $params["active"] = true;
714 $params["connection"] = 27;
715 $response = $apiClient->createInstallationInfo($params);
716 if ( $apiClient->getLastResponseCode() === SendinblueApiClient::RESPONSE_CODE_CREATED )
717 {
718 if(!empty($response["id"]))
719 {
720 update_option(SIB_Manager::INSTALLATION_ID, $response["id"]);
721 }
722 }
723 }
724
725 /**
726 * Uninstall service-worker script from plugin
727 * @return void
728 */
729 static function uninstall_service_worker_script()
730 {
731 try {
732 $service_worker_file = __DIR__ . self::SERVICE_WORKER_FILE_URL;
733 if (file_exists($service_worker_file)) {
734 wp_delete_file($service_worker_file);
735 }
736 update_option('sib_service_worker_install_exception', '');
737 } catch (\Throwable $th) {
738 update_option('sib_service_worker_uninstall_exception', $th->getMessage());
739 }
740 }
741
742 /**
743 * Install marketing automation script in header
744 */
745 function install_ma_script() {
746 $push_enabled = SIB_Push_Utils::is_push_sdk_enabled();
747 if ( SIB_Manager::is_ma_active() ) {
748 $init_options = json_encode( SIB_Push_Utils::brevo_init_options() );
749 $script_url = self::is_staging() ? self::SDK_LOADER_STAGING_URL : self::SDK_LOADER_PRODUCTION_URL;
750 $output = <<<EOT
751 <script type="text/javascript" src="{$script_url}" async></script>
752 <script type="text/javascript">
753 window.Brevo = window.Brevo || [];
754 window.Brevo.push(['init', {$init_options}]);
755 </script>
756 EOT;
757 echo $output;
758 } else {
759 self::uninstall_service_worker_script();
760 }
761 if ( $push_enabled ) {
762 $init_options = SIB_Push_Utils::wonderpush_init_options();
763 $script_url = 'https://cdn.by.wonderpush.com/sdk/1.1/wonderpush-loader.min.js';
764 if ($init_options) {
765 $encoded_init_options = json_encode($init_options);
766 $output = <<<EOT
767 <script type="text/javascript" src="{$script_url}" async></script>
768 <script type="text/javascript">
769 window.WonderPush = window.WonderPush || [];
770 window.WonderPush.push(['init', {$encoded_init_options}]);
771 </script>
772 EOT;
773 echo $output;
774 }
775 }
776 }
777
778 /**
779 * Register widget
780 */
781 function sib_create_widget() {
782 register_widget( 'SIB_Widget_Subscribe' );
783 }
784
785 /**
786 * Display form on front page
787 *
788 * @param string $frmID - form ID.
789 * @param string $lang - form language.
790 */
791 function generate_form_box( $frmID = '-1', $lang = '' ) {
792 if ( 'oldForm' == $frmID ) {
793 $frmID = get_option( 'sib_old_form_id' );
794 } elseif ( '' != $lang ) {
795 $trans_id = SIB_Forms_Lang::get_form_ID( $frmID, $lang );
796 if ( null != $trans_id ) {
797 $frmID = $trans_id;
798 }
799 }
800
801 $formData = SIB_Forms::getForm( $frmID );
802
803 if ( empty( $formData ) ) {
804 return;
805 }
806 // Add Google recaptcha
807 if( '0' != $formData['gCaptcha'] && $formData['selectCaptchaType'] != 3) {
808 if( '1' == $formData['gCaptcha'] ) { // For old forms.
809 $formData['html'] = preg_replace( '/([\s\S]*?)<div class="g-recaptcha"[\s\S]*?data-size="invisible"><\/div>/', '$1', $formData['html'] );
810 }
811 if ( '3' == $formData['gCaptcha'] ) // The case of using google recaptcha.
812 {
813 ?>
814 <script type="text/javascript">
815 var onloadSibCallback = function () {
816 jQuery('.g-recaptcha').each(function (index, el) {
817 grecaptcha.render(el, {
818 'sitekey': jQuery(el).attr('data-sitekey')
819 });
820 });
821 };
822 </script>
823 <?php
824 } else { // The case of using google invisible recaptcha.
825 $formData['html'] = str_contains( $formData['html'], 'sib-default-btn' ) ? str_replace(
826 'type="submit"',
827 'type="submit" id="invisible"',
828 $formData['html']
829 ) : $formData['html'];
830 ?>
831 <script type="text/javascript">
832 var gCaptchaSibWidget;
833 var onloadSibCallbackInvisible = function () {
834
835 var element = document.getElementsByClassName('sib-default-btn');
836 var countInvisible = 0;
837 var indexArray = [];
838 jQuery('.sib-default-btn').each(function (index, el) {
839 if ((jQuery(el).attr('id') == "invisible")) {
840 indexArray[countInvisible] = index;
841 countInvisible++
842 }
843 });
844
845 jQuery('.invi-recaptcha').each(function (index, el) {
846 grecaptcha.render(element[indexArray[index]], {
847 'sitekey': jQuery(el).attr('data-sitekey'),
848 'callback': sibVerifyCallback,
849 });
850 });
851 };
852 </script>
853 <?php
854 }
855 ?>
856 <script src="https://www.google.com/recaptcha/api.js?onload=<?php
857 echo esc_attr(
858 $formData['gCaptcha'] == '2' ? 'onloadSibCallbackInvisible' : 'onloadSibCallback'
859 ) ?>&render=explicit" async defer></script>
860 <?php
861 } else if ('0' != $formData['gCaptcha'] && $formData['selectCaptchaType'] == 3) { ?>
862
863 <script src="https://challenges.cloudflare.com/turnstile/v0/api.js"></script>
864
865 <?php } ?>
866
867 <form id="sib_signup_form_<?php echo esc_attr( $frmID ); ?>" method="post" class="sib_signup_form">
868 <div class="sib_loader" style="display:none;"><img
869 src="<?php echo esc_url( includes_url() ); ?>images/spinner.gif" alt="loader"></div>
870 <input type="hidden" name="sib_form_action" value="subscribe_form_submit">
871 <input type="hidden" name="sib_form_id" value="<?php echo esc_attr( $frmID ); ?>">
872 <input type="hidden" name="sib_form_alert_notice" value="<?php echo esc_attr($formData['requiredMsg']); ?>">
873 <input type="hidden" name="sib_form_invalid_email_notice" value="<?php echo esc_attr($formData['invalidMsg']); ?>">
874 <input type="hidden" name="sib_security" value="<?php echo esc_attr( wp_create_nonce( 'sib_front_ajax_nonce' ) ); ?>">
875 <div class="sib_signup_box_inside_<?php echo esc_attr( $frmID ); ?>">
876 <div style="/*display:none*/" class="sib_msg_disp">
877 </div>
878 <?php
879 if (($formData['gCaptcha'] == '2') && false === strpos(
880 $formData['html'],
881 'id="sib_captcha_invisible"'
882 )) { ?>
883 <div id="sib_captcha_invisible" class="invi-recaptcha" data-sitekey="<?php
884 echo esc_attr($formData['gCaptcha_site']); ?>"></div>
885 <?php
886 } ?>
887 <?php
888 // phpcs:ignore
889
890 if (false === strpos($formData['html'], 'class="g-recaptcha"')) {
891 $formData['html'] = str_replace(
892 'id="sib_captcha"',
893 'id="sib_captcha" class="g-recaptcha" data-sitekey="' . $formData['gCaptcha_site'] . '"',
894 $formData['html']
895 );
896 }
897
898 echo wp_kses($formData['html'], SIB_Manager::wordpress_allowed_attributes());
899 ?>
900 </div>
901 </form>
902 <style>
903 <?php
904
905 if ( ! $formData['dependTheme'] ) {
906 // Custom css.
907 $formData['css'] = str_replace( '[form]', 'form#sib_signup_form_' . $frmID, $formData['css'] );
908 echo esc_html($formData['css']);
909 }
910 $msgCss = str_replace( '[form]', 'form#sib_signup_form_' . $frmID, SIB_Forms::getDefaultMessageCss() );
911 echo esc_html($msgCss);
912 ?>
913 </style>
914 <?php
915 }
916
917 /**
918 * Shortcode for sign up form
919 *
920 * @param array $atts - shortcode parameter.
921 * @return string
922 */
923 function sibwp_form_shortcode( $atts ) {
924 $pull_atts = shortcode_atts(
925 array(
926 'id' => 'oldForm', // We will return 'oldForm' for shortcode of old form.
927 ), $atts
928 );
929 $frmID = $pull_atts['id'];
930 $lang = defined( 'ICL_LANGUAGE_CODE' ) ? ICL_LANGUAGE_CODE : '';
931
932 ob_start();
933 $this->generate_form_box( $frmID, $lang );
934
935 $output_string = ob_get_contents();
936 ob_end_clean();
937 return $output_string;
938 }
939
940 /**
941 * Sign up process
942 */
943 function signup_process() {
944 //Handling of backslash added by WP because magic quotes are enabled by default
945 array_walk_recursive( $_POST, function(&$value) {
946 $value = stripslashes($value);
947 });
948
949 if ( empty( $_POST['sib_security'] ) || empty(wp_verify_nonce($_POST['sib_security'], 'sib_front_ajax_nonce'))) {
950 wp_send_json(
951 array(
952 'status' => 'sib_security',
953 'msg' => 'Invalid Token Provided.',
954 )
955 );
956 }
957 $formID = isset( $_POST['sib_form_id'] ) ? sanitize_text_field( $_POST['sib_form_id'] ) : 1;
958 if ( 'oldForm' == $formID ) {
959 $formID = get_option( 'sib_old_form_id' );
960 }
961 $formData = SIB_Forms::getForm( $formID );
962
963 if (!SIB_Manager::is_done_validation(false) || 0 == count($formData)) {
964 wp_send_json(
965 array(
966 'status' => 'failure',
967 'msg' => array("errorMsg" => "Something wrong occurred"),
968 )
969 );
970 }
971 $turnstileCaptcha = false;
972 if ( '0' != $formData['gCaptcha'] && 3 != $formData['selectCaptchaType']) {
973 $turnstileCaptcha = true;
974 if ( ! isset( $_POST['g-recaptcha-response'] ) || empty( $_POST['g-recaptcha-response'] ) ) {
975 wp_send_json(
976 array(
977 'status' => 'gcaptchaEmpty',
978 'msg' => 'Please click on the reCAPTCHA box.',
979 )
980 );
981 }
982 $secret = $formData['gCaptcha_secret'];
983
984 $data = array(
985 'secret' => $secret,
986 'response' => sanitize_text_field( $_POST['g-recaptcha-response'] ),
987 );
988
989 $args = [
990 'method' => 'POST',
991 ];
992
993 try {
994 $data = wp_remote_retrieve_body(wp_remote_request(sprintf(self::RECAPTCHA_API_TEMPLATE, http_build_query($data)), $args));
995 $responseData = json_decode($data);
996 if ( ! $responseData->success ) {
997 wp_send_json(
998 array(
999 'status' => 'gcaptchaFail',
1000 'msg' => 'Robot verification failed, please try again.',
1001 )
1002 );
1003 }
1004 } catch (Exception $exception) {
1005 wp_send_json(
1006 array(
1007 'status' => 'gcaptchaFail',
1008 'msg' => $exception->getMessage(),
1009 )
1010 );
1011 }
1012 } else if ( '0' != $formData['gCaptcha'] && 3 == $formData['selectCaptchaType'] ) {
1013 $turnstileCaptcha = true;
1014 if ( ! isset( $_POST['cf-turnstile-response'] ) || empty( $_POST['cf-turnstile-response'] ) ) {
1015 wp_send_json(
1016 array(
1017 'status' => 'gcaptchaEmpty',
1018 'msg' => 'Captcha couldnot be verified. Please refresh the page.',
1019 )
1020 );
1021 }
1022 $secret = $formData['cCaptcha_secret'];
1023
1024 $args = [
1025 'method' => 'POST',
1026 ];
1027
1028 try {
1029
1030 $headers = array(
1031 'body' => [
1032 'secret' => $secret,
1033 'response' => sanitize_text_field( $_POST['cf-turnstile-response'] )
1034 ]
1035 );
1036 $verify = wp_remote_post(self::TURNSTILE_SITE_VERIFY, $headers);
1037 $verify = wp_remote_retrieve_body($verify);
1038 $response = json_decode($verify);
1039
1040 if($response->success) {
1041 $results['success'] = $response->success;
1042 } else {
1043 $results['success'] = false;
1044 }
1045
1046 if ( ! $response->success ) {
1047 wp_send_json(
1048 array(
1049 'status' => 'gcaptchaFail',
1050 'msg' => 'Robot verification failed, please try again.',
1051 )
1052 );
1053 }
1054 } catch (Exception $exception) {
1055 wp_send_json(
1056 array(
1057 'status' => 'gcaptchaFail',
1058 'msg' => $exception->getMessage(),
1059 )
1060 );
1061 }
1062 }
1063
1064 $listID = $formData['listID'];
1065 if (empty($listID)) {
1066 $listID = array();
1067 }
1068 $interestingLists = isset( $_POST['interestingLists']) ? array_map( 'sanitize_text_field', $_POST['interestingLists'] ) : array();
1069 $expectedLists = isset( $_POST['listIDs'] ) ? array_map( 'sanitize_text_field', $_POST['listIDs'] ) : array();
1070 if ( empty($interestingLists) )
1071 {
1072 $unlinkedLists = [];
1073 }
1074 else{
1075 $unwantedLists = array_diff( $interestingLists, $expectedLists );
1076 $unlinkedLists = array_diff( $unwantedLists, $listID);
1077 $listID = array_unique(array_merge( $listID, $expectedLists ));
1078 }
1079
1080 $email = isset( $_POST['email'] ) ? sanitize_email( $_POST['email'] ) : '';
1081 if ( ! is_email( $email ) ) {
1082 return;
1083 }
1084
1085 $isDoubleOptin = $formData['isDopt'];
1086 $isOptin = $formData['isOpt'];
1087 $redirectUrlInEmail = $formData['redirectInEmail'];
1088 $redirectUrlInForm = $formData['redirectInForm'];
1089
1090 $info = array();
1091 $attributes = explode( ',', $formData['attributes'] ); // String to array.
1092 if ( isset( $attributes ) && is_array( $attributes ) ) {
1093 foreach ( $_POST as $postAttribute => $postAttributeValue ) {
1094 $correspondingSibAttribute = $this->getCorrespondingSibAttribute($postAttribute, $attributes);
1095 if (!empty($correspondingSibAttribute)) {
1096 $info[$correspondingSibAttribute] = is_array($postAttributeValue)
1097 ? array_map('sanitize_text_field', $postAttributeValue)
1098 : sanitize_text_field($postAttributeValue);
1099
1100 }
1101 }
1102 }
1103 $templateID = $formData['templateID'];
1104
1105 if ( $isDoubleOptin ) {
1106 /*
1107 * Double optin process
1108 * 1. add record to db
1109 * 2. send confirmation email with activate code
1110 */
1111 $result = "success";
1112 // Send a double optin confirm email.
1113 if ( 'success' == $result ) {
1114 // Add a recode with activate code in db.
1115 $activateCode = $this->create_activate_code( $email, $info, $formID, $listID, $redirectUrlInEmail, $unlinkedLists );
1116 SIB_API_Manager::send_comfirm_email( $email, 'double-optin', $templateID, $info, $activateCode );
1117 }
1118 } elseif ( $isOptin ) {
1119 $result = SIB_API_Manager::create_subscriber( $email, $listID, $info, 'confirm', $unlinkedLists );
1120 if ( 'success' == $result ) {
1121 // Send a confirm email.
1122 SIB_API_Manager::send_comfirm_email( $email, 'confirm', $templateID, $info );
1123 }
1124 } else {
1125 $result = SIB_API_Manager::create_subscriber( $email, $listID, $info, 'simple', $unlinkedLists );
1126 }
1127 $msg = array(
1128 'successMsg' => $formData['successMsg'],
1129 'errorMsg' => $formData['errorMsg'],
1130 'existMsg' => $formData['existMsg'],
1131 'invalidMsg' => $formData['invalidMsg'],
1132 );
1133
1134 wp_send_json(
1135 array(
1136 'status' => $result,
1137 'msg' => $msg,
1138 'redirect' => $redirectUrlInForm,
1139 'turnstileCaptcha' => $turnstileCaptcha,
1140 )
1141 );
1142 }
1143
1144 /**
1145 * Create activate code for Double optin
1146 *
1147 * @param string $email - user email.
1148 * @param array $info - info.
1149 * @param string $formID - form ID.
1150 * @param array $listIDs - lists.
1151 * @param string $redirectUrl - redirect url.
1152 * @return string - activate code.
1153 */
1154 function create_activate_code( $email, $info, $formID, $listIDs, $redirectUrl, $unlinkedLists = null ) {
1155 $data = SIB_Model_Users::get_data_by_email( $email, $formID );
1156 $date = gmdate( 'Y-m-d H:i:s' );
1157 if ( $unlinkedLists != null )
1158 {
1159 $info['unlinkedLists'] = $unlinkedLists;
1160 }
1161 if ( false == $data ) {
1162 $uniqid = uniqid();
1163 $data = array(
1164 'email' => $email,
1165 'code' => $uniqid,
1166 'info' => maybe_serialize( $info ),
1167 'frmid' => $formID,
1168 'listIDs' => maybe_serialize( $listIDs ),
1169 'redirectUrl' => $redirectUrl,
1170 'user_added_date' => $date,
1171 'doi_sent' => 0,
1172 );
1173 SIB_Model_Users::add_record( $data );
1174 } else {
1175 $update_data = array(
1176 'id' => $data['id'],
1177 'email' => $email,
1178 'info' => maybe_serialize( $info ),
1179 );
1180 SIB_Model_Users::update_element( $update_data );
1181 $uniqid = $data['code'];
1182 }
1183 return $uniqid;
1184 }
1185
1186 /**
1187 * Use Sendinblue SMTP to send all emails
1188 *
1189 * @param string $to - reception email.
1190 * @param string $subject - subject of email.
1191 * @param string $message - message of email.
1192 * @param string $headers - header of email.
1193 * @param array $attachments - attachments.
1194 */
1195 static function wp_mail_native( $to, $subject, $message, $headers = '', $attachments = array() ) {
1196 $result = require self::$plugin_dir . '/inc/function.wp_mail.php';
1197 return $result;
1198 }
1199
1200 /**
1201 * To send the transactional email via Sendinblue
1202 * hook wp_mail
1203 *
1204 * @param string $to - reception email.
1205 * @param string $subject - subject of email.
1206 * @param string $message - message of email.
1207 * @param string $headers - header of email.
1208 * @param array $attachments - attachments
1209 * @param array $tags - tag.
1210 * @param string $from_name - sender name.
1211 * @param string $from_email - sender email.
1212 * @return mixed|WP_Error
1213 */
1214 static function sib_email( $to, $subject, $message, $headers = '', $attachments = array(), $tags = array(), $from_name = '', $from_email = '' ) {
1215 $data = [];
1216 // Compact the input, apply the filters, and extract them back out.
1217 extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) );
1218
1219 if ( !empty( $attachments ) && ! is_array( $attachments ) ) {
1220 $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );
1221 }
1222
1223 // From email and name.
1224 $home_settings = get_option( SIB_Manager::HOME_OPTION_NAME );
1225 if ( isset( $home_settings['sender'] ) ) {
1226 $from_name = $home_settings['from_name'];
1227 $from_email = $home_settings['from_email'];
1228 } else {
1229 $from_email = trim( get_bloginfo( 'admin_email' ) );
1230 $from_name = trim( get_bloginfo( 'name' ) );
1231 }
1232
1233 //Set additional address fields as empty
1234 $bcc = array();
1235 $cc = array();
1236 $reply_to = array();
1237 if ( ! is_array( $to ) ) {
1238 $to = explode( ',', $to );
1239 }
1240
1241 $from_email = apply_filters( 'wp_mail_from', $from_email );
1242 $from_name = apply_filters( 'wp_mail_from_name', $from_name );
1243
1244 if ( !empty( $headers ) ) {
1245 if( is_array( $headers ) ){
1246 foreach ($headers as $key => $val) {
1247 if( stripos($val, "Content-Type: text/html") !== false ) {
1248 unset( $headers[$key] );
1249 }
1250 }
1251 $headers = array_values( $headers );
1252 if( count( $headers ) == 1 && $headers[0] == '' ) {
1253 unset( $headers[0] );
1254 }
1255 }
1256 if( is_string( $headers ) ){
1257 $headers = str_replace("Content-Type: text/html", "", $headers);
1258 }
1259 if( !empty( $headers ) ){
1260 $data['headers'] = $headers;
1261 }
1262 if ( ! is_array( $headers ) ) {
1263 // Explode the headers out, so this function can take both.
1264 // string headers and an array of headers.
1265 $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
1266 } else {
1267 $tempheaders = $headers;
1268 }
1269 $headers = array();
1270 // If it's actually got contents.
1271 if ( ! empty( $tempheaders ) ) {
1272 // Iterate through the raw headers.
1273 foreach ( (array) $tempheaders as $header ) {
1274 if ( strpos( $header, ':' ) === false ) {
1275 if ( false !== stripos( $header, 'boundary=' ) ) {
1276 $parts = preg_split( '/boundary=/i', trim( $header ) );
1277 $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
1278 }
1279 continue;
1280 }
1281 // Explode them out.
1282 list($name, $content) = explode( ':', trim( $header ), 2 );
1283
1284 // Cleanup crew.
1285 $name = trim( $name );
1286 $content = trim( $content );
1287
1288 switch ( strtolower( $name ) ) {
1289 case 'content-type':
1290 $headers[ trim( $name ) ] = trim( $content );
1291 break;
1292 case 'x-mailin-tag':
1293 $headers[ trim( $name ) ] = trim( $content );
1294 break;
1295 case 'from':
1296 if ( strpos( $content, '<' ) !== false ) {
1297 // So... making my life hard again?
1298 $from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
1299 $from_name = str_replace( '"', '', $from_name );
1300 $from_name = trim( $from_name );
1301
1302 $from_email = substr( $content, strpos( $content, '<' ) + 1 );
1303 $from_email = str_replace( '>', '', $from_email );
1304 $from_email = trim( $from_email );
1305 } else {
1306 $from_name = '';
1307 $from_email = trim( $content );
1308 }
1309 break;
1310
1311 case 'cc':
1312 $cc = array_merge( (array) $cc, explode( ',', $content ) );
1313 break;
1314
1315 case 'bcc':
1316 $bcc = array_merge( (array) $bcc, explode( ',', $content ) );
1317 break;
1318
1319 case 'reply-to':
1320 $reply_to = array_merge( (array) $reply_to, explode( ',', $content ) );
1321 break;
1322 default:
1323 break;
1324 }
1325 }
1326 }
1327 }
1328
1329 // Set destination addresses, using appropriate methods for handling addresses.
1330 $address_headers = compact('to', 'cc', 'bcc', 'reply_to');
1331 $processed_address_fields = self::processAddressFields($address_headers);
1332 $data = array_merge($data, $processed_address_fields);
1333 // Attachments.
1334 $attachment_content = array();
1335 if ( ! empty( $attachments ) ) {
1336 foreach ( $attachments as $attachment ) {
1337 if ( !empty( $attachment ) ) {
1338 $content = self::getAttachmentStruct( $attachment );
1339 if ( ! is_wp_error( $content ) ) {
1340 array_push( $attachment_content, $content );
1341 }
1342 }
1343 }
1344 if ( !empty( $attachment_content ) ) {
1345 $data["attachment"] = $attachment_content;
1346 }
1347 }
1348
1349 // Common transformations for the HTML part.
1350 // If it is text/plain, New line break found.
1351 if ( strpos( $message, '</table>' ) === false && strpos( $message, '</div>' ) === false ) {
1352 if ( strpos( $message, "\n" ) !== false ) {
1353 if ( is_array( $message ) ) {
1354 foreach ( $message as &$value ) {
1355 $value['content'] = preg_replace( '#<(https?://[^*]+)>#', '$1', $value['content'] );
1356 $value['content'] = nl2br( $value['content'] );
1357 }
1358 } else {
1359 $message = preg_replace( '#<(https?://[^*]+)>#', '$1', $message );
1360 $message = nl2br( $message );
1361 }
1362 }
1363 }
1364 // Sending...
1365 $data['sender'] = ['email' => $from_email, 'name' => $from_name ];
1366 $data['subject'] = $subject;
1367 $data['htmlContent'] = $message;
1368
1369 try {
1370 $sent = SIB_API_Manager::send_email( $data );
1371 return $sent;
1372 } catch ( Exception $e ) {
1373 return new WP_Error( $e->getMessage() );
1374 }
1375 }
1376
1377 /**
1378 * @param array $address_fields
1379 * @return array
1380 */
1381 private static function processAddressFields($address_fields)
1382 {
1383 $data = [
1384 'to' => [],
1385 'cc' => [],
1386 'bcc' => [],
1387 'replyTo' => [],
1388 ];
1389
1390 $address_fields['reply_to'] = is_array($address_fields['reply_to'])
1391 && count($address_fields['reply_to']) > 1 ? $address_fields['reply_to'][0] : $address_fields['reply_to'];
1392 foreach ($address_fields as $address_header => $addresses) {
1393 if (empty($addresses)) {
1394 continue;
1395 }
1396
1397 foreach ((array) $addresses as $address) {
1398 // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>".
1399 if (preg_match('/(.*)<(.+)>/', $address, $matches)) {
1400 if (count($matches) == 3) {
1401 $address = preg_replace('/\s+/', '', $matches[2]); //strip whitespaces
1402 }
1403 }
1404
1405 switch ($address_header) {
1406 case 'to':
1407 $data['to'][] = ['email' => $address];
1408 break;
1409 case 'cc':
1410 $data['cc'][] = ['email' => $address];
1411 break;
1412 case 'bcc':
1413 $data['bcc'][] = ['email' => $address];
1414 break;
1415 case 'reply_to':
1416 $data['replyTo']['email'] = $address;
1417 break;
1418 }
1419 }
1420 }
1421 return $data;
1422 }
1423
1424 /**
1425 * @param string $path - attachment file path
1426 * @return array|WP_Error
1427 */
1428 static function getAttachmentStruct( $path ) {
1429
1430 $struct = array();
1431
1432 try {
1433
1434 if ( ! @is_file( $path ) ) {
1435 throw new Exception( $path . ' is not a valid file.' );
1436 }
1437
1438 $filename = basename( $path );
1439
1440 if ( ! function_exists( 'get_magic_quotes' ) ) {
1441 /**
1442 * @return bool
1443 */
1444 function get_magic_quotes() {
1445 return false;
1446 }
1447 }
1448 if ( ! function_exists( 'set_magic_quotes' ) ) {
1449 /**
1450 * @param $value
1451 * @return bool
1452 */
1453 function set_magic_quotes( $value ) {
1454 return true;
1455 }
1456 }
1457
1458 $isMagicQuotesSupported = version_compare( PHP_VERSION, '5.3.0', '<' )
1459 && function_exists( 'get_magic_quotes_runtime' )
1460 && function_exists( 'set_magic_quotes_runtime' );
1461
1462 if ( $isMagicQuotesSupported ) {
1463 // Escape linters check.
1464 $getMagicQuotesRuntimeFunc = 'get_magic_quotes_runtime';
1465 $setMagicQuotesRuntimeFunc = 'set_magic_quotes_runtime';
1466
1467 // Save magic quotes value.
1468 $magicQuotes = $getMagicQuotesRuntimeFunc();
1469 $setMagicQuotesRuntimeFunc (0);
1470 }
1471
1472 $file_buffer = file_get_contents( $path );
1473 $file_buffer = base64_encode($file_buffer);
1474
1475 if ( $isMagicQuotesSupported ) {
1476 // Restore magic quotes value.
1477 $setMagicQuotesRuntimeFunc($magicQuotes);
1478 }
1479
1480 $struct["name"] = $filename;
1481 $struct["content"] = $file_buffer;
1482
1483 } catch ( Exception $e ) {
1484 return new WP_Error( 'Error creating the attachment structure: ' . $e->getMessage() );
1485 }
1486
1487 return $struct;
1488 }
1489
1490 /**
1491 * Create custom page for form preview
1492 *
1493 * @param array $query_vars - query.
1494 * @return array
1495 */
1496 function sib_query_vars( $query_vars ) {
1497 $query_vars[] = 'sib_form';
1498 return $query_vars;
1499 }
1500
1501 /**
1502 * Parse request
1503 *
1504 * @param mixed $wp - object.
1505 */
1506 function sib_parse_request( &$wp ) {
1507 if ( array_key_exists( 'sib_form', $wp->query_vars ) ) {
1508 include 'inc/sib-form-preview.php';
1509 exit();
1510 }
1511 }
1512
1513 /**
1514 * Load Text domain.
1515 */
1516 static function LoadTextDomain() {
1517 // Load lang file.
1518 $i18n_file_name = 'mailin';
1519 $locale = apply_filters( 'plugin_locale', get_locale(), $i18n_file_name );
1520 // $locale = 'fr_FR';
1521 $filename = plugin_dir_path( __FILE__ ) . '/lang/' . $i18n_file_name . '-' . $locale . '.mo';
1522 load_textdomain( 'mailin', $filename );
1523 }
1524
1525 /**
1526 * Notice the language is difference than site's language
1527 */
1528 static function language_admin_notice() {
1529 if ( ! get_option( SIB_Manager::LANGUAGE_OPTION_NAME ) ) {
1530 $lang_prefix = substr( get_bloginfo( 'language' ), 0, 2 );
1531 $lang = self::getLanguageName( $lang_prefix );
1532 $class = 'error';
1533 $message = sprintf( 'Please note that your Brevo account is in %s, but Brevo WordPress plugin is only available in English / French for now. Sorry for inconvenience.', $lang );
1534 if ( 'en' !== $lang_prefix && 'fr' !== $lang_prefix ) {
1535 // phpcs:ignore
1536 echo ( "<div class=\"$class\" style='margin-left: 2px;margin-bottom: 4px;'> <p>$message<a class='' href='?dismiss_admin_lang_notice=1'> No problem...</a></p></div>" );
1537 }
1538 }
1539 }
1540
1541 /**
1542 * Notice wp_mail is not possible
1543 */
1544 static function wpMailNotices() {
1545 if ( self::$wp_mail_conflict ) {
1546 echo ( '<div class="error"><p>' . __( 'You cannot use Brevo SMTP now because wp_mail has been declared by another process or plugin. ', 'mailin' ) . '</p></div>' );
1547 }
1548 }
1549
1550 /**
1551 * Names of languages.
1552 *
1553 * @param string $prefix - language.
1554 * @return mixed
1555 */
1556 public static function getLanguageName( $prefix = 'en' ) {
1557 $lang = array();
1558 $lang['de'] = 'Deutsch';
1559 $lang['en'] = 'English';
1560 $lang['zh'] = '中文';
1561 $lang['ru'] = 'Русский';
1562 $lang['fi'] = 'suomi';
1563 $lang['fr'] = 'Français';
1564 $lang['nl'] = 'Nederlands';
1565 $lang['sv'] = 'Svenska';
1566 $lang['it'] = 'Italiano';
1567 $lang['ro'] = 'Română';
1568 $lang['hu'] = 'Magyar';
1569 $lang['ja'] = '日本語';
1570 $lang['es'] = 'Español';
1571 $lang['vi'] = 'Tiếng Việt';
1572 $lang['ar'] = 'العربية';
1573 $lang['pt'] = 'Português';
1574 $lang['pb'] = 'Português do Brasil';
1575 $lang['pl'] = 'Polski';
1576 $lang['gl'] = 'galego';
1577 $lang['tr'] = 'Turkish';
1578 $lang['et'] = 'Eesti';
1579 $lang['hr'] = 'Hrvatski';
1580 $lang['eu'] = 'Euskera';
1581 $lang['el'] = 'Ελληνικά';
1582 $lang['ua'] = 'Українська';
1583 $lang['ko'] = '한국어';
1584
1585 return $lang[ $prefix ];
1586 }
1587
1588 /**
1589 * Create language sidebar for wpml plugin.
1590 */
1591 public function sib_create_language_sidebar() {
1592 $languages = apply_filters( 'wpml_active_languages', array() );
1593 $page = isset( $_GET['page'] ) ? sanitize_text_field( $_GET['page'] ) : '';
1594 $action = isset( $_GET['action'] ) ? sanitize_text_field( $_GET['action'] ) : '';
1595 $frmID = isset( $_GET['id'] ) ? sanitize_text_field( $_GET['id'] ) : '';
1596 $pID = isset( $_GET['pid'] ) ? sanitize_text_field( $_GET['pid'] ) : '';
1597 $parent = true;
1598 if ( '' !== $frmID && '' !== $pID ) {
1599 $lang = SIB_Forms_Lang::get_lang( $frmID, $pID );
1600 $parent = false;
1601 } else {
1602 $lang = ICL_LANGUAGE_CODE;
1603 if ( '' !== $frmID && '' === $pID ) {
1604 $pID = $frmID;
1605
1606 }
1607 }
1608
1609 if ( 'sib_page_form' === $page && 'edit' === $action ) {
1610 ?>
1611 <div class="panel panel-default text-left box-border-box sib-small-content">
1612 <div class="panel-heading"><strong><?php esc_attr_e( 'About Brevo', 'mailin' ); ?></strong></div>
1613 <div class="panel-body">
1614 <p>
1615 <label for='sib_form_language'><?php esc_attr_e( 'Language of this form:', 'mailin' ); ?> </label>
1616 <select id="sib_form_lang" name="sib_form_lang" data-selected="">
1617 <?php
1618 foreach ( $languages as $language ) {
1619 $selected = (isset($language['code']) && ($language['code'] == $lang)) ? 'selected' : '';
1620 if ( isset($language['code']) && $language['code'] == $lang && true === $parent ) {
1621 $option_text = '<option value="" ' . $selected . '>' . $language['native_name'] . '</option>';
1622 } else {
1623 $exist = SIB_Forms_Lang::get_form_ID( $pID, $language['language_code'] );
1624
1625 if ( null === $exist ) {
1626 continue;
1627 } else {
1628 $option_text = ( 'selected' === $selected ) ?
1629 sprintf( '<option value="" selected>%s</option>', esc_html( $language['native_name'] ) ) :
1630 sprintf( '<option value="%s" %s>%s</option>',
1631 esc_url( add_query_arg( array(
1632 'page' => sanitize_text_field( $_REQUEST['page'] ),
1633 'action' => 'edit',
1634 'pid' => absint( $pID ),
1635 'lang' => sanitize_text_field( $language['language_code'] )
1636 ) ) ),
1637 $selected,
1638 esc_html( $language['native_name'] )
1639 );
1640 }
1641 }
1642 echo $option_text ;
1643 }
1644 ?>
1645 </select>
1646 </p>
1647 <div class="sib_form_translate">
1648 <p>
1649 <label><?php esc_attr_e( 'Translate this form', 'mailin' ); ?></label>
1650 </p>
1651 <table aria-describedby="wpml-language-table" class="sib_form_trans_table" style="border: 1px solid #8cceea;">
1652 <tr>
1653 <?php
1654 foreach ( $languages as $language ) {
1655 if ( isset($language['code']) && $language['code'] == $lang ) {
1656 continue;
1657 }
1658 ?>
1659 <th style="text-align: center;"><img
1660 src="<?php echo esc_url( $language['country_flag_url'] ); ?>" alt="Flag of <?php echo esc_attr( $language['translated_name'] ); ?>"></th>
1661 <?php
1662 }
1663 ?>
1664 </tr>
1665 <tr style="background-color: #EFF8FC;">
1666 <?php
1667 foreach ( $languages as $language ) {
1668 if ( isset($language['code']) && $language['code'] == $lang ) {
1669 continue;
1670 }
1671 if ( '' === $pID ) {
1672 $img_src = plugins_url( 'img/add_translation_disabled.png', __FILE__ );
1673 $td = '<img src="' . $img_src . '" style="margin:2px;">';
1674 } else {
1675 $exist = SIB_Forms_Lang::get_form_ID( $pID, $language['language_code'] );
1676
1677 if ( null === $exist ) {
1678 $img_src = plugins_url( 'img/add_translation.png', __FILE__ );
1679
1680 $href = sprintf( '<a class="sib-form-redirect" href="?page=%s&action=%s&pid=%s&lang=%s" style="width: 20px; text-align: center;padding: 2px 1px;">', esc_attr( $_REQUEST['page'] ), 'edit', absint( $pID ), $language['language_code'] );
1681 $td = $href . '<img src="' . $img_src . '" style="margin:2px;"></a>';
1682 } else {
1683 $img_src = plugins_url( 'img/edit_translation.png', __FILE__ );
1684 $href = sprintf( '<a class="sib-form-redirect" href="%s" style="width: 20px; text-align: center;padding: 2px 1px;">', esc_url( add_query_arg( array(
1685 'page' => sanitize_text_field( $_REQUEST['page'] ),
1686 'action' => 'edit',
1687 'id' => absint( $exist ),
1688 'pid' => absint( $pID ),
1689 'lang' => sanitize_text_field( $language['language_code'] )
1690 ) ) ) );
1691 $td = $href . '<img src="' . $img_src . '" style="margin:2px;"></a>';
1692 }
1693 }
1694 ?>
1695 <td style="text-align: center;"><?php echo wp_kses($td, wp_kses_allowed_html('post')); ?></td>
1696 <?php
1697 }
1698 ?>
1699 </tr>
1700 </table>
1701 </div>
1702 <?php if ( isset( $_GET['pid'] ) ) { ?>
1703 <div class="sib-form-duplicate">
1704 <button class="btn btn-default sib-duplicate-btn"><?php esc_attr_e( 'Copy content from origin form', 'mailin' ); ?></button>
1705 <span class="sib-spin"><i
1706 class="fa fa-circle-o-notch fa-spin fa-lg"></i>&nbsp;&nbsp;</span>
1707 <i title="<?php echo esc_attr_e( 'Copy content from origin form', 'mailin' ); ?>"
1708 data-container="body" data-toggle="popover" data-placement="left"
1709 data-content="<?php echo esc_attr_e( 'You can copy contents from origin form. You need to translate the contents by this language.', 'mailin' ); ?>"
1710 data-html="true" class="fa fa-question-circle popover-help-form"></i>
1711 </div>
1712 <?php } ?>
1713 </div>
1714 </div>
1715 <?php
1716 }
1717 }
1718
1719 public function ajax_get_country_prefix() {
1720 check_ajax_referer( 'sib_front_ajax_nonce', 'security' );
1721 $sms_manager = new SIB_SMS_Code();
1722 $country_list = $sms_manager->get_sms_code_list();
1723 $country_list_html = '';
1724 foreach ( $country_list as $item => $value ) {
1725 $flg_url = plugins_url( 'img/flags/', __FILE__ ).strtolower($item).'.png';
1726 $item_html = '<li class="sib-country-prefix" data-country-code="'.$item.'" data-dial-code="'.$value["code"].'"><div class="sib-flag-box"><div class="sib-flag '.$item.'" style="background-image: url('.$flg_url.')"></div><span>'.$value['name'].'</span><span class="sib-dial-code">+'.$value['code'].'</span></div></li>';
1727 $country_list_html .= $item_html;
1728 }
1729 wp_send_json($country_list_html);
1730 }
1731
1732 /**
1733 * @param string $postAttribute
1734 * @param array $sibAttributes
1735 * @return null|string the corresponding sib attribute or null if not found
1736 */
1737 private function getCorrespondingSibAttribute($postAttribute, $sibAttributes)
1738 {
1739 $normalizedPostAttribute = strtoupper(sanitize_text_field($postAttribute));
1740 foreach ($sibAttributes as $sibAttribute) {
1741 if ($normalizedPostAttribute == strtoupper($sibAttribute)) {
1742 return $sibAttribute;
1743 }
1744 }
1745
1746 return null;
1747 }
1748
1749 public function my_upgrade_function() {
1750 $current_plugin_path_name = plugin_basename( __FILE__ );
1751 activate_plugin( $current_plugin_path_name );
1752 }
1753
1754
1755 public function brevo_wp_load()
1756 {
1757 $installationId = get_option( SIB_Manager::INSTALLATION_ID );
1758 $pluginVersion = get_option( SIB_Manager::BREVO_PLUGIN_VERSION );
1759 if(!empty($installationId) && (empty($pluginVersion) || $pluginVersion != SendinblueApiClient::PLUGIN_VERSION))
1760 {
1761 $apiClient = new SendinblueApiClient();
1762 $params["connection"] = 27;
1763 $params["plugin_version"] = SendinblueApiClient::PLUGIN_VERSION;
1764 $params["shop_version"] = get_bloginfo('version');
1765 $apiClient->updateInstallationInfo($installationId, $params);;
1766 if ( $apiClient->getLastResponseCode() === SendinblueApiClient::RESPONSE_CODE_NO_CONTENT )
1767 {
1768 update_option(SIB_Manager::BREVO_PLUGIN_VERSION, SendinblueApiClient::PLUGIN_VERSION);
1769 }
1770 }
1771 }
1772
1773 public static function wordpress_allowed_attributes()
1774 {
1775 global $allowedposttags, $allowedtags, $allowedentitynames;
1776 $attributes = [$allowedposttags, $allowedtags, $allowedentitynames, self::SIB_ATTRIBUTE];
1777 $attributes = call_user_func_array("array_merge", $attributes);
1778
1779 add_filter( 'safe_style_css', function($css_attr) {
1780 array_push($css_attr, 'display');
1781 return $css_attr;
1782 });
1783
1784 return $attributes;
1785 }
1786
1787 static function create_brevo_rest_endpoints() {
1788 $routes = array(
1789 array(
1790 self::ROUTE_PATH => '/mailin_disconnect',
1791 self::ROUTE_METHODS => 'DELETE',
1792 self::ROUTE_CALLBACK => function ($request) {
1793 return self::mailin_disconnect($request);
1794 },
1795 self::PERMISSION_CALLBACK => '__return_true',
1796 ),
1797 array(
1798 self::ROUTE_PATH => '/testconnection',
1799 self::ROUTE_METHODS => 'GET',
1800 self::ROUTE_CALLBACK => function ($request) {
1801 return self::mailin_testconnection($request);
1802 },
1803 self::PERMISSION_CALLBACK => '__return_true',
1804 )
1805 );
1806
1807 foreach ($routes as $route) {
1808 self::register_route($route);
1809 }
1810 }
1811
1812 private static function register_route(array $route)
1813 {
1814 $arguments = array(
1815 self::ROUTE_METHODS => $route[self::ROUTE_METHODS],
1816 self::ROUTE_CALLBACK => $route[self::ROUTE_CALLBACK],
1817 self::PERMISSION_CALLBACK => $route[self::PERMISSION_CALLBACK]
1818 );
1819
1820 register_rest_route(
1821 self::API_NAMESPACE,
1822 $route[self::ROUTE_PATH],
1823 $arguments
1824 );
1825 }
1826
1827 private static function mailin_disconnect($request) {
1828 $request = $request->get_params();
1829 $user_connection_id = isset($request['id']) ? $request['id'] : '';
1830 if (!empty($user_connection_id)) {
1831 $installationId = get_option( SIB_Manager::INSTALLATION_ID );
1832
1833 if ($user_connection_id == $installationId) {
1834 self::delete_connection();
1835 } else {
1836 return new WP_REST_Response(
1837 array(
1838 'message' => "user_connection_id not found"
1839 ), 404);
1840 }
1841 }
1842 }
1843
1844 private static function mailin_testconnection($request) {
1845 $request = $request->get_params();
1846 $uc_id = isset($request['id']) ? $request['id'] : '';
1847 if (empty($uc_id)) {
1848 return new WP_REST_Response(array('success' => false), 404);
1849 }
1850 $installationId = get_option(SIB_Manager::INSTALLATION_ID);
1851 if ($installationId !== $uc_id) {
1852 return new WP_REST_Response(array('success' => false), 403);
1853 }
1854 $haveApiKey = get_option(SIB_Manager::API_KEY_V3_OPTION_NAME, false);
1855 if ( false === $haveApiKey ) {
1856 return new WP_REST_Response(array('success' => false), 404);
1857 }
1858
1859 return new WP_REST_Response(array('success' => true), 200);
1860 }
1861
1862 private static function delete_connection()
1863 {
1864 $setting = array();
1865 update_option( self::MAIN_OPTION_NAME, $setting );
1866 delete_option(self::API_KEY_V3_OPTION_NAME);
1867
1868 $home_settings = array(
1869 'activate_email' => 'no',
1870 'activate_ma' => 'default',
1871 );
1872 update_option( self::HOME_OPTION_NAME, $home_settings );
1873
1874 // remove sync users option.
1875 delete_option( 'sib_sync_users' );
1876 // remove all transients.
1877 SIB_API_Manager::remove_transients();
1878
1879 // remove all forms.
1880 SIB_Forms::removeAllForms();
1881 SIB_Forms_Lang::remove_all_trans();
1882 delete_option(SIB_Manager::INSTALLATION_ID);
1883 }
1884 }
1885
1886 add_action( 'sendinblue_init', 'sendinblue_init' );
1887 add_filter( 'widget_text', 'do_shortcode' );
1888 add_action( 'save_post', array( 'SIB_Push_Admin', 'on_save_post' ), 1, 3 );
1889 add_action( 'transition_post_status', array( 'SIB_Push_Admin', 'on_transition_post_status' ), 10, 3 );
1890
1891
1892 /**
1893 * Plugin entry point Process.
1894 */
1895 function sendinblue_init() {
1896 SIB_Manager::LoadTextDomain();
1897 new SIB_Manager();
1898 }
1899
1900 do_action( 'sendinblue_init' );
1901 }
1902