PluginProbe ʕ •ᴥ•ʔ
Brevo – Email, SMS, Web Push, Chat, and more. / 3.1.55
Brevo – Email, SMS, Web Push, Chat, and more. v3.1.55
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 4 years ago form 4 years ago img 6 years ago inc 3 years ago js 3 years ago lang 4 years ago model 3 years ago page 3 years ago scripts 4 years ago widget 4 years ago index.php 11 years ago readme.txt 3 years ago screenshot-1.png 5 years ago screenshot-2.png 5 years ago screenshot-3.png 5 years ago screenshot-4.png 5 years ago screenshot-5.png 5 years ago screenshot-6.png 5 years ago screenshot-7.png 5 years ago sendinblue.php 3 years ago
sendinblue.php
1557 lines
1 <?php
2 /**
3 * Plugin Name: Newsletter, SMTP, Email marketing and Subscribe forms by Sendinblue
4 * Plugin URI: https://www.sendinblue.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.1.55
7 * Author: Sendinblue
8 * Author URI: https://www.sendinblue.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( 'SIB_Manager' ) ) {
46 register_deactivation_hook( __FILE__, array( 'SIB_Manager', 'deactivate' ) );
47 register_activation_hook( __FILE__, array( 'SIB_Manager', 'install' ) );
48 register_uninstall_hook( __FILE__, array( 'SIB_Manager', 'uninstall' ) );
49
50 require_once( 'page/page-home.php' );
51 require_once( 'page/page-form.php' );
52 require_once( 'page/page-statistics.php' );
53 require_once( 'page/page-scenarios.php' );
54 require_once( 'widget/widget_form.php' );
55 require_once( 'inc/table-forms.php' );
56 require_once( 'inc/sib-api-manager.php' );
57 require_once( 'inc/sib-sms-code.php' );
58 require_once( 'model/model-forms.php' );
59 require_once( 'model/model-users.php' );
60 require_once( 'model/model-lang.php' );
61 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
62 require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
63 /**
64 * Class SIB_Manager
65 */
66 class SIB_Manager {
67
68 /** Main setting option name */
69 const MAIN_OPTION_NAME = 'sib_main_option';
70
71 /** Home setting option name */
72 const HOME_OPTION_NAME = 'sib_home_option';
73
74 /** Access token option name */
75 const ACCESS_TOKEN_OPTION_NAME = 'sib_token_store';
76
77 /** Plugin language notice option name */
78 const LANGUAGE_OPTION_NAME = 'sib_language_notice_option';
79
80 /** Form preview option name */
81 const PREVIEW_OPTION_NAME = 'sib_preview_form';
82
83 const API_KEY_V3_OPTION_NAME = 'sib_api_key_v3';
84
85 const RECAPTCHA_API_TEMPLATE = 'https://www.google.com/recaptcha/api/siteverify?%s';
86
87 /** Installation id option name */
88 const INSTALLATION_ID = 'sib_installation_id';
89
90 const SIB_ATTRIBUTE = array(
91 'input' => array(
92 'type' => true,
93 'name' => true,
94 'value' => true,
95 'class' => true,
96 'id' => true,
97 'size' => true,
98 'min' => true,
99 'max' => true,
100 'pattern' => true,
101 'title' => true,
102 'placeholder' => true,
103 'required' => true,
104 ),
105 'p' => array(
106 'align' => true,
107 'id' => true,
108 'class' => true,
109 'dir' => true,
110 'lang' => true,
111 'style' => true,
112 'xml:lang' => true,
113 ),
114 'iframe' => array(
115 'name' => true,
116 'id' => true,
117 'class' => true,
118 'src' => true,
119 'width' => true,
120 'height' => true,
121 'style' => true,
122 'loading' => true,
123 'allow' => true,
124 'allowfullscreen' => true,
125 ),
126 'div' => array(
127 'id' => true,
128 'class' => true,
129 'dir' => true,
130 'lang' => true,
131 'style' => true,
132 'xml:lang' => true,
133 'data-require' => true,
134 'data-sitekey' => true,
135 ),
136 'a' => array(
137 'href' => true,
138 'id' => true,
139 'class' => true,
140 'rel' => true,
141 'rev' => true,
142 'name' => true,
143 'target' => true,
144 ),
145 'style' => array(),
146 'script' => array(
147 'src' => true,
148 ),
149 'link' => array(
150 'rel' => true,
151 'href' => true,
152 'type' => true,
153 ),
154 'select' => array(
155 'name' => true,
156 'class' => true,
157 'id' => true,
158 'style' => true,
159 'required' => true,
160 ),
161 'option' => array(
162 'value' => true,
163 ),
164 'ul' => array(
165 'class' => true,
166 'style' => true,
167 ),
168 'center' => array(),
169 'download' => array(
170 'valueless' => 'y',
171 )
172 );
173
174 /**
175 * API key
176 *
177 * @var $access_key
178 */
179 public static $access_key;
180
181 /**
182 * Store instance
183 *
184 * @var $instance
185 */
186 public static $instance;
187
188 /**
189 * Plugin directory path value. set in constructor
190 *
191 * @var $plugin_dir
192 */
193 public static $plugin_dir;
194
195 /**
196 * Plugin url. set in constructor
197 *
198 * @var $plugin_url
199 */
200 public static $plugin_url;
201
202 /**
203 * Plugin name. set in constructor
204 *
205 * @var $plugin_name
206 */
207 public static $plugin_name;
208
209 /**
210 * Check if wp_mail is declared
211 *
212 * @var $wp_mail_conflict
213 */
214 static $wp_mail_conflict;
215
216 /**
217 * Class constructor
218 * Sets plugin url and directory and adds hooks to <i>init</i>. <i>admin_menu</i>
219 */
220 function __construct() {
221 // get basic info.
222 self::$plugin_dir = plugin_dir_path( __FILE__ );
223 self::$plugin_url = plugins_url( '', __FILE__ );
224 self::$plugin_name = plugin_basename( __FILE__ );
225
226 self::$wp_mail_conflict = false;
227
228 // api key for sendinblue.
229 $general_settings = get_option( self::MAIN_OPTION_NAME, array() );
230 self::$access_key = isset( $general_settings['access_key'] ) ? $general_settings['access_key'] : '';
231
232 self::$instance = $this;
233 add_action( 'upgrader_process_complete', array( &$this, 'my_upgrade_function' ), 10, 2);
234 add_action( 'admin_init', array( &$this, 'admin_init' ), 9999 );
235 add_action( 'admin_menu', array( &$this, 'admin_menu' ), 9999 );
236
237 add_action( 'wp_print_scripts', array( &$this, 'frontend_register_scripts' ), 9999 );
238 add_action( 'wp_enqueue_scripts', array( &$this, 'wp_head_ac' ), 999 );
239
240 // create custom url for form preview.
241 add_filter( 'query_vars', array( &$this, 'sib_query_vars' ) );
242 add_action( 'parse_request', array( &$this, 'sib_parse_request' ) );
243
244 add_action( 'wp_ajax_sib_validate_process', array( 'SIB_Page_Home', 'ajax_validation_process' ) );
245 add_action( 'wp_ajax_sib_validate_ma', array( 'SIB_Page_Home', 'ajax_validate_ma' ) );
246 add_action( 'wp_ajax_sib_activate_email_change', array( 'SIB_Page_Home', 'ajax_activate_email_change' ) );
247 add_action( 'wp_ajax_sib_sender_change', array( 'SIB_Page_Home', 'ajax_sender_change' ) );
248 add_action( 'wp_ajax_sib_send_email', array( 'SIB_Page_Home', 'ajax_send_email' ) );
249 add_action( 'wp_ajax_sib_remove_cache', array( 'SIB_Page_Home', 'ajax_remove_cache' ) );
250 add_action( 'wp_ajax_sib_sync_users', array( 'SIB_Page_Home', 'ajax_sync_users' ) );
251
252 add_action( 'wp_ajax_sib_change_template', array( 'SIB_Page_Form', 'ajax_change_template' ) );
253 add_action( 'wp_ajax_sib_get_lists', array( 'SIB_Page_Form', 'ajax_get_lists' ) );
254 add_action( 'wp_ajax_sib_get_templates', array( 'SIB_Page_Form', 'ajax_get_templates' ) );
255 add_action( 'wp_ajax_sib_get_attributes', array( 'SIB_Page_Form', 'ajax_get_attributes' ) );
256 add_action( 'wp_ajax_sib_update_form_html', array( 'SIB_Page_Form', 'ajax_update_html' ) );
257 add_action( 'wp_ajax_sib_copy_origin_form', array( 'SIB_Page_Form', 'ajax_copy_origin_form' ) );
258
259 add_action( 'wp_ajax_sib_get_country_prefix', array( $this, 'ajax_get_country_prefix' ) );
260 add_action( 'wp_ajax_nopriv_sib_get_country_prefix', array( $this, 'ajax_get_country_prefix' ) );
261
262 add_action( 'init', array( &$this, 'init' ) );
263
264 add_action( 'wp_login', array( &$this, 'sib_wp_login_identify' ), 10, 2 );
265
266 // change sib tables name on prior(2.6.9) versions.
267 SIB_Model_Users::add_prefix();
268 SIB_Forms::add_prefix();
269 SIB_Forms::modify_datatype();
270
271 if ( self::is_api_key_set() ) {
272 SIB_Manager::install_service_worker_script();
273 add_shortcode( 'sibwp_form', array( &$this, 'sibwp_form_shortcode' ) );
274 // register widget.
275 add_action( 'widgets_init', array( &$this, 'sib_create_widget' ) );
276
277 // create forms tables and create default form.
278 SIB_Forms::createTable();
279 // create users table.
280 SIB_Model_Users::createTable();
281 // add columns for old versions
282 SIB_Forms::alterTable();
283 SIB_Model_Users::add_user_added_date_column();
284 }
285
286 $use_api_version = get_option( 'sib_use_apiv2', '0' );
287 if ( '0' === $use_api_version ) {
288 self::uninstall();
289 update_option( 'sib_use_apiv2', '1' );
290 }
291
292 // Wpml plugin part.
293 if ( ! function_exists( 'is_plugin_active_for_network' ) ) :
294 require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
295 endif;
296 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' ) ) {
297 SIB_Forms_Lang::createTable();
298 add_action( 'sib_language_sidebar', array( $this, 'sib_create_language_sidebar' ) );
299 }
300
301 /**
302 * Hook wp_mail to send transactional emails
303 */
304
305 // check if wp_mail function is already declared by others.
306 if ( function_exists( 'wp_mail' ) ) {
307 self::$wp_mail_conflict = true;
308 }
309 $home_settings = get_option( SIB_Manager::HOME_OPTION_NAME, array() );
310
311 if( 'yes' === $home_settings['activate_email'] )
312 {
313 if ( false === self::$wp_mail_conflict ) {
314 /**
315 * Declare wp_mail function for Sendinblue SMTP module
316 *
317 * @param string $to - receiption email.
318 * @param string $subject - subject of email.
319 * @param string $message - message content.
320 * @param string $headers - header of email.
321 * @param array $attachments - attachments.
322 * @return bool
323 */
324 function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
325 $message = str_replace( 'NF_SIB', '', $message );
326 $message = str_replace( 'WC_SIB', '', $message );
327 try {
328 $sent = SIB_Manager::sib_email( $to, $subject, $message, $headers, $attachments );
329 if ( is_wp_error( $sent ) || ! isset( $sent['code'] ) || 'success' !== $sent['code'] ) {
330 try{
331 return true;
332 }catch( Exception $e ){
333 return false;
334 }
335 }
336 return true;
337 } catch ( Exception $e ) {
338 return false;
339 }
340 }
341 } else {
342 add_action( 'admin_notices', array( &$this, 'wpMailNotices' ) );
343 return;
344 }
345 }
346 }
347
348 /**
349 * Add identify tag for login users
350 *
351 * @param string $user_login - user login name.
352 * @param array $user - user.
353 */
354 function sib_wp_login_identify( $user_login, $user ) {
355
356 $userEmail = $user->user_email;
357 $data = array(
358 'email_id' => $userEmail,
359 'name' => $user_login,
360 );
361 SIB_API_Manager::identify_user( $data );
362 }
363
364 /**
365 * Initialize method. called on <i>init</i> action
366 */
367 function init() {
368 // Sign up process.
369 if ( isset( $_POST['sib_form_action'] ) && ( 'subscribe_form_submit' == sanitize_text_field($_POST['sib_form_action']) ) ) {
370 $this->signup_process();
371 }
372 // Subscribe.
373 if ( isset( $_GET['sib_action'] ) && ( 'subscribe' == sanitize_text_field($_GET['sib_action']) ) ) {
374 $code = isset( $_GET['code'] ) ? sanitize_text_field( $_GET['code'] ) : '';
375 $contact_info = SIB_Model_Users::get_data_by_code( $code );
376 $user_added_date = $contact_info['user_added_date'];
377 $current_date = gmdate( 'Y-m-d H:i:s' );
378 $date_diff = strtotime( $current_date ) - strtotime( $user_added_date );
379 if ( $date_diff > 5 ) {
380 SIB_API_Manager::subscribe( $contact_info );
381 } else {
382 $type = 'Bot Event';
383 SIB_API_Manager::template_subscribe( $type );
384 }
385 exit;
386 }
387 // Dismiss language notice.
388 if ( isset( $_GET['dismiss_admin_lang_notice'] ) && '1' == sanitize_text_field($_GET['dismiss_admin_lang_notice']) ) {
389 update_option( SIB_Manager::LANGUAGE_OPTION_NAME, true );
390 wp_safe_redirect( $_SERVER['HTTP_REFERER'] );
391 exit();
392 }
393
394 add_action( 'wp_head', array( &$this, 'install_ma_script' ) );
395 }
396
397 /**
398 * Hook admin_init
399 */
400 function admin_init() {
401 add_action( 'admin_action_sib_setting_subscription', array( 'SIB_Page_Form', 'save_setting_subscription' ) );
402 add_action( 'admin_action_nopriv_sib_setting_subscription', array( 'SIB_Page_Form', 'save_setting_subscription' ) );
403 SIB_Manager::LoadTextDomain();
404 $this->register_scripts();
405 $this->register_styles();
406 }
407
408 /**
409 * Hook admin_menu
410 */
411 function admin_menu() {
412 SIB_Manager::LoadTextDomain();
413 new SIB_Page_Home();
414 new SIB_Page_Form();
415 new SIB_Page_Statistics();
416 $home_settings = get_option( SIB_Manager::HOME_OPTION_NAME );
417 if ( isset( $home_settings['activate_ma'] ) && 'yes' == $home_settings['activate_ma'] ) {
418 new SIB_Page_Scenarios();
419 }
420
421 }
422
423 /**
424 * Register script for admin page
425 */
426 function register_scripts() {
427 wp_register_script( 'sib-bootstrap-js', self::$plugin_url . '/js/bootstrap/js/bootstrap.bundle.min.js', array( 'jquery' ), false );
428 wp_register_script( 'sib-admin-js', self::$plugin_url . '/js/admin.js', array( 'jquery' ), filemtime( self::$plugin_dir . '/js/admin.js' ) );
429 wp_register_script( 'sib-chosen-js', self::$plugin_url . '/js/chosen.jquery.min.js', array( 'jquery' ), false );
430 wp_enqueue_script('jquery-ui-datepicker');
431 wp_enqueue_script('jquery-ui-spinner');
432 }
433
434 /**
435 * Register stylesheet for admin page
436 */
437 function register_styles() {
438 wp_register_style( 'sib-bootstrap-css', self::$plugin_url . '/js/bootstrap/css/bootstrap.css', array(), false, 'all' );
439 wp_register_style( 'sib-fontawesome-css', self::$plugin_url . '/css/fontawesome/css/font-awesome.css', array(), false, 'all' );
440 wp_register_style( 'sib-chosen-css', self::$plugin_url . '/css/chosen.min.css' );
441 wp_register_style( 'sib-admin-css', self::$plugin_url . '/css/admin.css', array(), filemtime( self::$plugin_dir . '/css/admin.css' ), 'all' );
442 }
443
444 /**
445 * Registers scripts for frontend
446 */
447 function frontend_register_scripts() {
448
449 }
450
451 /**
452 * Enqueue script on front page
453 */
454 function wp_head_ac() {
455 wp_enqueue_script( 'sib-front-js', self::$plugin_url . '/js/mailin-front.js', array( 'jquery' ), filemtime( self::$plugin_dir . '/js/mailin-front.js' ), false );
456 wp_enqueue_style( 'sib-front-css', self::$plugin_url.'/css/mailin-front.css', array(), array(), 'all');
457 wp_localize_script(
458 'sib-front-js', 'sibErrMsg', array(
459 'invalidMail' => __( 'Please fill out valid email address', 'mailin' ),
460 'requiredField' => __( 'Please fill out required fields', 'mailin' ),
461 'invalidDateFormat' => __( 'Please fill out valid date format', 'mailin' ),
462 'invalidSMSFormat' => __( 'Please fill out valid phone number', 'mailin' ),
463 )
464 );
465 wp_localize_script(
466 'sib-front-js', 'ajax_sib_front_object',
467 array(
468 'ajax_url' => admin_url( 'admin-ajax.php' ),
469 'ajax_nonce' => wp_create_nonce( 'sib_front_ajax_nonce' ),
470 'flag_url' => plugins_url('img/flags/', __FILE__ ),
471 )
472 );
473 }
474
475 /**
476 * Install method is called once install this plugin.
477 * create tables, default option ...
478 */
479 static function install() {
480 $general_settings = get_option( self::MAIN_OPTION_NAME, array() );
481 $access_key = isset( $general_settings['access_key'] ) ? $general_settings['access_key'] : '';
482 if ( '' === $access_key ) {
483 // Default option when activate.
484 $home_settings = array(
485 'activate_email' => 'no',
486 'activate_ma' => 'no',
487 );
488 update_option( self::HOME_OPTION_NAME, $home_settings );
489 }
490 }
491
492 /**
493 * Uninstall method is called once uninstall this plugin
494 * delete tables, options that used in plugin
495 */
496 static function uninstall() {
497 $setting = array();
498 update_option( SIB_Manager::MAIN_OPTION_NAME, $setting );
499
500 $home_settings = array(
501 'activate_email' => 'no',
502 'activate_ma' => 'no',
503 );
504 update_option( SIB_Manager::HOME_OPTION_NAME, $home_settings );
505
506 // Delete access_token.
507 $token_settings = array();
508 update_option( SIB_Manager::ACCESS_TOKEN_OPTION_NAME, $token_settings );
509 delete_option(SIB_Manager::API_KEY_V3_OPTION_NAME);
510 // Empty tables.
511 SIB_Model_Users::removeTable();
512 SIB_Forms::removeTable();
513 SIB_Forms_Lang::removeTable();
514
515 // Remove all transient.
516 SIB_API_Manager::remove_transients();
517 }
518
519 /**
520 * Deactivate method is called once deactivate this plugin
521 */
522 static function deactivate() {
523 update_option( SIB_Manager::LANGUAGE_OPTION_NAME, false );
524 // Remove sync users option.
525 delete_option( 'sib_sync_users' );
526 // Remove all transient.
527 SIB_API_Manager::remove_transients();
528 }
529
530 /**
531 * Check if plugin is logged in.
532 *
533 * @param bool $redirect
534 * @return bool
535 */
536 static function is_done_validation($redirect = true) {
537 if (self::is_api_key_set()) {
538 $apiClient = new SendinblueApiClient();
539 $apiClient->getAccount();
540 if ( SendinblueApiClient::RESPONSE_CODE_OK === $apiClient->getLastResponseCode() ) {
541 return true;
542 } elseif (SendinblueApiClient::RESPONSE_CODE_UNAUTHORIZED === $apiClient->getLastResponseCode()) {
543 delete_option(SIB_Manager::API_KEY_V3_OPTION_NAME);
544 }
545 }
546
547 if ($redirect) {
548 self::redirect_to_sib_plugin_homepage();
549 }
550
551 return false;
552 }
553
554 static function redirect_to_sib_plugin_homepage() {
555 wp_safe_redirect(add_query_arg('page', SIB_Page_Home::PAGE_ID, admin_url('admin.php')));
556 }
557
558 /**
559 * @return bool
560 */
561 static function is_api_key_set() {
562 $api_key = get_option(SIB_Manager::API_KEY_V3_OPTION_NAME);
563 return !empty($api_key);
564 }
565
566 /**
567 * Install service-worker script in plugin for push notifications
568 * @return void
569 */
570 static function install_service_worker_script()
571 {
572 try {
573 $service_worker = __DIR__ . "/js/service-worker.js";
574 if (file_exists($service_worker)){
575 return;
576 }
577 $site_url = get_site_url();
578 $service_worker_file = strpos($site_url, 'staging') !== false
579 ? __DIR__ . '/scripts/service-worker-staging.php'
580 : __DIR__ . '/scripts/service-worker-prod.php';
581 $js_content = file_get_contents($service_worker_file);
582 $service_worker_script = fopen($service_worker, "w");
583 fwrite($service_worker_script, $js_content);
584 fclose($service_worker_script);
585 } catch(\Throwable $th){
586 update_option( 'sib_service_worker_install_exception', $th->getMessage());
587 }
588 }
589
590 /**
591 * Uninstall service-worker script from plugin
592 * @return void
593 */
594 static function uninstall_service_worker_script()
595 {
596 try {
597 $service_worker_file = __DIR__ . "/js/service-worker.js";
598 if (file_exists($service_worker_file)){
599 unlink($service_worker_file);
600 }
601 update_option( 'sib_service_worker_install_exception', '');
602 } catch(\Throwable $th){
603 update_option( 'sib_service_worker_uninstall_exception', $th->getMessage());
604 }
605 }
606
607 /**
608 * Install marketing automation script in header
609 */
610 function install_ma_script() {
611 $home_settings = get_option( SIB_Manager::HOME_OPTION_NAME, array() );
612 if ( isset( $home_settings['activate_ma'] ) && 'yes' == $home_settings['activate_ma'] ) {
613 $general_settings = get_option( SIB_Manager::MAIN_OPTION_NAME, array() );
614 $ma_email = '';
615 $current_user = wp_get_current_user();
616 if ( $current_user instanceof WP_User ) {
617 $ma_email = $current_user->user_email;
618 }
619 $ma_key = sanitize_text_field($general_settings['ma_key']);
620 $output = '<script type="text/javascript">
621 (function() {
622 window.sib ={equeue:[],client_key:"'. $ma_key .'"};/* OPTIONAL: email for identify request*/
623 window.sib.email_id = "'. sanitize_email($ma_email) .'";
624 window.sendinblue = {}; for (var j = [\'track\', \'identify\', \'trackLink\', \'page\'], i = 0; i < j.length; i++) { (function(k) { window.sendinblue[k] = function() { var arg = Array.prototype.slice.call(arguments); (window.sib[k] || function() { var t = {}; t[k] = arg; window.sib.equeue.push(t);})(arg[0], arg[1], arg[2]);};})(j[i]);}var n = document.createElement("script"),i = document.getElementsByTagName("script")[0]; n.type = "text/javascript", n.id = "sendinblue-js", n.async = !0, n.src = "https://sibautomation.com/sa.js?plugin=wordpress&key=" + window.sib.client_key, i.parentNode.insertBefore(n, i), window.sendinblue.page();})();
625 </script>';
626 echo html_entity_decode($output);
627 }
628 }
629
630 /**
631 * Register widget
632 */
633 function sib_create_widget() {
634 register_widget( 'SIB_Widget_Subscribe' );
635 }
636
637 /**
638 * Display form on front page
639 *
640 * @param string $frmID - form ID.
641 * @param string $lang - form language.
642 */
643 function generate_form_box( $frmID = '-1', $lang = '' ) {
644 if ( 'oldForm' == $frmID ) {
645 $frmID = get_option( 'sib_old_form_id' );
646 } elseif ( '' != $lang ) {
647 $trans_id = SIB_Forms_Lang::get_form_ID( $frmID, $lang );
648 if ( null != $trans_id ) {
649 $frmID = $trans_id;
650 }
651 }
652
653 $formData = SIB_Forms::getForm( $frmID );
654
655 if ( empty( $formData ) ) {
656 return;
657 }
658 // Add Google recaptcha
659 if( '0' != $formData['gCaptcha'] ) {
660 if( '1' == $formData['gCaptcha'] ) { // For old forms.
661 $formData['html'] = preg_replace( '/([\s\S]*?)<div class="g-recaptcha"[\s\S]*?data-size="invisible"><\/div>/', '$1', $formData['html'] );
662 }
663 if ( '3' == $formData['gCaptcha'] ) // The case of using google recaptcha.
664 {
665 ?>
666 <script type="text/javascript">
667 var onloadSibCallback = function () {
668 jQuery('.g-recaptcha').each(function (index, el) {
669 grecaptcha.render(el, {
670 'sitekey': jQuery(el).attr('data-sitekey')
671 });
672 });
673 };
674 </script>
675 <?php
676 } else { // The case of using google invisible recaptcha.
677 $formData['html'] = str_replace(
678 'class="sib-default-btn"',
679 'class="sib-default-btn" id="invisible"',
680 $formData['html']
681 );
682 ?>
683 <script type="text/javascript">
684 var gCaptchaSibWidget;
685 var onloadSibCallbackInvisible = function () {
686
687 var element = document.getElementsByClassName('sib-default-btn');
688 var countInvisible = 0;
689 var indexArray = [];
690 jQuery('.sib-default-btn').each(function (index, el) {
691 if ((jQuery(el).attr('id') == "invisible")) {
692 indexArray[countInvisible] = index;
693 countInvisible++
694 }
695 });
696
697 jQuery('.invi-recaptcha').each(function (index, el) {
698 grecaptcha.render(element[indexArray[index]], {
699 'sitekey': jQuery(el).attr('data-sitekey'),
700 'callback': sibVerifyCallback,
701 });
702 });
703 };
704 </script>
705 <?php
706 }
707 ?>
708 <script src="https://www.google.com/recaptcha/api.js?onload=<?php
709 echo esc_attr(
710 $formData['gCaptcha'] == '2' ? 'onloadSibCallbackInvisible' : 'onloadSibCallback'
711 ) ?>&render=explicit" async defer></script>
712 <?php
713 }
714
715 ?>
716 <form id="sib_signup_form_<?php echo esc_attr( $frmID ); ?>" method="post" class="sib_signup_form">
717 <div class="sib_loader" style="display:none;"><img
718 src="<?php echo esc_url( includes_url() ); ?>images/spinner.gif" alt="loader"></div>
719 <input type="hidden" name="sib_form_action" value="subscribe_form_submit">
720 <input type="hidden" name="sib_form_id" value="<?php echo esc_attr( $frmID ); ?>">
721 <input type="hidden" name="sib_form_alert_notice" value="<?php echo esc_attr($formData['requiredMsg']); ?>">
722 <input type="hidden" name="sib_security" value="<?php echo esc_attr( wp_create_nonce( 'sib_front_ajax_nonce' ) ); ?>">
723 <div class="sib_signup_box_inside_<?php echo esc_attr( $frmID ); ?>">
724 <div style="/*display:none*/" class="sib_msg_disp">
725 </div>
726 <?php
727 if (($formData['gCaptcha'] == '2') && false === strpos(
728 $formData['html'],
729 'id="sib_captcha_invisible"'
730 )) { ?>
731 <div id="sib_captcha_invisible" class="invi-recaptcha" data-sitekey="<?php
732 echo esc_attr($formData['gCaptcha_site']); ?>"></div>
733 <?php
734 } ?>
735 <?php
736 // phpcs:ignore
737
738 if (false === strpos($formData['html'], 'class="g-recaptcha"')) {
739 $formData['html'] = str_replace(
740 'id="sib_captcha"',
741 'id="sib_captcha" class="g-recaptcha" data-sitekey="' . $formData['gCaptcha_site'] . '"',
742 $formData['html']
743 );
744 }
745
746 echo wp_kses($formData['html'], SIB_Manager::wordpress_allowed_attributes());
747 ?>
748 </div>
749 </form>
750 <style>
751 <?php
752
753 if ( ! $formData['dependTheme'] ) {
754 // Custom css.
755 $formData['css'] = str_replace( '[form]', 'form#sib_signup_form_' . $frmID, $formData['css'] );
756 echo esc_html($formData['css']);
757 }
758 $msgCss = str_replace( '[form]', 'form#sib_signup_form_' . $frmID, SIB_Forms::getDefaultMessageCss() );
759 echo esc_html($msgCss);
760 ?>
761 </style>
762 <?php
763 }
764
765 /**
766 * Shortcode for sign up form
767 *
768 * @param array $atts - shortcode parameter.
769 * @return string
770 */
771 function sibwp_form_shortcode( $atts ) {
772 $pull_atts = shortcode_atts(
773 array(
774 'id' => 'oldForm', // We will return 'oldForm' for shortcode of old form.
775 ), $atts
776 );
777 $frmID = $pull_atts['id'];
778 $lang = defined( 'ICL_LANGUAGE_CODE' ) ? ICL_LANGUAGE_CODE : '';
779
780 ob_start();
781 $this->generate_form_box( $frmID, $lang );
782
783 $output_string = ob_get_contents();
784 ob_end_clean();
785 return $output_string;
786 }
787
788 /**
789 * Sign up process
790 */
791 function signup_process() {
792 //Handling of backslash added by WP because magic quotes are enabled by default
793 array_walk_recursive( $_POST, function(&$value) {
794 $value = stripslashes($value);
795 });
796
797 if ( empty( $_POST['sib_security'] ) ) {
798 wp_send_json(
799 array(
800 'status' => 'sib_security',
801 'msg' => 'Token not found.',
802 )
803 );
804 }
805 $formID = isset( $_POST['sib_form_id'] ) ? sanitize_text_field( $_POST['sib_form_id'] ) : 1;
806 if ( 'oldForm' == $formID ) {
807 $formID = get_option( 'sib_old_form_id' );
808 }
809 $formData = SIB_Forms::getForm( $formID );
810
811 if (!SIB_Manager::is_done_validation(false) || 0 == count($formData)) {
812 wp_send_json(
813 array(
814 'status' => 'failure',
815 'msg' => array("errorMsg" => "Something wrong occurred"),
816 )
817 );
818 }
819
820 if ( '0' != $formData['gCaptcha'] ) {
821 if ( ! isset( $_POST['g-recaptcha-response'] ) || empty( $_POST['g-recaptcha-response'] ) ) {
822 wp_send_json(
823 array(
824 'status' => 'gcaptchaEmpty',
825 'msg' => 'Please click on the reCAPTCHA box.',
826 )
827 );
828 }
829 $secret = $formData['gCaptcha_secret'];
830
831 $data = array(
832 'secret' => $secret,
833 'response' => sanitize_text_field( $_POST['g-recaptcha-response'] ),
834 );
835
836 $args = [
837 'method' => 'POST',
838 ];
839
840 try {
841 $data = wp_remote_retrieve_body(wp_remote_request(sprintf(self::RECAPTCHA_API_TEMPLATE, http_build_query($data)), $args));
842 $responseData = json_decode($data);
843 if ( ! $responseData->success ) {
844 wp_send_json(
845 array(
846 'status' => 'gcaptchaFail',
847 'msg' => 'Robot verification failed, please try again.',
848 )
849 );
850 }
851 } catch (Exception $exception) {
852 wp_send_json(
853 array(
854 'status' => 'gcaptchaFail',
855 'msg' => $exception->getMessage(),
856 )
857 );
858 }
859 }
860
861 $listID = $formData['listID'];
862 if (empty($listID)) {
863 $listID = array();
864 }
865 $interestingLists = isset( $_POST['interestingLists']) ? array_map( 'sanitize_text_field', $_POST['interestingLists'] ) : array();
866 $expectedLists = isset( $_POST['listIDs'] ) ? array_map( 'sanitize_text_field', $_POST['listIDs'] ) : array();
867 if ( empty($interestingLists) )
868 {
869 $unlinkedLists = [];
870 }
871 else{
872 $unwantedLists = array_diff( $interestingLists, $expectedLists );
873 $unlinkedLists = array_diff( $unwantedLists, $listID);
874 $listID = array_unique(array_merge( $listID, $expectedLists ));
875 }
876
877 $email = isset( $_POST['email'] ) ? sanitize_email( $_POST['email'] ) : '';
878 if ( ! is_email( $email ) ) {
879 return;
880 }
881
882 $isDoubleOptin = $formData['isDopt'];
883 $isOptin = $formData['isOpt'];
884 $redirectUrlInEmail = $formData['redirectInEmail'];
885 $redirectUrlInForm = $formData['redirectInForm'];
886
887 $info = array();
888 $attributes = explode( ',', $formData['attributes'] ); // String to array.
889 if ( isset( $attributes ) && is_array( $attributes ) ) {
890 foreach ( $_POST as $postAttribute => $postAttributeValue ) {
891 $correspondingSibAttribute = $this->getCorrespondingSibAttribute($postAttribute, $attributes);
892 if (!empty($correspondingSibAttribute)) {
893 $info[ $correspondingSibAttribute ] = sanitize_text_field( $postAttributeValue );
894 }
895 }
896 }
897 $templateID = $formData['templateID'];
898
899 if ( $isDoubleOptin ) {
900 /*
901 * Double optin process
902 * 1. add record to db
903 * 2. send confirmation email with activate code
904 */
905 $result = "success";
906 // Send a double optin confirm email.
907 if ( 'success' == $result ) {
908 // Add a recode with activate code in db.
909 $activateCode = $this->create_activate_code( $email, $info, $formID, $listID, $redirectUrlInEmail, $unlinkedLists );
910 SIB_API_Manager::send_comfirm_email( $email, 'double-optin', $templateID, $info, $activateCode );
911 }
912 } elseif ( $isOptin ) {
913 $result = SIB_API_Manager::create_subscriber( $email, $listID, $info, 'confirm', $unlinkedLists );
914 if ( 'success' == $result ) {
915 // Send a confirm email.
916 SIB_API_Manager::send_comfirm_email( $email, 'confirm', $templateID, $info );
917 }
918 } else {
919 $result = SIB_API_Manager::create_subscriber( $email, $listID, $info, 'simple', $unlinkedLists );
920 }
921 $msg = array(
922 'successMsg' => $formData['successMsg'],
923 'errorMsg' => $formData['errorMsg'],
924 'existMsg' => $formData['existMsg'],
925 'invalidMsg' => $formData['invalidMsg'],
926 );
927
928 wp_send_json(
929 array(
930 'status' => $result,
931 'msg' => $msg,
932 'redirect' => $redirectUrlInForm,
933 )
934 );
935 }
936
937 /**
938 * Create activate code for Double optin
939 *
940 * @param string $email - user email.
941 * @param array $info - info.
942 * @param string $formID - form ID.
943 * @param array $listIDs - lists.
944 * @param string $redirectUrl - redirect url.
945 * @return string - activate code.
946 */
947 function create_activate_code( $email, $info, $formID, $listIDs, $redirectUrl, $unlinkedLists = null ) {
948 $data = SIB_Model_Users::get_data_by_email( $email, $formID );
949 $date = gmdate( 'Y-m-d H:i:s' );
950 if ( $unlinkedLists != null )
951 {
952 $info['unlinkedLists'] = $unlinkedLists;
953 }
954 if ( false == $data ) {
955 $uniqid = uniqid();
956 $data = array(
957 'email' => $email,
958 'code' => $uniqid,
959 'info' => maybe_serialize( $info ),
960 'frmid' => $formID,
961 'listIDs' => maybe_serialize( $listIDs ),
962 'redirectUrl' => $redirectUrl,
963 'user_added_date' => $date,
964 );
965 SIB_Model_Users::add_record( $data );
966 } else {
967 $update_data = array(
968 'id' => $data['id'],
969 'email' => $email,
970 'info' => maybe_serialize( $info ),
971 );
972 SIB_Model_Users::update_element( $update_data );
973 $uniqid = $data['code'];
974 }
975 return $uniqid;
976 }
977
978 /**
979 * Use Sendinblue SMTP to send all emails
980 *
981 * @param string $to - reception email.
982 * @param string $subject - subject of email.
983 * @param string $message - message of email.
984 * @param string $headers - header of email.
985 * @param array $attachments - attachments.
986 */
987 static function wp_mail_native( $to, $subject, $message, $headers = '', $attachments = array() ) {
988 $result = require self::$plugin_dir . '/inc/function.wp_mail.php';
989 return $result;
990 }
991
992 /**
993 * To send the transactional email via Sendinblue
994 * hook wp_mail
995 *
996 * @param string $to - reception email.
997 * @param string $subject - subject of email.
998 * @param string $message - message of email.
999 * @param string $headers - header of email.
1000 * @param array $attachments - attachments
1001 * @param array $tags - tag.
1002 * @param string $from_name - sender name.
1003 * @param string $from_email - sender email.
1004 * @return mixed|WP_Error
1005 */
1006 static function sib_email( $to, $subject, $message, $headers = '', $attachments = array(), $tags = array(), $from_name = '', $from_email = '' ) {
1007 $data = [];
1008 // Compact the input, apply the filters, and extract them back out.
1009 extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) );
1010
1011 if ( !empty( $attachments ) && ! is_array( $attachments ) ) {
1012 $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );
1013 }
1014
1015 // From email and name.
1016 $home_settings = get_option( SIB_Manager::HOME_OPTION_NAME );
1017 if ( isset( $home_settings['sender'] ) ) {
1018 $from_name = $home_settings['from_name'];
1019 $from_email = $home_settings['from_email'];
1020 } else {
1021 $from_email = trim( get_bloginfo( 'admin_email' ) );
1022 $from_name = trim( get_bloginfo( 'name' ) );
1023 }
1024
1025 //Set additional address fields as empty
1026 $bcc = array();
1027 $cc = array();
1028 $reply_to = array();
1029 if ( ! is_array( $to ) ) {
1030 $to = explode( ',', $to );
1031 }
1032
1033 $from_email = apply_filters( 'wp_mail_from', $from_email );
1034 $from_name = apply_filters( 'wp_mail_from_name', $from_name );
1035
1036 if ( !empty( $headers ) ) {
1037 if( is_array( $headers ) ){
1038 foreach ($headers as $key => $val) {
1039 if( stripos($val, "Content-Type: text/html") !== false ) {
1040 unset( $headers[$key] );
1041 }
1042 }
1043 $headers = array_values( $headers );
1044 if( count( $headers ) == 1 && $headers[0] == '' ) {
1045 unset( $headers[0] );
1046 }
1047 }
1048 if( is_string( $headers ) ){
1049 $headers = str_replace("Content-Type: text/html", "", $headers);
1050 }
1051 if( !empty( $headers ) ){
1052 $data['headers'] = $headers;
1053 }
1054 if ( ! is_array( $headers ) ) {
1055 // Explode the headers out, so this function can take both.
1056 // string headers and an array of headers.
1057 $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
1058 } else {
1059 $tempheaders = $headers;
1060 }
1061 $headers = array();
1062 // If it's actually got contents.
1063 if ( ! empty( $tempheaders ) ) {
1064 // Iterate through the raw headers.
1065 foreach ( (array) $tempheaders as $header ) {
1066 if ( strpos( $header, ':' ) === false ) {
1067 if ( false !== stripos( $header, 'boundary=' ) ) {
1068 $parts = preg_split( '/boundary=/i', trim( $header ) );
1069 $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
1070 }
1071 continue;
1072 }
1073 // Explode them out.
1074 list($name, $content) = explode( ':', trim( $header ), 2 );
1075
1076 // Cleanup crew.
1077 $name = trim( $name );
1078 $content = trim( $content );
1079
1080 switch ( strtolower( $name ) ) {
1081 case 'content-type':
1082 $headers[ trim( $name ) ] = trim( $content );
1083 break;
1084 case 'x-mailin-tag':
1085 $headers[ trim( $name ) ] = trim( $content );
1086 break;
1087 case 'from':
1088 if ( strpos( $content, '<' ) !== false ) {
1089 // So... making my life hard again?
1090 $from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
1091 $from_name = str_replace( '"', '', $from_name );
1092 $from_name = trim( $from_name );
1093
1094 $from_email = substr( $content, strpos( $content, '<' ) + 1 );
1095 $from_email = str_replace( '>', '', $from_email );
1096 $from_email = trim( $from_email );
1097 } else {
1098 $from_name = '';
1099 $from_email = trim( $content );
1100 }
1101 break;
1102
1103 case 'cc':
1104 $cc = array_merge( (array) $cc, explode( ',', $content ) );
1105 break;
1106
1107 case 'bcc':
1108 $bcc = array_merge( (array) $bcc, explode( ',', $content ) );
1109 break;
1110
1111 case 'reply-to':
1112 $reply_to = array_merge( (array) $reply_to, explode( ',', $content ) );
1113 break;
1114 default:
1115 break;
1116 }
1117 }
1118 }
1119 }
1120
1121 // Set destination addresses, using appropriate methods for handling addresses.
1122 $address_headers = compact('to', 'cc', 'bcc', 'reply_to');
1123 $processed_address_fields = self::processAddressFields($address_headers);
1124 $data = array_merge($data, $processed_address_fields);
1125 // Attachments.
1126 $attachment_content = array();
1127 if ( ! empty( $attachments ) ) {
1128 foreach ( $attachments as $attachment ) {
1129 if ( !empty( $attachment ) ) {
1130 $content = self::getAttachmentStruct( $attachment );
1131 if ( ! is_wp_error( $content ) ) {
1132 $attachment_content = array_merge( $attachment_content, $content );
1133 }
1134 }
1135 }
1136 if ( !empty( $attachment_content ) ) {
1137 $data["attachment"] = array($attachment_content);
1138 }
1139 }
1140
1141 // Common transformations for the HTML part.
1142 // If it is text/plain, New line break found.
1143 if ( strpos( $message, '</table>' ) === false && strpos( $message, '</div>' ) === false ) {
1144 if ( strpos( $message, "\n" ) !== false ) {
1145 if ( is_array( $message ) ) {
1146 foreach ( $message as &$value ) {
1147 $value['content'] = preg_replace( '#<(https?://[^*]+)>#', '$1', $value['content'] );
1148 $value['content'] = nl2br( $value['content'] );
1149 }
1150 } else {
1151 $message = preg_replace( '#<(https?://[^*]+)>#', '$1', $message );
1152 $message = nl2br( $message );
1153 }
1154 }
1155 }
1156 // Sending...
1157 $data['sender'] = ['email' => $from_email, 'name' => $from_name ];
1158 $data['subject'] = $subject;
1159 $data['htmlContent'] = $message;
1160
1161 try {
1162 $sent = SIB_API_Manager::send_email( $data );
1163 return $sent;
1164 } catch ( Exception $e ) {
1165 return new WP_Error( $e->getMessage() );
1166 }
1167 }
1168
1169 /**
1170 * @param array $address_fields
1171 * @return array
1172 */
1173 private static function processAddressFields($address_fields)
1174 {
1175 $data = [
1176 'to' => [],
1177 'cc' => [],
1178 'bcc' => [],
1179 'replyTo' => [],
1180 ];
1181
1182 $address_fields['reply_to'] = is_array($address_fields['reply_to'])
1183 && count($address_fields['reply_to']) > 1 ? $address_fields['reply_to'][0] : $address_fields['reply_to'];
1184 foreach ($address_fields as $address_header => $addresses) {
1185 if (empty($addresses)) {
1186 continue;
1187 }
1188
1189 foreach ((array) $addresses as $address) {
1190 // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>".
1191 if (preg_match('/(.*)<(.+)>/', $address, $matches)) {
1192 if (count($matches) == 3) {
1193 $address = preg_replace('/\s+/', '', $matches[2]); //strip whitespaces
1194 }
1195 }
1196
1197 switch ($address_header) {
1198 case 'to':
1199 $data['to'][] = ['email' => $address];
1200 break;
1201 case 'cc':
1202 $data['cc'][] = ['email' => $address];
1203 break;
1204 case 'bcc':
1205 $data['bcc'][] = ['email' => $address];
1206 break;
1207 case 'reply_to':
1208 $data['replyTo']['email'] = $address;
1209 break;
1210 }
1211 }
1212 }
1213 return $data;
1214 }
1215
1216 /**
1217 * @param string $path - attachment file path
1218 * @return array|WP_Error
1219 */
1220 static function getAttachmentStruct( $path ) {
1221
1222 $struct = array();
1223
1224 try {
1225
1226 if ( ! @is_file( $path ) ) {
1227 throw new Exception( $path . ' is not a valid file.' );
1228 }
1229
1230 $filename = basename( $path );
1231
1232 if ( ! function_exists( 'get_magic_quotes' ) ) {
1233 /**
1234 * @return bool
1235 */
1236 function get_magic_quotes() {
1237 return false;
1238 }
1239 }
1240 if ( ! function_exists( 'set_magic_quotes' ) ) {
1241 /**
1242 * @param $value
1243 * @return bool
1244 */
1245 function set_magic_quotes( $value ) {
1246 return true;
1247 }
1248 }
1249
1250 $isMagicQuotesSupported = version_compare( PHP_VERSION, '5.3.0', '<' )
1251 && function_exists( 'get_magic_quotes_runtime' )
1252 && function_exists( 'set_magic_quotes_runtime' );
1253
1254 if ( $isMagicQuotesSupported ) {
1255 // Escape linters check.
1256 $getMagicQuotesRuntimeFunc = 'get_magic_quotes_runtime';
1257 $setMagicQuotesRuntimeFunc = 'set_magic_quotes_runtime';
1258
1259 // Save magic quotes value.
1260 $magicQuotes = $getMagicQuotesRuntimeFunc();
1261 $setMagicQuotesRuntimeFunc (0);
1262 }
1263
1264 $file_buffer = file_get_contents( $path );
1265 $file_buffer = chunk_split( base64_encode( $file_buffer ), 76, "\n" );
1266
1267 if ( $isMagicQuotesSupported ) {
1268 // Restore magic quotes value.
1269 $setMagicQuotesRuntimeFunc($magicQuotes);
1270 }
1271
1272 $struct["name"] = $filename;
1273 $struct["content"] = $file_buffer;
1274
1275 } catch ( Exception $e ) {
1276 return new WP_Error( 'Error creating the attachment structure: ' . $e->getMessage() );
1277 }
1278
1279 return $struct;
1280 }
1281
1282 /**
1283 * Create custom page for form preview
1284 *
1285 * @param array $query_vars - query.
1286 * @return array
1287 */
1288 function sib_query_vars( $query_vars ) {
1289 $query_vars[] = 'sib_form';
1290 return $query_vars;
1291 }
1292
1293 /**
1294 * Parse request
1295 *
1296 * @param mixed $wp - object.
1297 */
1298 function sib_parse_request( &$wp ) {
1299 if ( array_key_exists( 'sib_form', $wp->query_vars ) ) {
1300 include 'inc/sib-form-preview.php';
1301 exit();
1302 }
1303 }
1304
1305 /**
1306 * Load Text domain.
1307 */
1308 static function LoadTextDomain() {
1309 // Load lang file.
1310 $i18n_file_name = 'mailin';
1311 $locale = apply_filters( 'plugin_locale', get_locale(), $i18n_file_name );
1312 // $locale = 'fr_FR';
1313 $filename = plugin_dir_path( __FILE__ ) . '/lang/' . $i18n_file_name . '-' . $locale . '.mo';
1314 load_textdomain( 'mailin', $filename );
1315 }
1316
1317 /**
1318 * Notice the language is difference than site's language
1319 */
1320 static function language_admin_notice() {
1321 if ( ! get_option( SIB_Manager::LANGUAGE_OPTION_NAME ) ) {
1322 $lang_prefix = substr( get_bloginfo( 'language' ), 0, 2 );
1323 $lang = self::getLanguageName( $lang_prefix );
1324 $class = 'error';
1325 $message = sprintf( 'Please note that your Sendinblue account is in %s, but Sendinblue WordPress plugin is only available in English / French for now. Sorry for inconvenience.', $lang );
1326 if ( 'en' !== $lang_prefix && 'fr' !== $lang_prefix ) {
1327 // phpcs:ignore
1328 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>" );
1329 }
1330 }
1331 }
1332
1333 /**
1334 * Notice wp_mail is not possible
1335 */
1336 static function wpMailNotices() {
1337 if ( self::$wp_mail_conflict ) {
1338 echo ( '<div class="error"><p>' . __( 'You cannot use Sendinblue SMTP now because wp_mail has been declared by another process or plugin. ', 'mailin' ) . '</p></div>' );
1339 }
1340 }
1341
1342 /**
1343 * Names of languages.
1344 *
1345 * @param string $prefix - language.
1346 * @return mixed
1347 */
1348 public static function getLanguageName( $prefix = 'en' ) {
1349 $lang = array();
1350 $lang['de'] = 'Deutsch';
1351 $lang['en'] = 'English';
1352 $lang['zh'] = '中文';
1353 $lang['ru'] = 'Русский';
1354 $lang['fi'] = 'suomi';
1355 $lang['fr'] = 'Français';
1356 $lang['nl'] = 'Nederlands';
1357 $lang['sv'] = 'Svenska';
1358 $lang['it'] = 'Italiano';
1359 $lang['ro'] = 'Română';
1360 $lang['hu'] = 'Magyar';
1361 $lang['ja'] = '日本語';
1362 $lang['es'] = 'Español';
1363 $lang['vi'] = 'Tiếng Việt';
1364 $lang['ar'] = 'العربية';
1365 $lang['pt'] = 'Português';
1366 $lang['pb'] = 'Português do Brasil';
1367 $lang['pl'] = 'Polski';
1368 $lang['gl'] = 'galego';
1369 $lang['tr'] = 'Turkish';
1370 $lang['et'] = 'Eesti';
1371 $lang['hr'] = 'Hrvatski';
1372 $lang['eu'] = 'Euskera';
1373 $lang['el'] = 'Ελληνικά';
1374 $lang['ua'] = 'Українська';
1375 $lang['ko'] = '한국어';
1376
1377 return $lang[ $prefix ];
1378 }
1379
1380 /**
1381 * Create language sidebar for wpml plugin.
1382 */
1383 public function sib_create_language_sidebar() {
1384 $languages = apply_filters( 'wpml_active_languages', array() );
1385 $page = isset( $_GET['page'] ) ? sanitize_text_field( $_GET['page'] ) : '';
1386 $action = isset( $_GET['action'] ) ? sanitize_text_field( $_GET['action'] ) : '';
1387 $frmID = isset( $_GET['id'] ) ? sanitize_text_field( $_GET['id'] ) : '';
1388 $pID = isset( $_GET['pid'] ) ? sanitize_text_field( $_GET['pid'] ) : '';
1389 $parent = true;
1390 if ( '' !== $frmID && '' !== $pID ) {
1391 $lang = SIB_Forms_Lang::get_lang( $frmID, $pID );
1392 $parent = false;
1393 } else {
1394 $lang = ICL_LANGUAGE_CODE;
1395 if ( '' !== $frmID && '' === $pID ) {
1396 $pID = $frmID;
1397
1398 }
1399 }
1400
1401 if ( 'sib_page_form' === $page && 'edit' === $action ) {
1402 ?>
1403 <div class="panel panel-default text-left box-border-box sib-small-content">
1404 <div class="panel-heading"><strong><?php esc_attr_e( 'About Sendinblue', 'mailin' ); ?></strong></div>
1405 <div class="panel-body">
1406 <p>
1407 <label for='sib_form_language'><?php esc_attr_e( 'Language of this form:', 'mailin' ); ?> </label>
1408 <select id="sib_form_lang" name="sib_form_lang" data-selected="">
1409 <?php
1410 foreach ( $languages as $language ) {
1411 $selected = ($language['code'] == $lang) ? 'selected' : '';
1412 if ( $language['code'] == $lang && true === $parent ) {
1413 $option_text = '<option value="" ' . $selected . '>' . $language['native_name'] . '</option>';
1414 } else {
1415 $exist = SIB_Forms_Lang::get_form_ID( $pID, $language['language_code'] );
1416
1417 if ( null === $exist ) {
1418 continue;
1419 } else {
1420 $option_text = ( 'selected' === $selected ) ? sprintf( '<option value="" selected>%s</option>', $language['native_name'] ) : sprintf( '<option value="?page=%s&action=%s&pid=%s&lang=%s" %s >%s</option>', sanitize_text_field( $_REQUEST['page'] ), 'edit', absint( $pID ), $language['language_code'], $selected, $language['native_name'] );
1421 }
1422 }
1423 echo $option_text ;
1424 }
1425 ?>
1426 </select>
1427 </p>
1428 <div class="sib_form_translate">
1429 <p>
1430 <label><?php esc_attr_e( 'Translate this form', 'mailin' ); ?></label>
1431 </p>
1432 <table width="100%" class="sib_form_trans_table" style="border: 1px solid #8cceea;">
1433 <tr>
1434 <?php
1435 foreach ( $languages as $language ) {
1436 if ( $language['code'] == $lang ) {
1437 continue;
1438 }
1439 ?>
1440 <th style="text-align: center;"><img
1441 src="<?php echo esc_url( $language['country_flag_url'] ); ?>"></th>
1442 <?php
1443 }
1444 ?>
1445 </tr>
1446 <tr style="background-color: #EFF8FC;">
1447 <?php
1448 foreach ( $languages as $language ) {
1449 if ( $language['code'] == $lang ) {
1450 continue;
1451 }
1452 if ( '' === $pID ) {
1453 $img_src = plugins_url( 'img/add_translation_disabled.png', __FILE__ );
1454 $td = '<img src="' . $img_src . '" style="margin:2px;">';
1455 } else {
1456 $exist = SIB_Forms_Lang::get_form_ID( $pID, $language['language_code'] );
1457
1458 if ( null === $exist ) {
1459 $img_src = plugins_url( 'img/add_translation.png', __FILE__ );
1460
1461 $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'] );
1462 $td = $href . '<img src="' . $img_src . '" style="margin:2px;"></a>';
1463 } else {
1464 $img_src = plugins_url( 'img/edit_translation.png', __FILE__ );
1465 $href = sprintf( '<a class="sib-form-redirect" href="?page=%s&action=%s&id=%s&pid=%s&lang=%s" style="width: 20px; text-align: center;padding: 2px 1px;">', sanitize_text_field( $_REQUEST['page'] ), 'edit', absint( $exist ), absint( $pID ), $language['language_code'] );
1466 $td = $href . '<img src="' . $img_src . '" style="margin:2px;"></a>';
1467 }
1468 }
1469 ?>
1470 <td style="text-align: center;"><?php echo wp_kses($td, wp_kses_allowed_html()); ?></td>
1471 <?php
1472 }
1473 ?>
1474 </tr>
1475 </table>
1476 </div>
1477 <?php if ( isset( $_GET['pid'] ) ) { ?>
1478 <div class="sib-form-duplicate">
1479 <button class="btn btn-default sib-duplicate-btn"><?php esc_attr_e( 'Copy content from origin form', 'mailin' ); ?></button>
1480 <span class="sib-spin"><i
1481 class="fa fa-circle-o-notch fa-spin fa-lg"></i>&nbsp;&nbsp;</span>
1482 <i title="<?php echo esc_attr_e( 'Copy content from origin form', 'mailin' ); ?>"
1483 data-container="body" data-toggle="popover" data-placement="left"
1484 data-content="<?php echo esc_attr_e( 'You can copy contents from origin form. You need to translate the contents by this language.', 'mailin' ); ?>"
1485 data-html="true" class="fa fa-question-circle popover-help-form"></i>
1486 </div>
1487 <?php } ?>
1488 </div>
1489 </div>
1490 <?php
1491 }
1492 }
1493
1494 public function ajax_get_country_prefix() {
1495 check_ajax_referer( 'sib_front_ajax_nonce', 'security' );
1496 $sms_manager = new SIB_SMS_Code();
1497 $country_list = $sms_manager->get_sms_code_list();
1498 $country_list_html = '';
1499 foreach ( $country_list as $item => $value ) {
1500 $flg_url = plugins_url( 'img/flags/', __FILE__ ).strtolower($item).'.png';
1501 $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>';
1502 $country_list_html .= $item_html;
1503 }
1504 wp_send_json($country_list_html);
1505 }
1506
1507 /**
1508 * @param string $postAttribute
1509 * @param array $sibAttributes
1510 * @return null|string the corresponding sib attribute or null if not found
1511 */
1512 private function getCorrespondingSibAttribute($postAttribute, $sibAttributes)
1513 {
1514 $normalizedPostAttribute = strtoupper(sanitize_text_field($postAttribute));
1515 foreach ($sibAttributes as $sibAttribute) {
1516 if ($normalizedPostAttribute == strtoupper($sibAttribute)) {
1517 return $sibAttribute;
1518 }
1519 }
1520
1521 return null;
1522 }
1523
1524 public function my_upgrade_function() {
1525 $current_plugin_path_name = plugin_basename( __FILE__ );
1526 activate_plugin( $current_plugin_path_name );
1527 }
1528
1529 public static function wordpress_allowed_attributes()
1530 {
1531 global $allowedposttags, $allowedtags, $allowedentitynames;
1532 $attributes = [$allowedposttags, $allowedtags, $allowedentitynames, self::SIB_ATTRIBUTE];
1533 $attributes = call_user_func_array("array_merge", $attributes);
1534
1535 add_filter( 'safe_style_css', function($css_attr) {
1536 array_push($css_attr, 'display');
1537 return $css_attr;
1538 });
1539
1540 return $attributes;
1541 }
1542 }
1543
1544 add_action( 'sendinblue_init', 'sendinblue_init' );
1545 add_filter( 'widget_text', 'do_shortcode' );
1546
1547 /**
1548 * Plugin entry point Process.
1549 */
1550 function sendinblue_init() {
1551 SIB_Manager::LoadTextDomain();
1552 new SIB_Manager();
1553 }
1554
1555 do_action( 'sendinblue_init' );
1556 }
1557