timed-submit.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. function ninja_forms_register_field_timed_submit(){
  3. $args = array(
  4. 'name' => __( 'Timed Submit', 'ninja-forms' ),
  5. 'edit_options' => array(
  6. array(
  7. 'type' => 'text',
  8. 'name' => 'label',
  9. 'label' => __( 'Label', 'ninja-forms' ),
  10. 'desc' => __( 'Submit button text after timer expires', 'ninja-forms' ),
  11. 'default' => __( 'Submit', 'ninja-forms' ),
  12. 'class' => 'widefat ninja-forms-field-label'
  13. ),
  14. array(
  15. 'type' => 'text',
  16. 'name' => 'timer-text',
  17. 'label' => __( 'Label', 'ninja-forms' ),
  18. 'default' => __( 'Please wait %n seconds', 'ninja-forms' ),
  19. 'desc' => __( '%n will be used to signify the number of seconds', 'ninja-forms' ),
  20. 'width' => 'wide',
  21. 'class' => 'widefat ninja-forms-field-label'
  22. ),
  23. array(
  24. 'type' => 'number',
  25. 'name' => 'countdown',
  26. 'label' => __( 'Number of seconds for countdown', 'ninja-forms' ),
  27. 'desc' => __( 'This is how long a user must wait to submit the form', 'ninja-forms' ),
  28. 'default' => 10,
  29. ),
  30. ),
  31. 'display_function' => 'ninja_forms_field_timed_submit_display',
  32. 'group' => 'standard_fields',
  33. 'edit_label' => false,
  34. 'edit_label_pos' => false,
  35. 'edit_req' => false,
  36. 'default_label' => __( 'Submit', 'ninja-forms' ),
  37. 'edit_desc' => false,
  38. 'edit_custom_class' => true,
  39. 'edit_help' => false,
  40. 'edit_meta' => false,
  41. 'sidebar' => 'template_fields',
  42. 'display_label' => false,
  43. 'edit_conditional' => true,
  44. 'conditional' => array(
  45. 'value' => array(
  46. 'type' => 'text',
  47. ),
  48. ),
  49. 'process_field' => false,
  50. 'pre_process' => 'ninja_forms_field_timed_submit_pre_process',
  51. 'limit' => 1,
  52. 'visible' => false
  53. );
  54. ninja_forms_register_field('_timed_submit', $args);
  55. }
  56. add_action('init', 'ninja_forms_register_field_timed_submit');
  57. function ninja_forms_field_timed_submit_display( $field_id, $data, $form_id = '' ){
  58. $field_class = ninja_forms_get_field_class( $field_id, $form_id );
  59. if(isset($data['timer-text']) AND $data['timer-text'] != ''){
  60. $label = $data['timer-text'];
  61. }else{
  62. $label = __( 'Please wait %n seconds', 'ninja-forms' );
  63. }
  64. if(isset($data['countdown']) AND $data['countdown'] != ''){
  65. $countdown = $data['countdown'];
  66. }else{
  67. $countdown = '10';
  68. }
  69. if(isset($data['label']) AND $data['label'] != ''){
  70. $submit_text = $data['label'];
  71. }else{
  72. $submit_text = __( 'Submit', 'ninja-forms' );
  73. }
  74. $label = preg_replace( "/%n/", "<span>" . $countdown . "</span>", $label);
  75. ?>
  76. <input id="ninja_forms_field_<?php echo $field_id;?>_js" name="ninja_forms_field_<?php echo $field_id;?>[no-js]" type="hidden" value="1" rel="<?php echo $field_id;?>_js" class="no-js" />
  77. <button type="submit" name="ninja_forms_field_<?php echo $field_id;?>[timer]" class="<?php echo $field_class;?> countdown-timer" id="ninja_forms_field_<?php echo $field_id;?>" value="<?php echo $countdown;?>" rel="<?php echo $field_id;?>" data-countdown="<?php echo $countdown;?>" data-text="<?php esc_attr_e( $submit_text );?>"><?php echo $label ;?></button>
  78. <?php
  79. }
  80. function ninja_forms_field_timed_submit_pre_process( $field_id, $user_value ){
  81. global $ninja_forms_processing;
  82. $plugin_settings = nf_get_settings();
  83. if ( isset ( $plugin_settings['timed_submit_error'] ) ) {
  84. $timed_submit_error = __( $plugin_settings['timed_submit_error'], 'ninja-forms' );
  85. } else {
  86. $timed_submit_error = __('If you are a human, please slow down.', 'ninja-forms');
  87. }
  88. if ( isset ( $plugin_settings['javascript_error'] ) ) {
  89. $javascript_error = __( $plugin_settings['javascript_error'], 'ninja-forms' );
  90. } else {
  91. $javascript_error = __( 'You need JavaScript to submit this form. Please enable it and try again.', 'ninja-forms' );
  92. }
  93. if ( isset ( $user_value['no-js'] ) ){
  94. $ninja_forms_processing->add_error('javascript-general', $javascript_error, 'general' );
  95. } else {
  96. $timer = isset( $user_value['timer'] ) ? $user_value['timer'] : 10;
  97. if( intval( $timer ) > 0 ){
  98. $ninja_forms_processing->add_error('timer-'.$field_id, $timed_submit_error, $field_id);
  99. }
  100. }
  101. }