| 1 |
<?php |
| 2 |
|
| 3 |
namespace HTML_Forms; |
| 4 |
|
| 5 |
class Form { |
| 6 |
|
| 7 |
public $ID = 0; |
| 8 |
public $title = ''; |
| 9 |
public $slug = ''; |
| 10 |
public $markup = ''; |
| 11 |
public $messages = array(); |
| 12 |
public $settings = array(); |
| 13 |
|
| 14 |
/** |
| 15 |
* Form constructor. |
| 16 |
* |
| 17 |
* @param $ID |
| 18 |
*/ |
| 19 |
public function __construct($ID) |
| 20 |
{ |
| 21 |
$this->ID = $ID; |
| 22 |
} |
| 23 |
|
| 24 |
public function get_html() |
| 25 |
{ |
| 26 |
$form = $this; |
| 27 |
|
| 28 |
/** |
| 29 |
* Filters the CSS classes to be added to this form's class attribute. |
| 30 |
* |
| 31 |
* @param array $form_classes |
| 32 |
* @param Form $form |
| 33 |
*/ |
| 34 |
$form_classes_attr = apply_filters( 'hf_form_element_class_attr', '', $form ); |
| 35 |
|
| 36 |
/** |
| 37 |
* Filters the action attribute for this form. |
| 38 |
* |
| 39 |
* @param string $form_action |
| 40 |
* @param Form $form |
| 41 |
*/ |
| 42 |
$form_action = apply_filters( 'hf_form_element_action_attr', null, $form ); |
| 43 |
$form_action_attr = is_null( $form_action ) ? '' : sprintf('action="%s"', $form_action ); |
| 44 |
|
| 45 |
$html = ''; |
| 46 |
$html .= sprintf( '<!-- HTML Forms v%s - %s -->', HTML_FORMS_VERSION, 'https://wordpress.org/plugins/html-forms/' ); |
| 47 |
$html .= sprintf( '<form method="post" %s class="hf-form hf-form-%d %s" data-title="%s" data-slug="%s">', $form_action_attr, $this->ID, esc_attr( $form_classes_attr ), esc_attr( $this->title ), esc_attr( $this->slug ) ); |
| 48 |
|
| 49 |
$html .= '<div class="hf-fields-wrap">'; |
| 50 |
$html .= sprintf( '<input type="hidden" name="_hf_form_id" value="%d" />', $this->ID ); |
| 51 |
$html .= sprintf( '<div style="display: none;"><input type="text" name="_hf_h%d" value="" /></div>', $this->ID ); |
| 52 |
$html .= $this->get_markup(); |
| 53 |
$html .= '<noscript>' . __( "Please enable JavaScript for this form to work.", 'html-forms' ) . '</noscript>'; |
| 54 |
$html .= '</div>'; // end field wrap |
| 55 |
$html .= '</form>'; |
| 56 |
$html .= '<!-- / HTML Forms -->'; |
| 57 |
|
| 58 |
/** |
| 59 |
* Filters the resulting HTML for this form. |
| 60 |
* |
| 61 |
* @param string $html |
| 62 |
* @param Form $form |
| 63 |
*/ |
| 64 |
$html = apply_filters( 'hf_form_html', $html, $form ); |
| 65 |
return $html; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @return string |
| 70 |
*/ |
| 71 |
public function __toString() |
| 72 |
{ |
| 73 |
return $this->get_html(); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* @return string |
| 78 |
*/ |
| 79 |
public function get_markup() { |
| 80 |
return apply_filters( 'hf_form_markup', $this->markup ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @return array |
| 85 |
*/ |
| 86 |
public function get_required_fields() |
| 87 |
{ |
| 88 |
if( empty( $this->settings['required_fields'] ) ) { |
| 89 |
return array(); |
| 90 |
} |
| 91 |
|
| 92 |
$required_fields = explode( ',', $this->settings['required_fields'] ); |
| 93 |
return $required_fields; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* @return array |
| 98 |
*/ |
| 99 |
public function get_email_fields() |
| 100 |
{ |
| 101 |
if( empty( $this->settings['email_fields'] ) ) { |
| 102 |
return array(); |
| 103 |
} |
| 104 |
|
| 105 |
$email_fields = explode( ',', $this->settings['email_fields'] ); |
| 106 |
return $email_fields; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @param string $code |
| 111 |
* @return string |
| 112 |
*/ |
| 113 |
public function get_message( $code ) |
| 114 |
{ |
| 115 |
$form = $this; |
| 116 |
$message = isset( $this->messages[ $code ] ) ? $this->messages[ $code ] : ''; |
| 117 |
|
| 118 |
/** |
| 119 |
* @param string $message |
| 120 |
* @param Form $form |
| 121 |
*/ |
| 122 |
$message = apply_filters( 'hf_form_message_' . $code, $message, $form ); |
| 123 |
return $message; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* @return int The number of named fields in the form |
| 128 |
*/ |
| 129 |
public function get_field_count() { |
| 130 |
return substr_count( strtolower( $this->get_html() ), ' name=' ); |
| 131 |
} |
| 132 |
} |
| 133 |
|