/**
  * Outputs the HTML for this form field
  *
  * @return string HTML
  */
 function render()
 {
     global $current_user, $user_identity;
     $r = '';
     $field_id = $this->get_attribute('id');
     $field_type = $this->get_attribute('type');
     $field_label = $this->get_attribute('label');
     $field_required = $this->get_attribute('required');
     $placeholder = $this->get_attribute('placeholder');
     $class = $this->get_attribute('class');
     $field_placeholder = !empty($placeholder) ? "placeholder='" . esc_attr($placeholder) . "'" : '';
     $field_class = "class='" . trim(esc_attr($field_type) . " " . esc_attr($class)) . "' ";
     if (isset($_POST[$field_id])) {
         if (is_array($_POST[$field_id])) {
             $this->value = array_map('stripslashes', $_POST[$field_id]);
         } else {
             $this->value = stripslashes((string) $_POST[$field_id]);
         }
     } elseif (isset($_GET[$field_id])) {
         $this->value = stripslashes((string) $_GET[$field_id]);
     } elseif (is_user_logged_in() && (defined('IS_WPCOM') && IS_WPCOM || true === apply_filters('jetpack_auto_fill_logged_in_user', false))) {
         // Special defaults for logged-in users
         switch ($this->get_attribute('type')) {
             case 'email':
                 $this->value = $current_user->data->user_email;
                 break;
             case 'name':
                 $this->value = $user_identity;
                 break;
             case 'url':
                 $this->value = $current_user->data->user_url;
                 break;
             default:
                 $this->value = $this->get_attribute('default');
         }
     } else {
         $this->value = $this->get_attribute('default');
     }
     $field_value = Grunion_Contact_Form_Plugin::strip_tags($this->value);
     $field_label = Grunion_Contact_Form_Plugin::strip_tags($field_label);
     /**
      * Filter the Contact Form required field text
      *
      * @module contact-form
      *
      * @since 3.8.0
      *
      * @param string $var Required field text. Default is "(required)".
      */
     $required_field_text = esc_html(apply_filters('jetpack_required_field_text', __("(required)", 'jetpack')));
     switch ($field_type) {
         case 'email':
             $r .= "\n<div>\n";
             $r .= "\t\t<label for='" . esc_attr($field_id) . "' class='grunion-field-label email" . ($this->is_error() ? ' form-error' : '') . "'>" . esc_html($field_label) . ($field_required ? '<span>' . $required_field_text . '</span>' : '') . "</label>\n";
             $r .= "\t\t<input type='email' name='" . esc_attr($field_id) . "' id='" . esc_attr($field_id) . "' value='" . esc_attr($field_value) . "' " . $field_class . $field_placeholder . " " . ($field_required ? "required aria-required='true'" : "") . "/>\n";
             $r .= "\t</div>\n";
             break;
         case 'telephone':
             $r .= "\n<div>\n";
             $r .= "\t\t<label for='" . esc_attr($field_id) . "' class='grunion-field-label telephone" . ($this->is_error() ? ' form-error' : '') . "'>" . esc_html($field_label) . ($field_required ? '<span>' . $required_field_text . '</span>' : '') . "</label>\n";
             $r .= "\t\t<input type='tel' name='" . esc_attr($field_id) . "' id='" . esc_attr($field_id) . "' value='" . esc_attr($field_value) . "' " . $field_class . $field_placeholder . "/>\n";
             break;
         case 'textarea':
             $r .= "\n<div>\n";
             $r .= "\t\t<label for='contact-form-comment-" . esc_attr($field_id) . "' class='grunion-field-label textarea" . ($this->is_error() ? ' form-error' : '') . "'>" . esc_html($field_label) . ($field_required ? '<span>' . $required_field_text . '</span>' : '') . "</label>\n";
             $r .= "\t\t<textarea name='" . esc_attr($field_id) . "' id='contact-form-comment-" . esc_attr($field_id) . "' rows='20' " . $field_class . $field_placeholder . " " . ($field_required ? "required aria-required='true'" : "") . ">" . esc_textarea($field_value) . "</textarea>\n";
             $r .= "\t</div>\n";
             break;
         case 'radio':
             $r .= "\t<div><label class='grunion-field-label" . ($this->is_error() ? ' form-error' : '') . "'>" . esc_html($field_label) . ($field_required ? '<span>' . $required_field_text . '</span>' : '') . "</label>\n";
             foreach ($this->get_attribute('options') as $option) {
                 $option = Grunion_Contact_Form_Plugin::strip_tags($option);
                 $r .= "\t\t<label class='grunion-radio-label radio" . ($this->is_error() ? ' form-error' : '') . "'>";
                 $r .= "<input type='radio' name='" . esc_attr($field_id) . "' value='" . esc_attr($option) . "' " . $field_class . checked($option, $field_value, false) . " " . ($field_required ? "required aria-required='true'" : "") . "/> ";
                 $r .= esc_html($option) . "</label>\n";
                 $r .= "\t\t<div class='clear-form'></div>\n";
             }
             $r .= "\t\t</div>\n";
             break;
         case 'checkbox':
             $r .= "\t<div>\n";
             $r .= "\t\t<label class='grunion-field-label checkbox" . ($this->is_error() ? ' form-error' : '') . "'>\n";
             $r .= "\t\t<input type='checkbox' name='" . esc_attr($field_id) . "' value='" . esc_attr__('Yes', 'jetpack') . "' " . $field_class . checked((bool) $field_value, true, false) . " " . ($field_required ? "required aria-required='true'" : "") . "/> \n";
             $r .= "\t\t" . esc_html($field_label) . ($field_required ? '<span>' . $required_field_text . '</span>' : '') . "</label>\n";
             $r .= "\t\t<div class='clear-form'></div>\n";
             $r .= "\t</div>\n";
             break;
         case 'checkbox-multiple':
             $r .= "\t<div><label class='grunion-field-label" . ($this->is_error() ? ' form-error' : '') . "'>" . esc_html($field_label) . ($field_required ? '<span>' . $required_field_text . '</span>' : '') . "</label>\n";
             foreach ($this->get_attribute('options') as $option) {
                 $option = Grunion_Contact_Form_Plugin::strip_tags($option);
                 $r .= "\t\t<label class='grunion-checkbox-multiple-label checkbox-multiple" . ($this->is_error() ? ' form-error' : '') . "'>";
                 $r .= "<input type='checkbox' name='" . esc_attr($field_id) . "[]' value='" . esc_attr($option) . "' " . $field_class . checked(in_array($option, (array) $field_value), true, false) . " /> ";
                 $r .= esc_html($option) . "</label>\n";
                 $r .= "\t\t<div class='clear-form'></div>\n";
             }
             $r .= "\t\t</div>\n";
             break;
         case 'select':
             $r .= "\n<div>\n";
             $r .= "\t\t<label for='" . esc_attr($field_id) . "' class='grunion-field-label select" . ($this->is_error() ? ' form-error' : '') . "'>" . esc_html($field_label) . ($field_required ? '<span>' . $required_field_text . '</span>' : '') . "</label>\n";
             $r .= "\t<select name='" . esc_attr($field_id) . "' id='" . esc_attr($field_id) . "' " . $field_class . ($field_required ? "required aria-required='true'" : "") . ">\n";
             foreach ($this->get_attribute('options') as $option) {
                 $option = Grunion_Contact_Form_Plugin::strip_tags($option);
                 $r .= "\t\t<option" . selected($option, $field_value, false) . ">" . esc_html($option) . "</option>\n";
             }
             $r .= "\t</select>\n";
             $r .= "\t</div>\n";
             break;
         case 'date':
             $r .= "\n<div>\n";
             $r .= "\t\t<label for='" . esc_attr($field_id) . "' class='grunion-field-label " . esc_attr($field_type) . ($this->is_error() ? ' form-error' : '') . "'>" . esc_html($field_label) . ($field_required ? '<span>' . $required_field_text . '</span>' : '') . "</label>\n";
             $r .= "\t\t<input type='date' name='" . esc_attr($field_id) . "' id='" . esc_attr($field_id) . "' value='" . esc_attr($field_value) . "' " . $field_class . ($field_required ? "required aria-required='true'" : "") . "/>\n";
             $r .= "\t</div>\n";
             wp_enqueue_script('grunion-frontend', plugins_url('js/grunion-frontend.js', __FILE__), array('jquery', 'jquery-ui-datepicker'));
             break;
         default:
             // text field
             // note that any unknown types will produce a text input, so we can use arbitrary type names to handle
             // input fields like name, email, url that require special validation or handling at POST
             $r .= "\n<div>\n";
             $r .= "\t\t<label for='" . esc_attr($field_id) . "' class='grunion-field-label " . esc_attr($field_type) . ($this->is_error() ? ' form-error' : '') . "'>" . esc_html($field_label) . ($field_required ? '<span>' . $required_field_text . '</span>' : '') . "</label>\n";
             $r .= "\t\t<input type='text' name='" . esc_attr($field_id) . "' id='" . esc_attr($field_id) . "' value='" . esc_attr($field_value) . "' " . $field_class . $field_placeholder . " " . ($field_required ? "required aria-required='true'" : "") . "/>\n";
             $r .= "\t</div>\n";
     }
     /**
      * Filter the HTML of the Contact Form.
      *
      * @module contact-form
      *
      * @since 2.6.0
      *
      * @param string $r Contact Form HTML output.
      * @param string $field_label Field label.
      * @param int|null $id Post ID.
      */
     return apply_filters('grunion_contact_form_field_html', $r, $field_label, in_the_loop() ? get_the_ID() : null);
 }
Example #2
0
 /**
  * Outputs the HTML for this form field
  *
  * @return string HTML
  */
 function render()
 {
     global $current_user, $user_identity;
     $r = '';
     $field_id = $this->get_attribute('id');
     $field_type = $this->get_attribute('type');
     $field_label = $this->get_attribute('label');
     $field_required = $this->get_attribute('required');
     if (isset($_POST[$field_id])) {
         $this->value = stripslashes((string) $_POST[$field_id]);
     } elseif (is_user_logged_in()) {
         // Special defaults for logged-in users
         switch ($this->get_attribute('type')) {
             case 'email':
                 $this->value = $current_user->data->user_email;
                 break;
             case 'name':
                 $this->value = $user_identity;
                 break;
             case 'url':
                 $this->value = $current_user->data->user_url;
                 break;
             default:
                 $this->value = $this->get_attribute('default');
         }
     } else {
         $this->value = $this->get_attribute('default');
     }
     $field_value = Grunion_Contact_Form_Plugin::strip_tags($this->value);
     $field_label = Grunion_Contact_Form_Plugin::strip_tags($field_label);
     switch ($field_type) {
         case 'email':
             $r .= "\n<div>\n";
             $r .= "\t\t<label for='" . esc_attr($field_id) . "' class='grunion-field-label email" . ($this->is_error() ? ' form-error' : '') . "'>" . esc_html($field_label) . ($field_required ? '<span>' . __("(required)", 'jetpack') . '</span>' : '') . "</label>\n";
             $r .= "\t\t<input type='email' name='" . esc_attr($field_id) . "' id='" . esc_attr($field_id) . "' value='" . esc_attr($field_value) . "' class='email' />\n";
             $r .= "\t</div>\n";
             break;
         case 'textarea':
             $r .= "\n<div>\n";
             $r .= "\t\t<label for='contact-form-comment-" . esc_attr($field_id) . "' class='grunion-field-label textarea" . ($this->is_error() ? ' form-error' : '') . "'>" . esc_html($field_label) . ($field_required ? '<span>' . __("(required)", 'jetpack') . '</span>' : '') . "</label>\n";
             $r .= "\t\t<textarea name='" . esc_attr($field_id) . "' id='contact-form-comment-" . esc_attr($field_id) . "' rows='20'>" . esc_textarea($field_value) . "</textarea>\n";
             $r .= "\t</div>\n";
             break;
         case 'radio':
             $r .= "\t<div><label class='grunion-field-label" . ($this->is_error() ? ' form-error' : '') . "'>" . esc_html($field_label) . ($field_required ? '<span>' . __("(required)", 'jetpack') . '</span>' : '') . "</label>\n";
             foreach ($this->get_attribute('options') as $option) {
                 $option = Grunion_Contact_Form_Plugin::strip_tags($option);
                 $r .= "\t\t<label class='grunion-radio-label radio" . ($this->is_error() ? ' form-error' : '') . "'>";
                 $r .= "<input type='radio' name='" . esc_attr($field_id) . "' value='" . esc_attr($option) . "' class='radio' " . checked($option, $field_value, false) . " /> ";
                 $r .= esc_html($option) . "</label>\n";
                 $r .= "\t\t<div class='clear-form'></div>\n";
             }
             $r .= "\t\t</div>\n";
             break;
         case 'checkbox':
             $r .= "\t<div>\n";
             $r .= "\t\t<label class='grunion-field-label checkbox" . ($this->is_error() ? ' form-error' : '') . "'>\n";
             $r .= "\t\t<input type='checkbox' name='" . esc_attr($field_id) . "' value='" . esc_attr__('Yes', 'jetpack') . "' class='checkbox' " . checked((bool) $field_value, true, false) . " /> \n";
             $r .= "\t\t" . esc_html($field_label) . ($field_required ? '<span>' . __("(required)", 'jetpack') . '</span>' : '') . "</label>\n";
             $r .= "\t\t<div class='clear-form'></div>\n";
             $r .= "\t</div>\n";
             break;
         case 'select':
             $r .= "\n<div>\n";
             $r .= "\t\t<label for='" . esc_attr($field_id) . "' class='grunion-field-label select" . ($this->is_error() ? ' form-error' : '') . "'>" . esc_html($field_label) . ($field_required ? '<span>' . __("(required)", 'jetpack') . '</span>' : '') . "</label>\n";
             $r .= "\t<select name='" . esc_attr($field_id) . "' id='" . esc_attr($field_id) . "' class='select' >\n";
             foreach ($this->get_attribute('options') as $option) {
                 $option = Grunion_Contact_Form_Plugin::strip_tags($option);
                 $r .= "\t\t<option" . selected($option, $field_value, false) . ">" . esc_html($option) . "</option>\n";
             }
             $r .= "\t</select>\n";
             $r .= "\t</div>\n";
             break;
         case 'date':
             $r .= "\n<div>\n";
             $r .= "\t\t<label for='" . esc_attr($field_id) . "' class='grunion-field-label " . esc_attr($field_type) . ($this->is_error() ? ' form-error' : '') . "'>" . esc_html($field_label) . ($field_required ? '<span>' . __("(required)", 'jetpack') . '</span>' : '') . "</label>\n";
             $r .= "\t\t<input type='date' name='" . esc_attr($field_id) . "' id='" . esc_attr($field_id) . "' value='" . esc_attr($field_value) . "' class='" . esc_attr($field_type) . "'/>\n";
             $r .= "\t</div>\n";
             wp_enqueue_script('grunion-frontend', plugins_url('js/grunion-frontend.js', __FILE__), array('jquery', 'jquery-ui-datepicker'));
             break;
         default:
             // text field
             // note that any unknown types will produce a text input, so we can use arbitrary type names to handle
             // input fields like name, email, url that require special validation or handling at POST
             $r .= "\n<div>\n";
             $r .= "\t\t<label for='" . esc_attr($field_id) . "' class='grunion-field-label " . esc_attr($field_type) . ($this->is_error() ? ' form-error' : '') . "'>" . esc_html($field_label) . ($field_required ? '<span>' . __("(required)", 'jetpack') . '</span>' : '') . "</label>\n";
             $r .= "\t\t<input type='text' name='" . esc_attr($field_id) . "' id='" . esc_attr($field_id) . "' value='" . esc_attr($field_value) . "' class='" . esc_attr($field_type) . "'/>\n";
             $r .= "\t</div>\n";
     }
     return $r;
 }
Example #3
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;
}
 /**
  * @author tonykova
  * @covers Grunion_Contact_Form_Plugin::replace_tokens_with_input
  */
 public function test_token_with_curly_brackets_can_be_replaced()
 {
     $plugin = new Grunion_Contact_Form_Plugin();
     $subject = '{subject {token}}';
     $field_values = array('Subject {Token}' => 'Chicago');
     $this->assertEquals('Chicago', $plugin->replace_tokens_with_input($subject, $field_values));
 }
 function test_remove_contact_form_shortcode_from_filtered_content()
 {
     require_once JETPACK__PLUGIN_DIR . 'modules/contact-form/grunion-contact-form.php';
     $this->post->post_content = '<p>This post has a contact form:[contact-form][contact-field label=\'Name\' type=\'name\' required=\'1\'/][/contact-form]</p>';
     Grunion_Contact_Form_Plugin::init();
     wp_update_post($this->post);
     $this->assertContains('<form action=', apply_filters('the_content', $this->post->post_content));
     $this->sender->do_sync();
     $synced_post = $this->server_replica_storage->get_post($this->post->ID);
     $this->assertEquals("<p>This post has a contact form:</p>\n", $synced_post->post_content_filtered);
 }
 /**
  * Test map_parsed_field_contents_of_post_to_field_names
  *
  * @group csvexport
  */
 public function test_map_parsed_field_contents_of_post_to_field_names()
 {
     $input_data = array('test_field' => 'moonstruck', '_feedback_subject' => 'This is my form', '_feedback_author_email' => '', '_feedback_author' => 'John Smith', '_feedback_author_url' => 'http://example.com', '_feedback_main_comment' => 'This is my comment!', 'another_field' => 'thunderstruck');
     $plugin = new Grunion_Contact_Form_Plugin();
     $result = $plugin->map_parsed_field_contents_of_post_to_field_names($input_data);
     $expected_result = array('Contact Form' => 'This is my form', '1_Name' => 'John Smith', '3_Website' => 'http://example.com', '4_Comment' => 'This is my comment!');
     $this->assertEquals($expected_result, $result);
 }