PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.3.2
HTML Forms – Simple WordPress Forms Plugin v1.3.2
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 / trunk / src / functions.php

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

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