| 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 date 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 |
$use_theme_css = isset( $form_settings['use_theme_css'] ) ? $form_settings['use_theme_css'] : 'wpuf-style'; |
| 24 |
$value = ''; |
| 25 |
?> |
| 26 |
<li <?php $this->print_list_attributes( $field_settings ); ?>> |
| 27 |
<?php $this->print_label( $field_settings ); ?> |
| 28 |
|
| 29 |
<div class="wpuf-fields"> |
| 30 |
<input |
| 31 |
id="wpuf-date-<?php echo esc_attr( $field_settings['name'] ); ?>" |
| 32 |
type="text" |
| 33 |
<?php |
| 34 |
$field_settings['enforce_format'] = ! isset( $field_settings['enforce_format'] ) ? '' : $field_settings['enforce_format']; |
| 35 |
echo esc_attr( $field_settings['enforce_format'] !== 'yes' ) ? '' : 'readonly'; |
| 36 |
?> |
| 37 |
class="datepicker <?php echo ' wpuf_'.esc_attr( $field_settings['name'] ).'_'. esc_attr($form_id); ?>" |
| 38 |
data-required="<?php echo esc_attr($field_settings['required']) ?>" |
| 39 |
data-type="text" |
| 40 |
data-style="<?php echo esc_attr( $use_theme_css ); ?>" |
| 41 |
name="<?php echo esc_attr( $field_settings['name'] ); ?>" |
| 42 |
placeholder="<?php echo esc_attr( $field_settings['format'] ); ?>" |
| 43 |
value="<?php echo esc_attr( $value ) ?>" |
| 44 |
size="30" |
| 45 |
/> |
| 46 |
<?php $this->help_text( $field_settings ); ?> |
| 47 |
</div> |
| 48 |
</li> |
| 49 |
<?php |
| 50 |
|
| 51 |
$name = $field_settings['name']; |
| 52 |
$format = $field_settings["format"]; |
| 53 |
|
| 54 |
if ( $field_settings['time'] == 'yes' ) { |
| 55 |
$script = "jQuery(function($) { |
| 56 |
$('#wpuf-date-{$name}').datetimepicker({ dateFormat: '{$format}' }); |
| 57 |
});"; |
| 58 |
} else { |
| 59 |
$script = "jQuery(function($) { |
| 60 |
$('#wpuf-date-{$name}').datepicker({ dateFormat: '{$format}' }); |
| 61 |
});"; |
| 62 |
} |
| 63 |
|
| 64 |
wp_add_inline_script( 'wpuf-form', $script ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Get field options setting |
| 69 |
* |
| 70 |
* @return array |
| 71 |
*/ |
| 72 |
public function get_options_settings() { |
| 73 |
$default_options = $this->get_default_option_settings(); |
| 74 |
|
| 75 |
$settings = [ |
| 76 |
[ |
| 77 |
'name' => 'format', |
| 78 |
'title' => esc_html__( 'Date Format', 'weforms' ), |
| 79 |
'type' => 'text', |
| 80 |
'section' => 'advanced', |
| 81 |
'priority' => 23, |
| 82 |
'help_text' => esc_html__( 'The date format', 'weforms' ), |
| 83 |
], |
| 84 |
[ |
| 85 |
'name' => 'time', |
| 86 |
'title' => '', |
| 87 |
'type' => 'checkbox', |
| 88 |
'is_single_opt' => true, |
| 89 |
'options' => [ |
| 90 |
'yes' => esc_html__( 'Enable time input', 'weforms' ), |
| 91 |
], |
| 92 |
'section' => 'advanced', |
| 93 |
'priority' => 24, |
| 94 |
'help_text' => '', |
| 95 |
], |
| 96 |
[ |
| 97 |
'name' => 'is_publish_time', |
| 98 |
'title' => '', |
| 99 |
'type' => 'checkbox', |
| 100 |
'is_single_opt' => true, |
| 101 |
'options' => [ |
| 102 |
'yes' => esc_html__( 'Set this as publish time input', 'weforms' ), |
| 103 |
], |
| 104 |
'section' => 'advanced', |
| 105 |
'priority' => 24, |
| 106 |
'help_text' => '', |
| 107 |
], |
| 108 |
[ |
| 109 |
'name' => 'enforce_format', |
| 110 |
'title' => __( 'Toggle Keyboard input for Date', 'weforms' ), |
| 111 |
'type' => 'checkbox', |
| 112 |
'section' => 'advanced', |
| 113 |
'is_single_opt' => true, |
| 114 |
'options' => [ |
| 115 |
'yes' => esc_html__( 'Force Datepicker Input', 'weforms' ), |
| 116 |
], |
| 117 |
'default' => 'yes', |
| 118 |
'priority' => 24, |
| 119 |
'help_text' => esc_html__( 'Disables Keyboard Input and uses the Datepicker Format', 'weforms' ), |
| 120 |
], |
| 121 |
]; |
| 122 |
|
| 123 |
return array_merge( $default_options, $settings ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Get the field props |
| 128 |
* |
| 129 |
* @return array |
| 130 |
*/ |
| 131 |
public function get_field_props() { |
| 132 |
$defaults = $this->default_attributes(); |
| 133 |
$props = [ |
| 134 |
'format' => 'dd/mm/yy', |
| 135 |
'time' => '', |
| 136 |
'is_publish_time' => '', |
| 137 |
'enforce_format' => '', |
| 138 |
]; |
| 139 |
|
| 140 |
return array_merge( $defaults, $props ); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Prepare entry |
| 145 |
* |
| 146 |
* @param $field |
| 147 |
* |
| 148 |
* @return mixed |
| 149 |
*/ |
| 150 |
public function prepare_entry( $field, $args = [] ) { |
| 151 |
if( empty( $_POST['_wpnonce'] ) ) { |
| 152 |
wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); |
| 153 |
} |
| 154 |
|
| 155 |
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'wpuf_form_add' ) ) { |
| 156 |
wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); |
| 157 |
} |
| 158 |
|
| 159 |
$args = ! empty( $args ) ? $args : weforms_clean( $_POST ); |
| 160 |
|
| 161 |
return sanitize_text_field( trim( $args[$field['name']] ) ); |
| 162 |
} |
| 163 |
} |
| 164 |
|