Exemplo n.º 1
0
 /**
  * Find targeted messages and schedule them to be sent to a particular commenter
  * 
  * @global type $wpdb
  * @param object $comment result of get_comment
  * @return array Array of \WP_Error objects, if any
  */
 public function scheduleMessages($comment)
 {
     global $wpdb;
     // time of comment
     $comment_time = strtotime($comment->comment_date_gmt . " GMT");
     // load the post
     $post_id = $comment->comment_post_ID;
     $post = get_post($post_id);
     $post_tags_full = get_the_tags($post->ID);
     $post_categories_full = get_the_category($post->ID);
     $post_tags_full = is_array($post_tags_full) ? $post_tags_full : array();
     $post_categories_full = is_array($post_categories_full) ? $post_categories_full : array();
     // post tags and category ids
     $post_tags = array_map(array($this, "_getPostTermId"), $post_tags_full);
     $post_categories = array_map(array($this, "_getPostTermId"), $post_categories_full);
     // we're going to schedule any e-mails which match our criteria...
     $schedule = new Bbpp_ThankMeLater_Schedule();
     // read in all messages
     $this->readMessages();
     $num_messages = $this->numMessages();
     for ($mid = 0; $mid < $num_messages; $mid++) {
         $this->selectMessage($mid);
         $data = $this->getMessage();
         // number of these messages sent/waiting to be sent to a particular commenter
         $num_schedules = $schedule->getNumSchedules($data["id"], $comment->comment_author_email);
         $targeted = FALSE;
         // no restrictions => target all posts
         if (!$data["target_tags"] && !$data["target_categories"] && !$data["target_posts"]) {
             $targeted = TRUE;
         }
         // we are in a targeted tag
         if ($data["target_tags"] && array_intersect($data["target_tags"], $post_tags)) {
             $targeted = TRUE;
         }
         // we are in a targeted categories
         if ($data["target_categories"] && array_intersect($data["target_categories"], $post_categories)) {
             $targeted = TRUE;
         }
         // we are a targeted post
         if ($data["target_posts"] && in_array($post->ID, $data["target_posts"])) {
             $targeted = TRUE;
         }
         // make sure we have not exceeded the send limit
         if ($data["max_sends_per_email"] > 0 && $num_schedules >= $data["max_sends_per_email"]) {
             $targeted = FALSE;
         }
         // make sure we haven't already scheduled an e-mail (don't send twice!)
         if ($schedule->findScheduleId($data["id"], $comment->comment_ID)) {
             $targeted = FALSE;
         }
         if ($targeted) {
             // send in future or now, if send time has passed -- this may
             // happen if comment has been on hold for approval for some time.
             // It's important we do this, as otherwise the scheduler may
             // believe that the message has failed to be sent and delete it.s
             $send_date_gmt = max($comment_time + $this->getDelaySeconds(), time());
             // schedule the send of this message to the commenter.
             $schedule->addSchedule(array("message_id" => $data["id"], "comment_id" => $comment->comment_ID, "send_date_gmt" => $send_date_gmt, "sent" => 0));
         }
     }
     // add to schedule
     return $schedule->save();
 }