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