Parses shortcode to output the contact form field as HTML. Validates input.
상속: extends Crunion_Contact_Form_Shortcode
예제 #1
0
 /**
  * The contact-field shortcode processor
  * We use an object method here instead of a static Grunion_Contact_Form_Field class method to parse contact-field shortcodes so that we can tie them to the contact-form object.
  *
  * @param array $attributes Key => Value pairs as parsed by shortcode_parse_atts()
  * @param string|null $content The shortcode's inner content: [contact-field]$content[/contact-field]
  * @return HTML for the contact form field
  */
 static function parse_contact_field($attributes, $content)
 {
     // Don't try to parse contact form fields if not inside a contact form
     if (!Grunion_Contact_Form_Plugin::$using_contact_form_field) {
         $att_strs = array();
         foreach ($attributes as $att => $val) {
             if (is_numeric($att)) {
                 // Is a valueless attribute
                 $att_strs[] = esc_html($val);
             } else {
                 if (isset($val)) {
                     // A regular attr - value pair
                     $att_strs[] = esc_html($att) . '=\'' . esc_html($val) . '\'';
                 }
             }
         }
         $html = '[contact-field ' . implode(' ', $att_strs);
         if (isset($content) && !empty($content)) {
             // If there is content, let's add a closing tag
             $html .= ']' . esc_html($content) . '[/contact-field]';
         } else {
             // Otherwise let's add a closing slash in the first tag
             $html .= '/]';
         }
         return $html;
     }
     $form = Grunion_Contact_Form::$current_form;
     $field = new Grunion_Contact_Form_Field($attributes, $content, $form);
     $field_id = $field->get_attribute('id');
     if ($field_id) {
         $form->fields[$field_id] = $field;
     } else {
         $form->fields[] = $field;
     }
     if (isset($_POST['action']) && 'grunion-contact-form' === $_POST['action'] && isset($_POST['contact-form-id']) && $form->get_attribute('id') == $_POST['contact-form-id']) {
         // If we're processing a POST submission for this contact form, validate the field value so we can show errors as necessary.
         $field->validate();
     }
     // Output HTML
     return $field->render();
 }
예제 #2
0
 /**
  * The contact-field shortcode processor
  * We use an object method here instead of a static Grunion_Contact_Form_Field class method to parse contact-field shortcodes so that we can tie them to the contact-form object.
  *
  * @param array $attributes Key => Value pairs as parsed by shortcode_parse_atts()
  * @param string|null $content The shortcode's inner content: [contact-field]$content[/contact-field]
  * @return HTML for the contact form field
  */
 function parse_contact_field($attributes, $content)
 {
     $field = new Grunion_Contact_Form_Field($attributes, $content, $this);
     $field_id = $field->get_attribute('id');
     if ($field_id) {
         $this->fields[$field_id] = $field;
     } else {
         $this->fields[] = $field;
     }
     if (isset($_POST['action']) && 'grunion-contact-form' === $_POST['action'] && isset($_POST['contact-form-id']) && $this->get_attribute('id') == $_POST['contact-form-id']) {
         // If we're processing a POST submission for this contact form, validate the field value so we can show errors as necessary.
         $field->validate();
     }
     // Output HTML
     return $field->render();
 }