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

GravityView_Field_Product::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
ccs 0
cts 5
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 12 and the first side effect is on line 111.

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-product.php
4
 * @package GravityView
5
 * @subpackage includes\fields
6
 * @since 1.20
7
 */
8
9
/**
10
 * @since 1.20
11
 */
12
class GravityView_Field_Product extends GravityView_Field {
13
14
	var $name = 'product';
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...
15
16
	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...
17
18
	var $is_numeric = false;
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...
19
20
	var $search_operators = array( 'is', 'isnot', '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...
21
22
	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...
23
24
	/** @see GF_Field_Product */
25
	var $_gf_field_class_name = 'GF_Field_Product';
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...
26
27
	/**
28
	 * @since 1.20
29
	 */
30
	public function __construct() {
31
32
		add_filter( 'gravityview/edit_entry/field_blacklist', array( $this, 'edit_entry_field_blacklist' ), 10, 2 );
33
34
		add_filter( 'gravityview/edit_entry/after_update', array( $this, 'clear_product_info_cache' ), 10, 3 );
35
36
		parent::__construct();
37
	}
38
39
	/**
40
	 * If the edited entry has a product field and the fields are shown, remove entry purchase cache
41
	 *
42
	 * @since 1.20
43
	 *
44
	 * @param array $form Gravity Forms array
45
	 * @param int $entry_id Gravity Forms entry ID
46
	 * @param GravityView_Edit_Entry_Render $Edit_Entry_Render
47
	 *
48
	 * @return void
49
	 */
50 1
	function clear_product_info_cache( $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...
51
52 1
		if( $this->should_hide_product_fields( $Edit_Entry_Render->entry ) ) {
53
			return;
54
		}
55
56
		// Clear the purchase details so we can re-calculate them
57 1
		if ( GVCommon::has_product_field( $form ) ) {
58
			gform_delete_meta( $entry_id, 'gform_product_info__' );
59
			gform_delete_meta( $entry_id, 'gform_product_info__1' );
60
			gform_delete_meta( $entry_id, 'gform_product_info_1_' );
61
			gform_delete_meta( $entry_id, 'gform_product_info_1_1' );
62
		}
63
64 1
	}
65
66
	/**
67
	 * Maybe add Product fields to the Edit Entry blacklist
68
	 *
69
	 * @since 1.20
70
	 *
71
	 * @param array $blacklist Array of field types not to be shown in the Edit Entry form
72
	 * @param array $entry Gravity Forms entry array
73
	 *
74
	 * @return array Blacklist with product field types added, if should not be shown
75
	 */
76 1
	public function edit_entry_field_blacklist( $blacklist = array(), $entry = array() ) {
77
78 1
		if ( $this->should_hide_product_fields( $entry ) ) {
79
			$blacklist = array_merge( $blacklist, GVCommon::get_product_field_types() );
80
		}
81
82 1
		return $blacklist;
83
	}
84
85
	/**
86
	 * In Edit Entry, should Product fields be hidden? If entry has transaction data, they should be. Otherwise, no.
87
	 *
88
	 * @since 1.20
89
	 *
90
	 * @param array $entry Current Gravity Forms entry being edited
91
	 *
92
	 * @return bool True: hide product fields; False: show product fields
93
	 */
94 1
	public function should_hide_product_fields( $entry = array() ) {
95
96 1
		$has_transaction_data = GVCommon::entry_has_transaction_data( $entry );
97
98
		/**
99
		 * @filter `gravityview/edit_entry/hide-product-fields` Hide product fields from being editable
100
		 * @since 1.9.1
101
		 * @since 1.20 Changed default from false to whether or not entry has transaction data
102
		 * @see GVCommon::entry_has_transaction_data()
103
		 * @param boolean $hide_product_fields Whether to hide product fields in the editor. Uses $entry data to determine.
104
		 */
105 1
		$hide_product_fields = (bool) apply_filters( 'gravityview/edit_entry/hide-product-fields', $has_transaction_data );
106
107 1
		return $hide_product_fields;
108
	}
109
}
110
111
new GravityView_Field_Product;
112