|
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 $is_searchable = true; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
var $search_operators = array( 'less_than', 'greater_than', 'is', 'isnot' ); |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
var $group = 'advanced'; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
public function __construct() { |
|
24
|
|
|
|
|
25
|
|
|
$this->label = esc_html__( 'Date', 'gravityview' ); |
|
26
|
|
|
|
|
27
|
|
|
add_filter( 'gravityview/merge_tags/modifiers/value', array( $this, 'apply_format_date_modifiers' ), 10, 6 ); |
|
28
|
|
|
|
|
29
|
|
|
parent::__construct(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
function field_options( $field_options, $template_id = '', $field_id = '', $context = '', $input_type = '' ) { |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
if( 'edit' === $context ) { |
|
35
|
|
|
return $field_options; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$this->add_field_support('date_display', $field_options ); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
return $field_options; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Allow Date fields to take advantage of the GV date modifiers |
|
45
|
|
|
* |
|
46
|
|
|
* @since 2.0 |
|
47
|
|
|
* @uses GravityView_Merge_Tags::format_date |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $return The current merge tag value to be filtered. |
|
50
|
|
|
* @param string $raw_value The raw value submitted for this field. May be CSV or JSON-encoded. |
|
51
|
|
|
* @param string $value The original merge tag value, passed from Gravity Forms |
|
52
|
|
|
* @param string $merge_tag If the merge tag being executed is an individual field merge tag (i.e. {Name:3}), this variable will contain the field's ID. If not, this variable will contain the name of the merge tag (i.e. all_fields). |
|
53
|
|
|
* @param string $modifier The string containing any modifiers for this merge tag. For example, "maxwords:10" would be the modifiers for the following merge tag: `{Text:2:maxwords:10}`. |
|
54
|
|
|
* @param GF_Field $field The current field. |
|
55
|
|
|
* |
|
56
|
|
|
* @return string If Date field, run it through GravityView_Merge_Tags::format_date; otherwise, return the original value |
|
57
|
|
|
*/ |
|
58
|
|
|
public function apply_format_date_modifiers( $return, $raw_value = '', $value = '', $merge_tag = '', $modifier = '', $field = null ) { |
|
59
|
|
|
|
|
60
|
|
|
if ( 'date' === $field->type ) { |
|
61
|
|
|
$return = GravityView_Merge_Tags::format_date( $raw_value, $modifier ); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $return; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get the default date format for a field based on the field ID and the time format setting |
|
69
|
|
|
* |
|
70
|
|
|
* @since 1.16.4 |
|
71
|
|
|
|
|
72
|
|
|
* @param string $date_format The Gravity Forms date format for the field. Default: "mdy" |
|
73
|
|
|
* @param int $field_id The ID of the field. Used to figure out full date/day/month/year |
|
74
|
|
|
* |
|
75
|
|
|
* @return string PHP date format for the date |
|
76
|
|
|
*/ |
|
77
|
1 |
|
static public function date_display( $value = '', $date_format = 'mdy', $field_id = 0 ) { |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
// Let Gravity Forms figure out, based on the date format, what day/month/year values are. |
|
80
|
1 |
|
$parsed_date = GFCommon::parse_date( $value, $date_format ); |
|
81
|
|
|
|
|
82
|
|
|
// Are we displaying an input or the whole field? |
|
83
|
1 |
|
$field_input_id = gravityview_get_input_id_from_id( $field_id ); |
|
84
|
|
|
|
|
85
|
1 |
|
$date_field_output = ''; |
|
86
|
|
|
switch( $field_input_id ) { |
|
87
|
1 |
|
case 1: |
|
88
|
1 |
|
$date_field_output = \GV\Utils::get( $parsed_date, 'month' ); |
|
89
|
1 |
|
break; |
|
90
|
1 |
|
case 2: |
|
91
|
1 |
|
$date_field_output = \GV\Utils::get( $parsed_date, 'day' ); |
|
92
|
1 |
|
break; |
|
93
|
1 |
|
case 3: |
|
94
|
1 |
|
$date_field_output = \GV\Utils::get( $parsed_date, 'year' ); |
|
95
|
1 |
|
break; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @filter `gravityview_date_format` Whether to override the Gravity Forms date format with a PHP date format |
|
100
|
|
|
* @see https://codex.wordpress.org/Formatting_Date_and_Time |
|
101
|
|
|
* @param null|string Date Format (default: $field->dateFormat) |
|
102
|
|
|
*/ |
|
103
|
1 |
|
$full_date_format = apply_filters( 'gravityview_date_format', $date_format ); |
|
104
|
|
|
|
|
105
|
1 |
|
$full_date = GFCommon::date_display( $value, $full_date_format ); |
|
106
|
|
|
|
|
107
|
|
|
// If the field output is empty, use the full date. |
|
108
|
|
|
// Note: The output might be empty because $parsed_date didn't parse correctly. |
|
109
|
1 |
|
return ( '' === $date_field_output ) ? $full_date : $date_field_output; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
new GravityView_Field_Date; |
|
115
|
|
|
|
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.