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

GravityView_Field_Is_Fulfilled::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-is-fulfilled.php
4
 * @package GravityView
5
 * @subpackage includes\fields
6
 * @since 1.16
7
 */
8
9
class GravityView_Field_Is_Fulfilled extends GravityView_Field {
10
11
	var $name = 'is_fulfilled';
12
13
	var $is_searchable = true;
14
15
	var $is_numeric = false;
16
17
	var $search_operators = array( 'is', 'isnot' );
18
19
	var $group = 'pricing';
20
21
	var $_custom_merge_tag = 'is_fulfilled';
22
23
	/**
24
	 * @var int The value used by Gravity Forms when the order has not been fulfilled
25
	 */
26
	const NOT_FULFILLED = 0;
27
28
	/**
29
	 * @var int The value used by Gravity Forms when the order has been fulfilled
30
	 */
31
	const FULFILLED = 1;
32
33
	/**
34
	 * GravityView_Field_Is_Fulfilled constructor.
35
	 */
36
	public function __construct() {
37
		$this->label = esc_html__( 'Is Fulfilled', 'gravityview' );
38
39
		add_filter( 'gravityview_field_entry_value_' . $this->name . '_pre_link', array( $this, 'get_content' ), 10, 4 );
40
41
		parent::__construct();
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, $field_settings, $field ) {
59
60
		/** Overridden by a template. */
61
		if( ! empty( $field['field_path'] ) ) { return $output; }
62
63
		return $this->get_string_from_value( $output );
64
	}
65
66
	/**
67
	 * Get the string output based on the numeric value used by Gravity Forms
68
	 *
69
	 * @since 1.16
70
	 *
71
	 * @param int|string $value Number value for the field
72
	 *
73
	 * @return string
74
	 */
75 View Code Duplication
	private function get_string_from_value( $value ) {
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...
76
77
		switch ( intval( $value ) ) {
78
			case self::NOT_FULFILLED:
79
			default:
80
				$return = __('Not Fulfilled', 'gravityview');
81
				break;
82
83
			case self::FULFILLED:
0 ignored issues
show
Unused Code introduced by
case self::FULFILLED: ...avityview'); break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
84
				$return = __('Fulfilled', 'gravityview');
85
				break;
86
		}
87
88
		return $return;
89
	}
90
91
	/**
92
	 * Add {is_fulfilled} merge tag
93
	 *
94
	 * @since 1.16
95
	 **
96
	 * @param array $matches Array of Merge Tag matches found in text by preg_match_all
97
	 * @param string $text Text to replace
98
	 * @param array $form Gravity Forms form array
99
	 * @param array $entry Entry array
100
	 * @param bool $url_encode Whether to URL-encode output
101
	 *
102
	 * @return string Original text if {is_fulfilled} isn't found. Otherwise, "Not Fulfilled" or "Fulfilled"
103
	 */
104
	public function replace_merge_tag( $matches = array(), $text = '', $form = array(), $entry = array(), $url_encode = false, $esc_html = false  ) {
105
106
		$return = $text;
107
108
		foreach ( $matches as $match ) {
109
110
			$full_tag = $match[0];
111
112
			$fulfilled = rgar( $entry, 'is_fulfilled' );
113
114
			$value = $this->get_string_from_value( $fulfilled );
115
116
			$return = str_replace( $full_tag, $value, $return );
117
		}
118
119
		unset( $formatted_amount, $value, $amount, $full_tag, $matches );
120
121
		return $return;
122
	}
123
}
124
125
new GravityView_Field_Is_Fulfilled;
126