Grunion_Contact_Form_Plugin::parse_fields_from_content PHP Method

parse_fields_from_content() public static method

public static parse_fields_from_content ( $post_id )
    public static function parse_fields_from_content($post_id)
    {
        static $post_fields;
        if (!is_array($post_fields)) {
            $post_fields = array();
        }
        if (isset($post_fields[$post_id])) {
            return $post_fields[$post_id];
        }
        $all_values = array();
        $post_content = get_post_field('post_content', $post_id);
        $content = explode('<!--more-->', $post_content);
        $lines = array();
        if (count($content) > 1) {
            $content = str_ireplace(array('<br />', ')</p>'), '', $content[1]);
            $one_line = preg_replace('/\\s+/', ' ', $content);
            $one_line = preg_replace('/.*Array \\( (.*)\\)/', '$1', $one_line);
            preg_match_all('/\\[([^\\]]+)\\] =\\&gt\\; ([^\\[]+)/', $one_line, $matches);
            if (count($matches) > 1) {
                $all_values = array_combine(array_map('trim', $matches[1]), array_map('trim', $matches[2]));
            }
            $lines = array_filter(explode("\n", $content));
        }
        $var_map = array('AUTHOR' => '_feedback_author', 'AUTHOR EMAIL' => '_feedback_author_email', 'AUTHOR URL' => '_feedback_author_url', 'SUBJECT' => '_feedback_subject', 'IP' => '_feedback_ip');
        $fields = array();
        foreach ($lines as $line) {
            $vars = explode(': ', $line, 2);
            if (!empty($vars)) {
                if (isset($var_map[$vars[0]])) {
                    $fields[$var_map[$vars[0]]] = self::strip_tags(trim($vars[1]));
                }
            }
        }
        $fields['_feedback_all_fields'] = $all_values;
        $post_fields[$post_id] = $fields;
        return $fields;
    }

Usage Example

Example #1
0
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&amp;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&amp;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;
}
All Usage Examples Of Grunion_Contact_Form_Plugin::parse_fields_from_content