-
Notifications
You must be signed in to change notification settings - Fork 0
/
snappy-list-builder.php
2021 lines (1482 loc) · 52 KB
/
snappy-list-builder.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
/*
Plugin Name: Snappy List Builder
Plugin URI: https://developer.wordpress.org/plugins/the-basics/
Description: This is the best plugin to sbcribe
Version: 2.0
Author: Santos
Author URI: https://developer.wordpress.org/
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: wporg
Domain Path: snappy-list-builder
*/
/* 1. HOOK */
/* 1.1 HINT: REGISTER SHORTCODE WITH THE INIT EVENT */
add_action('init', 'slb_register_shortcode');
/* 1.2 HINT: REGISTER CUSTOM ADMIN COLUMNS HEADERS*/
add_filter('manage_edit-slb_subscribers_columns', 'slb_subscriber_column_headers');
add_filter('manage_edit-slb_list_columns', 'slb_list_column_headers');
/* 1.3 hint: register custom admin columns data */
add_filter('manage_slb_subscribers_posts_custom_column', 'slb_subscriber_column_data', 1, 2);
add_filter('manage_slb_list_posts_custom_column', 'slb_list_column_data', 1, 2);
/* 1.4 */
add_action('wp_ajax_nopriv_slb_save_subscription', 'slb_save_subscription');
add_action('wp_ajax_slb_save_subscription', 'slb_save_subscription');
add_action('wp_ajax_slb_download_subscribers_csv', 'slb_download_subscribers_csv');
add_action('wp_ajax_nopriv_slb_unsubscribe', 'slb_unsubscribe');
add_action('wp_ajax_slb_unsubscribe', 'slb_unsubscribe');
/* 1.5 */
add_action('wp_enqueue_scripts' ,'slb_public_scripts');
// 1.6 add ACF filters
add_filter('acf/settings/path', 'slb_acf_settings_path');
add_filter('acf/settings/dir', 'slb_acf_settings_dir');
add_filter('acf/settings/show_admin', 'slb_acf_show_admin');
if ( !defined('ACF_LITE')) define('ACF_LITE', true);
// 1.7
add_action('admin_menu', 'slb_admin_menus');
// 1.10
add_action('admin_init', 'slb_register_options');
// 1.11
// trigger reward download
add_action('wp', 'slb_trigger_reward_downloand');
//1.9
// register active/deactivate/unsistall functions
register_activation_hook( __FILE__, 'slb_activate_plugin' );
// Shortcode
// 2.1 hint : register shortcode
function slb_register_shortcode() {
add_shortcode('slb_form', 'slb_form_shortcode');
add_shortcode('slb_manage_subscriptions', 'slb_manage_subscriptions_shortcode');
add_shortcode('slb_confirm_subscription', 'slb_confirm_subscription_shortcode');
add_shortcode('slb_download_reward', 'slb_download_reward_shortcode');
}
// 2.2
function slb_form_shortcode( $args, $content="" ) {
if ( isset($args['id']) ) $list_id = (int)$args['id'];
$title = '';
if ( isset($args['title']) ) $title = (string)$args['title'];
$output = '
<form id="slb-form" class="slb-form" method="POST" action="/BootstrapToWordpress/wp-admin/admin-ajax.php?action=slb_save_subscription">
<input type="hidden" name="slb_list" value"'. $list_id . '">';
if (strlen($title)):
$output .= '<h3 class="title">' . $title . '</h3>';
endif;
$output .= '<p class="slb-input-container">
<label class="slb-label">Your Name</label>
<input type="text" name="slb_fname" class="slb-input" placeholder="First Name" />
<input type="text" name="slb_lname" class="slb-input mt-10" placeholder="Last Name" />
</p>
<p>
<label class="slb-label">Your Email</label>
<input type="email" name="slb_email" class="slb-input" placeholder="Email" />
</p>';
if( strlen( $content ) ) :
$output .= '<div class="slb-form-content">' . wpautop($content) . '</div>';
endif;
if ( strlen( $content ) ):
$output .= '<div class="slb_content">'. wpautop($content) .'</div>';
endif;
// get reward
$reward = slb_get_list_reward( $list_id );
// If reward exists
if ( $reward !== false ):
// include message about reward
$output .= '
<div class="slb-content slb-reward-message">
<p>Get a FREE DOWNLOAD of <strong>'. $reward['title'] .'</strong> when you join this list!</p>
</div>
';
endif;
$output .= '
<p class="slb-input-container text-right">
<input type="submit" class="slb-form-submit p-10 bg-success" value="send me up!" />
</p>
</form>';
return $output;
}
// 2.3
// Hint : displays a form for managing the users list subscriptions
// example: [slb_manage_subscriptions]
function slb_manage_subscriptions_shortcode( $args, $content="" ) {
$output = '<div class="slb slb-manage-subscriptions">';
try {
// get the email address from the URL
$email = (isset($_GET['email'] ) ) ? esc_attr($_GET['email']) : '' ;
// get the subscriber id from the email address
$subscriber_id = slb_get_subscriber_id( $email );
// get subscriber data
$subscriber_data = slb_get_subscriber_data($subscriber_id);
// IF subscriber exists
if ( $subscriber_id ):
// get subscription html
$output = slb_get_manage_subscriptions_html($subscriber_id);
else:
// invalid link
$output .= '<p>This link is invalid.</p>';
endif;
} catch (Exception $e) {
// php Error
}
$output .= '</div>';
// return our html
return $output;
}
// 2.4
// hint: displays subscription opt-in confirmation text and link to manage sunscriptions
// example: [slb_confirm_subscription]
function slb_confirm_subscription_shortcode( $args, $content="" ) {
// setup output variable
$output = '<div class="slb">';
// setup email and list_id variable and handle of they are not defined in the GET scope
$email = (isset( $_GET['email'] ) ) ? esc_attr( $_GET['email'] ) : '';
$list_id = ( isset( $_GET['list'] ) ) ? esc_attr( $_GET['list'] ) : 0;
// get subscriber id from email
$subscriber_id = slb_get_subscriber_id( $email );
$subscriber = get_post( $subscriber_id );
// If we found a subscriber matching that email address
if ( $subscriber_id && slb_validate_subscriber( $subscriber ) ):
// get list abject
$list = get_post( $list_id );
// If list and subscriber are valid
if ( slb_validate_list( $list ) ):
if ( !slb_subscriber_has_subscription( $subscriber_id, $list_id ) ):
// Complete opt-in
$option_complete = slb_comfirm_subscription( $subscriber_id, $list_id );
if ( !$option_complete ):
$output .= slb_get_message_html('Due to an unknown error, we were anable to comfirm yout subscription.', 'error');
$output .= '</div>';
return $output;
endif;
endif;
// get confirmation message html and append it to output
$output .= slb_get_message_html('Your subscripton to '. $list->post_title. ' has now been corfirmed.', 'confirmation');
// get manage subscription link
$manage_subscriptions_link = slb_get_manage_subscriptions_link( $email );
// append link output
$output .= '<p><a href="' . $manage_subscriptions_link . '">Click here to manage your subscriptions.</a></p>';
else:
$output .= slb_get_message_html('This link is invalid.', 'error');
endif;
else:
$output .= slb_get_message_html('This link is invalid.<br> Invalid Subscriber ' . $email . '.', 'error');
endif;
// close .slb div
$output .= '</div>';
// return output html;
return $output;
}
// 2.5
// [slb_download_reward]
// hint: returns a message if the download link has expired or is invalid
function slb_download_reward_shortcode($args, $content="") {
$output = '';
$uid = ($_GET['reward']) ? (string)$_GET['reward'] : 0;
// get reward form link uid
$reward = slb_get_reward( $uid );
// if reward was found
if ( $reward !== false ):
if ( $reward['downloads'] >= slb_get_option('slb_download_limit') ):
$output .= slb_get_message_html('This link has reached it\'s download limit.', 'warning' );
endif;
else:
$output .= slb_get_message_html('This link is invalid.', 'error');
endif;
return $output;
}
/* !3 FILTER */
/* 3.1 hint: */
function slb_subscriber_column_headers( $columns ) {
// creating custom columns header data
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => __('Subscriber Name'),
'name' => __('Name'),
'email' => __('Email Address')
);
// returning new columns
return $columns;
}
// 3.2
function slb_subscriber_column_data( $columns, $post_id ) {
// setup our output variable
$output = '';
switch ($columns) {
case 'name':
$fname = get_field( 'slb_fname', $post_id );
$lname = get_field( 'slb_lname', $post_id );
$output .= $fname . ' ' . $lname;
break;
case 'email':
$email = get_field( 'slb_email', $post_id );
$output .= $email;
break;
}
// echo the output
echo $output;
}
// 3.3
function slb_list_column_headers( $columns ) {
// creating custom columns header data
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => __('Subscriber Name'),
'reward' => __('Opt-In Reward'),
'subscribers' => __('Subscribers'),
'shortcode' => __('Shortcode')
);
// returning new columns
return $columns;
}
// 3.4
function slb_list_column_data( $columns, $post_id ) {
// setup our output variable
$output = '';
switch ($columns) {
case 'reward':
$reward = slb_get_list_reward($post_id);
if ($reward !== false) :
$output .= '<a href="' . $reward['file']['url'] .'" download="' . $reward['title'] . '">' . $reward['title'] . '</a>';
endif;
break;
case 'subscribers':
// get the count of current subscribers
$subscriber_count = slb_get_list_subscriber_count( $post_id );
// get our unique export link
$export_href = slb_get_export_link( $post_id );
// append the subcriber count to our output
$output .= $subscriber_count;
// if we have more than one subscriber, add our new export link to $output
if ( $subscriber_count ) $output .= '<a href="'. $export_href .'"></a>';
break; // endchanges5
case 'shortcode':
$output .= '[slb_form id="'. $post_id . '"][/slb_form]';
break;
}
// echo the output
echo $output;
}
// 3.5
// hint registers plugins costom admiin menun
function slb_admin_menus() {
/*admin menus*/
$top_menu_item = 'slb_dashboard_admin_page';
add_menu_page('','List Builder', 'manage_options', 'slb_dashboard_admin_page' , 'slb_dashboard_admin_page', 'dashicons-email-alt');
/*subsmenu item*/
// dashboard
add_submenu_page($top_menu_item, '', 'Dashboard', 'manage_options', $top_menu_item, $top_menu_item);
// Email list
add_submenu_page($top_menu_item, '', 'Email List', 'manage_options', 'edit.php?post_type=slb_list');
// ssubscribers
add_submenu_page($top_menu_item, '', 'Subscribers', 'manage_options', 'edit.php?post_type=slb_subscribers');
// import subscriptions
add_submenu_page($top_menu_item, '', 'Import Subscribers', 'manage_options', 'slb_import_admin_page', 'slb_import_admin_page');
// plugins options
add_submenu_page($top_menu_item, '' , 'Plugin Options', 'manage_options', 'slb_options_admin_page', 'slb_options_admin_page');
}
// EXTERNAL SCRIPTS
// 4.1
// include file of acf
require_once(plugin_dir_path(__FILE__) . 'libs/advanced-custom-fields/acf.php');
// 4.2
// hint the script of the page
function slb_public_scripts() {
wp_register_script('snappy-list-builder-js-public', plugins_url('/js/public/snappy-list-builder.js', __FILE__, array('jquery'), '', true));
wp_register_style('snappy-list-builder-css-public', plugins_url('/css/public/snappy-list-builder.css', __FILE__, array('jquery')));
wp_enqueue_script('snappy-list-builder-js-public');
wp_enqueue_style('snappy-list-builder-css-public');
}
// ACTIONS
//5.1
function slb_save_subscription() {
// set all the result data
$result = array(
'status' => 0,
'message' => 'Subscription was not save. ',
'error' => '',
'errors' => ''
);
try {
// get list id
$list_id = (int)$_POST['slb_list'];
// prepare subscriber data
$subscriber_data = array(
'fname' => esc_attr($_POST['slb_fname']),
'lname' => esc_attr($_POST['slb_lname']),
'email' => esc_attr($_POST['slb_email'])
);
$errors = array();
if ( !strlen($subscriber_data['fname'] ) ) $errors['fname'] = 'First name is required.';
if ( !strlen($subscriber_data['email'] ) ) $errors['email'] = 'Email is required.';
if ( strlen($subscriber_data['email'] ) && !is_email($subscriber_data['email']) ) $errors['email'] = 'First name is required.';
// If the are errors
if (count($errors)) :
// Append errors to result structure for later use
$result['error'] = "Some fields are still required";
$result['errors'] = $errors;
else:
// attempt to create / dave subscribe
$subscriber_id = slb_save_subscriber( $subscriber_data );
// if subscriber was save successfully $subscriber_id will be greater than 0
if ( $subscriber_id ):
// if subscriber already has this subscription
if ( slb_subscriber_has_subscription( $subscriber_id, $list_id ) ) :
// get list object
$list = get_post($list_id);
// return the detailerd error
$result['message'] .= esc_attr($subscriber_data['email'] . 'Is alredy subscribed to' . $list->post_title . '.' );
else:
// save new subscription
// $subscription_save = slb_add_subscription($subscriber_id, $list_id);
// if subscription was save succssefully
// if ($subscription_save) :
// subscripton save
// $result['status'] = 1;
// $result['message'] = 'Subscription save';
// send new subscriber a confirmation email, returns true if we were successfu
$email_sent = slb_send_subscriber_email( $subscriber_id, 'new_subscription', $list_id);
// IF email was send
if ( !$email_sent ):
// email could not be send
$result['error'] = 'Unable to send email.';
else:
// email send and subscription saved
$result['status'] = 1;
$result['message'] = 'Success! A confirmation email has been sent to ' . $subscriber_data['email'];
unset( $result['error'] );
endif;
// else:
// $result['error'] = 'Unable To Save Subscriptions.';
// endif;
endif;
endif;
endif;
} catch (Exception $e) {
}
// // return result jason
slb_return_json($result);
}
// 5.2
function slb_save_subscriber( $subscriber_data ) {
// setup default subscriber id
// 0 means that subscriber was not save
$subscriber_id = 0;
try {
$subscriber_id = slb_get_subscriber_id($subscriber_data['email']);
// if the subscriber does not already exists
if ( !$subscriber_id ) :
$subscriber_id = wp_insert_post(array(
'post_type' => 'slb_subscribers',
'post_title' => $subscriber_data['fname'] . ' ' . $subscriber_data['lname'],
'post_status' => 'publish'
),
true);
endif;
// add/update custom meta data
update_field(slb_get_acf_key('slb_fname'), $subscriber_data['fname'], $subscriber_id);
update_field(slb_get_acf_key('slb_lname'), $subscriber_data['lname'], $subscriber_id);
update_field(slb_get_acf_key('slb_email'), $subscriber_data['email'], $subscriber_id);
} catch (Exception $e) {
// php Error
}
// return the subscriber id
return $subscriber_id;
}
// 5.3
function slb_add_subscription( $subscriber_id, $list_id ) {
$subscription_saved = false;
// if the subscriber does not
if ( !slb_subscriber_has_subscription($subscriber_id, $list_id) ):
// get_subscriptions and append new $list_id
$subscriptions = slb_get_subscriptions($subscriber_id);
$subscriptions[] = $list_id;
// update slb_subscriptions
update_field(slb_get_acf_key('slb_subscription'), $subscriptions, $subscriber_id);
// return value
$subscription_saved = true;
endif;
return $subscription_saved;
}
// 5.4
// hint: removes one or more subscriptions from a subscriber and notifies them via email
// this function is a ajax form handler...
// expect form post data: $_POST['subscriber_id'] and $_POST['list_id']
function slb_unsubscribe() {
// setup default result data
$result = array(
'status' => 0,
'message' => 'Subscriptions were NOT updated. ',
'error' => '',
'errors' => array(),
);
$subscriber_id = ( isset($_POST['subscriber_id']) ) ? (int)$_POST['subscriber_id'] : 0;
$list_ids = ( isset($_POST['list_ids']) ) ? $_POST['list_ids'] : 0;
try {
// validate nounce
if ( check_ajax_referer( 'slb-update-subscriptions_' . $subscriber_id ) ) :
// if there are lists to remove
if( is_array($list_ids) ):
// loop over lists to remove
foreach ($list_ids as $list_id ):
// remove this subscription
slb_remove_subscription( $subscriber_id, $list_id );
endforeach;
endif;
// setup success status and message
$result['status'] = 1;
$result['message'] = 'Subscriptions updated. ';
// get the updated list of subscriptions as html
$result['html'] = slb_get_manage_subscriptions_html( $subscriber_id );
endif;
} catch(Exception $e) {
// PHP Error
}
slb_return_json( $result );
}
// 5.5
// hint: removes a single subscription from a subscriber
function slb_remove_subscription( $subscriber_id, $list_id ) {
// setup defalt return value
$subscription_saved = false;
// If the subscriber has the current list subscription
if( slb_subscriber_has_subscription( $subscriber_id, $list_id ) ):
// get current subscriptions
$subscriptions = slb_get_subscriptions( $subscriber_id );
// get the position of the $list_id to remove
$needle = array_search( $list_id, $subscriptions );
// remoce $list_id from $subscriptions array
unset( $subscriptions[$needle] );
// update slb_subscriptions
update_field(slb_get_acf_key('slb_subscription'), $subscriptions, $subscriber_id );
// subscriptions updated!
$subscription_saved = true;
endif;
// return result
return $subscription_saved;
}
// 5.6
// hint: sends a unquie customized email to a subscriber
function slb_send_subscriber_email( $subscriber_id, $email_template_name, $list_id) {
// setup return varialble
$email_sent = false;
// get email template data
$email_template_object = slb_get_email_template( $subscriber_id, $email_template_name, $list_id );
if ( !empty( $email_template_object ) ):
// get subscriber data
$subscriber_data = slb_get_subscriber_data( $subscriber_id );
// set WP_mail headers
$wp_mail_headers = array('Content-type: text/html; charset=UTF-8');
$mail_sent = wp_mail(array($subscriber_data['email']) , $email_template_object['subject'], $email_template_object['body'], $wp_mail_headers);
endif;
return $semail_sent;
}
// 5.7
// hint: adds subscription to database and email subscriber confirmation email
function slb_comfirm_subscription( $subscriber_id, $list_id ) {
// setup return variable
$option_complete = false;
// add new subscriptoin
$subscription_saved = slb_add_subscription( $subscriber_id, $list_id );
// IF subscription was saved
if ($subscription_saved):
// send email
// $email_sent = slb_send_subscriber_email( $subscriber_id, 'subscription_confirmed', $list_id );
// if email sent
// if ($email_sent):
// return true
$option_complete = true;
// endif;
endif;
// return result
return $option_complete;
}
// 5.8
// hint: creates custom table for our plugin
function slb_create_pliguin_tables() {
global $wpdb;
// setup return value
$return_value = false;
try {
$table_name = $wpdb->prefix . "slb_reward_links";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (id mediumint(11) NOT NULL AUTO_INCREMENT,
uid varchar(128) NOT NULL, subscriber_id mediumint(11) NOT NULL,
list_id mediumint(11) NOT NULL, attachment_id mediumint(11) NOT NULL,
downloads mediumint(11) DEFAULT 0 NOT NULL, UNIQUE KEY id (id) ) $charset_collate;";
// make sure we include wordpress functions for dbDelta
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
// dbDelta will create a new table if none exists or update an existing one
dbDelta($sql);
// return true
$return_value = true;
} catch (Exception $e) {
// PHP error
}
return $return_value;
}
// 5.9
// hint: runs on plugin activation
function slb_activate_plugin() {
//setup custom database table
slb_create_pliguin_tables();
}
// 5.10
// hint: adds new reward links to the database
function slb_add_reward_link( $uid, $subscriber_id, $list_id, $attachment_id ) {
global $wpdb;
//set our return value
$return_value = false;
try {
$table_name = $wpdb->prefix . "slb_reward_links";
$wpdb->insert(
$table_name, array(
'uid' => $uid,
'subscriber_id' => $subscriber_id,
'list_id' => $list_id,
'attachment_id' => $attachment_id
),
array(
'%s',
'$d',
'$d',
'%d'
)
);
// return value
$return_value = true;
} catch (Exception $e) {
}
// return result
return $return_value;
}
// 5.11
// hint: trigger a download of the reward file
function slb_trigger_reward_downloand() {
global $post;
if ( $post->ID == slb_get_option('slb_reward_page_id') && isset($_GET['reward']) ):
$uid = ($_GET['reward']) ? (string)$_GET['reward'] : 0;
// get reward form link uid
$reward = slb_get_reward( $uid );
// if reward was found
if ( $reward !== false && $reward['download'] < slb_get_option( 'slb_download_limit' ) ):
slb_update_reward_link_download( $uid );
// get the reward mimetype
$mimetype = $reward['file']['mine_type'];
// extract tge filetype from the mimetype
$mimetype_array = explode('/', $mimetype);
$filetype = $mimetype_array[1];
// setup file headers
header("Content-type:".$mimetype, true,200);
header("Content-Dispoisition: attachment; filename=". $reward['title'] . '.' . $filetype);
header("Progma: no-cache");
header("Expires: 0");
readfile($reward['file']['url']);
exit();
endif;
endif;
}
// 5.12
// hint: incrases reward link download count by one
function slb_update_reward_link_downloads() {
global $wpdb;
// setup our return value
$return_value = false;
try {
$table_name = $wpdb->prefix . "slb_reward_links";
// get current donwload count
$current_count = $wpdb->get_var( $wpdb->prepare("SELECT downloads FROM $table_name WHERE uid = %s", $uid) );
// set new count
$new_count = (int)$current_count+1;
// update downloads for this reward link entry
$wpdb->query( $wpdb->prepare("UPDATE $table_name SET downloads = $new_count WHERE uid = %s", $uid) );
$return_value = true;
} catch (Exception $e) {
// PHP Error
}
return $return_value;
}
// 6 HELPER
// 6.1
function slb_subscriber_has_subscription($subscriber_id, $list_id) {
// setup default return value
$has_subscription = false;
// Get subscriber
$subscriber = get_post($subscriber_id);
// get subscription
$subscription = slb_get_subscriptions($subscriber_id);
if (in_array($list_id, $subscription)):
// find the $list_id in $subscription
// The subscriber is already subscriber to this list
$has_subscription = true;
else:
// nothing
endif;
return $has_subscription;
}
// 6.2
function slb_get_subscriber_id( $email ) {
// default value
$subscriber_id = 0;
try {
// check if subscriber alreay exists
$subscriber_query = new WP_Query(array(
'post_type' => 'slb_subscribers',
'posts_per_page' => 1,
'meta_key' => 'slb_email',
'meta_query' => array(
array(
'key' => 'slb_email',
'value' => $email,
'compare' => '='
),
),
));
// if the subscriber exists...
if ($subscriber_query->have_posts() ):
// get subscriber_id
$subscriber_query->the_post();
$subscriber_id = get_the_ID();
endif;
} catch (Exception $e) {
// PHP Error
}
// reset the query
wp_reset_query();
// return the value of subscrver_idas an integer
return (int)$subscriber_id;
}
// 6.3
// hint: return an array of lists objects)
function slb_get_subscriptions($subscriber_id) {
// default $subscription value
$subscription = array();
// get subscription (returns an arrya of list object)
$lists = get_field(slb_get_acf_key('slb_subscription'), $subscriber_id );
// if $list returnsomethin
if ($lists):
// if $lists is an array and there is no one or more items
if (is_array($lists) && count($lists) ):
// build subscription: array of lists id's
foreach ($lists as $list):
$subscription[] = (int)$list->ID;
endforeach;
elseif(is_numeric($lists) ):
// sisngle result returned
$subscription = $lists;
endif;
endif;
return (array)$subscription;
}
// 6.4
function slb_return_json( $php_array ) {
$slb_return = json_encode( $php_array );
die($slb_return);
exit;
}
// 6.5
function slb_get_acf_key( $field_name ) {
$field_key = $field_name;
switch ($field_name) {
case 'slb_fname':
$field_key = 'field_5b5b3a0dd11e9';
break;
case 'slb_lname':
$field_key = 'field_5b5b3af0d11eb';
break;
case 'slb_email':
$field_key = 'field_5b5b3b23d11ec';
break;
case 'slb_subscription':
$field_key = 'field_5b5b3a91d11ea';
break;
case 'slb_enable_reward':
$field_key = 'field_5b86baf8f4007';
break;
case 'slb_reward_title':
$field_key = 'field_5b86bba6f4008';
break;
case 'slb_reward_file':
$field_key = 'field_5b86bc22f4009';
break;
}
return $field_key;
}
// 6.6
// return an array of subscriber data include subscriptions
function slb_get_subscriber_data( $subscriber_id ) {
$subscriber_data = array();
$subscriber = get_post($subscriber_id);
// le falta la ese por si me vieve a dar error en slb_subcriber
if ( isset($subscriber->post_type) && $subscriber->post_type == 'slb_subscribers' ):
$fname = get_field(slb_get_acf_key('slb_fname'), $subscriber_id);
$lname = get_field(slb_get_acf_key('slb_lname'), $subscriber_id);
$email = get_field(slb_get_acf_key('slb_email'), $subscriber_id);
$subscriber_data = array(
'name' => $fname . ' ' . $lname,
'fname' => $fname,
'lname' => $lname,
'email' => $email,
'subscriptions' => slb_get_subscriptions( $subscriber_id )
);
endif;
// RETURN SUBSCRIBER_DATA
return $subscriber_data;