1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* @file class-gravityview-field-date.php |
4
|
|
|
* @package GravityView |
5
|
|
|
* @subpackage includes\fields |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Add custom options for date fields |
10
|
|
|
*/ |
11
|
|
|
class GravityView_Field_Date extends GravityView_Field { |
12
|
|
|
|
13
|
|
|
var $name = 'date'; |
|
|
|
|
14
|
|
|
|
15
|
|
|
var $_gf_field_class_name = 'GF_Field_Date'; |
|
|
|
|
16
|
|
|
|
17
|
|
|
var $group = 'advanced'; |
|
|
|
|
18
|
|
|
|
19
|
|
|
public function __construct() { |
20
|
|
|
$this->label = esc_html__( 'Date', 'gravityview' ); |
21
|
|
|
parent::__construct(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
function field_options( $field_options, $template_id = '', $field_id = '', $context = '', $input_type = '' ) { |
|
|
|
|
25
|
|
|
|
26
|
|
|
if( 'edit' === $context ) { |
27
|
|
|
return $field_options; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$this->add_field_support('date_display', $field_options ); |
|
|
|
|
31
|
|
|
|
32
|
|
|
return $field_options; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get the default date format for a field based on the field ID and the time format setting |
37
|
|
|
* |
38
|
|
|
* @since 1.16.4 |
39
|
|
|
|
40
|
|
|
* @param string $date_format The Gravity Forms date format for the field. Default: "mdy" |
41
|
|
|
* @param int $field_id The ID of the field. Used to figure out full date/day/month/year |
42
|
|
|
* |
43
|
|
|
* @return string PHP date format for the date |
44
|
|
|
*/ |
45
|
|
|
static public function date_display( $value = '', $date_format = 'mdy', $field_id = 0 ) { |
|
|
|
|
46
|
|
|
|
47
|
|
|
// Let Gravity Forms figure out, based on the date format, what day/month/year values are. |
48
|
|
|
$parsed_date = GFCommon::parse_date( $value, $date_format ); |
49
|
|
|
|
50
|
|
|
// Are we displaying an input or the whole field? |
51
|
|
|
$field_input_id = gravityview_get_input_id_from_id( $field_id ); |
52
|
|
|
|
53
|
|
|
$date_field_output = ''; |
54
|
|
|
switch( $field_input_id ) { |
55
|
|
|
case 1: |
56
|
|
|
$date_field_output = rgar( $parsed_date, 'day' ); |
57
|
|
|
break; |
58
|
|
|
case 2: |
59
|
|
|
$date_field_output = rgar( $parsed_date, 'month' ); |
60
|
|
|
break; |
61
|
|
|
case 3: |
62
|
|
|
$date_field_output = rgar( $parsed_date, 'year' ); |
63
|
|
|
break; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @filter `gravityview_date_format` Whether to override the Gravity Forms date format with a PHP date format |
68
|
|
|
* @see https://codex.wordpress.org/Formatting_Date_and_Time |
69
|
|
|
* @param null|string Date Format (default: $field->dateFormat) |
70
|
|
|
*/ |
71
|
|
|
$full_date_format = apply_filters( 'gravityview_date_format', $date_format ); |
72
|
|
|
|
73
|
|
|
$full_date = GFCommon::date_display( $value, $full_date_format ); |
74
|
|
|
|
75
|
|
|
// If the field output is empty, use the full date. |
76
|
|
|
// Note: The output might be empty because $parsed_date didn't parse correctly. |
77
|
|
|
return ( '' === $date_field_output ) ? $full_date : $date_field_output; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
new GravityView_Field_Date; |
83
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.