Exemplo n.º 1
0
function custom_auto_mail_send()
{
    global $wpdb;
    global $sola_nl_camp_tbl;
    global $sola_nl_subs_tbl;
    global $sola_global_campid;
    global $sola_global_subid;
    //    echo 'Running';
    /*
     * Check to see if there are any auto posts available
     */
    $sql = "SELECT * FROM {$sola_nl_camp_tbl} WHERE `type` = 2 AND `action` = 3";
    $results = $wpdb->get_results($sql);
    if ($results) {
        foreach ($results as $result) {
            $auto_camp_id = $result->camp_id;
            //            echo "<h1>$auto_camp_id</h1>";
            $auto_data = $result->automatic_data;
            $raw_auto_data = maybe_unserialize($auto_data);
            // if not immediate but automatic post, create new campaign
            if ($raw_auto_data['type'] != 5 && $raw_auto_data['action'] == 3) {
                //                    var_dump("type != 5 action = 3");
                $current_time = date('Y-m-d H:i:s', current_time('timestamp'));
                $scheduled_time = $raw_auto_data['automatic_scheduled_date'];
                if ($current_time >= $scheduled_time && $scheduled_time != '0000-00-00 00:00:00') {
                    echo 'Running Because Time Is Less Than Current';
                    $are_there_new_posts_to_email = sola_nl_check_if_new_posts_exist($auto_camp_id, $raw_auto_data['automatic_options_posts'], $raw_auto_data['automatic_scheduled_date']);
                    if ($are_there_new_posts_to_email != null) {
                        $auto_camp_id = $result->camp_id;
                        $auto_data = $result->automatic_data;
                        $raw_auto_data = maybe_unserialize($auto_data);
                        //                    var_dump($raw_auto_data);
                        $camp = sola_nl_get_camp_details($auto_camp_id);
                        $lists = sola_nl_get_camp_lists($auto_camp_id);
                        //                            var_dump($lists);
                        foreach ($lists as $list) {
                            $list_id = $list->list_id;
                            $new_lists[] = $list_id;
                            //                        var_dump($new_lists);
                        }
                        $type = 1;
                        /* insert it as a normal campaign */
                        $letter = sola_nl_get_letter($auto_camp_id);
                        //                    echo $letter;
                        //                    exit();
                        $inserted_data = sola_nl_build_automatic_content($auto_camp_id, true);
                        //                    echo $inserted_data;
                        //                    $sola_global_campid = $auto_camp_id;
                        //                            var_dump("CAMP ID ". $auto_camp_id);
                        //                    exit();
                        $table = preg_replace('/<table id="sola_nl_automatic_container"(.*?)<\\/table>/is', $inserted_data, $letter);
                        //                    $table = do_shortcode($table);
                        echo $table;
                        $subject = do_shortcode($camp->subject);
                        $wpdb->insert($sola_nl_camp_tbl, array('camp_id' => '', 'subject' => $subject, 'theme_id' => $camp->theme_id, 'email' => $table, 'styles' => $camp->styles, 'type' => $type));
                        $camp_id = $wpdb->insert_id;
                        //                            var_dump("NEW CAMP ID = ".$camp_id);
                        global $sola_global_campid;
                        $sola_global_campid = $camp_id;
                        //$working_table = do_shortcode($table);
                        //echo $working_table;
                        //                    echo 'Doing Update Below This';
                        //$wpdb->update($sola_nl_camp_tbl, array('email' => $working_table), array('camp_id' => $camp_id));
                        $checker = sola_nl_finish_camp(3, $current_time, $camp->sent_from, $camp->sent_from_name, $camp->reply_to, $camp->reply_to_name, $camp_id, $new_lists, $auto = true);
                        //                            print_r($new_lists);
                        var_dump('This will be the new campaign id ' . $camp_id);
                        //                    exit();
                        if (is_wp_error($checker)) {
                            sola_return_error($checker);
                        }
                        /* we need to have a check here to see if this works or not */
                        if (function_exists('sola_nl_add_subs_to_camp_pro')) {
                            sola_nl_add_subs_to_camp_pro($camp_id);
                        } else {
                            sola_nl_add_subs_to_camp($camp_id);
                        }
                    } else {
                        /* there are no new posts during this scheduled run */
                        var_dump("there are no new posts to send in this scheduled run");
                    }
                }
                /* set the next scheduled run */
                $the_current_time = current_time('timestamp');
                var_dump("current time: " . date("Y-m-d H:i:s", $the_current_time));
                $type = $raw_auto_data['type'];
                $day = $raw_auto_data['data']['day'];
                var_dump($day);
                $custom_time = $raw_auto_data['data']['time'];
                var_dump($custom_time);
                //                echo 'The time here';
                //                $the_current_time = strtotime('2014-10-08 08:30:00');
                if ($type == 1) {
                    //Daily
                    if (strtotime(date("Y-m-d", $the_current_time) . " " . $custom_time . ":00") > $the_current_time) {
                        var_dump("current time less than time. set it for today");
                        $time = date('Y-m-d H:i:s', strtotime(date("Y-m-d", $the_current_time) . " " . $custom_time . ":00"));
                    } else {
                        var_dump("current time greater than time, set it for next time");
                        $time = date('Y-m-d H:i:s', strtotime('+1 day ' . $custom_time . ':00', $the_current_time));
                    }
                } else {
                    if ($type == 2) {
                        //Weekly
                        if ($day == 1) {
                            $chosen_day = 'Monday';
                        } else {
                            if ($day == 2) {
                                $chosen_day = 'Tuesday';
                            } else {
                                if ($day == 3) {
                                    $chosen_day = 'Wednesday';
                                } else {
                                    if ($day == 4) {
                                        $chosen_day = 'Thursday';
                                    } else {
                                        if ($day == 5) {
                                            $chosen_day = 'Friday';
                                        } else {
                                            if ($day == 6) {
                                                $chosen_day = 'Saturday';
                                            } else {
                                                if ($day == 7) {
                                                    $chosen_day = 'Sunday';
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        $time = date('Y-m-d H:i:s', strtotime("+1 Week " . $chosen_day . ' ' . $custom_time . ':00', $the_current_time));
                    } else {
                        $time = date('Y-m-d H:i:s', current_time('timestamp'));
                    }
                }
                //                echo $time;
                $new_automatic_data = array('type' => $raw_auto_data['type'], 'action' => $raw_auto_data['action'], 'data' => array('day' => $raw_auto_data['data']['day'], 'time' => $raw_auto_data['data']['time'], 'daynum' => $raw_auto_data['data']['daynum'], 'timeafter' => $raw_auto_data['data']['timeafter'], 'timeqty' => $raw_auto_data['data']['timeqty'], 'role' => $raw_auto_data['data']['role']), 'automatic_layout' => $raw_auto_data['automatic_layout'], 'automatic_options_posts' => $raw_auto_data['automatic_options_posts'], 'automatic_options_columns' => $raw_auto_data['automatic_options_columns'], 'automatic_image' => $raw_auto_data['automatic_image'], 'automatic_author' => $raw_auto_data['automatic_author'], 'automatic_title' => $raw_auto_data['automatic_title'], 'automatic_content' => $raw_auto_data['automatic_content'], 'automatic_readmore' => $raw_auto_data['automatic_readmore'], 'automatic_scheduled_date' => $time, 'automatic_post_date' => $raw_auto_data['automatic_post_date'], 'automatic_post_length' => $raw_auto_data['automatic_post_length']);
                $serialized_auto_data = maybe_serialize($new_automatic_data);
                $wpdb->update($sola_nl_camp_tbl, array('automatic_data' => $serialized_auto_data), array('camp_id' => $auto_camp_id));
                var_dump("Next scheduled date set to " . $time);
            }
        }
    }
    /*
     * Check to see if there are any subscribers that need to be emailed.
     */
    $sql = "SELECT * FROM {$sola_nl_subs_tbl} WHERE `status` = 1 AND `sola_nl_mail_sent` = 0";
    $sub_results = $wpdb->get_results($sql);
    //    var_dump($sub_results);
    if ($sub_results) {
        foreach ($sub_results as $sub) {
            $sub_id = $sub->sub_id;
            $user_email = $sub->sub_email;
            if ($sub->sub_name == '') {
                $user_name = $sub->sub_email;
            } else {
                $user_name = $sub->sub_name;
            }
            $user_mail_sent = $sub->sola_nl_mail_sent;
            $user_mail_send_time = $sub->sola_nl_mail_sending_time;
            if ($user_mail_sent != '' && $user_mail_send_time != '' && $user_mail_send_time != '0000-00-00 00:00:00') {
                $current_time = date('Y-m-d H:i:s', current_time('timestamp'));
                $sql = "SELECT * FROM {$sola_nl_camp_tbl} WHERE `type` = 2 AND `action` = 4";
                $results = $wpdb->get_results($sql);
                //                var_dump($results);
                if ($results) {
                    foreach ($results as $result) {
                        if (isset($result->camp_id)) {
                            $camp_id = $result->camp_id;
                        } else {
                            $camp_id = '';
                        }
                        $sola_global_campid = $camp_id;
                        $sql = "SELECT * FROM {$sola_nl_subs_tbl} WHERE `sub_email` = '{$user_email}'";
                        $subscriber_details = $wpdb->get_row($sql);
                        $sub_id = $subscriber_details->sub_id;
                        $sola_global_subid = $sub_id;
                        if (isset($user_email)) {
                            $to = $user_email;
                        } else {
                            $to = '';
                        }
                        if (isset($result->subject)) {
                            $subject = stripslashes(do_shortcode($result->subject));
                        } else {
                            $subject = '';
                        }
                        if (isset($result->email)) {
                            $body_contents = stripslashes(do_shortcode(trim($result->email)));
                        } else {
                            $body_contents = '';
                        }
                        //                        var_dump($result->email);
                        //                        echo SITE_URL;
                        //                        echo $body_contents;
                        //                        exit();
                        $body = sola_nl_mail_body($body_contents, $camp_id, $camp_id);
                        if (isset($result->styles)) {
                            $styles = $result->styles;
                        } else {
                            $styles = '';
                        }
                    }
                    if ($current_time >= $user_mail_send_time && $user_mail_sent == 0) {
                        $mail_sent = sola_mail($camp_id, $to, $subject, $body);
                        if ($mail_sent) {
                            $wpdb->update($sola_nl_subs_tbl, array('sola_nl_mail_sent' => 1), array('sub_id' => $sub_id));
                        }
                    }
                }
            }
        }
    }
    /*
     * Check to see if there are any users that need to be emailed.
     */
    $args = array('meta_key' => '_sola_nl_mail_sent', 'meta_value' => '0');
    $users = get_users($args);
    foreach ($users as $user) {
        $user_id = $user->ID;
        $user_email = $user->data->user_email;
        $user_name = $user->data->display_name;
        $user_role = $user->roles[0];
        $sql = "SELECT * FROM {$sola_nl_camp_tbl} WHERE `type` = 2 AND `action` = 5";
        $results = $wpdb->get_results($sql);
        //        var_dump("USERS");
        //        var_dump($results);
        foreach ($results as $result) {
            $auto_camp_id = $result->camp_id;
            $auto_data = $result->automatic_data;
            $raw_auto_data = maybe_unserialize($auto_data);
            $role = intval($raw_auto_data['data']['role']);
            if ($role == 1 || $role == 2 && $user_role == 'administrator' || $role == 3 && $user_role == 'editor' || $role == 4 && $user_role == 'author' || $role == 5 && $user_role == 'contributor' || $role == 6 && $user_role == 'subscriber') {
                $user_mail_sent = get_user_meta($user_id, '_sola_nl_mail_sent', true);
                $user_mail_send_time = get_user_meta($user_id, '_sola_nl_mail_sending_time', true);
                if ($user_mail_sent != '' && $user_mail_send_time != '') {
                    $current_time = date('Y-m-d H:i:s', current_time('timestamp'));
                    $user_meta = get_user_meta($user_id);
                    $user_data = get_userdata($user_id);
                    $user_role = $user_data->roles[0];
                    $sql = "SELECT * FROM {$sola_nl_camp_tbl} WHERE `type` = 2 AND `action` = 5";
                    $results = $wpdb->get_results($sql);
                    if ($results) {
                        foreach ($results as $result) {
                            $camp_id = $result->camp_id;
                            //                            echo $camp_id;
                            //                            global $sola_global_campid;
                            $sola_global_campid = $camp_id;
                            $to = $user_email;
                            $subject = do_shortcode($result->subject);
                            $body_contents = stripslashes(do_shortcode(trim($result->email)));
                            //                            echo $body_contents;
                            $body = sola_nl_mail_body($body_contents, $user_id, $camp_id);
                            $styles = $result->styles;
                        }
                        if ($current_time >= $user_mail_send_time && $user_mail_sent == 0) {
                            $mail_sent = sola_mail($camp_id, $to, $subject, $body);
                            if ($mail_sent) {
                                update_user_meta($user_id, '_sola_nl_mail_sent', 1);
                            }
                        }
                    }
                }
            }
        }
    }
}
Exemplo n.º 2
0
<?php

if (isset($_GET['camp_id'])) {
    $camp = sola_nl_get_camp_details($_GET['camp_id']);
    $letter = sola_nl_get_letter($_GET['camp_id']);
    global $wpdb;
    global $sola_nl_camp_tbl;
    $wpdb->insert($sola_nl_camp_tbl, array('camp_id' => '', 'subject' => $camp->subject, 'theme_id' => $camp->theme_id, 'email' => $letter));
    $camp_id = $wpdb->insert_id;
} else {
    exit;
}
?>
<div class="wrap">    
   <div id="icon-edit" class="icon32 icon32-posts-post"><br></div>
   <h2><?php 
_e("Create a New Campaign", "sola");
?>
</h2>
   <div>
      <form action="" method="POST">
          <input type="hidden" value="<?php 
if ($camp_id) {
    echo $camp_id;
}
?>
" name="camp_id" />
         <table>
            <tr>
               <td width="250px">
                  <label><h3>Subject</h3></label>
Exemplo n.º 3
0
echo get_option('admin_email');
?>
" class="sola-input" id="sola_nl_to_mail_test" />
            <button class="button-primary sola_send_preview"><?php 
_e("Send Test", "sola");
?>
</button>
            
        </div>
    </div>  
    
</div>

<div id="sola_newsletter_preview"> 
        <?php 
$letter = sola_nl_get_letter($_GET['camp_id'], $theme_id);
echo $letter;
?>
</div>



<?php 
/* 
 * <div style='display:block;  clear:both; width:602px;'>


        </div>