| 1 | <?php |
| 2 | /** |
| 3 | * Adds a 'Notify Failed Registrations' Tab in Tickets > Tools |
| 4 | */ |
| 5 | class CampTix_Addon_Notify_Failed extends CampTix_Addon { |
| 6 | |
| 7 | /** |
| 8 | * Register hook callbacks |
| 9 | */ |
| 10 | function camptix_init() { |
| 11 | add_filter( 'camptix_menu_tools_tabs', array( $this, 'menu_tools_tabs' ) ); |
| 12 | add_action( 'camptix_menu_tools_notify_failed', array( $this, 'menu_tools_notify_failed' ), 10, 0 ); |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Add Notify Failed Registrations to the list of Tabs in Tickets > Tools. |
| 17 | * |
| 18 | * @param array $types |
| 19 | * |
| 20 | * @return array |
| 21 | */ |
| 22 | function menu_tools_tabs( $types ) { |
| 23 | return array_merge( $types, array( |
| 24 | 'notify_failed' => __( 'Notify Failed Registrations', 'notify-failed-regs' ), |
| 25 | ) ); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Handles callback to add a Tab 'Notify Failed Registrations' in Tools |
| 30 | * |
| 31 | */ |
| 32 | |
| 33 | function menu_tools_notify_failed() { |
| 34 | global $post, $shortcode_tags, $camptix; |
| 35 | |
| 36 | // Use this array to store existing form data. |
| 37 | $form_data = array( |
| 38 | 'subject' => '', |
| 39 | 'body' => '', |
| 40 | 'tickets' => array(), |
| 41 | ); |
| 42 | |
| 43 | if ( isset( $_POST['tix_notify_attendees'] ) && check_admin_referer( 'tix_notify_attendees' ) ) { |
| 44 | $errors = array(); |
| 45 | |
| 46 | // Error handling. |
| 47 | if ( empty( $_POST['tix_notify_subject'] ) ) |
| 48 | $errors[] = __( 'Please enter a subject line.', 'camptix' ); |
| 49 | |
| 50 | if ( empty( $_POST['tix_notify_body'] ) ) |
| 51 | $errors[] = __( 'Please enter the e-mail body.', 'camptix' ); |
| 52 | |
| 53 | if ( ! isset( $_POST['tix_notify_tickets'] ) || count( (array) $_POST['tix_notify_tickets'] ) < 1 ) |
| 54 | $errors[] = __( 'Please select at least one ticket group.', 'camptix' ); |
| 55 | |
| 56 | // If everything went well. |
| 57 | if ( count( $errors ) == 0 && isset( $_POST['tix_notify_submit'] ) && $_POST['tix_notify_submit'] ) { |
| 58 | $tickets = array_map( 'absint', (array) $_POST['tix_notify_tickets'] ); |
| 59 | $subject = sanitize_text_field( wp_kses_post( stripslashes( $_POST['tix_notify_subject'] ) ) ); |
| 60 | $body = wp_kses_post( stripslashes( $_POST['tix_notify_body'] ) ); |
| 61 | $recipients = array(); |
| 62 | |
| 63 | $paged = 1; |
| 64 | while ( $attendees = get_posts( array( |
| 65 | 'post_type' => 'tix_attendee', |
| 66 | 'posts_per_page' => 200, |
| 67 | 'post_status' => array( 'failed', 'timeout' ), |
| 68 | 'paged' => $paged++, |
| 69 | 'fields' => 'ids', // ! no post objects |
| 70 | 'orderby' => 'ID', |
| 71 | 'order' => 'ASC', |
| 72 | 'cache_results' => false, // no caching |
| 73 | ) ) ) { |
| 74 | |
| 75 | // Disables object caching, see Revenue report for details. |
| 76 | $camptix->filter_post_meta = $camptix->prepare_metadata_for( $attendees ); |
| 77 | |
| 78 | foreach ( $attendees as $attendee_id ) { |
| 79 | $emails_query = new WP_Query( array( |
| 80 | 'post_type' => 'tix_email', |
| 81 | 'post_status' => array( 'publish', 'pending' ), |
| 82 | 'posts_per_page' => -1, |
| 83 | 'relation' => 'AND', |
| 84 | 'meta_key' => 'tix_email_recipient_id', |
| 85 | 'meta_value' => $attendee_id, |
| 86 | 'meta_compare' => '=', |
| 87 | |
| 88 | ) ); |
| 89 | |
| 90 | if ( array_key_exists( get_post_meta( $attendee_id, 'tix_ticket_id', true ), $tickets ) && $emails_query->post_count == 0 ) |
| 91 | $recipients[] = $attendee_id; |
| 92 | } |
| 93 | // Enable object caching for post meta back on. |
| 94 | $camptix->filter_post_meta = false; |
| 95 | } |
| 96 | |
| 97 | unset( $attendees ); |
| 98 | |
| 99 | // Create a new e-mail job. |
| 100 | $email_id = wp_insert_post( array( |
| 101 | 'post_type' => 'tix_email', |
| 102 | 'post_status' => 'pending', |
| 103 | 'post_title' => $subject, |
| 104 | 'post_content' => $body, |
| 105 | ) ); |
| 106 | |
| 107 | // Add recipients as post meta. |
| 108 | if ( $email_id ) { |
| 109 | add_settings_error( 'camptix', 'none', sprintf( __( 'Your e-mail job has been queued for %s recipients.', 'camptix' ), count( $recipients ) ), 'updated' ); |
| 110 | $camptix->log( sprintf( 'Created e-mail job with %s recipients.', count( $recipients ) ), $email_id, null, 'notify' ); |
| 111 | |
| 112 | foreach ( $recipients as $recipient_id ) |
| 113 | add_post_meta( $email_id, 'tix_email_recipient_id', $recipient_id ); |
| 114 | |
| 115 | update_post_meta( $email_id, 'tix_email_recipients_backup', $recipients ); // for logging purposes |
| 116 | unset( $recipients ); |
| 117 | } |
| 118 | } else { // errors or preview |
| 119 | |
| 120 | if ( count( $errors ) > 0 ) |
| 121 | foreach ( $errors as $error ) |
| 122 | add_settings_error( 'camptix', false, $error ); |
| 123 | |
| 124 | // Keep form data. |
| 125 | $form_data['subject'] = wp_kses_post( stripslashes( $_POST['tix_notify_subject'] ) ); |
| 126 | $form_data['body'] = wp_kses_post( stripslashes( $_POST['tix_notify_body'] ) ); |
| 127 | if ( isset( $_POST['tix_notify_tickets'] ) ) |
| 128 | $form_data['tickets'] = array_map( 'absint', (array) $_POST['tix_notify_tickets'] ); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // Remove all standard shortcodes. |
| 133 | $camptix->removed_shortcodes = $shortcode_tags; |
| 134 | remove_all_shortcodes(); |
| 135 | |
| 136 | do_action( 'camptix_init_notify_shortcodes' ); |
| 137 | ?> |
| 138 | <?php settings_errors( 'camptix' ); ?> |
| 139 | <form method="post" action="<?php echo esc_url( add_query_arg( 'tix_notify_attendees', 1 ) ); ?>"> |
| 140 | <h3>(Only for Attendees with Registration Status as 'Failed' and 'Timeout' )</h3> |
| 141 | <table class="form-table"> |
| 142 | <tbody> |
| 143 | <tr> |
| 144 | <th scope="row"><?php _e( 'To', 'camptix' ); ?></th> |
| 145 | <td> |
| 146 | <?php |
| 147 | $tickets_query = new WP_Query( array( |
| 148 | 'post_type' => 'tix_ticket', |
| 149 | 'post_status' => 'any', |
| 150 | 'posts_per_page' => -1, |
| 151 | ) ); |
| 152 | ?> |
| 153 | <?php while ( $tickets_query->have_posts() ) : $tickets_query->the_post(); ?> |
| 154 | <label><input type="checkbox" <?php checked( array_key_exists( get_the_ID(), $form_data['tickets'] ) ); ?> name="tix_notify_tickets[<?php the_ID(); ?>]" value="1" /> <?php echo sanitize_text_field( get_the_title() ) ; ?></label><br /> |
| 155 | <?php endwhile; ?> |
| 156 | </td> |
| 157 | </tr> |
| 158 | <tr> |
| 159 | <th scope="row"><?php _e( 'Subject', 'camptix' ); ?></th> |
| 160 | <td> |
| 161 | <input type="text" name="tix_notify_subject" value="<?php echo esc_attr( $form_data['subject'] ); ?>" class="large-text" /> |
| 162 | </td> |
| 163 | </tr> |
| 164 | <tr> |
| 165 | <th scope="row"><?php _e( 'Message', 'camptix' ); ?></th> |
| 166 | <td> |
| 167 | <textarea rows="10" name="tix_notify_body" id="tix-notify-body" class="large-text"><?php echo esc_textarea( $form_data['body'] ); ?></textarea><br /> |
| 168 | <?php if ( ! empty( $shortcode_tags ) ) : ?> |
| 169 | <p class=""><?php _e( 'You can use the following shortcodes:', 'camptix' ); ?> |
| 170 | <?php foreach ( $shortcode_tags as $key => $tag ) : ?> |
| 171 | <a href="#" class="tix-notify-shortcode"><code>[<?php echo esc_html( $key ); ?>]</code></a> |
| 172 | <?php endforeach; ?> |
| 173 | </p> |
| 174 | <?php endif; ?> |
| 175 | </td> |
| 176 | </tr> |
| 177 | <?php if ( isset( $_POST['tix_notify_preview'], $form_data ) ) : ?> |
| 178 | <?php |
| 179 | |
| 180 | $true = 0; |
| 181 | |
| 182 | $attendees = get_posts( array( |
| 183 | 'post_type' => 'tix_attendee', |
| 184 | 'posts_per_page' => -1, |
| 185 | 'post_status' => array( 'failed', 'timeout' ), |
| 186 | 'fields' => 'ids', // ! no post objects |
| 187 | 'orderby' => 'ID', |
| 188 | 'order' => 'DESC', |
| 189 | 'cache_results' => false, // no caching |
| 190 | ) ) ; |
| 191 | |
| 192 | $duplicate = 1; // For tracking if the User has already been mailed once |
| 193 | |
| 194 | foreach ($attendees as $attendee_id){ |
| 195 | |
| 196 | $attendee_ids = new WP_Query( array( |
| 197 | 'post_type' => 'tix_email', |
| 198 | 'post_status' => array( 'publish', 'pending' ), |
| 199 | 'posts_per_page' => -1, |
| 200 | 'relation' => 'AND', |
| 201 | 'meta_key' => 'tix_email_recipient_id', |
| 202 | 'meta_value' => $attendee_id, |
| 203 | 'meta_compare' => '=', |
| 204 | |
| 205 | ) ); |
| 206 | |
| 207 | if ( $attendee_ids->post_count == 0 ) { |
| 208 | $duplicate = 0; |
| 209 | break; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | $camptix->tmp( 'attendee_id', $attendee_id ); |
| 214 | |
| 215 | $subject = do_shortcode( $form_data['subject'] ); |
| 216 | $content = do_shortcode( $form_data['body'] ); |
| 217 | |
| 218 | $camptix->tmp( 'attendee_id', false ); |
| 219 | ?> |
| 220 | <tr> |
| 221 | <th scope="row">Preview</th> |
| 222 | <?php if ( $duplicate == 1 ) echo "<td>No New Failed Registration attendees found ! Cannot Display Preview ! </td>"; elseif( $duplicate == 0 ) { ?> |
| 223 | <td> |
| 224 | <div id="tix-notify-preview"> |
| 225 | <p><strong><?php echo esc_html( $subject ); ?></strong></p> |
| 226 | <p style="margin-bottom: 0;"><?php echo nl2br( esc_html( $content ) ); ?></p> |
| 227 | </div> |
| 228 | </td> |
| 229 | </tr> |
| 230 | <?php } endif;?> |
| 231 | </tbody> |
| 232 | </table> |
| 233 | <p class="submit"> |
| 234 | <?php wp_nonce_field( 'tix_notify_attendees' ); ?> |
| 235 | <input type="hidden" name="tix_notify_attendees" value="1" /> |
| 236 | |
| 237 | <div style="position: absolute; left: -9999px;"> |
| 238 | <?php /* Hit Preview, not Send, if the form is submitted with Enter. */ ?> |
| 239 | <?php submit_button( __( 'Preview', 'camptix' ), 'button', 'tix_notify_preview', false ); ?> |
| 240 | </div> |
| 241 | <?php submit_button( __( 'Send E-mails', 'camptix' ), 'primary', 'tix_notify_submit', false ); ?> |
| 242 | <?php submit_button( __( 'Preview', 'camptix' ), 'button', 'tix_notify_preview', false ); ?> |
| 243 | </p> |
| 244 | </form> |
| 245 | |
| 246 | <?php |
| 247 | |
| 248 | // Bring back the original shortcodes. |
| 249 | $shortcode_tags = $camptix->removed_shortcodes; |
| 250 | $camptix->removed_shortcodes = array(); |
| 251 | |
| 252 | $history_query = new WP_Query( array( |
| 253 | 'post_type' => 'tix_email', |
| 254 | 'post_status' => 'any', |
| 255 | 'posts_per_page' => -1, |
| 256 | 'order' => 'ASC', |
| 257 | ) ); |
| 258 | |
| 259 | if ( $history_query->have_posts() ) { |
| 260 | echo '<h3>' . __( 'History', 'camptix' ) . '</h3>'; |
| 261 | $rows = array(); |
| 262 | while ( $history_query->have_posts() ) { |
| 263 | $history_query->the_post(); |
| 264 | $rows[] = array( |
| 265 | __( 'Subject', 'camptix' ) => get_the_title(), |
| 266 | __( 'Updated', 'camptix' ) => sprintf( __( '%1$s at %2$s', 'camptix' ), get_the_date(), get_the_time() ), |
| 267 | __( 'Author', 'camptix' ) => get_the_author(), |
| 268 | __( 'Status', 'camptix' ) => $post->post_status, |
| 269 | ); |
| 270 | } |
| 271 | $camptix->table( $rows, 'widefat tix-email-history' ); |
| 272 | } |
| 273 | |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | // Register this class as a CampTix Addon. |
| 278 | camptix_register_addon( 'CampTix_Addon_Notify_Failed' ); |