PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.5.5
HTML Forms – Simple WordPress Forms Plugin v1.5.5
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 1.5.5, at src/class-form.php

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