Completed
Push — master ( d07b76...196a0a )
by Zack
18:54 queued 11:24
created

GravityView_Field_Total   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 21.74%

Importance

Changes 0
Metric Value
dl 0
loc 92
ccs 5
cts 23
cp 0.2174
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A add_to_blacklist() 0 10 3
B edit_entry_recalculate_totals() 0 25 4
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 9 and the first side effect is on line 102.

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-total.php
4
 * @package GravityView
5
 * @subpackage includes\fields
6
 * @since 1.20
7
 */
8
9
class GravityView_Field_Total extends GravityView_Field {
10
11
	var $name = 'total';
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...
12
13
	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...
14
15
	var $is_numeric = true;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $is_numeric.

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 $search_operators = array( 'is', 'isnot', 'greater_than', 'less_than', 'contains' );
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...
18
19
	var $group = 'product';
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...
20
21
	/** @see GF_Field_Total */
22
	var $_gf_field_class_name = 'GF_Field_Total';
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...
23
24
	public function __construct() {
25
		$this->label = esc_html__( 'Total', 'gravityview' );
26
27
		add_filter( 'gravityview/edit_entry/after_update', array( $this, 'edit_entry_recalculate_totals' ), 10, 3 );
28
29
		add_filter( 'gravityview_blacklist_field_types', array( $this, 'add_to_blacklist' ), 10, 2 );
30
31
		parent::__construct();
32
	}
33
34
	/**
35
	 * Prevent the Total fields from being displayed in the Edit Entry configuration screen -- for now
36
	 *
37
	 * Gravity Forms forms need to know all the pricing information available to calculate a Total.
38
	 *
39
	 * If you have an Edit Entry field with just two fields (Quantity and Total), the Total will not be able to calculate
40
	 * without the Product field, and possibly the Option, Shipping, and Coupon fields.
41
	 *
42
	 * The only options currently available are: show the whole form, or don't show the Total
43
	 *
44
	 * @since 1.20
45
	 *
46
	 * @todo Support Total fields in Edit Entry configuration
47
	 *
48
	 * @param array $blacklist Array of field types not able to be added to Edit Entry
49
	 * @param  string|null $context Context
50
	 *
51
	 * @return array Blacklist, with "total" added. If not edit context, original field blacklist. Otherwise, blacklist including total.
52
	 */
53
	public function add_to_blacklist( $blacklist = array(), $context = NULL  ){
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
54
55
		if( empty( $context ) || $context !== 'edit' ) {
0 ignored issues
show
introduced by
Found "!== '". Use Yoda Condition checks, you must
Loading history...
56
			return $blacklist;
57
		}
58
59
		$blacklist[] = 'total';
60
61
		return $blacklist;
62
	}
63
64
	/**
65
	 * If entry has totals fields, recalculate them
66
	 *
67
	 * @since 1.20
68
	 *
69
	 * @param array $form Gravity Forms form array
70
	 * @param int $entry_id Gravity Forms Entry ID
71
	 * @param GravityView_Edit_Entry_Render $Edit_Entry_Render
72
	 *
73
	 * @return void
74
	 */
75 1
	function edit_entry_recalculate_totals( $form = array(), $entry_id = 0, $Edit_Entry_Render = null ) {
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...
Unused Code introduced by
The parameter $Edit_Entry_Render is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
77 1
		$original_form = GFAPI::get_form( $form['id'] );
78
79 1
		$total_fields = GFCommon::get_fields_by_type( $original_form, 'total' );
80
81
		//saving total field as the last field of the form.
82 1
		if ( ! empty( $total_fields ) ) {
83
84
			$entry = GFAPI::get_entry( $entry_id );
85
86
			/** @var GF_Field_Total $total_field */
87
			foreach ( $total_fields as $total_field ) {
88
				$entry["{$total_field->id}"] = GFCommon::get_order_total( $original_form, $entry );
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
89
			}
90
91
			$return_entry = GFAPI::update_entry( $entry );
92
93
			if( is_wp_error( $return_entry ) ) {
94
				do_action( 'gravityview_log_error', __METHOD__ . ': Updating the entry total fields failed', $return_entry );
95
			} else {
96
				do_action( 'gravityview_log_debug', __METHOD__ . ': Updating the entry total fields succeeded' );
97
			}
98
		}
99 1
	}
100
}
101
102
new GravityView_Field_Total;
103