Completed
Push — master ( 4ba683...3a59f6 )
by Zack
31:21 queued 27:45
created

GravityView_Field_Date   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 53.33%

Importance

Changes 0
Metric Value
dl 0
loc 102
ccs 16
cts 30
cp 0.5333
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A field_options() 0 10 2
A apply_format_date_modifiers() 0 8 2
A date_display() 0 34 5
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 11 and the first side effect is on line 114.

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.

Loading history...
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';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $name.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
14
15
	var $_gf_field_class_name = 'GF_Field_Date';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $_gf_field_class_name.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
16
17
	var $is_searchable = true;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $is_searchable.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
18
19
	var $search_operators = array( 'less_than', 'greater_than', 'is', 'isnot' );
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $search_operators.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
20
21
	var $group = 'advanced';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $group.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
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 = '' ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
33
34
		if( 'edit' === $context ) {
35
			return $field_options;
36
		}
37
38
		$this->add_field_support('date_display', $field_options );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
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 ) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
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