| 788 | | } // CampTix_Require_Login |
| | 828 | |
| | 829 | /** |
| | 830 | * Send reminders to the unconfirmed attendees |
| | 831 | * |
| | 832 | * TODO: |
| | 833 | * - get proper text for the subject and body for each situation; |
| | 834 | * |
| | 835 | * @return array |
| | 836 | */ |
| | 837 | public function remind_unconfirmed_attendees() { |
| | 838 | /** @var $camptix CampTix_Plugin */ |
| | 839 | global $shortcode_tags, $camptix; |
| | 840 | |
| | 841 | // todo maybe split this into at least 3 functions: determine if ready to send, send status for unknown attendees to buyer, send individual unconfirmed attendees to their email |
| | 842 | // todo if they buy ticket 8 days before camp, don't send reminder on the 7th, wait at least 7? days since last reminder |
| | 843 | // todo add batch email functionality to camptix proper, then just push the data you want here. probably already exists to some extent, so use it |
| | 844 | |
| | 845 | // Set up a couple dates to use for calculations |
| | 846 | $wordcamp = get_wordcamp_post(); |
| | 847 | $days_until_camp = floor( ( $wordcamp->meta['Start Date (YYYY-mm-dd)'][0] - time() ) / DAY_IN_SECONDS ); |
| | 848 | $unknown_attendee_info = $this->get_unknown_attendee_info(); |
| | 849 | $camptix_options = $camptix->get_options(); |
| | 850 | $emails_sent = 0; |
| | 851 | $max_emails_to_send = 35; |
| | 852 | $transactions_with_unknown_attendees = array(); |
| | 853 | |
| | 854 | // Only send reminders once a week, but if the camp is happening this week and they still haven't registered, allow an extra one. |
| | 855 | if ( $days_until_camp >= 7 ) { |
| | 856 | $reminder_interval = 7 * DAY_IN_SECONDS; |
| | 857 | } else { |
| | 858 | $reminder_interval = 3 * DAY_IN_SECONDS; |
| | 859 | } |
| | 860 | |
| | 861 | // Remove all shortcodes before sending the e-mails, but bring them back later. |
| | 862 | $this->removed_shortcodes = $shortcode_tags; |
| | 863 | remove_all_shortcodes(); |
| | 864 | |
| | 865 | // setup shortcodes for these email templates |
| | 866 | do_action( 'camptix_init_email_templates_shortcodes' ); |
| | 867 | |
| | 868 | $unconfirmed_attendees = get_posts( array( |
| | 869 | 'post_type' => 'tix_attendee', |
| | 870 | 'post_status' => array( 'pending', 'publish' ), |
| | 871 | 'posts_per_page' => 100, // todo bigger? smaller? needs permenant comment explaining why it is what it is? |
| | 872 | 'meta_query' => array( |
| | 873 | array( |
| | 874 | 'key' => 'tix_username', |
| | 875 | 'value' => self::UNCONFIRMED_USERNAME, |
| | 876 | 'compare' => '=', |
| | 877 | ), |
| | 878 | |
| | 879 | array( |
| | 880 | 'key' => 'tix_last_unconfirmed_reminder', |
| | 881 | 'value' => time() - $reminder_interval, |
| | 882 | 'compare' => '<', |
| | 883 | |
| | 884 | /* todo will need to insert meta val when creating ticket, and manually insert for 2014.sf */ |
| | 885 | ), |
| | 886 | ), |
| | 887 | ) ); |
| | 888 | |
| | 889 | $subject = sprintf( __( "%s Unconfirmed Ticket Reminder", 'camptix' ), $wordcamp->post_title ); // todo update subj |
| | 890 | |
| | 891 | foreach ( $unconfirmed_attendees as $attendee ) { |
| | 892 | $attendee_meta = get_post_custom( $attendee->ID ); |
| | 893 | $camptix->tmp( 'attendee_id', $attendee->ID ); |
| | 894 | |
| | 895 | if ( $emails_sent >= $max_emails_to_send ) { |
| | 896 | break; |
| | 897 | } |
| | 898 | |
| | 899 | /* |
| | 900 | * We can't send e-mail to unknown attendees because we don't have their address, |
| | 901 | * so we'll send an e-mail to the buy later with the status of all their tickets |
| | 902 | */ |
| | 903 | if ( $unknown_attendee_info['email'] == $attendee_meta['tix_email'][0] ) { |
| | 904 | $transactions_with_unknown_attendees[] = $attendee_meta['tix_payment_token'][0]; |
| | 905 | continue; |
| | 906 | } |
| | 907 | |
| | 908 | /* |
| | 909 | doing this in mysql instead |
| | 910 | |
| | 911 | $last_reminder_sent = empty( $attendee_meta['tix_last_unconfirmed_reminder'] ) ? strtotime( $attendee->post_date_gmt ) : $attendee_meta['tix_last_unconfirmed_reminder']; |
| | 912 | |
| | 913 | // Don't nag attendees too often |
| | 914 | if ( time() - $last_reminder_sent > $reminder_interval ) { |
| | 915 | printf( '<p>skipping %s - last sent %s days ago', $attendee->ID, ( time() - $last_reminder_sent ) / DAY_IN_SECONDS ); // todo |
| | 916 | continue; |
| | 917 | } |
| | 918 | */ |
| | 919 | |
| | 920 | // Set notification vars |
| | 921 | $camptix->tmp( 'ticket_url', $camptix->get_edit_attendee_link( $attendee->ID, $attendee_meta['tix_edit_token'][0] ) ); |
| | 922 | |
| | 923 | // If there is an email address for the unconfirmed attendee use it, else use the receipt email address |
| | 924 | $send_to = $attendee_meta['tix_email'][0]; // todo test to confirm switching this didn't break it |
| | 925 | $email_template = apply_filters( 'camptix_email_tickets_template', 'email_template_multiple_purchase_unconfirmed_attendee_reminder', $attendee ); |
| | 926 | |
| | 927 | $content = do_shortcode( $camptix_options[ $email_template ] ); // todo test b/c changed |
| | 928 | |
| | 929 | printf( '<p>Sending unconfirmed %s', $attendee->post_title ); |
| | 930 | continue; |
| | 931 | |
| | 932 | // Send message |
| | 933 | if ( $camptix->wp_mail( $send_to, $subject, $content ) ) { |
| | 934 | update_post_meta( $attendee->ID, 'tix_last_unconfirmed_reminder', time() ); // todo bcc yourself for the first few days to check that they look good. have to do this in mu plugin |
| | 935 | $emails_sent++; |
| | 936 | } |
| | 937 | |
| | 938 | } |
| | 939 | |
| | 940 | if ( $emails_sent < $max_emails_to_send ) { |
| | 941 | $this->remind_unknown_attendees( array_unique( $transactions_with_unknown_attendees ), $camptix_options, $max_emails_to_send - $emails_sent ); |
| | 942 | } |
| | 943 | |
| | 944 | // Bring the original shortcodes back. |
| | 945 | $shortcode_tags = $this->removed_shortcodes; |
| | 946 | $this->removed_shortcodes = array(); |
| | 947 | |
| | 948 | } |
| | 949 | |
| | 950 | // todo |
| | 951 | // send all for entire transaction, but don't send any for next transaction if above the limit |
| | 952 | // already know they haven't been reminded recently |
| | 953 | protected function remind_unknown_attendees( $transactions, $camptix_options, $number_to_send ) { |
| | 954 | /** @var $camptix CampTix_Plugin */ |
| | 955 | global $camptix; |
| | 956 | $emails_sent = 0; |
| | 957 | |
| | 958 | foreach ( $transactions as $payment_token ) { |
| | 959 | if ( $emails_sent >= $number_to_send ) { |
| | 960 | break; |
| | 961 | } |
| | 962 | |
| | 963 | $attendees = get_posts( array( |
| | 964 | 'post_type' => 'tix_attendee', |
| | 965 | 'post_status' => array( 'pending', 'publish' ), |
| | 966 | 'posts_per_page' => -1, |
| | 967 | |
| | 968 | 'meta_query' => array( |
| | 969 | array( |
| | 970 | 'key' => 'tix_payment_token', |
| | 971 | 'value' => $payment_token |
| | 972 | ), |
| | 973 | ), |
| | 974 | ) ); |
| | 975 | |
| | 976 | $ticket_holder_status = ''; |
| | 977 | foreach ( $attendees as $attendee ) { |
| | 978 | if ( self::UNKNOWN_ATTENDEE_EMAIL ) { |
| | 979 | $edit_token = get_post_meta( $attendee->ID, 'edit_token', true ); |
| | 980 | |
| | 981 | $confirmation_link = sprintf( |
| | 982 | "\nConfirmation link: %s", |
| | 983 | $camptix->get_edit_attendee_link( $attendee->ID, $edit_token ) |
| | 984 | ); |
| | 985 | } else { |
| | 986 | $confirmation_link = ''; |
| | 987 | } |
| | 988 | |
| | 989 | $ticket_holder_status .= sprintf( |
| | 990 | "\nTicket %s: %s |
| | 991 | %s\n", |
| | 992 | 1, |
| | 993 | $attendee->post_title, |
| | 994 | $confirmation_link |
| | 995 | ); |
| | 996 | } |
| | 997 | $camptix->tmp( 'ticket_holder_status', $ticket_holder_status ); |
| | 998 | |
| | 999 | $email_template = apply_filters( 'camptix_email_tickets_template', 'email_template_multiple_purchase_unknown_attendees_reminder', $attendee ); |
| | 1000 | $content = do_shortcode( $camptix_options[ $email_template ] ); // todo test b/c changed |
| | 1001 | |
| | 1002 | |
| | 1003 | printf( '<p>sending unknown - %s', implode( ', ', wp_list_pluck( $attendees, 'post_title' ) ) ); |
| | 1004 | continue; |
| | 1005 | |
| | 1006 | if ( $camptix->wp_mail( $buyer_email, $subject, $content ) ) { |
| | 1007 | $emails_sent++; |
| | 1008 | |
| | 1009 | foreach ( $attendees as $attendee ) { |
| | 1010 | update_post_meta( $attendee->ID, 'tix_last_unconfirmed_reminder', time() ); // todo bcc yourself for the first few days to check that they look good. have to do this in mu plugin |
| | 1011 | } |
| | 1012 | } |
| | 1013 | } |
| | 1014 | } |
| | 1015 | |
| | 1016 | /** |
| | 1017 | * Returns the ticket holder status |
| | 1018 | */ |
| | 1019 | function email_template_shortcode_ticket_holder_status( $atts ) { |
| | 1020 | /** @var $camptix CampTix_Plugin */ |
| | 1021 | global $camptix; |
| | 1022 | |
| | 1023 | return $camptix->tmp( 'ticket_holder_status' ); |
| | 1024 | } |
| | 1025 | |
| | 1026 | } // CampTix_Require_Login |