PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.3.14
HTML Forms – Simple WordPress Forms Plugin v1.3.14
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 / functions.php

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

342 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use HTML_Forms\Form;
4 use HTML_Forms\Submission;
5
6 /**
7 * @param array $args
8 * @return array
9 */
10 function hf_get_forms(array $args = array()) {
11 $default_args = array(
12 'post_type' => 'html-form',
13 'post_status' => array( 'publish', 'draft', 'pending', 'future' ),
14 'posts_per_page' => -1,
15 'ignore_sticky_posts' => true,
16 'no_found_rows' => true,
17 );
18 $args = array_merge($default_args, $args);
19 $query = new WP_Query;
20 $posts = $query->query($args);
21 $forms = array_map('hf_get_form', $posts);
22 return $forms;
23 }
24
25 /**
26 * @param $form_id_or_slug int|string|WP_Post
27 * @return Form
28 * @throws Exception
29 */
30 function hf_get_form( $form_id_or_slug ) {
31
32 if( is_numeric( $form_id_or_slug ) || $form_id_or_slug instanceof WP_Post ) {
33 $post = get_post( $form_id_or_slug );
34
35 if( ! $post instanceof WP_Post || $post->post_type !== 'html-form' ) {
36 throw new Exception( "Invalid form ID" );
37 }
38 } else {
39
40 $query = new WP_Query;
41 $posts = $query->query(array(
42 'post_type' => 'html-form',
43 'name' => $form_id_or_slug,
44 'post_status' => 'publish',
45 'posts_per_page' => 1,
46 'ignore_sticky_posts' => true,
47 'no_found_rows' => true,
48 ));
49 if( empty( $posts ) ) {
50 throw new Exception( 'Invalid form slug' );
51 }
52 $post = $posts[0];
53 }
54
55 // get all post meta in a single call for performance
56 $post_meta = get_post_meta( $post->ID );
57
58 // grab & merge form settings
59 $default_settings = array(
60 'save_submissions' => 1,
61 'hide_after_success' => 0,
62 'redirect_url' => '',
63 'required_fields' =>'',
64 'email_fields' => '',
65 );
66 $default_settings = apply_filters( 'hf_form_default_settings', $default_settings );
67 $settings = array();
68 if( ! empty( $post_meta['_hf_settings'][0] ) ) {
69 $settings = (array) maybe_unserialize( $post_meta['_hf_settings'][0] );
70 }
71 $settings = array_merge( $default_settings, $settings );
72
73 // grab & merge form messages
74 $default_messages = array(
75 'success' => __('Thank you! We will be in touch soon.', 'html-forms'),
76 'invalid_email' => __( 'Sorry, that email address looks invalid.', 'html-forms' ),
77 'required_field_missing' => __( "Please fill in the required fields.", "html-forms" ),
78 'error' => __( 'Oops. An error occurred.', 'html-forms' ),
79 );
80 $default_messages = apply_filters( 'hf_form_default_messages', $default_messages );
81 $messages = array();
82 foreach( $post_meta as $meta_key => $meta_values ) {
83 if( strpos( $meta_key, 'hf_message_' ) === 0 ) {
84 $message_key = substr( $meta_key, strlen( 'hf_message_' ) );
85 $messages[$message_key] = (string) $meta_values[0];
86 }
87 }
88 $messages = array_merge( $default_messages, $messages );
89
90 // finally, create form instance
91 $form = new Form( $post->ID );
92 $form->title = $post->post_title;
93 $form->slug = $post->post_name;
94 $form->markup = $post->post_content;
95 $form->settings = $settings;
96 $form->messages = $messages;
97 return $form;
98 }
99
100 /**
101 * @param $form_id
102 * @param array $args
103 * @return Submission[]
104 */
105 function hf_get_form_submissions( $form_id, array $args = array() ) {
106 $default_args = array(
107 'offset' => 0,
108 'limit' => 1000,
109 );
110 $args = array_merge( $default_args, $args );
111
112 global $wpdb;
113 $table = $wpdb->prefix .'hf_submissions';
114 $results = $wpdb->get_results( $wpdb->prepare( "SELECT s.* FROM {$table} s WHERE s.form_id = %d ORDER BY s.submitted_at DESC LIMIT %d, %d;", $form_id, $args['offset'], $args['limit'] ), OBJECT_K );
115 $submissions = array();
116 foreach( $results as $key => $object ) {
117 $submission = Submission::from_object( $object );
118 $submissions[$key] = $submission;
119 }
120 return $submissions;
121 }
122
123 /**
124 * @param int $submission_id
125 * @return Submission
126 */
127 function hf_get_form_submission( $submission_id ) {
128 global $wpdb;
129 $table = $wpdb->prefix .'hf_submissions';
130 $object = $wpdb->get_row( $wpdb->prepare( "SELECT s.* FROM {$table} s WHERE s.id = %d;", $submission_id ), OBJECT );
131 $submission = Submission::from_object( $object );
132 return $submission;
133 }
134 /**
135 * @return array
136 */
137 function hf_get_settings() {
138 $default_settings = array(
139 'load_stylesheet' => 0,
140 );
141
142 $settings = get_option( 'hf_settings', null );
143
144 // prevent a SQL query when option does not yet exist
145 if ($settings === null) {
146 update_option('hf_settings', array(), true);
147 $settings = array();
148 }
149
150 // merge with default settings
151 $settings = array_merge( $default_settings, $settings );
152
153 /**
154 * Filters the global HTML Forms hf_settings
155 *
156 * @param array $settings
157 */
158 $settings = apply_filters( 'hf_settings', $settings );
159
160 return $settings;
161 }
162
163 /**
164 * Get element from array, allows for dot notation eg: "foo.bar"
165 *
166 * @param array $array
167 * @param string $key
168 * @param mixed $default
169 * @return mixed
170 */
171 function hf_array_get( $array, $key, $default = null ) {
172 if ( is_null( $key ) ) {
173 return $array;
174 }
175
176 if ( isset( $array[$key] ) ) {
177 return $array[$key];
178 }
179
180 foreach (explode( '.', $key ) as $segment) {
181 if ( ! is_array( $array ) || ! array_key_exists( $segment, $array ) ) {
182 return $default;
183 }
184
185 $array = $array[$segment];
186 }
187
188 return $array;
189 }
190
191 /**
192 * Processes template tags like {{user.user_email}}
193 *
194 * @param string $template
195 *
196 * @return string
197 */
198 function hf_template( $template ) {
199 $replacers = new HTML_Forms\TagReplacers();
200 $tags = array(
201 'user' => array( $replacers, 'user' ),
202 'post' => array( $replacers, 'post'),
203 'url_params' => array( $replacers, 'url_params' ),
204 );
205
206 /**
207 * Filters the available tags in HTML Forms templates, like {{user.user_email}}.
208 *
209 * Can be used to add simple scalar replacements or more advanced replacement functions that accept a parameter.
210 *
211 * @param array $tags
212 */
213 $tags = apply_filters( 'hf_template_tags', $tags );
214
215 $template = preg_replace_callback( '/\{\{ *(\w+)(?:\.([\w\.]+))? *(?:\|\| *(\w+))? *\}\}/', function( $matches ) use ( $tags ) {
216 $tag = $matches[1];
217 $param = ! isset( $matches[2] ) ? "" : $matches[2];
218 $default = ! isset( $matches[3] ) ? "" : $matches[3];
219
220 // do not change anything if we have no replacer with that key, could be custom user logic or another plugin.
221 if( ! isset( $tags[ $tag] ) ) {
222 return $matches[0];
223 }
224
225 $replacement = $tags[$tag];
226 $value = is_callable( $replacement ) ? call_user_func_array( $replacement, array( $param ) ) : $replacement;
227 return ! empty( $value ) ? $value : $default;
228 }, $template );
229
230 return $template;
231 }
232
233 /**
234 * @param string $string
235 * @param array $data
236 * @param Closure|string $escape_function
237 * @return string
238 */
239 function hf_replace_data_variables($string, $data = array(), $escape_function = null) {
240 $string = preg_replace_callback( '/\[([a-zA-Z0-9\-\._]+)\]/', function($matches) use ($data, $escape_function) {
241 $key = $matches[1];
242 $replacement = hf_array_get( $data, $key, '' );
243 $replacement = hf_field_value( $replacement, 0, $escape_function);
244 return $replacement;
245 }, $string );
246 return $string;
247 }
248
249 /**
250 * Returns a formatted & HTML-escaped field value. Detects file-, array- and date-types.
251 *
252 * Caveat: if value is a file, an HTML string is returned (which means email action should use "Content-Type: html" when it includes a file field).
253 *
254 * @param string|array $value
255 * @param int $limit
256 * @param Closure|string $escape_function
257 * @return string
258 * @since 1.3.1
259 */
260 function hf_field_value( $value, $limit = 0, $escape_function = 'esc_html') {
261 if( $value === '' ) {
262 return $value;
263 }
264
265 if( hf_is_file( $value ) ) {
266 $file_url = isset( $value['url'] ) ? $value['url'] : '';
267 if( isset( $value['attachment_id'] ) ) {
268 $file_url = admin_url( sprintf( 'post.php?action=edit&post=%d', $value['attachment_id'] ) );
269 }
270 $short_name = substr( $value['name'], 0, 20 );
271 $suffix = strlen( $value['name'] ) > 20 ? '...' : '';
272 return sprintf( '<a href="%s">%s%s</a> (%s)', esc_attr( $file_url ), esc_html( $short_name ), esc_html( $suffix ), hf_human_filesize( $value['size'] ) );
273 }
274
275 if( hf_is_date( $value ) ) {
276 $date_format = get_option( 'date_format' );
277 return date( $date_format, strtotime( $value ) );
278 }
279
280 // join array-values with comma
281 if( is_array( $value ) ) {
282 $value = join( ', ', $value );
283 }
284
285 // limit string to certain length
286 if( $limit > 0 ) {
287 $limited = strlen($value) > $limit;
288 $value = substr( $value, 0, $limit );
289
290 if ($limited) {
291 $value .= '...';
292 }
293 }
294
295 // escape value
296 if ($escape_function !== null && is_callable($escape_function)) {
297 $value = $escape_function($value);
298 }
299
300 return $value;
301 }
302
303 /**
304 * Returns true if value is a "file"
305 *
306 * @param mixed $value
307 * @return bool
308 */
309 function hf_is_file( $value ) {
310 return is_array( $value )
311 && isset( $value['name'] )
312 && isset( $value['size'] )
313 && isset( $value['type'] );
314 }
315
316 /**
317 * Returns true if value looks like a date-string submitted from a <input type="date">
318 * @param mixed $value
319 * @return bool
320 * @since 1.3.1
321 */
322 function hf_is_date( $value ) {
323 return is_string( $value )
324 && strlen( $value ) === 10
325 && preg_match( '/\d{2,4}[-\/]\d{2}[-\/]\d{2,4}/', $value ) > 0
326 && ( $timestamp = strtotime($value) )
327 && $timestamp != false;
328 }
329
330 /**
331 * @param int $size
332 * @param int $precision
333 * @return string
334 */
335 function hf_human_filesize($size, $precision = 2) {
336 for( $i = 0; ($size / 1024) > 0.9; $i++, $size /= 1024 ) {
337 // nothing, loop logic contains everything
338 }
339 $steps = array( 'B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' );
340 return round($size, $precision) . $steps[$i];
341 }
342