Completed
Push — develop ( 7592b8...3eb7c5 )
by Zack
14:44
created

GravityView_Field_Date_Created::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 10
ccs 0
cts 7
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
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 8 and the first side effect is on line 106.

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-created.php
4
 * @package GravityView
5
 * @subpackage includes\fields
6
 */
7
8
class GravityView_Field_Date_Created extends GravityView_Field {
9
10
	var $name = 'date_created';
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...
11
12
	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...
13
14
	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...
15
16
	var $group = 'meta';
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...
17
18
	var $contexts = array( 'single', 'multiple', 'export' );
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $contexts.

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...
19
20
	var $_custom_merge_tag = 'date_created';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $_custom_merge_tag.

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...
21
22
	/**
23
	 * GravityView_Field_Date_Created constructor.
24
	 */
25
	public function __construct() {
26
27
		$this->label = esc_html__( 'Date Created', 'gravityview' );
28
		$this->default_search_label = $this->label;
29
		$this->description = esc_html__( 'The date the entry was created.', 'gravityview' );
30
31
		add_filter( 'gravityview_field_entry_value_' . $this->name . '_pre_link', array( $this, 'get_content' ), 10, 4 );
32
33
		parent::__construct();
34
	}
35
36
	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...
37
38
		if( 'edit' === $context ) {
39
			return $field_options;
40
		}
41
42
		$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...
43
44
		return $field_options;
45
	}
46
47
	/**
48
	 * Filter the value of the field
49
	 *
50
	 * @todo Consider how to add to parent class
51
	 *
52
	 * @since 1.16
53
	 *
54
	 * @param string $output HTML value output
55
	 * @param array  $entry The GF entry array
56
	 * @param array  $field_settings Settings for the particular GV field
57
	 * @param array  $field Current field being displayed
58
	 *
59
	 * @return String values for this field based on the numeric values used by Gravity Forms
60
	 */
61
	public function get_content( $output = '', $entry = array(), $field_settings = array(), $field = array() ) {
62
63
		/** Overridden by a template. */
64
		if( ! empty( $field['field_path'] ) ) { return $output; }
65
66
		return GVCommon::format_date( $field['value'], 'format='.rgar( $field_settings, 'date_display' ) );
67
	}
68
69
	/**
70
	 * Add {date_created} merge tag and format the values using format_date
71
	 *
72
	 * @since 1.16
73
	 *
74
	 * @see http://docs.gravityview.co/article/331-date-created-merge-tag for usage information
75
	 *
76
	 * @param array $matches Array of Merge Tag matches found in text by preg_match_all
77
	 * @param string $text Text to replace
78
	 * @param array $form Gravity Forms form array
79
	 * @param array $entry Entry array
80
	 * @param bool $url_encode Whether to URL-encode output
81
	 *
82
	 * @return string Original text if {date_created} isn't found. Otherwise, replaced text.
83
	 */
84 1
	public function replace_merge_tag( $matches = array(), $text = '', $form = array(), $entry = array(), $url_encode = false, $esc_html = false  ) {
85
86 1
		$return = $text;
87
88
		/** Use $this->name instead of date_created because Payment Date uses this as well*/
89 1
		$date_created = rgar( $entry, $this->name );
90
91 1
		foreach ( $matches as $match ) {
92
93 1
			$full_tag          = $match[0];
94 1
			$property          = $match[1];
95
96 1
			$formatted_date = GravityView_Merge_Tags::format_date( $date_created, $property );
97
98 1
			$return = str_replace( $full_tag, $formatted_date, $return );
99
		}
100
101 1
		return $return;
102
	}
103
104
}
105
106
new GravityView_Field_Date_Created;