function grunion_ajax_spam()
{
global $wpdb;
if (empty($_POST['make_it'])) {
return;
}
$post_id = (int) $_POST['post_id'];
check_ajax_referer('grunion-post-status-' . $post_id);
if (!current_user_can("edit_page", $post_id)) {
wp_die(__('You are not allowed to manage this item.', 'jetpack'));
}
require_once dirname(__FILE__) . '/grunion-contact-form.php';
$current_menu = '';
if (preg_match('|post_type=feedback|', $_POST['sub_menu'])) {
if (preg_match('|post_status=spam|', $_POST['sub_menu'])) {
$current_menu = 'spam';
} else {
if (preg_match('|post_status=trash|', $_POST['sub_menu'])) {
$current_menu = 'trash';
} else {
$current_menu = 'messages';
}
}
}
$post = get_post($post_id);
$post_type_object = get_post_type_object($post->post_type);
$akismet_values = get_post_meta($post_id, '_feedback_akismet_values', TRUE);
if ($_POST['make_it'] == 'spam') {
$post->post_status = 'spam';
$status = wp_insert_post($post);
wp_transition_post_status('spam', 'publish', $post);
do_action('contact_form_akismet', 'spam', $akismet_values);
} elseif ($_POST['make_it'] == 'ham') {
$post->post_status = 'publish';
$status = wp_insert_post($post);
wp_transition_post_status('publish', 'spam', $post);
do_action('contact_form_akismet', 'spam', $akismet_values);
$comment_author_email = $reply_to_addr = $message = $to = $headers = false;
$blog_url = parse_url(site_url());
// resend the original email
$email = get_post_meta($post_id, '_feedback_email', TRUE);
$content_fields = Grunion_Contact_Form_Plugin::parse_fields_from_content($post_id);
if (!empty($email) && !empty($content_fields)) {
if (isset($content_fields['_feedback_author_email'])) {
$comment_author_email = $content_fields['_feedback_author_email'];
}
if (isset($email['to'])) {
$to = $email['to'];
}
if (isset($email['message'])) {
$message = $email['message'];
}
if (isset($email['headers'])) {
$headers = $email['headers'];
} else {
$headers = 'From: "' . $content_fields['_feedback_author'] . '" <wordpress@' . $blog_url['host'] . ">\r\n";
if (!empty($comment_author_email)) {
$reply_to_addr = $comment_author_email;
} elseif (is_array($to)) {
$reply_to_addr = $to[0];
}
if ($reply_to_addr) {
$headers .= 'Reply-To: "' . $content_fields['_feedback_author'] . '" <' . $reply_to_addr . ">\r\n";
}
$headers .= "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"";
}
$subject = apply_filters('contact_form_subject', $content_fields['_feedback_subject']);
wp_mail($to, $subject, $message, $headers);
}
} elseif ($_POST['make_it'] == 'publish') {
if (!current_user_can($post_type_object->cap->delete_post, $post_id)) {
wp_die(__('You are not allowed to move this item out of the Trash.', 'jetpack'));
}
if (!wp_untrash_post($post_id)) {
wp_die(__('Error in restoring from Trash.', 'jetpack'));
}
} elseif ($_POST['make_it'] == 'trash') {
if (!current_user_can($post_type_object->cap->delete_post, $post_id)) {
wp_die(__('You are not allowed to move this item to the Trash.', 'jetpack'));
}
if (!wp_trash_post($post_id)) {
wp_die(__('Error in moving to Trash.', 'jetpack'));
}
}
$sql = "\n\t\tSELECT post_status,\n\t\t\tCOUNT( * ) AS post_count\n\t\tFROM `{$wpdb->posts}`\n\t\tWHERE post_type = 'feedback'\n\t\tGROUP BY post_status\n\t";
$status_count = (array) $wpdb->get_results($sql, ARRAY_A);
$status = array();
$status_html = '';
foreach ($status_count as $i => $row) {
$status[$row['post_status']] = $row['post_count'];
}
if (isset($status['publish'])) {
$status_html .= '<li><a href="edit.php?post_type=feedback"';
if ($current_menu == 'messages') {
$status_html .= ' class="current"';
}
$status_html .= '>' . __('Messages', 'jetpack') . ' <span class="count">';
$status_html .= '(' . number_format($status['publish']) . ')';
$status_html .= '</span></a> |</li>';
}
if (isset($status['trash'])) {
$status_html .= '<li><a href="edit.php?post_status=trash&post_type=feedback"';
if ($current_menu == 'trash') {
$status_html .= ' class="current"';
}
$status_html .= '>' . __('Trash', 'jetpack') . ' <span class="count">';
$status_html .= '(' . number_format($status['trash']) . ')';
$status_html .= '</span></a>';
if (isset($status['spam'])) {
$status_html .= ' |';
}
$status_html .= '</li>';
}
if (isset($status['spam'])) {
$status_html .= '<li><a href="edit.php?post_status=spam&post_type=feedback"';
if ($current_menu == 'spam') {
$status_html .= ' class="current"';
}
$status_html .= '>' . __('Spam', 'jetpack') . ' <span class="count">';
$status_html .= '(' . number_format($status['spam']) . ')';
$status_html .= '</span></a></li>';
}
echo $status_html;
exit;
}