Making WordPress.org

Ticket #574: patch_574_meta.diff

File patch_574_meta.diff, 10.0 KB (added by bansod_deven, 10 years ago)

Patch for #574 Meta

  • trunk/camptix.php

     
    19771977                                        $this->menu_tools_export();
    19781978                                elseif ( $section == 'notify' )
    19791979                                        $this->menu_tools_notify();
     1980                                elseif ( $section == 'notify_failed' )
     1981                                        $this->menu_tools_notify_failed();
    19801982                                elseif ( $section == 'refund' && ! $this->options['archived'] )
    19811983                                        $this->menu_tools_refund();
    19821984                                else
     
    20072009                        'revenue' => __( 'Revenue', 'camptix' ),
    20082010                        'export' => __( 'Export', 'camptix' ),
    20092011                        'notify' => __( 'Notify', 'camptix' ),
     2012                        'notify_failed' => __( 'Notify Failed Registrations', 'camptix' ),
    20102013                ) );
    20112014
    20122015                if ( current_user_can( $this->caps['refund_all'] ) && ! $this->options['archived'] && $this->options['refund_all_enabled'] )
     
    27632766
    27642767        /**
    27652768         * Notify tools menu, allows to create, preview and send an e-mail
    2766          * to all attendees. See also: notify shortcodes.
     2769         * to the attendees with Failed or Timed Out Registration. See also: notify shortcodes.
    27672770         */
     2771        function menu_tools_notify_failed() {
     2772                global $post, $shortcode_tags;
     2773
     2774                // Use this array to store existing form data.
     2775                $form_data = array(
     2776                        'subject' => '',
     2777                        'body' => '',
     2778                        'tickets' => array(),
     2779                );
     2780
     2781                if ( isset( $_POST['tix_notify_attendees'] ) && check_admin_referer( 'tix_notify_attendees' ) ) {
     2782                        $errors = array();
     2783
     2784                        // Error handling.
     2785                        if ( empty( $_POST['tix_notify_subject'] ) )
     2786                                $errors[] = __( 'Please enter a subject line.', 'camptix' );
     2787
     2788                        if ( empty( $_POST['tix_notify_body'] ) )
     2789                                $errors[] = __( 'Please enter the e-mail body.', 'camptix' );
     2790
     2791                        if ( ! isset( $_POST['tix_notify_tickets'] ) || count( (array) $_POST['tix_notify_tickets'] ) < 1 )
     2792                                $errors[] = __( 'Please select at least one ticket group.', 'camptix' );
     2793
     2794                        // If everything went well.
     2795                        if ( count( $errors ) == 0 && isset( $_POST['tix_notify_submit'] ) && $_POST['tix_notify_submit'] ) {
     2796                                $tickets = array_map( 'absint', (array) $_POST['tix_notify_tickets'] );
     2797                                $subject = sanitize_text_field( wp_kses_post( stripslashes( $_POST['tix_notify_subject'] ) ) );
     2798                                $body = wp_kses_post( stripslashes( $_POST['tix_notify_body'] ) );
     2799                                $recipients = array();
     2800
     2801                                $paged = 1;
     2802                                while ( $attendees = get_posts( array(
     2803                                        'post_type' => 'tix_attendee',
     2804                                        'posts_per_page' => 200,
     2805                                        'post_status' => array( 'failed', 'timeout' ),
     2806                                        'paged' => $paged++,
     2807                                        'fields' => 'ids', // ! no post objects
     2808                                        'orderby' => 'ID',
     2809                                        'order' => 'ASC',
     2810                                        'cache_results' => false, // no caching
     2811                                ) ) ) {
     2812
     2813                                        // Disables object caching, see Revenue report for details.
     2814                                        $this->filter_post_meta = $this->prepare_metadata_for( $attendees );
     2815
     2816                                        foreach ( $attendees as $attendee_id ) {
     2817                                                $emails_query = new WP_Query( array(
     2818                                                                'post_type' => 'tix_email',
     2819                                                                'post_status' => array( 'publish', 'pending' ),
     2820                                                                'posts_per_page' => -1,
     2821                                                                'relation' => 'AND',
     2822                                                                'meta_key' => 'tix_email_recipient_id',
     2823                                                                'meta_value' => $attendee_id,
     2824                                                                'meta_compare' => '=',
     2825                                                               
     2826                                                ) );
     2827
     2828                                                if ( array_key_exists( get_post_meta( $attendee_id, 'tix_ticket_id', true ), $tickets ) && $emails_query->post_count == 0 )
     2829                                                        $recipients[] = $attendee_id;
     2830                                        }
     2831                                        // Enable object caching for post meta back on.
     2832                                        $this->filter_post_meta = false;
     2833                                }
     2834
     2835                                unset( $attendees );
     2836
     2837                                // Create a new e-mail job.
     2838                                $email_id = wp_insert_post( array(
     2839                                        'post_type' => 'tix_email',
     2840                                        'post_status' => 'pending',
     2841                                        'post_title' => $subject,
     2842                                        'post_content' => $body,
     2843                                ) );
     2844
     2845                                // Add recipients as post meta.
     2846                                if ( $email_id ) {
     2847                                        add_settings_error( 'camptix', 'none', sprintf( __( 'Your e-mail job has been queued for %s recipients.', 'camptix' ), count( $recipients ) ), 'updated' );
     2848                                        $this->log( sprintf( 'Created e-mail job with %s recipients.', count( $recipients ) ), $email_id, null, 'notify' );
     2849
     2850                                        foreach ( $recipients as $recipient_id )
     2851                                                add_post_meta( $email_id, 'tix_email_recipient_id', $recipient_id );
     2852
     2853                                        update_post_meta( $email_id, 'tix_email_recipients_backup', $recipients ); // for logging purposes
     2854                                        unset( $recipients );
     2855                                }
     2856                        } else { // errors or preview
     2857
     2858                                if ( count( $errors ) > 0 )
     2859                                        foreach ( $errors as $error )
     2860                                                add_settings_error( 'camptix', false, $error );
     2861
     2862                                // Keep form data.
     2863                                $form_data['subject'] = wp_kses_post( stripslashes( $_POST['tix_notify_subject'] ) );
     2864                                $form_data['body'] = wp_kses_post( stripslashes( $_POST['tix_notify_body'] ) );
     2865                                if ( isset( $_POST['tix_notify_tickets'] ) )
     2866                                        $form_data['tickets'] = array_map( 'absint', (array) $_POST['tix_notify_tickets'] );
     2867                        }
     2868                }
     2869
     2870                // Remove all standard shortcodes.
     2871                $this->removed_shortcodes = $shortcode_tags;
     2872                remove_all_shortcodes();
     2873
     2874                do_action( 'camptix_init_notify_shortcodes' );
     2875                ?>
     2876                <?php settings_errors( 'camptix' ); ?>
     2877                <form method="post" action="<?php echo esc_url( add_query_arg( 'tix_notify_attendees', 1 ) ); ?>">
     2878                        <h3>(Only for Attendees with Registration Status as 'Failed' and 'Timeout' )</h3>
     2879                        <table class="form-table">
     2880                                <tbody>
     2881                                        <tr>
     2882                                                <th scope="row"><?php _e( 'To', 'camptix' ); ?></th>
     2883                                                <td>
     2884                                                        <?php
     2885                                                                $tickets_query = new WP_Query( array(
     2886                                                                        'post_type' => 'tix_ticket',
     2887                                                                        'post_status' => 'any',
     2888                                                                        'posts_per_page' => -1,
     2889                                                                ) );
     2890                                                        ?>
     2891                                                        <?php while ( $tickets_query->have_posts() ) : $tickets_query->the_post(); ?>
     2892                                                        <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 />
     2893                                                        <?php endwhile; ?>
     2894                                                </td>
     2895                                        </tr>
     2896                                        <tr>
     2897                                                <th scope="row"><?php _e( 'Subject', 'camptix' ); ?></th>
     2898                                                <td>
     2899                                                        <input type="text" name="tix_notify_subject" value="<?php echo esc_attr( $form_data['subject'] ); ?>" class="large-text" />
     2900                                                </td>
     2901                                        </tr>
     2902                                        <tr>
     2903                                                <th scope="row"><?php _e( 'Message', 'camptix' ); ?></th>
     2904                                                <td>
     2905                                                        <textarea rows="10" name="tix_notify_body" id="tix-notify-body" class="large-text"><?php echo esc_textarea( $form_data['body'] ); ?></textarea><br />
     2906                                                        <?php if ( ! empty( $shortcode_tags ) ) : ?>
     2907                                                        <p class=""><?php _e( 'You can use the following shortcodes:', 'camptix' ); ?>
     2908                                                                <?php foreach ( $shortcode_tags as $key => $tag ) : ?>
     2909                                                                <a href="#" class="tix-notify-shortcode"><code>[<?php echo esc_html( $key ); ?>]</code></a>
     2910                                                                <?php endforeach; ?>
     2911                                                        </p>
     2912                                                        <?php endif; ?>
     2913                                                </td>
     2914                                        </tr>
     2915                                        <?php if ( isset( $_POST['tix_notify_preview'], $form_data ) ) : ?>
     2916                                        <?php
     2917                                               
     2918                                                $true = 0;
     2919                                               
     2920                                                $attendees = get_posts( array(
     2921                                                        'post_type' => 'tix_attendee',
     2922                                                        'posts_per_page' => -1,
     2923                                                        'post_status' => array( 'failed', 'timeout' ),
     2924                                                        'fields' => 'ids', // ! no post objects
     2925                                                        'orderby' => 'ID',
     2926                                                        'order' => 'DESC',
     2927                                                        'cache_results' => false, // no caching
     2928                                                ) ) ;
     2929
     2930                                                $duplicate = 1; // For tracking if the Attendee has already been mailed
     2931                                               
     2932                                                foreach ($attendees as $attendee_id){
     2933                                                       
     2934                                                        $emails_query = new WP_Query( array(
     2935                                                                'post_type' => 'tix_email',
     2936                                                                'post_status' => array( 'publish', 'pending' ),
     2937                                                                'posts_per_page' => -1,
     2938                                                                'relation' => 'AND',
     2939                                                                'meta_key' => 'tix_email_recipient_id',
     2940                                                                'meta_value' => $attendee_id,
     2941                                                                'meta_compare' => '=',
     2942                                                               
     2943                                                        ) );
     2944
     2945                                                        if ( $email_query->post_count == 0 ) {
     2946                                                                $duplicate = 0;
     2947                                                                break;
     2948                                                        }
     2949                                                }
     2950                                               
     2951                                                        $this->tmp( 'attendee_id', $attendee_id );
     2952                                               
     2953                                                        $subject = do_shortcode( $form_data['subject'] );
     2954                                                        $content = do_shortcode( $form_data['body'] );
     2955                                                               
     2956                                                        $this->tmp( 'attendee_id', false );
     2957                                        ?>
     2958                                        <tr>
     2959                                                <th scope="row">Preview</th>
     2960                                                <?php if ( $duplicate == 1 ) echo "<td>No New Failed Registration attendees found ! Cannot Display Preview ! </td>"; elseif( $duplicate == 0 ) { ?>
     2961                                                <td>
     2962                                                        <div id="tix-notify-preview">
     2963                                                                <p><strong><?php echo esc_html( $subject ); ?></strong></p>
     2964                                                                <p style="margin-bottom: 0;"><?php echo nl2br( esc_html( $content ) ); ?></p>
     2965                                                        </div>
     2966                                                </td>
     2967                                        </tr>
     2968                                        <?php } endif;?>
     2969                                </tbody>
     2970                        </table>
     2971                        <p class="submit">
     2972                                <?php wp_nonce_field( 'tix_notify_attendees' ); ?>
     2973                                <input type="hidden" name="tix_notify_attendees" value="1" />
     2974
     2975                                <div style="position: absolute; left: -9999px;">
     2976                                        <?php /* Hit Preview, not Send, if the form is submitted with Enter. */ ?>
     2977                                        <?php submit_button( __( 'Preview', 'camptix' ), 'button', 'tix_notify_preview', false ); ?>
     2978                                </div>
     2979                                <?php submit_button( __( 'Send E-mails', 'camptix' ), 'primary', 'tix_notify_submit', false ); ?>
     2980                                <?php submit_button( __( 'Preview', 'camptix' ), 'button', 'tix_notify_preview', false ); ?>
     2981                        </p>
     2982                </form>
     2983
     2984                <?php
     2985
     2986                // Bring back the original shortcodes.
     2987                $shortcode_tags = $this->removed_shortcodes;
     2988                $this->removed_shortcodes = array();
     2989
     2990                $history_query = new WP_Query( array(
     2991                        'post_type' => 'tix_email',
     2992                        'post_status' => 'any',
     2993                        'posts_per_page' => -1,
     2994                        'order' => 'ASC',
     2995                ) );
     2996
     2997                if ( $history_query->have_posts() ) {
     2998                        echo '<h3>' . __( 'History', 'camptix' ) . '</h3>';
     2999                        $rows = array();
     3000                        while ( $history_query->have_posts() ) {
     3001                                $history_query->the_post();
     3002                                $rows[] = array(
     3003                                        __( 'Subject', 'camptix' ) => get_the_title(),
     3004                                        __( 'Updated', 'camptix' ) => sprintf( __( '%1$s at %2$s', 'camptix' ), get_the_date(), get_the_time() ),
     3005                                        __( 'Author', 'camptix' ) => get_the_author(),
     3006                                        __( 'Status', 'camptix' ) => $post->post_status,
     3007                                );
     3008                        }
     3009                        $this->table( $rows, 'widefat tix-email-history' );
     3010                }
     3011       
     3012        }
     3013
     3014
    27683015        function menu_tools_notify() {
    27693016                global $post, $shortcode_tags;
    27703017