form.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. add_filter('gform_field_content', 'boot_gf', 10, 5);
  3. function boot_gf ($content, $field, $value, $lead_id, $form_id) {
  4. if($field["type"] != 'hidden' && $field["type"] != 'list' && $field["type"] != 'multiselect' && $field["type"] != 'checkbox' && $field["type"] != 'fileupload' && $field["type"] != 'date' && $field["type"] != 'html' && $field["type"] != 'address') {
  5. $content = str_replace('class=\'medium', 'class=\'form-control medium', $content);
  6. }
  7. if($field["type"] == 'name' || $field["type"] == 'address') {
  8. //$content = str_replace('<input ', '<input class=\'form-control\' ', $content);
  9. }
  10. if($field["type"] == 'textarea') {
  11. $content = str_replace('class=\'textarea', 'class=\'form-control medium textarea', $content);
  12. }
  13. if($field["type"] == 'checkbox') {
  14. $content = str_replace('li class=\'', 'li class=\'checkbox ', $content);
  15. $content = str_replace('<input ', '<input style=\'margin-left:1px;\' ', $content);
  16. }
  17. if($field["type"] == 'radio') {
  18. $content = str_replace('li class=\'', 'li class=\'radio ', $content);
  19. $content = str_replace('<input ', '<input style=\'margin-left:1px;\' ', $content);
  20. }
  21. return $content;
  22. }
  23. add_filter("gform_init_scripts_footer", "init_gscripts");
  24. function init_gscripts() {
  25. return true;
  26. }
  27. add_action( 'wp_print_scripts', 'dw_deregister_gf_js', 100 );
  28. function dw_deregister_gf_js() {
  29. if( is_page( 'contact' ) ) {
  30. //wp_deregister_script( 'gform_conditional_logic' );
  31. //wp_deregister_script( 'gform_json' );
  32. //wp_deregister_script( 'gform_datepicker_init' );
  33. //wp_deregister_script( 'gforms_stripe_frontend' );
  34. }
  35. }
  36. add_filter('gform_validation_message', 'change_validation_message', 10, 2);
  37. function change_validation_message($message, $form)
  38. {
  39. return "<div class='validation_error'><strong>Oops!</strong> Looks like something is missing.</div>";
  40. }
  41. add_filter( 'gform_submit_button', 'form_submit_button', 10, 5 );
  42. function form_submit_button ( $button, $form ){
  43. $button = str_replace( "input", "button", $button );
  44. $button = str_replace( "/", "", $button );
  45. $button .= "{$form['button']['text']}</button>";
  46. return $button;
  47. }
  48. /*============================================
  49. GF- Date Assist
  50. ==============================================*/
  51. class DW_GF_Date {
  52. public function __construct( $args = array() ) {
  53. $this->_args = wp_parse_args( $args, array(
  54. 'form_id' => false,
  55. 'target_field_id' => false,
  56. 'source_field_id' => false,
  57. 'format' => 'Y-m-d',
  58. 'modifier' => false
  59. ) );
  60. if( ! $this->_args['form_id'] || ! $this->_args['target_field_id'] ) {
  61. return;
  62. }
  63. add_action( 'init', array( $this, 'init' ) );
  64. }
  65. public function init() {
  66. if( $this->_args['source_field_id'] ) {
  67. add_action( 'gform_pre_submission', array( $this, 'populate_date_on_pre_submission' ) );
  68. } else {
  69. add_filter( 'gform_pre_render', array( $this, 'populate_date_on_pre_render' ) );
  70. }
  71. }
  72. public function populate_date_on_pre_render( $form ) {
  73. foreach( $form['fields'] as &$field ) {
  74. if( $field['id'] == $this->_args['target_field_id'] ) {
  75. $key = sprintf( 'gwpd_%d_%d', $form['id'], $field['id'] );
  76. $value = $this->get_modified_date( $field );
  77. $field['allowsPrepopulate'] = true;
  78. $field['inputName'] = $key;
  79. add_filter("gform_field_value_{$key}", create_function( '', 'return \'' . $value . '\';' ) );
  80. }
  81. }
  82. return $form;
  83. }
  84. public function populate_date_on_pre_submission( $form ) {
  85. foreach( $form['fields'] as &$field ) {
  86. if( $field['id'] == $this->_args['target_field_id'] ) {
  87. $timestamp = strtotime( rgpost( 'input_' . $this->_args['source_field_id'] ) );
  88. $value = $this->get_modified_date( $field, $timestamp );
  89. $_POST[ "input_{$field['id']}" ] = $value;
  90. }
  91. }
  92. }
  93. public function get_modified_date( $field, $timestamp = false ) {
  94. if( ! $timestamp ) {
  95. $timestamp = current_time( 'timestamp' );
  96. }
  97. if( GFFormsModel::get_input_type( $field ) == 'date' ) {
  98. list( $format, $divider ) = $field['dateFormat'] ? array_pad( explode( '_', $field['dateFormat' ] ), 2, 'slash' ) : array( 'mdy', 'slash' );
  99. $dividers = array( 'slash' => '/', 'dot' => '.', 'dash' => '-' );
  100. $format = str_replace( 'y', 'Y', $format );
  101. $divider = $dividers[$divider];
  102. $format = implode( $divider, str_split( $format ) );
  103. } else {
  104. $format = $this->_args['format'];
  105. }
  106. if( $this->_args['modifier'] ) {
  107. $date = date( $format, strtotime( $this->_args['modifier'], $timestamp ) );
  108. } else {
  109. $date = date( $format, $timestamp );
  110. }
  111. return $date;
  112. }
  113. }
  114. new DW_GF_Date( array(
  115. 'form_id' => 1,
  116. 'target_field_id' => 17,
  117. 'modifier' => '+4 months'
  118. ) );
  119. ?>