|
1
|
|
|
<?php |
|
|
|
|
|
|
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'; |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
var $is_searchable = true; |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
var $is_numeric = true; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
var $search_operators = array( 'is', 'isnot', 'greater_than', 'less_than', 'contains' ); |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
var $group = 'product'; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** @see GF_Field_Total */ |
|
22
|
|
|
var $_gf_field_class_name = 'GF_Field_Total'; |
|
|
|
|
|
|
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
|
|
|
parent::__construct(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* If entry has totals fields, recalculate them |
|
35
|
|
|
* |
|
36
|
|
|
* @since 1.20 |
|
37
|
|
|
* |
|
38
|
|
|
* @param array $form Gravity Forms form array |
|
39
|
|
|
* @param int $entry_id Gravity Forms Entry ID |
|
40
|
|
|
* @param GravityView_Edit_Entry_Render $Edit_Entry_Render |
|
41
|
|
|
* |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
44
|
|
|
function edit_entry_recalculate_totals( $form = array(), $entry_id = 0, $Edit_Entry_Render = null ) { |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
//saving total field as the last field of the form. |
|
47
|
|
|
if ( ! empty( $Edit_Entry_Render->total_fields ) ) { |
|
48
|
|
|
|
|
49
|
|
|
$entry = $Edit_Entry_Render->entry; |
|
50
|
|
|
|
|
51
|
|
|
/** @var GF_Field_Total $total_field */ |
|
52
|
|
|
foreach ( $Edit_Entry_Render->total_fields as $total_field ) { |
|
53
|
|
|
$entry["{$total_field->id}"] = GFCommon::get_order_total( $form, $Edit_Entry_Render->entry ); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$return_entry = GFAPI::update_entry( $entry ); |
|
57
|
|
|
|
|
58
|
|
|
if( is_wp_error( $return_entry ) ) { |
|
59
|
|
|
do_action( 'gravityview_log_error', __METHOD__ . ': Updating the entry total fields failed', $return_entry ); |
|
60
|
|
|
} else { |
|
61
|
|
|
do_action( 'gravityview_log_debug', __METHOD__ . ': Updating the entry total fields succeeded' ); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
new GravityView_Field_Total; |
|
68
|
|
|
|
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.