Making WordPress.org

Ticket #595: 595.2.patch

File 595.2.patch, 13.3 KB (added by iandunn, 11 years ago)
  • addons/require-login.php

    diff --git a/addons/require-login.php b/addons/require-login.php
    index 569dd49..4c29c0e 100644
    a b class CampTix_Require_Login extends CampTix_Addon { 
    2828                add_filter( 'camptix_checkout_attendee_info',                 array( $this, 'add_unknown_attendee_info_stubs' ) );
    2929                add_filter( 'camptix_edit_info_cell_content',                 array( $this, 'show_buyer_attendee_status_instead_of_edit_link' ), 10, 2 );
    3030                add_filter( 'camptix_attendee_info_default_value',            array( $this, 'prepopulate_known_fields' ), 10, 5 );
     31                add_action( 'camptix_ticket_emailed',                         array( $this, 'record_first_email_reminder' ) );
    3132
    3233                // wp-admin
    3334                add_filter( 'camptix_attendee_report_column_value_username',  array( $this, 'get_attendee_username_meta' ), 10, 2 );
    class CampTix_Require_Login extends CampTix_Addon { 
    4849                add_action( 'template_redirect',                              array( $this, 'block_unauthenticated_actions' ), 7 );    // before CampTix_Plugin->template_redirect()
    4950                add_filter( 'camptix_attendees_shortcode_query_args',         array( $this, 'hide_unconfirmed_attendees' ) );
    5051                add_filter( 'camptix_private_attendees_parameters',           array( $this, 'prevent_unknown_attendees_viewing_private_content' ) );
     52                add_action( 'hourly',                                         array( $this, 'remind_unconfirmed_attendees' ) );
     53                add_shortcode( 'ticket_holder_status',                        array( $this, 'email_template_shortcode_ticket_holder_status' ) );
    5154        }
    5255
    5356        /**
    class CampTix_Require_Login extends CampTix_Addon { 
    308311         */
    309312        public function get_attendee_search_meta( $attendee_search_meta ) {
    310313                $attendee_search_meta[] = 'tix_username';
    311                
     314
    312315                return $attendee_search_meta;
    313316        }
    314317
    class CampTix_Require_Login extends CampTix_Addon { 
    353356                        'callback_method' => 'field_textarea',
    354357                );
    355358
     359                $templates['email_template_unconfirmed_attendee_reminder'] = array(
     360                        'title'           => __( 'Multiple Purchase Reminder (to unconfirmed attendees)', 'camptix' ),      // todo just use the same one they get when they registered?
     361                        'callback_method' => 'field_textarea',
     362                );
     363
     364                $templates['email_template_unknown_attendee_reminder'] = array(
     365                        'title'           => __( 'Multiple Purchase Reminder (for unknown attendees)', 'camptix' ),
     366                        'callback_method' => 'field_textarea',
     367                );
     368
    356369                return $templates;
    357370        }
    358371
    class CampTix_Require_Login extends CampTix_Addon { 
    366379        public function custom_email_template_default_values( $options ) {
    367380                $options['email_template_multiple_purchase_receipt_unconfirmed_attendees'] = __( "Hi there!\n\nYou have purchased the following tickets:\n\n[receipt]\n\nYou can view and edit your order at any time before the event, by visiting the following link:\n\n[ticket_url]\n\nThe other attendees that you purchased tickets for will need to confirm their registration by visiting a link that was sent to them by e-mail.\n\nLet us know if you have any questions!", 'camptix' );
    368381                $options['email_template_multiple_purchase_unconfirmed_attendee']          = __( "Hi there!\n\nA ticket to [event_name] has been purchased for you by [buyer_full_name].\n\nPlease visit the following page and fill in your information to complete your registration:\n\n[ticket_url]\n\nLet us know if you have any questions!", 'camptix' );
     382                $options['email_template_multiple_purchase_unconfirmed_attendee_reminder'] = __( "Hi there!\n\nThe ticket to [event_name] that [buyer_full_name] purchased for you has not yet been confirmed.\n\nTo confirm your ticket, please complete the form on the following page:\n\n[ticket_url]\n\nLet us know if you have any questions!", 'camptix' );
    369383                $options['email_template_multiple_purchase_unknown_attendee']              = __( "Hi there!\n\nThis e-mail is for the unknown attendee that you purchased a ticket for. When you decide who will be using the ticket, please forward the link below to them so that they can complete their registration.\n\n[ticket_url]\n\nLet us know if you have any questions!", 'camptix' );
     384                $options['email_template_multiple_purchase_unknown_attendees_reminder']    = __( "Hi there!\n\nSome of the tickets that you purchased for [event_name] have not been claimed by their holders yet. Please review the summary below, and send the corresponding confirmation link to each ticket holder who has not yet claimed their ticket.\n\n[ticket_holder_status]\n\nLet us know if you have any questions!", 'camptix' );
    370385
    371386                return $options;
    372387        }
    class CampTix_Require_Login extends CampTix_Addon { 
    395410                                }
    396411
    397412                                break;
     413                        case 'email_template_unconfirmed_reminder':
     414                                $unknown_attendee_info = $this->get_unknown_attendee_info();
     415
     416                                if ( $unknown_attendee_info['email'] == get_post_meta( $attendee->ID, 'tix_email', true ) ) {
     417                                        $template = 'email_template_unknown_attendee_reminder';
     418                                } elseif ( self::UNCONFIRMED_USERNAME == get_post_meta( $attendee->ID, 'tix_username', true ) ) {
     419                                        $template = 'email_template_unconfirmed_attendee_reminder';
     420                                }
     421
     422                                break;
    398423                }
    399424
    400425                return $template;
    class CampTix_Require_Login extends CampTix_Addon { 
    581606        }
    582607
    583608        /**
     609         * Initialize the reminder timestamp for unconfirmed attendees.
     610         *
     611         * This fires when attendees are sent the initial e-mail upon successful registration. We will later want
     612         * to send them reminder e-mails if they haven't confirmed their ticket, but we don't want to do that if
     613         * they just registered yesterday, so we treat the ticket purchase timestamp as if it were the first reminder.
     614         *
     615         * @param int $attendee_id
     616         */
     617        public function record_first_email_reminder( $attendee_id ) {
     618                if ( self::UNCONFIRMED_USERNAME == get_post_meta( $attendee_id, 'tix_username', true ) ) {
     619                        update_post_meta( $attendee_id, 'tix_last_unconfirmed_reminder', time() );
     620                }
     621        }
     622
     623        /**
    584624         * Define the unknown attendee info stubs
    585625         *
    586626         * @return array
    class CampTix_Require_Login extends CampTix_Addon { 
    785825
    786826                return $parameters;
    787827        }
    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
    7891027
    7901028camptix_register_addon( 'CampTix_Require_Login' );