/
mailing-group-module.php
1826 lines (1597 loc) · 59.8 KB
/
mailing-group-module.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
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php /**
* @package Mailing_group_module
* @version 1.0
*/
/*
Plugin Name: WP Mailing Groups
Plugin URI: http://andres.codes
Description: WPMailman is a plugin for managing electronic mail discussion and discussion/workgroups groups. WP Mailman is integrated with the web, making it easy for users to manage their accounts and for list owners to administer their lists. WP Mailman supports built-in archiving, content filtering, and more.
Author: Andres Escobar
Version: 1.0
*/
if ( preg_match( '#' . basename( __FILE__ ) . '#', $_SERVER['PHP_SELF'] ) ) {
die( 'You are not allowed to call this page directly.' );
}
/**
* Indicates that a clean exit occured. Handled by set_exception_handler
*/
if ( ! class_exists( 'E_Clean_Exit' ) ) {
class E_Clean_Exit extends RuntimeException {
}
}
define( "WPMG_PLUGIN_URL", plugin_dir_url( __FILE__ ) );
define( "WPMG_PRODUCT_ITEM_NAME", "WP Mailman" );
define( "WPMG_STORE_URL", "" );
/* Class to be used in complete plugin for all db requests */
require_once( "lib/mailinggroupclass.php" );
$objMem = new mailinggroupClass();
global $objMem;
require_once( "lib/receivemail.class.php" );
$obj = new receiveMail( '', '', '', $mailserver = '', $servertype = '', $port = '', $ssl = '' );
global $obj;
/*Define global variable to be used in plugin*/
global $wpdb, $table_name_group, $table_name_message, $table_name_requestmanager, $table_name_requestmanager_taxonomy, $table_name_user_taxonomy, $table_name_parsed_emails, $table_name_emails_attachments, $table_name_sent_emails, $table_name_users, $table_name_usermeta;
$visibilityArray = array(
'Public' => '1',
'Invitation' => '2',
'Private' => '3'
);
$WPMG_SETTINGS = get_option( "WPMG_SETTINGS" );
global $WPMG_SETTINGS;
$table_name_users = $wpdb->prefix . "users";
$table_name_usermeta = $wpdb->prefix . "usermeta";
add_action( 'init', 'Mailing_Groups' );
add_action( 'add_meta_boxes', 'add_custom_meta_box' );
add_action( 'save_post', 'save_custom_meta', 10, 2 );
function Mailing_Groups() {
$labels = array(
'name' => _x( 'Mailing Groups', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Mailing Group', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Mailing Groups', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'all_items' => __( 'All Items', 'text_domain' ),
'view_item' => __( 'View Item', 'text_domain' ),
'add_new_item' => __( 'New Mailing Group', 'text_domain' ),
'add_new' => __( 'New Mailing Group', 'text_domain' ),
'edit_item' => __( 'Adjust Settings', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'search_items' => __( 'Search Item', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' )
);
$args = array(
'label' => __( 'mg_groups', 'text_domain' ),
'description' => __( 'Mailing Groups', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'author', ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 1,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page'
);
register_post_type( 'mg_groups', $args );
$labels = array(
'name' => _x( 'Group Threads', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Group Thread', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Group Threads', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'all_items' => __( 'All Items', 'text_domain' ),
'view_item' => __( 'View Item', 'text_domain' ),
'add_new_item' => __( 'New Thread', 'text_domain' ),
'add_new' => __( 'New Group Thread', 'text_domain' ),
'edit_item' => __( 'Adjust Email Display', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'search_items' => __( 'Search Item', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' )
);
$args = array(
'label' => __( 'mg_threads', 'text_domain' ),
'description' => __( 'Group Threads', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'page-attributes' ),
'taxonomies' => array( 'post_tag' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 1,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'mg_threads', $args );
}
// Add the Meta Box
function add_custom_meta_box() {
add_meta_box(
'mg_group_fields', // $id
'Mailing Group Options', // $title
'show_custom_meta_box', // $callback
'mg_groups', // $page
'normal', // $context
'high',
mg_group_custom_meta_fields()
);
add_meta_box(
'mg_group_thread_fields',
'Thread Content',
'show_custom_meta_box',
'mg_threads',
'normal',
'high',
mg_thread_custom_meta_fields()
);
}
//Group Custom Fields
function mg_group_custom_meta_fields() {
$prefix = 'mg_group_';
$custom_meta_fields = array(
array(
'label' => 'Use Title In Subject?',
'desc' => 'When sending out emails,append group title to subject line',
'id' => $prefix . 'use_in_subject',
'type' => 'checkbox'
),
array(
'label' => 'Group Email Address',
'id' => $prefix . 'email',
'type' => 'text'
),
array(
'label' => 'Password:',
'id' => $prefix . 'password',
'type' => 'password'
),
array(
'label' => 'Access Mailbox via :',
'id' => $prefix . 'server_type',
'type' => 'radio',
'options' => array(
'one' => array(
'label' => 'POP3',
'value' => 'pop3'
),
'two' => array(
'label' => 'IMAP',
'value' => 'imap'
)
)
),
array(
'label' => 'Incoming Mail Server :',
'id' => $prefix . 'server',
'type' => 'text'
),
array(
'label' => 'Port:',
'id' => $prefix . 'server_port',
'type' => 'text'
),
array(
'label' => 'User/Pass Required?',
'id' => $prefix . 'up_required',
'type' => 'checkbox'
),
array(
'label' => 'Username:',
'id' => $prefix . 'mail_username',
'type' => 'text'
),
array(
'label' => 'Password:',
'id' => $prefix . 'mail_password',
'type' => 'password'
),
array(
'label' => 'SSL/Secure',
'id' => $prefix . 'pop_ssl',
'type' => 'checkbox'
),
array(
'label' => 'Choose Mailing Function :',
'id' => $prefix . 'mail_type',
'type' => 'radio',
'options' => array(
'one' => array(
'label' => 'WP Mail',
'value' => 'wp'
),
'two' => array(
'label' => 'SMTP Mail',
'value' => 'smtp'
),
'three' => array(
'label' => 'PHP Mail(No Attachments)',
'value' => 'php'
)
)
),
array(
'label' => 'SMTP Server:',
'id' => $prefix . 'smtp_server',
'type' => 'text'
),
array(
'label' => 'Port:',
'id' => $prefix . 'smtp_port',
'type' => 'text'
),
array(
'label' => 'SSL/Secure Connection',
'id' => $prefix . 'smtp_ssl',
'type' => 'checkbox'
),
array(
'label' => 'Username:',
'id' => $prefix . 'smtp_username',
'type' => 'text'
),
array(
'label' => 'Password:',
'id' => $prefix . 'smtp_password',
'type' => 'password'
),
array(
'label' => 'Public Archive:',
'id' => $prefix . 'archive',
'type' => 'checkbox'
),
array(
'label' => 'Auto-delete old messages:',
'id' => $prefix . 'auto_delete',
'type' => 'radio',
'options' => array(
'one' => array(
'label' => 'No',
'value' => 'no'
),
'two' => array(
'label' => 'Yes',
'value' => 'yes'
)
)
),
array(
'label' => 'Days',
'id' => $prefix . 'auto_delete_limit',
'type' => 'text'
),
array(
'label' => 'Footer text for emails:',
'id' => $prefix . 'footer_text',
'type' => 'textarea',
'default' => '-- -- -- --
This message was sent to <a href="{%archive_url%}">{%grouptitle%}</a> by <a href="{%site_url%}">Site</a>'
),
array(
'label' => 'Available Variables',
'type' => 'description_block',
'example' => '<code>
{%grouptitle%} = Group Title<br>
{%site_url%} = Sites URL<br>
{%archive_url%} = Message Archive page URL<br>
{%profile_url%} = User profile URL<br></code>'
),
array(
'label' => 'Settings for Subscription Request messages',
'label_type' => 'header'
),
array(
'label' => 'Sender name:',
'id' => $prefix . 'sender_name',
'type' => 'text'
),
array(
'label' => 'Sender email:',
'id' => $prefix . 'sender_email',
'type' => 'text'
),
array(
'label' => 'Mailing Group Status',
'label_type' => 'header'
),
array(
'label' => 'Status:',
'id' => $prefix . 'status',
'type' => 'select',
'options' => array(
'one' => array(
'label' => 'Select Status',
'value' => 0
),
'two' => array(
'label' => 'Inactive',
'value' => 1
),
'three' => array(
'label' => 'Active',
'value' => 2
)
)
),
array(
'label' => 'Visibility:',
'id' => $prefix . 'visibility',
'type' => 'select',
'options' => array(
'one' => array(
'label' => 'Public',
'value' => 1
),
'two' => array(
'label' => 'Invitation',
'value' => 2
),
'three' => array(
'label' => 'Private',
'value' => 3
)
)
),
array(
'label' => 'Old Archived URL:',
'id' => $prefix . 'old_archive_url',
'type' => 'text'
)
);
return $custom_meta_fields;
}
//Threads Custom Fields
function mg_thread_custom_meta_fields() {
$prefix = 'mg_thread_';
$custom_meta_fields = array(
array(
'label' => 'Type',
'id' => $prefix . 'type',
'type' => 'text'
),
array(
'label' => 'Unique Header ID',
'id' => $prefix . 'UID',
'type' => 'text'
),
array(
'label' => 'References',
'id' => $prefix . 'references',
'type' => 'text'
),
array(
'label' => 'Parent ID',
'id' => $prefix . 'parent_id',
'type' => 'text'
),
array(
'label' => 'Email Bounced',
'id' => $prefix . 'email_bounced',
'type' => 'text'
),
array(
'label' => 'Email From',
'id' => $prefix . 'email_from',
'type' => 'text'
),
array(
'label' => 'Email From Name',
'id' => $prefix . 'email_from_name',
'type' => 'text'
),
array(
'label' => 'Email To',
'id' => $prefix . 'email_to',
'type' => 'text'
),
array(
'label' => 'Email To Name',
'id' => $prefix . 'email_to_name',
'type' => 'text'
),
array(
'label' => 'Email Subject',
'id' => $prefix . 'email_subject',
'type' => 'text'
),
array(
'label' => 'Email Content',
'readonly' => $prefix . 'email_content',
'type' => 'email'
),
array(
'label' => 'Email Group ID',
'id' => $prefix . 'email_group_id',
'type' => 'text'
),
array(
'label' => 'Date',
'id' => $prefix . 'date',
'type' => 'text'
),
array(
'label' => 'Status',
'id' => $prefix . 'email_status',
'type' => 'text'
),
array(
'label' => 'Status Error',
'id' => $prefix . 'email_status_error',
'type' => 'textarea'
)
);
return $custom_meta_fields;
}
//Show Boxes
function show_custom_meta_box( $post, $metabox ) {
// Field Array
$custom_meta_fields = $metabox['args'];
// Use nonce for verification
echo '<input type="hidden" name="custom_meta_box_nonce" value="' . wp_create_nonce( basename( __FILE__ ) ) . '" />';
// Begin the field table and loop
echo '<div class="form-table">';
foreach ( $custom_meta_fields as $field ) {
// get value of this field if it exists for this post
$meta = get_post_meta( $post->ID, $field['id'], true );
// begin a table row with
switch ( $field['label_type'] ) {
case '':
echo '<div class="' . $field['type'] . '" id="' . $field['id'] . 'Contain"><label for="' . $field['id'] . '">' . $field['label'] . '</label>';
break;
case 'header':
echo '<div class="' . $field['label_type'] . '"><h3>' . $field['label'] . '</h3></div>';
break;
}
switch ( $field['type'] ) {
// case items will go here
// text
case 'text':
echo '<input type="text" name="' . $field['id'] . '" id="' . $field['id'] . '" value="' . $meta . '" size="30" />
<br /><span class="description">' . $field['desc'] . '</span></div>';
break;
// password
case 'password':
echo '<input type="password" name="' . $field['id'] . '" id="' . $field['id'] . '" value="' . $meta . '" size="30" />
<br /><span class="description">' . $field['desc'] . '</span></div>';
break;
// textarea
case 'textarea':
$meta = ( $meta != '' ) ? $meta : $field['default'];
echo '<textarea name="' . $field['id'] . '" id="' . $field['id'] . '" cols="50" rows="5" >' . $meta . '</textarea>
<br /><span class="description">' . $field['desc'] . '</span></div>';
break;
// Email Content
case 'email':
echo '<div class="' . $field['type'] . 'Contain"> ' . get_post_meta( $post->ID, $field['readonly'], true ) . '</div></div>';
break;
// description_block
case 'description_block':
echo '' . $field['example'] . '</div>';
break;
// checkbox
case 'checkbox':
echo '<input type="checkbox" name="' . $field['id'] . '" id="' . $field['id'] . '" ', $meta ? ' checked="checked"' : '', '/><p class="checkbox" for="' . $field['id'] . '">' . $field['desc'] . '</p></div>';
break;
// select
case 'select':
echo '<select name="' . $field['id'] . '" id="' . $field['id'] . '">';
foreach ( $field['options'] as $option ) {
echo '<option', $meta == $option['value'] ? ' selected="selected"' : '', ' value="' . $option['value'] . '">' . $option['label'] . '</option></div>';
}
echo '</select><br /><span class="description">' . $field['desc'] . '</span></div>';
break;
// radio
case 'radio':
foreach ( $field['options'] as $option ) {
echo '<input type="radio" name="' . $field['id'] . '" id="' . $option['value'] . '" value="' . $option['value'] . '" ', $meta == $option['value'] ? ' checked="checked"' : '', ' />
<label class="radio" for="' . $option['value'] . '">' . $option['label'] . '</label>';
}
echo '</div>';
break;
} //end switch
} // end foreach
echo '</div>'; // end table
}
// Save the Data
function save_custom_meta( $post_id, $post ) {
$custom_meta_fields = '';
if ( $post->post_type == 'mg_groups' ) {
$custom_meta_fields = mg_group_custom_meta_fields();
}
if ( $post->post_type == 'mg_threads' ) {
$custom_meta_fields = mg_thread_custom_meta_fields();
}
// verify nonce
if ( ! wp_verify_nonce( $_POST['custom_meta_box_nonce'], basename( __FILE__ ) ) ) {
return $post_id;
}
// check autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// check permissions
if ( 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} elseif ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
// loop through fields and save the data
foreach ( $custom_meta_fields as $field ) {
$old = get_post_meta( $post_id, $field['id'], true );
$new = $_POST[ $field['id'] ];
if ( $new && $new != $old ) {
update_post_meta( $post_id, $field['id'], $new );
} elseif ( '' == $new && $old ) {
delete_post_meta( $post_id, $field['id'], $old );
}
} // end foreach
}
add_action( 'init', 'do_output_buffer' );
function do_output_buffer() {
ob_start();
}
add_filter( 'cron_schedules', 'cron_add_weekly' );
function cron_add_weekly( $schedules ) {
// Adds once weekly to the existing schedules.
/*$schedules['wpmg_two_minute'] = array(
'interval' => 120,
'display' => __( 'Every Two Minutes', 'mailing-group-module' )
);
$schedules['wpmg_five_minute'] = array(
'interval' => 300,
'display' => __( 'Every Five Minutes', 'mailing-group-module' )
);
$schedules['wpmg_fifteen_minute'] = array(
'interval' => 900,
'display' => __( 'Every Fifteen Minutes', 'mailing-group-module' )
);*/
//Testing purposes
$schedules['wpmg_two_minute'] = array(
'interval' => 120,
'display' => __( 'Every Two Minutes' )
);
$schedules['wpmg_three_minute'] = array(
'interval' => 180,
'display' => __( 'Every Three Minutes' )
);
$schedules['wpmg_five_minute'] = array(
'interval' => 300,
'display' => __( 'Every Five Minutes' )
);
$schedules['wpmg_fifteen_minute'] = array(
'interval' => 900,
'display' => __( 'Every Fifteen Minutes' )
);
return $schedules;
}
/* Install Plugin */
register_activation_hook( __FILE__, 'wpmg_add_mailing_group_plugin' );
function wpmg_add_mailing_group_plugin() {
/* Class to be used in complete plugin for all db requests */
require_once( "lib/mailinggroupclass.php" );
$objMem = new mailinggroupClass();
/* Commented out to test if caching was an issue, resolved. Can remove*/
// if ( ! wp_next_scheduled( 'wpmg_cron_task_send_email' ) ) {
// wp_schedule_event( time(), 'wpmg_two_minute', 'wpmg_cron_task_send_email' );
// }
// if ( ! wp_next_scheduled( 'wpmg_cron_task_parse_email' ) ) {
// wp_schedule_event( time(), 'wpmg_five_minute', 'wpmg_cron_task_parse_email' );
// }
wp_schedule_event( time(), 'wpmg_two_minute', 'wpmg_cron_task_send_email' );
wp_schedule_event( time(), 'wpmg_three_minute', 'wpmg_cron_task_parse_email' );
/*if ( ! wp_next_scheduled( 'wpmg_cron_task_bounced_email' ) ) {
wp_schedule_event( time(), 'wpmg_fifteen_minute', 'wpmg_cron_task_bounced_email' );
}*/
$wpmg_setting = array(
"MG_WEBSITE_URL" => "andres.codes",
"MG_VERSION_NO" => "1.0",
"MG_SUBSCRIPTION_REQUEST_CHECK" => "1",
"MG_SUBSCRIPTION_REQUEST_ALERT_EMAIL" => "e.g. your-mail@example.com",
"MG_BOUNCE_CHECK" => "0",
"MG_BOUNCE_CHECK_ALERT_TIMES" => "2",
"MG_BOUNCE_CHECK_ALERT_EMAIL" => "e.g. your-mail@example.com",
"MG_CUSTOM_STYLESHEET" => "",
"MG_CONTACT_ADDRESS" => "",
"MG_SUPPORT_EMAIL" => "",
"MG_SUPPORT_PHONE" => "1800-123-1234"
);
if ( ! get_option( 'WPMG_SETTINGS' ) ) {
add_option( 'WPMG_SETTINGS', $wpmg_setting );
}
if ( ! get_option( 'wp_mailman_admin_emails' ) ) {
add_option( 'wp_mailman_admin_emails', $objMem->admin_emails );
}
if ( ! get_option( 'wp_mailman_custom_emails' ) ) {
add_option( 'wp_mailman_custom_emails', $objMem->custom_default_emails );
}
}
/* Uninstall Plugin */
//register_uninstall_hook( __FILE__, "wpmg_mailing_group_uninstall" );
//function wpmg_mailing_group_uninstall() {
//
// return true;
//}
register_deactivation_hook( __FILE__, 'wpmg_mailing_group_uninstall' );
function wpmg_mailing_group_uninstall() {
/*Clears if any crons are scheduled with same name, removes them from cache*/
wp_clear_scheduled_hook( 'wpmg_cron_task_send_email' );
wp_clear_scheduled_hook( 'wpmg_cron_task_parse_email' );
wp_clear_scheduled_hook( 'wpmg_cron_task_bounced_email' );
}
/* Initialize menu */
add_action( 'admin_menu', 'wpmg_mailinggroup_Menu' );
function wpmg_mailinggroup_Menu() {
$admin_level = 10;
$user_level = 0;
/* Adding menus */
if ( current_user_can( 'manage_options' ) ) {
add_menu_page( __( 'WP Mailman Options', 'mailing-group-module' ), __( 'WP Mailman Options', 'mailing-group-module' ), $admin_level, 'wpmg_mailinggroup_intro', 'wpmg_mailinggroup_intro' );
add_submenu_page( 'wpmg_mailinggroup_intro', __( 'General Settings', 'mailing-group-module' ), __( 'General Settings', 'mailing-group-module' ), $admin_level, 'wpmg_mailinggroup_intro', 'wpmg_mailinggroup_intro' );
add_submenu_page( 'wpmg_mailinggroup_intro', __( 'Mailing Groups', 'mailing-group-module' ), __( 'Mailing Groups Manager', 'mailing-group-module' ), $admin_level, 'wpmg_mailinggroup_list', 'wpmg_mailinggroup_list' );
add_submenu_page( 'null', __( 'Member Manager', 'mailing-group-module' ), __( 'Member Manager', 'mailing-group-module' ), $admin_level, 'wpmg_mailinggroup_memberlist', 'wpmg_mailinggroup_memberlist' );
add_submenu_page( 'null', __( 'Add Member', 'mailing-group-module' ), __( 'Add Member', 'mailing-group-module' ), $admin_level, 'wpmg_mailinggroup_memberadd', 'wpmg_mailinggroup_memberadd' );
add_submenu_page( 'wpmg_mailinggroup_intro', __( 'Subscription Requests', 'mailing-group-module' ), __( 'Subscription Requests', 'mailing-group-module' ), $admin_level, 'wpmg_mailinggroup_requestmanagerlist', 'wpmg_mailinggroup_requestmanagerlist' );
add_submenu_page( 'wpmg_mailinggroup_intro', __( 'Add Subscribers', 'mailing-group-module' ), __( 'Add Subscribers', 'mailing-group-module' ), $admin_level, 'wpmg_mailinggroup_requestmanageradd', 'wpmg_mailinggroup_requestmanageradd' );
add_submenu_page( 'null', __( 'Add Subscription Request', 'mailing-group-module' ), __( 'Add Subscription Request', 'mailing-group-module' ), $admin_level, 'wpmg_mailinggroup_requestmanageradd', 'wpmg_mailinggroup_requestmanageradd' );
add_submenu_page( 'null', __( 'Send Message', 'mailing-group-module' ), __( 'Send Message', 'mailing-group-module' ), $admin_level, 'wpmg_mailinggroup_sendmessage', 'wpmg_mailinggroup_sendmessage' );
add_submenu_page( 'null', __( 'Messages Manager', 'mailing-group-module' ), __( 'Messages Manager', 'mailing-group-module' ), $admin_level, 'wpmg_mailinggroup_messagelist', 'wpmg_mailinggroup_messagelist' );
add_submenu_page( 'null', __( 'Messages Editor', 'mailing-group-module' ), __( 'Messages Editor', 'mailing-group-module' ), $admin_level, 'wpmg_mailinggroup_adminmessagelist', 'wpmg_mailinggroup_adminmessagelist' );
add_submenu_page( 'null', __( 'Add Message', 'mailing-group-module' ), __( 'Add Message', 'mailing-group-module' ), $admin_level, 'wpmg_mailinggroup_messageadd', 'wpmg_mailinggroup_messageadd' );
add_submenu_page( 'null', __( 'Add Admin Message', 'mailing-group-module' ), __( 'Add Admin Message', 'mailing-group-module' ), $admin_level, 'wpmg_mailinggroup_adminmessageadd', 'wpmg_mailinggroup_adminmessageadd' );
add_submenu_page( 'null', __( 'Style Manager', 'mailing-group-module' ), __( 'Style Manager', 'mailing-group-module' ), $admin_level, 'wpmg_mailinggroup_style', 'wpmg_mailinggroup_style' );
add_submenu_page( 'null', __( 'Contact Info', 'mailing-group-module' ), __( 'Contact Info', 'mailing-group-module' ), $admin_level, 'wpmg_mailinggroup_contact', 'wpmg_mailinggroup_contact' );
add_submenu_page( 'null', __( 'Help', 'mailing-group-module' ), __( 'Help', 'mailing-group-module' ), $admin_level, 'wpmg_mailinggroup_help', 'wpmg_mailinggroup_help' );
add_submenu_page( 'null', __( 'Add Message', 'mailing-group-module' ), __( 'Add Message', 'mailing-group-module' ), $admin_level, 'wpmg_mailinggroup_membergroups', 'wpmg_mailinggroup_membergroups' );
} else {
add_menu_page( __( 'Mailing Groups', 'mailing-group-module' ), __( 'Mailing Groups', 'mailing-group-module' ), $user_level, 'wpmg_mailinggroup_membergroups', 'wpmg_mailinggroup_membergroups' );
}
wp_register_style( 'demo_table.css', plugin_dir_url( __FILE__ ) . 'css/demo_table.css' );
wp_enqueue_style( 'demo_table.css' );
wp_register_script( 'jquery.dataTables.js', plugin_dir_url( __FILE__ ) . 'js/jquery.dataTables.js', array(
'jquery'
) );
wp_enqueue_script( 'jquery.dataTables.js' );
wp_register_script( 'custom.js', plugin_dir_url( __FILE__ ) . 'js/custom.js', array(
'jquery'
) );
wp_enqueue_script( 'custom.js' );
wp_localize_script( 'custom.js', 'PT_Ajax', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nextNonce' => wp_create_nonce( 'myajax-next-nonce' )
)
);
}
/* initialize languae loader */
function wpmg_mailinggroup_generalsettingtab() {
include "template/mg_settingstab.php";
}
/* defining template of all pages used start */
function wpmg_mailinggroup_intro() {
global $wpdb;
include "template/mg_intro_text.php";
}
function wpmg_mailinggroup_help() {
global $wpdb;
include "template/mg_help.php";
}
function wpmg_mailinggroup_style() {
global $wpdb;
include "template/mg_formstyle.php";
}
function wpmg_mailinggroup_contact() {
global $wpdb;
include "template/mg_contact.php";
}
function wpmg_mailinggroup_list() {
include "template/mg_mailinggrouplist.php";
}
function wpmg_mailinggroup_messagelist() {
global $wpdb, $objMem, $table_name_message;
include "template/mg_messagelist.php";
}
function wpmg_mailinggroup_messageadd() {
global $wpdb, $objMem, $table_name_message;
include "template/mg_messageadd.php";
}
function wpmg_mailinggroup_sendmessage() {
global $wpdb, $objMem, $table_name_group, $table_name_message, $table_name_requestmanager;
include "template/mg_sendmessage.php";
}
function wpmg_mailinggroup_requestmanagerlist() {
global $wpdb, $objMem, $table_name_group, $table_name_requestmanager_taxonomy, $table_name_user_taxonomy, $table_name_message, $table_name_requestmanager;
add_action( 'wp_enqueue_script', 'add_thickbox' );
include "template/mg_mailingrequest.php";
}
function wpmg_mailinggroup_requestmanageradd() {
global $wpdb, $objMem, $table_name_requestmanager_taxonomy, $table_name_user_taxonomy, $table_name_group, $table_name_requestmanager;
include "template/mg_mailingrequestadd.php";
}
function wpmg_mailinggroup_memberlist() {
global $wpdb, $objMem, $table_name_requestmanager_taxonomy, $table_name_user_taxonomy, $table_name_sent_emails, $table_name_group;
include "template/mg_memberlist.php";
}
function wpmg_mailinggroup_memberadd() {
global $wpdb, $objMem, $table_name_requestmanager_taxonomy, $table_name_user_taxonomy, $table_name_group;
include "template/mg_memberadd.php";
}
function wpmg_mailinggroup_membergroups() {
global $wpdb, $objMem, $table_name_requestmanager_taxonomy, $table_name_user_taxonomy, $table_name_group;
include "template/mg_membergroups.php";
}
function wpmg_mailinggroup_adminmessagelist() {
global $wpdb, $objMem, $table_name_message;
include "template/mg_adminmessagelist.php";
}
function wpmg_mailinggroup_adminmessageadd() {
global $wpdb, $objMem, $table_name_message;
include "template/mg_adminmessageadd.php";
}
/* defining template of all page s used end */
/* general function */
function wpmg_redirectTo( $page, $end = "admin" ) {
$url = "admin.php?page=" . $page;
if ( $end == 'front' ) {
$url = $_SERVER['REQUEST_URI'] . $page;
}
if ( $end == 'abs' ) {
$url = $page;
}
if ( headers_sent() ) {
?>
<html>
<head>
<script language="javascript" type="text/javascript">
window.self.location = '<?php echo $url; ?>';
</script>
</head>
</html>
<?php
exit;
} else {
header( "Location: " . $url );
exit;
}
}
function wpmg_stringlength( $value, $length = 75 ) {
return substr( $value, 0, $length ) . "...";
}
function wpmg_trimVal( $value, $by = "" ) {
if ( $by == "" ) {
return trim( $value );
} else {
return trim( $value, $by );
}
}
function wpmg_showmessages( $type, $message ) {
echo "<div class='" . $type . "' id='message'><p><strong>Mailing Group Manager: " . $message . "</strong></p></div>";
}
function wpmg_showmessages_ajax( $type, $message ) {
return "<div class='" . $type . "' id='message'><p><strong>Mailing Group Manager: " . $message . "</strong></p></div>";
}
/* general function */
/* ajax requests */
add_action( 'wp_ajax_wpmg_sendmessage', 'wpmg_sendmessage_callback' );
add_action( 'wp_ajax_wpmg_checkusername', 'wpmg_checkusername_callback' );
/* Short codes for ajax requests */
/* callback function for above ajax requests */
function wpmg_sendmessage_callback() {
global $wpdb, $objMem, $table_name_group, $table_name_message;
include "template/mg_sendmessage.php";
die();
}
function wpmg_checkusername_callback() {
$username = sanitize_text_field( $_REQUEST['username'] );
$username_e = username_exists( $username );
$email_e = email_exists( $username );
if ( $username_e || $email_e ) {
$available = "no";
} else {
$available = "yes";
}
echo $available;
wp_die();
}
//User requests fro groups
add_action( 'wp_ajax_wpmg_request_group', 'wpmg_request_group_callback' );
add_action( 'wp_ajax_wpmg_cancel_request', 'wpmg_cancel_request_callback' );
add_action( 'wp_ajax_wpmg_leave_group', 'wpmg_leave_group_callback' );
add_action( 'wp_ajax_wpmg_update_group_format', 'wpmg_update_group_format_callback' );
function wpmg_request_group_callback() {
// check nonce
$nonce = $_POST['nextNonce'];
if ( ! wp_verify_nonce( $nonce, 'myajax-next-nonce' ) ) {
die ( 'Busted!' );
}
$user_id = $_POST['user_id'];
$email = $_POST['email'];
$group_id = $_POST['group_id'];
$group_format = $_POST['group_format'];
$request = array(
'post_title' => $email . '-' . $group_id,
'post_type' => 'mg_requests',
'post_status' => 'publish'
);
$request_id = wp_insert_post( $request );
$new_request = array( 'group_format' => $group_format, 'request_id' => $request_id );
$old_request = get_user_meta( $user_id, 'mg_user_requested_groups', true );
if ( empty( $old_request ) ) {
$requested_groups[ $group_id ] = $new_request;
update_user_meta( $user_id, 'mg_user_requested_groups', $requested_groups );
} else {
$requested_groups = $old_request;
$requested_groups[ $group_id ] = $new_request;
update_user_meta( $user_id, 'mg_user_requested_groups', $requested_groups );
}
add_post_meta( $request_id, 'mg_request_user_id', $user_id, true );
add_post_meta( $request_id, 'mg_request_group_id', $group_id, true );
/*$wp_sent = wp_mail( 'aescobar@isda.org', 'New Subscribtion Request', 'A user has requested to update their subscription' );
if ( $wp_sent ) {
wpmg_showmessages(
"error", __( "Your request is being processed", 'mail
ing-group-module' ) );
exit;
} else {
wpmg_showmessages( "error", __( "Your request is being processed, please confirm with the admin as the email of the request was unable to send", 'mailing-group-module' ) );
}*/
$response = json_encode( $_POST );
// response output
header( "Content-Type: application/json" );
echo $response;
wp_die();
}
function wpmg_cancel_request_callback() {
// check nonce
$nonce = $_POST['nextNonce'];
if ( ! wp_verify_nonce( $nonce, 'myajax-next-nonce' ) ) {
die ( 'Busted!' );
}
$user_id = $_POST['user_id'];
$group_id = $_POST['group_id'];
$request_id = $_POST['request_id'];
$delete_request = wp_delete_post( $request_id, true );
if ( $delete_request ) {
$old_request = get_user_meta( $user_id, 'mg_user_requested_groups', true );
if ( empty( $old_request ) ) {
error_log( print_r( 'does not exist', true ) );
} else {
$requested_groups = $old_request;
unset( $requested_groups[ $group_id ] );
update_user_meta( $user_id, 'mg_user_requested_groups', $requested_groups, $old_request );
}
} else {
error_log( print_r( 'does not exist', true ) );
}
$response = json_encode( $_POST );
// response output
header( "Content-Type: application/json" );
echo $response;
wp_die();
}
function wpmg_leave_group_callback() {
// check nonce
$nonce = $_POST['nextNonce'];
if ( ! wp_verify_nonce( $nonce, 'myajax-next-nonce' ) ) {
die ( 'Busted!' );