PluginProbe ʕ •ᴥ•ʔ
Brevo – Email, SMS, Web Push, Chat, and more. / 3.1.63
Brevo – Email, SMS, Web Push, Chat, and more. v3.1.63
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 3 years ago form 4 years ago img 6 years ago inc 3 years ago js 3 years ago lang 3 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
1585 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.63
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_form_invalid_email_notice" value="<?php echo esc_attr($formData['invalidMsg']); ?>">
733 <input type="hidden" name="sib_security" value="<?php echo esc_attr( wp_create_nonce( 'sib_front_ajax_nonce' ) ); ?>">
734 <div class="sib_signup_box_inside_<?php echo esc_attr( $frmID ); ?>">
735 <div style="/*display:none*/" class="sib_msg_disp">
736 </div>
737 <?php
738 if (($formData['gCaptcha'] == '2') && false === strpos(
739 $formData['html'],
740 'id="sib_captcha_invisible"'
741 )) { ?>
742 <div id="sib_captcha_invisible" class="invi-recaptcha" data-sitekey="<?php
743 echo esc_attr($formData['gCaptcha_site']); ?>"></div>
744 <?php
745 } ?>
746 <?php
747 // phpcs:ignore
748
749 if (false === strpos($formData['html'], 'class="g-recaptcha"')) {
750 $formData['html'] = str_replace(
751 'id="sib_captcha"',
752 'id="sib_captcha" class="g-recaptcha" data-sitekey="' . $formData['gCaptcha_site'] . '"',
753 $formData['html']
754 );
755 }
756
757 echo wp_kses($formData['html'], SIB_Manager::wordpress_allowed_attributes());
758 ?>
759 </div>
760 </form>
761 <style>
762 <?php
763
764 if ( ! $formData['dependTheme'] ) {
765 // Custom css.
766 $formData['css'] = str_replace( '[form]', 'form#sib_signup_form_' . $frmID, $formData['css'] );
767 echo esc_html($formData['css']);
768 }
769 $msgCss = str_replace( '[form]', 'form#sib_signup_form_' . $frmID, SIB_Forms::getDefaultMessageCss() );
770 echo esc_html($msgCss);
771 ?>
772 </style>
773 <?php
774 }
775
776 /**
777 * Shortcode for sign up form
778 *
779 * @param array $atts - shortcode parameter.
780 * @return string
781 */
782 function sibwp_form_shortcode( $atts ) {
783 $pull_atts = shortcode_atts(
784 array(
785 'id' => 'oldForm', // We will return 'oldForm' for shortcode of old form.
786 ), $atts
787 );
788 $frmID = $pull_atts['id'];
789 $lang = defined( 'ICL_LANGUAGE_CODE' ) ? ICL_LANGUAGE_CODE : '';
790
791 ob_start();
792 $this->generate_form_box( $frmID, $lang );
793
794 $output_string = ob_get_contents();
795 ob_end_clean();
796 return $output_string;
797 }
798
799 /**
800 * Sign up process
801 */
802 function signup_process() {
803 //Handling of backslash added by WP because magic quotes are enabled by default
804 array_walk_recursive( $_POST, function(&$value) {
805 $value = stripslashes($value);
806 });
807
808 if ( empty( $_POST['sib_security'] ) ) {
809 wp_send_json(
810 array(
811 'status' => 'sib_security',
812 'msg' => 'Token not found.',
813 )
814 );
815 }
816 $formID = isset( $_POST['sib_form_id'] ) ? sanitize_text_field( $_POST['sib_form_id'] ) : 1;
817 if ( 'oldForm' == $formID ) {
818 $formID = get_option( 'sib_old_form_id' );
819 }
820 $formData = SIB_Forms::getForm( $formID );
821
822 if (!SIB_Manager::is_done_validation(false) || 0 == count($formData)) {
823 wp_send_json(
824 array(
825 'status' => 'failure',
826 'msg' => array("errorMsg" => "Something wrong occurred"),
827 )
828 );
829 }
830
831 if ( '0' != $formData['gCaptcha'] ) {
832 if ( ! isset( $_POST['g-recaptcha-response'] ) || empty( $_POST['g-recaptcha-response'] ) ) {
833 wp_send_json(
834 array(
835 'status' => 'gcaptchaEmpty',
836 'msg' => 'Please click on the reCAPTCHA box.',
837 )
838 );
839 }
840 $secret = $formData['gCaptcha_secret'];
841
842 $data = array(
843 'secret' => $secret,
844 'response' => sanitize_text_field( $_POST['g-recaptcha-response'] ),
845 );
846
847 $args = [
848 'method' => 'POST',
849 ];
850
851 try {
852 $data = wp_remote_retrieve_body(wp_remote_request(sprintf(self::RECAPTCHA_API_TEMPLATE, http_build_query($data)), $args));
853 $responseData = json_decode($data);
854 if ( ! $responseData->success ) {
855 wp_send_json(
856 array(
857 'status' => 'gcaptchaFail',
858 'msg' => 'Robot verification failed, please try again.',
859 )
860 );
861 }
862 } catch (Exception $exception) {
863 wp_send_json(
864 array(
865 'status' => 'gcaptchaFail',
866 'msg' => $exception->getMessage(),
867 )
868 );
869 }
870 }
871
872 $listID = $formData['listID'];
873 if (empty($listID)) {
874 $listID = array();
875 }
876 $interestingLists = isset( $_POST['interestingLists']) ? array_map( 'sanitize_text_field', $_POST['interestingLists'] ) : array();
877 $expectedLists = isset( $_POST['listIDs'] ) ? array_map( 'sanitize_text_field', $_POST['listIDs'] ) : array();
878 if ( empty($interestingLists) )
879 {
880 $unlinkedLists = [];
881 }
882 else{
883 $unwantedLists = array_diff( $interestingLists, $expectedLists );
884 $unlinkedLists = array_diff( $unwantedLists, $listID);
885 $listID = array_unique(array_merge( $listID, $expectedLists ));
886 }
887
888 $email = isset( $_POST['email'] ) ? sanitize_email( $_POST['email'] ) : '';
889 if ( ! is_email( $email ) ) {
890 return;
891 }
892
893 $isDoubleOptin = $formData['isDopt'];
894 $isOptin = $formData['isOpt'];
895 $redirectUrlInEmail = $formData['redirectInEmail'];
896 $redirectUrlInForm = $formData['redirectInForm'];
897
898 $info = array();
899 $attributes = explode( ',', $formData['attributes'] ); // String to array.
900 if ( isset( $attributes ) && is_array( $attributes ) ) {
901 foreach ( $_POST as $postAttribute => $postAttributeValue ) {
902 $correspondingSibAttribute = $this->getCorrespondingSibAttribute($postAttribute, $attributes);
903 if (!empty($correspondingSibAttribute)) {
904 $info[ $correspondingSibAttribute ] = sanitize_text_field( $postAttributeValue );
905 }
906 }
907 }
908 $templateID = $formData['templateID'];
909
910 if ( $isDoubleOptin ) {
911 /*
912 * Double optin process
913 * 1. add record to db
914 * 2. send confirmation email with activate code
915 */
916 $result = "success";
917 // Send a double optin confirm email.
918 if ( 'success' == $result ) {
919 // Add a recode with activate code in db.
920 $activateCode = $this->create_activate_code( $email, $info, $formID, $listID, $redirectUrlInEmail, $unlinkedLists );
921 SIB_API_Manager::send_comfirm_email( $email, 'double-optin', $templateID, $info, $activateCode );
922 }
923 } elseif ( $isOptin ) {
924 $result = SIB_API_Manager::create_subscriber( $email, $listID, $info, 'confirm', $unlinkedLists );
925 if ( 'success' == $result ) {
926 // Send a confirm email.
927 SIB_API_Manager::send_comfirm_email( $email, 'confirm', $templateID, $info );
928 }
929 } else {
930 $result = SIB_API_Manager::create_subscriber( $email, $listID, $info, 'simple', $unlinkedLists );
931 }
932 $msg = array(
933 'successMsg' => $formData['successMsg'],
934 'errorMsg' => $formData['errorMsg'],
935 'existMsg' => $formData['existMsg'],
936 'invalidMsg' => $formData['invalidMsg'],
937 );
938
939 wp_send_json(
940 array(
941 'status' => $result,
942 'msg' => $msg,
943 'redirect' => $redirectUrlInForm,
944 )
945 );
946 }
947
948 /**
949 * Create activate code for Double optin
950 *
951 * @param string $email - user email.
952 * @param array $info - info.
953 * @param string $formID - form ID.
954 * @param array $listIDs - lists.
955 * @param string $redirectUrl - redirect url.
956 * @return string - activate code.
957 */
958 function create_activate_code( $email, $info, $formID, $listIDs, $redirectUrl, $unlinkedLists = null ) {
959 $data = SIB_Model_Users::get_data_by_email( $email, $formID );
960 $date = gmdate( 'Y-m-d H:i:s' );
961 if ( $unlinkedLists != null )
962 {
963 $info['unlinkedLists'] = $unlinkedLists;
964 }
965 if ( false == $data ) {
966 $uniqid = uniqid();
967 $data = array(
968 'email' => $email,
969 'code' => $uniqid,
970 'info' => maybe_serialize( $info ),
971 'frmid' => $formID,
972 'listIDs' => maybe_serialize( $listIDs ),
973 'redirectUrl' => $redirectUrl,
974 'user_added_date' => $date,
975 );
976 SIB_Model_Users::add_record( $data );
977 } else {
978 $update_data = array(
979 'id' => $data['id'],
980 'email' => $email,
981 'info' => maybe_serialize( $info ),
982 );
983 SIB_Model_Users::update_element( $update_data );
984 $uniqid = $data['code'];
985 }
986 return $uniqid;
987 }
988
989 /**
990 * Use Sendinblue SMTP to send all emails
991 *
992 * @param string $to - reception email.
993 * @param string $subject - subject of email.
994 * @param string $message - message of email.
995 * @param string $headers - header of email.
996 * @param array $attachments - attachments.
997 */
998 static function wp_mail_native( $to, $subject, $message, $headers = '', $attachments = array() ) {
999 $result = require self::$plugin_dir . '/inc/function.wp_mail.php';
1000 return $result;
1001 }
1002
1003 /**
1004 * To send the transactional email via Sendinblue
1005 * hook wp_mail
1006 *
1007 * @param string $to - reception email.
1008 * @param string $subject - subject of email.
1009 * @param string $message - message of email.
1010 * @param string $headers - header of email.
1011 * @param array $attachments - attachments
1012 * @param array $tags - tag.
1013 * @param string $from_name - sender name.
1014 * @param string $from_email - sender email.
1015 * @return mixed|WP_Error
1016 */
1017 static function sib_email( $to, $subject, $message, $headers = '', $attachments = array(), $tags = array(), $from_name = '', $from_email = '' ) {
1018 $data = [];
1019 // Compact the input, apply the filters, and extract them back out.
1020 extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) );
1021
1022 if ( !empty( $attachments ) && ! is_array( $attachments ) ) {
1023 $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );
1024 }
1025
1026 // From email and name.
1027 $home_settings = get_option( SIB_Manager::HOME_OPTION_NAME );
1028 if ( isset( $home_settings['sender'] ) ) {
1029 $from_name = $home_settings['from_name'];
1030 $from_email = $home_settings['from_email'];
1031 } else {
1032 $from_email = trim( get_bloginfo( 'admin_email' ) );
1033 $from_name = trim( get_bloginfo( 'name' ) );
1034 }
1035
1036 //Set additional address fields as empty
1037 $bcc = array();
1038 $cc = array();
1039 $reply_to = array();
1040 if ( ! is_array( $to ) ) {
1041 $to = explode( ',', $to );
1042 }
1043
1044 $from_email = apply_filters( 'wp_mail_from', $from_email );
1045 $from_name = apply_filters( 'wp_mail_from_name', $from_name );
1046
1047 if ( !empty( $headers ) ) {
1048 if( is_array( $headers ) ){
1049 foreach ($headers as $key => $val) {
1050 if( stripos($val, "Content-Type: text/html") !== false ) {
1051 unset( $headers[$key] );
1052 }
1053 }
1054 $headers = array_values( $headers );
1055 if( count( $headers ) == 1 && $headers[0] == '' ) {
1056 unset( $headers[0] );
1057 }
1058 }
1059 if( is_string( $headers ) ){
1060 $headers = str_replace("Content-Type: text/html", "", $headers);
1061 }
1062 if( !empty( $headers ) ){
1063 $data['headers'] = $headers;
1064 }
1065 if ( ! is_array( $headers ) ) {
1066 // Explode the headers out, so this function can take both.
1067 // string headers and an array of headers.
1068 $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
1069 } else {
1070 $tempheaders = $headers;
1071 }
1072 $headers = array();
1073 // If it's actually got contents.
1074 if ( ! empty( $tempheaders ) ) {
1075 // Iterate through the raw headers.
1076 foreach ( (array) $tempheaders as $header ) {
1077 if ( strpos( $header, ':' ) === false ) {
1078 if ( false !== stripos( $header, 'boundary=' ) ) {
1079 $parts = preg_split( '/boundary=/i', trim( $header ) );
1080 $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
1081 }
1082 continue;
1083 }
1084 // Explode them out.
1085 list($name, $content) = explode( ':', trim( $header ), 2 );
1086
1087 // Cleanup crew.
1088 $name = trim( $name );
1089 $content = trim( $content );
1090
1091 switch ( strtolower( $name ) ) {
1092 case 'content-type':
1093 $headers[ trim( $name ) ] = trim( $content );
1094 break;
1095 case 'x-mailin-tag':
1096 $headers[ trim( $name ) ] = trim( $content );
1097 break;
1098 case 'from':
1099 if ( strpos( $content, '<' ) !== false ) {
1100 // So... making my life hard again?
1101 $from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
1102 $from_name = str_replace( '"', '', $from_name );
1103 $from_name = trim( $from_name );
1104
1105 $from_email = substr( $content, strpos( $content, '<' ) + 1 );
1106 $from_email = str_replace( '>', '', $from_email );
1107 $from_email = trim( $from_email );
1108 } else {
1109 $from_name = '';
1110 $from_email = trim( $content );
1111 }
1112 break;
1113
1114 case 'cc':
1115 $cc = array_merge( (array) $cc, explode( ',', $content ) );
1116 break;
1117
1118 case 'bcc':
1119 $bcc = array_merge( (array) $bcc, explode( ',', $content ) );
1120 break;
1121
1122 case 'reply-to':
1123 $reply_to = array_merge( (array) $reply_to, explode( ',', $content ) );
1124 break;
1125 default:
1126 break;
1127 }
1128 }
1129 }
1130 }
1131
1132 // Set destination addresses, using appropriate methods for handling addresses.
1133 $address_headers = compact('to', 'cc', 'bcc', 'reply_to');
1134 $processed_address_fields = self::processAddressFields($address_headers);
1135 $data = array_merge($data, $processed_address_fields);
1136 // Attachments.
1137 $attachment_content = array();
1138 if ( ! empty( $attachments ) ) {
1139 foreach ( $attachments as $attachment ) {
1140 if ( !empty( $attachment ) ) {
1141 $content = self::getAttachmentStruct( $attachment );
1142 if ( ! is_wp_error( $content ) ) {
1143 array_push( $attachment_content, $content );
1144 }
1145 }
1146 }
1147 if ( !empty( $attachment_content ) ) {
1148 $data["attachment"] = $attachment_content;
1149 }
1150 }
1151
1152 // Common transformations for the HTML part.
1153 // If it is text/plain, New line break found.
1154 if ( strpos( $message, '</table>' ) === false && strpos( $message, '</div>' ) === false ) {
1155 if ( strpos( $message, "\n" ) !== false ) {
1156 if ( is_array( $message ) ) {
1157 foreach ( $message as &$value ) {
1158 $value['content'] = preg_replace( '#<(https?://[^*]+)>#', '$1', $value['content'] );
1159 $value['content'] = nl2br( $value['content'] );
1160 }
1161 } else {
1162 $message = preg_replace( '#<(https?://[^*]+)>#', '$1', $message );
1163 $message = nl2br( $message );
1164 }
1165 }
1166 }
1167 // Sending...
1168 $data['sender'] = ['email' => $from_email, 'name' => $from_name ];
1169 $data['subject'] = $subject;
1170 $data['htmlContent'] = $message;
1171
1172 try {
1173 $sent = SIB_API_Manager::send_email( $data );
1174 return $sent;
1175 } catch ( Exception $e ) {
1176 return new WP_Error( $e->getMessage() );
1177 }
1178 }
1179
1180 /**
1181 * @param array $address_fields
1182 * @return array
1183 */
1184 private static function processAddressFields($address_fields)
1185 {
1186 $data = [
1187 'to' => [],
1188 'cc' => [],
1189 'bcc' => [],
1190 'replyTo' => [],
1191 ];
1192
1193 $address_fields['reply_to'] = is_array($address_fields['reply_to'])
1194 && count($address_fields['reply_to']) > 1 ? $address_fields['reply_to'][0] : $address_fields['reply_to'];
1195 foreach ($address_fields as $address_header => $addresses) {
1196 if (empty($addresses)) {
1197 continue;
1198 }
1199
1200 foreach ((array) $addresses as $address) {
1201 // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>".
1202 if (preg_match('/(.*)<(.+)>/', $address, $matches)) {
1203 if (count($matches) == 3) {
1204 $address = preg_replace('/\s+/', '', $matches[2]); //strip whitespaces
1205 }
1206 }
1207
1208 switch ($address_header) {
1209 case 'to':
1210 $data['to'][] = ['email' => $address];
1211 break;
1212 case 'cc':
1213 $data['cc'][] = ['email' => $address];
1214 break;
1215 case 'bcc':
1216 $data['bcc'][] = ['email' => $address];
1217 break;
1218 case 'reply_to':
1219 $data['replyTo']['email'] = $address;
1220 break;
1221 }
1222 }
1223 }
1224 return $data;
1225 }
1226
1227 /**
1228 * @param string $path - attachment file path
1229 * @return array|WP_Error
1230 */
1231 static function getAttachmentStruct( $path ) {
1232
1233 $struct = array();
1234
1235 try {
1236
1237 if ( ! @is_file( $path ) ) {
1238 throw new Exception( $path . ' is not a valid file.' );
1239 }
1240
1241 $filename = basename( $path );
1242
1243 if ( ! function_exists( 'get_magic_quotes' ) ) {
1244 /**
1245 * @return bool
1246 */
1247 function get_magic_quotes() {
1248 return false;
1249 }
1250 }
1251 if ( ! function_exists( 'set_magic_quotes' ) ) {
1252 /**
1253 * @param $value
1254 * @return bool
1255 */
1256 function set_magic_quotes( $value ) {
1257 return true;
1258 }
1259 }
1260
1261 $isMagicQuotesSupported = version_compare( PHP_VERSION, '5.3.0', '<' )
1262 && function_exists( 'get_magic_quotes_runtime' )
1263 && function_exists( 'set_magic_quotes_runtime' );
1264
1265 if ( $isMagicQuotesSupported ) {
1266 // Escape linters check.
1267 $getMagicQuotesRuntimeFunc = 'get_magic_quotes_runtime';
1268 $setMagicQuotesRuntimeFunc = 'set_magic_quotes_runtime';
1269
1270 // Save magic quotes value.
1271 $magicQuotes = $getMagicQuotesRuntimeFunc();
1272 $setMagicQuotesRuntimeFunc (0);
1273 }
1274
1275 $file_buffer = file_get_contents( $path );
1276 $file_buffer = base64_encode($file_buffer);
1277
1278 if ( $isMagicQuotesSupported ) {
1279 // Restore magic quotes value.
1280 $setMagicQuotesRuntimeFunc($magicQuotes);
1281 }
1282
1283 $struct["name"] = $filename;
1284 $struct["content"] = $file_buffer;
1285
1286 } catch ( Exception $e ) {
1287 return new WP_Error( 'Error creating the attachment structure: ' . $e->getMessage() );
1288 }
1289
1290 return $struct;
1291 }
1292
1293 /**
1294 * Create custom page for form preview
1295 *
1296 * @param array $query_vars - query.
1297 * @return array
1298 */
1299 function sib_query_vars( $query_vars ) {
1300 $query_vars[] = 'sib_form';
1301 return $query_vars;
1302 }
1303
1304 /**
1305 * Parse request
1306 *
1307 * @param mixed $wp - object.
1308 */
1309 function sib_parse_request( &$wp ) {
1310 if ( array_key_exists( 'sib_form', $wp->query_vars ) ) {
1311 include 'inc/sib-form-preview.php';
1312 exit();
1313 }
1314 }
1315
1316 /**
1317 * Load Text domain.
1318 */
1319 static function LoadTextDomain() {
1320 // Load lang file.
1321 $i18n_file_name = 'mailin';
1322 $locale = apply_filters( 'plugin_locale', get_locale(), $i18n_file_name );
1323 // $locale = 'fr_FR';
1324 $filename = plugin_dir_path( __FILE__ ) . '/lang/' . $i18n_file_name . '-' . $locale . '.mo';
1325 load_textdomain( 'mailin', $filename );
1326 }
1327
1328 /**
1329 * Notice the language is difference than site's language
1330 */
1331 static function language_admin_notice() {
1332 if ( ! get_option( SIB_Manager::LANGUAGE_OPTION_NAME ) ) {
1333 $lang_prefix = substr( get_bloginfo( 'language' ), 0, 2 );
1334 $lang = self::getLanguageName( $lang_prefix );
1335 $class = 'error';
1336 $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 );
1337 if ( 'en' !== $lang_prefix && 'fr' !== $lang_prefix ) {
1338 // phpcs:ignore
1339 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>" );
1340 }
1341 }
1342 }
1343
1344 /**
1345 * Notice wp_mail is not possible
1346 */
1347 static function wpMailNotices() {
1348 if ( self::$wp_mail_conflict ) {
1349 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>' );
1350 }
1351 }
1352
1353 /**
1354 * Names of languages.
1355 *
1356 * @param string $prefix - language.
1357 * @return mixed
1358 */
1359 public static function getLanguageName( $prefix = 'en' ) {
1360 $lang = array();
1361 $lang['de'] = 'Deutsch';
1362 $lang['en'] = 'English';
1363 $lang['zh'] = '中文';
1364 $lang['ru'] = 'Русский';
1365 $lang['fi'] = 'suomi';
1366 $lang['fr'] = 'Français';
1367 $lang['nl'] = 'Nederlands';
1368 $lang['sv'] = 'Svenska';
1369 $lang['it'] = 'Italiano';
1370 $lang['ro'] = 'Română';
1371 $lang['hu'] = 'Magyar';
1372 $lang['ja'] = '日本語';
1373 $lang['es'] = 'Español';
1374 $lang['vi'] = 'Tiếng Việt';
1375 $lang['ar'] = 'العربية';
1376 $lang['pt'] = 'Português';
1377 $lang['pb'] = 'Português do Brasil';
1378 $lang['pl'] = 'Polski';
1379 $lang['gl'] = 'galego';
1380 $lang['tr'] = 'Turkish';
1381 $lang['et'] = 'Eesti';
1382 $lang['hr'] = 'Hrvatski';
1383 $lang['eu'] = 'Euskera';
1384 $lang['el'] = 'Ελληνικά';
1385 $lang['ua'] = 'Українська';
1386 $lang['ko'] = '한국어';
1387
1388 return $lang[ $prefix ];
1389 }
1390
1391 /**
1392 * Create language sidebar for wpml plugin.
1393 */
1394 public function sib_create_language_sidebar() {
1395 $languages = apply_filters( 'wpml_active_languages', array() );
1396 $page = isset( $_GET['page'] ) ? sanitize_text_field( $_GET['page'] ) : '';
1397 $action = isset( $_GET['action'] ) ? sanitize_text_field( $_GET['action'] ) : '';
1398 $frmID = isset( $_GET['id'] ) ? sanitize_text_field( $_GET['id'] ) : '';
1399 $pID = isset( $_GET['pid'] ) ? sanitize_text_field( $_GET['pid'] ) : '';
1400 $parent = true;
1401 if ( '' !== $frmID && '' !== $pID ) {
1402 $lang = SIB_Forms_Lang::get_lang( $frmID, $pID );
1403 $parent = false;
1404 } else {
1405 $lang = ICL_LANGUAGE_CODE;
1406 if ( '' !== $frmID && '' === $pID ) {
1407 $pID = $frmID;
1408
1409 }
1410 }
1411
1412 if ( 'sib_page_form' === $page && 'edit' === $action ) {
1413 ?>
1414 <div class="panel panel-default text-left box-border-box sib-small-content">
1415 <div class="panel-heading"><strong><?php esc_attr_e( 'About Sendinblue', 'mailin' ); ?></strong></div>
1416 <div class="panel-body">
1417 <p>
1418 <label for='sib_form_language'><?php esc_attr_e( 'Language of this form:', 'mailin' ); ?> </label>
1419 <select id="sib_form_lang" name="sib_form_lang" data-selected="">
1420 <?php
1421 foreach ( $languages as $language ) {
1422 $selected = ($language['code'] == $lang) ? 'selected' : '';
1423 if ( $language['code'] == $lang && true === $parent ) {
1424 $option_text = '<option value="" ' . $selected . '>' . $language['native_name'] . '</option>';
1425 } else {
1426 $exist = SIB_Forms_Lang::get_form_ID( $pID, $language['language_code'] );
1427
1428 if ( null === $exist ) {
1429 continue;
1430 } else {
1431 $option_text = ( 'selected' === $selected ) ?
1432 sprintf( '<option value="" selected>%s</option>', esc_html( $language['native_name'] ) ) :
1433 sprintf( '<option value="%s" %s>%s</option>',
1434 esc_url( add_query_arg( array(
1435 'page' => sanitize_text_field( $_REQUEST['page'] ),
1436 'action' => 'edit',
1437 'pid' => absint( $pID ),
1438 'lang' => sanitize_text_field( $language['language_code'] )
1439 ) ) ),
1440 $selected,
1441 esc_html( $language['native_name'] )
1442 );
1443 }
1444 }
1445 echo $option_text ;
1446 }
1447 ?>
1448 </select>
1449 </p>
1450 <div class="sib_form_translate">
1451 <p>
1452 <label><?php esc_attr_e( 'Translate this form', 'mailin' ); ?></label>
1453 </p>
1454 <table aria-describedby="wpml-language-table" class="sib_form_trans_table" style="border: 1px solid #8cceea;">
1455 <tr>
1456 <?php
1457 foreach ( $languages as $language ) {
1458 if ( $language['code'] == $lang ) {
1459 continue;
1460 }
1461 ?>
1462 <th style="text-align: center;"><img
1463 src="<?php echo esc_url( $language['country_flag_url'] ); ?>" alt="Flag of <?php echo esc_attr( $language['country_name'] ); ?>"></th>
1464 <?php
1465 }
1466 ?>
1467 </tr>
1468 <tr style="background-color: #EFF8FC;">
1469 <?php
1470 foreach ( $languages as $language ) {
1471 if ( $language['code'] == $lang ) {
1472 continue;
1473 }
1474 if ( '' === $pID ) {
1475 $img_src = plugins_url( 'img/add_translation_disabled.png', __FILE__ );
1476 $td = '<img src="' . $img_src . '" style="margin:2px;">';
1477 } else {
1478 $exist = SIB_Forms_Lang::get_form_ID( $pID, $language['language_code'] );
1479
1480 if ( null === $exist ) {
1481 $img_src = plugins_url( 'img/add_translation.png', __FILE__ );
1482
1483 $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'] );
1484 $td = $href . '<img src="' . $img_src . '" style="margin:2px;"></a>';
1485 } else {
1486 $img_src = plugins_url( 'img/edit_translation.png', __FILE__ );
1487 $href = sprintf( '<a class="sib-form-redirect" href="%s" style="width: 20px; text-align: center;padding: 2px 1px;">', esc_url( add_query_arg( array(
1488 'page' => sanitize_text_field( $_REQUEST['page'] ),
1489 'action' => 'edit',
1490 'id' => absint( $exist ),
1491 'pid' => absint( $pID ),
1492 'lang' => sanitize_text_field( $language['language_code'] )
1493 ) ) ) );
1494 $td = $href . '<img src="' . $img_src . '" style="margin:2px;"></a>';
1495 }
1496 }
1497 ?>
1498 <td style="text-align: center;"><?php echo wp_kses($td, wp_kses_allowed_html()); ?></td>
1499 <?php
1500 }
1501 ?>
1502 </tr>
1503 </table>
1504 </div>
1505 <?php if ( isset( $_GET['pid'] ) ) { ?>
1506 <div class="sib-form-duplicate">
1507 <button class="btn btn-default sib-duplicate-btn"><?php esc_attr_e( 'Copy content from origin form', 'mailin' ); ?></button>
1508 <span class="sib-spin"><i
1509 class="fa fa-circle-o-notch fa-spin fa-lg"></i>&nbsp;&nbsp;</span>
1510 <i title="<?php echo esc_attr_e( 'Copy content from origin form', 'mailin' ); ?>"
1511 data-container="body" data-toggle="popover" data-placement="left"
1512 data-content="<?php echo esc_attr_e( 'You can copy contents from origin form. You need to translate the contents by this language.', 'mailin' ); ?>"
1513 data-html="true" class="fa fa-question-circle popover-help-form"></i>
1514 </div>
1515 <?php } ?>
1516 </div>
1517 </div>
1518 <?php
1519 }
1520 }
1521
1522 public function ajax_get_country_prefix() {
1523 check_ajax_referer( 'sib_front_ajax_nonce', 'security' );
1524 $sms_manager = new SIB_SMS_Code();
1525 $country_list = $sms_manager->get_sms_code_list();
1526 $country_list_html = '';
1527 foreach ( $country_list as $item => $value ) {
1528 $flg_url = plugins_url( 'img/flags/', __FILE__ ).strtolower($item).'.png';
1529 $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>';
1530 $country_list_html .= $item_html;
1531 }
1532 wp_send_json($country_list_html);
1533 }
1534
1535 /**
1536 * @param string $postAttribute
1537 * @param array $sibAttributes
1538 * @return null|string the corresponding sib attribute or null if not found
1539 */
1540 private function getCorrespondingSibAttribute($postAttribute, $sibAttributes)
1541 {
1542 $normalizedPostAttribute = strtoupper(sanitize_text_field($postAttribute));
1543 foreach ($sibAttributes as $sibAttribute) {
1544 if ($normalizedPostAttribute == strtoupper($sibAttribute)) {
1545 return $sibAttribute;
1546 }
1547 }
1548
1549 return null;
1550 }
1551
1552 public function my_upgrade_function() {
1553 $current_plugin_path_name = plugin_basename( __FILE__ );
1554 activate_plugin( $current_plugin_path_name );
1555 }
1556
1557 public static function wordpress_allowed_attributes()
1558 {
1559 global $allowedposttags, $allowedtags, $allowedentitynames;
1560 $attributes = [$allowedposttags, $allowedtags, $allowedentitynames, self::SIB_ATTRIBUTE];
1561 $attributes = call_user_func_array("array_merge", $attributes);
1562
1563 add_filter( 'safe_style_css', function($css_attr) {
1564 array_push($css_attr, 'display');
1565 return $css_attr;
1566 });
1567
1568 return $attributes;
1569 }
1570 }
1571
1572 add_action( 'sendinblue_init', 'sendinblue_init' );
1573 add_filter( 'widget_text', 'do_shortcode' );
1574
1575 /**
1576 * Plugin entry point Process.
1577 */
1578 function sendinblue_init() {
1579 SIB_Manager::LoadTextDomain();
1580 new SIB_Manager();
1581 }
1582
1583 do_action( 'sendinblue_init' );
1584 }
1585