Completed
Pull Request — master (#664)
by Zack
07:00 queued 03:02
created

GravityView_Field_Date::date_display()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 34
rs 8.439
cc 5
eloc 17
nc 8
nop 3
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 82.

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 $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...
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 = '' ) {
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...
25
26
		if( 'edit' === $context ) {
27
			return $field_options;
28
		}
29
30
		$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...
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 ) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
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