PluginProbe
HTML Forms – Simple WordPress Forms Plugin / trunk
HTML Forms – Simple WordPress Forms Plugin vtrunk
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 / class-form.php

class-form.php in HTML Forms – Simple WordPress Forms Plugin trunk, at src/class-form.php

198 lines 4.9 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 $this->ID = $id;
21 }
22
23 /**
24 * Magic method for accessing unexisting properties, eg lowercase "id".
25 * @param string $property
26 * @return mixed
27 */
28 public function __get( $property ) {
29 if ( $property === 'id' ) {
30 return $this->ID;
31 }
32 }
33
34 public function get_html() {
35 $form = $this;
36
37 /**
38 * Filters the CSS classes to be added to this form's class attribute.
39 *
40 * @param array $form_classes
41 * @param Form $form
42 */
43 $form_classes_attr = apply_filters( 'hf_form_element_class_attr', '', $form );
44
45 /**
46 * Filters the action attribute for this form.
47 *
48 * @param string $form_action
49 * @param Form $form
50 */
51 $form_action = apply_filters( 'hf_form_element_action_attr', null, $form );
52 $form_action_attr = is_null( $form_action ) ? '' : sprintf( 'action="%s"', $form_action );
53
54 $data_attributes = $this->get_data_attributes();
55 $settings = hf_get_settings();
56
57 $html = '';
58 $html .= sprintf( '<!-- HTML Forms v%s - %s -->', HTML_FORMS_VERSION, 'https://wordpress.org/plugins/html-forms/' ) . PHP_EOL;
59 $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 );
60
61 if ( $settings['enable_nonce'] ) {
62 $html .= wp_nonce_field( 'html_forms_submit', '_wpnonce', true, false );
63 }
64
65 $html .= sprintf( '<input type="hidden" name="_hf_form_id" value="%d" />', $this->ID );
66 $html .= sprintf( '<div style="display: none;"><input type="text" name="_hf_h%d" value="" /></div>', $this->ID );
67 $html .= '<div class="hf-fields-wrap">';
68 $html .= $this->get_markup();
69 $html .= '<noscript>' . __( 'Please enable JavaScript for this form to work.', 'html-forms' ) . '</noscript>';
70 $html .= '</div>'; // end field wrap
71 $html .= '</form>';
72 $html .= '<!-- / HTML Forms -->';
73
74 // ensure JS script is enqueued whenever this function is called
75 if ( function_exists( 'wp_enqueue_script' ) ) {
76 wp_enqueue_script( 'html-forms' );
77 }
78
79 /**
80 * Filters the resulting HTML for this form.
81 *
82 * @param string $html
83 * @param Form $form
84 */
85 $html = apply_filters( 'hf_form_html', $html, $form );
86 return $html;
87 }
88
89 public function get_data_attributes() {
90 $form = $this;
91 $attributes = array(
92 'id' => $this->ID,
93 'title' => $this->title,
94 'slug' => $this->slug,
95 );
96
97 // add messages
98 foreach ( $this->messages as $key => $message ) {
99 $key = str_replace( '_', '-', $key );
100 $attributes[ 'message-' . $key ] = $message;
101 }
102
103 /**
104 * Filters the data attributes to be added to the form attribute.
105 *
106 * @param array $attributes
107 * @param Form $form
108 */
109 $attributes = apply_filters( 'hf_form_element_data_attributes', $attributes, $form );
110
111 // create string of attribute key-value pairs
112 $string = '';
113 foreach ( $attributes as $attr => $value ) {
114 // prefix all attributes with data-
115 if ( substr( $attr, 0, 5 ) !== 'data-' ) {
116 $attr = 'data-' . $attr;
117 }
118
119 $string .= sprintf( '%s="%s" ', $attr, esc_attr( $value ) );
120 }
121 $string = rtrim( $string, ' ' );
122
123 return $string;
124 }
125
126
127 /**
128 * @return string
129 */
130 public function __toString() {
131 return $this->get_html();
132 }
133
134 /**
135 * @return string
136 */
137 public function get_markup() {
138 /**
139 * @param string $markup
140 * @param Form $form
141 */
142 return apply_filters( 'hf_form_markup', $this->markup, $this );
143 }
144
145 /**
146 * @return array
147 */
148 public function get_required_fields() {
149 if ( empty( $this->settings['required_fields'] ) ) {
150 return array();
151 }
152
153 $required_fields = explode( ',', $this->settings['required_fields'] );
154 return $required_fields;
155 }
156
157 /**
158 * @return array
159 */
160 public function get_email_fields() {
161 if ( empty( $this->settings['email_fields'] ) ) {
162 return array();
163 }
164
165 $email_fields = explode( ',', $this->settings['email_fields'] );
166 return $email_fields;
167 }
168
169 /**
170 * @param string $code
171 * @return string
172 */
173 public function get_message( $code ) {
174 $form = $this;
175 $message = isset( $this->messages[ $code ] ) ? $this->messages[ $code ] : '';
176
177 /**
178 * @param string $message
179 * @param Form $form
180 */
181 $message = apply_filters( 'hf_form_message_' . $code, $message, $form );
182 return $message;
183 }
184
185 /**
186 * @return int The number of named fields in the form
187 *
188 * Note: this includes all default fields and an additional field for the "was-required" element we include in every request.
189 */
190 public function get_field_count() {
191 $pattern = '/\bname\s*=\s*["\']/i';
192 preg_match_all( $pattern, $this->get_html(), $matches );
193 $count = ! empty( $matches ) ? count( $matches[0] ) : 0;
194 $count++; // Add one for 'was-required'
195 return $count;
196 }
197 }
198