| @@ -100,8 +100,19 @@ | ||
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | /** |
| 103 | 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 | |
| 104 | 115 | * @param array $args |
| 105 | 116 | * @return Submission[] |
| 106 | 117 | */ |
| 107 | 118 | function hf_get_form_submissions( $form_id, array $args = array() ) { |
| @@ -242,12 +253,14 @@ | ||
| 242 | 253 | * @param Closure|string $escape_function |
| 243 | 254 | * @return string |
| 244 | 255 | */ |
| 245 | 256 | function hf_replace_data_variables( $string, $data = array(), $escape_function = null ) { |
| 246 | - $string = preg_replace_callback( | |
| 247 | - '/\[([a-zA-Z0-9\-\._]+)\]/', | |
| 257 | + return preg_replace_callback( | |
| 258 | + '/\[(.+?)\]/', | |
| 248 | 259 | function( $matches ) use ( $data, $escape_function ) { |
| 249 | - $key = $matches[1]; | |
| 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 ); | |
| 250 | 263 | $replacement = hf_array_get( $data, $key, '' ); |
| 251 | 264 | $replacement = hf_field_value( $replacement, 0, $escape_function ); |
| 252 | 265 | return $replacement; |
| 253 | 266 | }, |
| @@ -252,9 +265,8 @@ | ||
| 252 | 265 | return $replacement; |
| 253 | 266 | }, |
| 254 | 267 | $string |
| 255 | 268 | ); |
| 256 | - return $string; | |
| 257 | 269 | } |
| 258 | 270 | |
| 259 | 271 | /** |
| 260 | 272 | * Returns a formatted & HTML-escaped field value. Detects file-, array- and date-types. |
| @@ -273,9 +285,9 @@ | ||
| 273 | 285 | } |
| 274 | 286 | |
| 275 | 287 | if ( hf_is_file( $value ) ) { |
| 276 | 288 | $file_url = isset( $value['url'] ) ? $value['url'] : ''; |
| 277 | - if ( isset( $value['attachment_id'] ) ) { | |
| 289 | + if ( isset( $value['attachment_id'] ) && apply_filters( 'hf_file_upload_use_direct_links', false ) === false ) { | |
| 278 | 290 | $file_url = admin_url( sprintf( 'post.php?action=edit&post=%d', $value['attachment_id'] ) ); |
| 279 | 291 | } |
| 280 | 292 | $short_name = substr( $value['name'], 0, 20 ); |
| 281 | 293 | $suffix = strlen( $value['name'] ) > 20 ? '...' : ''; |
| @@ -283,9 +295,9 @@ | ||
| 283 | 295 | } |
| 284 | 296 | |
| 285 | 297 | if ( hf_is_date( $value ) ) { |
| 286 | 298 | $date_format = get_option( 'date_format' ); |
| 287 | - return gmdate( $date_format, strtotime( $value ) ); | |
| 299 | + return gmdate( $date_format, strtotime( str_replace( '/', '-', $value ) ) ); | |
| 288 | 300 | } |
| 289 | 301 | |
| 290 | 302 | // join array-values with comma |
| 291 | 303 | if ( is_array( $value ) ) { |
| @@ -306,8 +318,13 @@ | ||
| 306 | 318 | if ( $escape_function !== null && is_callable( $escape_function ) ) { |
| 307 | 319 | $value = $escape_function( $value ); |
| 308 | 320 | } |
| 309 | 321 | |
| 322 | + // add line breaks, if not string limited to certain length | |
| 323 | + if ( $limit === 0 ) { | |
| 324 | + $value = nl2br( $value ); | |
| 325 | + } | |
| 326 | + | |
| 310 | 327 | return $value; |
| 311 | 328 | } |
| 312 | 329 | |
| 313 | 330 | /** |
| @@ -329,9 +346,8 @@ | ||
| 329 | 346 | * @return bool |
| 330 | 347 | * @since 1.3.1 |
| 331 | 348 | */ |
| 332 | 349 | function hf_is_date( $value ) { |
| 333 | - | |
| 334 | 350 | if ( ! is_string( $value ) |
| 335 | 351 | || strlen( $value ) !== 10 |
| 336 | 352 | || (int) preg_match( '/\d{2,4}[-\/]\d{2}[-\/]\d{2,4}/', $value ) === 0 ) { |
| 337 | 353 | return false; |
| @@ -370,5 +386,89 @@ | ||
| 370 | 386 | if ( $form->settings['save_submissions'] ) { |
| 371 | 387 | $tabs['submissions'] = __( 'Submissions', 'html-forms' ); |
| 372 | 388 | } |
| 373 | 389 | return apply_filters( 'hf_admin_tabs', $tabs, $form ); |
| 390 | +} | |
| 391 | + | |
| 392 | +function _hf_on_plugin_activation() { | |
| 393 | + if ( is_multisite() ) { | |
| 394 | + _hf_on_plugin_activation_multisite(); | |
| 395 | + return; | |
| 396 | + } | |
| 397 | + | |
| 398 | + // install table for regular wp install | |
| 399 | + _hf_create_submissions_table(); | |
| 400 | + | |
| 401 | + // add "edit_forms" cap to user that activated the plugin | |
| 402 | + $user = wp_get_current_user(); | |
| 403 | + $user->add_cap( 'edit_forms', true ); | |
| 404 | +} | |
| 405 | + | |
| 406 | +function _hf_on_plugin_activation_multisite() { | |
| 407 | + $added_caps = array(); | |
| 408 | + | |
| 409 | + foreach ( get_sites( array( 'number' => PHP_INT_MAX ) ) as $site ) { | |
| 410 | + switch_to_blog( (int) $site->blog_id ); | |
| 411 | + | |
| 412 | + // install table for current blog | |
| 413 | + _hf_create_submissions_table(); | |
| 414 | + | |
| 415 | + // iterate through current blog admins | |
| 416 | + foreach ( get_users( | |
| 417 | + array( | |
| 418 | + 'blog_id' => (int) $site->blog_id, | |
| 419 | + 'role' => 'administrator', | |
| 420 | + 'fields' => 'ID', | |
| 421 | + ) | |
| 422 | + ) as $admin_id ) { | |
| 423 | + if ( ! (int) $admin_id || in_array( $admin_id, $added_caps ) ) { | |
| 424 | + continue; | |
| 425 | + } | |
| 426 | + | |
| 427 | + // add "edit_forms" cap to site admin | |
| 428 | + $user = new \WP_User( (int) $admin_id ); | |
| 429 | + $user->add_cap( 'edit_forms', true ); | |
| 430 | + | |
| 431 | + $added_caps[] = $admin_id; | |
| 432 | + } | |
| 433 | + | |
| 434 | + restore_current_blog(); | |
| 435 | + } | |
| 436 | +} | |
| 437 | + | |
| 438 | +// install table for main site on regular installs, or active site for multisite | |
| 439 | +function _hf_create_submissions_table() { | |
| 440 | + /** @var wpdb */ | |
| 441 | + global $wpdb; | |
| 442 | + | |
| 443 | + $charset_collate = $wpdb->get_charset_collate(); | |
| 444 | + | |
| 445 | + // create table for storing submissions | |
| 446 | + $table = $wpdb->prefix . 'hf_submissions'; | |
| 447 | + $wpdb->query( | |
| 448 | + "CREATE TABLE IF NOT EXISTS {$table}( | |
| 449 | + `id` INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, | |
| 450 | + `form_id` INT UNSIGNED NOT NULL, | |
| 451 | + `data` TEXT NOT NULL, | |
| 452 | + `user_agent` TEXT NULL, | |
| 453 | + `ip_address` VARCHAR(255) NULL, | |
| 454 | + `referer_url` TEXT NULL, | |
| 455 | + `submitted_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP | |
| 456 | +) {$charset_collate};" | |
| 457 | + ); | |
| 458 | +} | |
| 459 | + | |
| 460 | +function _hf_on_add_user_to_blog( $user_id, $role, $blog_id ) { | |
| 461 | + if ( 'administrator' !== $role ) { | |
| 462 | + return; | |
| 463 | + } | |
| 464 | + | |
| 465 | + // add "edit_forms" cap to site admin | |
| 466 | + $user = new \WP_User( (int) $user_id ); | |
| 467 | + $user->add_cap( 'edit_forms', true ); | |
| 468 | +} | |
| 469 | + | |
| 470 | +function _hf_on_wp_insert_site( \WP_Site $site ) { | |
| 471 | + switch_to_blog( (int) $site->blog_id ); | |
| 472 | + _hf_create_submissions_table(); | |
| 473 | + restore_current_blog(); | |
| 374 | 474 | } |