PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.1.3
HTML Forms – Simple WordPress Forms Plugin v1.1.3
trunk 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2.0 1.3.0 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 All 66 releases
html-forms / src / Form.php

Form.php in HTML Forms – Simple WordPress Forms Plugin 1.1.3, at src/Form.php

173 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $data_attributes = $this->get_data_attributes();
46
47 $html = '';
48 $html .= sprintf( '<!-- HTML Forms v%s - %s -->', HTML_FORMS_VERSION, 'https://wordpress.org/plugins/html-forms/' );
49 $html .= sprintf( '<form method="post" %s class="hf-form hf-form-%d %s" %s>', $form_action_attr, $this->ID, esc_attr( $form_classes_attr ), $data_attributes );
50
51 $html .= '<div class="hf-fields-wrap">';
52 $html .= sprintf( '<input type="hidden" name="_hf_form_id" value="%d" />', $this->ID );
53 $html .= sprintf( '<div style="display: none;"><input type="text" name="_hf_h%d" value="" /></div>', $this->ID );
54 $html .= $this->get_markup();
55 $html .= '<noscript>' . __( "Please enable JavaScript for this form to work.", 'html-forms' ) . '</noscript>';
56 $html .= '</div>'; // end field wrap
57 $html .= '</form>';
58 $html .= '<!-- / HTML Forms -->';
59
60 /**
61 * Filters the resulting HTML for this form.
62 *
63 * @param string $html
64 * @param Form $form
65 */
66 $html = apply_filters( 'hf_form_html', $html, $form );
67 return $html;
68 }
69
70 public function get_data_attributes() {
71 $form = $this;
72 $attributes = array(
73 'id' => $this->ID,
74 'title' => $this->title,
75 'slug' => $this->slug,
76 );
77
78 // add messages
79 foreach( $this->messages as $key => $message ) {
80 $key = str_replace( '_', '-', $key );
81 $attributes["message-".$key] = $message;
82 }
83
84 /**
85 * Filters the data attributes to be added to the form attribute.
86 *
87 * @param array $attributes
88 * @param Form $form
89 */
90 $attributes = apply_filters( 'hf_form_element_data_attributes', $attributes, $form );
91
92 // create string of attribute key-value pairs
93 $string = '';
94 foreach( $attributes as $attr => $value ) {
95 // prefix all attributes with data-
96 if( substr( $attr, 0, 5 ) !== 'data-' ) {
97 $attr = "data-" . $attr;
98 }
99
100 $string .= sprintf( '%s="%s" ', $attr, esc_attr( $value ) );
101 }
102 $string = rtrim( $string, ' ' );
103
104 return $string;
105 }
106
107
108 /**
109 * @return string
110 */
111 public function __toString()
112 {
113 return $this->get_html();
114 }
115
116 /**
117 * @return string
118 */
119 public function get_markup() {
120 return apply_filters( 'hf_form_markup', $this->markup );
121 }
122
123 /**
124 * @return array
125 */
126 public function get_required_fields()
127 {
128 if( empty( $this->settings['required_fields'] ) ) {
129 return array();
130 }
131
132 $required_fields = explode( ',', $this->settings['required_fields'] );
133 return $required_fields;
134 }
135
136 /**
137 * @return array
138 */
139 public function get_email_fields()
140 {
141 if( empty( $this->settings['email_fields'] ) ) {
142 return array();
143 }
144
145 $email_fields = explode( ',', $this->settings['email_fields'] );
146 return $email_fields;
147 }
148
149 /**
150 * @param string $code
151 * @return string
152 */
153 public function get_message( $code )
154 {
155 $form = $this;
156 $message = isset( $this->messages[ $code ] ) ? $this->messages[ $code ] : '';
157
158 /**
159 * @param string $message
160 * @param Form $form
161 */
162 $message = apply_filters( 'hf_form_message_' . $code, $message, $form );
163 return $message;
164 }
165
166 /**
167 * @return int The number of named fields in the form
168 */
169 public function get_field_count() {
170 return substr_count( strtolower( $this->get_html() ), ' name=' );
171 }
172 }
173