| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Date Field Class |
| 5 |
*/ |
| 6 |
class WeForms_Form_Field_Date_Free extends WeForms_Field_Contract { |
| 7 |
|
| 8 |
public function __construct() { |
| 9 |
$this->name = __( 'Date / Time', 'weforms' ); |
| 10 |
$this->input_type = 'date_field'; |
| 11 |
$this->icon = 'calendar-o'; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Render the text field |
| 16 |
* |
| 17 |
* @param array $field_settings |
| 18 |
* @param int $form_id |
| 19 |
* |
| 20 |
* @return void |
| 21 |
*/ |
| 22 |
public function render( $field_settings, $form_id ) { |
| 23 |
$value = ''; ?> |
| 24 |
<li <?php $this->print_list_attributes( $field_settings ); ?>> |
| 25 |
<?php $this->print_label( $field_settings ); ?> |
| 26 |
|
| 27 |
<div class="wpuf-fields"> |
| 28 |
<input id="wpuf-date-<?php echo esc_attr( $field_settings['name'] ); ?>" type="text" class="datepicker <?php echo ' wpuf_'.esc_attr( $field_settings['name'] ).'_'. esc_attr($form_id); ?>" data-required="<?php echo esc_attr($field_settings['required']) ?>" data-type="text" name="<?php echo esc_attr( $field_settings['name'] ); ?>" placeholder="<?php echo esc_attr( $field_settings['format'] ); ?>" value="<?php echo esc_attr( $value ) ?>" size="30" /> |
| 29 |
<?php $this->help_text( $field_settings ); ?> |
| 30 |
</div> |
| 31 |
</li> |
| 32 |
<?php |
| 33 |
|
| 34 |
$name = $field_settings['name']; |
| 35 |
$format = $field_settings["format"]; |
| 36 |
|
| 37 |
if ( $field_settings['time'] == 'yes' ) { |
| 38 |
$script = "jQuery(function($) { |
| 39 |
$('#wpuf-date-{$name}').datetimepicker({ dateFormat: '{$format}' }); |
| 40 |
});"; |
| 41 |
} else { |
| 42 |
$script = "jQuery(function($) { |
| 43 |
$('#wpuf-date-{$name}').datepicker({ dateFormat: '{$format}' }); |
| 44 |
});"; |
| 45 |
} |
| 46 |
|
| 47 |
wp_add_inline_script( 'wpuf-form', $script ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get field options setting |
| 52 |
* |
| 53 |
* @return array |
| 54 |
*/ |
| 55 |
public function get_options_settings() { |
| 56 |
$default_options = $this->get_default_option_settings(); |
| 57 |
|
| 58 |
$settings = [ |
| 59 |
[ |
| 60 |
'name' => 'format', |
| 61 |
'title' => __( 'Date Format', 'weforms' ), |
| 62 |
'type' => 'text', |
| 63 |
'section' => 'advanced', |
| 64 |
'priority' => 23, |
| 65 |
'help_text' => __( 'The date format', 'weforms' ), |
| 66 |
], |
| 67 |
[ |
| 68 |
'name' => 'time', |
| 69 |
'title' => '', |
| 70 |
'type' => 'checkbox', |
| 71 |
'is_single_opt' => true, |
| 72 |
'options' => [ |
| 73 |
'yes' => __( 'Enable time input', 'weforms' ), |
| 74 |
], |
| 75 |
'section' => 'advanced', |
| 76 |
'priority' => 24, |
| 77 |
'help_text' => '', |
| 78 |
], |
| 79 |
[ |
| 80 |
'name' => 'is_publish_time', |
| 81 |
'title' => '', |
| 82 |
'type' => 'checkbox', |
| 83 |
'is_single_opt' => true, |
| 84 |
'options' => [ |
| 85 |
'yes' => __( 'Set this as publish time input', 'weforms' ), |
| 86 |
], |
| 87 |
'section' => 'advanced', |
| 88 |
'priority' => 24, |
| 89 |
'help_text' => '', |
| 90 |
], |
| 91 |
]; |
| 92 |
|
| 93 |
return array_merge( $default_options, $settings ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Get the field props |
| 98 |
* |
| 99 |
* @return array |
| 100 |
*/ |
| 101 |
public function get_field_props() { |
| 102 |
$defaults = $this->default_attributes(); |
| 103 |
$props = [ |
| 104 |
'format' => 'dd/mm/yy', |
| 105 |
'time' => '', |
| 106 |
'is_publish_time' => '', |
| 107 |
]; |
| 108 |
|
| 109 |
return array_merge( $defaults, $props ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Prepare entry |
| 114 |
* |
| 115 |
* @param $field |
| 116 |
* |
| 117 |
* @return mixed |
| 118 |
*/ |
| 119 |
public function prepare_entry( $field, $args = [] ) { |
| 120 |
if( empty( $_POST['_wpnonce'] ) ) { |
| 121 |
wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); |
| 122 |
} |
| 123 |
|
| 124 |
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'wpuf_form_add' ) ) { |
| 125 |
wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); |
| 126 |
} |
| 127 |
|
| 128 |
$args = ! empty( $args ) ? $args : weforms_clean( $_POST ); |
| 129 |
|
| 130 |
return sanitize_text_field( trim( $args[$field['name']] ) ); |
| 131 |
} |
| 132 |
} |
| 133 |
|