forked from wpmudev/subscribe-by-email
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscribe-by-email.php
executable file
·885 lines (680 loc) · 28.3 KB
/
subscribe-by-email.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
<?php
/*
Plugin Name: Subscribe by Email
Plugin URI: http://premium.wpmudev.org/project/subscribe-by-email
Description: This plugin allows you and your users to offer subscriptions to email notification of new posts
Author: WPMU DEV
Version: 3.6.0
Author URI: http://premium.wpmudev.org
WDP ID: 127
Text Domain: subscribe-by-email
*/
class Incsub_Subscribe_By_Email {
static $freq_weekly_transient_slug = 'next_week_scheduled';
static $freq_daily_transient_slug = 'next_day_scheduled';
static $pending_mails_transient_slug = 'sbe_pending_mails_sent';
// Max mail subject length
static $max_subject_length = 120;
// Time between batches
static $time_between_batches;
// Max time in seconds during which the user can activate his subscription
static $max_confirmation_time = 604800;
//Menus
static $admin_subscribers_page;
static $admin_add_new_subscriber_page;
static $admin_settings_page;
static $network_settings_page;
static $admin_sent_emails_page;
static $admin_export_subscribers_page;
// Widget
public $widget;
public static $instance = null;
public static function get_instance() {
if ( self::$instance === null ) {
self::$instance = new self();
}
return self::$instance;
}
public function __construct() {
self::$time_between_batches = apply_filters( 'sbe_time_between_batches', 1200 );
$this->set_globals();
$this->includes();
$this->model = incsub_sbe_get_model();
add_action( 'init', array( &$this, 'init_plugin' ), 1 );
add_action( 'init', array( &$this, 'confirm_subscription' ), 2 );
add_action( 'init', array( &$this, 'cancel_subscription' ), 20 );
if ( ! get_transient( 'incsub_sbe_updating' ) ) {
$settings = incsub_sbe_get_settings();
foreach ( $settings['post_types'] as $post_type ) {
add_action( "rest_after_insert_{$post_type}", array( $this, 'process_instant_subscriptions_for_gutenberg' ) );
}
add_action( 'transition_post_status', array( $this, 'process_instant_subscriptions' ), 2, 3 );
add_action( 'wp_loaded', array( &$this, 'process_scheduled_subscriptions' ) );
add_action( 'init', array( &$this, 'maybe_delete_logs' ) );
}
register_activation_hook( __FILE__, array( &$this, 'activate' ) );
register_deactivation_hook( __FILE__, array( &$this, 'deactivate' ) );
add_action( 'widgets_init', array( &$this, 'widget_init' ) );
add_action( 'plugins_loaded', array( &$this, 'load_text_domain' ) );
add_action( 'wpmu_drop_tables', array( &$this, 'uninstall' ) );
}
public function init_plugin() {
$this->register_taxonomies();
$this->maybe_upgrade_network();
$this->maybe_upgrade();
if ( ! get_transient( 'incsub_sbe_updating' ) )
$this->maybe_send_pending_emails();
if ( ! is_admin() ) {
require_once( INCSUB_SBE_PLUGIN_DIR . 'front/manage-subscription.php' );
$manage_subscription_page = new Incsub_Subscribe_By_Email_Manage_Subscription();
}
self::$admin_subscribers_page = new Incsub_Subscribe_By_Email_Admin_Subscribers_Page();
self::$admin_add_new_subscriber_page = new Incsub_Subscribe_By_Email_Admin_Add_Subscribers_Page();
self::$admin_settings_page = new Incsub_Subscribe_By_Email_Admin_Settings_Page();
self::$admin_sent_emails_page = new Incsub_Subscribe_By_Email_Sent_Emails_Page();
if ( is_multisite() ) {
require_once( INCSUB_SBE_PLUGIN_DIR . 'admin/pages/network-settings-page.php' );
self::$network_settings_page = new Incsub_Subscribe_By_Email_Network_Settings_Page();
}
$this->init_follow_button();
$this->init_recaptcha();
new Subscribe_By_Email_Shortcode();
}
public function init_follow_button() {
if ( ! is_admin() ) {
$settings = incsub_sbe_get_settings();
if ( ! $settings['follow_button'] )
return;
require_once( INCSUB_SBE_PLUGIN_DIR . 'front/follow-button.php' );
new Incsub_Subscribe_By_Email_Follow_Button( $settings );
}
}
public function init_recaptcha() {
if( is_admin() && ! wp_doing_ajax() ){
return;
}
require_once( INCSUB_SBE_PLUGIN_DIR . 'front/recaptcha.php' );
Incsub_Subscribe_By_Email_Recaptcha::get_instance();
}
public function widget_init() {
require_once( INCSUB_SBE_PLUGIN_DIR . 'front/widget.php' );
register_widget( 'Incsub_Subscribe_By_Email_Widget' );
}
public function load_text_domain() {
$locale = apply_filters( 'plugin_locale', get_locale(), INCSUB_SBE_LANG_DOMAIN );
load_textdomain( INCSUB_SBE_LANG_DOMAIN, WP_LANG_DIR . '/' . INCSUB_SBE_LANG_DOMAIN . '/' . INCSUB_SBE_LANG_DOMAIN . '-' . $locale . '.mo' );
load_plugin_textdomain( INCSUB_SBE_LANG_DOMAIN, FALSE, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Set the globals variables/constants
*/
private function set_globals() {
if ( ! defined( 'INCSUB_SBE_VERSION' ) )
define( 'INCSUB_SBE_VERSION', '3.6.0' );
if ( ! defined( 'INCSUB_SBE_PLUGIN_URL' ) )
define( 'INCSUB_SBE_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
if ( ! defined( 'INCSUB_SBE_PLUGIN_DIR' ) )
define( 'INCSUB_SBE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
if ( ! defined( 'INCSUB_SBE_LOGS_DIR' ) )
define( 'INCSUB_SBE_LOGS_DIR', WP_CONTENT_DIR . '/subscribe-by-email-logs' );
if ( ! defined( 'INCSUB_SBE_LANG_DOMAIN' ) )
define( 'INCSUB_SBE_LANG_DOMAIN', 'subscribe-by-email' );
if ( ! defined( 'INCSUB_SBE_ASSETS_URL' ) )
define( 'INCSUB_SBE_ASSETS_URL', INCSUB_SBE_PLUGIN_URL . 'assets/' );
if ( ! defined( 'INCSUB_SBE_PLUGIN_FILE' ) )
define( 'INCSUB_SBE_PLUGIN_FILE', plugin_basename( __FILE__ ) );
}
/**
* Include needed files
*/
private function includes() {
// Admin pages
require_once( INCSUB_SBE_PLUGIN_DIR . 'admin/pages/admin-page.php' );
require_once( INCSUB_SBE_PLUGIN_DIR . 'admin/pages/admin-settings-page.php' );
require_once( INCSUB_SBE_PLUGIN_DIR . 'admin/pages/admin-subscribers-page.php' );
require_once( INCSUB_SBE_PLUGIN_DIR . 'admin/pages/admin-add-subscribers-page.php' );
require_once( INCSUB_SBE_PLUGIN_DIR . 'admin/pages/admin-sent-emails-page.php' );
require_once( INCSUB_SBE_PLUGIN_DIR . 'admin/meta-boxes/do-not-send-meta-box.php' );
// Settings handler
require_once( INCSUB_SBE_PLUGIN_DIR . 'inc/settings.php' );
// Model
require_once( INCSUB_SBE_PLUGIN_DIR . 'model/model.php' );
require_once( INCSUB_SBE_PLUGIN_DIR . 'inc/integration.php' );
require_once( INCSUB_SBE_PLUGIN_DIR . 'front/shortcode.php' );
// Helpers
require_once( INCSUB_SBE_PLUGIN_DIR . 'inc/helpers/general-helpers.php' );
require_once( INCSUB_SBE_PLUGIN_DIR . 'inc/helpers/extra-fields-helpers.php' );
// Log class
require_once( INCSUB_SBE_PLUGIN_DIR . 'inc/debugger.php' );
// Subscriber class
require_once( INCSUB_SBE_PLUGIN_DIR . 'inc/classes/class-subscriber.php' );
require_once( INCSUB_SBE_PLUGIN_DIR . 'inc/classes/class-campaign.php' );
require_once( INCSUB_SBE_PLUGIN_DIR . 'inc/classes/class-queue-item.php' );
}
public function activate() {
$model = Incsub_Subscribe_By_Email_Model::get_instance();
$model->create_squema();
$model->create_network_squema();
$this->register_taxonomies();
flush_rewrite_rules();
$this->add_capabilities();
update_option( 'incsub_sbe_version', INCSUB_SBE_VERSION );
update_site_option( 'incsub_sbe_network_version', INCSUB_SBE_VERSION );
$settings = incsub_sbe_get_settings();
incsub_sbe_update_settings( $settings );
}
public function deactivate() {}
public function add_capabilities() {
$role = get_role( 'administrator' );
$role->add_cap( 'manage_subscribe_by_email' );
}
public function register_taxonomies() {
// Register the Subscriber Post Type
$labels = array(
'name' => _x( 'Subscribers', 'Post Type General Name', 'subscribe-by-email' ),
'singular_name' => _x( 'Subscriber', 'Post Type Singular Name', 'subscribe-by-email' ),
'menu_name' => __( 'Subscribers', 'subscribe-by-email' ),
'parent_item_colon' => __( 'Parent Subscriber:', 'subscribe-by-email' ),
'all_items' => __( 'All Subscribers', 'subscribe-by-email' ),
'view_item' => __( 'View Subscriber', 'subscribe-by-email' ),
'add_new_item' => __( 'Add New Subscriber', 'subscribe-by-email' ),
'add_new' => __( 'Add New', 'subscribe-by-email' ),
'edit_item' => __( 'Edit Subscriber', 'subscribe-by-email' ),
'update_item' => __( 'Update Subscriber', 'subscribe-by-email' ),
'search_items' => __( 'Search Subscriber', 'subscribe-by-email' ),
'not_found' => __( 'Not found', 'subscribe-by-email' ),
'not_found_in_trash' => __( 'Not found in Trash', 'subscribe-by-email' ),
);
$capabilities = array(
'edit_post' => 'manage_subscribe_by_email',
'read_post' => 'manage_subscribe_by_email',
'delete_post' => 'manage_subscribe_by_email',
'edit_posts' => 'manage_subscribe_by_email',
'edit_others_posts' => 'manage_subscribe_by_email',
'publish_posts' => 'manage_subscribe_by_email',
'read_private_posts' => 'manage_subscribe_by_email',
);
$args = array(
'label' => __( 'Subscriber', 'subscribe-by-email' ),
'description' => __( 'Subscribers', 'subscribe-by-email' ),
'labels' => $labels,
'supports' => array( 'title' ),
'hierarchical' => false,
'public' => false,
'show_ui' => false,
'show_in_menu' => false,
'show_in_nav_menus' => false,
'show_in_admin_bar' => false,
'menu_position' => 5,
'menu_icon' => '',
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => false,
'rewrite' => false,
'capabilities' => $capabilities,
);
register_post_type( 'subscriber', $args );
}
public function uninstall( $tables ) {
$model = incsub_sbe_get_model();
$new_tables = array( $model->subscriptions_log_table );
return array_merge( $tables, $new_tables );
}
/**
* Upgrade settings and tables to the new version
*/
public function maybe_upgrade() {
$current_version = get_option( 'incsub_sbe_version' );
if ( $current_version == INCSUB_SBE_VERSION )
return;
if ( $current_version === false ) {
$this->activate();
return;
}
set_transient( 'incsub_sbe_updating', true, 1800 );
do_action( 'sbe_upgrade', $current_version, INCSUB_SBE_VERSION );
include_once( INCSUB_SBE_PLUGIN_DIR . 'upgrade.php' );
update_option( 'incsub_sbe_version', INCSUB_SBE_VERSION );
delete_transient( 'incsub_sbe_updating' );
}
private function maybe_upgrade_network() {
$current_network_version = get_site_option( 'incsub_sbe_network_version' );
if ( $current_network_version == INCSUB_SBE_VERSION )
return;
if ( $current_network_version === false ) {
$this->activate();
return;
}
if ( version_compare( $current_network_version, '2.9.1', '<' ) ) {
$model = incsub_sbe_get_model();
$model->create_network_squema();
}
if ( version_compare( $current_network_version, '3.0.2', '<' ) ) {
$model = incsub_sbe_get_model();
$model->create_network_squema();
}
update_site_option( 'incsub_sbe_network_version', INCSUB_SBE_VERSION );
}
/**
* Subscribe a new user
*
* @param Integer $subscription_id
*/
public static function subscribe_user( $user_email, $note, $type, $autopt = false, $meta = array() ) {
$subscribe_user = apply_filters( 'sbe_pre_subscribe_user', true, $user_email, $note, $type, $autopt, $meta );
if ( ! $subscribe_user )
return false;
$args = array(
'note' => $note,
'type' => $type,
'meta' => $meta
);
$sid = incsub_sbe_insert_subscriber( $user_email, $autopt, $args );
return $sid;
}
/**
* Sends a confirmation mail. Make uses of Confirmation Mail Template
*
* @param Integer $subscription_id
*/
public static function send_confirmation_mail( $subscription_id, $force = false ) {
incsub_sbe_send_confirmation_email( $subscription_id, $force );
}
/**
* Loaded on front page, confirm a subscription linked from a user mail
*/
public function confirm_subscription() {
if ( isset( $_GET['sbe_confirm'] ) ) {
$subscriber = incsub_sbe_get_subscriber_by_key( $_GET['sbe_confirm'] );
if ( ! $subscriber ) {
$this->sbe_subscribing_notice( __( 'Sorry, your subscription no longer exists, please subscribe again.', INCSUB_SBE_LANG_DOMAIN ), __( 'Your subscription no longer exists', INCSUB_SBE_LANG_DOMAIN ) );
wp_die();
}
if ( $subscriber->is_confirmed() ) {
$redirect_to = remove_query_arg( 'sbe_confirm' );
wp_redirect( $redirect_to );
exit;
}
incsub_sbe_confirm_subscription( $subscriber->ID );
$title = __( 'Thank you, your subscription has been confirmed.', INCSUB_SBE_LANG_DOMAIN );
$this->sbe_subscribing_notice( $title, $title );
$settings = incsub_sbe_get_settings();
if ( $settings['get_notifications'] ) {
require_once( INCSUB_SBE_PLUGIN_DIR . 'inc/mail-templates/administrators-notices.php' );
$admin_notice = new Incsub_Subscribe_By_Email_Administrators_Subscribed_Notice_Template( $subscriber->subscription_email );
$admin_notice->send_email();
}
wp_die();
}
}
/**
* Loaded on front page, cancel a subscription linked from a user mail
*/
public function cancel_subscription() {
if ( isset( $_GET['sbe_unsubscribe'] ) ) {
$subscriber = incsub_sbe_get_subscriber_by_key( $_GET['sbe_unsubscribe'] );
if ( $subscriber ) {
incsub_sbe_cancel_subscription( $subscriber->ID );
$settings = incsub_sbe_get_settings();
if ( $settings['get_notifications'] ) {
require_once( INCSUB_SBE_PLUGIN_DIR . 'inc/mail-templates/administrators-notices.php' );
$admin_notice = new Incsub_Subscribe_By_Email_Administrators_Unsubscribed_Notice_Template( $subscriber->subscription_email );
$admin_notice->send_email();
}
}
$title = __( 'Your email subscription has been successfully cancelled.', INCSUB_SBE_LANG_DOMAIN );
$this->sbe_subscribing_notice( $title, $title );
wp_die();
}
}
public function sbe_subscribing_notice( $text, $title = false ) {
nocache_headers();
@header( 'Content-Type: ' . get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' ) );
if ( empty( $title ) )
$title = get_bloginfo( 'blogtitle' );
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
<head>
<meta name="viewport" content="width=device-width" />
<meta http-equiv="Content-Type" content="<?php bloginfo( 'html_type' ); ?>; charset=<?php echo get_option( 'blog_charset' ); ?>" />
<title><?php echo esc_html( $title ); ?></title>
<?php $this->subscribe_notices_styles(); ?>
</head>
<body class="wp-core-ui">
<div class="sbe-notice">
<p> <?php echo esc_html( $text ); ?> </p>
<a href="<?php echo get_home_url(); ?>" class="button-primary" title="<?php esc_attr_e( 'Go to site', INCSUB_SBE_LANG_DOMAIN ); ?>"><?php _e( 'Go to site', INCSUB_SBE_LANG_DOMAIN ); ?></a>
</div>
</body>
</html>
<?php
}
private function subscribe_notices_styles() {
global $content_width;
if ( empty( $content_width ) )
$content_width = 900;
$settings = incsub_sbe_get_settings();
?>
<style>
.wp-core-ui {
border-top:2px solid <?php echo $settings['header_color']; ?>;
border-bottom:2px solid <?php echo $settings['header_color']; ?>;
}
.wp-core-ui .button-primary {
background: #2ea2cc;
border-color: #0074a2;
-webkit-box-shadow: inset 0 1px 0 rgba( 120, 200, 230, 0.5), 0 1px 0 rgba( 0, 0, 0, 0.15 );
box-shadow: inset 0 1px 0 rgba( 120, 200, 230, 0.5 ), 0 1px 0 rgba( 0, 0, 0, 0.15 );
color: #fff;
text-decoration: none;
vertical-align: baseline;
display: inline-block;
font-size: 13px;
line-height: 26px;
height: 28px;
margin: 0;
padding: 0 10px 1px;
cursor: pointer;
border-width: 1px;
border-style: solid;
-webkit-appearance: none;
-webkit-border-radius: 3px;
border-radius: 3px;
white-space: nowrap;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.wp-core-ui .button-primary:hover {
background: #1e8cbe;
border-color: #0074a2;
-webkit-box-shadow: inset 0 1px 0 rgba( 120, 200, 230, 0.6 );
box-shadow: inset 0 1px 0 rgba( 120, 200, 230, 0.6 );
color: #fff;
}
.wp-core-ui .button-primary:focus {
-webkit-box-shadow: inset 0 1px 0 rgba( 120, 200, 230, 0.6 ), 0 0 0 1px #5b9dd9, 0 0 2px 1px rgba(30, 140, 190, .8);
box-shadow: inset 0 1px 0 rgba( 120, 200, 230, 0.6 ), 0 0 0 1px #5b9dd9, 0 0 2px 1px rgba(30, 140, 190, .8);
background: #1e8cbe;
border-color: #0074a2;
color: #fff;
}
.wp-core-ui .button-primary:active {
background: #1b7aa6;
border-color: #005684;
color: rgba( 255, 255, 255, 0.95 );
-webkit-box-shadow: inset 0 1px 0 rgba( 0, 0, 0, 0.1 );
box-shadow: inset 0 1px 0 rgba( 0, 0, 0, 0.1 );
vertical-align: top;
outline: none;
}
</style>
<?php
}
/**
* Send a batch of mails
*
*/
public function maybe_send_pending_emails() {
if ( ! get_transient( self::$pending_mails_transient_slug ) ) {
// We first set the transient for at least 20min
set_transient( self::$pending_mails_transient_slug, 'next', 1200 );
do_action( 'sbe_before_send_pending_emails' );
$settings = incsub_sbe_get_settings();
$args = apply_filters( 'sbe_send_pending_emails_args', array(
'per_page' => $settings['mails_batch_size']
) );
$queue_items = incsub_sbe_get_queue_items( $args );
$return = array();
if ( ! empty( $queue_items['items'] ) ) {
incsub_sbe_include_templates_files();
$digest_sender = new SBE_Digest_Sender();
$content_generator = new Incsub_Subscribe_By_Email_Content_Generator( $settings['frequency'], $settings['post_types'] );
foreach ( $queue_items['items'] as $item ) {
$subscriber = incsub_sbe_get_subscriber( $item->subscriber_email );
// In order to avoid duplicated emails we'll set temporary this email as sent
incsub_sbe_set_queue_item_sent_status( $item->id, 1 );
incsub_sbe_increment_campaign_recipients( $item->campaign_id );
$result = $digest_sender->send_digest( $item->get_subscriber_posts(), $subscriber );
// Now we update the status
incsub_sbe_set_queue_item_sent_status( $item->id, absint( $result ) );
if ( $result === 4 ) {
// There have been an error in PHPMailer?
global $phpmailer;
if ( ! empty( $phpmailer->ErrorInfo ) ) {
incsub_sbe_set_queue_item_error_message( $item->id, $phpmailer->ErrorInfo );
}
}
$return[ $item->subscriber_email ] = $result;
}
}
do_action( 'sbe_after_send_pending_emails' );
// Once we have finished we set the transient right
set_transient( self::$pending_mails_transient_slug, 'next', self::$time_between_batches );
return $return;
}
return false;
}
public function enqueue_emails( $posts_ids = array() ) {
$args = array( 'posts_ids' => array() );
$settings = incsub_sbe_get_settings();
// Check if the post has been already sent
foreach ( $posts_ids as $post_id ) {
$is_sent = get_post_meta( $post_id, 'sbe_sent', true );
$do_not_send = get_post_meta( $post_id, '_sbe_do_not_send', true );
if ( ! $do_not_send && is_admin() && isset( $_POST['sbe-do-not-send'] ) )
$do_not_send = true;
if ( ! $is_sent && ! $do_not_send )
$args['posts_ids'][] = $post_id;
}
// Check if the post type is valid
$allowed_post_types = $settings['post_types'];
foreach ( $args['posts_ids'] as $key => $post_id ) {
if ( ! in_array( get_post_type( $post_id ), $allowed_post_types ) ) {
unset( $args['posts_ids'][ $key ] );
}
}
// Check if the taxonomy is valid
foreach ( $args['posts_ids'] as $key => $post_id ) {
$post_type = get_post_type( $post_id );
if ( isset( $settings['taxonomies'][ $post_type ] ) ) {
$allowed_post_type_taxonomies = array_keys( $settings['taxonomies'][ $post_type ] );
$allowed_post_type_terms = array();
foreach ( $allowed_post_type_taxonomies as $post_type_tax ) {
$allowed_post_type_terms = array_merge( $allowed_post_type_terms, array_values( $settings['taxonomies'][ $post_type ][ $post_type_tax ] ) );
}
}
else {
// The post type has no taxonomies, send it
$allowed_post_type_terms = array( 'all' );
$allowed_post_type_taxonomies = array();
}
if ( in_array( 'all', $allowed_post_type_terms ) ) {
// All terms in this taxonomy are accepted
continue;
}
$post_terms = wp_get_post_terms( $post_id, $allowed_post_type_taxonomies );
if ( is_wp_error( $post_terms ) )
$post_terms = array();
$post_terms = wp_list_pluck( $post_terms, 'term_id' );
$intersect = array_intersect( $post_terms, $allowed_post_type_terms );
if ( empty( $intersect ) )
unset( $args['posts_ids'][ $key ] );
}
$args = apply_filters( 'sbe_enqueue_emails_campaign_args', $args );
if ( empty( $args['posts_ids'] ) )
return;
$campaign_id = incsub_sbe_insert_campaign( '', $args );
$result = incsub_sbe_insert_queue_items( $campaign_id );
if ( ! $result )
incsub_sbe_delete_campaign( $campaign_id );
foreach ( $args['posts_ids'] as $post_id )
update_post_meta( $post_id, 'sbe_sent', true );
return $campaign_id;
}
/**
* Executed each time a post created via the REST API.
*
* If the user wants to send a newsletter everytime a post is published
* it will call send_mail function
*
* @param Object $post
* @return type
*/
public function process_instant_subscriptions_for_gutenberg( $post ) {
$settings = incsub_sbe_get_settings();
if ( in_array( $post->post_type, $settings['post_types'] ) && 'publish' == $post->post_status && $settings['frequency'] == 'inmediately' ) {
$this->enqueue_emails( array( $post->ID ) );
// Trigger the first batch
delete_transient( self::$pending_mails_transient_slug );
}
}
/**
* Executed each time a post changes its status. If the user
* wants to send a newsletter everytime a post is published
* it will call send_mail function
*
* @param String $new_status
* @param String $old_status
* @param Object $post
* @return type
*/
public function process_instant_subscriptions( $new_status, $old_status, $post ) {
$settings = incsub_sbe_get_settings();
if ( in_array( $post->post_type, $settings['post_types'] ) && $new_status != $old_status && 'publish' == $new_status && $settings['frequency'] == 'inmediately' ) {
$this->enqueue_emails( array( $post->ID ) );
// Trigger the first batch
delete_transient( self::$pending_mails_transient_slug );
}
}
public function process_scheduled_subscriptions() {
$settings = incsub_sbe_get_settings();
if ( 'weekly' == $settings['frequency'] && $next_time = get_option( self::$freq_weekly_transient_slug ) ) {
if ( current_time( 'timestamp' ) > $next_time ) {
$model = incsub_sbe_get_model();
self::set_next_week_schedule_time( $settings['day_of_week'], $settings['time'] );
$days = self::get_last_x_days_sending_time( 7 );
$args = array(
'post_type' => $settings['post_types'],
'after_date' => date( 'Y-m-d H:i:s', $days )
);
$posts_ids = incsub_sbe_get_digest_posts_ids( $args );
$this->enqueue_emails( $posts_ids );
// Trigger the first batch
delete_transient( self::$pending_mails_transient_slug );
}
}
elseif ( 'daily' == $settings['frequency'] && $next_time = get_option( self::$freq_daily_transient_slug ) ) {
if ( current_time( 'timestamp' ) > $next_time ) {
$model = incsub_sbe_get_model();
self::set_next_day_schedule_time( $settings['time'] );
$days = self::get_last_x_days_sending_time( 1 );
$args = array(
'post_type' => $settings['post_types'],
'after_date' => date( 'Y-m-d H:i:s', $days )
);
$posts_ids = incsub_sbe_get_digest_posts_ids( $args );
$this->enqueue_emails( $posts_ids );
// Trigger the first batch
delete_transient( self::$pending_mails_transient_slug );
}
}
}
public static function get_last_x_days_sending_time( $days ) {
$today_sending_time = self::get_today_sending_time();
return strtotime( '-' . $days . ' days', $today_sending_time );
}
public static function get_today_sending_time() {
$settings = incsub_sbe_get_settings();
$time = str_pad( $settings['time'], 2, '0', STR_PAD_LEFT );
$current_date = date( 'Y-m-d', current_time( 'timestamp' ) );
$sending_time = $current_date . ' ' . $time . ':00:00';
return strtotime( $sending_time );
}
/**
* Set the next schedule for a weekly frequency
*
* @param String $day_of_week
*/
public static function set_next_week_schedule_time( $day_of_week, $time ) {
switch ( $day_of_week ) {
case 0:
$day = 'sunday';
break;
case 1:
$day = 'monday';
break;
case 2:
$day = 'tuesday';
break;
case 3:
$day = 'wednesday';
break;
case 4:
$day = 'thursday';
break;
case 5:
$day = 'friday';
break;
case 6:
$day = 'saturday';
break;
}
$next_time = strtotime( 'next ' . $day, current_time( 'timestamp' ) );
$next_time_date = date( 'Y-m-d', $next_time ) . ' ' . str_pad( $time, 2, 0, STR_PAD_LEFT ) . ':00:00';
$next_time_timestamp = strtotime( $next_time_date );
update_option( self::$freq_weekly_transient_slug, $next_time_timestamp );
delete_option( self::$freq_daily_transient_slug );
}
/**
* Set the next schedule for a daily frequency
*
* @param Integer $time Hour of a day
*/
public static function set_next_day_schedule_time( $time ) {
$today = date( 'Y-m-d', current_time( 'timestamp' ) );
$today_at_time = $today . ' ' . str_pad( $time, 2, 0, STR_PAD_LEFT ) . ':00:00';
$today_at_time_unix = strtotime( $today_at_time );
$next_time = strtotime( '+1 day', $today_at_time_unix );
$next_time_mysql = date( 'Y-m-d H:i:s', $next_time );
update_option( self::$freq_daily_transient_slug, $next_time );
delete_option( self::$freq_weekly_transient_slug );
}
public static function get_next_scheduled_date() {
$settings = incsub_sbe_get_settings();
if ( 'daily' == $settings['frequency'] && $time = get_option( self::$freq_daily_transient_slug ) ) {
return date( get_option('date_format', 'Y-m-d' ) . ' ' . get_option( 'time_format', 'H:i:s') , $time );
}
if ( 'weekly' == $settings['frequency'] && $time = get_option( self::$freq_weekly_transient_slug ) ) {
return date( get_option('date_format', 'Y-m-d' ) . ' ' . get_option( 'time_format', 'H:i:s') , $time );
}
}
public static function get_next_scheduled_batch_date() {
$timeout = get_option( '_transient_timeout_' . self::$pending_mails_transient_slug );
$date = date_i18n( 'Y-m-d H:i:s', $timeout );
return get_date_from_gmt( $date, get_option('date_format', 'Y-m-d' ) . ' ' . get_option( 'time_format', 'H:i:s') );
}
public function maybe_delete_logs() {
if ( ! get_transient( 'incsub_sbe_check_logs' ) ) {
$settings = incsub_sbe_get_settings();
$model = incsub_sbe_get_model();
$days_old = absint( $settings['keep_logs_for'] );
$timestamp_old = current_time( 'timestamp' ) - ( $days_old * 24 * 60 * 60 );
$old_campaigns = incsub_sbe_get_campaigns_since( $timestamp_old );
if ( ! empty( $old_campaigns ) ) {
foreach ( $old_campaigns as $campaign )
incsub_sbe_delete_campaign( $campaign->id );
}
set_transient( 'incsub_sbe_check_logs', true, 86400 ); // We'll check every day
}
}
}
if ( ! function_exists( 'subscribe_by_email' ) ) {
function subscribe_by_email() {
return Incsub_Subscribe_By_Email::get_instance();
}
global $subscribe_by_email_plugin;
$subscribe_by_email_plugin = subscribe_by_email();
}