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