Completed
Push — master ( 3ac4b1...21157a )
by Zack
05:18
created

GravityView_Field_Date_Created::get_content()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4285
cc 2
eloc 3
nc 2
nop 4
1
<?php
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';
11
12
	var $search_operators = array( 'less_than', 'greater_than', 'is', 'isnot' );
13
14
	var $group = 'meta';
15
16
	var $contexts = array( 'single', 'multiple', 'export' );
17
18
	var $_custom_merge_tag = 'date_created';
19
20
	/**
21
	 * GravityView_Field_Date_Created constructor.
22
	 */
23 View Code Duplication
	public function __construct() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

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