· 7 years ago · Nov 21, 2018, 12:06 PM
1<?php
2/*
3Plugin Name:Pet Ageing Transfer Plugin
4Plugin URI: http://www.petsonthewall.com
5Description: Transfer pets age to human readable age
6Version: 1.0
7Author: Hugh Ben
8License: GPL2
9*/
10
11//HOOKS
12//register all our custom shortcodes on init
13add_action('init','slb_register_shortcodes');
14
15//register custom admin column headers
16add_filter('manage_edit-slb_subscriber_columns','slb_subscriber_column_headers');
17add_filter('manage_edit-slb_list_columns','slb_list_column_headers');
18
19
20//register custom admin column data
21add_filter('manage_slb_subscriber_posts_custom_colum','slb_subscriber_column_data',1,2);
22add_action('admin_head-edit.php','slb_register_custom_admin_titles');
23
24add_filter('manage_slb_list_posts_custom_colum','slb_list_column_data',1,2);
25
26add_action('wp_ajax_nopriv_slb_save_subscription','slb_save_subscription');//regular website visitor
27add_action('wp_ajax_slb_save_subscription','slb_save_subscription');//admin user
28add_action('wp_ajax_nopriv_slb_unsubscribe', 'slb_unsubscribe'); // regular website visitor
29add_action('wp_ajax_slb_unsubscribe', 'slb_unsubscribe'); // admin user
30//load external files to public website
31add_action('wp_enqueue_scripts','slb_public_scripts');
32
33
34//register custom menus
35add_action('admin_menu','slb_admin_menus');
36
37//load external files in wordpress admin
38add_action('admin_enqueue_scripts','slb_admin_scripts');
39
40// register plugin options
41add_action('admin_init', 'slb_register_options');
42//register all custom shortcodes
43function slb_register_shortcodes(){
44 add_shortcode('slb_form','slb_form_shortcode');
45 add_shortcode('slb_manage_subscriptions','slb_manage_subscriptions_shortcode');
46}
47
48function slb_form_shortcode($args, $content=""){
49
50 //get the list id
51 $list_id = 0;
52 if(isset($args['id'])) $list_id = (int)$args['id'];
53
54 $title = '';
55 if(isset($args['title'])) $title = (string)$args['title'];
56 //setup our output variable - the form html
57 $output = '
58 <div class="slb">
59 <form id="slb_register_form" name="slb_register_form" class="slb-register-form" method="post"
60 action="/wp-admin/admin-ajax.php?action=slb_save_subscription" method="post">
61
62 <input type="hidden" name="slb_list" value="'. $list_id .'">';
63
64 if(strlen($title)):
65 $output .= '<h3 class="slb-title">' .$title .'</h3>';
66 endif;
67 $output .= '<p class="slb-input-container">
68 <lable>Your Name</label><br />
69 <input type="text" name="slb_fname" placeholder="First Name" />
70 <input type="text" name="slb_lname" placeholder="Last Name" />
71 </p>
72
73 <p class="slb-input-container">
74 <lable>Your Email</label><br />
75 <input type="email" name="slb_email" placeholder="example@email.com" />
76 </p>';
77 if(strlen($content)):
78 $output .= '<div class="slb-content">'. wpautop($content) .'</div>';
79 endif;
80
81 $output .='<p class="slb-input-container">
82 <input type="submit" name="slb_submit" value="Sign Me Up" />
83 </p>
84 </form>
85
86 </div>
87 ';
88
89 return $output;
90}
91
92// hint: displays a form for managing the users list subscriptions
93// example: [slb_manage_subscriptions]
94function slb_manage_subscriptions_shortcode( $args, $content="" ) {
95
96 // setup our return string
97 $output = '<div class="slb slb-manage-subscriptions">';
98
99 try {
100
101 // get the email address from the URL
102 $email = ( isset( $_GET['email'] ) ) ? esc_attr( $_GET['email'] ) : '';
103
104 // get the subscriber id from the email address
105 $subscriber_id = slb_get_subscriber_id( $email );
106
107 // get subscriber data
108 $subscriber_data = slb_get_subscriber_data( $subscriber_id );
109
110 // IF subscriber exists
111 if( $subscriber_id ):
112
113 // get subscriptions html
114 $output .= slb_get_manage_subscriptions_html( $subscriber_id );
115
116 else:
117
118 // invalid link
119 $output .= '<p>This link is invalid.</p>';
120
121 endif;
122
123
124 } catch(Exception $e) {
125
126 // php error
127
128 }
129
130 // close our html div tag
131 $output .= '</div>';
132
133 // return our html
134 return $output;
135
136}
137
138
139//include acf
140include_once(plugin_dir_path(__FILE__) .'lib/advanced-custom-fields/acf.php');
141
142function slb_admin_scripts(){
143 wp_register_script('petPlugin-js-private',plugins_url('/js/private/petPlugin.js',__FILE__),array('jquery'),'',true);
144 wp_enqueue_script('petPlugin-js-private');
145}
146
147function slb_public_scripts(){
148 //register scripts with wordpress internal library
149 wp_register_script('petPlugin-js-public',plugins_url('/js/public/petPlugin.js',__FILE__),array('jquery'),'',true);
150
151 wp_register_style('petPlugin-css-public',plugins_url('/css/public/petPlugin.css',__FILE__));
152 //add to que of scripts that get loaded into every page
153 wp_enqueue_script('petPlugin-js-public');
154 wp_enqueue_style('petPlugin-css-public');
155}
156
157//FILTERS
158function slb_subscriber_column_headers($columns){
159 //create custom column header data
160 $columns = array(
161 'cb' => '<input type="checkbox" />',
162 'title' => __('Subscriber Name'),
163 'email' => __('Email Address'),
164 );
165
166 return $columns;
167}
168
169function slb_subscriber_column_data($column, $post_id){
170
171 //setup our return text
172 $output='';
173 switch ($column) {
174 case 'title':
175 $fname= get_field('slb_fname',$post_id);
176 $lname= get_field('slb_lname',$post_id);
177 $output .= $fname. ' '. $lname;
178 break;
179 case 'email':
180 $email = get_field('slb_email',$post_id);
181 $output = $email;
182 break;
183 }
184
185 echo $output;
186}
187
188//register special custom admin title columns
189function slb_register_custom_admin_titles(){
190 add_filter(
191 'the_title',
192 'slb_custom_admin_titles',
193 99,
194 2
195 );
196}
197
198//handles custom admin title 'title' columndata for post types without titles
199function slb_custom_admin_titles($title,$post_id){
200 global $post;
201 $output = $title;
202
203 if( isset($post->post_type)):
204 switch ($post->post_type) {
205 case 'slb_subscriber':
206 $fname = get_field('slb_fname',$post_id);
207 $lname = get_field('slb_lname',$post_id);
208 $output = $fname .' '. $lname;
209 break;
210 }
211 endif;
212
213 return $output;
214}
215
216
217function slb_list_column_headers($columns){
218 //create custom column header data
219 $columns = array(
220 'cb' => '<input type="checkbox" />',
221 'title' => __('List Name'),
222 'shorcode' => __('Shortcode'),
223 );
224
225 return $columns;
226}
227
228
229function slb_list_column_data($column, $post_id){
230
231 //setup our return text
232 $output='';
233 switch ($column) {
234 case 'shorcode':
235 $output .='[slb_form id="'. $post_id .'"]';
236 break;
237 }
238
239 echo $output;
240}
241
242function slb_save_subscription(){
243
244 // setup default result data
245 $result = array(
246 'status' => 0,
247 'message' => 'Subscription was not saved. ',
248 'error'=>'',
249 'errors'=>array()
250 );
251
252 try {
253
254 // get list_id
255 $list_id = (int)$_POST['slb_list'];
256
257 // prepare subscriber data
258 $subscriber_data = array(
259 'fname'=> esc_attr( $_POST['slb_fname'] ),
260 'lname'=> esc_attr( $_POST['slb_lname'] ),
261 'email'=> esc_attr( $_POST['slb_email'] ),
262 );
263
264 // setup our errors array
265 $errors = array();
266
267 // form validation
268 if( !strlen( $subscriber_data['fname'] ) ) $errors['fname'] = 'First name is required.';
269 if( !strlen( $subscriber_data['email'] ) ) $errors['email'] = 'Email address is required.';
270 if( strlen( $subscriber_data['email'] ) && !is_email( $subscriber_data['email'] ) ) $errors['email'] = 'Email address must be valid.';
271
272 // IF there are errors
273 if( count($errors) ):
274
275 // append errors to result structure for later use
276 $result['error'] = 'Some fields are still required. ';
277 $result['errors'] = $errors;
278
279 else:
280 // IF there are no errors, proceed...
281
282 // attempt to create/save subscriber
283 $subscriber_id = slb_save_subscriber( $subscriber_data );
284
285 // IF subscriber was saved successfully $subscriber_id will be greater than 0
286 if( $subscriber_id ):
287
288 // IF subscriber already has this subscription
289 if( slb_subscriber_has_subscription( $subscriber_id, $list_id ) ):
290
291 // get list object
292 $list = get_post( $list_id );
293
294 // return detailed error
295 $result['error'] = esc_attr( $subscriber_data['email'] .' is already subscribed to '. $list->post_title .'.');
296
297 else:
298
299 // save new subscription
300 $subscription_saved = slb_add_subscription( $subscriber_id, $list_id );
301
302 // IF subscription was saved successfully
303 if( $subscription_saved ):
304
305 // send new subscriber a confirmation email, returns true if we were successful
306 $email_sent = slb_send_subscriber_email( $subscriber_id, 'new_subscription', $list_id);
307
308 // IF email was sent
309 if( !$email_sent ):
310
311 // remove subscription
312 slb_remove_subscription( $subscriber_id, $list_id);
313
314 // email could not be sent
315 $result['error'] = 'Unable to send email. ';
316
317 else:
318
319 // email sent and subscription saved!
320 $result['status']=1;
321 $result['message']='Success! A confirmation email has been sent to '. $subscriber_data['email'];
322
323 // clean up: remove our empty error
324 unset( $result['error'] );
325
326 endif;
327 // $result['status']=1;
328 // $result['message']='Success! Saved successfully ';
329
330 else:
331
332 // return detailed error
333 $result['error'] = 'Unable to save subscription.';
334
335
336 endif;
337
338 endif;
339
340 endif;
341
342 endif;
343
344 } catch ( Exception $e ) {
345
346 }
347
348 // return result as json
349 slb_return_json($result);
350}
351
352function slb_save_subscriber($subscriber_data){
353
354 // setup default subscriber id
355 // 0 means the subscriber was not saved
356 $subscriber_id = 0;
357
358 try {
359
360 $subscriber_id = slb_get_subscriber_id( $subscriber_data['email'] );
361
362 // IF the subscriber does not already exists...
363 if( !$subscriber_id ):
364
365 // add new subscriber to database
366 $subscriber_id = wp_insert_post(
367 array(
368 'post_type'=>'slb_subscriber',
369 'post_title'=>$subscriber_data['fname'] .' '. $subscriber_data['lname'],
370 'post_status'=>'publish',
371 ),
372 true
373 );
374
375 endif;
376
377 // add/update custom meta data
378 update_field(slb_get_acf_key('slb_fname'), $subscriber_data['fname'], $subscriber_id);
379 update_field(slb_get_acf_key('slb_lname'), $subscriber_data['lname'], $subscriber_id);
380 update_field(slb_get_acf_key('slb_email'), $subscriber_data['email'], $subscriber_id);
381
382 } catch( Exception $e ) {
383
384 // a php error occurred
385
386 }
387
388 // return subscriber_id
389 return $subscriber_id;
390}
391
392function slb_add_subscription($subscriber_id, $list_id){
393
394 // setup default return value
395 $subscription_saved = false;
396
397 // IF the subscriber does NOT have the current list subscription
398 if( !slb_subscriber_has_subscription( $subscriber_id, $list_id ) ):
399
400 // get subscriptions and append new $list_id
401 $subscriptions = slb_get_subscriptions( $subscriber_id );
402 $subscriptions[]=$list_id;
403
404 // update slb_subscriptions
405 update_field( slb_get_acf_key('slb_subscriptions'), $subscriptions, $subscriber_id );
406
407 // subscriptions updated!
408 $subscription_saved = true;
409
410 endif;
411
412 // return result
413 return $subscription_saved;
414}
415
416
417
418
419// hint: validates whether the post object exists and that it's a validate post_type
420function slb_validate_list( $list_object ) {
421
422 $list_valid = false;
423
424 if( isset($list_object->post_type) && $list_object->post_type == 'slb_list' ):
425
426 $list_valid = true;
427
428 endif;
429
430 return $list_valid;
431
432}
433
434
435// hint: validates whether the post object exists and that it's a validate post_type
436function slb_validate_subscriber( $subscriber_object ) {
437
438 $subscriber_valid = false;
439
440 if( isset($subscriber_object->post_type) && $subscriber_object->post_type == 'slb_subscriber' ):
441
442 $subscriber_valid = true;
443
444 endif;
445
446 return $subscriber_valid;
447
448}
449
450// hint: removes one or more subscriptions from a subscriber and notifies them via email
451// this function is a ajax form handler...
452// expects form post data: $_POST['subscriber_id'] and $_POST['list_id']
453function slb_unsubscribe() {
454
455 // setup default result data
456 $result = array(
457 'status' => 0,
458 'message' => 'Subscriptions were NOT updated. ',
459 'error' => '',
460 'errors' => array(),
461 );
462
463 $subscriber_id = ( isset($_POST['subscriber_id']) ) ? esc_attr( (int)$_POST['subscriber_id'] ) : 0;
464 $list_ids = ( isset($_POST['list_ids']) ) ? $_POST['list_ids'] : 0;
465
466 try {
467
468 // if there are lists to remove
469 if( is_array($list_ids) ):
470
471 // loop over lists to remove
472 foreach( $list_ids as &$list_id ):
473
474 // remove this subscription
475 slb_remove_subscription( $subscriber_id, $list_id );
476
477 endforeach;
478
479 endif;
480
481 // setup success status and message
482 $result['status']=1;
483 $result['message']='Subscriptions updated. ';
484
485 // get the updated list of subscriptions as html
486 $result['html']= slb_get_manage_subscriptions_html( $subscriber_id );
487
488 } catch( Exception $e ) {
489
490 // php error
491
492 }
493
494 // return result as json
495 slb_return_json( $result );
496
497}
498
499// hint: removes a single subscription from a subscriber
500function slb_remove_subscription( $subscriber_id, $list_id ) {
501
502 // setup default return value
503 $subscription_saved = false;
504
505 // IF the subscriber has the current list subscription
506 if( slb_subscriber_has_subscription( $subscriber_id, $list_id ) ):
507
508 // get current subscriptions
509 $subscriptions = slb_get_subscriptions( $subscriber_id );
510
511 // get the position of the $list_id to remove
512 $needle = array_search( $list_id, $subscriptions );
513
514 // remove $list_id from $subscriptions array
515 unset( $subscriptions[$needle] );
516
517 // update slb_subscriptions
518 update_field(slb_get_acf_key( 'slb_subscriptions'), $subscriptions, $subscriber_id);
519
520 // subscriptions updated!
521 $subscription_saved = true;
522
523 endif;
524
525 // return result
526 return $subscription_saved;
527
528}
529
530// hint: sends a unqiue customized email to a subscriber
531function slb_send_subscriber_email( $subscriber_id, $email_template_name, $list_id ) {
532
533 // setup return variable
534 $email_sent = false;
535
536 // get email template data
537 $email_template_object = slb_get_email_template( $subscriber_id, $email_template_name, $list_id );
538
539 // IF email template data was found
540 if( !empty( $email_template_object ) ):
541
542 // get subscriber data
543 $subscriber_data = slb_get_subscriber_data( $subscriber_id );
544
545 // set wp_mail headers
546 $wp_mail_headers = array('Content-Type: text/html; charset=UTF-8');
547
548 // use wp_mail to send email
549 $email_sent = wp_mail( array( $subscriber_data['email'] ) , $email_template_object['subject'], $email_template_object['body'], $wp_mail_headers );
550
551 endif;
552
553 return $email_sent;
554
555}
556
557
558// hint: returns an array of email template data IF the template exists
559function slb_get_email_template( $subscriber_id, $email_template_name, $list_id ) {
560
561 // setup return variable
562 $template_data = array();
563
564 // create new array to store email templates
565 $email_templates = array();
566
567 // get list object
568 $list = get_post( $list_id );
569
570 // get subscriber object
571 $subscriber = get_post( $subscriber_id );
572
573 if( !slb_validate_list( $list ) || !slb_validate_subscriber( $subscriber ) ):
574
575 // the list or the subscriber is not valid
576
577 else:
578
579 // get subscriber data
580 $subscriber_data = slb_get_subscriber_data( $subscriber_id );
581
582 // get unique manage subscription link
583 $manage_subscriptions_link = slb_get_manage_subscriptions_link( $subscriber_data['email'], $list_id );
584
585 // get default email header
586 $default_email_header = '
587 <p>
588 Hello, '. $subscriber_data['fname'] .'
589 </p>
590 ';
591
592 // get default email footer
593 $default_email_footer = slb_get_option('slb_default_email_footer');
594
595 // setup unsubscribe text
596 $unsubscribe_text = '
597 <br /><br />
598 <hr />
599 <p><a href="'. $manage_subscriptions_link .'">Click here to unsubscribe</a> from this or any other email list.</p>';
600
601
602
603 // setup email templates
604
605 // template: new_subscription
606 $email_templates['new_subscription'] = array(
607 'subject' => 'Thank you for subscribing to '. $list->post_title .'!',
608 'body' => '
609 '. $default_email_header .'
610 <p>Thank you for subscribing to '. $list->post_title .'!</p>
611 '. $default_email_footer . $unsubscribe_text,
612 );
613
614 endif;
615
616 // IF the requested email template exists
617 if( isset( $email_templates[ $email_template_name ] ) ):
618
619 // add template data to return variable
620 $template_data = $email_templates[ $email_template_name ];
621
622 endif;
623
624 // return template data
625 return $template_data;
626
627}
628
629// hint: returns a unique link for managing a particular users subscriptions
630function slb_get_manage_subscriptions_link( $email, $list_id=0 ) {
631
632 $link_href = '';
633
634 try {
635
636 $page = get_post( slb_get_option('slb_manage_subscription_page_id') );
637 $slug = $page->post_name;
638
639 $permalink = get_permalink($page);
640
641 // get character to start querystring
642 $startquery = slb_get_querystring_start( $permalink );
643
644 $link_href = $permalink . $startquery .'email='. urlencode($email) .'&list='. $list_id;
645
646 } catch( Exception $e ) {
647
648 //$link_href = $e->getMessage();
649
650 }
651
652 return esc_url($link_href);
653
654}
655
656// hint: returns the appropriate character for the begining of a querystring
657function slb_get_querystring_start( $permalink ) {
658
659 // setup our default return variable
660 $querystring_start = '&';
661
662 // IF ? is not found in the permalink
663 if( strpos($permalink, '?') === false ):
664 $querystring_start = '?';
665 endif;
666
667 return $querystring_start;
668
669}
670
671//get the unique act field key from field name
672function slb_get_acf_key($field_name){
673 $field_key = $field_name;
674 switch ($field_name) {
675 case 'slb_fname':
676 $field_key = 'field_5be2e3e848fc2';
677 break;
678 case 'slb_lname':
679 $field_key = 'field_5be2e42448fc3';
680 break;
681 case 'slb_email':
682 $field_key = 'field_5be2e45748fc4';
683 break;
684 case 'slb_subscriptions':
685 $field_key = 'field_5be2e49248fc5';
686 break;
687 }
688
689 return $field_key;
690
691}
692
693function slb_get_subscriber_data($subscriber_id){
694
695 // setup subscriber_data
696 $subscriber_data = array();
697
698 // get subscriber object
699 $subscriber = get_post( $subscriber_id );
700
701 // IF subscriber object is valid
702 if( isset($subscriber->post_type) && $subscriber->post_type == 'slb_subscriber' ):
703
704 $fname = get_field( slb_get_acf_key('slb_fname'), $subscriber_id);
705 $lname = get_field( slb_get_acf_key('slb_lname'), $subscriber_id);
706
707 // build subscriber_data for return
708 $subscriber_data = array(
709 'name'=> $fname .' '. $lname,
710 'fname'=>$fname,
711 'lname'=>$lname,
712 'email'=>get_field( slb_get_acf_key('slb_email'), $subscriber_id),
713 'subscriptions'=>slb_get_subscriptions( $subscriber_id )
714 );
715
716
717 endif;
718
719 // return subscriber_data
720 return $subscriber_data;
721}
722
723function slb_subscriber_has_subscription($subscriber_id, $list_id){
724 // setup default return value
725 $has_subscription = false;
726
727 // get subscriber
728 $subscriber = get_post($subscriber_id);
729
730 // get subscriptions
731 $subscriptions = slb_get_subscriptions( $subscriber_id );
732
733 // check subscriptions for $list_id
734 if( in_array($list_id, $subscriptions) ):
735
736 // found the $list_id in $subscriptions
737 // this subscriber is already subscribed to this list
738 $has_subscription = true;
739
740 else:
741
742 // did not find $list_id in $subscriptions
743 // this subscriber is not yet subscribed to this list
744
745 endif;
746
747 return $has_subscription;
748}
749
750function slb_get_subscriber_id($email){
751 $subscriber_id = 0;
752
753 try {
754
755 // check if subscriber already exists
756 $subscriber_query = new WP_Query(
757 array(
758 'post_type' => 'slb_subscriber',
759 'posts_per_page' => 1,
760 'meta_key' => 'slb_email',
761 'meta_query' => array(
762 array(
763 'key' => 'slb_email',
764 'value' => $email, // or whatever it is you're using here
765 'compare' => '=',
766 ),
767 ),
768 )
769 );
770
771 // IF the subscriber exists...
772 if( $subscriber_query->have_posts() ):
773
774 // get the subscriber_id
775 $subscriber_query->the_post();
776 $subscriber_id = get_the_ID();
777
778 endif;
779
780 } catch( Exception $e ) {
781
782 // a php error occurred
783
784 }
785
786 // reset the Wordpress post object
787 wp_reset_query();
788
789 return (int)$subscriber_id;
790
791}
792
793function slb_get_subscriptions($subscriber_id){
794
795 $subscriptions = array();
796
797 // get subscriptions (returns array of list objects)
798 $lists = get_field( slb_get_acf_key('slb_subscriptions'), $subscriber_id );
799
800 // IF $lists returns something
801 if( $lists ):
802
803 // IF $lists is an array and there is one or more items
804 if( is_array($lists) && count($lists) ):
805 // build subscriptions: array of list id's
806 foreach( $lists as &$list):
807 $subscriptions[]= (int)$list->ID;
808 endforeach;
809 elseif( is_numeric($lists) ):
810 // single result returned
811 $subscriptions[]= $lists;
812 endif;
813
814 endif;
815
816 return (array)$subscriptions;
817}
818
819function slb_return_json($php_array){
820
821 $json_result = json_encode($php_array);
822 //return result
823 die($json_result);
824 //stop all other processing
825 exit;
826}
827
828// hint: generates an html form for managing subscriptions
829function slb_get_manage_subscriptions_html( $subscriber_id ) {
830
831 $output = '';
832
833 try {
834
835 // get array of list_ids for this subscriber
836 $lists = slb_get_subscriptions( $subscriber_id );
837
838 // get the subscriber data
839 $subscriber_data = slb_get_subscriber_data( $subscriber_id );
840
841 // set the title
842 $title = $subscriber_data['fname'] .'\'s Subscriptions';
843
844 // build out output html
845 $output = '
846 <form id="slb_manage_subscriptions_form" class="slb-form" method="post"
847 action="/wp-admin/admin-ajax.php?action=slb_unsubscribe">
848
849 <input type="hidden" name="subscriber_id" value="'. $subscriber_id .'">
850
851 <h3 class="slb-title">'. $title .'</h3>';
852
853 if( !count($lists) ):
854
855 $output .='<p>There are no active subscriptions.</p>';
856
857 else:
858
859 $output .= '<table>
860 <tbody>';
861
862 // loop over lists
863 foreach( $lists as &$list_id ):
864
865 $list_object = get_post( $list_id );
866
867 $output .= '<tr>
868 <td>'.
869 $list_object->post_title
870 .'</td>
871 <td>
872 <label>
873 <input
874 type="checkbox" name="list_ids[]"
875 value="'. $list_object->ID .'"
876 /> UNSUBSCRIBE
877 </label>
878 </td>
879 </tr>';
880
881 endforeach;
882
883 // close up our output html
884 $output .='</tbody>
885 </table>
886
887 <p><input type="submit" value="Save Changes" /></p>';
888
889 endif;
890
891 $output .='
892 </form>
893 ';
894
895 } catch( Exception $e ) {
896
897 // php error
898
899 }
900
901 // return output
902 return $output;
903
904}
905
906
907//add admin page
908function slb_dashboard_admin_page(){
909 $output = '
910 <div class="wrap">
911 <h2>Snappy List Builder</h2>
912 <p>The ultimate email list building plugin for wordpress. Capture new subscribers. Reward subscribers with a custom download upon opt-in.Build unlimited lists.Import and export subscribers easily with .csv</p>
913 </div>
914 ';
915
916 echo $output;
917}
918//import subscribers amin page
919function slb_import_admin_page(){
920 $output = '
921 <div class="wrap">
922 <h2>Import Subscribers</h2>
923 <p>Page description...</P>
924 </div>
925 ';
926 echo $output;
927}
928
929// hint: returns default option values as an associative array
930function slb_get_default_options() {
931
932 $defaults = array();
933
934 try {
935
936 // get front page id
937 $front_page_id = get_option('page_on_front');
938
939 // setup default email footer
940 $default_email_footer = '
941 <p>
942 Sincerely, <br /><br />
943 The '. get_bloginfo('name') .' Team<br />
944 <a href="'. get_bloginfo('url') .'">'. get_bloginfo('url') .'</a>
945 </p>
946 ';
947
948 // setup defaults array
949 $defaults = array(
950 'slb_manage_subscription_page_id'=>$front_page_id,
951 'slb_confirmation_page_id'=>$front_page_id,
952 'slb_reward_page_id'=>$front_page_id,
953 'slb_default_email_footer'=>$default_email_footer,
954 'slb_download_limit'=>3,
955 );
956
957 } catch( Exception $e) {
958
959 // php error
960
961 }
962
963 // return defaults
964 return $defaults;
965
966
967}
968
969// hint: returns the requested page option value or it's default
970function slb_get_option( $option_name ) {
971
972 // setup return variable
973 $option_value = '';
974
975
976 try {
977
978 // get default option values
979 $defaults = slb_get_default_options();
980
981 // get the requested option
982 switch( $option_name ) {
983
984 case 'slb_manage_subscription_page_id':
985 // subscription page id
986 $option_value = (get_option('slb_manage_subscription_page_id')) ? get_option('slb_manage_subscription_page_id') : $defaults['slb_manage_subscription_page_id'];
987 break;
988 case 'slb_confirmation_page_id':
989 // confirmation page id
990 $option_value = (get_option('slb_confirmation_page_id')) ? get_option('slb_confirmation_page_id') : $defaults['slb_confirmation_page_id'];
991 break;
992 case 'slb_reward_page_id':
993 // reward page id
994 $option_value = (get_option('slb_reward_page_id')) ? get_option('slb_reward_page_id') : $defaults['slb_reward_page_id'];
995 break;
996 case 'slb_default_email_footer':
997 // email footer
998 $option_value = (get_option('slb_default_email_footer')) ? get_option('slb_default_email_footer') : $defaults['slb_default_email_footer'];
999 break;
1000 case 'slb_download_limit':
1001 // reward download limit
1002 $option_value = (get_option('slb_download_limit')) ? (int)get_option('slb_download_limit') : $defaults['slb_download_limit'];
1003 break;
1004
1005 }
1006
1007 } catch( Exception $e) {
1008
1009 // php error
1010
1011 }
1012
1013 // return option value or it's default
1014 return $option_value;
1015
1016}
1017
1018
1019// hint: get's the current options and returns values in associative array
1020function slb_get_current_options() {
1021
1022 // setup our return variable
1023 $current_options = array();
1024
1025 try {
1026
1027 // build our current options associative array
1028 $current_options = array(
1029 'slb_manage_subscription_page_id' => slb_get_option('slb_manage_subscription_page_id'),
1030 'slb_confirmation_page_id' => slb_get_option('slb_confirmation_page_id'),
1031 'slb_reward_page_id' => slb_get_option('slb_reward_page_id'),
1032 'slb_default_email_footer' => slb_get_option('slb_default_email_footer'),
1033 'slb_download_limit' => slb_get_option('slb_download_limit'),
1034 );
1035
1036 } catch( Exception $e ) {
1037
1038 // php error
1039
1040 }
1041
1042 // return current options
1043 return $current_options;
1044
1045}
1046
1047
1048
1049// hint: returns html for a page selector
1050function slb_get_page_select( $input_name="slb_page", $input_id="", $parent=-1, $value_field="id", $selected_value="" ) {
1051
1052 // get WP pages
1053 $pages = get_pages(
1054 array(
1055 'sort_order' => 'asc',
1056 'sort_column' => 'post_title',
1057 'post_type' => 'page',
1058 'parent' => $parent,
1059 'status'=>array('draft','publish'),
1060 )
1061 );
1062
1063 // setup our select html
1064 $select = '<select name="'. $input_name .'" ';
1065
1066 // IF $input_id was passed in
1067 if( strlen($input_id) ):
1068
1069 // add an input id to our select html
1070 $select .= 'id="'. $input_id .'" ';
1071
1072 endif;
1073
1074 // setup our first select option
1075 $select .= '><option value="">- Select One -</option>';
1076
1077 // loop over all the pages
1078 foreach ( $pages as &$page ):
1079
1080 // get the page id as our default option value
1081 $value = $page->ID;
1082
1083 // determine which page attribute is the desired value field
1084 switch( $value_field ) {
1085 case 'slug':
1086 $value = $page->post_name;
1087 break;
1088 case 'url':
1089 $value = get_page_link( $page->ID );
1090 break;
1091 default:
1092 $value = $page->ID;
1093 }
1094
1095 // check if this option is the currently selected option
1096 $selected = '';
1097 if( $selected_value == $value ):
1098 $selected = ' selected="selected" ';
1099 endif;
1100
1101 // build our option html
1102 $option = '<option value="' . $value . '" '. $selected .'>';
1103 $option .= $page->post_title;
1104 $option .= '</option>';
1105
1106 // append our option to the select html
1107 $select .= $option;
1108
1109 endforeach;
1110
1111 // close our select html tag
1112 $select .= '</select>';
1113
1114 // return our new select
1115 return $select;
1116
1117}
1118
1119
1120//plugin options admin page
1121function slb_options_admin_page() {
1122
1123 // get the default values for our options
1124 $options = slb_get_current_options();
1125
1126 echo('<div class="wrap">
1127
1128 <h2>petPlugin Options</h2>
1129
1130 <form action="options.php" method="post">');
1131
1132 // outputs a unique nounce for our plugin options
1133 settings_fields('slb_plugin_options');
1134 // generates a unique hidden field with our form handling url
1135 //@do_settings_fields('slb_plugin_options');
1136
1137 echo('<table class="form-table">
1138
1139 <tbody>
1140
1141 <tr>
1142 <th scope="row"><label for="slb_manage_subscription_page_id">Manage Subscriptions Page</label></th>
1143 <td>
1144 '. slb_get_page_select( 'slb_manage_subscription_page_id', 'slb_manage_subscription_page_id', 0, 'id', $options['slb_manage_subscription_page_id'] ) .'
1145 <p class="description" id="slb_manage_subscription_page_id-description">This is the page where Snappy List Builder will send subscribers to manage their subscriptions. <br />
1146 IMPORTANT: In order to work, the page you select must contain the shortcode: <strong>[slb_manage_subscriptions]</strong>.</p>
1147 </td>
1148 </tr>
1149
1150
1151 <tr>
1152 <th scope="row"><label for="slb_confirmation_page_id">Opt-In Page</label></th>
1153 <td>
1154 '. slb_get_page_select( 'slb_confirmation_page_id', 'slb_confirmation_page_id', 0, 'id', $options['slb_confirmation_page_id'] ) .'
1155 <p class="description" id="slb_confirmation_page_id-description">This is the page where Snappy List Builder will send subscribers to confirm their subscriptions. <br />
1156 IMPORTANT: In order to work, the page you select must contain the shortcode: <strong>[slb_confirm_subscription]</strong>.</p>
1157 </td>
1158 </tr>
1159
1160
1161 <tr>
1162 <th scope="row"><label for="slb_reward_page_id">Download Reward Page</label></th>
1163 <td>
1164 '. slb_get_page_select( 'slb_reward_page_id', 'slb_reward_page_id', 0, 'id', $options['slb_reward_page_id'] ) .'
1165 <p class="description" id="slb_reward_page_id-description">This is the page where Snappy List Builder will send subscribers to retrieve their reward downloads. <br />
1166 IMPORTANT: In order to work, the page you select must contain the shortcode: <strong>[slb_download_reward]</strong>.</p>
1167 </td>
1168 </tr>
1169
1170 <tr>
1171 <th scope="row"><label for="slb_default_email_footer">Email Footer</label></th>
1172 <td>');
1173
1174
1175 // wp_editor will act funny if it's stored in a string so we run it like this...
1176 wp_editor( $options['slb_default_email_footer'], 'slb_default_email_footer', array( 'textarea_rows'=>8 ) );
1177
1178
1179 echo('<p class="description" id="slb_default_email_footer-description">The default text that appears at the end of emails generated by this plugin.</p>
1180 </td>
1181 </tr>
1182
1183 <tr>
1184 <th scope="row"><label for="slb_download_limit">Reward Download Limit</label></th>
1185 <td>
1186 <input type="number" name="slb_download_limit" value="'. $options['slb_download_limit'] .'" class="" />
1187 <p class="description" id="slb_download_limit-description">The amount of downloads a reward link will allow before expiring.</p>
1188 </td>
1189 </tr>
1190
1191 </tbody>
1192
1193 </table>');
1194
1195 // outputs the WP submit button html
1196 @submit_button();
1197
1198
1199 echo('</form>
1200
1201 </div>');
1202
1203}
1204
1205
1206function slb_admin_menus(){
1207 //main menu
1208 $top_menu_item = 'slb_dashboard_admin_page';
1209 add_menu_page('','List Builder','manage_options','slb_dashboard_admin_page','slb_dashboard_admin_page','dashicons-email-alt');
1210 //submenu items
1211 add_submenu_page($top_menu_item,'','Dashboard','manage_options',$top_menu_item,$top_menu_item);
1212 add_submenu_page($top_menu_item,'','Email Lists','manage_options','edit.php?post_type=slb_list');
1213 add_submenu_page($top_menu_item,'','Subscribers','manage_options','edit.php?post_type=slb_subscriber');
1214 add_submenu_page($top_menu_item,'','Import Subscribers','manage_options','slb_import_admin_page','slb_import_admin_page');
1215 add_submenu_page($top_menu_item,'','Plugin Options','manage_options','slb_options_admin_page','slb_options_admin_page');
1216
1217
1218}
1219
1220
1221
1222// hint: registers all our plugin options
1223function slb_register_options() {
1224 // plugin options
1225 register_setting('slb_plugin_options', 'slb_manage_subscription_page_id');
1226 register_setting('slb_plugin_options', 'slb_confirmation_page_id');
1227 register_setting('slb_plugin_options', 'slb_reward_page_id');
1228 register_setting('slb_plugin_options', 'slb_default_email_footer');
1229 register_setting('slb_plugin_options', 'slb_download_limit');
1230}