Exemplo n.º 1
0
function post_notification_send()
{
    global $wpdb, $timestart;
    if (get_option('post_notification_lock') == 'db') {
        if (!$wpdb->get_var("SELECT GET_LOCK('" . $wpdb->prefix . 'post_notification_lock' . "', 0)")) {
            return;
        }
    } else {
        $mutex = @fopen(POST_NOTIFICATION_PATH . '_temp/post_notification.lock', 'w');
        if (!$mutex || !flock($mutex, LOCK_EX | LOCK_NB, $eWouldBlock) || $eWouldBlock) {
            // There is already someone mailing.
            @fclose($mutex);
            return;
        }
    }
    //Make sure plugins don't think we're a page or something....
    $GLOBALS['wp_query']->init_query_flags();
    ignore_user_abort(true);
    //Let's get this done....
    //some general stuff
    $t_emails = $wpdb->prefix . 'post_notification_emails';
    $t_posts = $wpdb->prefix . 'post_notification_posts';
    $t_cats = $wpdb->prefix . 'post_notification_cats';
    $posts = $wpdb->get_results("SELECT id, notification_sent " . post_notification_sql_posts_to_send());
    if (!$posts) {
        //This shouldn't happen, but never mind.
        post_notification_set_next_send();
        return;
        //Nothing to do.
    }
    //Include user functions, if they exist.
    //We don't want an error if the file does not exist.
    if (file_exists(POST_NOTIFICATION_PATH . 'userfunctions.php')) {
        include_once POST_NOTIFICATION_PATH . 'userfunctions.php';
    }
    // Mail out mails
    $maxsend = get_option('post_notification_maxsend');
    $mailssent = -1;
    $endtime = ini_get('max_execution_time');
    if ($endtime != 0) {
        $endtime += floor($timestart) - 5;
        //Make shure we will have a least 5 sek left.
    }
    $time_remain = 1;
    foreach ($posts as $post) {
        if (get_option('post_notification_debug') == 'yes') {
            echo '<hr />Sending post: ' . $post->id . '<br />';
        }
        //Find the categories
        if (get_option('db_version') < 6124) {
            $cats = $wpdb->get_results("SELECT category_id FROM " . $wpdb->post2cat . " WHERE post_id = " . $post->id);
        } else {
            $cats = $wpdb->get_results("SELECT term_id \r\n\t\t\t\t\t\t\t\t\t\tFROM " . $wpdb->term_relationships . " \r\n\t\t\t\t\t\t\t\t\t\tJOIN " . $wpdb->term_taxonomy . " USING (term_taxonomy_id)\r\n\t\t\t\t\t\t\t\t\t\tWHERE taxonomy = 'category' AND object_id = " . $post->id);
        }
        $cat_ids = array();
        foreach ($cats as $cat) {
            if (get_option('db_version') < 6124) {
                $last_cat = $cat->category_id;
            } else {
                $last_cat = $cat->term_id;
            }
            while ($last_cat != 0) {
                $cat_ids[] = (string) $last_cat;
                if (get_option('db_version') < 6124) {
                    $last_cat = $wpdb->get_var("SELECT category_parent FROM " . $wpdb->categories . " WHERE cat_ID = {$last_cat}");
                } else {
                    $last_cat = $wpdb->get_var("SELECT parent FROM " . $wpdb->term_taxonomy . " \r\n\t\t\t\t\t\t\t\t\t\t\t\tWHERE term_id = {$last_cat} AND taxonomy = 'category'");
                }
            }
        }
        $cat_ids[] = (string) $last_cat;
        $cat_ids = implode(", ", $cat_ids);
        //convert to string
        if (get_option('post_notification_debug') == 'yes') {
            echo 'The cat-Ids are: ' . $cat_ids . '<br />';
        }
        // Get the Emailadds
        $emails = $wpdb->get_results(" SELECT e.email_addr, e.id, e.act_code" . " FROM {$t_emails} e, {$t_cats} c " . " WHERE c.cat_id IN ({$cat_ids}) AND c.id = e.id AND e.gets_mail = 1 AND e.id >= " . $post->notification_sent . " GROUP BY e.id " . " ORDER BY e.id ASC");
        //We need this. Otherwise we can't be shure whether we already have sent mail.
        if (get_option('post_notification_debug') == 'yes') {
            echo count($emails) . ' Emails were found.<br />';
        }
        if ($emails) {
            //Check wheater ther are any mails to send anything.
            //Get Data
            $maildata = post_notification_create_email($post->id);
            foreach ($emails as $email) {
                if (get_option('post_notification_debug') == 'yes') {
                    echo 'Sending mail to: ' . $email->email_addr . '<br />';
                }
                if ($endtime != 0) {
                    //if this is 0 we have as much time as we want.
                    $time_remain = $endtime - time();
                }
                if ($maxsend < 1 || $time_remain < 0) {
                    //Are we allowed to send any more mails?
                    $mailssent = $email->id;
                    //Save where we stoped
                    break;
                }
                post_notification_sendmail($maildata, $email->email_addr, $email->act_code);
                $maxsend--;
            }
        }
        // Update notification_sent to -1 (We're done)
        $wpdb->query(" UPDATE {$t_posts} " . " SET " . " notification_sent = " . $mailssent . " WHERE post_id = " . $post->id);
        if ($maxsend < 1) {
            break;
        }
        //We dont need to go on, if there's nothing to send.
    }
    update_option('post_notification_lastsend', time());
    post_notification_set_next_send();
    if (get_option('post_notification_lock') == 'db') {
        $wpdb->query("SELECT RELEASE_LOCK('" . $wpdb->prefix . 'post_notification_lock' . "')");
    } else {
        flock($mutex, LOCK_UN);
        fclose($mutex);
    }
}
Exemplo n.º 2
0
function post_notification_add($post_ID)
{
    global $wpdb;
    $post = get_post($post_ID);
    $t_posts = $wpdb->prefix . 'post_notification_posts';
    $notify = $_POST['post_notification_notify'];
    //The post came in some other way. Set to def.
    if ($notify == '') {
        $notify = 'def';
    }
    //Todo, userlevels
    $status = $wpdb->get_var("SELECT notification_sent FROM {$t_posts} WHERE post_ID = '{$post_ID}'");
    if ($notify == 'def' && !isset($status)) {
        //default is not to change
        if (get_option('db_version') < 4772) {
            if ($post->post_status == 'post') {
                $notify = get_option('post_notification_send_default');
            }
            if ($post->post_status == 'private') {
                $notify = get_option('post_notification_send_default');
            }
            if ($post->post_status == 'static') {
                $notify = get_option('post_notification_send_page');
            }
        } else {
            if ($post->post_type == 'post') {
                $notify = get_option('post_notification_send_default');
            }
            if ($post->post_type == 'post' && $post->post_status == 'private') {
                $notify = get_option('post_notification_send_default');
            }
            if ($post->post_type == 'page') {
                $notify = get_option('post_notification_send_page');
            }
        }
    }
    if ($notify == 'yes') {
        if (isset($status)) {
            $wpdb->query("UPDATE {$t_posts}  SET notification_sent = 0 WHERE post_id = " . $post_ID);
        } else {
            $wpdb->query("INSERT INTO {$t_posts}  (post_ID, notification_sent) VALUES ('{$post_ID}',  0)");
        }
    } else {
        if ($notify == 'no') {
            if ($status != -1) {
                //Mails are sent - no reason to change this
                if (isset($status)) {
                    $wpdb->query("UPDATE {$t_posts}  SET notification_sent = -2 WHERE post_id = " . $post_ID);
                } else {
                    $wpdb->query("INSERT INTO {$t_posts}  (post_ID, notification_sent) VALUES ('{$post_ID}',  -2)");
                }
            }
        }
    }
    // We should have an entry now, so lets write the time.
    $wpdb->query("UPDATE {$t_posts}  SET date_saved = '" . post_notification_date2mysql() . "' WHERE post_id = " . $post_ID);
    post_notification_set_next_send();
}