function deliver_category_subscription($catid, $post) { global $wpdb; $getAllBlogSubscribersQuery = sprintf("SELECT a.* FROM {$wpdb->prefix}wpr_subscribers a,{$wpdb->prefix}wpr_blog_subscription b where b.type='cat' and b.catid=%d and a.id=b.sid and a.active=1 and a.confirmed=1", $catid); $subscribers = $wpdb->get_results($getAllBlogSubscribersQuery); foreach ($subscribers as $subscriber) { deliverBlogPost($subscriber->id, $post->ID); } }
function _wpr_process_blog_category_subscriptions() { global $wpdb; $prefix = $wpdb->prefix; set_time_limit(3600); //categories $args = array('type' => 'post', 'hide_empty' => 1, 'hierarchical' => 0, 'taxonomy' => 'category'); $categories = get_categories($args); //ENSURE THAT ANOTHER INSTANCE OF THIS PROCESS DOESN'T START $whetherRunning = get_option("_wpr_blog_category_delivery_processor_state"); $timeNow = time(); $timeStamp = intval($whetherRunning); $timeSinceStart = $timeNow - $timeStamp; if ($timeSinceStart < 3600 && $whetherRunning != "off") { echo "Another process is running."; exit; } update_option("_wpr_blog_category_delivery_processor_state", $timeNow); //END ENSURING THAT ANOTHER INSTANCE DOESN'T START foreach ($categories as $category) { //now process the people who subscribe to the blog $timeStampForNow = date("Y-m-d H:i:s"); $getLatestPostQuery = sprintf("SELECT a.* FROM {$wpdb->prefix}posts a, {$wpdb->prefix}term_relationships b WHERE a.ID=b.`object_id` AND b.`term_taxonomy_id`=%d AND a.`post_type`='post' AND a.`post_status`='publish' AND a.`post_date_gmt` < '%s' AND a.post_password='' ORDER BY a.`post_date_gmt` DESC LIMIT 1;", $category->term_id, $timeStampForNow); $latestBlogPostResult = $wpdb->get_results($getLatestPostQuery); if (0 == count($latestBlogPostResult)) { //there aren't any blog posts at all. continue; } //find the latest published, non-password protected, past dated blog post $latestBlogPostTimestamp = strtotime($latestBlogPostResult[0]->post_date_gmt); $getNumberOfSubscriptions = sprintf("SELECT COUNT(*) number FROM %swpr_blog_subscription b,\n %swpr_subscribers s\n WHERE b.type='cat' AND \n b.catid=%d AND\n (b.last_published_post_date< %d OR\n b.pending_reprocess=1) AND\n s.id=b.sid AND\n s.active=1 AND\n s.confirmed=1;", $wpdb->prefix, $wpdb->prefix, $category->term_id, $latestBlogPostTimestamp); $getCountRes = $wpdb->get_results($getNumberOfSubscriptions); $number = $getCountRes[0]->number; $perIteration = intval(WPR_MAX_BLOG_SUBSCRIPTION_PROCESSED_PER_ITERATION); $perIteration = 0 == $perIteration ? 100 : $perIteration; $numberOfIterations = ceil($number / $perIteration); for ($iter = 0; $iter < $numberOfIterations; $iter++) { $getSubscriptionsQuery = sprintf("SELECT COUNT(*) number FROM %swpr_blog_subscription b,\n %swpr_subscribers s\n WHERE b.type='cat' AND \n b.catid=%d AND\n (b.last_published_post_date< %d OR\n b.pending_reprocess=1) AND\n s.id=b.sid AND\n s.active=1 AND\n s.confirmed=1 ORDER BY last_published_post_date ASC, last_processed_date ASC LIMIT %d;", $wpdb->prefix, $wpdb->prefix, $category->term_id, $latestBlogPostTimestamp, $perIteration); $blogSubscriptionsToProcess = $wpdb->get_results($getSubscriptionsQuery); foreach ($blogSubscriptionsToProcess as $subscription) { //the first thing to do: update last processed date. $updateLastProcessedDateQuery = sprintf("UPDATE `%swpr_blog_subscription` SET `last_processed_date`=%d WHERE id=%d", $wpdb->prefix, time(), $subscription->id); $wpdb->query($updateLastProcessedDateQuery); $post = _wpr_blog_subscription_get_category_post_to_deliver($subscription); if ($post == false) { continue; } $footerMessage = sprintf(__("You are receiving this email because you are subscribed to articles published at <a href=\"%s\">%s</a>", 'wpr_autoresponder'), $blogURL, $blogName); $postId = (int) $post->ID; if ($postId == 0) { continue; } deliverBlogPost($subscription->sid, $postId, $footerMessage); $publishTime = strtotime($post->post_date_gmt); //update the subscription to this post $updateSubscriptionQuery = sprintf("UPDATE `%swpr_blog_subscription` \n SET `last_published_postid`=%d, \n `last_published_post_date`='%s' \n WHERE id=%d", $wpdb->prefix, $postId, $publishTime, $subscription->id); $wpdb->query($updateSubscriptionQuery); } } } update_option("_wpr_blog_category_delivery_processor_state", "off"); }
function deliver_category_subscription($catid, $post) { global $wpdb; $prefix = $wpdb->prefix; $query = "SELECT a.* FROM " . $prefix . "wpr_subscribers a," . $prefix . "wpr_blog_subscription b where b.type='cat' and b.catid='{$catid}' and a.id=b.sid and a.active=1 and a.confirmed=1"; $subscribers = $wpdb->get_results($query); $theCategory = get_category($catid); $categoryName = $categoryname->name; $blogName = get_bloginfo("name"); $blogURL = get_bloginfo("siteurl"); $footerMessage = "You are receiving this e-mail because you have subscribed to the {$categoryName} category of {$blogName}\r\n\r\n{$blogUrl}"; foreach ($subscribers as $subscriber) { deliverBlogPost($subscriber->id, $post->ID); } }