PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.3.30
HTML Forms – Simple WordPress Forms Plugin v1.3.30
1.7.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 All 67 releases
← All changes | src/functions.php +344 -204 1.3.101.3.30 View file →
@@ -6,18 +6,21 @@
6 6 /**
7 7 * @param array $args
8 8 * @return array
9 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;
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;
20 23 }
21 24
22 25 /**
23 26 * @param $form_id_or_slug int|string|WP_Post
@@ -25,96 +28,110 @@
25 28 * @throws Exception
26 29 */
27 30 function hf_get_form( $form_id_or_slug ) {
28 31
29 - if( is_numeric( $form_id_or_slug ) || $form_id_or_slug instanceof WP_Post ) {
30 - $post = get_post( $form_id_or_slug );
32 + if ( is_numeric( $form_id_or_slug ) || $form_id_or_slug instanceof WP_Post ) {
33 + $post = get_post( $form_id_or_slug );
31 34
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 - );
35 + if ( ! $post instanceof WP_Post || $post->post_type !== 'html-form' ) {
36 + throw new Exception( 'Invalid form ID' );
37 + }
38 + } else {
44 39
45 - if( empty( $posts ) ) {
46 - throw new Exception( 'Invalid form slug' );
47 - }
48 - $post = $posts[0];
49 - }
40 + $query = new WP_Query;
41 + $posts = $query->query(
42 + array(
43 + 'post_type' => 'html-form',
44 + 'name' => $form_id_or_slug,
45 + 'post_status' => 'publish',
46 + 'posts_per_page' => 1,
47 + 'ignore_sticky_posts' => true,
48 + 'no_found_rows' => true,
49 + )
50 + );
51 + if ( empty( $posts ) ) {
52 + throw new Exception( 'Invalid form slug' );
53 + }
54 + $post = $posts[0];
55 + }
50 56
51 - // get all post meta in a single call for performance
52 - $post_meta = get_post_meta( $post->ID );
57 + // get all post meta in a single call for performance
58 + $post_meta = get_post_meta( $post->ID );
53 59
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 );
60 + // grab & merge form settings
61 + $default_settings = array(
62 + 'save_submissions' => 1,
63 + 'hide_after_success' => 0,
64 + 'redirect_url' => '',
65 + 'required_fields' => '',
66 + 'email_fields' => '',
67 + );
68 + $default_settings = apply_filters( 'hf_form_default_settings', $default_settings );
69 + $settings = array();
70 + if ( ! empty( $post_meta['_hf_settings'][0] ) ) {
71 + $settings = (array) maybe_unserialize( $post_meta['_hf_settings'][0] );
72 + }
73 + $settings = array_merge( $default_settings, $settings );
68 74
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 + // grab & merge form messages
76 + $default_messages = array(
77 + 'success' => __( 'Thank you! We will be in touch soon.', 'html-forms' ),
78 + 'invalid_email' => __( 'Sorry, that email address looks invalid.', 'html-forms' ),
79 + 'required_field_missing' => __( 'Please fill in the required fields.', 'html-forms' ),
80 + 'error' => __( 'Oops. An error occurred.', 'html-forms' ),
75 81 );
76 82 $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 );
83 + $messages = array();
84 + foreach ( $post_meta as $meta_key => $meta_values ) {
85 + if ( strpos( $meta_key, 'hf_message_' ) === 0 ) {
86 + $message_key = substr( $meta_key, strlen( 'hf_message_' ) );
87 + $messages[ $message_key ] = (string) $meta_values[0];
88 + }
89 + }
90 + $messages = array_merge( $default_messages, $messages );
85 91
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;
92 + // finally, create form instance
93 + $form = new Form( $post->ID );
94 + $form->title = $post->post_title;
95 + $form->slug = $post->post_name;
96 + $form->markup = $post->post_content;
97 + $form->settings = $settings;
98 + $form->messages = $messages;
99 + return $form;
94 100 }
95 101
96 102 /**
97 103 * @param $form_id
104 + * @return int
105 + */
106 +function hf_count_form_submissions( $form_id ) {
107 + global $wpdb;
108 + $table = $wpdb->prefix . 'hf_submissions';
109 + $result = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$table} s WHERE s.form_id = %d;", $form_id ) );
110 + return (int) $result;
111 +}
112 +
113 +/**
114 + * @param $form_id
98 115 * @param array $args
99 116 * @return Submission[]
100 117 */
101 118 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 );
119 + $default_args = array(
120 + 'offset' => 0,
121 + 'limit' => 1000,
122 + );
123 + $args = array_merge( $default_args, $args );
107 124
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;
125 + global $wpdb;
126 + $table = $wpdb->prefix . 'hf_submissions';
127 + $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 );
128 + $submissions = array();
129 + foreach ( $results as $key => $object ) {
130 + $submission = Submission::from_object( $object );
131 + $submissions[ $key ] = $submission;
132 + }
133 + return $submissions;
117 134 }
118 135
119 136 /**
120 137 * @param int $submission_id
@@ -120,35 +137,41 @@
120 137 * @param int $submission_id
121 138 * @return Submission
122 139 */
123 140 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;
141 + global $wpdb;
142 + $table = $wpdb->prefix . 'hf_submissions';
143 + $object = $wpdb->get_row( $wpdb->prepare( "SELECT s.* FROM {$table} s WHERE s.id = %d;", $submission_id ), OBJECT );
144 + $submission = Submission::from_object( $object );
145 + return $submission;
129 146 }
130 147 /**
131 148 * @return array
132 149 */
133 150 function hf_get_settings() {
134 - $default_settings = array(
135 - 'load_stylesheet' => 0,
136 - );
151 + $default_settings = array(
152 + 'load_stylesheet' => 0,
153 + );
137 154
138 - $settings = get_option( 'hf_settings', array() );
155 + $settings = get_option( 'hf_settings', null );
139 156
140 - // merge with default settings
141 - $settings = array_merge( $default_settings, $settings );
157 + // prevent a SQL query when option does not yet exist
158 + if ( $settings === null ) {
159 + update_option( 'hf_settings', array(), true );
160 + $settings = array();
161 + }
142 162
143 - /**
144 - * Filters the global HTML Forms hf_settings
145 - *
146 - * @param array $settings
147 - */
148 - $settings = apply_filters( 'hf_settings', $settings );
163 + // merge with default settings
164 + $settings = array_merge( $default_settings, $settings );
149 165
150 - return $settings;
166 + /**
167 + * Filters the global HTML Forms hf_settings
168 + *
169 + * @param array $settings
170 + */
171 + $settings = apply_filters( 'hf_settings', $settings );
172 +
173 + return $settings;
151 174 }
152 175
153 176 /**
154 177 * Get element from array, allows for dot notation eg: "foo.bar"
@@ -158,25 +181,25 @@
158 181 * @param mixed $default
159 182 * @return mixed
160 183 */
161 184 function hf_array_get( $array, $key, $default = null ) {
162 - if ( is_null( $key ) ) {
163 - return $array;
164 - }
185 + if ( is_null( $key ) ) {
186 + return $array;
187 + }
165 188
166 - if ( isset( $array[$key] ) ) {
167 - return $array[$key];
168 - }
189 + if ( isset( $array[ $key ] ) ) {
190 + return $array[ $key ];
191 + }
169 192
170 - foreach (explode( '.', $key ) as $segment) {
171 - if ( ! is_array( $array ) || ! array_key_exists( $segment, $array ) ) {
172 - return $default;
173 - }
193 + foreach ( explode( '.', $key ) as $segment ) {
194 + if ( ! is_array( $array ) || ! array_key_exists( $segment, $array ) ) {
195 + return $default;
196 + }
174 197
175 - $array = $array[$segment];
176 - }
198 + $array = $array[ $segment ];
199 + }
177 200
178 - return $array;
201 + return $array;
179 202 }
180 203
181 204 /**
182 205 * Processes template tags like {{user.user_email}}
@@ -185,110 +208,119 @@
185 208 *
186 209 * @return string
187 210 */
188 211 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 - );
212 + $replacers = new HTML_Forms\TagReplacers();
213 + $tags = array(
214 + 'user' => array( $replacers, 'user' ),
215 + 'post' => array( $replacers, 'post' ),
216 + 'url_params' => array( $replacers, 'url_params' ),
217 + );
195 218
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 );
219 + /**
220 + * Filters the available tags in HTML Forms templates, like {{user.user_email}}.
221 + *
222 + * Can be used to add simple scalar replacements or more advanced replacement functions that accept a parameter.
223 + *
224 + * @param array $tags
225 + */
226 + $tags = apply_filters( 'hf_template_tags', $tags );
204 227
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];
228 + $template = preg_replace_callback(
229 + '/\{\{ *(\w+)(?:\.([\w\.]+))? *(?:\|\| *(\w+))? *\}\}/',
230 + function( $matches ) use ( $tags ) {
231 + $tag = $matches[1];
232 + $param = ! isset( $matches[2] ) ? '' : $matches[2];
233 + $default = ! isset( $matches[3] ) ? '' : $matches[3];
209 234
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 - }
235 + // do not change anything if we have no replacer with that key, could be custom user logic or another plugin.
236 + if ( ! isset( $tags[ $tag ] ) ) {
237 + return $matches[0];
238 + }
214 239
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 );
240 + $replacement = $tags[ $tag ];
241 + $value = is_callable( $replacement ) ? call_user_func_array( $replacement, array( $param ) ) : $replacement;
242 + return ! empty( $value ) ? $value : $default;
243 + },
244 + $template
245 + );
219 246
220 - return $template;
247 + return $template;
221 248 }
222 249
223 250 /**
224 251 * @param string $string
225 252 * @param array $data
226 - *
253 + * @param Closure|string $escape_function
227 254 * @return string
228 255 */
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 -}
256 +function hf_replace_data_variables( $string, $data = array(), $escape_function = null ) {
257 + return preg_replace_callback(
258 + '/\[(.+?)\]/',
259 + function( $matches ) use ( $data, $escape_function ) {
260 + $key = $matches[1];
261 + // replace spaces in name with underscores to match PHP requirement for keys in $_POST superglobal
262 + $key = str_replace( ' ', '_', $key );
263 + $replacement = hf_array_get( $data, $key, '' );
264 + $replacement = hf_field_value( $replacement, 0, $escape_function );
265 + return $replacement;
266 + },
267 + $string
268 + );
269 +}
241 270
242 271 /**
243 -* Returns a formatted field value. Detects file-, array- and date-types.
272 +* Returns a formatted & HTML-escaped field value. Detects file-, array- and date-types.
244 273 *
245 274 * 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 275 *
247 276 * @param string|array $value
248 -* @param int $limit
277 +* @param int $limit
278 +* @param Closure|string $escape_function
249 279 * @return string
250 280 * @since 1.3.1
251 281 */
252 -function hf_field_value( $value, $limit = 0 ) {
253 - if( $value === '' ) {
254 - return $value;
255 - }
282 +function hf_field_value( $value, $limit = 0, $escape_function = 'esc_html' ) {
283 + if ( $value === '' ) {
284 + return $value;
285 + }
256 286
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 - }
287 + if ( hf_is_file( $value ) ) {
288 + $file_url = isset( $value['url'] ) ? $value['url'] : '';
289 + if ( isset( $value['attachment_id'] ) && apply_filters( 'hf_file_upload_use_direct_links', false ) === false ) {
290 + $file_url = admin_url( sprintf( 'post.php?action=edit&post=%d', $value['attachment_id'] ) );
291 + }
292 + $short_name = substr( $value['name'], 0, 20 );
293 + $suffix = strlen( $value['name'] ) > 20 ? '...' : '';
294 + 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'] ) );
295 + }
266 296
267 - if( hf_is_date( $value ) ) {
268 - $date_format = get_option( 'date_format' );
269 - return date( $date_format, strtotime( $value ) );
270 - }
297 + if ( hf_is_date( $value ) ) {
298 + $date_format = get_option( 'date_format' );
299 + return gmdate( $date_format, strtotime( $value ) );
300 + }
271 301
272 - // join array-values with comma
273 - if( is_array( $value ) ) {
274 - $value = join( ', ', $value );
275 - }
302 + // join array-values with comma
303 + if ( is_array( $value ) ) {
304 + $value = join( ', ', $value );
305 + }
276 306
277 - // limit string to certain length
278 - if( $limit > 0 ) {
279 - $limited = strlen($value) > $limit;
280 - $value = substr( $value, 0, $limit );
307 + // limit string to certain length
308 + if ( $limit > 0 ) {
309 + $limited = strlen( $value ) > $limit;
310 + $value = substr( $value, 0, $limit );
281 311
282 - if ($limited) {
283 - $value .= '...';
284 - }
285 - }
312 + if ( $limited ) {
313 + $value .= '...';
314 + }
315 + }
286 316
287 - // escape
288 - $value = wp_check_invalid_utf8($value);
289 - $value = htmlentities($value, ENT_NOQUOTES);
290 - return $value;
317 + // escape value
318 + if ( $escape_function !== null && is_callable( $escape_function ) ) {
319 + $value = $escape_function( $value );
320 + }
321 +
322 + return $value;
291 323 }
292 324
293 325 /**
294 326 * Returns true if value is a "file"
@@ -296,12 +328,12 @@
296 328 * @param mixed $value
297 329 * @return bool
298 330 */
299 331 function hf_is_file( $value ) {
300 - return is_array( $value )
301 - && isset( $value['name'] )
302 - && isset( $value['size'] )
303 - && isset( $value['type'] );
332 + return is_array( $value )
333 + && isset( $value['name'] )
334 + && isset( $value['size'] )
335 + && isset( $value['type'] );
304 336 }
305 337
306 338 /**
307 339 * Returns true if value looks like a date-string submitted from a <input type="date">
@@ -309,21 +341,129 @@
309 341 * @return bool
310 342 * @since 1.3.1
311 343 */
312 344 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;
345 + if ( ! is_string( $value )
346 + || strlen( $value ) !== 10
347 + || (int) preg_match( '/\d{2,4}[-\/]\d{2}[-\/]\d{2,4}/', $value ) === 0 ) {
348 + return false;
349 + }
350 +
351 + $timestamp = strtotime( $value );
352 + return $timestamp != false;
318 353 }
319 354
320 -/**
321 -* @return string
355 +/**
356 + * @param int $size
357 + * @param int $precision
358 + * @return string
322 359 */
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];
360 +function hf_human_filesize( $size, $precision = 2 ) {
361 + for ( $i = 0; ( $size / 1024 ) > 0.9; $i++, $size /= 1024 ) {
362 + // nothing, loop logic contains everything
363 + }
364 + $steps = array( 'B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' );
365 + return round( $size, $precision ) . $steps[ $i ];
366 +}
367 +
368 +/**
369 + * Gets all the form tabs to show in the admin.
370 + * @param Form $form
371 + * @return array
372 + */
373 +function hf_get_admin_tabs( Form $form ) {
374 + $tabs = array(
375 + 'fields' => __( 'Fields', 'html-forms' ),
376 + 'messages' => __( 'Messages', 'html-forms' ),
377 + 'settings' => __( 'Settings', 'html-forms' ),
378 + 'actions' => __( 'Actions', 'html-forms' ),
379 + );
380 +
381 + if ( $form->settings['save_submissions'] ) {
382 + $tabs['submissions'] = __( 'Submissions', 'html-forms' );
383 + }
384 + return apply_filters( 'hf_admin_tabs', $tabs, $form );
385 +}
386 +
387 +function _hf_on_plugin_activation() {
388 + if ( is_multisite() ) {
389 + _hf_on_plugin_activation_multisite();
390 + return;
391 + }
392 +
393 + // install table for regular wp install
394 + _hf_create_submissions_table();
395 +
396 + // add "edit_forms" cap to user that activated the plugin
397 + $user = wp_get_current_user();
398 + $user->add_cap( 'edit_forms', true );
399 +}
400 +
401 +function _hf_on_plugin_activation_multisite() {
402 + $added_caps = array();
403 +
404 + foreach ( get_sites( array( 'number' => PHP_INT_MAX ) ) as $site ) {
405 + switch_to_blog( (int) $site->blog_id );
406 +
407 + // install table for current blog
408 + _hf_create_submissions_table();
409 +
410 + // iterate through current blog admins
411 + foreach ( get_users(
412 + array(
413 + 'blog_id' => (int) $site->blog_id,
414 + 'role' => 'administrator',
415 + 'fields' => 'ID',
416 + )
417 + ) as $admin_id ) {
418 + if ( ! (int) $admin_id || in_array( $admin_id, $added_caps ) ) {
419 + continue;
420 + }
421 +
422 + // add "edit_forms" cap to site admin
423 + $user = new \WP_User( (int) $admin_id );
424 + $user->add_cap( 'edit_forms', true );
425 +
426 + $added_caps[] = $admin_id;
427 + }
428 +
429 + restore_current_blog();
430 + }
431 +}
432 +
433 +// install table for main site on regular installs, or active site for multisite
434 +function _hf_create_submissions_table() {
435 + /** @var wpdb */
436 + global $wpdb;
437 +
438 + $charset_collate = $wpdb->get_charset_collate();
439 +
440 + // create table for storing submissions
441 + $table = $wpdb->prefix . 'hf_submissions';
442 + $wpdb->query(
443 + "CREATE TABLE IF NOT EXISTS {$table}(
444 + `id` INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
445 + `form_id` INT UNSIGNED NOT NULL,
446 + `data` TEXT NOT NULL,
447 + `user_agent` TEXT NULL,
448 + `ip_address` VARCHAR(255) NULL,
449 + `referer_url` TEXT NULL,
450 + `submitted_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
451 +) {$charset_collate};"
452 + );
453 +}
454 +
455 +function _hf_on_add_user_to_blog( $user_id, $role, $blog_id ) {
456 + if ( 'administrator' !== $role ) {
457 + return;
458 + }
459 +
460 + // add "edit_forms" cap to site admin
461 + $user = new \WP_User( (int) $user_id );
462 + $user->add_cap( 'edit_forms', true );
463 +}
464 +
465 +function _hf_on_wp_insert_site( \WP_Site $site ) {
466 + switch_to_blog( (int) $site->blog_id );
467 + _hf_create_submissions_table();
468 + restore_current_blog();
329 469 }