parse_fields_from_content() публичный статический Метод

public static parse_fields_from_content ( $post_id )
Пример #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;
}
Пример #2
0
 /**
  * Returns a compiled form with labels and values in a form of  an array
  * of lines.
  * @param int $feedback_id
  * @param object Grunion_Contact_Form $form
  *
  * @return array $lines
  */
 static function get_compiled_form($feedback_id, $form)
 {
     $feedback = get_post($feedback_id);
     $field_ids = $form->get_field_ids();
     $content_fields = Grunion_Contact_Form_Plugin::parse_fields_from_content($feedback_id);
     // Maps field_ids to post_meta keys
     $field_value_map = array('name' => 'author', 'email' => 'author_email', 'url' => 'author_url', 'subject' => 'subject', 'textarea' => false);
     $compiled_form = array();
     // "Standard" field whitelist
     foreach ($field_value_map as $type => $meta_key) {
         if (isset($field_ids[$type])) {
             $field = $form->fields[$field_ids[$type]];
             if ($meta_key) {
                 if (isset($content_fields["_feedback_{$meta_key}"])) {
                     $value = $content_fields["_feedback_{$meta_key}"];
                 }
             } else {
                 // The feedback content is stored as the first "half" of post_content
                 $value = $feedback->post_content;
                 list($value) = explode('<!--more-->', $value);
                 $value = trim($value);
             }
             $field_index = array_search($field_ids[$type], $field_ids['all']);
             $compiled_form[$field_index] = sprintf('<b>%1$s:</b> %2$s<br /><br />', wp_kses($field->get_attribute('label'), array()), nl2br(wp_kses($value, array())));
         }
     }
     // "Non-standard" fields
     if ($field_ids['extra']) {
         // array indexed by field label (not field id)
         $extra_fields = get_post_meta($feedback_id, '_feedback_extra_fields', true);
         /**
          * Only get data for the compiled form if `$extra_fields` is a valid and non-empty array.
          */
         if (is_array($extra_fields) && !empty($extra_fields)) {
             $extra_field_keys = array_keys($extra_fields);
             $i = 0;
             foreach ($field_ids['extra'] as $field_id) {
                 $field = $form->fields[$field_id];
                 $field_index = array_search($field_id, $field_ids['all']);
                 $label = $field->get_attribute('label');
                 $compiled_form[$field_index] = sprintf('<b>%1$s:</b> %2$s<br /><br />', wp_kses($label, array()), nl2br(wp_kses($extra_fields[$extra_field_keys[$i]], array())));
                 $i++;
             }
         }
     }
     // Sorting lines by the field index
     ksort($compiled_form);
     return $compiled_form;
 }
 static function success_message($feedback_id, $form)
 {
     $r_success_message = '';
     $feedback = get_post($feedback_id);
     $field_ids = $form->get_field_ids();
     $content_fields = Grunion_Contact_Form_Plugin::parse_fields_from_content($feedback_id);
     // Maps field_ids to post_meta keys
     $field_value_map = array('name' => 'author', 'email' => 'author_email', 'url' => 'author_url', 'subject' => 'subject', 'textarea' => false);
     $contact_form_message = "<blockquote>\n";
     // "Standard" field whitelist
     foreach ($field_value_map as $type => $meta_key) {
         if (isset($field_ids[$type])) {
             $field = $form->fields[$field_ids[$type]];
             if ($meta_key) {
                 if (isset($content_fields["_feedback_{$meta_key}"])) {
                     $value = $content_fields["_feedback_{$meta_key}"];
                 }
             } else {
                 // The feedback content is stored as the first "half" of post_content
                 $value = $feedback->post_content;
                 list($value) = explode('<!--more-->', $value);
                 $value = trim($value);
             }
             $contact_form_message .= sprintf(_x('%1$s: %2$s', '%1$s = form field label, %2$s = form field value', 'jetpack'), wp_kses($field->get_attribute('label'), array()), wp_kses($value, array())) . '<br />';
         }
     }
     // "Non-standard" fields
     if ($field_ids['extra']) {
         // array indexed by field label (not field id)
         $extra_fields = get_post_meta($feedback_id, '_feedback_extra_fields', true);
         foreach ($field_ids['extra'] as $field_id) {
             $field = $form->fields[$field_id];
             $label = $field->get_attribute('label');
             $contact_form_message .= sprintf(_x('%1$s: %2$s', '%1$s = form field label, %2$s = form field value', 'jetpack'), wp_kses($label, array()), wp_kses($extra_fields[$label], array())) . '<br />';
         }
     }
     $contact_form_message .= "</blockquote><br /><br />";
     $r_success_message .= wp_kses($contact_form_message, array('br' => array(), 'blockquote' => array()));
     return $r_success_message;
 }