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 / functions.php

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

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