· 10 years ago · Jul 25, 2016, 08:30 PM
1<?php
2/*
3 * Plugin Name: Bloom
4 * Plugin URI: http://www.elegantthemes.com/plugins/bloom/
5 * Version: 1.1.7
6 * Description: A simple, comprehensive and beautifully constructed email opt-in plugin built to help you quickly grow your mailing list.
7 * Author: Elegant Themes
8 * Author URI: http://www.elegantthemes.com
9 * License: GPLv2 or later
10 */
11
12if ( ! defined( 'ABSPATH' ) ) {
13 exit; // Exit if accessed directly
14}
15
16define( 'ET_BLOOM_PLUGIN_DIR', trailingslashit( dirname(__FILE__) ) );
17define( 'ET_BLOOM_PLUGIN_URI', plugins_url('', __FILE__) );
18
19if ( ! class_exists( 'ET_Dashboard' ) ) {
20 require_once( ET_BLOOM_PLUGIN_DIR . 'dashboard/dashboard.php' );
21}
22
23class ET_Bloom extends ET_Dashboard {
24 var $plugin_version = '1.1.7';
25 var $db_version = '1.0';
26 var $_options_pagename = 'et_bloom_options';
27 var $menu_page;
28 var $protocol;
29
30 private static $_this;
31
32 public static $scripts_enqueued = false;
33
34 function __construct() {
35 // Don't allow more than one instance of the class
36 if ( isset( self::$_this ) ) {
37 wp_die( sprintf( esc_html__( '%s is a singleton class and you cannot create a second instance.', 'bloom' ),
38 get_class( $this ) )
39 );
40 }
41
42 self::$_this = $this;
43
44 $this->protocol = is_ssl() ? 'https' : 'http';
45
46 add_action( 'admin_menu', array( $this, 'add_menu_link' ) );
47
48 add_action( 'plugins_loaded', array( $this, 'add_localization' ), 1 );
49
50 add_filter( 'et_bloom_import_sub_array', array( $this, 'import_settings' ) );
51 add_filter( 'et_bloom_import_array', array( $this, 'import_filter' ) );
52 add_filter( 'et_bloom_export_exclude', array( $this, 'filter_export_settings' ) );
53 add_filter( 'et_bloom_save_button_class', array( $this, 'save_btn_class' ) );
54
55 // generate home tab in dashboard
56 add_action( 'et_bloom_after_header_options', array( $this, 'generate_home_tab' ) );
57
58 add_action( 'et_bloom_after_main_options', array( $this, 'generate_premade_templates' ) );
59
60 add_action( 'et_bloom_after_save_button', array( $this, 'add_next_button') );
61
62 $plugin_file = plugin_basename( __FILE__ );
63 add_filter( "plugin_action_links_{$plugin_file}", array( $this, 'add_settings_link' ) );
64
65 // construct dashboard at the plugins_loaded hook, to make sure localization applied correctly.
66 add_action( 'plugins_loaded', array( $this, 'construct_dashboard' ), 99 );
67
68 // Register save settings function for ajax request
69 add_action( 'wp_ajax_et_bloom_save_settings', array( $this, 'bloom_save_settings' ) );
70
71 add_action( 'admin_enqueue_scripts', array( $this, 'register_scripts' ), 99 );
72
73 add_action( 'wp_enqueue_scripts', array( $this, 'register_frontend_scripts' ) );
74
75 add_action( 'wp_ajax_reset_options_page', array( $this, 'reset_options_page' ) );
76
77 add_action( 'wp_ajax_bloom_remove_optin', array( $this, 'remove_optin' ) );
78
79 add_action( 'wp_ajax_bloom_duplicate_optin', array( $this, 'duplicate_optin' ) );
80
81 add_action( 'wp_ajax_bloom_add_variant', array( $this, 'add_variant' ) );
82
83 add_action( 'wp_ajax_bloom_home_tab_tables', array( $this, 'home_tab_tables' ) );
84
85 add_action( 'wp_ajax_bloom_toggle_optin_status', array( $this, 'toggle_optin_status' ) );
86
87 add_action( 'wp_ajax_bloom_authorize_account', array( $this, 'authorize_account' ) );
88
89 add_action( 'wp_ajax_bloom_reset_accounts_table', array( $this, 'reset_accounts_table' ) );
90
91 add_action( 'wp_ajax_bloom_generate_mailing_lists', array( $this, 'generate_mailing_lists' ) );
92
93 add_action( 'wp_ajax_bloom_generate_new_account_fields', array( $this, 'generate_new_account_fields' ) );
94
95 add_action( 'wp_ajax_bloom_generate_accounts_list', array( $this, 'generate_accounts_list' ) );
96
97 add_action( 'wp_ajax_bloom_generate_current_lists', array( $this, 'generate_current_lists' ) );
98
99 add_action( 'wp_ajax_bloom_generate_edit_account_page', array( $this, 'generate_edit_account_page' ) );
100
101 add_action( 'wp_ajax_bloom_save_account_tab', array( $this, 'save_account_tab' ) );
102
103 add_action( 'wp_ajax_bloom_save_updates_tab', array( $this, 'save_updates_tab' ) );
104
105 add_action( 'wp_ajax_bloom_ab_test_actions', array( $this, 'ab_test_actions' ) );
106
107 add_action( 'wp_ajax_bloom_get_stats_graph_ajax', array( $this, 'get_stats_graph_ajax' ) );
108
109 add_action( 'wp_ajax_bloom_refresh_optins_stats_table', array( $this, 'refresh_optins_stats_table' ) );
110
111 add_action( 'wp_ajax_bloom_reset_stats', array( $this, 'reset_stats' ) );
112
113 add_action( 'wp_ajax_bloom_pick_winner_optin', array( $this, 'pick_winner_optin' ) );
114
115 add_action( 'wp_ajax_bloom_clear_stats', array( $this, 'clear_stats' ) );
116
117 add_action( 'wp_ajax_bloom_get_premade_values', array( $this, 'get_premade_values' ) );
118 add_action( 'wp_ajax_bloom_generate_premade_grid', array( $this, 'generate_premade_grid' ) );
119
120 add_action( 'wp_ajax_bloom_display_preview', array( $this, 'display_preview' ) );
121
122 add_action( 'wp_ajax_bloom_handle_stats_adding', array( $this, 'handle_stats_adding' ) );
123 add_action( 'wp_ajax_nopriv_bloom_handle_stats_adding', array( $this, 'handle_stats_adding' ) );
124
125 add_action( 'wp_ajax_bloom_subscribe', array( $this, 'subscribe' ) );
126 add_action( 'wp_ajax_nopriv_bloom_subscribe', array( $this, 'subscribe' ) );
127
128 add_action( 'widgets_init', array( $this, 'register_widget' ) );
129
130 add_action( 'after_setup_theme', array( $this, 'register_image_sizes' ) );
131
132 add_shortcode( 'et_bloom_inline', array( $this, 'display_inline_shortcode' ) );
133 add_shortcode( 'et_bloom_locked', array( $this, 'display_locked_shortcode' ) );
134
135 add_filter( 'body_class', array( $this, 'add_body_class' ) );
136
137 register_deactivation_hook( __FILE__, array( $this, 'deactivate_plugin' ) );
138
139 add_action( 'bloom_lists_auto_refresh', array( $this, 'perform_auto_refresh' ) );
140 add_action( 'bloom_stats_auto_refresh', array( $this, 'perform_stats_refresh' ) );
141
142 add_action( 'admin_init', array( $this, 'maybe_set_db_version' ) );
143
144 $this->frontend_register_locations();
145
146 foreach ( array('post.php','post-new.php') as $hook ) {
147 add_action( "admin_head-$hook", array( $this, 'tiny_mce_vars' ) );
148 add_action( "admin_head-$hook", array( $this, 'add_mce_button_filters' ) );
149 }
150
151 // Plugins Updates system should be loaded before a theme core loads
152 $this->add_updates();
153 }
154
155 function construct_dashboard() {
156 $dashboard_args = array(
157 'et_dashboard_options_pagename' => $this->_options_pagename,
158 'et_dashboard_plugin_name' => 'bloom',
159 'et_dashboard_save_button_text' => esc_html__( 'Save & Exit', 'bloom' ),
160 'et_dashboard_plugin_class_name' => 'et_bloom',
161 'et_dashboard_options_path' => ET_BLOOM_PLUGIN_DIR . '/dashboard/includes/options.php',
162 'et_dashboard_options_page' => 'toplevel_page',
163 );
164
165 parent::__construct( $dashboard_args );
166 }
167
168 function add_updates() {
169 require_once( ET_BLOOM_PLUGIN_DIR . 'core/updates_init.php' );
170
171 et_core_enable_automatic_updates( ET_BLOOM_PLUGIN_URI, $this->plugin_version );
172 }
173
174 static function activate_plugin() {
175 // schedule lists auto update daily
176 wp_schedule_event( time(), 'daily', 'bloom_lists_auto_refresh' );
177
178 //install the db for stats
179 self::db_install();
180
181 update_option( 'bloom_is_just_activated', 'true' );
182 }
183
184 function deactivate_plugin() {
185 // remove lists auto updates from wp cron if plugin deactivated
186 wp_clear_scheduled_hook( 'bloom_lists_auto_refresh' );
187 wp_clear_scheduled_hook( 'bloom_stats_auto_refresh' );
188 }
189
190 function define_page_name() {
191 return $this->_options_pagename;
192 }
193
194 /**
195 * Returns an instance of the object
196 *
197 * @return object
198 */
199 static function get_this() {
200 return self::$_this;
201 }
202
203 function add_menu_link() {
204 $menu_page = add_menu_page( esc_html__( 'Bloom', 'bloom' ), esc_html__( 'Bloom', 'bloom' ), 'manage_options', 'et_bloom_options', array( $this, 'options_page' ) );
205 add_submenu_page( 'et_bloom_options', esc_html__( 'Optin Forms', 'bloom' ), esc_html__( 'Optin Forms', 'bloom' ), 'manage_options', 'et_bloom_options' );
206 add_submenu_page( 'et_bloom_options', esc_html__( 'Email Accounts', 'bloom' ), esc_html__( 'Email Accounts', 'bloom' ), 'manage_options', 'admin.php?page=et_bloom_options#tab_et_dashboard_tab_content_header_accounts' );
207 add_submenu_page( 'et_bloom_options', esc_html__( 'Statistics', 'bloom' ), esc_html__( 'Statistics', 'bloom' ), 'manage_options', 'admin.php?page=et_bloom_options#tab_et_dashboard_tab_content_header_stats' );
208 add_submenu_page( 'et_bloom_options', esc_html__( 'Import & Export', 'bloom' ), esc_html__( 'Import & Export', 'bloom' ), 'manage_options', 'admin.php?page=et_bloom_options#tab_et_dashboard_tab_content_header_importexport' );
209 }
210
211 function add_body_class( $body_class ) {
212 $body_class[] = 'et_bloom';
213
214 return $body_class;
215 }
216
217 function save_btn_class() {
218 return 'et_dashboard_custom_save';
219 }
220
221 /**
222 * Adds plugin localization
223 * Domain: bloom
224 *
225 * @return void
226 */
227 function add_localization() {
228 load_plugin_textdomain( 'bloom', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
229 }
230
231 // Add settings link on plugin page
232 function add_settings_link( $links ) {
233 $settings_link = sprintf( '<a href="admin.php?page=et_bloom_options">%1$s</a>', esc_html__( 'Settings', 'bloom' ) );
234 array_unshift( $links, $settings_link );
235 return $links;
236 }
237
238 function options_page() {
239 ET_Bloom::generate_options_page( $this->generate_optin_id() );
240 }
241
242 function import_settings() {
243 return true;
244 }
245
246 function bloom_save_settings() {
247 ET_Bloom::dashboard_save_settings();
248 }
249
250 function filter_export_settings( $options ) {
251 $updated_array = array_merge( $options, array( 'accounts' ) );
252 return $updated_array;
253 }
254
255 /**
256 *
257 * Adds the "Next" button into the Bloom dashboard via ET_Dashboard action.
258 * @return prints the data on screen
259 *
260 */
261 function add_next_button() {
262 printf( '
263 <div class="et_dashboard_row et_dashboard_next_design">
264 <button class="et_dashboard_icon">%1$s</button>
265 </div>',
266 esc_html__( 'Next: Design Your Optin', 'bloom' )
267 );
268
269 printf( '
270 <div class="et_dashboard_row et_dashboard_next_display">
271 <button class="et_dashboard_icon">%1$s</button>
272 </div>',
273 esc_html__( 'Next: Display Settings', 'bloom' )
274 );
275
276 printf( '
277 <div class="et_dashboard_row et_dashboard_next_customize">
278 <button class="et_dashboard_icon" data-selected_layout="layout_1">%1$s</button>
279 </div>',
280 esc_html__( 'Next: Customize', 'bloom' )
281 );
282
283 printf( '
284 <div class="et_dashboard_row et_dashboard_next_shortcode">
285 <button class="et_dashboard_icon">%1$s</button>
286 </div>',
287 esc_html__( 'Generate Shortcode', 'bloom' )
288 );
289 }
290
291 /**
292 * Retrieves the Bloom options from DB and makes it available outside the class
293 * @return array
294 */
295 public static function get_bloom_options() {
296 return get_option( 'et_bloom_options' ) ? get_option( 'et_bloom_options' ) : array();
297 }
298
299 /**
300 * Updates the Bloom options outside the class
301 * @return void
302 */
303 public static function update_bloom_options( $update_array ) {
304 $dashboard_options = ET_Bloom::get_bloom_options();
305
306 $updated_options = array_merge( $dashboard_options, $update_array );
307 update_option( 'et_bloom_options', $updated_options );
308 }
309
310 /**
311 * Filters the options_array before importing data. Function generates new IDs for imported options to avoid replacement of existing ones.
312 * Filter is used in ET_Dashboard class
313 * @return array
314 */
315 function import_filter( $options_array ) {
316 $updated_array = array();
317 $new_id = $this->generate_optin_id( false );
318
319 foreach ( $options_array as $key => $value ) {
320 $updated_array['optin_' . $new_id] = $options_array[$key];
321
322 //reset accounts settings and make all new optins inactive
323 $updated_array['optin_' . $new_id]['email_provider'] = 'empty';
324 $updated_array['optin_' . $new_id]['account_name'] = 'empty';
325 $updated_array['optin_' . $new_id]['email_list'] = 'empty';
326 $updated_array['optin_' . $new_id]['optin_status'] = 'inactive';
327 $new_id++;
328 }
329
330 return $updated_array;
331 }
332
333 function add_mce_button_filters() {
334 add_filter( 'mce_external_plugins', array( $this, 'add_mce_button' ) );
335 add_filter( 'mce_buttons', array( $this, 'register_mce_button' ) );
336 }
337
338 function add_mce_button( $plugin_array ) {
339 global $typenow;
340
341 wp_enqueue_style( 'bloom-shortcodes', ET_BLOOM_PLUGIN_URI . '/css/tinymcebutton.css', array(), $this->plugin_version );
342 $plugin_array['bloom'] = ET_BLOOM_PLUGIN_URI . '/js/bloom-mce-buttons.js';
343
344
345 return $plugin_array;
346 }
347
348 function register_mce_button( $buttons ) {
349 global $typenow;
350
351 array_push( $buttons, 'bloom_button' );
352
353 return $buttons;
354 }
355
356
357 /**
358 * Pass locked_optins and inline_optins lists to tiny-MCE script
359 */
360 function tiny_mce_vars() {
361 $options_array = ET_Bloom::get_bloom_options();
362 $locked_array = array();
363 $inline_array = array();
364 if ( ! empty( $options_array ) ) {
365 foreach ( $options_array as $optin_id => $details ) {
366 if ( 'accounts' !== $optin_id ) {
367 if ( isset( $details['optin_status'] ) && 'active' === $details['optin_status'] && empty( $details['child_of'] ) ) {
368 if ( 'inline' == $details['optin_type'] ) {
369 $inline_array = array_merge( $inline_array, array( $optin_id => preg_replace( '/[^A-Za-z0-9 _-]/', '', $details['optin_name'] ) ) );
370 }
371
372 if ( 'locked' == $details['optin_type'] ) {
373 $locked_array = array_merge( $locked_array, array( $optin_id => preg_replace( '/[^A-Za-z0-9 _-]/', '', $details['optin_name'] ) ) );
374 }
375 }
376 }
377 }
378 }
379
380 if ( empty( $locked_array ) ) {
381 $locked_array = array(
382 'empty' => esc_html__( 'No optins available', 'bloom' ),
383 );
384 }
385
386 if ( empty( $inline_array ) ) {
387 $inline_array = array(
388 'empty' => esc_html__( 'No optins available', 'bloom' ),
389 );
390 }
391 ?>
392
393 <!-- TinyMCE Shortcode Plugin -->
394 <script type='text/javascript'>
395 var bloom = {
396 'locked_optins' : '<?php echo json_encode( $locked_array ); ?>',
397 'inline_optins' : '<?php echo json_encode( $inline_array ); ?>',
398 'bloom_tooltip' : '<?php echo json_encode( esc_html__( "insert bloom Opt-In", "bloom" ) ); ?>',
399 'inline_text' : '<?php echo json_encode( esc_html__( "Inline Opt-In", "bloom" ) ); ?>',
400 'locked_text' : '<?php echo json_encode( esc_html__( "Locked Content Opt-In", "bloom" ) ); ?>'
401 }
402 </script>
403 <!-- TinyMCE Shortcode Plugin -->
404<?php
405 }
406
407 static function db_install() {
408 global $wpdb;
409
410 $table_name = $wpdb->prefix . 'et_bloom_stats';
411
412 /*
413 * We'll set the default character set and collation for this table.
414 * If we don't do this, some characters could end up being converted
415 * to just ?'s when saved in our table.
416 */
417 $charset_collate = '';
418
419 if ( ! empty( $wpdb->charset ) ) {
420 $charset_collate = sprintf(
421 'DEFAULT CHARACTER SET %1$s',
422 sanitize_text_field( $wpdb->charset )
423 );
424 }
425
426 if ( ! empty( $wpdb->collate ) ) {
427 $charset_collate .= sprintf(
428 ' COLLATE %1$s',
429 sanitize_text_field( $wpdb->collate )
430 );
431 }
432
433 $sql = "CREATE TABLE $table_name (
434 id mediumint(9) NOT NULL AUTO_INCREMENT,
435 record_date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
436 record_type varchar(3) NOT NULL,
437 optin_id varchar(20) NOT NULL,
438 list_id varchar(100) NOT NULL,
439 ip_address varchar(45) NOT NULL,
440 page_id varchar(20) NOT NULL,
441 removed_flag boolean NOT NULL,
442 UNIQUE KEY id (id)
443 ) $charset_collate;";
444
445 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
446 dbDelta( $sql );
447 }
448
449 /**
450 * Save db_version to the database, when the plugin is activated
451 *
452 * @return void
453 */
454 function maybe_set_db_version() {
455 if ( 'true' !== get_option( 'bloom_is_just_activated' ) ) {
456 return;
457 }
458
459 $db_version = array(
460 'db_version' => $this->db_version,
461 );
462
463 ET_Bloom::update_option( $db_version );
464
465 delete_option( 'bloom_is_just_activated' );
466 }
467
468 function register_image_sizes() {
469 add_image_size( 'bloom_image', 610 );
470 }
471
472 /**
473 * Generates the Bloom's Home, Stats, Accounts tabs. Hooked to Dashboard class
474 */
475 function generate_home_tab( $option, $dashboard_settings = array() ) {
476 switch ( $option['type'] ) {
477 case 'home' :
478 printf( '
479 <div class="et_dashboard_row et_dashboard_new_optin">
480 <h1>%2$s</h1>
481 <button class="et_dashboard_icon">%1$s</button>
482 <input type="hidden" name="action" value="new_optin" />
483 </div>' ,
484 esc_html__( 'new optin', 'bloom' ),
485 esc_html__( 'Active Optins', 'bloom' )
486 );
487 printf( '
488 <div class="et_dashboard_row et_dashboard_optin_select">
489 <h3>%1$s</h3>
490 <span class="et_dashboard_icon et_dashboard_close_button"></span>
491 <ul>
492 <li class="et_dashboard_optin_type et_dashboard_optin_add et_dashboard_optin_type_popup" data-type="pop_up">
493 <h6>%2$s</h6>
494 <div class="optin_select_grey">
495 <div class="optin_select_blue">
496 </div>
497 </div>
498 </li>
499 <li class="et_dashboard_optin_type et_dashboard_optin_add et_dashboard_optin_type_flyin" data-type="flyin">
500 <h6>%3$s</h6>
501 <div class="optin_select_grey"></div>
502 <div class="optin_select_blue"></div>
503 </li>
504 <li class="et_dashboard_optin_type et_dashboard_optin_add et_dashboard_optin_type_below" data-type="below_post">
505 <h6>%4$s</h6>
506 <div class="optin_select_grey"></div>
507 <div class="optin_select_blue"></div>
508 </li>
509 <li class="et_dashboard_optin_type et_dashboard_optin_add et_dashboard_optin_type_inline" data-type="inline">
510 <h6>%5$s</h6>
511 <div class="optin_select_grey"></div>
512 <div class="optin_select_blue"></div>
513 <div class="optin_select_grey"></div>
514 </li>
515 <li class="et_dashboard_optin_type et_dashboard_optin_add et_dashboard_optin_type_locked" data-type="locked">
516 <h6>%6$s</h6>
517 <div class="optin_select_grey"></div>
518 <div class="optin_select_blue"></div>
519 <div class="optin_select_grey"></div>
520 </li>
521 <li class="et_dashboard_optin_type et_dashboard_optin_add et_dashboard_optin_type_widget" data-type="widget">
522 <h6>%7$s</h6>
523 <div class="optin_select_grey"></div>
524 <div class="optin_select_blue"></div>
525 <div class="optin_select_grey_small"></div>
526 <div class="optin_select_grey_small last"></div>
527 </li>
528 </ul>
529 </div>',
530 esc_html__( 'select optin type to begin', 'bloom' ),
531 esc_html__( 'pop up', 'bloom' ),
532 esc_html__( 'fly in', 'bloom' ),
533 esc_html__( 'below post', 'bloom' ),
534 esc_html__( 'inline', 'bloom' ),
535 esc_html__( 'locked content', 'bloom' ),
536 esc_html__( 'widget', 'bloom' )
537 );
538
539 $this->display_home_tab_tables();
540 break;
541
542 case 'account' :
543 printf( '
544 <div class="et_dashboard_row et_dashboard_new_account_row">
545 <h1>%2$s</h1>
546 <button class="et_dashboard_icon">%1$s</button>
547 <input type="hidden" name="action" value="new_account" />
548 </div>' ,
549 esc_html__( 'new account', 'bloom' ),
550 esc_html__( 'My Accounts', 'bloom' )
551 );
552
553 $this->display_accounts_table();
554 break;
555
556 case 'edit_account' :
557 echo '<div id="et_dashboard_edit_account_tab"></div>';
558 break;
559
560 case 'stats' :
561 printf( '
562 <div class="et_dashboard_row et_dashboard_stats_row">
563 <h1>%1$s</h1>
564 <div class="et_bloom_stats_controls">
565 <button class="et_dashboard_icon et_bloom_clear_stats">%2$s</button>
566 <span class="et_dashboard_confirmation">%4$s</span>
567 <button class="et_dashboard_icon et_bloom_refresh_stats">%3$s</button>
568 </div>
569 </div>
570 <span class="et_bloom_stats_spinner"></span>
571 <div class="et_dashboard_stats_contents"></div>',
572 esc_html( $option['title'] ),
573 esc_html__( 'Clear Stats', 'bloom' ),
574 esc_html__( 'Refresh Stats', 'bloom' ),
575 sprintf(
576 '%1$s<span class="et_dashboard_confirm_stats">%2$s</span><span class="et_dashboard_cancel_delete">%3$s</span>',
577 esc_html__( 'Remove all the stats data?', 'bloom' ),
578 esc_html__( 'Yes', 'bloom' ),
579 esc_html__( 'No', 'bloom' )
580 )
581 );
582 break;
583
584 case 'updates' :
585 $et_updates_settings = get_option( 'et_automatic_updates_options' );
586 printf( '
587 <div class="et_dashboard_row et_dashboard_updates_settings_row">
588 <h1>%1$s</h1>
589 <p>%11$s</p>
590 <div class="et_dashboard_form">
591 <div class="et_dashboard_account_row">
592 <label for="%2$s">%3$s</label>
593 <input type="password" value="%4$s" id="%2$s">%5$s
594 </div>
595 <div class="et_dashboard_account_row">
596 <label for="%6$s">%7$s</label>
597 <input type="password" value="%8$s" id="%6$s">%9$s
598 </div>
599 <button class="et_dashboard_icon et_pb_save_updates_settings">%10$s</button>
600 <span class="spinner"></span>
601 </div>
602 </div>' ,
603 esc_html__( 'Enable Updates', 'bloom' ),
604 esc_attr( 'et_bloom_updates_username' ),
605 esc_html__( 'Username', 'bloom' ),
606 isset( $et_updates_settings['username'] ) ? esc_attr( $et_updates_settings['username'] ) : '',
607 ET_Bloom::generate_hint( esc_html__( 'Please enter your ElegantThemes.com username.', 'bloom' ), true ), // #5
608 esc_attr( 'et_bloom_updates_api_key' ),
609 esc_html__( 'API Key', 'bloom' ),
610 isset( $et_updates_settings['api_key'] ) ? esc_attr( $et_updates_settings['api_key'] ) : '',
611 ET_Bloom::generate_hint(
612 sprintf( esc_html__( 'Enter your %1$s here.', 'bloom' ),
613 sprintf( '<a href="%1$s" target="_blank">%2$s</a>',
614 esc_attr( 'https://www.elegantthemes.com/members-area/api-key.php' ),
615 esc_html__( 'Elegant Themes API Key', 'bloom' )
616 )
617 ), false ),
618 esc_html__( 'Save', 'bloom' ), // #10
619 sprintf( esc_html__( 'Keeping your plugins updated is important. To %1$s for Bloom, you must first authenticate your Elegant Themes account by inputting your account Username and API Key below. Your username is the same username you use when logging into your Elegant Themes account, and your API Key can be found by logging into your account and navigating to the Account > API Key page.', 'bloom' ),
620 sprintf( '<a href="%1$s" target="_blank">%2$s</a>',
621 esc_attr( 'https://www.elegantthemes.com/members-area/documentation.html#update' ),
622 esc_html__( 'enable updates', 'bloom' )
623 )
624 ) // #11
625 );
626 break;
627 }
628 }
629
630 /**
631 * Generates tab for the premade layouts selection
632 */
633 function generate_premade_templates( $option ) {
634 switch ( $option['type'] ) {
635 case 'premade_templates' :
636 echo '<div class="et_bloom_premade_grid"><span class="spinner et_bloom_premade_spinner"></span></div>';
637 break;
638 case 'preview_optin' :
639 printf( '
640 <div class="et_dashboard_row et_dashboard_preview">
641 <button class="et_dashboard_icon">%1$s</button>
642 </div>',
643 esc_html__( 'Preview', 'bloom' )
644 );
645 break;
646 }
647 }
648
649 function generate_premade_grid() {
650 if ( ! wp_verify_nonce( $_POST['bloom_premade_nonce'] , 'bloom_premade' ) ) {
651 die( -1 );
652 }
653
654 if ( ! current_user_can( 'manage_options' ) ) {
655 die( -1 );
656 }
657
658 require_once( ET_BLOOM_PLUGIN_DIR . 'includes/premade-layouts.php' );
659 $output = '';
660
661 if ( isset( $all_layouts ) ) {
662 $i = 0;
663
664 $output .= '<div class="et_bloom_premade_grid">';
665
666 foreach( $all_layouts as $layout_id => $layout_options ) {
667 $output .= sprintf( '
668 <div class="et_bloom_premade_item%2$s et_bloom_premade_id_%1$s" data-layout="%1$s">
669 <div class="et_bloom_premade_item_inner">
670 <img src="%3$s" alt="" />
671 </div>
672 </div>',
673 esc_attr( $layout_id ),
674 0 == $i ? ' et_bloom_layout_selected' : '',
675 esc_url( ET_BLOOM_PLUGIN_URI . '/images/thumb_' . $layout_id . '.svg' )
676 );
677 $i++;
678 }
679
680 $output .= '</div>';
681 }
682
683 die( $output );
684 }
685
686 /**
687 * Gets the layouts data, converts it to json string and passes back to js script to fill the form with predefined values
688 */
689 function get_premade_values() {
690 if ( ! wp_verify_nonce( $_POST['bloom_premade_nonce'] , 'bloom_premade' ) ) {
691 die( -1 );
692 }
693
694 if ( ! current_user_can( 'manage_options' ) ) {
695 die( -1 );
696 }
697
698 $premade_data_json = str_replace( '\\', '' , $_POST['premade_data_array'] );
699 $premade_data = json_decode( $premade_data_json, true );
700 $layout_id = $premade_data['id'];
701
702 require_once( ET_BLOOM_PLUGIN_DIR . 'includes/premade-layouts.php' );
703
704 if ( isset( $all_layouts[$layout_id] ) ) {
705 $options_set = json_encode( $all_layouts[$layout_id] );
706 }
707
708 die( $options_set );
709 }
710
711 /**
712 * Generates output for the Stats tab
713 */
714 function generate_stats_tab() {
715 if ( ! current_user_can( 'manage_options' ) ) {
716 die( -1 );
717 }
718
719 $options_array = ET_Bloom::get_bloom_options();
720
721 $output = sprintf( '
722 <div class="et_dashboard_stats_contents et_dashboard_stats_ready">
723 <div class="et_dashboard_all_time_stats">
724 <h3>%1$s</h3>
725 %2$s
726 </div>
727 <div class="et_dashboard_optins_stats et_dashboard_optins_all_table">
728 <div class="et_dashboard_optins_list">
729 %3$s
730 </div>
731 </div>
732 <div class="et_dashboard_optins_stats et_dashboard_lists_stats_graph">
733 <div class="et_bloom_graph_header">
734 <h3>%6$s</h3>
735 <div class="et_bloom_graph_controls">
736 <a href="#" class="et_bloom_graph_button et_bloom_active_button" data-period="30">%7$s</a>
737 <a href="#" class="et_bloom_graph_button" data-period="12">%8$s</a>
738 <select class="et_bloom_graph_select_list">%9$s</select>
739 </div>
740 </div>
741 %5$s
742 </div>
743 <div class="et_dashboard_optins_stats et_dashboard_lists_stats">
744 %4$s
745 </div>
746 %10$s
747 </div>',
748 esc_html__( 'Overview', 'bloom' ),
749 $this->generate_all_time_stats(),
750 $this->generate_optins_stats_table( 'conversion_rate', true ),
751 ( ! empty( $options_array['accounts'] ) )
752 ? sprintf(
753 '<div class="et_dashboard_optins_list">
754 %1$s
755 </div>',
756 $this->generate_lists_stats_table( 'count', true )
757 )
758 : '',
759 $this->generate_lists_stats_graph( 30, 'day', '' ), // #5
760 esc_html__( 'New sign ups', 'bloom' ),
761 esc_html__( 'Last 30 days', 'bloom' ),
762 esc_html__( 'Last 12 month', 'bloom' ),
763 $this->generate_all_lists_select(),
764 $this->generate_pages_stats() // #10
765 );
766
767 return $output;
768 }
769
770 /**
771 * Generates the stats tab and passes it to jQuery
772 * @return string
773 */
774 function reset_stats() {
775 if ( ! wp_verify_nonce( $_POST['bloom_stats_nonce'] , 'bloom_stats' ) ) {
776 die( -1 );
777 }
778
779 if ( ! current_user_can( 'manage_options' ) ) {
780 die( -1 );
781 }
782
783 $force_update = ! empty( $_POST['bloom_force_upd_stats'] ) ? sanitize_text_field( $_POST['bloom_force_upd_stats'] ) : '';
784
785 if ( get_option( 'et_bloom_stats_cache' ) && 'true' !== $force_update ) {
786 $output = get_option( 'et_bloom_stats_cache' );
787 } else {
788 $output = $this->generate_stats_tab();
789 update_option( 'et_bloom_stats_cache', $output );
790 }
791
792 if ( ! wp_get_schedule( 'bloom_stats_auto_refresh' ) ) {
793 wp_schedule_event( time(), 'daily', 'bloom_stats_auto_refresh' );
794 }
795
796 die( $output );
797 }
798
799 /**
800 * Update Stats and save it into WP DB
801 * @return void
802 */
803 function perform_stats_refresh() {
804 $fresh_stats = $output = $this->generate_stats_tab();
805 update_option( 'et_bloom_stats_cache', $fresh_stats );
806 }
807
808 /**
809 * Removes all the stats data from DB
810 * @return void
811 */
812 function clear_stats() {
813 if ( ! wp_verify_nonce( $_POST['bloom_stats_nonce'] , 'bloom_stats' ) ) {
814 die( -1 );
815 }
816
817 if ( ! current_user_can( 'manage_options' ) ) {
818 die( -1 );
819 }
820
821 global $wpdb;
822
823 $table_name = $wpdb->prefix . 'et_bloom_stats';
824
825 // construct sql query to mark removed options as removed in stats DB
826 $sql = "TRUNCATE TABLE $table_name";
827
828 $wpdb->query( $sql );
829 }
830
831 /**
832 * Generates the Lists menu for Lists stats graph
833 * @return string
834 */
835 function generate_all_lists_select() {
836 $options_array = ET_Bloom::get_bloom_options();
837 $output = sprintf( '<option value="all">%1$s</option>', esc_html__( 'All lists', 'bloom' ) );
838
839 if ( ! empty( $options_array['accounts'] ) ) {
840 foreach ( $options_array['accounts'] as $service => $accounts ) {
841 foreach ( $accounts as $name => $details ) {
842 if ( ! empty( $details['lists'] ) ) {
843 foreach ( $details['lists'] as $id => $list_data ) {
844 $output .= sprintf(
845 '<option value="%2$s">%1$s</option>',
846 esc_html( $service . ' - ' . $list_data['name'] ),
847 esc_attr( $service . '_' . $id )
848 );
849 }
850 }
851 }
852 }
853 }
854
855 return $output;
856 }
857
858 /**
859 * Generates the Overview part of stats page
860 * @return string
861 */
862 function generate_all_time_stats( $empty_stats = false ) {
863
864 $conversion_rate = $this->conversion_rate( 'all' );
865
866 $all_subscribers = $this->calculate_subscribers( 'all' );
867
868 $growth_rate = $this->calculate_growth_rate( 'all' );
869
870 $ouptut = sprintf(
871 '<div class="et_dashboard_stats_container">
872 <div class="all_stats_column conversion_rate">
873 <span class="value">%1$s</span>
874 <span class="caption">%2$s</span>
875 </div>
876 <div class="all_stats_column subscribers">
877 <span class="value">%3$s</span>
878 <span class="caption">%4$s</span>
879 </div>
880 <div class="all_stats_column growth_rate">
881 <span class="value">%5$s<span>/%7$s</span></span>
882 <span class="caption">%6$s</span>
883 </div>
884 <div style="clear: both;"></div>
885 </div>',
886 esc_html( $conversion_rate . '%' ),
887 esc_html__( 'Conversion Rate', 'bloom' ),
888 esc_html( $all_subscribers ),
889 esc_html__( 'Subscribers', 'bloom' ),
890 esc_html( $growth_rate ),
891 esc_html__( 'Subscriber Growth', 'bloom' ),
892 esc_html__( 'week', 'bloom' )
893 );
894
895 return $ouptut;
896 }
897
898 /**
899 * Generates the stats table with optins
900 * @return string
901 */
902 function generate_optins_stats_table( $orderby = 'conversion_rate', $include_header = false ) {
903 if ( ! current_user_can( 'manage_options' ) ) {
904 die( -1 );
905 }
906
907 $options_array = ET_Bloom::get_bloom_options();
908 $optins_count = 0;
909 $output = '';
910 $total_impressions = 0;
911 $total_conversions = 0;
912
913 foreach ( $options_array as $optin_id => $value ) {
914 if ( 'accounts' !== $optin_id && 'db_version' !== $optin_id ) {
915 if ( 0 === $optins_count ) {
916 if ( true == $include_header ) {
917 $output .= sprintf(
918 '<ul>
919 <li data-table="optins">
920 <div class="et_dashboard_table_name et_dashboard_table_column et_table_header">%1$s</div>
921 <div class="et_dashboard_table_impressions et_dashboard_table_column et_dashboard_icon et_dashboard_sort_button" data-order_by="impressions">%2$s</div>
922 <div class="et_dashboard_table_conversions et_dashboard_table_column et_dashboard_icon et_dashboard_sort_button" data-order_by="conversions">%3$s</div>
923 <div class="et_dashboard_table_rate et_dashboard_table_column et_dashboard_icon et_dashboard_sort_button active_sorting" data-order_by="conversion_rate">%4$s</div>
924 <div style="clear: both;"></div>
925 </li>
926 </ul>',
927 esc_html__( 'My Optins', 'bloom' ),
928 esc_html__( 'Impressions', 'bloom' ),
929 esc_html__( 'Conversions', 'bloom' ),
930 esc_html__( 'Conversion Rate', 'bloom' )
931 );
932 }
933
934 $output .= '<ul class="et_dashboard_table_contents">';
935 }
936
937 $total_impressions += $impressions = $this->stats_count( $optin_id, 'imp' );
938 $total_conversions += $conversions = $this->stats_count( $optin_id, 'con' );
939
940 $unsorted_optins[$optin_id] = array(
941 'name' => $value['optin_name'],
942 'impressions' => $impressions,
943 'conversions' => $conversions,
944 'conversion_rate' => $this->conversion_rate( $optin_id, $conversions, $impressions ),
945 'type' => $value['optin_type'],
946 'status' => $value['optin_status'],
947 'child_of' => $value['child_of'],
948 );
949 $optins_count++;
950
951 }
952 }
953
954 if ( ! empty( $unsorted_optins ) ) {
955 $sorted_optins = $this->sort_array( $unsorted_optins, $orderby );
956
957 foreach ( $sorted_optins as $id => $details ) {
958 if ( '' !== $details['child_of'] ) {
959 $status = $options_array[$details['child_of']]['optin_status'];
960 } else {
961 $status = $details['status'];
962 }
963
964 $output .= sprintf(
965 '<li class="et_dashboard_optins_item et_dashboard_parent_item">
966 <div class="et_dashboard_table_name et_dashboard_table_column et_dashboard_icon et_dashboard_type_%5$s et_dashboard_status_%6$s">%1$s</div>
967 <div class="et_dashboard_table_impressions et_dashboard_table_column">%2$s</div>
968 <div class="et_dashboard_table_conversions et_dashboard_table_column">%3$s</div>
969 <div class="et_dashboard_table_rate et_dashboard_table_column">%4$s</div>
970 <div style="clear: both;"></div>
971 </li>',
972 esc_html( $details['name'] ),
973 esc_html( $details['impressions'] ),
974 esc_html( $details['conversions'] ),
975 esc_html( $details['conversion_rate'] ) . '%',
976 esc_attr( $details['type'] ),
977 esc_attr( $status )
978 );
979 }
980 }
981
982 if ( 0 < $optins_count ) {
983 $output .= sprintf(
984 '<li class="et_dashboard_optins_item_bottom_row">
985 <div class="et_dashboard_table_name et_dashboard_table_column"></div>
986 <div class="et_dashboard_table_impressions et_dashboard_table_column">%1$s</div>
987 <div class="et_dashboard_table_conversions et_dashboard_table_column">%2$s</div>
988 <div class="et_dashboard_table_rate et_dashboard_table_column">%3$s</div>
989 </li>',
990 esc_html( $this->get_compact_number( $total_impressions ) ),
991 esc_html( $this->get_compact_number( $total_conversions ) ),
992 ( 0 !== $total_impressions )
993 ? esc_html( round( ( $total_conversions * 100 ) / $total_impressions, 1 ) . '%' )
994 : '0%'
995 );
996 $output .= '</ul>';
997 }
998
999 return $output;
1000 }
1001
1002
1003 /**
1004 * Changes the order of rows in array based on input parameters
1005 * @return array
1006 */
1007 function sort_array( $unsorted_array, $orderby, $order = SORT_DESC ) {
1008 $temp_array = array();
1009 foreach ( $unsorted_array as $ma ) {
1010 $temp_array[] = $ma[$orderby];
1011 }
1012
1013 array_multisort( $temp_array, $order, $unsorted_array );
1014
1015 return $unsorted_array;
1016 }
1017
1018 /**
1019 * Generates the highest converting pages table
1020 * @return string
1021 */
1022 function generate_pages_stats() {
1023 $all_pages_id = $this->get_all_stats_pages();
1024 $con_by_pages = array();
1025 $output = '';
1026
1027 if ( empty( $all_pages_id ) ) {
1028 return;
1029 }
1030
1031 foreach( $all_pages_id as $page ) {
1032 $con_by_pages[$page['page_id']] = $this->get_unique_optins_by_page( $page['page_id'] );
1033 }
1034
1035 if ( ! empty( $con_by_pages ) ) {
1036 foreach ( $con_by_pages as $page_id => $optins ) {
1037 $unique_optins = array();
1038 foreach( $optins as $optin_id ) {
1039 if ( ! in_array( $optin_id, $unique_optins ) ) {
1040 $unique_optins[] = $optin_id;
1041 $rate_by_pages[$page_id][] = array(
1042 $optin_id => $this->conversion_rate( $optin_id, '0', '0', $page_id ),
1043 );
1044 }
1045 }
1046 }
1047
1048 $i = 0;
1049
1050 foreach ( $rate_by_pages as $page_id => $rate ) {
1051 $page_rate = 0;
1052 $rates_count = 0;
1053 $optins_data = array();
1054 $j = 0;
1055
1056 foreach ( $rate as $current_optin ) {
1057 foreach ( $current_optin as $optin_id => $current_rate ) {
1058 $page_rate = $page_rate + $current_rate;
1059 $rates_count++;
1060
1061 $optins_data[$j] = array(
1062 'optin_id' => $optin_id,
1063 'optin_rate' => $current_rate,
1064 );
1065
1066 }
1067 $j++;
1068 }
1069
1070 $average_rate = 0 != $rates_count ? round( $page_rate / $rates_count, 1 ) : 0;
1071 $rate_by_pages_unsorted[$i]['page_id'] = $page_id;
1072 $rate_by_pages_unsorted[$i]['page_rate'] = $average_rate;
1073 $rate_by_pages_unsorted[$i]['optins_data'] = $this->sort_array( $optins_data, 'optin_rate', $order = SORT_DESC );
1074
1075 $i++;
1076 }
1077
1078 $rate_by_pages_sorted = $this->sort_array( $rate_by_pages_unsorted, 'page_rate', $order = SORT_DESC );
1079 $output = '';
1080
1081 if ( ! empty( $rate_by_pages_sorted ) ) {
1082 $options_array = ET_Bloom::get_bloom_options();
1083 $table_contents = '<ul>';
1084
1085 for ( $i = 0; $i < 5; $i++ ) {
1086 if ( ! empty( $rate_by_pages_sorted[$i] ) ) {
1087 $table_contents .= sprintf(
1088 '<li class="et_table_page_row">
1089 <div class="et_dashboard_table_name et_dashboard_table_column et_table_page_row">%1$s</div>
1090 <div class="et_dashboard_table_pages_rate et_dashboard_table_column">%2$s</div>
1091 <div style="clear: both;"></div>
1092 </li>',
1093 -1 == $rate_by_pages_sorted[$i]['page_id']
1094 ? esc_html__( 'Homepage', 'bloom' )
1095 : esc_html( get_the_title( $rate_by_pages_sorted[$i]['page_id'] ) ),
1096 esc_html( $rate_by_pages_sorted[$i]['page_rate'] ) . '%'
1097 );
1098 foreach ( $rate_by_pages_sorted[$i]['optins_data'] as $optin_details ) {
1099 if ( isset( $options_array[$optin_details['optin_id']]['child_of'] ) && '' !== $options_array[$optin_details['optin_id']]['child_of'] ) {
1100 $status = $options_array[$options_array[$optin_details['optin_id']]['child_of']]['optin_status'];
1101 } else {
1102 $status = isset( $options_array[$optin_details['optin_id']]['optin_status'] ) ? $options_array[$optin_details['optin_id']]['optin_status'] : 'inactive';
1103 }
1104
1105 $table_contents .= sprintf(
1106 '<li class="et_table_optin_row et_dashboard_optins_item">
1107 <div class="et_dashboard_table_name et_dashboard_table_column et_dashboard_icon et_dashboard_type_%3$s et_dashboard_status_%4$s">%1$s</div>
1108 <div class="et_dashboard_table_pages_rate et_dashboard_table_column">%2$s</div>
1109 <div style="clear: both;"></div>
1110 </li>',
1111 ( isset( $options_array[$optin_details['optin_id']]['optin_name'] ) )
1112 ? esc_html( $options_array[$optin_details['optin_id']]['optin_name'] )
1113 : '',
1114 esc_html( $optin_details['optin_rate'] ) . '%',
1115 ( isset( $options_array[$optin_details['optin_id']]['optin_type'] ) )
1116 ? esc_attr( $options_array[$optin_details['optin_id']]['optin_type'] )
1117 : '',
1118 esc_attr( $status )
1119 );
1120 }
1121 }
1122 }
1123
1124 $table_contents .= '</ul>';
1125
1126 $output = sprintf(
1127 '<div class="et_dashboard_optins_stats et_dashboard_pages_stats">
1128 <div class="et_dashboard_optins_list">
1129 <ul>
1130 <li>
1131 <div class="et_dashboard_table_name et_dashboard_table_column et_table_header">%1$s</div>
1132 <div class="et_dashboard_table_pages_rate et_dashboard_table_column et_table_header">%2$s</div>
1133 <div style="clear: both;"></div>
1134 </li>
1135 </ul>
1136 %3$s
1137 </div>
1138 </div>',
1139 esc_html__( 'Highest converting pages', 'bloom' ),
1140 esc_html__( 'Conversion rate', 'bloom' ),
1141 $table_contents
1142 );
1143 }
1144 }
1145
1146 return $output;
1147 }
1148
1149 /**
1150 * Generates the stats table with lists
1151 * @return string
1152 */
1153 function generate_lists_stats_table( $orderby = 'count', $include_header = false ) {
1154 if ( ! current_user_can( 'manage_options' ) ) {
1155 die( -1 );
1156 }
1157
1158 $options_array = ET_Bloom::get_bloom_options();
1159 $optins_count = 0;
1160 $output = '';
1161 $total_subscribers = 0;
1162
1163 if ( ! empty( $options_array['accounts'] ) ) {
1164 foreach ( $options_array['accounts'] as $service => $accounts ) {
1165 foreach ( $accounts as $name => $details ) {
1166 if ( ! empty( $details['lists'] ) ) {
1167 foreach ( $details['lists'] as $id => $list_data ) {
1168 if ( 0 === $optins_count ) {
1169 if ( true == $include_header ) {
1170 $output .= sprintf(
1171 '<ul>
1172 <li data-table="lists">
1173 <div class="et_dashboard_table_name et_dashboard_table_column et_table_header">%1$s</div>
1174 <div class="et_dashboard_table_impressions et_dashboard_table_column et_dashboard_icon et_dashboard_sort_button" data-order_by="service">%2$s</div>
1175 <div class="et_dashboard_table_rate et_dashboard_table_column et_dashboard_icon et_dashboard_sort_button active_sorting" data-order_by="count">%3$s</div>
1176 <div class="et_dashboard_table_conversions et_dashboard_table_column et_dashboard_icon et_dashboard_sort_button" data-order_by="growth">%4$s</div>
1177 <div style="clear: both;"></div>
1178 </li>
1179 </ul>',
1180 esc_html__( 'My Lists', 'bloom' ),
1181 esc_html__( 'Provider', 'bloom' ),
1182 esc_html__( 'Subscribers', 'bloom' ),
1183 esc_html__( 'Growth Rate', 'bloom' )
1184 );
1185 }
1186
1187 $output .= '<ul class="et_dashboard_table_contents">';
1188 }
1189
1190 $total_subscribers += $list_data['subscribers_count'];
1191
1192 $unsorted_array[] = array(
1193 'name' => $list_data['name'],
1194 'service' => $service,
1195 'count' => $list_data['subscribers_count'],
1196 'growth' => $list_data['growth_week'],
1197 );
1198
1199 $optins_count++;
1200 }
1201 }
1202 }
1203 }
1204 }
1205
1206 if ( ! empty( $unsorted_array ) ) {
1207 $order = 'service' == $orderby ? SORT_ASC : SORT_DESC;
1208
1209 $sorted_array = $this->sort_array( $unsorted_array, $orderby, $order );
1210
1211 foreach ( $sorted_array as $single_list ) {
1212 $output .= sprintf(
1213 '<li class="et_dashboard_optins_item et_dashboard_parent_item">
1214 <div class="et_dashboard_table_name et_dashboard_table_column">%1$s</div>
1215 <div class="et_dashboard_table_conversions et_dashboard_table_column">%2$s</div>
1216 <div class="et_dashboard_table_rate et_dashboard_table_column">%3$s</div>
1217 <div class="et_dashboard_table_impressions et_dashboard_table_column">%4$s/%5$s</div>
1218 <div style="clear: both;"></div>
1219 </li>',
1220 esc_html( $single_list['name'] ),
1221 esc_html( $single_list['service'] ),
1222 'ontraport' == $single_list['service'] ? esc_html__( 'n/a', 'bloom' ) : esc_html( $single_list['count'] ),
1223 esc_html( $single_list['growth'] ),
1224 esc_html__( 'week', 'bloom' )
1225 );
1226 }
1227 }
1228
1229 if ( 0 < $optins_count ) {
1230 $output .= sprintf(
1231 '<li class="et_dashboard_optins_item_bottom_row">
1232 <div class="et_dashboard_table_name et_dashboard_table_column"></div>
1233 <div class="et_dashboard_table_conversions et_dashboard_table_column"></div>
1234 <div class="et_dashboard_table_rate et_dashboard_table_column">%1$s</div>
1235 <div class="et_dashboard_table_impressions et_dashboard_table_column">%2$s/%3$s</div>
1236 </li>',
1237 esc_html( $total_subscribers ),
1238 esc_html( $this->calculate_growth_rate( 'all' ) ),
1239 esc_html__( 'week', 'bloom' )
1240 );
1241 $output .= '</ul>';
1242 }
1243
1244 return $output;
1245 }
1246
1247 /**
1248 * Calculates the conversion rate for the optin
1249 * Can calculate rate for removed/existing optins and for particular pages.
1250 * @return int
1251 */
1252 function conversion_rate( $optin_id, $con_data = '0', $imp_data = '0', $page_id = 'all' ) {
1253 $conversion_rate = 0;
1254
1255 $current_conversion = '0' === $con_data ? $this->stats_count( $optin_id, 'con', $page_id ) : $con_data;
1256 $current_impression = '0' === $imp_data ? $this->stats_count( $optin_id, 'imp', $page_id ) : $imp_data;
1257
1258 if ( 0 < $current_impression ) {
1259 $conversion_rate = ( $current_conversion * 100 )/$current_impression;
1260 }
1261
1262 $conversion_rate_output = round( $conversion_rate, 1 );
1263
1264 return $conversion_rate_output;
1265 }
1266
1267 /**
1268 * Calculates the conversions/impressions count for the optin
1269 * Can calculate conversions for particular pages.
1270 * @return int
1271 */
1272 function stats_count( $optin_id, $type = 'imp', $page_id = 'all' ) {
1273 global $wpdb;
1274
1275 $stats_count = 0;
1276 $optin_id = 'all' == $optin_id ? '*' : $optin_id;
1277
1278 $table_name = $wpdb->prefix . 'et_bloom_stats';
1279
1280 if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) == $table_name ) {
1281 // construct sql query to get all the conversions from db
1282 $sql = "SELECT COUNT(*) FROM $table_name WHERE record_type = %s AND optin_id = %s";
1283 $sql_args = array(
1284 sanitize_text_field( $type ),
1285 sanitize_text_field( $optin_id )
1286 );
1287
1288 if ( 'all' !== $page_id ) {
1289 $sql .= " AND page_id = %s";
1290 $sql_args[] = sanitize_text_field( $page_id );
1291 }
1292
1293 // cache the data from conversions table
1294 $stats_count = $wpdb->get_var( $wpdb->prepare( $sql, $sql_args ) );
1295 }
1296
1297 return $stats_count;
1298 }
1299
1300 function get_conversions() {
1301 global $wpdb;
1302 $conversions = array();
1303
1304 $table_name = $wpdb->prefix . 'et_bloom_stats';
1305
1306 if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) == $table_name ) {
1307 // construct sql query to get all the conversions from db
1308 $sql = "SELECT * FROM $table_name WHERE record_type = 'con' ORDER BY record_date DESC";
1309
1310 // cache the data from conversions table
1311 $conversions = $wpdb->get_results( $sql, ARRAY_A );
1312 }
1313
1314 return $conversions;
1315 }
1316
1317 function get_all_stats_pages() {
1318 global $wpdb;
1319
1320 $all_pages = array();
1321
1322 $table_name = $wpdb->prefix . 'et_bloom_stats';
1323
1324 if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) == $table_name ) {
1325 // construct sql query to get all the conversions from db
1326 $sql = "SELECT DISTINCT page_id FROM $table_name";
1327
1328 // cache the data from conversions table
1329 $all_pages = $wpdb->get_results( $sql, ARRAY_A );
1330 }
1331
1332 return $all_pages;
1333 }
1334
1335 function get_unique_optins_by_page( $page_id ) {
1336 global $wpdb;
1337
1338 $all_optins = array();
1339 $all_optins_final = array();
1340
1341 $table_name = $wpdb->prefix . 'et_bloom_stats';
1342
1343 if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) == $table_name ) {
1344 // construct sql query to get all the conversions from db
1345 $sql = "SELECT DISTINCT optin_id FROM $table_name where page_id = %s";
1346 $sql_args = array( sanitize_text_field( $page_id ) );
1347
1348 // cache the data from conversions table
1349 $all_optins = $wpdb->get_results( $wpdb->prepare( $sql, $sql_args ), ARRAY_A );
1350 }
1351 if ( ! empty( $all_optins ) ) {
1352 foreach( $all_optins as $optin ) {
1353 $all_optins_final[] = $optin['optin_id'];
1354 }
1355 }
1356 return $all_optins_final;
1357 }
1358
1359 /**
1360 * Calculates growth rate of the list. list_id should be provided in following format: <service>_<list_id>
1361 * @return int
1362 */
1363 function calculate_growth_rate( $list_id ) {
1364 $list_id = 'all' == $list_id ? '' : $list_id;
1365
1366 $stats = $this->generate_stats_by_period( 28, 'day', $this->get_conversions(), $list_id );
1367 $total_subscribers = $stats['total_subscribers_28'];
1368 $oldest_record = -1;
1369
1370 for ( $i = 28; $i > 0; $i-- ) {
1371 if ( !empty( $stats[$i] ) ) {
1372 if ( -1 === $oldest_record ) {
1373 $oldest_record = $i;
1374 }
1375 }
1376 }
1377
1378 if ( -1 === $oldest_record ) {
1379 $growth_rate = 0;
1380 } else {
1381 $weeks_count = round( ( $oldest_record ) / 7, 0 );
1382 $weeks_count = 0 == $weeks_count ? 1 : $weeks_count;
1383 $growth_rate = round( $total_subscribers / $weeks_count, 0 );
1384 }
1385
1386 return $growth_rate;
1387 }
1388
1389 /**
1390 * Calculates all the subscribers using data from accounts
1391 * @return string
1392 */
1393 function calculate_subscribers( $period, $service = '', $account_name = '', $list_id = '' ) {
1394 $options_array = ET_Bloom::get_bloom_options();
1395 $subscribers_count = 0;
1396
1397 if ( 'all' === $period ) {
1398 if ( ! empty( $options_array['accounts']) ) {
1399 foreach ( $options_array['accounts'] as $service => $accounts ) {
1400 foreach ( $accounts as $name => $details ) {
1401 if ( ! empty( $details['lists'] ) ) {
1402 foreach( $details['lists'] as $id => $list_details ) {
1403 if ( ! empty( $list_details['subscribers_count'] ) ) {
1404 $subscribers_count += $list_details['subscribers_count'];
1405 }
1406 }
1407 }
1408 }
1409 }
1410 }
1411 }
1412
1413 return $this->get_compact_number( $subscribers_count );
1414 }
1415
1416 /**
1417 * Generates output for the lists stats graph.
1418 */
1419 function generate_lists_stats_graph( $period, $day_or_month, $list_id = '' ) {
1420 if ( ! current_user_can( 'manage_options' ) ) {
1421 die( -1 );
1422 }
1423
1424 $all_stats_rows = $this->get_conversions();
1425
1426 $stats = $this->generate_stats_by_period( $period, $day_or_month, $all_stats_rows, $list_id );
1427
1428 $output = $this->generate_stats_graph_output( $period, $day_or_month, $stats );
1429
1430 return $output;
1431 }
1432
1433 /**
1434 * Generates stats array by specified period and using provided data.
1435 * @return array
1436 */
1437 function generate_stats_by_period( $period, $day_or_month, $input_data, $list_id = '' ) {
1438 $subscribers = array();
1439
1440 $j = 0;
1441 $count_subscribers = 0;
1442
1443 for( $i = 1; $i <= $period; $i++ ) {
1444 if ( array_key_exists( $j, $input_data ) ) {
1445 $count_subtotal = 1;
1446
1447 while ( array_key_exists( $j, $input_data ) && strtotime( 'now' ) <= strtotime( sprintf( '+ %d %s', $i, 'day' == $day_or_month ? 'days' : 'month' ), strtotime( $input_data[ $j ][ 'record_date' ] ) ) ) {
1448
1449 if ( '' === $list_id || ( '' !== $list_id && $list_id === $input_data[$j]['list_id'] ) ) {
1450 $subscribers[$i]['subtotal'] = $count_subtotal++;
1451
1452 $count_subscribers++;
1453
1454 if ( array_key_exists( $i, $subscribers ) && array_key_exists( $input_data[$j]['list_id'], $subscribers[$i] ) ) {
1455 $subscribers[$i][$input_data[$j]['list_id']]['count']++;
1456 } else {
1457 $subscribers[$i][$input_data[$j]['list_id']]['count'] = 1;
1458 }
1459 }
1460
1461 $j++;
1462 }
1463 }
1464
1465 // Add total counts for each period into array
1466 if ( 'day' == $day_or_month ) {
1467 if ( $i == $period ) {
1468 $subscribers[ 'total_subscribers_' . $period ] = $count_subscribers;
1469 }
1470 } else {
1471 if ( $i == 12 ) {
1472 $subscribers[ 'total_subscribers_12' ] = $count_subscribers;
1473 }
1474 }
1475 }
1476
1477 return $subscribers;
1478 }
1479
1480 /**
1481 * Generated the output for lists graph. Period and data array are required
1482 * @return string
1483 */
1484 function generate_stats_graph_output( $period, $day_or_month, $data ) {
1485 $result = '<div class="et_dashboard_lists_stats_graph_container">';
1486 $result .= sprintf(
1487 '<ul class="et_bloom_graph_%1$s et_bloom_graph">',
1488 esc_attr( $period )
1489 );
1490 $bars_count = 0;
1491
1492 for ( $i = 1; $i <= $period ; $i++ ) {
1493 $result .= sprintf( '<li%1$s>',
1494 $period == $i ? ' class="et_bloom_graph_last"' : ''
1495 );
1496
1497 if ( array_key_exists( $i, $data ) ) {
1498 $result .= sprintf( '<div value="%1$s" class="et_bloom_graph_bar">',
1499 esc_attr( $data[$i]['subtotal'] )
1500 );
1501
1502 $bars_count++;
1503
1504 $result .= '</div>';
1505 } else {
1506 $result .= '<div value="0"></div>';
1507 }
1508
1509 $result .= '</li>';
1510 }
1511
1512 $result .= '</ul>';
1513
1514 if ( 0 < $bars_count ) {
1515 $per_day = round( $data['total_subscribers_' . $period] / $bars_count, 0 );
1516 } else {
1517 $per_day = 0;
1518 }
1519
1520 $result .= sprintf(
1521 '<div class="et_bloom_overall">
1522 <span class="total_signups">%1$s | </span>
1523 <span class="signups_period">%2$s</span>
1524 </div>',
1525 sprintf(
1526 '%1$s %2$s',
1527 esc_html( $data['total_subscribers_' . $period] ),
1528 esc_html__( 'New Signups', 'bloom' )
1529 ),
1530 sprintf(
1531 '%1$s %2$s %3$s',
1532 esc_html( $per_day ),
1533 esc_html__( 'Per', 'bloom' ),
1534 'day' == $day_or_month ? esc_html__( 'Day', 'bloom' ) : esc_html__( 'Month', 'bloom' )
1535 )
1536 );
1537
1538 $result .= '</div>';
1539
1540 return $result;
1541 }
1542
1543 /**
1544 * Generates the lists stats graph and passes it to jQuery
1545 */
1546 function get_stats_graph_ajax() {
1547 if ( ! wp_verify_nonce( $_POST['bloom_stats_nonce'] , 'bloom_stats' ) ) {
1548 die( -1 );
1549 }
1550
1551 if ( ! current_user_can( 'manage_options' ) ) {
1552 die( -1 );
1553 }
1554
1555 $list_id = ! empty( $_POST['bloom_list'] ) ? sanitize_text_field( $_POST['bloom_list'] ) : '';
1556 $period = ! empty( $_POST['bloom_period'] ) ? sanitize_text_field( $_POST['bloom_period'] ) : '';
1557
1558 $day_or_month = '30' == $period ? 'day' : 'month';
1559 $list_id = 'all' == $list_id ? '' : $list_id;
1560
1561 $output = $this->generate_lists_stats_graph( $period, $day_or_month, $list_id );
1562
1563 die( $output );
1564 }
1565
1566 /**
1567 * Generates the optins stats table and passes it to jQuery
1568 */
1569 function refresh_optins_stats_table() {
1570 if ( ! wp_verify_nonce( $_POST['bloom_stats_nonce'] , 'bloom_stats' ) ) {
1571 die( -1 );
1572 }
1573
1574 if ( ! current_user_can( 'manage_options' ) ) {
1575 die( -1 );
1576 }
1577
1578 $orderby = ! empty( $_POST['bloom_orderby'] ) ? sanitize_text_field( $_POST['bloom_orderby'] ) : '';
1579 $table = ! empty( $_POST['bloom_stats_table'] ) ? sanitize_text_field( $_POST['bloom_stats_table'] ) : '';
1580
1581 $output = '';
1582 if ( 'optins' === $table ) {
1583 $output = $this->generate_optins_stats_table( $orderby );
1584 } else if ( 'lists' === $table ) {
1585 $output = $this->generate_lists_stats_table( $orderby );
1586 }
1587
1588 die( $output );
1589 }
1590
1591 /**
1592 * Converts number >1000 into compact numbers like 1k
1593 */
1594 public static function get_compact_number( $full_number ) {
1595 if ( 1000000 <= $full_number ) {
1596 $full_number = floor( $full_number / 100000 ) / 10;
1597 $full_number .= 'Mil';
1598 } elseif ( 1000 < $full_number ) {
1599 $full_number = floor( $full_number / 100 ) / 10;
1600 $full_number .= 'k';
1601 }
1602
1603 return $full_number;
1604 }
1605
1606 /**
1607 * Converts compact numbers like 1k into full numbers like 1000
1608 */
1609 public static function get_full_number( $compact_number ) {
1610 if ( false !== strrpos( $compact_number, 'k' ) ) {
1611 $compact_number = floatval( str_replace( 'k', '', $compact_number ) ) * 1000;
1612 }
1613 if ( false !== strrpos( $compact_number, 'Mil' ) ) {
1614 $compact_number = floatval( str_replace( 'Mil', '', $compact_number ) ) * 1000000;
1615 }
1616
1617 return $compact_number;
1618 }
1619
1620 /**
1621 * Generates the fields set for new account based on service and passes it to jQuery
1622 */
1623 function generate_new_account_fields() {
1624 if ( ! wp_verify_nonce( $_POST['accounts_tab_nonce'] , 'accounts_tab' ) ) {
1625 die( -1 );
1626 }
1627
1628 if ( ! current_user_can( 'manage_options' ) ) {
1629 die( -1 );
1630 }
1631
1632 $service = ! empty( $_POST['bloom_service'] ) ? sanitize_text_field( $_POST['bloom_service'] ) : '';
1633
1634 if ( 'empty' == $service ) {
1635 echo '<ul class="et_dashboard_new_account_fields"><li></li></ul>';
1636 } else {
1637 $form_fields = $this->generate_new_account_form( $service );
1638
1639 printf(
1640 '<ul class="et_dashboard_new_account_fields">
1641 <li class="select et_dashboard_select_account">
1642 %3$s
1643 <button class="et_dashboard_icon authorize_service new_account_tab" data-service="%2$s">%1$s</button>
1644 <span class="spinner"></span>
1645 </li>
1646 </ul>',
1647 esc_html__( 'Authorize', 'bloom' ),
1648 esc_attr( $service ),
1649 $form_fields
1650 );
1651 }
1652
1653 die();
1654 }
1655
1656 /**
1657 * Generates the fields set for account editing form based on service and account name and passes it to jQuery
1658 */
1659 function generate_edit_account_page(){
1660 if ( ! wp_verify_nonce( $_POST['accounts_tab_nonce'] , 'accounts_tab' ) ) {
1661 die( -1 );
1662 }
1663
1664 if ( ! current_user_can( 'manage_options' ) ) {
1665 die( -1 );
1666 }
1667
1668 $edit_account = ! empty( $_POST['bloom_edit_account'] ) ? sanitize_text_field( $_POST['bloom_edit_account'] ) : '';
1669 $account_name = ! empty( $_POST['bloom_account_name'] ) ? sanitize_text_field( $_POST['bloom_account_name'] ) : '';
1670 $service = ! empty( $_POST['bloom_service'] ) ? sanitize_text_field( $_POST['bloom_service'] ) : '';
1671
1672 echo '<div id="et_dashboard_edit_account_tab">';
1673
1674 printf(
1675 '<div class="et_dashboard_row et_dashboard_new_account_row">
1676 <h1>%1$s</h1>
1677 <p>%2$s</p>
1678 </div>',
1679 ( 'true' == $edit_account )
1680 ? esc_html( $account_name )
1681 : esc_html__( 'New Account Setup', 'bloom' ),
1682 ( 'true' == $edit_account )
1683 ? esc_html__( 'You can view and re-authorize this account’s settings below', 'bloom' )
1684 : esc_html__( 'Setup a new email marketing service account below', 'bloom' )
1685 );
1686
1687 if ( 'true' == $edit_account ) {
1688 $form_fields = $this->generate_new_account_form( $service, $account_name, false );
1689
1690 printf(
1691 '<div class="et_dashboard_form et_dashboard_row">
1692 <h2>%1$s</h2>
1693 <div style="clear:both;"></div>
1694 <ul class="et_dashboard_new_account_fields et_dashboard_edit_account_fields">
1695 <li class="select et_dashboard_select_account">
1696 %2$s
1697 <button class="et_dashboard_icon authorize_service new_account_tab" data-service="%7$s" data-account_name="%4$s">%3$s</button>
1698 <span class="spinner"></span>
1699 </li>
1700 </ul>
1701 %5$s
1702 <button class="et_dashboard_icon save_account_tab" data-service="%7$s">%6$s</button>
1703 </div>',
1704 esc_html( $service ),
1705 $form_fields,
1706 esc_html__( 'Re-Authorize', 'bloom' ),
1707 esc_attr( $account_name ),
1708 $this->display_currrent_lists( $service, $account_name ),
1709 esc_html__( 'save & exit', 'bloom' ),
1710 esc_attr( $service )
1711 );
1712 } else {
1713 $providers = ET_Bloom_Email_Providers::get_providers();
1714 $additional_provider_options = '';
1715 if ( ! empty( $providers ) ) {
1716 foreach ( $providers as $provider ) {
1717 $additional_provider_options .= sprintf(
1718 '<option value="%1$s">%2$s</option>',
1719 esc_attr( $provider->slug ),
1720 esc_html( $provider->name )
1721 );
1722 }
1723 }
1724
1725 printf(
1726 '<div class="et_dashboard_form et_dashboard_row">
1727 <h2>%1$s</h2>
1728 <div style="clear:both;"></div>
1729 <ul>
1730 <li class="select et_dashboard_select_provider_new">
1731 <p>%19$s</p>
1732 <select>
1733 <option value="empty" selected>%2$s</option>
1734 <option value="mailchimp">%3$s</option>
1735 <option value="aweber">%4$s</option>
1736 <option value="constant_contact">%5$s</option>
1737 <option value="campaign_monitor">%6$s</option>
1738 <option value="madmimi">%7$s</option>
1739 <option value="icontact">%8$s</option>
1740 <option value="getresponse">%9$s</option>
1741 <option value="sendinblue">%10$s</option>
1742 <option value="mailpoet">%11$s</option>
1743 <option value="ontraport">%13$s</option>
1744 <option value="feedblitz">%14$s</option>
1745 <option value="infusionsoft">%15$s</option>
1746 <option value="hubspot">%16$s</option>
1747 <option value="salesforce">%17$s</option>
1748 %18$s
1749 </select>
1750 </li>
1751 </ul>
1752 <ul class="et_dashboard_new_account_fields"><li></li></ul>
1753 <button class="et_dashboard_icon save_account_tab">%12$s</button>
1754 </div>',
1755 esc_html__( 'New account settings', 'bloom' ),
1756 esc_html__( 'Select One...', 'bloom' ),
1757 esc_html__( 'MailChimp', 'bloom' ),
1758 esc_html__( 'AWeber', 'bloom' ),
1759 esc_html__( 'Constant Contact', 'bloom' ),
1760 esc_html__( 'Campaign Monitor', 'bloom' ),
1761 esc_html__( 'Mad Mimi', 'bloom' ),
1762 esc_html__( 'iContact', 'bloom' ),
1763 esc_html__( 'GetResponse', 'bloom' ),
1764 esc_html__( 'Sendinblue', 'bloom' ),
1765 esc_html__( 'MailPoet', 'bloom' ),
1766 esc_html__( 'save & exit', 'bloom' ),
1767 esc_html__( 'Ontraport', 'bloom' ),
1768 esc_html__( 'Feedblitz', 'bloom' ),
1769 esc_html__( 'Infusionsoft', 'bloom' ),
1770 esc_html__( 'HubSpot', 'bloom' ),
1771 esc_html__( 'SalesForce', 'bloom' ),
1772 $additional_provider_options, // #18
1773 esc_html__( 'Select Email Provider', 'bloom' )
1774 );
1775 }
1776
1777 echo '</div>';
1778
1779 die();
1780 }
1781
1782 /**
1783 * Generates the list of Lists for specific account and passes it to jQuery
1784 */
1785 function generate_current_lists() {
1786 if ( ! wp_verify_nonce( $_POST['accounts_tab_nonce'] , 'accounts_tab' ) ) {
1787 die( -1 );
1788 }
1789
1790 if ( ! current_user_can( 'manage_options' ) ) {
1791 die( -1 );
1792 }
1793
1794 $service = ! empty( $_POST['bloom_service'] ) ? sanitize_text_field( $_POST['bloom_service'] ) : '';
1795 $name = ! empty( $_POST['bloom_upd_name'] ) ? sanitize_text_field( $_POST['bloom_upd_name'] ) : '';
1796
1797 echo $this->display_currrent_lists( $service, $name );
1798
1799 die();
1800 }
1801
1802 /**
1803 * Generates the list of Lists for specific account
1804 * @return string
1805 */
1806 function display_currrent_lists( $service = '', $name = '' ) {
1807 if ( ! current_user_can( 'manage_options' ) ) {
1808 die( -1 );
1809 }
1810
1811 $options_array = ET_Bloom::get_bloom_options();
1812 $all_lists = array();
1813 $name = str_replace( array( '"', "'" ), '', stripslashes( $name ) );
1814
1815 // SalesForce has no list
1816 if ( 'salesforce' === $service ) {
1817 return '';
1818 }
1819
1820 if ( ! empty( $options_array['accounts'][$service][$name]['lists'] ) ) {
1821 foreach ( $options_array['accounts'][$service][$name]['lists'] as $id => $list_details ) {
1822 $all_lists[] = $list_details['name'];
1823 }
1824 }
1825
1826 $output = sprintf(
1827 '<div class="et_dashboard_row et_dashboard_new_account_lists">
1828 <h2>%1$s</h2>
1829 <div style="clear:both;"></div>
1830 <p>%2$s</p>
1831 </div>',
1832 esc_html__( 'Account Lists', 'bloom' ),
1833 ! empty( $all_lists )
1834 ? implode( ', ', array_map( 'esc_html', $all_lists ) )
1835 : esc_html__( 'No lists available for this account', 'bloom' )
1836 );
1837
1838 return $output;
1839 }
1840
1841 /**
1842 * Saves the account data during editing/creating account
1843 */
1844 function save_account_tab() {
1845 if ( ! wp_verify_nonce( $_POST['accounts_tab_nonce'] , 'accounts_tab' ) ) {
1846 die( -1 );
1847 }
1848
1849 if ( ! current_user_can( 'manage_options' ) ) {
1850 die( -1 );
1851 }
1852
1853 $service = ! empty( $_POST['bloom_service'] ) ? sanitize_text_field( $_POST['bloom_service'] ) : '';
1854 $name = ! empty( $_POST['bloom_account_name'] ) ? sanitize_text_field( $_POST['bloom_account_name'] ) : '';
1855
1856 $options_array = ET_Bloom::get_bloom_options();
1857
1858 if ( ! isset( $options_array['accounts'][$service][$name] ) ) {
1859 $this->update_account( $service, $name, array(
1860 'lists' => array(),
1861 'is_authorized' => 'false',
1862 ) );
1863 }
1864
1865 die();
1866 }
1867
1868 /**
1869 * Generates and displays the table with all accounts for Accounts tab
1870 */
1871 function display_accounts_table(){
1872 if ( ! current_user_can( 'manage_options' ) ) {
1873 die( -1 );
1874 }
1875
1876 $options_array = ET_Bloom::get_bloom_options();
1877
1878 echo '<div class="et_dashboard_accounts_content">';
1879 if( ! empty( $options_array['accounts'] ) ) {
1880 foreach ( $options_array['accounts'] as $service => $details ) {
1881 if ( ! empty( $details ) ) {
1882 $optins_count = 0;
1883 $output = '';
1884 printf(
1885 '<div class="et_dashboard_row et_dashboard_accounts_title">
1886 <span class="et_dashboard_service_logo_%1$s"></span>
1887 </div>',
1888 esc_attr( $service )
1889 );
1890 foreach ( $details as $account_name => $value ) {
1891 if ( 0 === $optins_count ) {
1892 $output .= sprintf(
1893 '<div class="et_dashboard_optins_list">
1894 <ul>
1895 <li>
1896 <div class="et_dashboard_table_acc_name et_dashboard_table_column et_dashboard_table_header">%1$s</div>
1897 <div class="et_dashboard_table_subscribers et_dashboard_table_column et_dashboard_table_header">%2$s</div>
1898 <div class="et_dashboard_table_growth_rate et_dashboard_table_column et_dashboard_table_header">%3$s</div>
1899 <div class="et_dashboard_table_actions et_dashboard_table_column"></div>
1900 <div style="clear: both;"></div>
1901 </li>',
1902 esc_html__( 'Account name', 'bloom' ),
1903 esc_html__( 'Subscribers', 'bloom' ),
1904 esc_html__( 'Growth rate', 'bloom' )
1905 );
1906 }
1907
1908 // Force "faux authorized" for SalesForce, since it is web-to-lead based
1909 if ( 'salesforce' === $service ) {
1910 $value['is_authorized'] = 'true';
1911 }
1912
1913 $output .= sprintf(
1914 '<li class="et_dashboard_optins_item" data-account_name="%1$s" data-service="%2$s">
1915 <div class="et_dashboard_table_acc_name et_dashboard_table_column">%3$s</div>
1916 <div class="et_dashboard_table_subscribers et_dashboard_table_column"></div>
1917 <div class="et_dashboard_table_growth_rate et_dashboard_table_column"></div>',
1918 esc_attr( $account_name ),
1919 esc_attr( $service ),
1920 esc_html( $account_name )
1921 );
1922
1923 $output .= sprintf( '
1924 <div class="et_dashboard_table_actions et_dashboard_table_column">
1925 <span class="et_dashboard_icon_edit_account et_optin_button et_dashboard_icon" title="%8$s" data-account_name="%1$s" data-service="%2$s"></span>
1926 <span class="et_dashboard_icon_delete et_optin_button et_dashboard_icon" title="%4$s"><span class="et_dashboard_confirmation">%5$s</span></span>
1927 %3$s
1928 <span class="et_dashboard_icon_indicator_%7$s et_optin_button et_dashboard_icon" title="%6$s"></span>
1929 </div>
1930 <div style="clear: both;"></div>
1931 </li>',
1932 esc_attr( $account_name ),
1933 esc_attr( $service ),
1934 ( isset( $value['is_authorized'] ) && 'true' == $value['is_authorized'] )
1935 ? sprintf( '
1936 <span class="et_dashboard_icon_update_lists et_optin_button et_dashboard_icon" title="%1$s" data-account_name="%2$s" data-service="%3$s">
1937 <span class="spinner"></span>
1938 </span>',
1939 esc_attr__( 'Update Lists', 'bloom' ),
1940 esc_attr( $account_name ),
1941 esc_attr( $service )
1942 )
1943 : '',
1944 esc_attr__( 'Remove account', 'bloom' ),
1945 sprintf(
1946 '%1$s<span class="et_dashboard_confirm_delete" data-optin_id="%4$s" data-remove_account="true">%2$s</span><span class="et_dashboard_cancel_delete">%3$s</span>',
1947 esc_html__( 'Remove this account from list?', 'bloom' ),
1948 esc_html__( 'Yes', 'bloom' ),
1949 esc_html__( 'No', 'bloom' ),
1950 esc_attr( $account_name )
1951 ), //#5
1952 ( isset( $value['is_authorized'] ) && 'true' == $value['is_authorized'] )
1953 ? esc_html__( 'Authorized', 'bloom' )
1954 : esc_html__( 'Not Authorized', 'bloom' ),
1955 ( isset( $value['is_authorized'] ) && 'true' == $value['is_authorized'] )
1956 ? 'check'
1957 : 'dot',
1958 esc_html__( 'Edit account', 'bloom' )
1959 );
1960
1961 if ( isset( $value['lists'] ) && ! empty( $value['lists'] ) ) {
1962 foreach ( $value['lists'] as $id => $list ) {
1963 $output .= sprintf( '
1964 <li class="et_dashboard_lists_row">
1965 <div class="et_dashboard_table_acc_name et_dashboard_table_column">%1$s</div>
1966 <div class="et_dashboard_table_subscribers et_dashboard_table_column">%2$s</div>
1967 <div class="et_dashboard_table_growth_rate et_dashboard_table_column">%3$s / %4$s</div>
1968 <div class="et_dashboard_table_actions et_dashboard_table_column"></div>
1969 </li>',
1970 esc_html( $list['name'] ),
1971 'ontraport' == $service ? esc_html__( 'n/a', 'bloom' ) : esc_html( $list['subscribers_count'] ),
1972 esc_html( $list['growth_week'] ),
1973 esc_html__( 'week', 'bloom' )
1974 );
1975 }
1976 } else {
1977 $output .= sprintf(
1978 '<li class="et_dashboard_lists_row">
1979 <div class="et_dashboard_table_acc_name et_dashboard_table_column">%1$s</div>
1980 <div class="et_dashboard_table_subscribers et_dashboard_table_column"></div>
1981 <div class="et_dashboard_table_growth_rate et_dashboard_table_column"></div>
1982 <div class="et_dashboard_table_actions et_dashboard_table_column"></div>
1983 </li>',
1984 esc_html__( 'No lists available', 'bloom' )
1985 );
1986 }
1987
1988 $optins_count++;
1989 }
1990
1991 echo $output;
1992 echo '
1993 </ul>
1994 </div>';
1995 }
1996 }
1997 }
1998 echo '</div>';
1999 }
2000
2001 /**
2002 * Displays tables of Active and Inactive optins on homepage
2003 */
2004 function display_home_tab_tables() {
2005 if ( ! current_user_can( 'manage_options' ) ) {
2006 die( -1 );
2007 }
2008
2009 $options_array = ET_Bloom::get_bloom_options();
2010
2011 echo '<div class="et_dashboard_home_tab_content">';
2012
2013 $this->generate_optins_list( $options_array, 'active' );
2014
2015 $this->generate_optins_list( $options_array, 'inactive' );
2016
2017 echo '</div>';
2018
2019 }
2020
2021 /**
2022 * Generates tables of Active and Inactive optins on homepage and passes it to jQuery
2023 */
2024 function home_tab_tables() {
2025 if ( ! wp_verify_nonce( $_POST['home_tab_nonce'] , 'home_tab' ) ) {
2026 die( -1 );
2027 }
2028
2029 if ( ! current_user_can( 'manage_options' ) ) {
2030 die( -1 );
2031 }
2032
2033 $this->display_home_tab_tables();
2034 die();
2035 }
2036
2037 /**
2038 * Generates accounts tables and passes it to jQuery
2039 */
2040 function reset_accounts_table() {
2041 if ( ! wp_verify_nonce( $_POST['accounts_tab_nonce'] , 'accounts_tab' ) ) {
2042 die( -1 );
2043 }
2044
2045 if ( ! current_user_can( 'manage_options' ) ) {
2046 die( -1 );
2047 }
2048
2049 $this->display_accounts_table();
2050 die();
2051 }
2052
2053 /**
2054 * Generates optins table for homepage. Can generate table for active or inactive optins
2055 */
2056 function generate_optins_list( $options_array = array(), $status = 'active' ) {
2057 $optins_count = 0;
2058 $output = '';
2059 $total_impressions = 0;
2060 $total_conversions = 0;
2061
2062 foreach ( $options_array as $optin_id => $value ) {
2063 if ( isset( $value['optin_status'] ) && $status === $value['optin_status'] && empty( $value['child_of'] ) ) {
2064 $child_row = '';
2065
2066 if ( 0 === $optins_count ) {
2067
2068 $output .= sprintf(
2069 '<div class="et_dashboard_optins_list">
2070 <ul>
2071 <li>
2072 <div class="et_dashboard_table_name et_dashboard_table_column">%1$s</div>
2073 <div class="et_dashboard_table_impressions et_dashboard_table_column">%2$s</div>
2074 <div class="et_dashboard_table_conversions et_dashboard_table_column">%3$s</div>
2075 <div class="et_dashboard_table_rate et_dashboard_table_column">%4$s</div>
2076 <div class="et_dashboard_table_actions et_dashboard_table_column"></div>
2077 <div style="clear: both;"></div>
2078 </li>',
2079 esc_html__( 'Optin Name', 'bloom' ),
2080 esc_html__( 'Impressions', 'bloom' ),
2081 esc_html__( 'Conversions', 'bloom' ),
2082 esc_html__( 'Conversion Rate', 'bloom' )
2083 );
2084 }
2085
2086 if ( ! empty( $value['child_optins'] ) && 'active' == $status ) {
2087 $optins_data = array();
2088
2089 foreach( $value['child_optins'] as $id ) {
2090 $total_impressions += $impressions = $this->stats_count( $id, 'imp' );
2091 $total_conversions += $conversions = $this->stats_count( $id, 'con' );
2092
2093 $optins_data[] = array(
2094 'name' => $options_array[$id]['optin_name'],
2095 'id' => $id,
2096 'rate' => $this->conversion_rate( $id, $conversions, $impressions ),
2097 'impressions' => $impressions,
2098 'conversions' => $conversions,
2099 );
2100 }
2101
2102 $child_optins_data = $this->sort_array( $optins_data, 'rate', SORT_DESC );
2103
2104 $child_row = '<ul class="et_dashboard_child_row">';
2105
2106 foreach( $child_optins_data as $child_details ) {
2107 $child_row .= sprintf(
2108 '<li class="et_dashboard_optins_item et_dashboard_child_item" data-optin_id="%1$s">
2109 <div class="et_dashboard_table_name et_dashboard_table_column">%2$s</div>
2110 <div class="et_dashboard_table_impressions et_dashboard_table_column">%3$s</div>
2111 <div class="et_dashboard_table_conversions et_dashboard_table_column">%4$s</div>
2112 <div class="et_dashboard_table_rate et_dashboard_table_column">%5$s</div>
2113 <div class="et_dashboard_table_actions et_dashboard_table_column">
2114 <span class="et_dashboard_icon_edit et_optin_button et_dashboard_icon" title="%8$s" data-parent_id="%9$s"><span class="spinner"></span></span>
2115 <span class="et_dashboard_icon_delete et_optin_button et_dashboard_icon" title="%6$s"><span class="et_dashboard_confirmation">%7$s</span></span>
2116 </div>
2117 <div style="clear: both;"></div>
2118 </li>',
2119 esc_attr( $child_details['id'] ),
2120 esc_html( $child_details['name'] ),
2121 esc_html( $child_details['impressions'] ),
2122 esc_html( $child_details['conversions'] ),
2123 esc_html( $child_details['rate'] . '%' ), // #5
2124 esc_attr__( 'Delete Optin', 'bloom' ),
2125 sprintf(
2126 '%1$s<span class="et_dashboard_confirm_delete" data-optin_id="%4$s" data-parent_id="%5$s">%2$s</span>
2127 <span class="et_dashboard_cancel_delete">%3$s</span>',
2128 esc_html__( 'Delete this optin?', 'bloom' ),
2129 esc_html__( 'Yes', 'bloom' ),
2130 esc_html__( 'No', 'bloom' ),
2131 esc_attr( $child_details['id'] ),
2132 esc_attr( $optin_id )
2133 ),
2134 esc_attr__( 'Edit Optin', 'bloom' ),
2135 esc_attr( $optin_id ) // #9
2136 );
2137 }
2138
2139 $child_row .= sprintf(
2140 '<li class="et_dashboard_add_variant et_dashboard_optins_item">
2141 <a href="#" class="et_dashboard_add_var_button">%1$s</a>
2142 <div class="child_buttons_right">
2143 <a href="#" class="et_dashboard_start_test%5$s" data-parent_id="%4$s">%2$s</a>
2144 <a href="#" class="et_dashboard_end_test" data-parent_id="%4$s">%3$s</a>
2145 </div>
2146 </li>',
2147 esc_html__( 'Add variant', 'bloom' ),
2148 ( isset( $value['test_status'] ) && 'active' == $value['test_status'] ) ? esc_html__( 'Pause test', 'bloom' ) : esc_html__( 'Start test', 'bloom' ),
2149 esc_html__( 'End & pick winner', 'bloom' ),
2150 esc_attr( $optin_id ),
2151 ( isset( $value['test_status'] ) && 'active' == $value['test_status'] ) ? ' et_dashboard_pause_test' : ''
2152 );
2153
2154 $child_row .= '</ul>';
2155 }
2156
2157 $total_impressions += $impressions = $this->stats_count( $optin_id, 'imp' );
2158 $total_conversions += $conversions = $this->stats_count( $optin_id, 'con' );
2159
2160 $output .= sprintf(
2161 '<li class="et_dashboard_optins_item et_dashboard_parent_item" data-optin_id="%1$s">
2162 <div class="et_dashboard_table_name et_dashboard_table_column et_dashboard_icon et_dashboard_type_%13$s">%2$s</div>
2163 <div class="et_dashboard_table_impressions et_dashboard_table_column">%3$s</div>
2164 <div class="et_dashboard_table_conversions et_dashboard_table_column">%4$s</div>
2165 <div class="et_dashboard_table_rate et_dashboard_table_column">%5$s</div>
2166 <div class="et_dashboard_table_actions et_dashboard_table_column">
2167 <span class="et_dashboard_icon_edit et_optin_button et_dashboard_icon" title="%10$s"><span class="spinner"></span></span>
2168 <span class="et_dashboard_icon_delete et_optin_button et_dashboard_icon" title="%9$s"><span class="et_dashboard_confirmation">%12$s</span></span>
2169 <span class="et_dashboard_icon_duplicate duplicate_id_%1$s et_optin_button et_dashboard_icon" title="%8$s"><span class="spinner"></span></span>
2170 <span class="et_dashboard_icon_%11$s et_dashboard_toggle_status et_optin_button et_dashboard_icon%16$s" data-toggle_to="%11$s" data-optin_id="%1$s" title="%7$s"><span class="spinner"></span></span>
2171 %14$s
2172 %6$s
2173 </div>
2174 <div style="clear: both;"></div>
2175 %15$s
2176 </li>',
2177 esc_attr( $optin_id ),
2178 esc_html( $value['optin_name'] ),
2179 esc_html( $impressions ),
2180 esc_html( $conversions ),
2181 esc_html( $this->conversion_rate( $optin_id, $conversions, $impressions ) . '%' ), // #5
2182 ( 'locked' === $value['optin_type'] || 'inline' === $value['optin_type'] )
2183 ? sprintf(
2184 '<span class="et_dashboard_icon_shortcode et_optin_button et_dashboard_icon" title="%1$s" data-type="%2$s"></span>',
2185 esc_attr__( 'Generate shortcode', 'bloom' ),
2186 esc_attr( $value['optin_type'] )
2187 )
2188 : '',
2189 'active' === $status ? esc_html__( 'Make Inactive', 'bloom' ) : esc_html__( 'Make Active', 'bloom' ),
2190 esc_attr__( 'Duplicate', 'bloom' ),
2191 esc_attr__( 'Delete Optin', 'bloom' ),
2192 esc_attr__( 'Edit Optin', 'bloom' ), //#10
2193 'active' === $status ? 'inactive' : 'active',
2194 sprintf(
2195 '%1$s<span class="et_dashboard_confirm_delete" data-optin_id="%4$s">%2$s</span>
2196 <span class="et_dashboard_cancel_delete">%3$s</span>',
2197 esc_html__( 'Delete this optin?', 'bloom' ),
2198 esc_html__( 'Yes', 'bloom' ),
2199 esc_html__( 'No', 'bloom' ),
2200 esc_attr( $optin_id )
2201 ),
2202 esc_attr( $value['optin_type'] ),
2203 ( 'active' === $status )
2204 ? sprintf(
2205 '<span class="et_dashboard_icon_abtest et_optin_button et_dashboard_icon%2$s" title="%1$s"></span>',
2206 esc_attr__( 'A/B Testing', 'bloom' ),
2207 ( '' != $child_row ) ? ' active_child_optins' : ''
2208 )
2209 : '',
2210 $child_row, //#15
2211 ( 'empty' == $value['email_provider'] || ( 'custom_html' !== $value['email_provider'] && 'empty' == $value['email_list'] ) )
2212 ? ' et_bloom_no_account'
2213 : '' //#16
2214 );
2215 $optins_count++;
2216 }
2217 }
2218
2219 if ( 'active' === $status && 0 < $optins_count ) {
2220 $output .= sprintf(
2221 '<li class="et_dashboard_optins_item_bottom_row">
2222 <div class="et_dashboard_table_name et_dashboard_table_column"></div>
2223 <div class="et_dashboard_table_impressions et_dashboard_table_column">%1$s</div>
2224 <div class="et_dashboard_table_conversions et_dashboard_table_column">%2$s</div>
2225 <div class="et_dashboard_table_rate et_dashboard_table_column">%3$s</div>
2226 <div class="et_dashboard_table_actions et_dashboard_table_column"></div>
2227 </li>',
2228 esc_html( $this->get_compact_number( $total_impressions ) ),
2229 esc_html( $this->get_compact_number( $total_conversions ) ),
2230 ( 0 !== $total_impressions )
2231 ? esc_html( round( ( $total_conversions * 100 ) / $total_impressions, 1 ) . '%' )
2232 : '0%'
2233 );
2234 }
2235
2236 if ( 0 < $optins_count ) {
2237 if ( 'inactive' === $status ) {
2238 printf( '
2239 <div class="et_dashboard_row">
2240 <h1>%1$s</h1>
2241 </div>',
2242 esc_html__( 'Inactive Optins', 'bloom' )
2243 );
2244 }
2245
2246 echo $output . '</ul></div>';
2247 }
2248 }
2249
2250 function add_admin_body_class( $classes ) {
2251 return "$classes et_bloom";
2252 }
2253
2254 function register_scripts( $hook ) {
2255
2256 wp_enqueue_style( 'et-bloom-menu-icon', ET_BLOOM_PLUGIN_URI . '/css/bloom-menu.css', array(), $this->plugin_version );
2257
2258 if ( "toplevel_page_{$this->_options_pagename}" !== $hook ) {
2259 return;
2260 }
2261
2262 add_filter( 'admin_body_class', array( $this, 'add_admin_body_class' ) );
2263 wp_enqueue_script( 'et_bloom-uniform-js', ET_BLOOM_PLUGIN_URI . '/js/jquery.uniform.min.js', array( 'jquery' ), $this->plugin_version, true );
2264 wp_enqueue_style( 'et-open-sans-700', "{$this->protocol}://fonts.googleapis.com/css?family=Open+Sans:700", array(), $this->plugin_version );
2265 wp_enqueue_style( 'et-bloom-css', ET_BLOOM_PLUGIN_URI . '/css/admin.css', array(), $this->plugin_version );
2266 wp_enqueue_style( 'et_bloom-preview-css', ET_BLOOM_PLUGIN_URI . '/css/style.css', array(), $this->plugin_version );
2267 wp_enqueue_script( 'et-bloom-js', ET_BLOOM_PLUGIN_URI . '/js/admin.js', array( 'jquery' ), $this->plugin_version, true );
2268 wp_localize_script( 'et-bloom-js', 'bloom_settings', array(
2269 'bloom_nonce' => wp_create_nonce( 'bloom_nonce' ),
2270 'ajaxurl' => admin_url( 'admin-ajax.php', $this->protocol ),
2271 'reset_options' => wp_create_nonce( 'reset_options' ),
2272 'remove_option' => wp_create_nonce( 'remove_option' ),
2273 'duplicate_option' => wp_create_nonce( 'duplicate_option' ),
2274 'home_tab' => wp_create_nonce( 'home_tab' ),
2275 'toggle_status' => wp_create_nonce( 'toggle_status' ),
2276 'optin_type_title' => esc_html__( 'select optin type to begin', 'bloom' ),
2277 'shortcode_text' => esc_html__( 'Shortcode for this optin:', 'bloom' ),
2278 'get_lists' => wp_create_nonce( 'get_lists' ),
2279 'add_account' => wp_create_nonce( 'add_account' ),
2280 'accounts_tab' => wp_create_nonce( 'accounts_tab' ),
2281 'retrieve_lists' => wp_create_nonce( 'retrieve_lists' ),
2282 'ab_test' => wp_create_nonce( 'ab_test' ),
2283 'bloom_stats' => wp_create_nonce( 'bloom_stats' ),
2284 'redirect_url' => rawurlencode( admin_url( 'admin.php?page=' . $this->_options_pagename, $this->protocol ) ),
2285 'authorize_text' => esc_html__( 'Authorize', 'bloom' ),
2286 'reauthorize_text' => esc_html__( 'Re-Authorize', 'bloom' ),
2287 'no_account_name_text' => esc_html__( 'Account name is not defined', 'bloom' ),
2288 'ab_test_pause_text' => esc_html__( 'Pause test', 'bloom' ),
2289 'ab_test_start_text' => esc_html__( 'Start test', 'bloom' ),
2290 'bloom_premade_nonce' => wp_create_nonce( 'bloom_premade' ),
2291 'preview_nonce' => wp_create_nonce( 'bloom_preview' ),
2292 'no_account_text' => esc_html__( 'You Have Not Added An Email List. Before your opt-in can be activated, you must first add an account and select an email list. You can save and exit, but the opt-in will remain inactive until an account is added.', 'bloom' ),
2293 'add_account_button' => esc_html__( 'Add An Account', 'bloom' ),
2294 'save_inactive_button' => esc_html__( 'Save As Inactive', 'bloom' ),
2295 'cannot_activate_text' => esc_html__( 'You Have Not Added An Email List. Before your opt-in can be activated, you must first add an account and select an email list.', 'bloom' ),
2296 'save_settings' => wp_create_nonce( 'save_settings' ),
2297 'updates_tab' => wp_create_nonce( 'updates_tab' )
2298 ) );
2299 }
2300
2301 /**
2302 * Generates unique ID for new set of options
2303 * @return string or int
2304 */
2305 function generate_optin_id( $full_id = true ) {
2306
2307 $options_array = ET_Bloom::get_bloom_options();
2308 $form_id = (int) 0;
2309
2310 if( ! empty( $options_array ) ) {
2311 foreach ( $options_array as $key => $value) {
2312 $keys_array[] = (int) str_replace( 'optin_', '', $key );
2313 }
2314
2315 $form_id = max( $keys_array ) + 1;
2316 }
2317
2318 $result = true === $full_id ? (string) 'optin_' . $form_id : (int) $form_id;
2319
2320 return $result;
2321
2322 }
2323
2324 /**
2325 * Generates options page for specific optin ID
2326 * @return string
2327 */
2328 function reset_options_page() {
2329 if ( ! wp_verify_nonce( $_POST['reset_options_nonce'] , 'reset_options' ) ) {
2330 die( -1 );
2331 }
2332
2333 if ( ! current_user_can( 'manage_options' ) ) {
2334 die( -1 );
2335 }
2336
2337 $optin_id = ! empty( $_POST['reset_optin_id'] )
2338 ? sanitize_text_field( $_POST['reset_optin_id'] )
2339 : $this->generate_optin_id();
2340 $additional_options = '';
2341
2342 ET_Bloom::generate_options_page( $optin_id );
2343
2344 die();
2345 }
2346
2347 /**
2348 * Handles "Duplicate" button action
2349 * @return string
2350 */
2351 function duplicate_optin() {
2352 if ( ! wp_verify_nonce( $_POST['duplicate_option_nonce'] , 'duplicate_option' ) ) {
2353 die( -1 );
2354 }
2355
2356 if ( ! current_user_can( 'manage_options' ) ) {
2357 die( -1 );
2358 }
2359
2360 $duplicate_optin_id = ! empty( $_POST['duplicate_optin_id'] ) ? sanitize_text_field( $_POST['duplicate_optin_id'] ) : '';
2361 $duplicate_optin_type = ! empty( $_POST['duplicate_optin_type'] ) ? sanitize_text_field( $_POST['duplicate_optin_type'] ) : '';
2362
2363 $this->perform_option_duplicate( $duplicate_optin_id, $duplicate_optin_type, false );
2364
2365 die();
2366 }
2367
2368 /**
2369 * Handles "Add Variant" button action
2370 * @return string
2371 */
2372 function add_variant() {
2373 if ( ! wp_verify_nonce( $_POST['duplicate_option_nonce'] , 'duplicate_option' ) ) {
2374 die( -1 );
2375 }
2376
2377 if ( ! current_user_can( 'manage_options' ) ) {
2378 die( -1 );
2379 }
2380
2381 $duplicate_optin_id = ! empty( $_POST['duplicate_optin_id'] ) ? sanitize_text_field( $_POST['duplicate_optin_id'] ) : '';
2382
2383 $variant_id = $this->perform_option_duplicate( $duplicate_optin_id, '', true );
2384
2385 die( $variant_id );
2386 }
2387
2388 /**
2389 * Toggles testing status
2390 * @return void
2391 */
2392 function ab_test_actions() {
2393 if ( ! wp_verify_nonce( $_POST['ab_test_nonce'] , 'ab_test' ) ) {
2394 die( -1 );
2395 }
2396
2397 if ( ! current_user_can( 'manage_options' ) ) {
2398 die( -1 );
2399 }
2400
2401 $parent_id = ! empty( $_POST['parent_id'] ) ? sanitize_text_field( $_POST['parent_id'] ) : '';
2402 $action = ! empty( $_POST['test_action'] ) ? sanitize_text_field( $_POST['test_action'] ) : '';
2403 $options_array = ET_Bloom::get_bloom_options();
2404 $update_test_status[$parent_id] = $options_array[$parent_id];
2405
2406 switch( $action ) {
2407 case 'start' :
2408 $update_test_status[$parent_id]['test_status'] = 'active';
2409 $result = 'ok';
2410 break;
2411 case 'pause' :
2412 $update_test_status[$parent_id]['test_status'] = 'inactive';
2413 $result = 'ok';
2414 break;
2415
2416 case 'end' :
2417 $result = $this->generate_end_test_modal( $parent_id );
2418 break;
2419 }
2420
2421 ET_Bloom::update_option( $update_test_status );
2422
2423 die( $result );
2424 }
2425
2426 /**
2427 * Generates modal window for the pick winner option
2428 * @return string
2429 */
2430 function generate_end_test_modal( $parent_id ) {
2431 if ( ! current_user_can( 'manage_options' ) ) {
2432 die( -1 );
2433 }
2434
2435 $options_array = ET_Bloom::get_bloom_options();
2436 $test_optins = $options_array[$parent_id]['child_optins'];
2437 $test_optins[] = $parent_id;
2438 $output = '';
2439
2440 if ( ! empty( $test_optins ) ) {
2441 foreach( $test_optins as $id ) {
2442 $optins_data[] = array(
2443 'name' => $options_array[$id]['optin_name'],
2444 'id' => $id,
2445 'rate' => $this->conversion_rate( $id ),
2446 );
2447 }
2448
2449 $optins_data = $this->sort_array( $optins_data, 'rate', SORT_DESC );
2450
2451 $table = sprintf(
2452 '<div class="end_test_table">
2453 <ul data-optins_set="%3$s" data-parent_id="%4$s">
2454 <li class="et_test_table_header">
2455 <div class="et_dashboard_table_column">%1$s</div>
2456 <div class="et_dashboard_table_column et_test_conversion">%2$s</div>
2457 </li>',
2458 esc_html__( 'Optin name', 'bloom' ),
2459 esc_html__( 'Conversion rate', 'bloom' ),
2460 esc_attr( implode( '#', $test_optins ) ),
2461 esc_attr( $parent_id )
2462 );
2463
2464 foreach( $optins_data as $single ) {
2465 $table .= sprintf(
2466 '<li class="et_dashboard_content_row" data-optin_id="%1$s">
2467 <div class="et_dashboard_table_column">%2$s</div>
2468 <div class="et_dashboard_table_column et_test_conversion">%3$s</div>
2469 </li>',
2470 esc_attr( $single['id'] ),
2471 esc_html( $single['name'] ),
2472 esc_html( $single['rate'] . '%' )
2473 );
2474 }
2475
2476 $table .= '</ul></div>';
2477
2478 $output = sprintf(
2479 '<div class="et_dashboard_networks_modal et_dashboard_end_test">
2480 <div class="et_dashboard_inner_container">
2481 <div class="et_dashboard_modal_header">
2482 <span class="modal_title">%1$s</span>
2483 <span class="et_dashboard_close"></span>
2484 </div>
2485 <div class="dashboard_icons_container">
2486 %3$s
2487 </div>
2488 <div class="et_dashboard_modal_footer">
2489 <a href="#" class="et_dashboard_ok et_dashboard_warning_button">%2$s</a>
2490 </div>
2491 </div>
2492 </div>',
2493 esc_html__( 'Choose an optin', 'bloom' ),
2494 esc_html__( 'cancel', 'bloom' ),
2495 $table
2496 );
2497 }
2498
2499 return $output;
2500 }
2501
2502 /**
2503 * Handles "Pick winner" function. Replaces the content of parent optin with the content of "winning" optin.
2504 * Updates options and stats accordingly.
2505 * @return void
2506 */
2507 function pick_winner_optin() {
2508 if ( ! wp_verify_nonce( $_POST['remove_option_nonce'] , 'remove_option' ) ) {
2509 die( -1 );
2510 }
2511
2512 if ( ! current_user_can( 'manage_options' ) ) {
2513 die( -1 );
2514 }
2515
2516 $winner_id = ! empty( $_POST['winner_id'] ) ? sanitize_text_field( $_POST['winner_id'] ) : '';
2517 $optins_set = ! empty( $_POST['optins_set'] ) ? sanitize_text_field( $_POST['optins_set'] ) : '';
2518 $parent_id = ! empty( $_POST['parent_id'] ) ? sanitize_text_field( $_POST['parent_id'] ) : '';
2519
2520 $options_array = ET_Bloom::get_bloom_options();
2521 $temp_array = $options_array[$winner_id];
2522
2523 $temp_array['test_status'] = 'inactive';
2524 $temp_array['child_optins'] = array();
2525 $temp_array['child_of'] = '';
2526 $temp_array['next_optin'] = '-1';
2527 $temp_array['display_on'] = $options_array[$parent_id]['display_on'];
2528 $temp_array['post_types'] = $options_array[$parent_id]['post_types'];
2529 $temp_array['post_categories'] = $options_array[$parent_id]['post_categories'];
2530 $temp_array['pages_exclude'] = $options_array[$parent_id]['pages_exclude'];
2531 $temp_array['pages_include'] = $options_array[$parent_id]['pages_include'];
2532 $temp_array['posts_exclude'] = $options_array[$parent_id]['posts_exclude'];
2533 $temp_array['posts_include'] = $options_array[$parent_id]['posts_include'];
2534 $temp_array['email_provider'] = $options_array[$parent_id]['email_provider'];
2535 $temp_array['account_name'] = $options_array[$parent_id]['account_name'];
2536 $temp_array['email_list'] = $options_array[$parent_id]['email_list'];
2537 $temp_array['custom_html'] = $options_array[$parent_id]['custom_html'];
2538
2539 $updated_array[$parent_id] = $temp_array;
2540
2541 if ( $parent_id != $winner_id ){
2542 $this->update_stats_for_winner( $parent_id, $winner_id );
2543 }
2544
2545 $optins_set = explode( '#', $optins_set );
2546 foreach ( $optins_set as $optin_id ) {
2547 if ( $parent_id != $optin_id ) {
2548 $this->perform_optin_removal( $optin_id, false, '', '', false );
2549 }
2550 }
2551
2552 ET_Bloom::update_option( $updated_array );
2553 }
2554
2555 /**
2556 * Updates stats table when A/B testing finished winner optin selected
2557 * @return void
2558 */
2559 function update_stats_for_winner( $optin_id, $winner_id ) {
2560 if ( ! current_user_can( 'manage_options' ) ) {
2561 die( -1 );
2562 }
2563
2564 global $wpdb;
2565
2566 $table_name = $wpdb->prefix . 'et_bloom_stats';
2567
2568 $optin_id = sanitize_text_field( $optin_id );
2569 $winner_id = sanitize_text_field( $winner_id );
2570
2571 $this->remove_optin_from_db( $optin_id );
2572
2573 $sql = "UPDATE $table_name SET optin_id = %s WHERE optin_id = %s AND removed_flag <> 1";
2574
2575 $sql_args = array(
2576 $optin_id,
2577 $winner_id
2578 );
2579
2580 $wpdb->query( $wpdb->prepare( $sql, $sql_args ) );
2581 }
2582
2583 /**
2584 * Performs duplicating of optin. Can duplicate parent optin as well as child optin based on $is_child parameter
2585 * @return string
2586 */
2587 function perform_option_duplicate( $duplicate_optin_id, $duplicate_optin_type = '', $is_child = false ) {
2588 if ( ! current_user_can( 'manage_options' ) ) {
2589 die( -1 );
2590 }
2591
2592 $new_optin_id = $this->generate_optin_id();
2593 $suffix = true == $is_child ? '_child' : '_copy';
2594
2595 if ( '' !== $duplicate_optin_id ) {
2596 $options_array = ET_Bloom::get_bloom_options();
2597 $new_option[$new_optin_id] = $options_array[$duplicate_optin_id];
2598 $new_option[$new_optin_id]['optin_name'] = $new_option[$new_optin_id]['optin_name'] . $suffix;
2599 $new_option[$new_optin_id]['optin_status'] = 'active';
2600
2601 if ( true == $is_child ) {
2602 $new_option[$new_optin_id]['child_of'] = $duplicate_optin_id;
2603 $updated_optin[$duplicate_optin_id] = $options_array[$duplicate_optin_id];
2604 unset( $new_option[$new_optin_id]['child_optins'] );
2605 $updated_optin[$duplicate_optin_id]['child_optins'] = isset( $options_array[$duplicate_optin_id]['child_optins'] ) ? array_merge( $options_array[$duplicate_optin_id]['child_optins'], array( $new_optin_id ) ) : array( $new_optin_id );
2606 ET_Bloom::update_option( $updated_optin );
2607 } else {
2608 $new_option[$new_optin_id]['optin_type'] = $duplicate_optin_type;
2609 unset( $new_option[$new_optin_id]['child_optins'] );
2610 }
2611
2612 if ( 'breakout_edge' === $new_option[$new_optin_id]['edge_style'] && 'pop_up' !== $duplicate_optin_type ) {
2613 $new_option[$new_optin_id]['edge_style'] = 'basic_edge';
2614 }
2615
2616 if ( ! ( 'flyin' === $duplicate_optin_type || 'pop_up' === $duplicate_optin_type ) ) {
2617 unset( $new_option[$new_optin_id]['display_on'] );
2618 }
2619
2620 ET_Bloom::update_option( $new_option );
2621
2622 return $new_optin_id;
2623 }
2624 }
2625
2626 /**
2627 * Handles optin/account removal function called via jQuery
2628 */
2629 function remove_optin() {
2630 if ( ! wp_verify_nonce( $_POST['remove_option_nonce'] , 'remove_option' ) ) {
2631 die( -1 );
2632 }
2633
2634 if ( ! current_user_can( 'manage_options' ) ) {
2635 die( -1 );
2636 }
2637
2638 $optin_id = ! empty( $_POST['remove_optin_id'] ) ? sanitize_text_field( $_POST['remove_optin_id'] ) : '';
2639 $is_account = ! empty( $_POST['is_account'] ) ? sanitize_text_field( $_POST['is_account'] ) : '';
2640 $service = ! empty( $_POST['service'] ) ? sanitize_text_field( $_POST['service'] ) : '';
2641 $parent_id = ! empty( $_POST['parent_id'] ) ? sanitize_text_field( $_POST['parent_id'] ) : '';
2642
2643 $this->perform_optin_removal( $optin_id, $is_account, $service, $parent_id );
2644
2645 die();
2646 }
2647
2648 /**
2649 * Performs removal of optin or account. Can remove parent optin, child optin or account
2650 * @return void
2651 */
2652 function perform_optin_removal( $optin_id, $is_account = false, $service = '', $parent_id = '', $remove_child = true ) {
2653 if ( ! current_user_can( 'manage_options' ) ) {
2654 die( -1 );
2655 }
2656
2657 $options_array = ET_Bloom::get_bloom_options();
2658
2659 if ( '' !== $optin_id ) {
2660 if ( 'true' == $is_account ) {
2661 if ( '' !== $service ) {
2662 if ( isset( $options_array['accounts'][$service][$optin_id] ) ){
2663 unset( $options_array['accounts'][$service][$optin_id] );
2664
2665 foreach ( $options_array as $id => $details ) {
2666 if ( 'accounts' !== $id ) {
2667 if ( $optin_id == $details['account_name'] ) {
2668 $options_array[$id]['email_provider'] = 'empty';
2669 $options_array[$id]['account_name'] = 'empty';
2670 $options_array[$id]['email_list'] = 'empty';
2671 $options_array[$id]['optin_status'] = 'inactive';
2672 }
2673 }
2674 }
2675
2676 ET_Bloom::update_option( $options_array );
2677 }
2678 }
2679 } else {
2680 if ( '' != $parent_id ) {
2681 $updated_array[$parent_id] = $options_array[$parent_id];
2682 $new_child_optins = array();
2683
2684 foreach( $updated_array[$parent_id]['child_optins'] as $child ) {
2685 if ( $child != $optin_id ) {
2686 $new_child_optins[] = $child;
2687 }
2688 }
2689
2690 $updated_array[$parent_id]['child_optins'] = $new_child_optins;
2691
2692 // change test status to 'inactive' if there is no child options after removal.
2693 if ( empty( $new_child_optins ) ) {
2694 $updated_array[$parent_id]['test_status'] = 'inactive';
2695 }
2696
2697 ET_Bloom::update_option( $updated_array );
2698 } else {
2699 if ( ! empty( $options_array[$optin_id]['child_optins'] ) && true == $remove_child ) {
2700 foreach( $options_array[$optin_id]['child_optins'] as $single_optin ) {
2701 ET_Bloom::remove_option( $single_optin );
2702 $this->remove_optin_from_db( $single_optin );
2703 }
2704 }
2705 }
2706
2707 ET_Bloom::remove_option( $optin_id );
2708 $this->remove_optin_from_db( $optin_id );
2709 }
2710 }
2711 }
2712
2713 /**
2714 * Remove the optin data from stats tabel.
2715 */
2716 function remove_optin_from_db( $optin_id ) {
2717 if ( '' !== $optin_id ) {
2718 global $wpdb;
2719
2720 $table_name = $wpdb->prefix . 'et_bloom_stats';
2721
2722 $optin_id = sanitize_text_field( $optin_id );
2723
2724 // construct sql query to mark removed options as removed in stats DB
2725 $sql = "DELETE FROM $table_name WHERE optin_id = %s";
2726
2727 $sql_args = array(
2728 $optin_id,
2729 );
2730
2731 $wpdb->query( $wpdb->prepare( $sql, $sql_args ) );
2732 }
2733 }
2734
2735 /**
2736 * Toggles status of optin from active to inactive and vice versa
2737 * @return void
2738 */
2739 function toggle_optin_status() {
2740 if ( ! wp_verify_nonce( $_POST['toggle_status_nonce'] , 'toggle_status' ) ) {
2741 die( -1 );
2742 }
2743
2744 if ( ! current_user_can( 'manage_options' ) ) {
2745 die( -1 );
2746 }
2747
2748 $optin_id = ! empty( $_POST['status_optin_id'] ) ? sanitize_text_field( $_POST['status_optin_id'] ) : '';
2749 $toggle_to = ! empty( $_POST['status_new'] ) ? sanitize_text_field( $_POST['status_new'] ) : '';
2750
2751 if ( '' !== $optin_id ) {
2752 $options_array = ET_Bloom::get_bloom_options();
2753 $update_option[$optin_id] = $options_array[$optin_id];
2754 $update_option[$optin_id]['optin_status'] = 'active' === $toggle_to ? 'active' : 'inactive';
2755
2756 ET_Bloom::update_option( $update_option );
2757 }
2758
2759 die();
2760 }
2761
2762 /**
2763 * Updates the account details in DB.
2764 * @return void
2765 */
2766 function update_account( $service, $name, $data_array = array() ) {
2767 if ( '' !== $service && '' !== $name ) {
2768 $name = str_replace( array( '"', "'" ), '', stripslashes( $name ) );
2769 $name = sanitize_text_field( $name );
2770
2771 $options_array = ET_Bloom::get_bloom_options();
2772 $new_account['accounts'] = isset( $options_array['accounts'] ) ? $options_array['accounts'] : array();
2773 $new_account['accounts'][$service][$name] = isset( $new_account['accounts'][$service][$name] )
2774 ? array_merge( $new_account['accounts'][$service][$name], $data_array )
2775 : $data_array;
2776
2777 ET_Bloom::update_option( $new_account );
2778 }
2779 }
2780
2781 /**
2782 * Used to sync the accounts data. Executed by wp_cron daily.
2783 * In case of errors adds record to WP log
2784 */
2785 function perform_auto_refresh() {
2786 $options_array = ET_Bloom::get_bloom_options();
2787 if ( isset( $options_array['accounts'] ) ) {
2788 foreach ( $options_array['accounts'] as $service => $account ) {
2789 foreach ( $account as $name => $details ) {
2790 if ( 'true' == $details['is_authorized'] ) {
2791 $provider = ET_Bloom_Email_Providers::get_provider( $service );
2792 if ( $provider ) {
2793 $error_message = $provider->fetch_lists( $details );
2794 }
2795
2796 switch ( $service ) {
2797 case 'mailchimp' :
2798 $error_message = $this->get_mailchimp_lists( $details['api_key'], $name );
2799 break;
2800
2801 case 'constant_contact' :
2802 $error_message = $this->get_constant_contact_lists( $details['api_key'], $details['token'], $name );
2803 break;
2804
2805 case 'madmimi' :
2806 $error_message = $this->get_madmimi_lists( $details['username'], $details['api_key'], $name );
2807 break;
2808
2809 case 'icontact' :
2810 $error_message = $this->get_icontact_lists( $details['client_id'], $details['username'], $details['password'], $name );
2811 break;
2812
2813 case 'getresponse' :
2814 $error_message = $this->get_getresponse_lists( $details['api_key'], $name );
2815 break;
2816
2817 case 'sendinblue' :
2818 $error_message = $this->get_sendinblue_lists( $details['api_key'], $name );
2819 break;
2820
2821 case 'mailpoet' :
2822 $error_message = $this->get_mailpoet_lists( $name );
2823 break;
2824
2825 case 'aweber' :
2826 $error_message = $this->get_aweber_lists( $details['api_key'], $name );
2827 break;
2828
2829 case 'campaign_monitor' :
2830 $error_message = $this->get_campaign_monitor_lists( $details['api_key'], $name );
2831 break;
2832
2833 case 'ontraport' :
2834 $error_message = $this->get_ontraport_lists( $details['api_key'], $details['client_id'], $name );
2835 break;
2836
2837 case 'feedblitz' :
2838 $error_message = $this->get_feedblitz_lists( $details['api_key'], $name );
2839 break;
2840
2841 case 'infusionsoft' :
2842 $error_message = $this->get_infusionsoft_lists( $details['client_id'], $details['api_key'], $name );
2843 break;
2844
2845 case 'hubspot' :
2846 $error_message = $this->get_hubspot_lists( $details['api_key'], $name );
2847 break;
2848 }
2849 }
2850
2851 $result = 'success' == $error_message
2852 ? ''
2853 : 'bloom_error: ' . $service . ' ' . $name . ' ' . esc_html__( 'Authorization failed: ', 'bloom' ) . $error_message;
2854
2855 // Log errors into WP log for troubleshooting
2856 if ( '' !== $result ) {
2857 error_log( $result );
2858 }
2859 }
2860 }
2861 }
2862 }
2863
2864 /**
2865 * Handles accounts authorization. Basically just executes specific function based on service and returns error message.
2866 * Supports authorization of new accounts and re-authorization of existing accounts.
2867 * @return string
2868 */
2869 function authorize_account() {
2870 if ( ! wp_verify_nonce( $_POST['get_lists_nonce'] , 'get_lists' ) ) {
2871 die( -1 );
2872 }
2873
2874 if ( ! current_user_can( 'manage_options' ) ) {
2875 die( -1 );
2876 }
2877
2878 $service = ! empty( $_POST['bloom_upd_service'] ) ? sanitize_text_field( $_POST['bloom_upd_service'] ) : '';
2879 $name = ! empty( $_POST['bloom_upd_name'] ) ? sanitize_text_field( $_POST['bloom_upd_name'] ) : '';
2880 $update_existing = ! empty( $_POST['bloom_account_exists'] ) ? sanitize_text_field( $_POST['bloom_account_exists'] ) : '';
2881
2882 if ( 'true' == $update_existing ) {
2883 $options_array = ET_Bloom::get_bloom_options();
2884 $accounts_data = $options_array['accounts'];
2885
2886 $api_key = ! empty( $accounts_data[$service][$name]['api_key'] ) ? sanitize_text_field( $accounts_data[$service][$name]['api_key'] ) : '';
2887 $token = ! empty( $accounts_data[$service][$name]['token'] ) ? sanitize_text_field( $accounts_data[$service][$name]['token'] ) : '';
2888 $app_id = ! empty( $accounts_data[$service][$name]['client_id'] ) ? sanitize_text_field( $accounts_data[$service][$name]['client_id'] ) : '';
2889 $organization_id = ! empty( $accounts_data[$service][$name]['organization_id'] ) ? sanitize_text_field( $accounts_data[$service][$name]['organization_id'] ) : '';
2890 $username = ! empty( $accounts_data[$service][$name]['username'] ) ? sanitize_text_field( $accounts_data[$service][$name]['username'] ) : '';
2891 $password = ! empty( $accounts_data[$service][$name]['password'] ) ? sanitize_text_field( $accounts_data[$service][$name]['password'] ) : '';
2892 } else {
2893 $api_key = ! empty( $_POST['bloom_api_key'] ) ? sanitize_text_field( $_POST['bloom_api_key'] ) : '';
2894 $token = ! empty( $_POST['bloom_constant_token'] ) ? sanitize_text_field( $_POST['bloom_constant_token'] ) : '';
2895 $app_id = ! empty( $_POST['bloom_client_id'] ) ? sanitize_text_field( $_POST['bloom_client_id'] ) : '';
2896 $organization_id = ! empty( $_POST['bloom_organization_id'] ) ? sanitize_text_field( $_POST['bloom_organization_id'] ) : '';
2897 $username = ! empty( $_POST['bloom_username'] ) ? sanitize_text_field( $_POST['bloom_username'] ) : '';
2898 $password = ! empty( $_POST['bloom_password'] ) ? sanitize_text_field( $_POST['bloom_password'] ) : '';
2899 }
2900
2901 $error_message = '';
2902
2903 $provider = ET_Bloom_Email_Providers::get_provider( $service );
2904 if ( $provider ) {
2905 $args = array(
2906 'name' => $name,
2907 );
2908
2909 foreach ( $provider->get_fields() as $field_name => $field ) {
2910 if ( 'true' == $update_existing ) {
2911 $field_value = ! empty( $accounts_data[ $service ][ $name ][ $field_name ] ) ? sanitize_text_field( $accounts_data[ $service ][ $name ][ $field_name ] ) : '';
2912 } else {
2913 $post_field_name = $field_name . '_' .$provider->slug;
2914 $field_value = ! empty( $_POST[ $post_field_name ] ) ? sanitize_text_field( $_POST[ $post_field_name ] ) : '';
2915 }
2916
2917 $args[ $field_name ] = $field_value;
2918 }
2919
2920 $error_message = $provider->fetch_lists( $args );
2921 }
2922
2923 switch ( $service ) {
2924 case 'mailchimp' :
2925 $error_message = $this->get_mailchimp_lists( $api_key, $name );
2926 break;
2927
2928 case 'constant_contact' :
2929 $error_message = $this->get_constant_contact_lists( $api_key, $token, $name );
2930 break;
2931
2932 case 'madmimi' :
2933 $error_message = $this->get_madmimi_lists( $username, $api_key, $name );
2934 break;
2935
2936 case 'icontact' :
2937 $error_message = $this->get_icontact_lists( $app_id, $username, $password, $name );
2938 break;
2939
2940 case 'getresponse' :
2941 $error_message = $this->get_getresponse_lists( $api_key, $name );
2942 break;
2943
2944 case 'sendinblue' :
2945 $error_message = $this->get_sendinblue_lists( $api_key, $name );
2946 break;
2947
2948 case 'mailpoet' :
2949 $error_message = $this->get_mailpoet_lists( $name );
2950 break;
2951
2952 case 'aweber' :
2953 $error_message = $this->get_aweber_lists( $api_key, $name );
2954 break;
2955
2956 case 'campaign_monitor' :
2957 $error_message = $this->get_campaign_monitor_lists( $api_key, $name );
2958 break;
2959
2960 case 'ontraport' :
2961 $error_message = $this->get_ontraport_lists( $api_key, $app_id, $name );
2962 break;
2963
2964 case 'feedblitz' :
2965 $error_message = $this->get_feedblitz_lists( $api_key, $name );
2966 break;
2967
2968 case 'infusionsoft' :
2969 $error_message = $this->get_infusionsoft_lists( $app_id, $api_key, $name );
2970 break;
2971
2972 case 'salesforce':
2973 $error_message = $this->save_salesforce_organization_data( $organization_id, $name );
2974 break;
2975
2976 case 'hubspot' :
2977 $error_message = $this->get_hubspot_lists( $api_key, $name );
2978 break;
2979 }
2980
2981 $result = 'success' == $error_message ?
2982 $error_message
2983 : esc_html__( 'Authorization failed: ', 'bloom' ) . $error_message;
2984
2985 die( esc_html( $result ) );
2986 }
2987
2988 /**
2989 * Handles subscribe action and sends the success or error message to jQuery.
2990 */
2991 function subscribe() {
2992 if ( ! wp_verify_nonce( $_POST['subscribe_nonce'] , 'subscribe' ) ) {
2993 die( -1 );
2994 }
2995
2996 $subscribe_data_json = str_replace( '\\', '' , $_POST[ 'subscribe_data_array' ] );
2997 $subscribe_data_array = json_decode( $subscribe_data_json, true );
2998
2999 $service = sanitize_text_field( $subscribe_data_array['service'] );
3000 $account_name = sanitize_text_field( $subscribe_data_array['account_name'] );
3001 $name = isset( $subscribe_data_array['name'] ) ? sanitize_text_field( $subscribe_data_array['name'] ) : '';
3002 $last_name = isset( $subscribe_data_array['last_name'] ) ? sanitize_text_field( $subscribe_data_array['last_name'] ) : '';
3003 $dbl_optin = isset( $subscribe_data_array['dbl_optin'] ) ? sanitize_text_field( $subscribe_data_array['dbl_optin'] ) : '';
3004 $email = sanitize_email( $subscribe_data_array['email'] );
3005 $list_id = sanitize_text_field( $subscribe_data_array['list_id'] );
3006 $page_id = sanitize_text_field( $subscribe_data_array['page_id'] );
3007 $optin_id = sanitize_text_field( $subscribe_data_array['optin_id'] );
3008 $result = '';
3009
3010 if ( is_email( $email ) ) {
3011 $options_array = ET_Bloom::get_bloom_options();
3012
3013 $provider = ET_Bloom_Email_Providers::get_provider( $service );
3014 if ( $provider ) {
3015 $args = array(
3016 'list_id' => $list_id,
3017 'email' => $email,
3018 'name' => $name,
3019 'last_name' => $last_name,
3020 );
3021
3022 foreach ( $provider->get_fields() as $field_name => $field ) {
3023 $field_value = ! empty( $options_array['accounts'][ $service ][ $account_name ][ $field_name ] ) ? $options_array['accounts'][ $service ][ $account_name ][ $field_name ] : '';
3024 $args[ $field_name ] = sanitize_text_field( $field_value );
3025 }
3026
3027 $error_message = $provider->add_subscriber( $args );
3028 }
3029
3030 switch ( $service ) {
3031 case 'mailchimp' :
3032 $api_key = sanitize_text_field( $options_array['accounts'][$service][$account_name]['api_key'] );
3033 $error_message = $this->subscribe_mailchimp( $api_key, $list_id, $email, $name, $last_name, $dbl_optin );
3034 break;
3035
3036 case 'constant_contact' :
3037 $api_key = sanitize_text_field( $options_array['accounts'][$service][$account_name]['api_key'] );
3038 $token = sanitize_text_field( $options_array['accounts'][$service][$account_name]['token'] );
3039 $error_message = $this->subscribe_constant_contact( $email, $api_key, $token, $list_id, $name, $last_name );
3040 break;
3041
3042 case 'madmimi' :
3043 $api_key = sanitize_text_field( $options_array['accounts'][$service][$account_name]['api_key'] );
3044 $username = sanitize_text_field( $options_array['accounts'][$service][$account_name]['username'] );
3045 $error_message = $this->subscribe_madmimi( $username, $api_key, $list_id, $email, $name, $last_name );
3046 break;
3047
3048 case 'icontact' :
3049 $app_id = sanitize_text_field( $options_array['accounts'][$service][$account_name]['client_id'] );
3050 $username = sanitize_text_field( $options_array['accounts'][$service][$account_name]['username'] );
3051 $password = sanitize_text_field( $options_array['accounts'][$service][$account_name]['password'] );
3052 $folder_id = sanitize_text_field( $options_array['accounts'][$service][$account_name]['lists'][$list_id]['folder_id'] );
3053 $account_id = sanitize_text_field( $options_array['accounts'][$service][$account_name]['lists'][$list_id]['account_id'] );
3054 $error_message = $this->subscribe_icontact( $app_id, $username, $password, $folder_id, $account_id, $list_id, $email, $name, $last_name );
3055 break;
3056
3057 case 'getresponse' :
3058 $api_key = sanitize_text_field( $options_array['accounts'][$service][$account_name]['api_key'] );
3059 $error_message = $this->subscribe_get_response( $list_id, $email, $api_key, $name );
3060 break;
3061
3062 case 'sendinblue' :
3063 $api_key = sanitize_text_field( $options_array['accounts'][$service][$account_name]['api_key'] );
3064 $error_message = $this->subscribe_sendinblue( $api_key, $email, $list_id, $name, $last_name );
3065 break;
3066
3067 case 'mailpoet' :
3068 $error_message = $this->subscribe_mailpoet( $list_id, $email, $name, $last_name );
3069 break;
3070
3071 case 'aweber' :
3072 $error_message = $this->subscribe_aweber( $list_id, $account_name, $email, $name );
3073 break;
3074
3075 case 'campaign_monitor' :
3076 $api_key = sanitize_text_field( $options_array['accounts'][$service][$account_name]['api_key'] );
3077 $error_message = $this->subscribe_campaign_monitor( $api_key, $email, $list_id, $name );
3078 break;
3079
3080 case 'ontraport' :
3081 $app_id = sanitize_text_field( $options_array['accounts'][$service][$account_name]['client_id'] );
3082 $api_key = sanitize_text_field( $options_array['accounts'][$service][$account_name]['api_key'] );
3083 $error_message = $this->subscribe_ontraport( $app_id, $api_key, $name, $email, $list_id, $last_name );
3084 break;
3085
3086 case 'feedblitz' :
3087 $api_key = sanitize_text_field( $options_array['accounts'][$service][$account_name]['api_key'] );
3088 $error_message = $this->subscribe_feedblitz( $api_key, $list_id, $name, $email, $last_name );
3089 break;
3090
3091 case 'infusionsoft' :
3092 $api_key = sanitize_text_field( $options_array['accounts'][$service][$account_name]['api_key'] );
3093 $app_id = sanitize_text_field( $options_array['accounts'][$service][$account_name]['client_id'] );
3094 $error_message = $this->subscribe_infusionsoft( $api_key, $app_id, $list_id, $email, $name, $last_name );
3095 break;
3096
3097 case 'salesforce':
3098 $error_message = $this->subscribe_salesforce( $options_array['accounts'][$service][$account_name]['organization_id'], $email, $name, $last_name );
3099 break;
3100
3101 case 'hubspot' :
3102 $api_key = sanitize_text_field( $options_array['accounts'][ $service ][ $account_name ]['api_key'] );
3103 $error_message = $this->subscribe_hubspot( $api_key, $list_id, $email, $name, $last_name );
3104 break;
3105 }
3106 } else {
3107 $error_message = esc_html__( 'Invalid email', 'bloom' );
3108 }
3109
3110 if ( 'success' == $error_message ) {
3111 ET_Bloom::add_stats_record( 'con', $optin_id, $page_id, $service . '_' . $list_id );
3112 $result = json_encode( array( 'success' => $error_message ) );
3113 } else {
3114 $result = json_encode( array( 'error' => esc_html( $error_message ) ) );
3115 }
3116
3117 die( $result );
3118 }
3119
3120 /**
3121 * Retrieves the lists via Infusionsoft API and updates the data in DB.
3122 * @return string
3123 */
3124
3125 function get_infusionsoft_lists( $app_id, $api_key, $name ) {
3126 if ( ! function_exists( 'curl_init' ) ) {
3127 return esc_html__( 'curl_init is not defined ', 'bloom' );
3128 }
3129
3130 if ( ! class_exists( 'ET_Infusionsoft' ) ) {
3131 require_once( ET_BLOOM_PLUGIN_DIR . 'subscription/infusionsoft/et_infusionsoft_api.php' );
3132 }
3133
3134 $lists = array();
3135
3136 $infusion_app = new ET_Infusionsoft( $api_key, $app_id );
3137 $error_message = $infusion_app->connection_check();
3138
3139 if ( empty( $error_message ) ) {
3140 $need_request = true;
3141 $page = 0;
3142 $all_lists = array();
3143
3144 while ( true == $need_request ) {
3145 $error_message = 'success';
3146 $lists_data = $infusion_app->database_query( 'ContactGroup', 1000, $page, array( 'Id' => '%' ), array( 'Id', 'GroupName' ) );
3147 $all_lists = array_merge( $all_lists, (array) $lists_data['array']->data );
3148 if ( 1000 > count( (array) $lists_data['array']->data ) ) {
3149 $need_request = false;
3150 } else {
3151 $page++;
3152 }
3153 }
3154 }
3155
3156 if ( ! empty( $all_lists['value'] ) ) {
3157 foreach( $all_lists['value'] as $list ) {
3158 $list_id = (string) $list->struct->member[1]->value->i4;
3159 $list_name = $list->struct->member[0]->value;
3160 $group_query = '%' . $list_id . '%';
3161 $subscribers_count = $infusion_app->database_count( 'Contact', array( 'Groups' => $group_query ) );
3162 $lists[ $list_id ]['name'] = sanitize_text_field( $list_name );
3163 $lists[ $list_id ]['subscribers_count'] = sanitize_text_field( $subscribers_count['i4'] );
3164 $lists[ $list_id ]['growth_week'] = sanitize_text_field( $this->calculate_growth_rate( 'infusionsoft_' . $list_id ) );
3165 }
3166
3167 $this->update_account( 'infusionsoft', sanitize_text_field( $name ), array(
3168 'lists' => $lists,
3169 'api_key' => sanitize_text_field( $api_key ),
3170 'client_id' => sanitize_text_field( $app_id ),
3171 'is_authorized' => 'true',
3172 ) );
3173 }
3174
3175 return $error_message;
3176 }
3177
3178 /**
3179 * Subscribes to Infusionsoft list. Returns either "success" string or error message.
3180 * @return string
3181 */
3182 function subscribe_infusionsoft( $api_key, $app_id, $list_id, $email, $name = '', $last_name = '' ) {
3183 if ( ! function_exists( 'curl_init' ) ) {
3184 return esc_html__( 'curl_init is not defined ', 'bloom' );
3185 }
3186
3187 if ( ! class_exists( 'ET_Infusionsoft' ) ) {
3188 require_once( ET_BLOOM_PLUGIN_DIR . 'subscription/infusionsoft/et_infusionsoft_api.php' );
3189 }
3190
3191 $infusion_app = new ET_Infusionsoft( $api_key, $app_id );
3192 $error_message = $infusion_app->connection_check();
3193
3194 if ( empty( $error_message ) ) {
3195 $contact_data = $infusion_app->database_query( 'Contact', 1, 0, array( 'Email' => $email ), array( 'Id', 'Groups' ) );
3196 $found_contact = isset( $contact_data['array']->data->value->struct->member ) ? $contact_data['array']->data->value->struct->member : array();
3197 if ( 0 < count( $found_contact ) ) {
3198 if ( false === strpos( $found_contact[0]->value, $list_id ) ) {
3199 $infusion_app->add_to_list( $found_contact[1]->value->i4, $list_id );
3200 $error_message = 'success';
3201 } else {
3202 $error_message = esc_html__( 'Already subscribed', 'bloom' );
3203 }
3204 } else {
3205 $contact_details = array(
3206 'FirstName' => $name,
3207 'LastName' => $last_name,
3208 'Email' => $email,
3209 );
3210
3211 $new_contact_id = $infusion_app->add_contact( $contact_details );
3212 $infusion_app->add_to_list( $new_contact_id, $list_id );
3213 $infusion_app->opt_in_email( $email, esc_html__( 'Subscribed via Bloom', 'bloom' ) );
3214
3215 $error_message = 'success';
3216 }
3217 }
3218
3219 return $error_message;
3220 }
3221
3222 /**
3223 * Save SalesForce Organization ID for web-to-lead
3224 * @return string
3225 */
3226 function save_salesforce_organization_data( $organization_id, $name ) {
3227 if ( '' !== $organization_id ) {
3228 $this->update_account( 'salesforce', sanitize_text_field( $name ), array(
3229 'organization_id' => sanitize_text_field( $organization_id ),
3230 'is_authorized' => 'true',
3231 ) );
3232
3233 return 'success';
3234 }
3235
3236 return esc_html__( 'Organization ID cannot be empty', 'bloom' );
3237 }
3238
3239 /**
3240 * Post web-to-lead request to SalesForce
3241 * @return string
3242 */
3243 function subscribe_salesforce( $account_name = '', $email, $name = '', $last_name = '' ) {
3244 if ( '' === $account_name ) {
3245 return esc_html__( 'Unknown Organization ID', 'bloom' );
3246 }
3247
3248 // Define SalesForce web-to-lead endpoint
3249 $url = "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8";
3250
3251 // Prepare arguments for web-to-lead POST
3252 $args = array(
3253 'body' => array(
3254 'oid' => sanitize_text_field( $account_name ),
3255 'retURL' => esc_url( home_url( '/' ) ),
3256 'email' => sanitize_email( $email ),
3257 ),
3258 );
3259
3260 if ( '' !== $name ) {
3261 $args['body']['first_name'] = sanitize_text_field( $name );
3262 }
3263
3264 if ( '' !== $last_name ) {
3265 $args['body']['last_name'] = sanitize_text_field( $last_name );
3266 }
3267
3268 // Post to SalesForce web-to-lead endpoint
3269 $post = wp_remote_post( $url, $args );
3270
3271 if ( ! is_wp_error( $post ) ) {
3272 return 'success';
3273 }
3274
3275 return esc_html__( 'An error occured. Please try again', 'bloom' );
3276 }
3277
3278 /*
3279 * Retrieves the lists via HubSpot API and updates the data in DB.
3280 * @return string
3281 */
3282 function get_hubspot_lists( $api_key = '', $name = '' ) {
3283 $lists = array();
3284
3285 $error_message = '';
3286
3287 $request_url = esc_url_raw(
3288 sprintf(
3289 'https://api.hubapi.com/contacts/v1/lists/static?count=20&hapikey=%1$s&offset=0',
3290 sanitize_text_field( $api_key )
3291 )
3292 );
3293
3294 $theme_request = wp_remote_get( $request_url, array( 'timeout' => 30 ) );
3295
3296 $response_code = wp_remote_retrieve_response_code( $theme_request );
3297
3298 if ( ! is_wp_error( $theme_request ) && $response_code === 200 ) {
3299 $theme_response = json_decode( wp_remote_retrieve_body( $theme_request ), true );
3300 if ( ! empty( $theme_response ) ) {
3301 $error_message = 'success';
3302 $received_lists_data = $theme_response['lists'];
3303 $need_more_data = isset( $theme_response['has-more'] ) && $theme_response['has-more'];
3304 $start_from = $theme_response['offset'];
3305
3306 // request all the lists from HubSpot API
3307 while ( $need_more_data ) {
3308 $additional_request_url = esc_url_raw(
3309 sprintf(
3310 'https://api.hubapi.com/contacts/v1/lists/static?count=20&hapikey=%1$s&offset=%2$s',
3311 sanitize_text_field( $api_key ),
3312 sanitize_text_field( $start_from )
3313 )
3314 );
3315
3316 $additional_theme_request = wp_remote_get( $additional_request_url, array( 'timeout' => 30 ) );
3317 $additional_response_code = wp_remote_retrieve_response_code( $theme_request );
3318
3319 if ( ! is_wp_error( $additional_theme_request ) && $additional_response_code === 200 ) {
3320 $additional_theme_response = json_decode( wp_remote_retrieve_body( $additional_theme_request ), true );
3321 $need_more_data = isset( $additional_theme_response['has-more'] ) && $additional_theme_response['has-more'];
3322 $start_from = $additional_theme_response['offset'];
3323 $received_lists_data = array_merge( $received_lists_data, $additional_theme_response['lists'] );
3324 } else {
3325 $need_more_data = false;
3326 }
3327 }
3328
3329 foreach ( $received_lists_data as $list_data ) {
3330 $lists[ $list_data['listId'] ]['name'] = $list_data['name'];
3331 $lists[ $list_data['listId'] ]['subscribers_count'] = $list_data['metaData']['size'];
3332 $lists[ $list_data['listId'] ]['growth_week'] = $this->calculate_growth_rate( 'hubspot_' . $list_data['listId'] );
3333 }
3334
3335 $this->update_account( 'hubspot', $name, array(
3336 'api_key' => sanitize_text_field( $api_key ),
3337 'lists' => $lists,
3338 'is_authorized' => 'true',
3339 ) );
3340
3341 }
3342 } else {
3343 if ( is_wp_error( $theme_request ) ) {
3344 $error_message = $theme_request->get_error_message();
3345 } else {
3346 switch ( $response_code ) {
3347 case '401' :
3348 $error_message = esc_html__( 'Invalid API key', 'bloom' );
3349 break;
3350
3351 default :
3352 $error_message = $response_code;
3353 break;
3354 }
3355 }
3356 }
3357
3358 return $error_message;
3359 }
3360
3361 /**
3362 * Subscribes to HubSpot list. Returns either "success" string or error message.
3363 * @return string
3364 */
3365 function subscribe_hubspot( $api_key, $list_id, $email, $name = '', $last_name = '' ) {
3366 if ( ! function_exists( 'curl_init' ) ) {
3367 return;
3368 }
3369
3370 // prepare array of data to add new contact
3371 $contact_data = array(
3372 'properties' => array(
3373 array(
3374 'property' => 'email',
3375 'value' => sanitize_email( $email ),
3376 ),
3377 array(
3378 'property' => 'firstname',
3379 'value' => sanitize_text_field( $name ),
3380 ),
3381 array(
3382 'property' => 'lastname',
3383 'value' => sanitize_text_field( $last_name ),
3384 ),
3385 ),
3386 );
3387 $contact_data_json = json_encode( $contact_data );
3388
3389 $request_url = esc_url_raw(
3390 sprintf(
3391 'https://api.hubapi.com/contacts/v1/contact?hapikey=%1$s',
3392 sanitize_text_field( $api_key )
3393 )
3394 );
3395
3396 // Get cURL resource
3397 $curl = curl_init();
3398
3399 // Set some options
3400 curl_setopt_array( $curl, array(
3401 CURLOPT_POST => true,
3402 CURLOPT_URL => $request_url,
3403 CURLOPT_SSL_VERIFYPEER => FALSE, //we need this option since we perform request to https
3404 CURLOPT_POSTFIELDS => $contact_data_json,
3405 CURLOPT_HTTPHEADER => array( 'Content-Type: application/json' ),
3406 CURLOPT_RETURNTRANSFER => true,
3407 ) );
3408
3409 // Send the request & save response to $resp
3410 $resp = curl_exec( $curl );
3411
3412 // Close request to clear up some resources
3413 curl_close( $curl );
3414
3415 $new_contact_data = json_decode( $resp, true );
3416
3417 if ( isset( $new_contact_data['error'] ) && 'CONTACT_EXISTS' !== $new_contact_data['error'] ) {
3418 return $new_contact_data['error'];
3419 }
3420
3421 // contact ID stored in different place if contact already exists
3422 if ( isset( $new_contact_data['error'] ) && 'CONTACT_EXISTS' === $new_contact_data['error'] ) {
3423 $new_contact_vid = isset( $new_contact_data['identityProfile']['vid'] ) ? $new_contact_data['identityProfile']['vid'] : '';
3424 } else {
3425 $new_contact_vid = isset( $new_contact_data['vid'] ) ? $new_contact_data['vid'] : '';
3426 }
3427
3428 if ( '' === $new_contact_vid ) {
3429 return esc_html__( 'You were not subscribed. Please try again later', 'bloom' );
3430 }
3431
3432 $error_message = 'success';
3433
3434 // prepare data to add new contact to list
3435 $contact_vids = array(
3436 'vids' => array(
3437 (int) sanitize_text_field( $new_contact_vid ),
3438 ),
3439 );
3440 $contact_vids_json = json_encode( $contact_vids );
3441
3442 $add_to_list_url = esc_url_raw(
3443 sprintf(
3444 'https://api.hubapi.com/contacts/v1/lists/%2$s/add?hapikey=%1$s',
3445 sanitize_text_field( $api_key ),
3446 $list_id
3447 )
3448 );
3449
3450 // add the contact to appropriate list
3451 $theme_request = wp_remote_post( $add_to_list_url, array(
3452 'timeout' => 30,
3453 'headers' => array(
3454 'content-type' => 'application/json',
3455 ),
3456 'body' => $contact_vids_json,
3457 ) );
3458
3459 return $error_message;
3460 }
3461
3462 /**
3463 * Retrieves the lists via MailChimp API and updates the data in DB.
3464 * @return string
3465 */
3466 function get_mailchimp_lists( $api_key = '', $name = '' ) {
3467 $lists = array();
3468
3469 $error_message = '';
3470
3471 if ( ! function_exists( 'curl_init' ) ) {
3472 return esc_html__( 'curl_init is not defined ', 'bloom' );
3473 }
3474
3475 if ( ! class_exists( 'MailChimp_Bloom' ) ) {
3476 require_once( ET_BLOOM_PLUGIN_DIR . 'subscription/mailchimp/mailchimp.php' );
3477 }
3478
3479 if ( false === strpos( $api_key, '-' ) ) {
3480 $error_message = esc_html__( 'invalid API key', 'bloom' );
3481 } else {
3482 $mailchimp = new MailChimp_Bloom( $api_key );
3483 $lists_data = array();
3484
3485 // retrieve lists from Mailchimp account, set the limit = 100 ( max allowed by Mailchimp api )
3486 $retval = $mailchimp->call( 'lists/list', array( 'limit' => 100 ) );
3487
3488 if ( ! empty( $retval ) && empty( $retval['errors'] ) ) {
3489 $error_message = 'success';
3490 $lists_data = $retval['data'];
3491
3492 // if there is more than 100 lists in account, then perform additional calls to retrieve all the lists.
3493 if ( 100 < $retval['total'] ) {
3494 // determine how many requests we need to retrieve all the lists
3495 $total_pages = ceil( $retval['total'] / 100 );
3496
3497 for ( $i = 1; $i <= $total_pages; $i++ ) {
3498 $retval_additional = $mailchimp->call( 'lists/list', array(
3499 'limit' => 100,
3500 'start' => $i,
3501 )
3502 );
3503
3504 if ( ! empty( $retval_additional ) && empty( $retval_additional['errors'] ) ) {
3505 if ( ! empty( $retval_additional['data'] ) ) {
3506 $lists_data = array_merge( $lists_data, $retval_additional['data'] );
3507 }
3508 }
3509 }
3510 }
3511
3512 if ( ! empty( $lists_data ) ) {
3513 foreach ( $lists_data as $list ) {
3514 $lists[$list['id']]['name'] = sanitize_text_field( $list['name'] );
3515 $lists[$list['id']]['subscribers_count'] = sanitize_text_field( $list['stats']['member_count'] );
3516 $lists[$list['id']]['growth_week'] = sanitize_text_field( $this->calculate_growth_rate( 'mailchimp_' . $list['id'] ) );
3517 }
3518 }
3519 $this->update_account( 'mailchimp', sanitize_text_field( $name ), array(
3520 'lists' => $lists,
3521 'api_key' => sanitize_text_field( $api_key ),
3522 'is_authorized' => 'true',
3523 ) );
3524 } else {
3525 if ( ! empty( $retval['errors'] ) ) {
3526 $errors = '';
3527 foreach( $retval['errors'] as $error ) {
3528 $errors .= $error . ' ';
3529 }
3530 $error_message = $errors;
3531 }
3532
3533 if ( '' !== $error_message ) {
3534 $error_message = sprintf( '%1$s: %2$s',
3535 esc_html__( 'Additional Information: ' ),
3536 $error_message
3537 );
3538 }
3539
3540 $error_message = sprintf( '%1$s. %2$s',
3541 esc_html__( 'An error occured during API request. Make sure API Key is correct', 'bloom' ),
3542 $error_message
3543 );
3544 }
3545 }
3546
3547 return $error_message;
3548 }
3549
3550 /**
3551 * Subscribes to Mailchimp list. Returns either "success" string or error message.
3552 * @return string
3553 */
3554 function subscribe_mailchimp( $api_key, $list_id, $email, $name = '', $last_name = '', $disable_dbl = '' ) {
3555 if ( ! function_exists( 'curl_init' ) ) {
3556 return;
3557 }
3558
3559 if ( ! class_exists( 'MailChimp_Bloom' ) ) {
3560 require_once( ET_BLOOM_PLUGIN_DIR . 'subscription/mailchimp/mailchimp.php' );
3561 }
3562
3563 $mailchimp = new MailChimp_Bloom( $api_key );
3564
3565 $email = array( 'email' => $email );
3566 $double_optin = '' === $disable_dbl ? 'true' : 'false';
3567
3568 $merge_vars = array(
3569 'FNAME' => $name,
3570 'LNAME' => $last_name,
3571 );
3572
3573 $retval = $mailchimp->call( 'lists/subscribe', array(
3574 'id' => $list_id,
3575 'email' => $email,
3576 'double_optin' => $double_optin,
3577 'merge_vars' => $merge_vars,
3578 ));
3579
3580 if ( isset( $retval['error'] ) ) {
3581 if ( '214' == $retval['code'] ) {
3582 $error_message = str_replace( 'Click here to update your profile.', '', $retval['error'] );
3583 } else {
3584 $error_message = $retval['error'];
3585 }
3586 } else {
3587 $error_message = 'success';
3588 }
3589
3590 return $error_message;
3591 }
3592
3593 /**
3594 * Retrieves the lists via Constant Contact API and updates the data in DB.
3595 * @return string
3596 */
3597 function get_constant_contact_lists( $api_key, $token, $name ) {
3598 $lists = array();
3599
3600 $request_url = esc_url_raw( 'https://api.constantcontact.com/v2/lists?api_key=' . $api_key );
3601
3602 $theme_request = wp_remote_get( $request_url, array(
3603 'timeout' => 30,
3604 'headers' => array( 'Authorization' => 'Bearer ' . $token ),
3605 ) );
3606
3607 $response_code = wp_remote_retrieve_response_code( $theme_request );
3608
3609 if ( ! is_wp_error( $theme_request ) && $response_code == 200 ){
3610 $theme_response = wp_remote_retrieve_body( $theme_request );
3611 if ( ! empty( $theme_response ) ) {
3612 $error_message = 'success';
3613
3614 $response = json_decode( $theme_response, true );
3615
3616 foreach ( $response as $key => $value ) {
3617 if ( isset( $value['id'] ) ) {
3618 $lists[$value['id']]['name'] = sanitize_text_field( $value['name'] );
3619 $lists[$value['id']]['subscribers_count'] = sanitize_text_field( $value['contact_count'] );
3620 $lists[$value['id']]['growth_week'] = sanitize_text_field( $this->calculate_growth_rate( 'constant_contact_' . $value['id'] ) );
3621 }
3622 }
3623
3624 $this->update_account( 'constant_contact', sanitize_text_field( $name ), array(
3625 'lists' => $lists,
3626 'api_key' => sanitize_text_field( $api_key ),
3627 'token' => sanitize_text_field( $token ),
3628 'is_authorized' => 'true',
3629 ) );
3630 } else {
3631 $error_message .= esc_html__( 'empty response', 'bloom' );
3632 }
3633 } else {
3634 if ( is_wp_error( $theme_request ) ) {
3635 $error_message = $theme_request->get_error_message();
3636 } else {
3637 switch ( $response_code ) {
3638 case '401' :
3639 $error_message = esc_html__( 'Invalid Token', 'bloom' );
3640 break;
3641
3642 case '403' :
3643 $error_message = esc_html__( 'Invalid API key', 'bloom' );
3644 break;
3645
3646 default :
3647 $error_message = $response_code;
3648 break;
3649 }
3650 }
3651 }
3652
3653 return $error_message;
3654 }
3655
3656 /**
3657 * Subscribes to Constant Contact list. Returns either "success" string or error message.
3658 * @return string
3659 */
3660 function subscribe_constant_contact( $email, $api_key, $token, $list_id, $name = '', $last_name = '' ) {
3661 $request_url = esc_url_raw( 'https://api.constantcontact.com/v2/contacts?email=' . $email . '&api_key=' . $api_key );
3662 $error_message = '';
3663
3664 $theme_request = wp_remote_get( $request_url, array(
3665 'timeout' => 30,
3666 'headers' => array( 'Authorization' => 'Bearer ' . $token ),
3667 ) );
3668 $response_code = wp_remote_retrieve_response_code( $theme_request );
3669
3670 if ( ! is_wp_error( $theme_request ) && $response_code == 200 ){
3671 $theme_response = wp_remote_retrieve_body( $theme_request );
3672 $response = json_decode( $theme_response, true );
3673
3674 // check whether we have current email in any list or whether contact has 'REMOVED' status
3675 if ( ! empty( $response['results'][0]['lists'] ) || ( isset( $response['results'][0]['status'] ) && 'REMOVED' === $response['results'][0]['status'] ) ) {
3676 $current_user_lists = '"lists":[';
3677
3678 // determine whether current email subscribed to current list and build the list of List ID for further update
3679 if ( ! empty( $response['results'][0]['lists'] ) ) {
3680 foreach( $response['results'][0]['lists'] as $list_details ) {
3681 $current_user_lists .= '{"id": "' . $list_details['id'] . '"},';
3682
3683 if ( $list_id === $list_details['id'] ) {
3684 return esc_html__( 'Already subscribed', 'bloom' );
3685 }
3686 }
3687 }
3688
3689 // add current list ID into list of IDs.
3690 $current_user_lists .= '{"id": "' . $list_id . '"}]';
3691 $contact_id = $response['results'][0]['id'];
3692
3693 $request_url = esc_url_raw( 'https://api.constantcontact.com/v2/contacts/' . $contact_id . '?action_by=ACTION_BY_VISITOR&api_key=' . $api_key );
3694
3695 // all the contact fields will be updated during API request, so we need to pass all the info including email, name, last name and all the list IDs
3696 $body_request = sprintf(
3697 '{"email_addresses":[{"email_address": "%1$s" }], %2$s, "first_name": "%3$s", "last_name" : "%4$s" }',
3698 sanitize_email( $email ),
3699 $current_user_lists,
3700 sanitize_text_field( $name ),
3701 sanitize_text_field( $last_name )
3702 );
3703 $theme_request = wp_remote_request( $request_url, array(
3704 'method' => 'PUT',
3705 'timeout' => 30,
3706 'headers' => array(
3707 'Authorization' => 'Bearer ' . $token,
3708 'content-type' => 'application/json',
3709 ),
3710 'body' => $body_request,
3711 ) );
3712 $response_code = wp_remote_retrieve_response_code( $theme_request );
3713
3714 if ( ! is_wp_error( $theme_request ) && $response_code == 200 ) {
3715 $error_message = 'success';
3716 } else {
3717 if ( is_wp_error( $theme_request ) ) {
3718 $error_message = $theme_request->get_error_message();
3719 } else {
3720 $error_message = $response_code;
3721 }
3722 }
3723 } else {
3724 $request_url = esc_url_raw( 'https://api.constantcontact.com/v2/contacts?action_by=ACTION_BY_VISITOR&api_key=' . $api_key );
3725 $body_request = sprintf(
3726 '{"email_addresses":[{"email_address": "%1$s" }], "lists":[{"id": "%2$s"}], "first_name": "%3$s", "last_name" : "%4$s" }',
3727 sanitize_email( $email ),
3728 esc_html( $list_id ),
3729 sanitize_text_field( $name ),
3730 sanitize_text_field( $last_name )
3731 );
3732 $theme_request = wp_remote_post( $request_url, array(
3733 'timeout' => 30,
3734 'headers' => array(
3735 'Authorization' => 'Bearer ' . $token,
3736 'content-type' => 'application/json',
3737 ),
3738 'body' => $body_request,
3739 ) );
3740 $response_code = wp_remote_retrieve_response_code( $theme_request );
3741 if ( ! is_wp_error( $theme_request ) && $response_code == 201 ) {
3742 $error_message = 'success';
3743 } else {
3744 if ( is_wp_error( $theme_request ) ) {
3745 $error_message = $theme_request->get_error_message();
3746 } else {
3747 switch ( $response_code ) {
3748 case '409' :
3749 $error_message = esc_html__( 'Already subscribed', 'bloom' );
3750 break;
3751
3752 default :
3753 $error_message = $response_code;
3754 break;
3755 }
3756 }
3757 }
3758 }
3759 } else {
3760 if ( is_wp_error( $theme_request ) ) {
3761 $error_message = $theme_request->get_error_message();
3762 } else {
3763 switch ( $response_code ) {
3764 case '401' :
3765 $error_message = esc_html__( 'Invalid Token', 'bloom' );
3766 break;
3767
3768 case '403' :
3769 $error_message = esc_html__( 'Invalid API key', 'bloom' );
3770 break;
3771
3772 default :
3773 $error_message = $response_code;
3774 break;
3775 }
3776 }
3777 }
3778
3779 return $error_message;
3780 }
3781
3782
3783 /**
3784 * Retrieves the lists via Campaign Monitor API and updates the data in DB.
3785 * @return string
3786 */
3787 function get_campaign_monitor_lists( $api_key, $name ) {
3788 if ( ! class_exists( 'CS_REST_Clients' ) ) {
3789 require_once( ET_BLOOM_PLUGIN_DIR . 'subscription/createsend-php-4.0.2/csrest_clients.php' );
3790 }
3791 if ( ! class_exists( 'CS_REST_Lists' ) ) {
3792 require_once( ET_BLOOM_PLUGIN_DIR . 'subscription/createsend-php-4.0.2/csrest_lists.php' );
3793 }
3794
3795 $auth = array(
3796 'api_key' => $api_key,
3797 );
3798
3799 $request_url = esc_url_raw( 'https://api.createsend.com/api/v3.1/clients.json?pretty=true' );
3800 $all_clients_id = array();
3801 $all_lists = array();
3802
3803 if ( ! function_exists( 'curl_init' ) ) {
3804 return esc_html__( 'curl_init is not defined ', 'bloom' );
3805 }
3806
3807 // Get cURL resource
3808 $curl = curl_init();
3809 // Set some options
3810 curl_setopt_array( $curl, array(
3811 CURLOPT_RETURNTRANSFER => 1,
3812 CURLOPT_URL => $request_url,
3813 CURLOPT_SSL_VERIFYPEER => FALSE, //we need this option since we perform request to https
3814 CURLOPT_USERPWD => $api_key . ':x'
3815 ) );
3816 // Send the request & save response to $resp
3817 $resp = curl_exec( $curl );
3818 $httpCode = curl_getinfo( $curl, CURLINFO_HTTP_CODE );
3819 // Close request to clear up some resources
3820 curl_close( $curl );
3821
3822 $clients_array = json_decode( $resp, true );
3823
3824 if ( '200' == $httpCode ) {
3825 $error_message = 'success';
3826
3827 foreach( $clients_array as $client => $client_details ) {
3828 $all_clients_id[] = $client_details['ClientID'];
3829 }
3830
3831 if ( ! empty( $all_clients_id ) ) {
3832 foreach( $all_clients_id as $client ) {
3833 $wrap = new CS_REST_Clients( $client, $auth );
3834 $lists_data = $wrap->get_lists();
3835
3836 foreach ( $lists_data->response as $list => $single_list ) {
3837 $all_lists[$single_list->ListID]['name'] = sanitize_text_field( $single_list->Name );
3838
3839 $wrap_stats = new CS_REST_Lists( $single_list->ListID, $auth );
3840 $result_stats = $wrap_stats->get_stats();
3841 $all_lists[$single_list->ListID]['subscribers_count'] = sanitize_text_field( $result_stats->response->TotalActiveSubscribers );
3842 $all_lists[$single_list->ListID]['growth_week'] = sanitize_text_field( $this->calculate_growth_rate( 'campaign_monitor_' . $single_list->ListID ) );
3843 }
3844 }
3845 }
3846
3847 $this->update_account( 'campaign_monitor', sanitize_text_field( $name ), array(
3848 'api_key' => sanitize_text_field( $api_key ),
3849 'lists' => $all_lists,
3850 'is_authorized' => 'true',
3851 ) );
3852 } else {
3853 if ( '401' == $httpCode ) {
3854 $error_message = esc_html__( 'invalid API key', 'bloom' );
3855 } else {
3856 $error_message = sanitize_text_field( $httpCode );
3857 }
3858 }
3859
3860 return $error_message;
3861 }
3862
3863 /**
3864 * Subscribes to Campaign Monitor list. Returns either "success" string or error message.
3865 * @return string
3866 */
3867 function subscribe_campaign_monitor( $api_key, $email, $list_id, $name = '' ) {
3868 if ( ! class_exists( 'CS_REST_Subscribers' ) ) {
3869 require_once( ET_BLOOM_PLUGIN_DIR . 'subscription/createsend-php-4.0.2/csrest_subscribers.php' );
3870 }
3871
3872 $auth = array(
3873 'api_key' => $api_key,
3874 );
3875 $wrap = new CS_REST_Subscribers( $list_id, $auth);
3876 $is_subscribed = $wrap->get( $email );
3877
3878 if ( $is_subscribed->was_successful() ) {
3879 $error_message = esc_html__( 'Already subscribed', 'bloom' );
3880 } else {
3881 $result = $wrap->add( array(
3882 'EmailAddress' => sanitize_email( $email ),
3883 'Name' => sanitize_text_field( $name ),
3884 'Resubscribe' => false,
3885 ) );
3886 if( $result->was_successful() ) {
3887 $error_message = 'success';
3888 } else {
3889 $error_message = $result->response->message;
3890 }
3891 }
3892
3893 return $error_message;
3894 }
3895
3896 /**
3897 * Retrieves the lists via Mad Mimi API and updates the data in DB.
3898 * @return string
3899 */
3900 function get_madmimi_lists( $username, $api_key, $name ) {
3901 $lists = array();
3902
3903 $name = sanitize_text_field( $name );
3904 $username = sanitize_text_field( $username );
3905 $api_key = sanitize_text_field( $api_key );
3906
3907 $request_url = esc_url_raw( 'https://api.madmimi.com/audience_lists/lists.json?username=' . rawurlencode( $username ) . '&api_key=' . $api_key );
3908
3909 $theme_request = wp_remote_get( $request_url, array( 'timeout' => 30 ) );
3910
3911 $response_code = wp_remote_retrieve_response_code( $theme_request );
3912
3913 if ( ! is_wp_error( $theme_request ) && $response_code == 200 ){
3914 $theme_response = json_decode( wp_remote_retrieve_body( $theme_request ), true );
3915 if ( ! empty( $theme_response ) ) {
3916 $error_message = 'success';
3917
3918 foreach ( $theme_response as $list_data ) {
3919 $lists[$list_data['id']]['name'] = $list_data['name'];
3920 $lists[$list_data['id']]['subscribers_count'] = $list_data['list_size'];
3921 $lists[$list_data['id']]['growth_week'] = $this->calculate_growth_rate( 'madmimi_' . $list_data['id'] );
3922 }
3923
3924 $this->update_account( 'madmimi', $name, array(
3925 'api_key' => esc_html( $api_key ),
3926 'username' => esc_html( $username ),
3927 'lists' => $lists,
3928 'is_authorized' => esc_html( 'true' ),
3929 ) );
3930
3931 } else {
3932 $error_message = esc_html__( 'Please make sure you have at least 1 list in your account and try again', 'bloom' );
3933 }
3934 } else {
3935 if ( is_wp_error( $theme_request ) ) {
3936 $error_message = $theme_request->get_error_message();
3937 } else {
3938 switch ( $response_code ) {
3939 case '401' :
3940 $error_message = esc_html__( 'Invalid Username or API key', 'bloom' );
3941 break;
3942
3943 default :
3944 $error_message = $response_code;
3945 break;
3946 }
3947 }
3948 }
3949
3950 return $error_message;
3951 }
3952
3953 /**
3954 * Subscribes to Mad Mimi list. Returns either "success" string or error message.
3955 * @return string
3956 */
3957 function subscribe_madmimi( $username, $api_key, $list_id, $email, $name = '', $last_name = '' ) {
3958 // check whether the user already subscribed
3959 $check_user_url = esc_url_raw( sprintf(
3960 'https://api.madmimi.com/audience_members/%1$s/lists.json?username=%2$s&api_key=%3$s',
3961 rawurlencode( sanitize_email( $email ) ),
3962 rawurlencode( sanitize_text_field( $username ) ),
3963 sanitize_text_field( $api_key )
3964 ));
3965
3966 $check_user_request = wp_remote_get( $check_user_url, array( 'timeout' => 30 ) );
3967 $check_user_response_code = wp_remote_retrieve_response_code( $check_user_request );
3968
3969 if ( ! is_wp_error( $check_user_request ) && $check_user_response_code == 200 ){
3970 $check_user_response = json_decode( wp_remote_retrieve_body( $check_user_request ), true );
3971 if ( ! empty( $check_user_response ) ) {
3972 // check whether current email subscribed to current list and return if true
3973 foreach( $check_user_response as $list ) {
3974 if ( ( int ) $list_id === ( int ) $list['id'] ) {
3975 return esc_html__( 'Already subscribed', 'bloom' );
3976 }
3977 }
3978 }
3979
3980 // if user is not subscribed yet - try to subscribe
3981 $request_url = esc_url_raw( sprintf(
3982 'https://api.madmimi.com/audience_lists/%1$s/add?email=%2$s&first_name=%3$s&last_name=%4$s&username=%5$s&api_key=%6$s',
3983 sanitize_text_field( $list_id ),
3984 rawurlencode( sanitize_email( $email ) ),
3985 sanitize_text_field( $name ),
3986 sanitize_text_field( $last_name ),
3987 rawurlencode( $username ),
3988 sanitize_text_field( $api_key )
3989 ));
3990
3991 $theme_request = wp_remote_post( $request_url, array( 'timeout' => 30 ) );
3992
3993 $response_code = wp_remote_retrieve_response_code( $theme_request );
3994
3995 if ( ! is_wp_error( $theme_request ) && $response_code == 200 ){
3996 $error_message = 'success';
3997 } else {
3998 if ( is_wp_error( $theme_request ) ) {
3999 $error_message = $theme_request->get_error_message();
4000 } else {
4001 switch ( $response_code ) {
4002 case '401' :
4003 $error_message = esc_html__( 'Invalid Username or API key', 'bloom' );
4004 break;
4005 case '400' :
4006 $error_message = wp_remote_retrieve_body( $theme_request );
4007 break;
4008
4009 default :
4010 $error_message = $response_code;
4011 break;
4012 }
4013 }
4014 }
4015 } else {
4016 if ( is_wp_error( $theme_request ) ) {
4017 $error_message = $theme_request->get_error_message();
4018 } else {
4019 switch ( $response_code ) {
4020 case '401' :
4021 $error_message = esc_html__( 'Invalid Username or API key', 'bloom' );
4022 break;
4023 default :
4024 $error_message = $response_code;
4025 break;
4026 }
4027 }
4028 }
4029
4030 return $error_message;
4031 }
4032
4033 /**
4034 * Retrieves the lists via iContact API and updates the data in DB.
4035 * @return string
4036 */
4037 function get_icontact_lists( $app_id, $username, $password, $name ) {
4038 $lists = array();
4039 $account_id = '';
4040 $folder_id = '';
4041
4042 $request_account_id_url = esc_url_raw( 'https://app.icontact.com/icp/a/' );
4043
4044 $account_data = $this->icontacts_remote_request( $request_account_id_url, $app_id, $username, $password );
4045
4046 if ( is_array( $account_data ) ) {
4047 $account_id = $account_data['accounts'][0]['accountId'];
4048
4049 if ( '' !== $account_id ) {
4050 $request_folder_id_url = esc_url_raw( 'https://app.icontact.com/icp/a/' . $account_id . '/c' );
4051
4052 $folder_data = $this->icontacts_remote_request( $request_folder_id_url, $app_id, $username, $password );
4053
4054 if ( is_array( $folder_data ) ) {
4055 $folder_id = $folder_data['clientfolders'][0]['clientFolderId'];
4056
4057 $request_lists_url = esc_url_raw( 'https://app.icontact.com/icp/a/' . $account_id . '/c/' . $folder_id . '/lists' );
4058 $lists_data = $this->icontacts_remote_request( $request_lists_url, $app_id, $username, $password );
4059
4060 if ( is_array( $lists_data ) ) {
4061 $error_message = 'success';
4062 foreach ( $lists_data['lists'] as $single_list ) {
4063 $lists[$single_list['listId']]['name'] = sanitize_text_field( $single_list['name'] );
4064 $lists[$single_list['listId']]['account_id'] = sanitize_text_field( $account_id );
4065 $lists[$single_list['listId']]['folder_id'] = sanitize_text_field( $folder_id );
4066
4067 //request for subscribers
4068 $request_contacts_url = esc_url_raw( 'https://app.icontact.com/icp/a/' . $account_id . '/c/' . $folder_id . '/contacts?status=total&listId=' . $single_list['listId'] );
4069 $subscribers_data = $this->icontacts_remote_request( $request_contacts_url, $app_id, $username, $password );
4070 $total_subscribers = isset( $subscribers_data['total'] ) ? sanitize_text_field( $subscribers_data['total'] ) : 0;
4071
4072 $lists[$single_list['listId']]['subscribers_count'] = $total_subscribers;
4073 $lists[$single_list['listId']]['growth_week'] = $this->calculate_growth_rate( 'icontact_' . $single_list['listId'] );
4074 }
4075
4076 $this->update_account( 'icontact', $name, array(
4077 'client_id' => sanitize_text_field( $app_id ),
4078 'username' => sanitize_text_field( $username ),
4079 'password' => sanitize_text_field( $password ),
4080 'lists' => $lists,
4081 'is_authorized' => 'true',
4082 ) );
4083 } else {
4084 $error_message = $lists_data;
4085 }
4086 } else {
4087 $error_message = $folder_data;
4088 }
4089 } else {
4090 $error_message = esc_html__( 'Account ID is not defined', 'bloom' );
4091 }
4092 } else {
4093 $error_message = $account_data;
4094 }
4095
4096 return $error_message;
4097 }
4098
4099 /**
4100 * Subscribes to iContact list. Returns either "success" string or error message.
4101 * @return string
4102 */
4103 function subscribe_icontact( $app_id, $username, $password, $folder_id, $account_id, $list_id, $email, $name = '', $last_name = '' ) {
4104 // prepare URL which will check whether current email subscribed to current list
4105 $check_subscription_url = esc_url_raw( sprintf(
4106 'https://app.icontact.com/icp/a/%1$s/c/%2$s/contacts?listId=%3$s&email=%4$s',
4107 sanitize_text_field( $account_id ),
4108 sanitize_text_field( $folder_id ),
4109 sanitize_text_field( $list_id ),
4110 rawurlencode( sanitize_email( $email ) )
4111 ));
4112 $is_subscribed = $this->icontacts_remote_request( $check_subscription_url, $app_id, $username, $password );
4113 if ( is_array( $is_subscribed ) ) {
4114 // if current email is not subscribed to current list, then subscribe
4115 if ( empty( $is_subscribed['contacts'] ) ) {
4116 $add_body = '[{
4117 "email":"' . $email .'",
4118 "firstName":"' . $name . '",
4119 "lastName":"' . $last_name . '",
4120 "status":"normal"
4121 }]';
4122 $add_subscriber_url = esc_url_raw( 'https://app.icontact.com/icp/a/' . $account_id . '/c/' . $folder_id . '/contacts/' );
4123
4124 $added_account = $this->icontacts_remote_request( $add_subscriber_url, $app_id, $username, $password, true, $add_body );
4125 if ( is_array( $added_account ) ) {
4126 if ( ! empty( $added_account['contacts'][0]['contactId'] ) ) {
4127 $map_contact = '[{
4128 "contactId":' . $added_account['contacts'][0]['contactId'] . ',
4129 "listId":' . $list_id . ',
4130 "status":"normal"
4131 }]';
4132 $map_subscriber_url = esc_url_raw( 'https://app.icontact.com/icp/a/' . $account_id . '/c/' . $folder_id . '/subscriptions/' );
4133
4134 $add_to_list = $this->icontacts_remote_request( $map_subscriber_url, $app_id, $username, $password, true, $map_contact );
4135 }
4136 $error_message = 'success';
4137 } else {
4138 $error_message = $added_account;
4139 }
4140 } else {
4141 $error_message = esc_html__( 'Already subscribed', 'bloom' );
4142 }
4143 } else {
4144 $error_message = $is_subscribed;
4145 }
4146
4147 return $error_message;
4148 }
4149
4150 /**
4151 * Executes remote request to iContacts API
4152 * @return string
4153 */
4154 function icontacts_remote_request( $request_url, $app_id, $username, $password, $is_post = false, $body = '' ) {
4155 if ( false === $is_post ) {
4156 $theme_request = wp_remote_get( $request_url, array(
4157 'timeout' => 30,
4158 'headers' => array(
4159 'Accept' => 'application/json',
4160 'Content-Type' => 'application/json',
4161 'Api-Version' => '2.0',
4162 'Api-AppId' => sanitize_text_field( $app_id ),
4163 'Api-Username' => sanitize_text_field( $username ),
4164 'API-Password' => sanitize_text_field( $password ),
4165 )
4166 ) );
4167 } else {
4168 $theme_request = wp_remote_post( $request_url, array(
4169 'timeout' => 30,
4170 'headers' => array(
4171 'Accept' => 'application/json',
4172 'Content-Type' => 'application/json',
4173 'Api-Version' => '2.0',
4174 'Api-AppId' => sanitize_text_field( $app_id ),
4175 'Api-Username' => sanitize_text_field( $username ),
4176 'API-Password' => sanitize_text_field( $password ),
4177 ),
4178 'body' => $body,
4179 ) );
4180 }
4181
4182 $response_code = wp_remote_retrieve_response_code( $theme_request );
4183 if ( ! is_wp_error( $theme_request ) && $response_code == 200 ){
4184 $theme_response = wp_remote_retrieve_body( $theme_request );
4185 if ( ! empty( $theme_response ) ) {
4186 $error_message = json_decode( wp_remote_retrieve_body( $theme_request ), true );
4187 } else {
4188 $error_message = esc_html__( 'empty response', 'bloom' );
4189 }
4190 } else {
4191 if ( is_wp_error( $theme_request ) ) {
4192 $error_message = $theme_request->get_error_message();
4193 } else {
4194 switch ( $response_code ) {
4195 case '401' :
4196 $error_message = esc_html__( 'Invalid App ID, Username or Password', 'bloom' );
4197 break;
4198
4199 default :
4200 $error_message = $response_code;
4201 break;
4202 }
4203 }
4204 }
4205
4206 return $error_message;
4207 }
4208
4209 /**
4210 * Retrieves the lists via GetResponse API and updates the data in DB.
4211 * @return string
4212 */
4213 function get_getresponse_lists( $api_key, $name ) {
4214 if ( ! function_exists( 'curl_init' ) ) {
4215 return;
4216 }
4217 $lists = array();
4218
4219 if ( ! class_exists( 'ET_Getresponse' ) ) {
4220 require_once( ET_BLOOM_PLUGIN_DIR . 'subscription/getresponse/et_getresponse_api.php' );
4221 }
4222
4223 $api = new ET_Getresponse( $api_key );
4224
4225 $campaigns = (array) $api->get_campaigns();
4226
4227 if ( ! empty( $campaigns['result'] ) ) {
4228 $error_message = 'success';
4229
4230 foreach( $campaigns['result'] as $id => $details ) {
4231 $lists[$id]['name'] = sanitize_text_field( $details['name'] );
4232
4233 $contacts = (array) $api->get_contacts( array( $id ) );
4234
4235 $total_contacts = count( $contacts['result'] );
4236 $lists[$id]['subscribers_count'] = sanitize_text_field( $total_contacts );
4237
4238 $lists[$id]['growth_week'] = $this->calculate_growth_rate( 'getresponse_' . $id );
4239 }
4240
4241 $this->update_account( 'getresponse', $name, array(
4242 'api_key' => sanitize_text_field( $api_key ),
4243 'lists' => $lists,
4244 'is_authorized' => sanitize_text_field( 'true' ),
4245 ) );
4246 } else {
4247 $error_message = esc_html__( 'Invalid API key or something went wrong during Authorization request', 'bloom' );
4248 }
4249
4250 return $error_message;
4251 }
4252
4253 /**
4254 * Subscribes to GetResponse list. Returns either "success" string or error message.
4255 * @return string
4256 */
4257 function subscribe_get_response( $list, $email, $api_key, $name = '-' ) {
4258 if ( ! function_exists( 'curl_init' ) ) {
4259 return;
4260 }
4261
4262 if ( ! class_exists( 'ET_Getresponse' ) ) {
4263 require_once( ET_BLOOM_PLUGIN_DIR . 'subscription/getresponse/et_getresponse_api.php' );
4264 }
4265
4266 $name = '' == $name ? '-' : $name;
4267
4268 $client = new ET_Getresponse( $api_key );
4269 $result = $client->add_contact(
4270 array(
4271 'campaign' => sanitize_text_field( $list ),
4272 'name' => sanitize_text_field( $name ),
4273 'email' => sanitize_email( $email ),
4274 'cycle_day' => 0,
4275 'action' => 'standard',
4276 )
4277 );
4278
4279 if ( isset( $result['result']['queued'] ) && 1 === $result['result']['queued'] ) {
4280 $result = 'success';
4281 } else {
4282 if ( isset( $result['error']['message'] ) ) {
4283 $result = $result['error']['message'];
4284 } else {
4285 $result = 'unknown error';
4286 }
4287 }
4288
4289 return $result;
4290 }
4291
4292 /**
4293 * Retrieves the lists via Sendinblue API and updates the data in DB.
4294 * @return string
4295 */
4296 function get_sendinblue_lists( $api_key, $name ) {
4297 $lists = array();
4298
4299 if ( ! function_exists( 'curl_init' ) ) {
4300 return esc_html__( 'curl_init is not defined ', 'bloom' );
4301 }
4302
4303 if ( ! class_exists( 'Mailin' ) ) {
4304 require_once( ET_BLOOM_PLUGIN_DIR . 'subscription/sendinblue-v2.0/mailin.php' );
4305 }
4306
4307 $mailin = new Mailin( 'https://api.sendinblue.com/v2.0', $api_key );
4308 $page = 1;
4309 $page_limit = 50;
4310 $all_lists = array();
4311 $need_request = true;
4312
4313 while ( true == $need_request ) {
4314 $lists_array = $mailin->get_lists( $page, $page_limit );
4315 $all_lists = array_merge( $all_lists, $lists_array );
4316 if ( 50 > count( $lists_array ) ) {
4317 $need_request = false;
4318 } else {
4319 $page++;
4320 }
4321 }
4322
4323 if ( ! empty( $all_lists ) ) {
4324 if ( isset( $all_lists['code'] ) && 'success' === $all_lists['code'] ) {
4325 $error_message = 'success';
4326
4327 if ( ! empty( $all_lists['data']['lists'] ) ) {
4328 foreach( $all_lists['data']['lists'] as $single_list ) {
4329 $lists[$single_list['id']]['name'] = sanitize_text_field( $single_list['name'] );
4330
4331 $total_contacts = isset( $single_list['total_subscribers'] ) ? $single_list['total_subscribers'] : 0;
4332 $lists[$single_list['id']]['subscribers_count'] = sanitize_text_field( $total_contacts );
4333
4334 $lists[$single_list['id']]['growth_week'] = $this->calculate_growth_rate( 'sendinblue_' . $single_list['id'] );
4335 }
4336 }
4337
4338 $this->update_account( 'sendinblue', $name, array(
4339 'api_key' => sanitize_text_field( $api_key ),
4340 'lists' => $lists,
4341 'is_authorized' => 'true',
4342 ) );
4343 } else {
4344 $error_message = $all_lists['message'];
4345 }
4346 } else {
4347 $error_message = esc_html__( 'Invalid API key or something went wrong during Authorization request', 'bloom' );
4348 }
4349
4350 return $error_message;
4351 }
4352
4353 /**
4354 * Subscribes to Sendinblue list. Returns either "success" string or error message.
4355 * @return string
4356 */
4357 function subscribe_sendinblue( $api_key, $email, $list_id, $name, $last_name = '' ) {
4358 if ( ! function_exists( 'curl_init' ) ) {
4359 return esc_html__( 'curl_init is not defined ', 'bloom' );
4360 }
4361
4362 if ( ! class_exists( 'Mailin' ) ) {
4363 require_once( ET_BLOOM_PLUGIN_DIR . 'subscription/sendinblue-v2.0/mailin.php' );
4364 }
4365
4366 $mailin = new Mailin( 'https://api.sendinblue.com/v2.0', $api_key );
4367 $user = $mailin->get_user( $email );
4368
4369 //check whether current email subscribed to current list
4370 if ( ! empty( $user['data']['listid'] ) ) {
4371 foreach ( $user['data']['listid'] as $single_list_id ) {
4372 if ( (int) $list_id === (int) $single_list_id ) {
4373 return esc_html__( 'Already subscribed', 'bloom' );
4374 }
4375 }
4376 }
4377
4378 // subscribe current email to current list if not subscribed yet
4379 $attributes = array(
4380 "NAME" => sanitize_text_field( $name ),
4381 "SURNAME" => sanitize_text_field( $last_name ),
4382 );
4383 $blacklisted = 0;
4384 $listid = array( $list_id );
4385 $listid_unlink = array();
4386 $blacklisted_sms = 0;
4387
4388 $result = $mailin->create_update_user( $email, $attributes, $blacklisted, $listid, $listid_unlink, $blacklisted_sms );
4389
4390 if ( 'success' == $result['code'] ) {
4391 $error_message = 'success';
4392 } else {
4393 if ( ! empty( $result['message'] ) ) {
4394 $error_message = $result['message'];
4395 } else {
4396 $error_message = esc_html__( 'Unknown error', 'bloom' );
4397 }
4398 }
4399
4400 return $error_message;
4401 }
4402
4403 /**
4404 * Retrieves the lists from MailPoet table and updates the data in DB.
4405 * @return string
4406 */
4407 function get_mailpoet_lists( $name ) {
4408 $lists = array();
4409
4410 global $wpdb;
4411 $table_name = $wpdb->prefix . 'wysija_list';
4412 $table_users = $wpdb->prefix . 'wysija_user_list';
4413
4414 if ( ! class_exists( 'WYSIJA' ) ) {
4415 $error_message = esc_html__( 'MailPoet plugin is not installed or not activated', 'bloom' );
4416 } else {
4417 $list_model = WYSIJA::get( 'list', 'model' );
4418 $all_lists_array = $list_model->get( array( 'name', 'list_id' ), array( 'is_enabled' => '1' ) );
4419
4420 $error_message = 'success';
4421
4422 if ( ! empty( $all_lists_array ) ) {
4423 foreach ( $all_lists_array as $list_details ) {
4424 $lists[$list_details['list_id']]['name'] = sanitize_text_field( $list_details['name'] );
4425
4426 $user_model = WYSIJA::get( 'user_list', 'model' );
4427 $all_subscribers_array = $user_model->get( array( 'user_id' ), array( 'list_id' => $list_details['list_id'] ) );
4428
4429 $subscribers_count = count( $all_subscribers_array );
4430 $lists[$list_details['list_id']]['subscribers_count'] = sanitize_text_field( $subscribers_count );
4431
4432 $lists[$list_details['list_id']]['growth_week'] = $this->calculate_growth_rate( 'mailpoet_' . $list_details['list_id'] );
4433 }
4434 }
4435
4436 $this->update_account( 'mailpoet', $name, array(
4437 'lists' => $lists,
4438 'is_authorized' => 'true',
4439 ) );
4440 }
4441
4442 return $error_message;
4443 }
4444
4445 /**
4446 * Subscribes to MailPoet list. Returns either "success" string or error message.
4447 * @return string
4448 */
4449 function subscribe_mailpoet( $list_id, $email, $name = '', $last_name = '' ) {
4450 global $wpdb;
4451 $table_user = $wpdb->prefix . 'wysija_user';
4452 $table_user_lists = $wpdb->prefix . 'wysija_user_list';
4453
4454 if ( ! class_exists( 'WYSIJA' ) ) {
4455 $error_message = esc_html__( 'MailPoet plugin is not installed or not activated', 'bloom' );
4456 } else {
4457 $sql_user_id = "SELECT user_id FROM $table_user WHERE email = %s";
4458 $sql_args = array(
4459 $email,
4460 );
4461
4462 // get the ID of subscriber if he's in the list already
4463 $subscriber_id = $wpdb->get_var( $wpdb->prepare( $sql_user_id, $sql_args ) );
4464 $already_subscribed = 0;
4465
4466 // if current email is subscribed, then check whether it subscribed to the current list
4467 if ( ! empty( $subscriber_id ) ) {
4468 $sql_is_subscribed = "SELECT COUNT(*) FROM $table_user_lists WHERE user_id = %s AND list_id = %s";
4469 $sql_args = array(
4470 $subscriber_id,
4471 $list_id,
4472 );
4473
4474 $already_subscribed = (int) $wpdb->get_var( $wpdb->prepare( $sql_is_subscribed, $sql_args ) );
4475 }
4476
4477 // if email is not subscribed to current list, then subscribe.
4478 if ( 0 === $already_subscribed ) {
4479 $new_user = array(
4480 'user' => array(
4481 'email' => $email,
4482 'firstname' => $name,
4483 'lastname' => $last_name
4484 ),
4485
4486 'user_list' => array( 'list_ids' => array( $list_id ) )
4487 );
4488
4489 $mailpoet_class = WYSIJA::get( 'user', 'helper' );
4490 $error_message = $mailpoet_class->addSubscriber( $new_user );
4491 $error_message = is_int( $error_message ) ? 'success' : $error_message;
4492 } else {
4493 $error_message = esc_html__( 'Already Subscribed', 'bloom' );
4494 }
4495 }
4496
4497 return $error_message;
4498 }
4499
4500 /**
4501 * Retrieves the lists via AWeber API and updates the data in DB.
4502 * @return string
4503 */
4504 function get_aweber_lists( $api_key, $name ) {
4505 $options_array = ET_Bloom::get_bloom_options();
4506 $lists = array();
4507
4508 if ( ! isset( $options_array['accounts']['aweber'][$name]['consumer_key'] ) || ( $api_key != $options_array['accounts']['aweber'][$name]['api_key'] ) ) {
4509 $error_message = $this->aweber_authorization( $api_key, $name );
4510 } else {
4511 $error_message = 'success';
4512 }
4513
4514 if ( 'success' === $error_message ) {
4515 if ( ! class_exists( 'AWeberAPI' ) ) {
4516 require_once( ET_BLOOM_PLUGIN_DIR . 'subscription/aweber/aweber_api.php' );
4517 }
4518
4519 $account = $this->get_aweber_account( $name );
4520
4521 if ( $account ) {
4522 $aweber_lists = $account->lists;
4523 if ( isset( $aweber_lists ) ) {
4524 foreach ( $aweber_lists as $list ) {
4525 $lists[$list->id]['name'] = sanitize_text_field( $list->name );
4526
4527 $total_subscribers = $list->total_subscribers;
4528 $lists[$list->id]['subscribers_count'] = sanitize_text_field( $total_subscribers );
4529
4530 $lists[$list->id]['growth_week'] = $this->calculate_growth_rate( 'aweber_' . $list->id );
4531 }
4532 }
4533 }
4534
4535 $this->update_account( 'aweber', $name, array( 'lists' => $lists ) );
4536 }
4537
4538 return $error_message;
4539 }
4540
4541 /**
4542 * Subscribes to Aweber list. Returns either "success" string or error message.
4543 * @return string
4544 */
4545 function subscribe_aweber( $list_id, $account_name, $email, $name = '' ) {
4546 if ( ! class_exists( 'AWeberAPI' ) ) {
4547 require_once( ET_BLOOM_PLUGIN_DIR . 'subscription/aweber/aweber_api.php' );
4548 }
4549
4550 $account = $this->get_aweber_account( $account_name );
4551
4552 if ( ! $account ) {
4553 $error_message = esc_html__( 'Aweber: Wrong configuration data', 'bloom' );
4554 }
4555
4556 try {
4557 $list_url = "/accounts/{$account->id}/lists/{$list_id}";
4558 $list = $account->loadFromUrl( $list_url );
4559
4560 $new_subscriber = $list->subscribers->create(
4561 array(
4562 'email' => $email,
4563 'name' => $name,
4564 )
4565 );
4566
4567 $error_message = 'success';
4568 } catch ( Exception $exc ) {
4569 $error_message = $exc->message;
4570 }
4571
4572 return $error_message;
4573 }
4574
4575 /**
4576 * Retrieves the tokens from AWeber
4577 * @return string
4578 */
4579 function aweber_authorization( $api_key, $name ) {
4580
4581 if ( ! class_exists( 'AWeberAPI' ) ) {
4582 require_once( ET_BLOOM_PLUGIN_DIR . 'subscription/aweber/aweber_api.php' );
4583 }
4584
4585 try {
4586 $auth = AWeberAPI::getDataFromAweberID( $api_key );
4587
4588 if ( ! ( is_array( $auth ) && 4 === count( $auth ) ) ) {
4589 $error_message = esc_html__( 'Authorization code is invalid. Try regenerating it and paste in the new code.', 'bloom' );
4590 } else {
4591 $error_message = 'success';
4592 list( $consumer_key, $consumer_secret, $access_key, $access_secret ) = $auth;
4593
4594 $this->update_account( 'aweber', $name, array(
4595 'api_key' => sanitize_text_field( $api_key ),
4596 'consumer_key' => sanitize_text_field( $consumer_key ),
4597 'consumer_secret' => sanitize_text_field( $consumer_secret ),
4598 'access_key' => sanitize_text_field( $access_key ),
4599 'access_secret' => sanitize_text_field( $access_secret ),
4600 'is_authorized' => 'true',
4601 ) );
4602 }
4603 } catch ( AWeberAPIException $exc ) {
4604 $error_message = sprintf(
4605 '<p>%4$s</p>
4606 <ul>
4607 <li>%5$s: %1$s</li>
4608 <li>%6$s: %2$s</li>
4609 <li>%7$s: %3$s</li>
4610 </ul>',
4611 esc_html( $exc->type ),
4612 esc_html( $exc->message ),
4613 esc_html( $exc->documentation_url ),
4614 esc_html__( 'AWeberAPIException.', 'bloom' ),
4615 esc_html__( 'Type', 'bloom' ),
4616 esc_html__( 'Message', 'bloom' ),
4617 esc_html__( 'Documentation', 'bloom' )
4618 );
4619 }
4620
4621 return $error_message;
4622 }
4623
4624 /**
4625 * Creates Aweber account using the data saved to plugin's database.
4626 * @return object or false
4627 */
4628 function get_aweber_account( $name ) {
4629 if ( ! class_exists( 'AWeberAPI' ) ) {
4630 require_once( get_template_directory() . '/includes/subscription/aweber/aweber_api.php' );
4631 }
4632
4633 $options_array = ET_Bloom::get_bloom_options();
4634 $account = false;
4635
4636 if ( isset( $options_array['accounts']['aweber'][$name] ) ) {
4637 $consumer_key = sanitize_text_field( $options_array['accounts']['aweber'][$name]['consumer_key'] );
4638 $consumer_secret = sanitize_text_field( $options_array['accounts']['aweber'][$name]['consumer_secret'] );
4639 $access_key = sanitize_text_field( $options_array['accounts']['aweber'][$name]['access_key'] );
4640 $access_secret = sanitize_text_field( $options_array['accounts']['aweber'][$name]['access_secret'] );
4641
4642 try {
4643 // Aweber requires curl extension to be enabled
4644 if ( ! function_exists( 'curl_init' ) ) {
4645 return false;
4646 }
4647
4648 $aweber = new AWeberAPI( $consumer_key, $consumer_secret );
4649
4650 if ( ! $aweber ) {
4651 return false;
4652 }
4653
4654 $account = $aweber->getAccount( $access_key, $access_secret );
4655 } catch ( Exception $exc ) {
4656 return false;
4657 }
4658 }
4659
4660 return $account;
4661 }
4662
4663 /**
4664 * Retrieves the lists via feedblitz API and updates the data in DB.
4665 * @return string
4666 */
4667 function get_feedblitz_lists( $api_key, $name ) {
4668 $lists = array();
4669
4670 $request_url = esc_url_raw( 'https://www.feedblitz.com/f.api/syndications?key=' . $api_key );
4671
4672 $theme_request = wp_remote_get( $request_url, array( 'timeout' => 30, 'sslverify' => false ) );
4673
4674 $response_code = wp_remote_retrieve_response_code( $theme_request );
4675
4676 if ( ! is_wp_error( $theme_request ) && $response_code == 200 ){
4677 $theme_response = $this->xml_to_array( wp_remote_retrieve_body( $theme_request ) );
4678
4679 if ( ! empty( $theme_response ) ) {
4680 if ( 'ok' == $theme_response['rsp']['@attributes']['stat'] ) {
4681 $error_message = 'success';
4682 $lists_array = $theme_response['syndications']['syndication'];
4683
4684 if ( ! empty( $lists_array ) ) {
4685 foreach( $lists_array as $list_data ) {
4686 $lists[$list_data['id']]['name'] = sanitize_text_field( $list_data['name'] );
4687 $lists[$list_data['id']]['subscribers_count'] = sanitize_text_field( $list_data['subscribersummary']['subscribers'] );
4688
4689 $lists[$list_data['id']]['growth_week'] = $this->calculate_growth_rate( 'feedblitz_' . $list_data['id'] );
4690 }
4691 }
4692
4693 $this->update_account( 'feedblitz', $name, array(
4694 'api_key' => sanitize_text_field( $api_key ),
4695 'lists' => $lists,
4696 'is_authorized' => 'true',
4697 ) );
4698 } else {
4699 $error_message = isset( $theme_response['rsp']['err']['@attributes']['msg'] ) ? $theme_response['rsp']['err']['@attributes']['msg'] : esc_html__( 'Unknown error', 'bloom' );
4700 }
4701
4702 } else {
4703 $error_message = esc_html__( 'empty response', 'bloom' );
4704 }
4705 } else {
4706 if ( is_wp_error( $theme_request ) ) {
4707 $error_message = $theme_request->get_error_message();
4708 } else {
4709 $error_message = $response_code;
4710 }
4711 }
4712
4713 return $error_message;
4714
4715 }
4716
4717 /**
4718 * Subscribes to feedblitz list. Returns either "success" string or error message.
4719 * @return string
4720 */
4721 function subscribe_feedblitz( $api_key, $list_id, $name, $email = '', $last_name = '' ) {
4722 $request_url = esc_url_raw( 'https://www.feedblitz.com/f?SimpleApiSubscribe&key=' . $api_key . '&email=' . rawurlencode( $email ) . '&listid=' . $list_id . '&FirstName=' . $name . '&LastName=' . $last_name );
4723 $theme_request = wp_remote_get( $request_url, array( 'timeout' => 30, 'sslverify' => false ) );
4724
4725 $response_code = wp_remote_retrieve_response_code( $theme_request );
4726
4727 if ( ! is_wp_error( $theme_request ) && $response_code == 200 ){
4728 $theme_response = $this->xml_to_array( wp_remote_retrieve_body( $theme_request ) );
4729 if ( ! empty( $theme_response ) ) {
4730 if ( 'ok' == $theme_response['rsp']['@attributes']['stat'] ) {
4731 if ( empty( $theme_response['rsp']['success']['@attributes']['msg'] ) ) {
4732 $error_message = 'success';
4733 } else {
4734 $error_message = $theme_response['rsp']['success']['@attributes']['msg'];
4735 }
4736 } else {
4737 $error_message = isset( $theme_response['rsp']['err']['@attributes']['msg'] ) ? $theme_response['rsp']['err']['@attributes']['msg'] : esc_html__( 'Unknown error', 'bloom' );
4738 }
4739 } else {
4740 $error_message = esc_html__( 'empty response', 'bloom' );
4741 }
4742 } else {
4743 if ( is_wp_error( $theme_request ) ) {
4744 $error_message = $theme_request->get_error_message();
4745 } else {
4746 $error_message = $response_code;
4747 }
4748 }
4749
4750 return $error_message;
4751 }
4752
4753 /**
4754 * Retrieves the lists via OntraPort API and updates the data in DB.
4755 * @return string
4756 */
4757 function get_ontraport_lists( $api_key, $app_id, $name ) {
4758 $appid = $app_id;
4759 $key = $api_key;
4760 $lists = array();
4761 $list_id_array = array();
4762
4763 // get sequences (lists)
4764 $req_type = "fetch_sequences";
4765 $postargs = sprintf(
4766 'appid=%1$s&key=%2$s&reqType=%3$s',
4767 sanitize_text_field( $appid ),
4768 sanitize_text_field( $key ),
4769 sanitize_text_field( $req_type )
4770 );
4771 $request = "https://api.ontraport.com/cdata.php";
4772 $result = $this->ontraport_request( $postargs, $request );
4773 $lists_array = $this->xml_to_array( $result );
4774 $lists_id = simplexml_load_string( $result );
4775
4776 foreach ( $lists_id->sequence as $value ) {
4777 $list_id_array[] = (int) $value->attributes()->id;
4778 }
4779
4780 if ( is_array( $lists_array ) ) {
4781 $error_message = 'success';
4782 if ( ! empty( $lists_array['sequence'] ) ) {
4783 $sequence_array = is_array( $lists_array['sequence'] )
4784 ? $lists_array['sequence']
4785 : $lists_array;
4786
4787 $i = 0;
4788
4789 foreach( $sequence_array as $id => $list_name ) {
4790 $lists[$list_id_array[$i]]['name'] = sanitize_text_field( $list_name );
4791
4792 // we cannot get amount of subscribers for each sequence due to API limitations, so set it to 0.
4793 $lists[$list_id_array[$i]]['subscribers_count'] = 0;
4794
4795 $lists[$list_id_array[$i]]['growth_week'] = $this->calculate_growth_rate( 'ontraport_' . $list_id_array[$i] );
4796 $i++;
4797 }
4798 }
4799 $this->update_account( 'ontraport', $name, array(
4800 'api_key' => sanitize_text_field( $api_key ),
4801 'client_id' => sanitize_text_field( $app_id ),
4802 'lists' => $lists,
4803 'is_authorized' => 'true',
4804 ) );
4805 } else {
4806 $error_message = $lists_array;
4807 }
4808
4809 return $error_message;
4810 }
4811
4812 function subscribe_ontraport( $app_id, $api_key, $name, $email, $list_id, $last_name = '' ) {
4813
4814// Construct contact data in XML format
4815$data = <<<STRING
4816<contact>
4817<Group_Tag name="Contact Information">
4818<field name="First Name">
4819STRING;
4820$data .= sanitize_text_field( $name );
4821$data .= <<<STRING
4822</field>
4823<field name="Last Name">
4824STRING;
4825$data .= sanitize_text_field( $last_name );
4826$data .= <<<STRING
4827</field>
4828<field name="Email">
4829STRING;
4830$data .= sanitize_email( $email );
4831$data .= <<<STRING
4832</field>
4833</Group_Tag>
4834<Group_Tag name="Sequences and Tags">
4835<field name="Contact Tags"></field>
4836<field name="Sequences">*/*
4837STRING;
4838$data .= sanitize_text_field( $list_id );
4839$data .= <<<STRING
4840*/*</field>
4841</Group_Tag>
4842</contact>
4843STRING;
4844
4845 $data = urlencode( urlencode( $data ) );
4846 $reqType = "add";
4847 $postargs = sprintf(
4848 'appid=%1$s&key=%2$s&return_id=1&reqType=%3$s&data=%4$s',
4849 sanitize_text_field( $app_id ),
4850 sanitize_text_field( $api_key ),
4851 sanitize_text_field( $reqType ),
4852 $data
4853 );
4854
4855 $result = $this->ontraport_request( $postargs );
4856 $user_array = $this->xml_to_array( $result );
4857
4858 if ( isset( $user_array['status'] ) && 'Success' == $user_array['status'] ) {
4859 $error_message = 'success';
4860 } else {
4861 $error_message = esc_html__( 'Error occured during subscription', 'bloom' );
4862 }
4863
4864 return $error_message;
4865 }
4866
4867 /**
4868 * Performs the request to OntraPort API and handles the response
4869 * @return xml
4870 */
4871 function ontraport_request( $postargs ) {
4872 if ( ! function_exists( 'curl_init' ) ) {
4873 $response = esc_html__( 'curl_init is not defined ', 'bloom' );
4874 } else {
4875 $response = '';
4876 $httpCode = '';
4877 // Get cURL resource
4878 $curl = curl_init();
4879 // Set some options
4880 curl_setopt_array( $curl, array(
4881 CURLOPT_RETURNTRANSFER => 1,
4882 CURLOPT_HEADER => FALSE,
4883 CURLOPT_URL => "https://api.ontraport.com/cdata.php",
4884 CURLOPT_POST => TRUE,
4885 CURLOPT_POSTFIELDS => $postargs,
4886 CURLOPT_SSL_VERIFYPEER => FALSE, //we need this option since we perform request to https
4887 ) );
4888 // Send the request & save response to $resp
4889 $response = curl_exec( $curl );
4890 $httpCode = curl_getinfo( $curl, CURLINFO_HTTP_CODE );
4891 // Close request to clear up some resources
4892 curl_close( $curl );
4893
4894 if ( 200 == $httpCode ) {
4895 $response = $response;
4896 } else {
4897 $response = $httpCode;
4898 }
4899 }
4900
4901 return $response;
4902 }
4903
4904 /**
4905 * Converts xml data to array
4906 * @return array
4907 */
4908 function xml_to_array( $xml_data ) {
4909 $xml = simplexml_load_string( $xml_data );
4910 $json = json_encode( $xml );
4911 $array = json_decode( $json, true );
4912
4913 return $array;
4914 }
4915
4916 /**
4917 * Generates output for the "Form Integration" options.
4918 * @return string
4919 */
4920 function generate_accounts_list() {
4921 if ( ! wp_verify_nonce( $_POST['retrieve_lists_nonce'] , 'retrieve_lists' ) ) {
4922 die( -1 );
4923 }
4924
4925 if ( ! current_user_can( 'manage_options' ) ) {
4926 die( -1 );
4927 }
4928
4929 $service = !empty( $_POST['bloom_service'] ) ? sanitize_text_field( $_POST['bloom_service'] ) : '';
4930 $optin_id = !empty( $_POST['bloom_optin_id'] ) ? sanitize_text_field( $_POST['bloom_optin_id'] ) : '';
4931 $new_account = !empty( $_POST['bloom_add_account'] ) ? sanitize_text_field( $_POST['bloom_add_account'] ) : '';
4932
4933 $options_array = ET_Bloom::get_bloom_options();
4934 $current_account = isset( $options_array[$optin_id]['account_name'] ) ? $options_array[$optin_id]['account_name'] : 'empty';
4935
4936 $available_accounts = array();
4937
4938 if ( isset( $options_array['accounts'] ) ) {
4939 if ( isset( $options_array['accounts'][$service] ) ) {
4940 foreach ( $options_array['accounts'][$service] as $account_name => $details ) {
4941 $available_accounts[] = sanitize_text_field( $account_name );
4942 }
4943 }
4944 }
4945
4946 if ( ! empty( $available_accounts ) && '' === $new_account ) {
4947 printf(
4948 '<li class="select et_dashboard_select_account">
4949 <p>%1$s</p>
4950 <select name="et_dashboard[account_name]" data-service="%4$s">
4951 <option value="empty" %3$s>%2$s</option>
4952 <option value="add_new">%5$s</option>',
4953 esc_html__( 'Select Account', 'bloom' ),
4954 esc_html__( 'Select One...', 'bloom' ),
4955 selected( 'empty', $current_account, false ),
4956 esc_attr( $service ),
4957 esc_html__( 'Add Account', 'bloom' )
4958 );
4959
4960 if ( ! empty( $available_accounts ) ) {
4961 foreach ( $available_accounts as $account ) {
4962 printf( '<option value="%1$s" %3$s>%2$s</option>',
4963 esc_attr( $account ),
4964 esc_html( $account ),
4965 selected( $account, $current_account, false )
4966 );
4967 }
4968 }
4969
4970 printf( '
4971 </select>
4972 </li>' );
4973 } else {
4974 $form_fields = $this->generate_new_account_form( $service );
4975
4976 printf(
4977 '<li class="select et_dashboard_select_account et_dashboard_new_account">
4978 %3$s
4979 <button class="et_dashboard_icon authorize_service" data-service="%2$s">%1$s</button>
4980 <span class="spinner"></span>
4981 </li>',
4982 esc_html__( 'Add Account', 'bloom' ),
4983 esc_attr( $service ),
4984 $form_fields
4985 );
4986 }
4987
4988 die();
4989 }
4990
4991 /**
4992 * Generates fields for the account authorization form based on the service
4993 * @return string
4994 */
4995 function generate_new_account_form( $service, $account_name = '', $display_name = true ) {
4996 if ( ! current_user_can( 'manage_options' ) ) {
4997 die( -1 );
4998 }
4999
5000 $field_values = '';
5001
5002 if ( '' !== $account_name ) {
5003 $options_array = ET_Bloom::get_bloom_options();
5004 $field_values = $options_array['accounts'][$service][$account_name];
5005 }
5006
5007 $form_fields = sprintf(
5008 '<div class="account_settings_fields" data-service="%1$s">',
5009 esc_attr( $service )
5010 );
5011
5012 if ( true === $display_name ) {
5013 $form_fields .= sprintf( '
5014 <div class="et_dashboard_account_row">
5015 <label for="%1$s">%2$s</label>
5016 <input type="text" value="%3$s" id="%1$s">%4$s
5017 </div>',
5018 esc_attr( 'name_' . $service ),
5019 esc_html__( 'Account Name', 'bloom' ),
5020 esc_attr( $account_name ),
5021 ET_Bloom::generate_hint( esc_html__( 'Enter the name for your account', 'bloom' ), true )
5022 );
5023 }
5024
5025 $provider = ET_Bloom_Email_Providers::get_provider( $service );
5026 if ( $provider ) {
5027 foreach ( $provider->get_fields() as $field_name => $field ) {
5028 $form_fields .= sprintf(
5029 '<div class="et_dashboard_account_row">
5030 <label for="%1$s">%2$s</label>
5031 <input%6$s class="provider_field_%5$s%7$s" value="%3$s" id="%1$s">%4$s
5032 </div>',
5033 esc_attr( $field_name . '_' . $service ),
5034 esc_html( $field['label'] ),
5035 ( ! empty( $field_values ) && isset( $field_values[ $field_name ] ) ? esc_attr( $field_values[ $field_name ] ) : '' ),
5036 ET_Bloom::generate_hint( sprintf(
5037 '<a href="http://www.elegantthemes.com/plugins/bloom/documentation/accounts/" target="_blank">%1$s</a>',
5038 esc_html__( 'Click here for more information', 'bloom' )
5039 ), false ),
5040 esc_attr( $service ),
5041 ( isset( $field['apply_password_mask'] ) && ! $field['apply_password_mask'] ? '' : ' type="password"' ),
5042 ( isset( $field['not_required'] ) && $field['not_required'] ? ' et_dashboard_not_required' : '' )
5043 );
5044 }
5045 }
5046
5047 switch ( $service ) {
5048 case 'madmimi' :
5049
5050 $form_fields .= sprintf( '
5051 <div class="et_dashboard_account_row">
5052 <label for="%1$s">%3$s</label>
5053 <input type="password" value="%5$s" id="%1$s">%7$s
5054 </div>
5055 <div class="et_dashboard_account_row">
5056 <label for="%2$s">%4$s</label>
5057 <input type="password" value="%6$s" id="%2$s">%7$s
5058 </div>',
5059 esc_attr( 'username_' . $service ),
5060 esc_attr( 'api_key_' . $service ),
5061 esc_html__( 'Username', 'bloom' ),
5062 esc_html__( 'API key', 'bloom' ),
5063 ( '' !== $field_values && isset( $field_values['username'] ) ) ? esc_html( $field_values['username'] ) : '',
5064 ( '' !== $field_values && isset( $field_values['api_key'] ) ) ? esc_html( $field_values['api_key'] ) : '',
5065 ET_Bloom::generate_hint( sprintf(
5066 '<a href="http://www.elegantthemes.com/plugins/bloom/documentation/accounts/" target="_blank">%1$s</a>',
5067 esc_html__( 'Click here for more information', 'bloom' )
5068 ), false
5069 )
5070 );
5071
5072 break;
5073
5074 case 'mailchimp' :
5075 case 'constant_contact' :
5076 case 'getresponse' :
5077 case 'sendinblue' :
5078 case 'campaign_monitor' :
5079 case 'feedblitz' :
5080 case 'hubspot' :
5081
5082 $form_fields .= sprintf( '
5083 <div class="et_dashboard_account_row">
5084 <label for="%1$s">%2$s</label>
5085 <input type="password" value="%3$s" id="%1$s">%4$s
5086 </div>',
5087 esc_attr( 'api_key_' . $service ),
5088 esc_html__( 'API key', 'bloom' ),
5089 ( '' !== $field_values && isset( $field_values['api_key'] ) ) ? esc_attr( $field_values['api_key'] ) : '',
5090 ET_Bloom::generate_hint( sprintf(
5091 '<a href="http://www.elegantthemes.com/plugins/bloom/documentation/accounts/" target="_blank">%1$s</a>',
5092 esc_html__( 'Click here for more information', 'bloom' )
5093 ), false
5094 )
5095 );
5096
5097 $form_fields .= ( 'constant_contact' == $service ) ?
5098 sprintf(
5099 '<div class="et_dashboard_account_row">
5100 <label for="%1$s">%2$s</label>
5101 <input type="password" value="%3$s" id="%1$s">%4$s
5102 </div>',
5103 esc_attr( 'token_' . $service ),
5104 esc_html__( 'Token', 'bloom' ),
5105 ( '' !== $field_values && isset( $field_values['token'] ) ) ? esc_attr( $field_values['token'] ) : '',
5106 ET_Bloom::generate_hint( sprintf(
5107 '<a href="http://www.elegantthemes.com/plugins/bloom/documentation/accounts/" target="_blank">%1$s</a>',
5108 esc_html__( 'Click here for more information', 'bloom' )
5109 ), false )
5110 )
5111 : '';
5112
5113 break;
5114
5115 case 'aweber' :
5116 $app_id = 'e233dabd';
5117 $aweber_auth_endpoint = 'https://auth.aweber.com/1.0/oauth/authorize_app/' . $app_id;
5118
5119 $form_fields .= sprintf( '
5120 <div class="et_dashboard_account_row et_dashboard_aweber_row">%1$s%2$s</div>',
5121 sprintf(
5122 __( 'Step 1: <a href="%1$s" target="_blank">Generate authorization code</a><br/>', 'bloom' ),
5123 esc_url( $aweber_auth_endpoint )
5124 ),
5125 sprintf( '
5126 %2$s
5127 <input type="password" value="%3$s" id="%1$s">',
5128 esc_attr( 'api_key_' . $service ),
5129 esc_html__( 'Step 2: Paste in the authorization code and click "Authorize" button: ', 'bloom' ),
5130 ( '' !== $field_values && isset( $field_values['api_key'] ) )
5131 ? esc_attr( $field_values['api_key'] )
5132 : ''
5133 )
5134 );
5135 break;
5136
5137 case 'icontact' :
5138 $form_fields .= sprintf('
5139 <div class="et_dashboard_account_row">%1$s</div>',
5140 sprintf( '
5141 <div class="et_dashboard_account_row">
5142 <label for="%1$s">%4$s</label>
5143 <input type="password" value="%7$s" id="%1$s">%10$s
5144 </div>
5145 <div class="et_dashboard_account_row">
5146 <label for="%2$s">%5$s</label>
5147 <input type="password" value="%8$s" id="%2$s">%10$s
5148 </div>
5149 <div class="et_dashboard_account_row">
5150 <label for="%3$s">%6$s</label>
5151 <input type="password" value="%9$s" id="%3$s">%10$s
5152 </div>',
5153 esc_attr( 'client_id_' . $service ),
5154 esc_attr( 'username_' . $service ),
5155 esc_attr( 'password_' . $service ),
5156 esc_html__( 'App ID', 'bloom' ),
5157 esc_html__( 'Username', 'bloom' ),
5158 esc_html__( 'Password', 'bloom' ),
5159 ( '' !== $field_values && isset( $field_values['client_id'] ) ) ? esc_html( $field_values['client_id'] ) : '',
5160 ( '' !== $field_values && isset( $field_values['username'] ) ) ? esc_html( $field_values['username'] ) : '',
5161 ( '' !== $field_values && isset( $field_values['password'] ) ) ? esc_html( $field_values['password'] ) : '',
5162 ET_Bloom::generate_hint( sprintf(
5163 '<a href="http://www.elegantthemes.com/plugins/bloom/documentation/accounts/" target="_blank">%1$s</a>',
5164 esc_html__( 'Click here for more information', 'bloom' )
5165 ), false )
5166 )
5167 );
5168 break;
5169
5170 case 'ontraport' :
5171 $form_fields .= sprintf('
5172 <div class="et_dashboard_account_row">
5173 <label for="%1$s">%3$s</label>
5174 <input type="password" value="%5$s" id="%1$s">%7$s
5175 </div>
5176 <div class="et_dashboard_account_row">
5177 <label for="%2$s">%4$s</label>
5178 <input type="password" value="%6$s" id="%2$s">%7$s
5179 </div>',
5180 esc_attr( 'api_key_' . $service ),
5181 esc_attr( 'client_id_' . $service ),
5182 esc_html__( 'API key', 'bloom' ),
5183 esc_html__( 'APP ID', 'bloom' ),
5184 ( '' !== $field_values && isset( $field_values['api_key'] ) ) ? esc_attr( $field_values['api_key'] ) : '',
5185 ( '' !== $field_values && isset( $field_values['client_id'] ) ) ? esc_attr( $field_values['client_id'] ) : '',
5186 ET_Bloom::generate_hint( sprintf(
5187 '<a href="http://www.elegantthemes.com/plugins/bloom/documentation/accounts/" target="_blank">%1$s</a>',
5188 esc_html__( 'Click here for more information', 'bloom' )
5189 ), false )
5190 );
5191 break;
5192
5193 case 'infusionsoft' :
5194 $form_fields .= sprintf( '
5195 <div class="et_dashboard_account_row">
5196 <label for="%1$s">%3$s</label>
5197 <input type="password" value="%5$s" id="%1$s">%7$s
5198 </div>
5199 <div class="et_dashboard_account_row">
5200 <label for="%2$s">%4$s</label>
5201 <input type="password" value="%6$s" id="%2$s">%7$s
5202 </div>',
5203 esc_attr( 'api_key_' . $service ),
5204 esc_attr( 'client_id_' . $service ),
5205 esc_html__( 'API Key', 'bloom' ),
5206 esc_html__( 'Application name', 'bloom' ),
5207 ( '' !== $field_values && isset( $field_values['api_key'] ) ) ? esc_attr( $field_values['api_key'] ) : '',
5208 ( '' !== $field_values && isset( $field_values['client_id'] ) ) ? esc_attr( $field_values['client_id'] ) : '',
5209 ET_Bloom::generate_hint( sprintf(
5210 '<a href="http://www.elegantthemes.com/plugins/bloom/documentation/accounts/" target="_blank">%1$s</a>',
5211 esc_html__( 'Click here for more information', 'bloom' )
5212 ), false )
5213 );
5214 break;
5215
5216 case 'salesforce':
5217 $form_fields .= sprintf(
5218 '<div class="et_dashboard_account_row">%1$s</div>',
5219 sprintf( '
5220 <div class="et_dashboard_account_row">
5221 <label for="%1$s">%2$s</label>
5222 <input type="text" value="%3$s" id="%1$s">%4$s
5223 </div>
5224 ',
5225 esc_attr( 'organization_id_' . $service ),
5226 esc_html__( 'Organization ID', 'bloom' ),
5227 ( '' !== $field_values && isset( $field_values['organization_id'] ) ) ? esc_attr( $field_values['organization_id'] ) : '',
5228 ET_Bloom::generate_hint( sprintf(
5229 '<a href="http://www.elegantthemes.com/plugins/bloom/documentation/accounts/" target="_blank">%1$s</a>',
5230 esc_html__( 'Click here for more information', 'bloom' )
5231 ), false )
5232 )
5233 );
5234 break;
5235 }
5236
5237 $form_fields .= '</div>';
5238
5239 return $form_fields;
5240 }
5241
5242 /**
5243 * Retrieves lists for specific account from Plugin options.
5244 * @return string
5245 */
5246 function retrieve_accounts_list( $service, $accounts_list = array() ) {
5247 $options_array = ET_Bloom::get_bloom_options();
5248 if ( isset( $options_array['accounts'] ) ) {
5249 if ( isset( $options_array['accounts'][$service] ) ) {
5250 foreach ( $options_array['accounts'][$service] as $account_name => $details ) {
5251 $accounts_list[$account_name] = sanitize_text_field( $account_name );
5252 }
5253 }
5254 }
5255
5256 return $accounts_list;
5257 }
5258
5259 /**
5260 * Generates the list of "Lists" for selected account in the Dashboard. Returns the generated form to jQuery.
5261 */
5262 function generate_mailing_lists( $service = '', $account_name = '' ) {
5263 if ( ! wp_verify_nonce( $_POST['retrieve_lists_nonce'] , 'retrieve_lists' ) ) {
5264 die( -1 );
5265 }
5266
5267 if ( ! current_user_can( 'manage_options' ) ) {
5268 die( -1 );
5269 }
5270
5271 $account_for = ! empty( $_POST['bloom_account_name'] ) ? sanitize_text_field( $_POST['bloom_account_name'] ) : '';
5272 $service = ! empty( $_POST['bloom_service'] ) ? sanitize_text_field( $_POST['bloom_service'] ) : '';
5273 $optin_id = ! empty( $_POST['bloom_optin_id'] ) ? sanitize_text_field( $_POST['bloom_optin_id'] ) : '';
5274
5275 // SalesForce integration works using web-to-lead with no list support
5276 if ( 'salesforce' === $service ) {
5277 die();
5278 }
5279
5280 $options_array = ET_Bloom::get_bloom_options();
5281 $current_email_list = isset( $options_array[$optin_id] ) ? $options_array[$optin_id]['email_list'] : 'empty';
5282
5283 $available_lists = array();
5284
5285 if ( isset( $options_array['accounts'] ) ) {
5286 if ( isset( $options_array['accounts'][$service] ) ) {
5287 foreach ( $options_array['accounts'][$service] as $account_name => $details ) {
5288 if ( $account_for == $account_name ) {
5289 if ( isset( $details['lists'] ) ) {
5290 $available_lists = $details['lists'];
5291 }
5292 }
5293 }
5294 }
5295 }
5296
5297 printf( '
5298 <li class="select et_dashboard_select_list">
5299 <p>%1$s</p>
5300 <select name="et_dashboard[email_list]">
5301 <option value="empty" %3$s>%2$s</option>',
5302 esc_html__( 'Select Email List', 'bloom' ),
5303 esc_html__( 'Select One...', 'bloom' ),
5304 selected( 'empty', $current_email_list, false )
5305 );
5306
5307 if ( ! empty( $available_lists ) ) {
5308 foreach ( $available_lists as $list_id => $list_details ) {
5309 printf( '<option value="%1$s" %3$s>%2$s</option>',
5310 esc_attr( $list_id ),
5311 esc_html( $list_details['name'] ),
5312 selected( $list_id, $current_email_list, false )
5313 );
5314 }
5315 }
5316
5317 printf( '
5318 </select>
5319 </li>' );
5320
5321 die();
5322 }
5323
5324
5325/**-------------------------**/
5326/** Front end **/
5327/**-------------------------**/
5328 function register_frontend_scripts() {
5329 wp_register_script( 'et_bloom-uniform-js', ET_BLOOM_PLUGIN_URI . '/js/jquery.uniform.min.js', array( 'jquery' ), $this->plugin_version, true );
5330 wp_register_script( 'et_bloom-custom-js', ET_BLOOM_PLUGIN_URI . '/js/custom.js', array( 'jquery' ), $this->plugin_version, true );
5331 wp_register_script( 'et_bloom-idle-timer-js', ET_BLOOM_PLUGIN_URI . '/js/idle-timer.min.js', array( 'jquery' ), $this->plugin_version, true );
5332 wp_register_style( 'et-gf-open-sans', esc_url_raw( "{$this->protocol}://fonts.googleapis.com/css?family=Open+Sans:400,700" ), array(), null );
5333 wp_register_style( 'et_bloom-css', ET_BLOOM_PLUGIN_URI . '/css/style.css', array(), $this->plugin_version );
5334 }
5335
5336 public static function load_scripts_styles() {
5337 // do not proceed if scripts have been enqueued already
5338 if ( ET_Bloom::$scripts_enqueued ) {
5339 return;
5340 }
5341
5342 wp_enqueue_script( 'et_bloom-uniform-js' );
5343 wp_enqueue_script( 'et_bloom-custom-js' );
5344 wp_enqueue_script( 'et_bloom-idle-timer-js' );
5345 wp_enqueue_style( 'et-gf-open-sans' );
5346 wp_enqueue_style( 'et_bloom-css' );
5347 wp_localize_script( 'et_bloom-custom-js', 'bloomSettings', array(
5348 'ajaxurl' => admin_url( 'admin-ajax.php', is_ssl() ? 'https' : 'http' ),
5349 'pageurl' => ( is_singular( get_post_types() ) ? get_permalink() : '' ),
5350 'stats_nonce' => wp_create_nonce( 'update_stats' ),
5351 'subscribe_nonce' => wp_create_nonce( 'subscribe' ),
5352 'is_user_logged_in' => is_user_logged_in() ? 'logged' : 'not_logged',
5353 ) );
5354
5355 ET_Bloom::$scripts_enqueued = true;
5356 }
5357
5358 /**
5359 * Generates the array of all taxonomies supported by Bloom.
5360 * Bloom fully supports only taxonomies from ET themes.
5361 * @return array
5362 */
5363 function get_supported_taxonomies( $post_types ) {
5364 $taxonomies = array();
5365
5366 if ( ! empty( $post_types ) ) {
5367 foreach( $post_types as $single_type ) {
5368 if ( 'post' != $single_type ) {
5369 $taxonomies[] = $this->get_tax_slug( $single_type );
5370 }
5371 }
5372 }
5373
5374 return $taxonomies;
5375 }
5376
5377 /**
5378 * Returns the slug for supported taxonomy based on post type.
5379 * Returns empty string if taxonomy is not supported
5380 * Bloom fully supports only taxonomies from ET themes.
5381 * @return string
5382 */
5383 function get_tax_slug( $post_type ) {
5384 $theme_name = wp_get_theme();
5385 $taxonomy = '';
5386
5387 switch ( $post_type ) {
5388 case 'project' :
5389 $taxonomy = 'project_category';
5390
5391 break;
5392
5393 case 'product' :
5394 $taxonomy = 'product_cat';
5395
5396 break;
5397
5398 case 'listing' :
5399 if ( 'Explorable' == $theme_name ) {
5400 $taxonomy = 'listing_type';
5401 } else {
5402 $taxonomy = 'listing_category';
5403 }
5404
5405 break;
5406
5407 case 'event' :
5408 $taxonomy = 'event_category';
5409
5410 break;
5411
5412 case 'gallery' :
5413 $taxonomy = 'gallery_category';
5414
5415 break;
5416
5417 case 'post' :
5418 $taxonomy = 'category';
5419
5420 break;
5421 }
5422
5423 return $taxonomy;
5424 }
5425
5426 /**
5427 * Returns true if form should be displayed on particular page depending on user settings.
5428 * @return bool
5429 */
5430 function check_applicability( $optin_id ) {
5431 $options_array = ET_Bloom::get_bloom_options();
5432
5433 $display_there = false;
5434
5435 $optin_type = sanitize_text_field( $options_array[ $optin_id ]['optin_type'] );
5436
5437 $current_optin_limits = array(
5438 'post_types' => $options_array[ $optin_id ]['post_types'],
5439 'categories' => $options_array[ $optin_id ]['post_categories'],
5440 'on_cat_select' => isset( $options_array[ $optin_id ]['display_on'] ) && in_array( 'category', $options_array[ $optin_id ]['display_on'] ) ? true : false,
5441 'pages_exclude' => is_array( $options_array[ $optin_id ]['pages_exclude'] ) ? $options_array[ $optin_id ]['pages_exclude'] : explode( ',', $options_array[ $optin_id ]['pages_exclude'] ),
5442 'pages_include' => is_array( $options_array[ $optin_id ]['pages_include'] ) ? $options_array[ $optin_id ]['pages_include'] : explode( ',', $options_array[ $optin_id ]['pages_include'] ),
5443 'posts_exclude' => is_array( $options_array[ $optin_id ]['posts_exclude'] ) ? $options_array[ $optin_id ]['posts_exclude'] : explode( ',', $options_array[ $optin_id ]['posts_exclude'] ),
5444 'posts_include' => is_array( $options_array[ $optin_id ]['posts_include'] ) ? $options_array[ $optin_id ]['posts_include'] : explode( ',', $options_array[ $optin_id ]['posts_include'] ),
5445 'on_tag_select' => isset( $options_array[ $optin_id ]['display_on'] ) && in_array( 'tags', $options_array[$optin_id]['display_on'] )
5446 ? true
5447 : false,
5448 'on_archive_select' => isset( $options_array[ $optin_id ]['display_on'] ) && in_array( 'archive', $options_array[ $optin_id ]['display_on'] )
5449 ? true
5450 : false,
5451 'homepage_select' => isset( $options_array[ $optin_id ]['display_on'] ) && in_array( 'home', $options_array[ $optin_id ]['display_on'] )
5452 ? true
5453 : false,
5454 'blog_select' => isset( $options_array[ $optin_id ]['display_on'] ) && in_array( 'blog', $options_array[ $optin_id ]['display_on'] )
5455 ? true
5456 : false,
5457 'everything_select' => isset( $options_array[ $optin_id ]['display_on'] ) && in_array( 'everything', $options_array[ $optin_id ]['display_on'] )
5458 ? true
5459 : false,
5460 'auto_select' => isset( $options_array[ $optin_id ]['post_categories']['auto_select'] )
5461 ? $options_array[ $optin_id ]['post_categories']['auto_select']
5462 : false,
5463 'previously_saved' => isset( $options_array[ $optin_id ]['post_categories']['previously_saved'] )
5464 ? explode( ',', $options_array[ $optin_id ]['post_categories']['previously_saved'] )
5465 : false,
5466 );
5467
5468 unset( $current_optin_limits['categories']['previously_saved'] );
5469
5470 $tax_to_check = $this->get_supported_taxonomies( $current_optin_limits['post_types'] );
5471
5472 if ( ( 'flyin' == $optin_type || 'pop_up' == $optin_type ) && true == $current_optin_limits['everything_select'] ) {
5473 if ( is_singular() ) {
5474 if ( ( is_singular( 'page' ) && ! in_array( get_the_ID(), $current_optin_limits['pages_exclude'] ) ) || ( ! is_singular( 'page' ) && ! in_array( get_the_ID(), $current_optin_limits['posts_exclude'] ) ) ) {
5475 $display_there = true;
5476 }
5477 } else {
5478 $display_there = true;
5479 }
5480 } else {
5481 if ( is_archive() && ( 'flyin' == $optin_type || 'pop_up' == $optin_type ) ) {
5482 if ( true == $current_optin_limits['on_archive_select'] ) {
5483 $display_there = true;
5484 } else {
5485 if ( ( ( is_category( $current_optin_limits['categories'] ) || ( ! empty( $tax_to_check ) && is_tax( $tax_to_check, $current_optin_limits['categories'] ) ) ) && true == $current_optin_limits['on_cat_select'] ) || ( is_tag() && true == $current_optin_limits['on_tag_select'] ) ) {
5486 $display_there = true;
5487 }
5488 }
5489 } else {
5490 if ( ET_Bloom::is_blogpage() ) {
5491 if ( true === $current_optin_limits['blog_select'] ) {
5492 $display_there = true;
5493 }
5494 } else {
5495 $page_id = ( ET_Bloom::is_homepage() && !is_page() ) ? 'homepage' : get_the_ID();
5496 $current_post_type = 'homepage' === $page_id ? 'home' : get_post_type( $page_id );
5497
5498 if ( is_singular() || ( 'home' === $current_post_type && ( in_array( $optin_type, array( 'flyin', 'pop_up' ) ) ) ) ) {
5499 if ( in_array( $page_id, $current_optin_limits['pages_include'] ) || in_array( $page_id, $current_optin_limits['posts_include'] ) ) {
5500 $display_there = true;
5501 }
5502
5503 if ( true == $current_optin_limits['homepage_select'] && ET_Bloom::is_homepage() ) {
5504 $display_there = true;
5505 }
5506 }
5507 }
5508
5509 if ( ! empty( $current_optin_limits['post_types'] ) && is_singular( $current_optin_limits['post_types'] ) ) {
5510
5511 switch ( $current_post_type ) {
5512 case 'page' :
5513 case 'home' :
5514 if ( ( 'home' == $current_post_type && ( 'flyin' == $optin_type || 'pop_up' == $optin_type ) ) || 'home' != $current_post_type ) {
5515 if ( ! in_array( $page_id, $current_optin_limits['pages_exclude'] ) ) {
5516 $display_there = true;
5517 }
5518 }
5519 break;
5520
5521 default :
5522 $taxonomy_slug = $this->get_tax_slug( $current_post_type );
5523
5524 if ( ! in_array( $page_id, $current_optin_limits['posts_exclude'] ) ) {
5525 if ( '' != $taxonomy_slug ) {
5526 $categories = get_the_terms( $page_id, $taxonomy_slug );
5527 $post_cats = array();
5528 if ( $categories ) {
5529 foreach ( $categories as $category ) {
5530 $post_cats[] = $category->term_id;
5531 }
5532 }
5533
5534 foreach ( $post_cats as $single_cat ) {
5535 if ( in_array( $single_cat, $current_optin_limits['categories'] ) ) {
5536 $display_there = true;
5537 }
5538 }
5539
5540 if ( false === $display_there && 1 == $current_optin_limits['auto_select'] ) {
5541 foreach ( $post_cats as $single_cat ) {
5542 if ( ! in_array( $single_cat, $current_optin_limits['previously_saved'] ) ) {
5543 $display_there = true;
5544 }
5545 }
5546 }
5547 } else {
5548 $display_there = true;
5549 }
5550 }
5551
5552 break;
5553 }
5554 }
5555 }
5556 }
5557
5558 return $display_there;
5559 }
5560
5561 /**
5562 * Calculates and returns the ID of optin which should be displayed if A/B testing is enabled
5563 * @return string
5564 */
5565 public static function choose_form_ab_test( $optin_id, $optins_set, $update_option = true ) {
5566 $chosen_form = $optin_id;
5567
5568 if( ! empty( $optins_set[$optin_id]['child_optins'] ) && 'active' == $optins_set[$optin_id]['test_status'] ) {
5569 $chosen_form = ( '-1' != $optins_set[$optin_id]['next_optin'] || empty( $optins_set[$optin_id]['next_optin'] ) )
5570 ? $optins_set[$optin_id]['next_optin']
5571 : $optin_id;
5572
5573 if ( '-1' == $optins_set[$optin_id]['next_optin'] ) {
5574 $next_optin = $optins_set[$optin_id]['child_optins'][0];
5575 } else {
5576 $child_forms_count = count( $optins_set[$optin_id]['child_optins'] );
5577
5578 for ( $i = 0; $i < $child_forms_count; $i++ ) {
5579 if ( $optins_set[$optin_id]['next_optin'] == $optins_set[$optin_id]['child_optins'][$i] ) {
5580 $current_optin_number = $i;
5581 }
5582 }
5583
5584 if ( ( $child_forms_count - 1 ) == $current_optin_number ) {
5585 $next_optin = '-1';
5586 } else {
5587 $next_optin = $optins_set[$optin_id]['child_optins'][$current_optin_number + 1];
5588 }
5589
5590 }
5591 if ( true === $update_option ) {
5592 $update_test_optin[$optin_id] = $optins_set[$optin_id];
5593 $update_test_optin[$optin_id]['next_optin'] = $next_optin;
5594 ET_Bloom::update_bloom_options( $update_test_optin );
5595 }
5596 }
5597
5598 return $chosen_form;
5599 }
5600
5601 /**
5602 * Handles the stats adding request via jQuery
5603 * @return void
5604 */
5605 function handle_stats_adding() {
5606 if ( ! wp_verify_nonce( $_POST['update_stats_nonce'] , 'update_stats' ) ) {
5607 die( -1 );
5608 }
5609
5610 $stats_data_json = str_replace( '\\', '' , $_POST[ 'stats_data_array' ] );
5611 $stats_data_array = json_decode( $stats_data_json, true );
5612
5613 ET_Bloom::add_stats_record( $stats_data_array['type'], $stats_data_array['optin_id'], $stats_data_array['page_id'], $stats_data_array['list_id'] );
5614
5615 die();
5616
5617 }
5618
5619 /**
5620 * Adds the record to stats table. Either conversion or impression for specific list on specific form on specific page.
5621 * @return void
5622 */
5623 public static function add_stats_record( $type, $optin_id, $page_id, $list_id ) {
5624 // do not update stats if visitor logged in
5625 if ( is_user_logged_in() ) {
5626 return;
5627 }
5628
5629 global $wpdb;
5630
5631 $row_added = false;
5632
5633 $table_name = $wpdb->prefix . 'et_bloom_stats';
5634
5635 $record_date = current_time( 'mysql' );
5636 $ip_address = $_SERVER[ 'REMOTE_ADDR' ];
5637
5638 $wpdb->insert(
5639 $table_name,
5640 array(
5641 'record_date' => sanitize_text_field( $record_date ),
5642 'optin_id' => sanitize_text_field( $optin_id ),
5643 'record_type' => sanitize_text_field( $type ),
5644 'page_id' => (int) $page_id,
5645 'list_id' => sanitize_text_field( $list_id ),
5646 'ip_address' => sanitize_text_field( $ip_address ),
5647 'removed_flag' => (int) 0,
5648 ),
5649 array(
5650 '%s', // record_date
5651 '%s', // optin_id
5652 '%s', // record_type
5653 '%d', // page_id
5654 '%s', // list_id
5655 '%s', // ip_address
5656 '%d', // removed_flag
5657 )
5658 );
5659
5660 $row_added = true;
5661
5662 return $row_added;
5663 }
5664
5665 /**
5666 * Saves the Updates Settings
5667 */
5668 function save_updates_tab() {
5669 if ( ! wp_verify_nonce( $_POST['updates_tab_nonce'] , 'updates_tab' ) ) {
5670 die( -1 );
5671 }
5672
5673 if ( ! current_user_can( 'manage_options' ) ) {
5674 die( -1 );
5675 }
5676
5677 $username = ! empty( $_POST['et_bloom_updates_username'] ) ? sanitize_text_field( $_POST['et_bloom_updates_username'] ) : '';
5678 $api_key = ! empty( $_POST['et_bloom_updates_api_key'] ) ? sanitize_text_field( $_POST['et_bloom_updates_api_key'] ) : '';
5679
5680 update_option( 'et_automatic_updates_options', array(
5681 'username' => $username,
5682 'api_key' => $api_key,
5683 ) );
5684
5685 die();
5686 }
5687
5688 // add marker at the bottom of the_content() for the "Trigger at bottom of post" option.
5689 function trigger_bottom_mark( $content ) {
5690 $content .= '<span class="et_bloom_bottom_trigger"></span>';
5691 return $content;
5692 }
5693
5694 /**
5695 * Generates the content for the optin.
5696 * @return string
5697 */
5698 public static function generate_form_content( $optin_id, $page_id, $details = array() ) {
5699 if ( empty( $details ) ) {
5700 $all_optins = ET_Bloom::get_bloom_options();
5701 $details = $all_optins[$optin_id];
5702 }
5703
5704 $hide_img_mobile_class = isset( $details['hide_mobile'] ) && '1' == $details['hide_mobile'] ? 'et_bloom_hide_mobile' : '';
5705 $image_animation_class = isset( $details['image_animation'] )
5706 ? esc_attr( ' et_bloom_image_' . $details['image_animation'] )
5707 : 'et_bloom_image_no_animation';
5708 $image_class = $hide_img_mobile_class . $image_animation_class . ' et_bloom_image';
5709
5710 // Translate all strings if WPML is enabled
5711 if ( function_exists ( 'icl_translate' ) ) {
5712 $optin_title = icl_translate( 'bloom', 'optin_title_' . $optin_id, $details['optin_title'] );
5713 $optin_message = icl_translate( 'bloom', 'optin_message_' . $optin_id, $details['optin_message'] );
5714 $email_text = icl_translate( 'bloom', 'email_text_' . $optin_id, $details['email_text'] );
5715 $first_name_text = icl_translate( 'bloom', 'name_text_' . $optin_id, $details['name_text'] );
5716 $single_name_text = icl_translate( 'bloom', 'single_name_text_' . $optin_id, $details['single_name_text'] );
5717 $last_name_text = icl_translate( 'bloom', 'last_name_' . $optin_id, $details['last_name'] );
5718 $button_text = icl_translate( 'bloom', 'button_text_' . $optin_id, $details['button_text'] );
5719 $success_text = icl_translate( 'bloom', 'success_message_' . $optin_id, $details['success_message'] );
5720 $footer_text = icl_translate( 'bloom', 'footer_text_' . $optin_id, $details['footer_text'] );
5721 } else {
5722 $optin_title = $details['optin_title'];
5723 $optin_message = $details['optin_message'];
5724 $email_text = $details['email_text'];
5725 $first_name_text = $details['name_text'];
5726 $single_name_text = $details['single_name_text'];
5727 $last_name_text = $details['last_name'];
5728 $button_text = $details['button_text'];
5729 $success_text = $details['success_message'];
5730 $footer_text = $details['footer_text'];
5731 }
5732
5733 $formatted_title = '<h2> </h2>' != $details['optin_title']
5734 ? str_replace( ' ', '', $optin_title )
5735 : '';
5736 $formatted_message = '' != $details['optin_message'] ? $optin_message : '';
5737 $formatted_footer = '' != $details['footer_text']
5738 ? sprintf(
5739 '<div class="et_bloom_form_footer">
5740 <p>%1$s</p>
5741 </div>',
5742 stripslashes( esc_html( $footer_text ) )
5743 )
5744 : '';
5745
5746 $is_single_name = ( isset( $details['display_name'] ) && '1' == $details['display_name'] ) ? false : true;
5747
5748 $output = sprintf( '
5749 <div class="et_bloom_form_container_wrapper clearfix">
5750 <div class="et_bloom_header_outer">
5751 <div class="et_bloom_form_header%1$s%13$s">
5752 %2$s
5753 %3$s
5754 %4$s
5755 </div>
5756 </div>
5757 <div class="et_bloom_form_content%5$s%6$s%7$s%12$s"%11$s>
5758 %8$s
5759 <div class="et_bloom_success_container">
5760 <span class="et_bloom_success_checkmark"></span>
5761 </div>
5762 <h2 class="et_bloom_success_message">%9$s</h2>
5763 %10$s
5764 </div>
5765 </div>
5766 <span class="et_bloom_close_button"></span>',
5767 ( 'right' == $details['image_orientation'] || 'left' == $details['image_orientation'] ) && 'widget' !== $details['optin_type']
5768 ? sprintf( ' split%1$s', 'right' == $details['image_orientation']
5769 ? ' image_right'
5770 : '' )
5771 : '',
5772 ( ( 'above' == $details['image_orientation'] || 'right' == $details['image_orientation'] || 'left' == $details['image_orientation'] ) && 'widget' !== $details['optin_type'] ) || ( 'above' == $details['image_orientation_widget'] && 'widget' == $details['optin_type'] )
5773 ? sprintf(
5774 '%1$s',
5775 empty( $details['image_url']['id'] )
5776 ? sprintf(
5777 '<img src="%1$s" alt="%2$s" %3$s>',
5778 esc_url( $details['image_url']['url'] ),
5779 esc_attr( wp_strip_all_tags( html_entity_decode( $formatted_title ) ) ),
5780 '' !== $image_class
5781 ? sprintf( 'class="%1$s"', esc_attr( $image_class ) )
5782 : ''
5783 )
5784 : wp_get_attachment_image( $details['image_url']['id'], 'bloom_image', false, array( 'class' => $image_class ) )
5785 )
5786 : '',
5787 ( '' !== $formatted_title || '' !== $formatted_message )
5788 ? sprintf(
5789 '<div class="et_bloom_form_text">
5790 %1$s%2$s
5791 </div>',
5792 stripslashes( html_entity_decode( $formatted_title, ENT_QUOTES, 'UTF-8' ) ),
5793 stripslashes( html_entity_decode( $formatted_message, ENT_QUOTES, 'UTF-8' ) )
5794 )
5795 : '',
5796 ( 'below' == $details['image_orientation'] && 'widget' !== $details['optin_type'] ) || ( isset( $details['image_orientation_widget'] ) && 'below' == $details['image_orientation_widget'] && 'widget' == $details['optin_type'] )
5797 ? sprintf(
5798 '%1$s',
5799 empty( $details['image_url']['id'] )
5800 ? sprintf(
5801 '<img src="%1$s" alt="%2$s" %3$s>',
5802 esc_url( $details['image_url']['url'] ),
5803 esc_attr( wp_strip_all_tags( html_entity_decode( $formatted_title ) ) ),
5804 '' !== $image_class ? sprintf( 'class="%1$s"', esc_attr( $image_class ) ) : ''
5805 )
5806 : wp_get_attachment_image( $details['image_url']['id'], 'bloom_image', false, array( 'class' => $image_class ) )
5807 )
5808 : '', //#5
5809 ( 'no_name' == $details['name_fields'] && ! ET_Bloom::is_only_name_support( $details['email_provider'] ) ) || ( ET_Bloom::is_only_name_support( $details['email_provider'] ) && $is_single_name )
5810 ? ' et_bloom_1_field'
5811 : sprintf(
5812 ' et_bloom_%1$s_fields',
5813 'first_last_name' == $details['name_fields'] && ! ET_Bloom::is_only_name_support( $details['email_provider'] )
5814 ? '3'
5815 : '2'
5816 ),
5817 'inline' == $details['field_orientation'] && 'bottom' == $details['form_orientation'] && 'widget' !== $details['optin_type']
5818 ? ' et_bloom_bottom_inline'
5819 : '',
5820 ( 'stacked' == $details['field_orientation'] && 'bottom' == $details['form_orientation'] ) || 'widget' == $details['optin_type']
5821 ? ' et_bloom_bottom_stacked'
5822 : '',
5823 'custom_html' == $details['email_provider']
5824 ? stripslashes( html_entity_decode( $details['custom_html'] ) )
5825 : sprintf( '
5826 %1$s
5827 <form method="post" class="clearfix">
5828 %3$s
5829 <p class="et_bloom_popup_input et_bloom_subscribe_email">
5830 <input placeholder="%2$s">
5831 </p>
5832
5833 <button data-optin_id="%4$s" data-service="%5$s" data-list_id="%6$s" data-page_id="%7$s" data-account="%8$s" data-disable_dbl_optin="%11$s" class="et_bloom_submit_subscription%12$s">
5834 <span class="et_bloom_subscribe_loader"></span>
5835 <span class="et_bloom_button_text et_bloom_button_text_color_%10$s">%9$s</span>
5836 </button>
5837 </form>',
5838 'basic_edge' == $details['edge_style'] || '' == $details['edge_style']
5839 ? ''
5840 : ET_Bloom::get_the_edge_code( $details['edge_style'], 'widget' == $details['optin_type'] ? 'bottom' : $details['form_orientation'] ),
5841 '' != $email_text ? stripslashes( esc_attr( $email_text ) ) : esc_attr__( 'Email', 'bloom' ),
5842 ( 'no_name' == $details['name_fields'] && ! ET_Bloom::is_only_name_support( $details['email_provider'] ) ) || ( ET_Bloom::is_only_name_support( $details['email_provider'] ) && $is_single_name )
5843 ? ''
5844 : sprintf(
5845 '<p class="et_bloom_popup_input et_bloom_subscribe_name">
5846 <input placeholder="%1$s%2$s" maxlength="50">
5847 </p>%3$s',
5848 'first_last_name' == $details['name_fields']
5849 ? sprintf(
5850 '%1$s',
5851 '' != $first_name_text
5852 ? stripslashes( esc_attr( $first_name_text ) )
5853 : esc_attr__( 'First Name', 'bloom' )
5854 )
5855 : '',
5856 ( 'first_last_name' != $details['name_fields'] )
5857 ? sprintf( '%1$s', '' != $single_name_text
5858 ? stripslashes( esc_attr( $single_name_text ) )
5859 : esc_attr__( 'Name', 'bloom' ) ) : '',
5860 'first_last_name' == $details['name_fields'] && ! ET_Bloom::is_only_name_support( $details['email_provider'] )
5861 ? sprintf( '
5862 <p class="et_bloom_popup_input et_bloom_subscribe_last">
5863 <input placeholder="%1$s" maxlength="50">
5864 </p>',
5865 '' != $last_name_text ? stripslashes( esc_attr( $last_name_text ) ) : esc_attr__( 'Last Name', 'bloom' )
5866 )
5867 : ''
5868 ),
5869 esc_attr( $optin_id ),
5870 esc_attr( $details['email_provider'] ), //#5
5871 esc_attr( $details['email_list'] ),
5872 esc_attr( $page_id ),
5873 esc_attr( $details['account_name'] ),
5874 '' != $button_text ? stripslashes( esc_html( $button_text ) ) : esc_html__( 'SUBSCRIBE!', 'bloom' ),
5875 isset( $details['button_text_color'] ) ? esc_attr( $details['button_text_color'] ) : '', // #10
5876 isset( $details['disable_dbl_optin'] ) && '1' === $details['disable_dbl_optin'] ? 'disable' : '',
5877 'locked' === $details['optin_type'] ? ' et_bloom_submit_subscription_locked' : '' // #12
5878 ), //#9
5879 '' != $success_text
5880 ? html_entity_decode( wp_kses( stripslashes( $success_text ), array(
5881 'a' => array(),
5882 'br' => array(),
5883 'span' => array(),
5884 'strong' => array(),
5885 ) ) )
5886 : esc_html__( 'You have Successfully Subscribed!', 'bloom' ), //#10
5887 $formatted_footer,
5888 'custom_html' == $details['email_provider']
5889 ? sprintf(
5890 ' data-optin_id="%1$s" data-service="%2$s" data-list_id="%3$s" data-page_id="%4$s" data-account="%5$s"',
5891 esc_attr( $optin_id ),
5892 'custom_form',
5893 'custom_form',
5894 esc_attr( $page_id ),
5895 'custom_form'
5896 )
5897 : '',
5898 'custom_html' == $details['email_provider'] ? ' et_bloom_custom_html_form' : '',
5899 isset( $details['header_text_color'] )
5900 ? sprintf(
5901 ' et_bloom_header_text_%1$s',
5902 esc_attr( $details['header_text_color'] )
5903 )
5904 : ' et_bloom_header_text_dark' //#14
5905 );
5906
5907 return $output;
5908 }
5909
5910 /**
5911 * Checks whether network supports only First Name
5912 * @return string
5913 */
5914 public static function is_only_name_support( $service ) {
5915 $single_name_networks = array(
5916 'aweber',
5917 'getresponse'
5918 );
5919 $result = in_array( $service, $single_name_networks );
5920
5921 return $result;
5922 }
5923
5924 /**
5925 * Generates the svg code for edges
5926 * @return bool
5927 */
5928 public static function get_the_edge_code( $style, $orientation ) {
5929 $output = '';
5930 switch ( $style ) {
5931 case 'wedge_edge' :
5932 $output = sprintf(
5933 '<svg class="triangle et_bloom_default_edge" xmlns="http://www.w3.org/2000/svg" version="1.1" width="%2$s" height="%3$s" viewBox="0 0 100 100" preserveAspectRatio="none">
5934 <path d="%1$s" fill=""></path>
5935 </svg>',
5936 'bottom' == $orientation ? 'M0 0 L50 100 L100 0 Z' : 'M0 0 L0 100 L100 50 Z',
5937 'bottom' == $orientation ? '100%' : '20',
5938 'bottom' == $orientation ? '20' : '100%'
5939 );
5940
5941 //if right or left orientation selected we still need to generate bottom edge to support responsive design
5942 if ( 'bottom' !== $orientation ) {
5943 $output .= sprintf(
5944 '<svg class="triangle et_bloom_responsive_edge" xmlns="http://www.w3.org/2000/svg" version="1.1" width="%2$s" height="%3$s" viewBox="0 0 100 100" preserveAspectRatio="none">
5945 <path d="%1$s" fill=""></path>
5946 </svg>',
5947 'M0 0 L50 100 L100 0 Z',
5948 '100%',
5949 '20'
5950 );
5951 }
5952
5953 break;
5954 case 'curve_edge' :
5955 $output = sprintf(
5956 '<svg class="curve et_bloom_default_edge" xmlns="http://www.w3.org/2000/svg" version="1.1" width="%2$s" height="%3$s" viewBox="0 0 100 100" preserveAspectRatio="none">
5957 <path d="%1$s"></path>
5958 </svg>',
5959 'bottom' == $orientation ? 'M0 0 C40 100 60 100 100 0 Z' : 'M0 0 C0 0 100 50 0 100 z',
5960 'bottom' == $orientation ? '100%' : '20',
5961 'bottom' == $orientation ? '20' : '100%'
5962 );
5963
5964 //if right or left orientation selected we still need to generate bottom edge to support responsive design
5965 if ( 'bottom' !== $orientation ) {
5966 $output .= sprintf(
5967 '<svg class="curve et_bloom_responsive_edge" xmlns="http://www.w3.org/2000/svg" version="1.1" width="%2$s" height="%3$s" viewBox="0 0 100 100" preserveAspectRatio="none">
5968 <path d="%1$s"></path>
5969 </svg>',
5970 'M0 0 C40 100 60 100 100 0 Z',
5971 '100%',
5972 '20'
5973 );
5974 }
5975
5976 break;
5977 }
5978
5979 return $output;
5980 }
5981
5982 /**
5983 * Displays the Flyin content on front-end.
5984 */
5985 function display_flyin() {
5986 $optins_set = $this->flyin_optins;
5987
5988 if ( ! empty( $optins_set ) ) {
5989 foreach( $optins_set as $optin_id => $details ) {
5990 if ( $this->check_applicability( $optin_id ) ) {
5991
5992 ET_Bloom::load_scripts_styles();
5993
5994 $display_optin_id = ET_Bloom::choose_form_ab_test( $optin_id, $optins_set );
5995
5996 if ( $display_optin_id != $optin_id ) {
5997 $all_optins = ET_Bloom::get_bloom_options();
5998 $optin_id = $display_optin_id;
5999 $details = $all_optins[$optin_id];
6000 }
6001
6002 if ( is_singular() || ET_Bloom::is_homepage() ) {
6003 $page_id = ET_Bloom::is_homepage() ? -1 : get_the_ID();
6004 } else {
6005 $page_id = 0;
6006 }
6007
6008 printf(
6009 '<div class="et_bloom_flyin et_bloom_optin et_bloom_resize et_bloom_flyin_%6$s et_bloom_%5$s%17$s%1$s%2$s%18$s%19$s%20$s%22$s%27$s%28$s"%3$s%4$s%16$s%21$s%26$s>
6010 <div class="et_bloom_form_container%7$s%8$s%9$s%10$s%12$s%13$s%14$s%15$s%23$s%24$s%25$s">
6011 %11$s
6012 </div>
6013 </div>',
6014 true == $details['post_bottom'] ? ' et_bloom_trigger_bottom' : '',
6015 isset( $details['trigger_idle'] ) && true == $details['trigger_idle'] ? ' et_bloom_trigger_idle' : '',
6016 isset( $details['trigger_auto'] ) && true == $details['trigger_auto']
6017 ? sprintf( ' data-delay="%1$s"', esc_attr( $details['load_delay'] ) )
6018 : '',
6019 true == $details['session']
6020 ? ' data-cookie_duration="' . esc_attr( $details['session_duration'] ) . '"'
6021 : '',
6022 esc_attr( $optin_id ), // #5
6023 esc_attr( $details['flyin_orientation'] ),
6024 'bottom' !== $details['form_orientation'] && 'custom_html' !== $details['email_provider']
6025 ? sprintf(
6026 ' et_bloom_form_%1$s',
6027 esc_attr( $details['form_orientation'] )
6028 )
6029 : ' et_bloom_form_bottom',
6030 'basic_edge' == $details['edge_style'] || '' == $details['edge_style']
6031 ? ''
6032 : sprintf( ' with_edge %1$s', esc_attr( $details['edge_style'] ) ),
6033 ( 'no_border' !== $details['border_orientation'] )
6034 ? sprintf(
6035 ' et_bloom_with_border et_bloom_border_%1$s%2$s',
6036 esc_attr( $details['border_style'] ),
6037 esc_attr( ' et_bloom_border_position_' . $details['border_orientation'] )
6038 )
6039 : '',
6040 ( 'rounded' == $details['corner_style'] ) ? ' et_bloom_rounded_corners' : '', //#10
6041 ET_Bloom::generate_form_content( $optin_id, $page_id ),
6042 'bottom' == $details['form_orientation'] && ( 'no_image' == $details['image_orientation'] || 'above' == $details['image_orientation'] || 'below' == $details['image_orientation'] ) && 'stacked' == $details['field_orientation']
6043 ? ' et_bloom_stacked_flyin'
6044 : '',
6045 ( 'rounded' == $details['field_corner'] ) ? ' et_bloom_rounded' : '',
6046 'light' == $details['text_color'] ? ' et_bloom_form_text_light' : ' et_bloom_form_text_dark',
6047 isset( $details['load_animation'] )
6048 ? sprintf(
6049 ' et_bloom_animation_%1$s',
6050 esc_attr( $details['load_animation'] )
6051 )
6052 : ' et_bloom_animation_no_animation', //#15
6053 isset( $details['trigger_idle'] ) && true == $details['trigger_idle']
6054 ? sprintf( ' data-idle_timeout="%1$s"', esc_attr( $details['idle_timeout'] ) )
6055 : '',
6056 isset( $details['trigger_auto'] ) && true == $details['trigger_auto']
6057 ? ' et_bloom_auto_popup'
6058 : '',
6059 isset( $details['comment_trigger'] ) && true == $details['comment_trigger']
6060 ? ' et_bloom_after_comment'
6061 : '',
6062 isset( $details['purchase_trigger'] ) && true == $details['purchase_trigger']
6063 ? ' et_bloom_after_purchase'
6064 : '', //#20
6065 isset( $details['trigger_scroll'] ) && true == $details['trigger_scroll']
6066 ? ' et_bloom_scroll'
6067 : '',
6068 isset( $details['trigger_scroll'] ) && true == $details['trigger_scroll']
6069 ? sprintf( ' data-scroll_pos="%1$s"', esc_attr( $details['scroll_pos'] ) )
6070 : '',
6071 isset( $details['hide_mobile_optin'] ) && true == $details['hide_mobile_optin']
6072 ? ' et_bloom_hide_mobile_optin'
6073 : '',
6074 ( 'no_name' == $details['name_fields'] && ! ET_Bloom::is_only_name_support( $details['email_provider'] ) ) || ( ET_Bloom::is_only_name_support( $details['email_provider'] ) && $is_single_name )
6075 ? ' et_flyin_1_field'
6076 : sprintf(
6077 ' et_flyin_%1$s_fields',
6078 'first_last_name' == $details['name_fields'] && ! ET_Bloom::is_only_name_support( $details['email_provider'] )
6079 ? '3'
6080 : '2'
6081 ),
6082 'inline' == $details['field_orientation'] && 'bottom' == $details['form_orientation']
6083 ? ' et_bloom_flyin_bottom_inline'
6084 : '',
6085 'stacked' == $details['field_orientation'] && 'bottom' == $details['form_orientation'] && ( 'right' == $details['image_orientation'] || 'left' == $details['image_orientation'] )
6086 ? ' et_bloom_flyin_bottom_stacked'
6087 : '', //#25
6088 isset( $details['trigger_click'] ) && $details['trigger_click']
6089 ? ' data-trigger_click="' . esc_attr( $details['trigger_click_selector'] ) . '"'
6090 : '',
6091 isset( $details['trigger_click'] ) && $details['trigger_click'] ? ' et_bloom_trigger_click' : '',
6092 isset( $details['auto_close'] ) && true === ( bool ) $details['auto_close'] ? ' et_bloom_auto_close' : '' //#28
6093 );
6094 }
6095 }
6096 }
6097 }
6098
6099 /**
6100 * Displays the PopUp content on front-end.
6101 */
6102 function display_popup() {
6103 $optins_set = $this->popup_optins;
6104
6105 if ( ! empty( $optins_set ) ) {
6106 foreach( $optins_set as $optin_id => $details ) {
6107 if ( $this->check_applicability( $optin_id ) ) {
6108
6109 ET_Bloom::load_scripts_styles();
6110
6111 $display_optin_id = ET_Bloom::choose_form_ab_test( $optin_id, $optins_set );
6112
6113 if ( $display_optin_id != $optin_id ) {
6114 $all_optins = ET_Bloom::get_bloom_options();
6115 $optin_id = $display_optin_id;
6116 $details = $all_optins[$optin_id];
6117 }
6118
6119 if ( is_singular() || ET_Bloom::is_homepage() ) {
6120 $page_id = ET_Bloom::is_homepage() ? -1 : get_the_ID();
6121 } else {
6122 $page_id = 0;
6123 }
6124
6125 printf(
6126 '<div class="et_bloom_popup et_bloom_optin et_bloom_resize et_bloom_%5$s%15$s%1$s%2$s%16$s%17$s%18$s%20$s%22$s%23$s"%3$s%4$s%14$s%19$s%21$s>
6127 <div class="et_bloom_form_container et_bloom_popup_container%6$s%7$s%8$s%9$s%11$s%12$s%13$s">
6128 %10$s
6129 </div>
6130 </div>',
6131 true == $details['post_bottom'] ? ' et_bloom_trigger_bottom' : '',
6132 isset( $details['trigger_idle'] ) && true == $details['trigger_idle']
6133 ? ' et_bloom_trigger_idle'
6134 : '',
6135 isset( $details['trigger_auto'] ) && true == $details['trigger_auto']
6136 ? sprintf( ' data-delay="%1$s"', esc_attr( $details['load_delay'] ) )
6137 : '',
6138 true == $details['session']
6139 ? ' data-cookie_duration="' . esc_attr( $details['session_duration'] ) . '"'
6140 : '',
6141 esc_attr( $optin_id ), // #5
6142 'bottom' !== $details['form_orientation'] && 'custom_html' !== $details['email_provider']
6143 ? sprintf( ' et_bloom_form_%1$s', esc_attr( $details['form_orientation'] ) )
6144 : ' et_bloom_form_bottom',
6145 'basic_edge' == $details['edge_style'] || '' == $details['edge_style']
6146 ? ''
6147 : sprintf( ' with_edge %1$s', esc_attr( $details['edge_style'] ) ),
6148 ( 'no_border' !== $details['border_orientation'] )
6149 ? sprintf(
6150 ' et_bloom_with_border et_bloom_border_%1$s%2$s',
6151 esc_attr( $details['border_style'] ),
6152 esc_attr( ' et_bloom_border_position_' . $details['border_orientation'] )
6153 )
6154 : '',
6155 ( 'rounded' == $details['corner_style'] ) ? ' et_bloom_rounded_corners' : '',
6156 ET_Bloom::generate_form_content( $optin_id, $page_id ), //#10
6157 ( 'rounded' == $details['field_corner'] ) ? ' et_bloom_rounded' : '',
6158 'light' == $details['text_color'] ? ' et_bloom_form_text_light' : ' et_bloom_form_text_dark',
6159 isset( $details['load_animation'] )
6160 ? sprintf( ' et_bloom_animation_%1$s', esc_attr( $details['load_animation'] ) )
6161 : ' et_bloom_animation_no_animation',
6162 isset( $details['trigger_idle'] ) && true == $details['trigger_idle']
6163 ? sprintf( ' data-idle_timeout="%1$s"', esc_attr( $details['idle_timeout'] ) )
6164 : '',
6165 isset( $details['trigger_auto'] ) && true == $details['trigger_auto'] ? ' et_bloom_auto_popup' : '', //#15
6166 isset( $details['comment_trigger'] ) && true == $details['comment_trigger'] ? ' et_bloom_after_comment' : '',
6167 isset( $details['purchase_trigger'] ) && true == $details['purchase_trigger'] ? ' et_bloom_after_purchase' : '',
6168 isset( $details['trigger_scroll'] ) && true == $details['trigger_scroll'] ? ' et_bloom_scroll' : '',
6169 isset( $details['trigger_scroll'] ) && true == $details['trigger_scroll']
6170 ? sprintf( ' data-scroll_pos="%1$s"', esc_attr( $details['scroll_pos'] ) )
6171 : '',
6172 ( isset( $details['hide_mobile_optin'] ) && true == $details['hide_mobile_optin'] )
6173 ? ' et_bloom_hide_mobile_optin'
6174 : '',
6175 isset( $details['trigger_click'] ) && $details['trigger_click']
6176 ? ' data-trigger_click="' . esc_attr( $details['trigger_click_selector'] ) . '"'
6177 : '',
6178 isset( $details['trigger_click'] ) && true == $details['trigger_click'] ? ' et_bloom_trigger_click' : '',
6179 isset( $details['auto_close'] ) && true === ( bool ) $details['auto_close'] ? ' et_bloom_auto_close' : '' //#23
6180 );
6181 }
6182 }
6183 }
6184 }
6185
6186 function display_preview() {
6187 if ( ! wp_verify_nonce( $_POST['bloom_preview_nonce'] , 'bloom_preview' ) ) {
6188 die( -1 );
6189 }
6190
6191 if ( ! current_user_can( 'manage_options' ) ) {
6192 die( -1 );
6193 }
6194
6195 $options = $_POST['preview_options'];
6196 $processed_string = str_replace( array( '%5B', '%5D' ), array( '[', ']' ), $options );
6197 parse_str( $processed_string, $processed_array );
6198 $details = $processed_array['et_dashboard'];
6199 $fonts_array = array();
6200
6201 if ( ! isset( $fonts_array[$details['header_font']] ) && isset( $details['header_font'] ) ) {
6202 $fonts_array[] = $details['header_font'];
6203 }
6204 if ( ! isset( $fonts_array[$details['body_font']] ) && isset( $details['body_font'] ) ) {
6205 $fonts_array[] = $details['body_font'];
6206 }
6207
6208 $popup_array['popup_code'] = $this->generate_preview_popup( $details );
6209 $popup_array['popup_css'] = '<style id="et_bloom_preview_css">' . ET_Bloom::generate_custom_css( '.et_bloom .et_bloom_preview_popup', $details ) . '</style>';
6210 $popup_array['fonts'] = $fonts_array;
6211
6212 die( json_encode( $popup_array ) );
6213 }
6214
6215 /**
6216 * Displays the PopUp preview in dashboard.
6217 */
6218 function generate_preview_popup( $details ) {
6219 if ( ! current_user_can( 'manage_options' ) ) {
6220 die( -1 );
6221 }
6222
6223 $output = '';
6224 $output = sprintf(
6225 '<div class="et_bloom_popup et_bloom_animated et_bloom_preview_popup et_bloom_optin et_bloom_preview_%8$s">
6226 <div class="et_bloom_form_container et_bloom_animation_fadein et_bloom_popup_container%1$s%2$s%3$s%4$s%5$s%6$s">
6227 %7$s
6228 </div>
6229 </div>',
6230 'bottom' !== $details['form_orientation'] && 'custom_html' !== $details['email_provider'] && 'widget' !== $details['optin_type']
6231 ? sprintf( ' et_bloom_form_%1$s', esc_attr( $details['form_orientation'] ) )
6232 : ' et_bloom_form_bottom',
6233 'basic_edge' == $details['edge_style'] || '' == $details['edge_style']
6234 ? ''
6235 : sprintf( ' with_edge %1$s', esc_attr( $details['edge_style'] ) ),
6236 ( 'no_border' !== $details['border_orientation'] )
6237 ? sprintf(
6238 ' et_bloom_with_border et_bloom_border_%1$s%2$s',
6239 esc_attr( $details['border_style'] ),
6240 esc_attr( ' et_bloom_border_position_' . $details['border_orientation'] )
6241 )
6242 : '',
6243 ( 'rounded' == $details['corner_style'] ) ? ' et_bloom_rounded_corners' : '',
6244 ( 'rounded' == $details['field_corner'] ) ? ' et_bloom_rounded' : '',
6245 'light' == $details['text_color'] ? ' et_bloom_form_text_light' : ' et_bloom_form_text_dark',
6246 ET_Bloom::generate_form_content( 0, 0, $details ),
6247 esc_attr( $details['optin_type'] ) // #8
6248 );
6249
6250 return $output;
6251 }
6252
6253 /**
6254 * Modifies the_content to add the form below content.
6255 */
6256 function display_below_post( $content ) {
6257 $optins_set = $this->below_post_optins;
6258
6259 if ( ! empty( $optins_set ) && ! is_singular( 'product' ) ) {
6260 foreach( $optins_set as $optin_id => $details ) {
6261 if ( $this->check_applicability( $optin_id ) ) {
6262 $content .= '<div class="et_bloom_below_post">' . $this->generate_inline_form( $optin_id, $details ) . '</div>';
6263 }
6264 }
6265 }
6266
6267 return $content;
6268 }
6269
6270 /**
6271 * Display the form on woocommerce product page.
6272 */
6273 function display_on_wc_page() {
6274 $optins_set = $this->below_post_optins;
6275
6276 if ( ! empty( $optins_set ) ) {
6277 foreach( $optins_set as $optin_id => $details ) {
6278 if ( $this->check_applicability( $optin_id ) ) {
6279 echo $this->generate_inline_form( $optin_id, $details );
6280 }
6281 }
6282 }
6283 }
6284
6285 /**
6286 * Generates the content for inline form. Used to generate "Below content", "Inilne" and "Locked content" forms.
6287 */
6288 function generate_inline_form( $optin_id, $details, $update_stats = true ) {
6289
6290 ET_Bloom::load_scripts_styles();
6291
6292 $output = '';
6293
6294 $page_id = get_the_ID();
6295 $list_id = $details['email_provider'] . '_' . $details['email_list'];
6296 $custom_css_output = '';
6297
6298 $all_optins = ET_Bloom::get_bloom_options();
6299 $display_optin_id = ET_Bloom::choose_form_ab_test( $optin_id, $all_optins );
6300
6301 if ( $display_optin_id != $optin_id ) {
6302 $optin_id = $display_optin_id;
6303 $details = $all_optins[$optin_id];
6304 }
6305 if ( true === $update_stats ) {
6306 ET_Bloom::add_stats_record( 'imp', $optin_id, $page_id, $list_id );
6307 }
6308 if ( 'below_post' !== $details['optin_type'] ) {
6309 $custom_css = ET_Bloom::generate_custom_css( '.et_bloom .et_bloom_' . $display_optin_id, $details );
6310 $custom_css_output = '' !== $custom_css ? sprintf( '<style type="text/css">%1$s</style>', $custom_css ) : '';
6311 }
6312
6313 $output .= sprintf(
6314 '<div class="et_bloom_inline_form et_bloom_optin et_bloom_make_form_visible et_bloom_%1$s%9$s" style="display: none;">
6315 %10$s
6316 <div class="et_bloom_form_container %3$s%4$s%5$s%6$s%7$s%8$s%11$s">
6317 %2$s
6318 </div>
6319 </div>',
6320 esc_attr( $optin_id ),
6321 ET_Bloom::generate_form_content( $optin_id, $page_id ),
6322 'basic_edge' == $details['edge_style'] || '' == $details['edge_style']
6323 ? ''
6324 : sprintf( ' with_edge %1$s', esc_attr( $details['edge_style'] ) ),
6325 ( 'no_border' !== $details['border_orientation'] )
6326 ? sprintf(
6327 ' et_bloom_border_%1$s%2$s',
6328 esc_attr( $details['border_style'] ),
6329 'full' !== $details['border_orientation']
6330 ? esc_attr( ' et_bloom_border_position_' . $details['border_orientation'] )
6331 : ''
6332 )
6333 : '',
6334 ( 'rounded' == $details['corner_style'] ) ? ' et_bloom_rounded_corners' : '', //#5
6335 ( 'rounded' == $details['field_corner'] ) ? ' et_bloom_rounded' : '',
6336 'light' == $details['text_color'] ? ' et_bloom_form_text_light' : ' et_bloom_form_text_dark',
6337 'bottom' !== $details['form_orientation'] && 'custom_html' !== $details['email_provider']
6338 ? sprintf(
6339 ' et_bloom_form_%1$s',
6340 esc_html( $details['form_orientation'] )
6341 )
6342 : ' et_bloom_form_bottom',
6343 ( isset( $details['hide_mobile_optin'] ) && true == $details['hide_mobile_optin'] )
6344 ? ' et_bloom_hide_mobile_optin'
6345 : '',
6346 $custom_css_output, //#10
6347 ( 'no_name' == $details['name_fields'] && ! ET_Bloom::is_only_name_support( $details['email_provider'] ) ) || ( ET_Bloom::is_only_name_support( $details['email_provider'] ) && $is_single_name )
6348 ? ' et_bloom_inline_1_field'
6349 : sprintf(
6350 ' et_bloom_inline_%1$s_fields',
6351 'first_last_name' == $details['name_fields'] && ! ET_Bloom::is_only_name_support( $details['email_provider'] )
6352 ? '3'
6353 : '2'
6354 )
6355 );
6356
6357 return $output;
6358 }
6359
6360 /**
6361 * Displays the Inline shortcode on front-end.
6362 */
6363 function display_inline_shortcode( $atts ) {
6364 $atts = shortcode_atts( array(
6365 'optin_id' => '',
6366 ), $atts );
6367 $optin_id = $atts['optin_id'];
6368
6369 $optins_set = ET_Bloom::get_bloom_options();
6370 $selected_optin = isset( $optins_set[$optin_id] ) ? $optins_set[$optin_id] : '';
6371 $output = '';
6372
6373 if ( '' !== $selected_optin && 'active' == $selected_optin['optin_status'] && 'inline' == $selected_optin['optin_type'] && empty( $selected_optin['child_of'] ) ) {
6374 $output = $this->generate_inline_form( $optin_id, $selected_optin );
6375 }
6376
6377 return $output;
6378 }
6379
6380 /**
6381 * Displays the "locked content" shortcode on front-end.
6382 */
6383 function display_locked_shortcode( $atts, $content=null ) {
6384 $atts = shortcode_atts( array(
6385 'optin_id' => '',
6386 ), $atts );
6387 $optin_id = $atts['optin_id'];
6388 $optins_set = ET_Bloom::get_bloom_options();
6389 $selected_optin = isset( $optins_set[$optin_id] ) ? $optins_set[$optin_id] : '';
6390 if ( '' == $selected_optin ) {
6391 $output = $content;
6392 } else {
6393 $form = '';
6394 $page_id = get_the_ID();
6395 $list_id = 'custom_html' == $selected_optin['email_provider'] ? 'custom_html' : $selected_optin['email_provider'] . '_' . $selected_optin['email_list'];
6396
6397 if ( '' !== $selected_optin && 'active' == $selected_optin['optin_status'] && 'locked' == $selected_optin['optin_type'] && empty( $selected_optin['child_of'] ) ) {
6398 $form = $this->generate_inline_form( $optin_id, $selected_optin, false );
6399 }
6400
6401 $output = sprintf(
6402 '<div class="et_bloom_locked_container et_bloom_%4$s" data-page_id="%3$s" data-optin_id="%4$s" data-list_id="%5$s">
6403 <div class="et_bloom_locked_content" style="display: none;">
6404 %1$s
6405 </div>
6406 <div class="et_bloom_locked_form">
6407 %2$s
6408 </div>
6409 </div>',
6410 $content,
6411 $form,
6412 esc_attr( $page_id ),
6413 esc_attr( $optin_id ),
6414 esc_attr( $list_id )
6415 );
6416 }
6417
6418 return $output;
6419 }
6420
6421 function register_widget() {
6422 require_once( ET_BLOOM_PLUGIN_DIR . 'includes/bloom-widget.php' );
6423 register_widget( 'BloomWidget' );
6424 }
6425
6426 /**
6427 * Displays the Widget content on front-end.
6428 */
6429 public static function display_widget( $optin_id ) {
6430 $optins_set = ET_Bloom::get_bloom_options();
6431 $selected_optin = isset( $optins_set[$optin_id] ) ? $optins_set[$optin_id] : '';
6432 $output = '';
6433
6434 if ( '' !== $selected_optin && 'active' == $optins_set[$optin_id]['optin_status'] && empty( $optins_set[$optin_id]['child_of'] ) ) {
6435
6436 ET_Bloom::load_scripts_styles();
6437
6438 $display_optin_id = ET_Bloom::choose_form_ab_test( $optin_id, $optins_set );
6439
6440 if ( $display_optin_id != $optin_id ) {
6441 $optin_id = $display_optin_id;
6442 $selected_optin = $optins_set[$optin_id];
6443 }
6444
6445 if ( is_singular() || ET_Bloom::is_homepage() ) {
6446 $page_id = ET_Bloom::is_homepage() ? -1 : get_the_ID();
6447 } else {
6448 $page_id = 0;
6449 }
6450
6451 $list_id = $selected_optin['email_provider'] . '_' . $selected_optin['email_list'];
6452
6453 $custom_css = ET_Bloom::generate_custom_css( '.et_bloom .et_bloom_' . $display_optin_id, $selected_optin );
6454 $custom_css_output = '' !== $custom_css ? sprintf( '<style type="text/css">%1$s</style>', $custom_css ) : '';
6455
6456 ET_Bloom::add_stats_record( 'imp', $optin_id, $page_id, $list_id );
6457
6458 $output = sprintf(
6459 '<div class="et_bloom_widget_content et_bloom_make_form_visible et_bloom_optin et_bloom_%7$s" style="display: none;">
6460 %8$s
6461 <div class="et_bloom_form_container %2$s%3$s%4$s%5$s%6$s">
6462 %1$s
6463 </div>
6464 </div>',
6465 ET_Bloom::generate_form_content( $optin_id, $page_id ),
6466 'basic_edge' == $selected_optin['edge_style'] || '' == $selected_optin['edge_style']
6467 ? ''
6468 : sprintf( ' with_edge %1$s', esc_attr( $selected_optin['edge_style'] ) ),
6469 ( 'no_border' !== $selected_optin['border_orientation'] )
6470 ? sprintf(
6471 ' et_bloom_border_%1$s%2$s',
6472 $selected_optin['border_style'],
6473 'full' !== $selected_optin['border_orientation']
6474 ? esc_attr( ' et_bloom_border_position_' . $selected_optin['border_orientation'] )
6475 : ''
6476 )
6477 : '',
6478 ( 'rounded' == $selected_optin['corner_style'] ) ? ' et_bloom_rounded_corners' : '', //#5
6479 ( 'rounded' == $selected_optin['field_corner'] ) ? ' et_bloom_rounded' : '',
6480 'light' == $selected_optin['text_color'] ? ' et_bloom_form_text_light' : ' et_bloom_form_text_dark',
6481 esc_attr( $optin_id ),
6482 $custom_css_output //#8
6483 );
6484 }
6485
6486 return $output;
6487 }
6488
6489 /**
6490 * Returns list of widget optins to generate select option in widget settings
6491 * @return array
6492 */
6493 public static function widget_optins_list() {
6494 $optins_set = ET_Bloom::get_bloom_options();
6495 $output = array(
6496 'empty' => esc_html__( 'Select optin', 'bloom' ),
6497 );
6498
6499 if ( ! empty( $optins_set ) ) {
6500 foreach( $optins_set as $optin_id => $details ) {
6501 if ( isset( $details['optin_status'] ) && 'active' === $details['optin_status'] && empty( $details['child_of'] ) ) {
6502 if ( 'widget' == $details['optin_type'] ) {
6503 $output = array_merge( $output, array( $optin_id => $details['optin_name'] ) );
6504 }
6505 }
6506 }
6507 } else {
6508 $output = array(
6509 'empty' => esc_html__( 'No Widget optins created yet', 'bloom' ),
6510 );
6511 }
6512
6513 return $output;
6514 }
6515
6516 function set_custom_css() {
6517 $options_array = ET_Bloom::get_bloom_options();
6518 $custom_css = '';
6519 $font_functions = ET_Bloom::load_fonts_class();
6520 $fonts_array = array();
6521
6522 foreach( $options_array as $id => $single_optin ) {
6523 if ( 'accounts' != $id && 'db_version' != $id && isset( $single_optin['optin_type'] ) ) {
6524 if ( 'inactive' !== $single_optin['optin_status'] ) {
6525 $current_optin_id = ET_Bloom::choose_form_ab_test( $id, $options_array, false );
6526 $single_optin = $options_array[$current_optin_id];
6527
6528 if ( ( ( 'flyin' == $single_optin['optin_type'] || 'pop_up' == $single_optin['optin_type'] || 'below_post' == $single_optin['optin_type'] ) && $this->check_applicability ( $id ) ) && ( isset( $single_optin['custom_css'] ) || isset( $single_optin['form_bg_color'] ) || isset( $single_optin['header_bg_color'] ) || isset( $single_optin['form_button_color'] ) || isset( $single_optin['border_color'] ) ) ) {
6529 $form_class = '.et_bloom .et_bloom_' . $current_optin_id;
6530
6531 $custom_css .= ET_Bloom::generate_custom_css( $form_class, $single_optin );
6532 }
6533
6534 if ( ! isset( $fonts_array[$single_optin['header_font']] ) && isset( $single_optin['header_font'] ) ) {
6535 $fonts_array[] = $single_optin['header_font'];
6536 }
6537
6538 if ( ! isset( $fonts_array[$single_optin['body_font']] ) && isset( $single_optin['body_font'] ) ) {
6539 $fonts_array[] = $single_optin['body_font'];
6540 }
6541 }
6542 }
6543 }
6544
6545 if ( ! empty( $fonts_array ) ) {
6546 $font_functions->et_gf_enqueue_fonts( $fonts_array );
6547 }
6548
6549 if ( '' != $custom_css ) {
6550 printf(
6551 '<style type="text/css" id="et-bloom-custom-css">
6552 %1$s
6553 </style>',
6554 stripslashes( $custom_css )
6555 );
6556 }
6557 }
6558
6559 /**
6560 * Generated the output for custom css with specified class based on input option
6561 * @return string
6562 */
6563 public static function generate_custom_css( $form_class, $single_optin = array() ) {
6564 $font_functions = ET_Bloom::load_fonts_class();
6565 $custom_css = '';
6566
6567 if ( isset( $single_optin['form_bg_color'] ) && '' !== $single_optin['form_bg_color'] ) {
6568 $custom_css .= esc_html( $form_class ) . ' .et_bloom_form_content { background-color: ' . esc_html( $single_optin['form_bg_color'] ) . ' !important; } ';
6569
6570 if ( 'zigzag_edge' === $single_optin['edge_style'] ) {
6571 $custom_css .=
6572 esc_html( $form_class ) . ' .zigzag_edge .et_bloom_form_content:before { background: linear-gradient(45deg, transparent 33.33%, ' . esc_html( $single_optin['form_bg_color'] ) . ' 33.333%, ' . esc_html( $single_optin['form_bg_color'] ) . ' 66.66%, transparent 66.66%), linear-gradient(-45deg, transparent 33.33%, ' . esc_html( $single_optin['form_bg_color'] ) . ' 33.33%, ' . esc_html( $single_optin['form_bg_color'] ) . ' 66.66%, transparent 66.66%) !important; background-size: 20px 40px !important; } ' .
6573 esc_html( $form_class ) . ' .zigzag_edge.et_bloom_form_right .et_bloom_form_content:before, ' . esc_html( $form_class ) . ' .zigzag_edge.et_bloom_form_left .et_bloom_form_content:before { background-size: 40px 20px !important; }
6574 @media only screen and ( max-width: 767px ) {' .
6575 esc_html( $form_class ) . ' .zigzag_edge.et_bloom_form_right .et_bloom_form_content:before, ' . esc_html( $form_class ) . ' .zigzag_edge.et_bloom_form_left .et_bloom_form_content:before { background: linear-gradient(45deg, transparent 33.33%, ' . esc_html( $single_optin['form_bg_color'] ) . ' 33.333%, ' . esc_html( $single_optin['form_bg_color'] ) . ' 66.66%, transparent 66.66%), linear-gradient(-45deg, transparent 33.33%, ' . esc_html( $single_optin['form_bg_color'] ) . ' 33.33%, ' . esc_html( $single_optin['form_bg_color'] ) . ' 66.66%, transparent 66.66%) !important; background-size: 20px 40px !important; } ' .
6576 '}';
6577 }
6578 }
6579
6580 if ( isset( $single_optin['header_bg_color'] ) && '' !== $single_optin['header_bg_color'] ) {
6581 $custom_css .= esc_html( $form_class ) . ' .et_bloom_form_container .et_bloom_form_header { background-color: ' . esc_html( $single_optin['header_bg_color'] ) . ' !important; } ';
6582
6583 switch ( $single_optin['edge_style'] ) {
6584 case 'curve_edge' :
6585 $custom_css .= esc_html( $form_class ) . ' .curve_edge .curve { fill: ' . esc_html( $single_optin['header_bg_color'] ) . '} ';
6586 break;
6587
6588 case 'wedge_edge' :
6589 $custom_css .= esc_html( $form_class ) . ' .wedge_edge .triangle { fill: ' . esc_html( $single_optin['header_bg_color'] ) . '} ';
6590 break;
6591
6592 case 'carrot_edge' :
6593 $custom_css .=
6594 esc_html( $form_class ) . ' .carrot_edge .et_bloom_form_content:before { border-top-color: ' . esc_html( $single_optin['header_bg_color'] ) . ' !important; } ' .
6595 esc_html( $form_class ) . ' .carrot_edge.et_bloom_form_right .et_bloom_form_content:before, ' . esc_html( $form_class ) . ' .carrot_edge.et_bloom_form_left .et_bloom_form_content:before { border-top-color: transparent !important; border-left-color: ' . esc_html( $single_optin['header_bg_color'] ) . ' !important; }
6596 @media only screen and ( max-width: 767px ) {' .
6597 esc_html( $form_class ) . ' .carrot_edge.et_bloom_form_right .et_bloom_form_content:before, ' . esc_html( $form_class ) . ' .carrot_edge.et_bloom_form_left .et_bloom_form_content:before { border-top-color: ' . esc_html( $single_optin['header_bg_color'] ) . ' !important; border-left-color: transparent !important; }
6598 }';
6599 break;
6600 }
6601
6602 if ( 'dashed' === $single_optin['border_style'] ) {
6603 if ( 'breakout_edge' !== $single_optin['edge_style'] ) {
6604 $custom_css .= esc_html( $form_class ) . ' .et_bloom_form_container { background-color: ' . esc_html( $single_optin['header_bg_color'] ) . ' !important; } ';
6605 } else {
6606 $custom_css .= esc_html( $form_class ) . ' .et_bloom_header_outer { background-color: ' . esc_html( $single_optin['header_bg_color'] ) . ' !important; } ';
6607 }
6608 }
6609 }
6610
6611 if ( isset( $single_optin['form_button_color'] ) && '' !== $single_optin['form_button_color'] ) {
6612 $custom_css .= esc_html( $form_class ) . ' .et_bloom_form_content button { background-color: ' . esc_html( $single_optin['form_button_color'] ) . ' !important; } ';
6613 }
6614
6615 if ( isset( $single_optin['border_color'] ) && '' !== $single_optin['border_color'] && 'no_border' !== $single_optin['border_orientation'] ) {
6616 if ( 'breakout_edge' === $single_optin['edge_style'] ) {
6617 switch ( $single_optin['border_style'] ) {
6618 case 'letter' :
6619 $custom_css .= esc_html( $form_class ) . ' .breakout_edge.et_bloom_border_letter .et_bloom_header_outer { background: repeating-linear-gradient( 135deg, ' . esc_html( $single_optin['border_color'] ) . ', ' . esc_html( $single_optin['border_color'] ) . ' 10px, #fff 10px, #fff 20px, #f84d3b 20px, #f84d3b 30px, #fff 30px, #fff 40px ) !important; } ';
6620 break;
6621
6622 case 'double' :
6623 $custom_css .= esc_html( $form_class ) . ' .breakout_edge.et_bloom_border_double .et_bloom_form_header { -moz-box-shadow: inset 0 0 0 6px ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 0 0 8px ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 0 0 0 6px ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 0 0 8px ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 0 0 0 6px ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 0 0 8px ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['border_color'] ) . '; } ';
6624
6625 switch ( $single_optin['border_orientation'] ) {
6626 case 'top' :
6627 $custom_css .= esc_html( $form_class ) . ' .breakout_edge.et_bloom_border_double.et_bloom_border_position_top .et_bloom_form_header { -moz-box-shadow: inset 0 6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 8px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 0 6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 8px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 0 6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 8px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['border_color'] ) . '; } ';
6628 break;
6629
6630 case 'right' :
6631 $custom_css .= esc_html( $form_class ) . ' .breakout_edge.et_bloom_border_double.et_bloom_border_position_right .et_bloom_form_header { -moz-box-shadow: inset -6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset -8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset -6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset -8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset -6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset -8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['border_color'] ) . '; } ';
6632 break;
6633
6634 case 'bottom' :
6635 $custom_css .= esc_html( $form_class ) . ' .breakout_edge.et_bloom_border_double.et_bloom_border_position_bottom .et_bloom_form_header { -moz-box-shadow: inset 0 -6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 -8px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 0 -6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 -8px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 0 -6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 -8px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['border_color'] ) . '; } ';
6636 break;
6637
6638 case 'left' :
6639 $custom_css .= esc_html( $form_class ) . ' .breakout_edge.et_bloom_border_double.et_bloom_border_position_left .et_bloom_form_header { -moz-box-shadow: inset 6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['border_color'] ) . '; } ';
6640 break;
6641
6642 case 'top_bottom' :
6643 $custom_css .= esc_html( $form_class ) . ' .breakout_edge.et_bloom_border_double.et_bloom_border_position_top_bottom .et_bloom_form_header { -moz-box-shadow: inset 0 6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 8px 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset 0 -6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 -8px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 0 6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 8px 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset 0 -6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 -8px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 0 6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 8px 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset 0 -6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 -8px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['border_color'] ) . '; } ';
6644 break;
6645
6646 case 'left_right' :
6647 $custom_css .= esc_html( $form_class ) . ' .breakout_edge.et_bloom_border_double.et_bloom_border_position_left_right .et_bloom_form_header { -moz-box-shadow: inset 6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset -6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset -8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset -6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset -8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset -6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset -8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['border_color'] ) . '; } ';
6648 }
6649 break;
6650
6651 case 'inset' :
6652 $custom_css .= esc_html( $form_class ) . ' .breakout_edge.et_bloom_border_inset .et_bloom_form_header { -moz-box-shadow: inset 0 0 0 3px ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 0 0 0 3px ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 0 0 0 3px ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['header_bg_color'] ) . '; } ';
6653
6654 switch ( $single_optin['border_orientation'] ) {
6655 case 'top' :
6656 $custom_css .= esc_html( $form_class ) . ' .breakout_edge.et_bloom_border_inset.et_bloom_border_position_top .et_bloom_form_header { -moz-box-shadow: inset 0 3px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 0 3px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 0 3px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['header_bg_color'] ) . '; } ';
6657 break;
6658
6659 case 'right' :
6660 $custom_css .= esc_html( $form_class ) . ' .breakout_edge.et_bloom_border_inset.et_bloom_border_position_right .et_bloom_form_header { -moz-box-shadow: inset -3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset -3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset -3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['header_bg_color'] ) . '; } ';
6661 break;
6662
6663 case 'bottom' :
6664 $custom_css .= esc_html( $form_class ) . ' .breakout_edge.et_bloom_border_inset.et_bloom_border_position_bottom .et_bloom_form_header { -moz-box-shadow: inset 0 -3px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 0 -3px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 0 -3px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['header_bg_color'] ) . '; } ';
6665 break;
6666
6667 case 'left' :
6668 $custom_css .= esc_html( $form_class ) . ' .breakout_edge.et_bloom_border_inset.et_bloom_border_position_left .et_bloom_form_header { -moz-box-shadow: inset 3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['header_bg_color'] ) . '; } ';
6669 break;
6670
6671 case 'top_bottom' :
6672 $custom_css .= esc_html( $form_class ) . ' .breakout_edge.et_bloom_border_inset.et_bloom_border_position_top_bottom .et_bloom_form_header { -moz-box-shadow: inset 0 3px 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset 0 -3px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 0 3px 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset 0 -3px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 0 3px 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset 0 -3px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['header_bg_color'] ) . '; } ';
6673 break;
6674
6675 case 'left_right' :
6676 $custom_css .= esc_html( $form_class ) . ' .breakout_edge.et_bloom_border_inset.et_bloom_border_position_left_right .et_bloom_form_header { -moz-box-shadow: inset 3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset -3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset -3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset -3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['header_bg_color'] ) . '; } ';
6677 }
6678 break;
6679
6680 case 'solid' :
6681 $custom_css .= esc_html( $form_class ) . ' .breakout_edge.et_bloom_border_solid .et_bloom_form_header { border-color: ' . esc_html( $single_optin['border_color'] ) . ' !important } ';
6682 break;
6683
6684 case 'dashed' :
6685 $custom_css .= esc_html( $form_class ) . ' .breakout_edge.et_bloom_border_dashed .et_bloom_form_header { border-color: ' . esc_html( $single_optin['border_color'] ) . ' !important } ';
6686 break;
6687 }
6688 } else {
6689 switch ( $single_optin['border_style'] ) {
6690 case 'letter' :
6691 $custom_css .= esc_html( $form_class ) . ' .et_bloom_border_letter { background: repeating-linear-gradient( 135deg, ' . esc_html( $single_optin['border_color'] ) . ', ' . esc_html( $single_optin['border_color'] ) . ' 10px, #fff 10px, #fff 20px, #f84d3b 20px, #f84d3b 30px, #fff 30px, #fff 40px ) !important; } ';
6692 break;
6693
6694 case 'double' :
6695 $custom_css .= esc_html( $form_class ) . ' .et_bloom_border_double { -moz-box-shadow: inset 0 0 0 6px ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 0 0 8px ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 0 0 0 6px ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 0 0 8px ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 0 0 0 6px ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 0 0 8px ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['border_color'] ) . '; } ';
6696
6697 switch ( $single_optin['border_orientation'] ) {
6698 case 'top' :
6699 $custom_css .= esc_html( $form_class ) . ' .et_bloom_border_double.et_bloom_border_position_top { -moz-box-shadow: inset 0 6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 8px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 0 6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 8px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 0 6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 8px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['border_color'] ) . '; } ';
6700 break;
6701
6702 case 'right' :
6703 $custom_css .= esc_html( $form_class ) . ' .et_bloom_border_double.et_bloom_border_position_right { -moz-box-shadow: inset -6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset -8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset -6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset -8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset -6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset -8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['border_color'] ) . '; } ';
6704 break;
6705
6706 case 'bottom' :
6707 $custom_css .= esc_html( $form_class ) . ' .et_bloom_border_double.et_bloom_border_position_bottom { -moz-box-shadow: inset 0 -6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 -8px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 0 -6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 -8px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 0 -6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 -8px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['border_color'] ) . '; } ';
6708 break;
6709
6710 case 'left' :
6711 $custom_css .= esc_html( $form_class ) . ' .et_bloom_border_double.et_bloom_border_position_left { -moz-box-shadow: inset 6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['border_color'] ) . '; } ';
6712 break;
6713
6714 case 'top_bottom' :
6715 $custom_css .= esc_html( $form_class ) . ' .et_bloom_border_double.et_bloom_border_position_top_bottom { -moz-box-shadow: inset 0 6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 8px 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset 0 -6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 -8px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 0 6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 8px 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset 0 -6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 -8px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 0 6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 8px 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset 0 -6px 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 0 -8px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['border_color'] ) . '; } ';
6716 break;
6717
6718 case 'left_right' :
6719 $custom_css .= esc_html( $form_class ) . ' .et_bloom_border_double.et_bloom_border_position_left_right { -moz-box-shadow: inset 6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset -6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset -8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset -6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset -8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset 8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset -6px 0 0 0 ' . esc_html( $single_optin['header_bg_color'] ) . ', inset -8px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['border_color'] ) . '; } ';
6720 }
6721 break;
6722
6723 case 'inset' :
6724 $custom_css .= esc_html( $form_class ) . ' .et_bloom_border_inset { -moz-box-shadow: inset 0 0 0 3px ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 0 0 0 3px ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 0 0 0 3px ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['header_bg_color'] ) . '; } ';
6725
6726 switch ( $single_optin['border_orientation'] ) {
6727 case 'top' :
6728 $custom_css .= esc_html( $form_class ) . ' .et_bloom_border_inset.et_bloom_border_position_top { -moz-box-shadow: inset 0 3px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 0 3px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 0 3px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['header_bg_color'] ) . '; } ';
6729 break;
6730
6731 case 'right' :
6732 $custom_css .= esc_html( $form_class ) . ' .et_bloom_border_inset.et_bloom_border_position_right { -moz-box-shadow: inset -3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset -3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset -3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['header_bg_color'] ) . '; } ';
6733 break;
6734
6735 case 'bottom' :
6736 $custom_css .= esc_html( $form_class ) . ' .et_bloom_border_inset.et_bloom_border_position_bottom { -moz-box-shadow: inset 0 -3px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 0 -3px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 0 -3px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['header_bg_color'] ) . '; } ';
6737 break;
6738
6739 case 'left' :
6740 $custom_css .= esc_html( $form_class ) . ' .et_bloom_border_inset.et_bloom_border_position_left { -moz-box-shadow: inset 3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['header_bg_color'] ) . '; } ';
6741 break;
6742
6743 case 'top_bottom' :
6744 $custom_css .= esc_html( $form_class ) . ' .et_bloom_border_inset.et_bloom_border_position_top_bottom { -moz-box-shadow: inset 0 3px 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset 0 -3px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 0 3px 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset 0 -3px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 0 3px 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset 0 -3px 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['header_bg_color'] ) . '; } ';
6745 break;
6746
6747 case 'left_right' :
6748 $custom_css .= esc_html( $form_class ) . ' .et_bloom_border_inset.et_bloom_border_position_left_right { -moz-box-shadow: inset 3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset -3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; -webkit-box-shadow: inset 3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset -3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; box-shadow: inset 3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . ', inset -3px 0 0 0 ' . esc_html( $single_optin['border_color'] ) . '; border-color: ' . esc_html( $single_optin['header_bg_color'] ) . '; } ';
6749 }
6750 break;
6751
6752 case 'solid' :
6753 $custom_css .= esc_html( $form_class ) . ' .et_bloom_border_solid { border-color: ' . esc_html( $single_optin['border_color'] ) . ' !important } ';
6754 break;
6755
6756 case 'dashed' :
6757 $custom_css .= esc_html( $form_class ) . ' .et_bloom_border_dashed .et_bloom_form_container_wrapper { border-color: ' . esc_html( $single_optin['border_color'] ) . ' !important } ';
6758 break;
6759 }
6760 }
6761 }
6762
6763 $custom_css .= isset( $single_optin['form_button_color'] ) && '' !== $single_optin['form_button_color'] ? esc_html( $form_class ) . ' .et_bloom_form_content button { background-color: ' . esc_html( $single_optin['form_button_color'] ) . ' !important; } ' : '';
6764 $custom_css .= isset( $single_optin['header_font'] ) ? $font_functions->et_gf_attach_font( $single_optin['header_font'], $form_class . ' h2, ' . $form_class . ' h2 span, ' . $form_class . ' h2 strong' ) : '';
6765 $custom_css .= isset( $single_optin['body_font'] ) ? $font_functions->et_gf_attach_font( $single_optin['body_font'], $form_class . ' p, ' . $form_class . ' p span, ' . $form_class . ' p strong, ' . $form_class . ' form input, ' . $form_class . ' form button span' ) : '';
6766
6767 $custom_css .= isset( $single_optin['custom_css'] ) ? ' ' . $single_optin['custom_css'] : '';
6768
6769 return $custom_css;
6770 }
6771
6772 /**
6773 * Modifies the URL of post after commenting to trigger the popup after comment
6774 * @return string
6775 */
6776 function after_comment_trigger( $location ){
6777 $newurl = $location;
6778 $newurl = substr( $location, 0, strpos( $location, '#comment' ) );
6779 $delimeter = false === strpos( $location, '?' ) ? '?' : '&';
6780 $params = 'et_bloom_popup=true';
6781
6782 $newurl .= $delimeter . $params;
6783
6784 return $newurl;
6785 }
6786
6787 /**
6788 * Generated content for purchase trigger
6789 * @return string
6790 */
6791 function add_purchase_trigger() {
6792 echo '<div class="et_bloom_after_order"></div>';
6793 }
6794
6795 /**
6796 * Check the homepage
6797 * @return bool
6798 */
6799 public static function is_homepage() {
6800 return is_front_page() || is_home();
6801 }
6802
6803 /**
6804 * Check the Blog Page
6805 * @return bool
6806 */
6807 public static function is_blogpage() {
6808 if ( is_front_page() && is_home() ) {
6809 // Default homepage
6810 return false;
6811 } elseif ( is_front_page() ) {
6812 // static homepage
6813 return false;
6814 } elseif ( is_home() ) {
6815 // blog page
6816 return true;
6817 }
6818
6819 //everything else
6820 return false;
6821 }
6822
6823
6824 /**
6825 * Adds appropriate actions for Flyin, Popup, Below Content to wp_footer,
6826 * Adds custom_css function to wp_head
6827 * Adds trigger_bottom_mark to the_content filter for Flyin and Popup
6828 * Creates arrays with optins for for Flyin, Popup, Below Content to improve the performance during forms displaying
6829 */
6830 function frontend_register_locations() {
6831 $options_array = ET_Bloom::get_bloom_options();
6832
6833 if ( ! is_admin() && ! empty( $options_array ) ) {
6834 add_action( 'wp_head', array( $this, 'set_custom_css' ) );
6835
6836 $flyin_count = 0;
6837 $popup_count = 0;
6838 $below_count = 0;
6839 $after_comment = 0;
6840 $after_purchase = 0;
6841
6842 foreach ( $options_array as $optin_id => $details ) {
6843 if ( 'accounts' !== $optin_id ) {
6844 if ( isset( $details['optin_status'] ) && 'active' === $details['optin_status'] && empty( $details['child_of'] ) ) {
6845 switch( $details['optin_type'] ) {
6846 case 'flyin' :
6847 if ( 0 === $flyin_count ) {
6848 add_action( 'wp_footer', array( $this, "display_flyin" ) );
6849 $flyin_count++;
6850 }
6851
6852 if ( 0 === $after_comment && isset( $details['comment_trigger'] ) && true == $details['comment_trigger'] ) {
6853 add_filter( 'comment_post_redirect', array( $this, 'after_comment_trigger' ) );
6854 $after_comment++;
6855 }
6856
6857 if ( 0 === $after_purchase && isset( $details['purchase_trigger'] ) && true == $details['purchase_trigger'] ) {
6858 add_action( 'woocommerce_thankyou', array( $this, 'add_purchase_trigger' ) );
6859 $after_purchase++;
6860 }
6861
6862 $this->flyin_optins[$optin_id] = $details;
6863 break;
6864
6865 case 'pop_up' :
6866 if ( 0 === $popup_count ) {
6867 add_action( 'wp_footer', array( $this, "display_popup" ) );
6868 $popup_count++;
6869 }
6870
6871 if ( 0 === $after_comment && isset( $details['comment_trigger'] ) && true == $details['comment_trigger'] ) {
6872 add_filter( 'comment_post_redirect', array( $this, 'after_comment_trigger' ) );
6873 $after_comment++;
6874 }
6875
6876 if ( 0 === $after_purchase && isset( $details['purchase_trigger'] ) && true == $details['purchase_trigger'] ) {
6877 add_action( 'woocommerce_thankyou', array( $this, 'add_purchase_trigger' ) );
6878 $after_purchase++;
6879 }
6880
6881 $this->popup_optins[$optin_id] = $details;
6882 break;
6883
6884 case 'below_post' :
6885 if ( 0 === $below_count ) {
6886 add_filter( 'the_content', array( $this, 'display_below_post' ), 9999 );
6887 add_action( 'woocommerce_after_single_product_summary', array( $this, 'display_on_wc_page' ) );
6888 $below_count++;
6889 }
6890
6891 $this->below_post_optins[$optin_id] = $details;
6892 break;
6893 }
6894 }
6895 }
6896 }
6897
6898 if ( 0 < $flyin_count || 0 < $popup_count ) {
6899 add_filter( 'the_content', array( $this, 'trigger_bottom_mark' ), 9999 );
6900 }
6901 }
6902 }
6903
6904}
6905
6906class ET_Bloom_Email_Providers {
6907 static $providers = array();
6908
6909 public static function add_provider( $provider ) {
6910 self::$providers[ $provider->slug ] = $provider;
6911 }
6912
6913 public static function get_provider( $slug ) {
6914 return isset( self::$providers[ $slug ] ) ? self::$providers[ $slug ] : false;
6915 }
6916
6917 public static function get_providers() {
6918 return self::$providers;
6919 }
6920}
6921
6922class ET_Bloom_Email_Provider {
6923 public $name;
6924
6925 public $slug;
6926
6927 public $fields = array();
6928
6929 function __construct() {
6930 $this->init();
6931
6932 ET_Bloom_Email_Providers::add_provider( $this );
6933 }
6934
6935 public function get_fields() {}
6936
6937 public function get_settings() {
6938 return $this->fields;
6939 }
6940
6941 public function update_account( $name, $data_array = array() ) {
6942 global $et_bloom;
6943
6944 $service = $this->slug;
6945
6946 if ( ! empty( $name ) ) {
6947 $name = str_replace( array( '"', "'" ), '', stripslashes( $name ) );
6948 $options_array = ET_Bloom::get_bloom_options();
6949 $new_account['accounts'] = isset( $options_array['accounts'] ) ? $options_array['accounts'] : array();
6950 $new_account['accounts'][ $service ][ $name ] = isset( $new_account['accounts'][ $service ][ $name ] )
6951 ? array_merge( $new_account['accounts'][ $service ][ $name ], $data_array )
6952 : $data_array;
6953
6954 $et_bloom->update_option( $new_account );
6955 }
6956 }
6957
6958 public function fetch_lists( $args ) {}
6959
6960 public function add_subscriber( $args ) {}
6961}
6962
6963class Bloom_ActiveCampaign extends ET_Bloom_Email_Provider {
6964 function init() {
6965 $this->name = esc_html__( 'ActiveCampaign', 'bloom' );
6966 $this->slug = 'activecampaign';
6967 }
6968
6969 function get_fields() {
6970 $fields = array(
6971 'api_url' => array(
6972 'label' => esc_html__( 'API URL', 'bloom' ),
6973 ),
6974 'api_key' => array(
6975 'label' => esc_html__( 'API Key', 'bloom' ),
6976 ),
6977 'form_id' => array(
6978 'label' => esc_html__( 'Form ID', 'bloom' ),
6979 'not_required' => true,
6980 'apply_password_mask' => false,
6981 ),
6982 );
6983
6984 return $fields;
6985 }
6986
6987 public function fetch_lists( $args ) {
6988 global $et_bloom;
6989
6990 $api_url = $args['api_url'];
6991 $api_key = $args['api_key'];
6992 $form_id = $args['form_id'];
6993 $name = $args['name'];
6994
6995 $request_url = sprintf( '%s/admin/api.php', $api_url );
6996
6997 $query_args = array(
6998 'api_key' => $api_key,
6999 'api_action' => 'list_list',
7000 'api_output' => 'json',
7001 'ids' => 'all',
7002 'full' => '1',
7003 );
7004
7005 $request_url = add_query_arg( $query_args, $request_url );
7006
7007 $request_url = esc_url_raw( $request_url, array( 'https' ) );
7008
7009 $response = wp_remote_get( $request_url );
7010
7011 $response_code = wp_remote_retrieve_response_code( $response );
7012
7013 if ( is_array( $response ) && 200 === $response_code ) {
7014 $api_response = json_decode( wp_remote_retrieve_body( $response ), true );
7015
7016 if ( empty( $api_response['result_code'] ) ) {
7017 switch ( $api_response['result_message'] ) {
7018 case 'Failed: Nothing is returned':
7019 $error_message = esc_html__( 'No lists found.', 'bloom' );
7020 break;
7021 case 'You are not authorized to access this file':
7022 $error_message = esc_html__( 'Invalid API URL/ API Key.', 'bloom' );
7023 break;
7024 default:
7025 $error_message = esc_html( $api_response['result_message'] );
7026 break;
7027 }
7028 }
7029
7030 } else {
7031 $error_message = esc_html__( 'API request failed, please try again.', 'bloom' );
7032 }
7033
7034 $lists = array();
7035
7036 if ( empty( $error_message ) ) {
7037 foreach( $api_response as $key => $list ) {
7038 if ( ! is_numeric( $key ) ) {
7039 continue;
7040 }
7041
7042 $error_message = 'success';
7043
7044 $lists[ $list['id'] ]['name'] = sanitize_text_field( $list['name'] );
7045 $lists[ $list['id'] ]['subscribers_count'] = sanitize_text_field( $list['subscriber_count'] );
7046 $lists[ $list['id'] ]['growth_week'] = sanitize_text_field( $et_bloom->calculate_growth_rate( $this->slug . '_' . $list['id'] ) );
7047 }
7048
7049 $account_args = array(
7050 'lists' => $lists,
7051 'api_key' => sanitize_text_field( $api_key ),
7052 'api_url' => sanitize_text_field( $api_url ),
7053 'is_authorized' => 'true',
7054 );
7055
7056 if ( ! empty( $form_id ) ) {
7057 $account_args['form_id'] = (int) $form_id;
7058 }
7059
7060 $this->update_account( sanitize_text_field( $name ), $account_args );
7061 }
7062
7063 return $error_message;
7064 }
7065
7066 public function add_subscriber( $args ) {
7067 $api_url = $args['api_url'];
7068 $api_key = $args['api_key'];
7069 $list_id = $args['list_id'];
7070 $email = $args['email'];
7071 $name = $args['name'];
7072 $last_name = $args['last_name'];
7073 $form_id = ! empty( $args['form_id'] ) ? $args['form_id'] : '';
7074
7075 $request_url = sprintf( '%s/admin/api.php', $api_url );
7076
7077 $query_args = array(
7078 'api_key' => $api_key,
7079 'api_action' => 'contact_add',
7080 'api_output' => 'json',
7081 );
7082
7083 $request_url = add_query_arg( $query_args, $request_url );
7084
7085 $request_url = esc_url_raw( $request_url, array( 'https' ) );
7086
7087 $body_args = array(
7088 'p[' . $list_id .']' => $list_id,
7089 'email' => $email,
7090 'first_name' => $name,
7091 'last_name' => $last_name,
7092 );
7093
7094 if ( '' !== $form_id ) {
7095 $body_args['form'] = (int) $form_id;
7096 }
7097
7098 $request_args = array(
7099 'body' => $body_args,
7100 );
7101
7102 $response = wp_remote_post( $request_url, $request_args );
7103
7104 $response_code = wp_remote_retrieve_response_code( $response );
7105
7106 if ( is_array( $response ) && 200 === $response_code ) {
7107 $api_response = json_decode( wp_remote_retrieve_body( $response ), true );
7108
7109 if ( empty( $api_response['result_code'] ) ) {
7110 $error_message = esc_html( $api_response['result_message'] );
7111 }
7112
7113 } else {
7114 $error_message = esc_html__( 'API request failed, please try again.', 'bloom');
7115 }
7116
7117 if ( empty( $error_message ) ) {
7118 $error_message = 'success';
7119 }
7120
7121 return $error_message;
7122 }
7123}
7124
7125class Bloom_Emma extends ET_Bloom_Email_Provider {
7126 function init() {
7127 $this->name = esc_html__( 'Emma', 'bloom' );
7128 $this->slug = 'emma';
7129 }
7130
7131 function get_fields() {
7132 $fields = array(
7133 'api_key' => array(
7134 'label' => esc_html__( 'Public API Key', 'bloom' ),
7135 ),
7136 'private_api_key' => array(
7137 'label' => esc_html__( 'Private API Key', 'bloom' ),
7138 ),
7139 'user_id' => array(
7140 'label' => esc_html__( 'Account ID', 'bloom' ),
7141 ),
7142 );
7143 return $fields;
7144 }
7145
7146 public function fetch_lists( $args ) {
7147 global $et_bloom;
7148
7149 if ( ! function_exists( 'curl_init' ) ) {
7150 return esc_html__( 'curl_init is not defined ', 'bloom' );
7151 }
7152
7153 $api_key = $args['api_key'];
7154 $private_api_key = $args['private_api_key'];
7155 $user_id = $args['user_id'];
7156 $name = $args['name'];
7157
7158 $request_url = sprintf( 'https://api.e2ma.net/%s/groups?group_types=all', $user_id );
7159 $api_response = $this->emma_request( $api_key, $private_api_key, $request_url );
7160
7161 $lists = array();
7162
7163 if ( ! empty( $api_response ) && is_array( $api_response ) ) {
7164 foreach( $api_response as $list => $list_details ) {
7165 $error_message = 'success';
7166
7167 $lists[ $list_details['member_group_id'] ]['name'] = sanitize_text_field( $list_details['group_name'] );
7168 $lists[ $list_details['member_group_id'] ]['subscribers_count'] = sanitize_text_field( $list_details['active_count'] );
7169 $lists[ $list_details['member_group_id'] ]['growth_week'] = sanitize_text_field( $et_bloom->calculate_growth_rate( $this->slug . '_' . $list_details['member_group_id'] ) );
7170 }
7171
7172 $this->update_account( sanitize_text_field( $name ), array(
7173 'lists' => $lists,
7174 'api_key' => sanitize_text_field( $api_key ),
7175 'private_api_key' => sanitize_text_field( $private_api_key ),
7176 'user_id' => sanitize_text_field( $user_id ),
7177 'is_authorized' => 'true',
7178 ) );
7179 } else {
7180 switch ( $api_response ) {
7181 case '401 ' :
7182 return esc_html__( 'Invalid Public API key or Private API key', 'bloom' );
7183 break;
7184 case '403 Forbidden' :
7185 return esc_html__( 'Invalid Account ID', 'bloom' );
7186 break;
7187 default:
7188 return $api_response;
7189 break;
7190 }
7191 }
7192
7193 return $error_message;
7194 }
7195
7196 public function add_subscriber( $args ) {
7197 $api_key = $args['api_key'];
7198 $private_api_key = $args['private_api_key'];
7199 $user_id = $args['user_id'];
7200 $list_id = $args['list_id'];
7201 $email = $args['email'];
7202 $name = $args['name'];
7203 $last_name = $args['last_name'];
7204
7205 if ( ! function_exists( 'curl_init' ) ) {
7206 return esc_html__( 'curl_init is not defined ', 'bloom' );
7207 }
7208
7209 $request_url = sprintf( 'https://api.e2ma.net/%s/members/signup', $user_id );
7210 $contact_data = array(
7211 'email' => sanitize_email( $email ),
7212 'fields' => array(
7213 'first_name' => sanitize_text_field( $name ),
7214 'last_name' => sanitize_text_field( $last_name ),
7215 ),
7216 'group_ids' => array( $list_id ),
7217 );
7218
7219 $api_response = $this->emma_request( $api_key, $private_api_key, $request_url, $contact_data );
7220
7221 if ( ! empty( $api_response['member_id'] ) ) {
7222 $error_message = 'success';
7223 } else {
7224 $error_message = esc_html__( 'An error occured, please try again later', 'bloom' );
7225 }
7226
7227 return $error_message;
7228 }
7229
7230 private function emma_request( $api_key, $private_api_key, $request_url, $post_data = array() ) {
7231 $curl = curl_init();
7232
7233 curl_setopt( $curl, CURLOPT_USERPWD, $api_key . ":" . $private_api_key );
7234 curl_setopt( $curl, CURLOPT_URL, esc_url_raw( $request_url ) );
7235 curl_setopt( $curl, CURLOPT_HTTPHEADER, array( 'Content-type: application/json' ) );
7236 curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
7237 curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
7238 if ( ! empty( $post_data ) ) {
7239 curl_setopt( $curl, CURLOPT_POST, true );
7240 curl_setopt( $curl, CURLOPT_POSTFIELDS, json_encode( $post_data ) );
7241 }
7242
7243 $response = curl_exec( $curl );
7244 $http_code = curl_getinfo( $curl, CURLINFO_HTTP_CODE );
7245
7246 curl_close( $curl );
7247
7248 if ( 200 !== $http_code ) {
7249 return $http_code . ' ' . $response;
7250 }
7251
7252 return json_decode( $response, true );
7253 }
7254}
7255
7256function et_bloom_init_plugin() {
7257 $et_bloom = new ET_Bloom();
7258 $GLOBALS['et_bloom'] = $et_bloom;
7259
7260 new Bloom_ActiveCampaign;
7261 new Bloom_Emma;
7262}
7263add_action( 'plugins_loaded', 'et_bloom_init_plugin' );
7264
7265register_activation_hook( __FILE__, array( 'ET_Bloom', 'activate_plugin' ) );