Example #1
0
 /**
  * Generate a message preview (called with AJAX)
  * 
  * @param string $from_name
  * @param string $from_email
  * @param string $subject
  * @param string $body
  */
 public function preview($from_name, $from_email, $subject, $body)
 {
     // create a virtual Thank Me Later message
     $message = new Bbpp_ThankMeLater_Message();
     $message->addMessage(array("from_name" => $from_name, "from_email" => $from_email, "subject" => $subject, "message" => $body));
     $post_id = get_option("bbpp_thankmelater_preview_post_id");
     $comment = null;
     $current_user = wp_get_current_user();
     // create a virtual post
     if (!$post_id || !get_post($post_id)) {
         $post_id = wp_insert_post(array("post_author" => $current_user->ID, "post_content" => __("This is a sample post created by Thank Me Later.", "bbpp-thankmelater"), "post_status" => "bbpp-thankmelater-pv", "post_title" => __("Sample post title", "bbpp-thankmelater"), "post_type" => "post", "post_date_gmt" => gmdate("Y-m-d H:i:s")));
         update_option("bbpp_thankmelater_preview_post_id", $post_id);
     } else {
         $comment_id = get_option("bbpp_thankmelater_preview_comment_id");
         $comment = get_comment($comment_id);
     }
     // create a virtual comment
     if (!$comment || !get_post($comment->comment_post_ID)) {
         $comment_id = wp_insert_comment(array("comment_post_ID" => $post_id, "comment_author" => _x("John Doe", "placeholder name", "bbpp-thankmelater"), "comment_author_email" => _x("*****@*****.**", "placeholder e-mail", "bbpp-thankmelater"), "comment_content" => __("This is a sample comment used by Thank Me Later. It is not displayed on any blog posts.", "bbpp-thankmelater"), "comment_author_IP" => "127.0.0.1", "comment_approved" => "1", "comment_agent" => "Firefox", "comment_date" => date("Y-m-d H:i:s"), "comment_date_gmt" => gmdate("Y-m-d H:i:s"), "user_id" => $current_user->ID));
         $comment = get_comment($comment_id);
         update_option("bbpp_thankmelater_preview_comment_id", $comment_id);
     } else {
         $comment = get_comment($comment_id);
     }
     $response = array("from" => $message->getParsedFrom($comment), "subject" => $message->getParsedSubject($comment));
     $response["message"] = array("text" => $message->getParsedPlainMessage($comment), "html" => $message->getParsedHtmlMessage($comment));
     echo json_encode($response);
     exit;
 }