PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.0
HTML Forms – Simple WordPress Forms Plugin v1.0
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 / Forms.php

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

239 lines 7.4 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 Forms
6 {
7
8 /**
9 * @var string
10 */
11 private $plugin_file;
12
13 /**
14 * @var array
15 */
16 private $settings;
17
18 /**
19 * Forms constructor.
20 *
21 * @param string $plugin_file
22 * @param array $settings
23 */
24 public function __construct($plugin_file, array $settings )
25 {
26 $this->plugin_file = $plugin_file;
27 $this->settings = $settings;
28 }
29
30 public function hook()
31 {
32 add_action('init', array($this, 'register'));
33 add_action('init', array($this, 'listen'));
34 add_action('wp_enqueue_scripts', array($this, 'assets'));
35 }
36
37 public function register()
38 {
39 // register post type
40 register_post_type('html-form', array(
41 'labels' => array(
42 'name' => 'HTML Forms',
43 'singular_name' => 'HTML Form',
44 ),
45 'public' => false
46 )
47 );
48
49 add_shortcode('hf_form', array($this, 'shortcode'));
50
51 // enable shortcodes in text widgets
52 add_filter( 'widget_text', 'shortcode_unautop' );
53 add_filter( 'widget_text', 'do_shortcode', 11 );
54 }
55
56 public function assets()
57 {
58 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
59 wp_enqueue_script('html-forms', plugins_url('assets/js/public'. $suffix .'.js', $this->plugin_file), array(), HTML_FORMS_VERSION, true);
60 wp_localize_script('html-forms', 'hf_js_vars', array(
61 'ajax_url' => admin_url('admin-ajax.php'),
62 ));
63
64 if( $this->settings['load_stylesheet'] ) {
65 wp_enqueue_style( 'html-forms', plugins_url( 'assets/css/forms' . $suffix . '.css', $this->plugin_file ), array(), HTML_FORMS_VERSION );
66 }
67 }
68
69 /**
70 * @param Form $form
71 * @param array $data
72 * @return string
73 */
74 private function validate_form(Form $form, array $data)
75 {
76 $honeypot_key = sprintf( '_hf_h%d', $form->ID );
77 if( ! isset( $data[$honeypot_key] ) || $data[$honeypot_key] !== "" ) {
78 return 'spam';
79 }
80
81 /**
82 * This filter allows you to perform your own form validation.
83 *
84 * Return a non-empty string if you want to raise an error.
85 * Error codes with a specific error message are: "required_field_missing", "invalid_email", and "error"
86 *
87 * @param string $error_code
88 * @param Form $form
89 * @param array $data
90 */
91 $error = apply_filters( 'hf_validate_form', '', $form, $data );
92 if( ! empty( $error ) ) {
93 return $error;
94 }
95
96 $required_fields = $form->get_required_fields();
97 foreach ($required_fields as $field_name) {
98 $value = hf_array_get( $data, $field_name );
99 if ( empty( $value ) ) {
100 return 'required_field_missing';
101 }
102 }
103
104 $email_fields = $form->get_email_fields();
105 foreach ($email_fields as $field_name) {
106 $value = hf_array_get( $data, $field_name );
107 if ( ! empty( $value ) && ! is_email( $value ) ) {
108 return 'invalid_email';
109 }
110 }
111
112 // all good: no errors!
113 return '';
114 }
115
116 public function sanitize( $value )
117 {
118 if (is_string($value)) {
119 // strip all HTML tags & whitespace
120 $value = trim(strip_tags($value));
121
122 // convert &amp; back to &
123 $value = html_entity_decode($value, ENT_NOQUOTES);
124 } elseif (is_array($value)) {
125 // filter out empty values
126 $value = array_filter( $value, function( $v ) {
127 return ! empty( $v );
128 });
129
130 $value = array_map(array( $this, 'sanitize' ), $value);
131 } elseif (is_object($value)) {
132 $vars = get_object_vars($value);
133 foreach ($vars as $key => $data) {
134 $value->{$key} = $this->sanitize($data);
135 }
136 }
137
138 return $value;
139 }
140
141 public function listen() {
142 // only respond to AJAX requests with _hf_form_id set.
143 if (empty($_POST['_hf_form_id'])
144 || empty( $_SERVER['HTTP_X_REQUESTED_WITH'] )
145 || strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) !== strtolower( 'XMLHttpRequest' ) ) {
146 return;
147 }
148
149 $data = $_POST;
150 $form_id = (int) $data['_hf_form_id'];
151 $form = hf_get_form($form_id);
152 $error_code = $this->validate_form($form, $data);
153
154 if (empty( $error_code ) ) {
155
156 // filter out all field names starting with _
157 $data = array_filter( $data, function( $k ) {
158 return ! empty( $k ) && $k[0] !== '_';
159 }, ARRAY_FILTER_USE_KEY );
160
161 // strip slashes
162 $data = stripslashes_deep( $data );
163
164 // sanitize data: strip tags etc.
165 $data = $this->sanitize( $data );
166
167 // save form submission
168 $submission = new Submission();
169 $submission->form_id = $form_id;
170 $submission->data = $data;
171 $submission->ip_address = sanitize_text_field( $_SERVER['REMOTE_ADDR'] );
172 $submission->user_agent = sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] );
173 $submission->referer_url = sanitize_text_field( $_SERVER['HTTP_REFERER'] );
174 $submission->save();
175
176 // process form actions
177 foreach( $form->settings['actions'] as $action_settings ) {
178 /**
179 * Processes the specified form action and passes related data.
180 *
181 * @param array $action_settings
182 * @param Submission $submission
183 * @param Form $form
184 */
185 do_action('hf_process_form_action_' . $action_settings['type'], $action_settings, $submission, $form );
186 }
187
188 /**
189 * General purpose hook after all form actions have been processed.
190 *
191 * @param Submission $submission
192 * @param Form $form
193 */
194 do_action( 'hf_form_success', $submission, $form );
195
196 $response = array(
197 'message' => array(
198 'type' => 'success',
199 'text' => $form->messages['success'],
200 ),
201 'hide_form' => (bool)$form->settings['hide_after_success'],
202 );
203
204 if (!empty($form->settings['redirect_url'])) {
205 $response['redirect_url'] = $form->settings['redirect_url'];
206 }
207 } else {
208 $response = array(
209 'message' => array(
210 'type' => 'warning',
211 'text' => isset( $form->messages[ $error_code ] ) ? $form->messages[ $error_code ] : $form->messages['error'],
212 ),
213 'error' => $error_code,
214 );
215
216 /**
217 * General purpose hook for when a form error occurred
218 *
219 * @param string $error_code
220 * @param Form $form
221 * @param array $data
222 */
223 do_action( 'hf_form_error', $error_code, $form, $data );
224 }
225
226 send_origin_headers();
227 send_nosniff_header();
228 nocache_headers();
229
230 wp_send_json($response, 200);
231 exit;
232 }
233
234 public function shortcode($attributes = array(), $content = '')
235 {
236 $form = hf_get_form($attributes['slug']);
237 return $form . $content;
238 }
239 }