PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.3.12
HTML Forms – Simple WordPress Forms Plugin v1.3.12
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.3.12, at src/Form.php

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