Round Up Price
Rounds up the Pay Per Word calculated price to the nearest whole dollar amount.
Instructions
See “Where do I put snippets?” in our documentation for installation instructions.
Code
Filename: gpppw-round-up-pricing.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* Gravity Wiz // Pay Per Word // Round Up Price
* https://gravitywiz.com/documentation/gravity-forms-pay-per-word/
*
* Rounds up the Pay Per Word calculated price to the nearest whole dollar amount.
*
* Plugin Name: GP Pay Per Word - Round Up Price
* Plugin URI: https://gravitywiz.com/documentation/gravity-forms-pay-per-word/
* Description: Rounds up the Pay Per Word calculated price to the nearest whole dollar amount.
* Version: 0.1
* Author URI: https://gravitywiz.com/
*/
class GW_Pay_Per_Word_Round_Up_Price {
private $_args = array();
public function __construct( $args = array() ) {
$this->_args = wp_parse_args( $args, array(
'form_id' => false,
'field_id' => false,
) );
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
// Hook into the Pay Per Word price filter
add_filter( 'gwppw_price', array( $this, 'round_up_price' ), 10, 4 );
// Add JavaScript for frontend price rounding
add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 );
add_action( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 10, 2 );
}
/**
* Round up the calculated price to the nearest whole dollar
*
* @param float $price The calculated price
* @param object $price_field The price field object
* @param object $word_field The word field object
* @param int $word_count The calculated word count
* @return float The rounded up price
*/
public function round_up_price( $price, $price_field, $word_field, $word_count ) {
if ( ! $this->is_applicable_form( $price_field->formId ) ) {
return $price;
}
if ( ! $this->is_applicable_field( $price_field ) ) {
return $price;
}
return ceil( $price );
}
public function load_form_script( $form, $is_ajax_enabled ) {
if ( $this->is_applicable_form( $form ) && $this->has_pay_per_word_field( $form ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) {
add_action( 'wp_footer', array( $this, 'output_script' ) );
add_action( 'gform_preview_footer', array( $this, 'output_script' ) );
}
return $form;
}
public function output_script() {
?>
<script type="text/javascript">
( function( $ ) {
window.GWPayPerWordRoundUpPrice = function( args ) {
var self = this;
for( var prop in args ) {
if( args.hasOwnProperty( prop ) ) {
self[ prop ] = args[ prop ];
}
}
self.isApplicableField = function( fieldId ) {
if ( typeof fieldId !== 'number' ) {
fieldId = parseInt( fieldId );
}
if ( ! self.fieldId ) {
return true;
}
if ( typeof self.fieldId !== 'object' ) {
self.fieldId = [ self.fieldId ];
}
self.fieldId = self.fieldId.map( function( fieldId ) {
if ( typeof fieldId === 'string' ) {
fieldId = parseInt( fieldId );
}
return fieldId;
} );
return self.fieldId.indexOf( fieldId ) !== -1;
};
self.init = function() {
gform.addFilter( 'gwppw_price', function( price, wordCount, pricePerWord, ppwField, formId ) {
if ( self.formId && parseInt( formId ) !== parseInt( self.formId ) ) {
return price;
}
if ( ! self.isApplicableField( ppwField.price_field ) ) {
return price;
}
return Math.ceil( price );
} );
gform.addFilter( 'gpppw_price', function( price, wordCount, pricePerWord, ppwField, formId ) {
if ( self.formId && parseInt( formId ) !== parseInt( self.formId ) ) {
return price;
}
if ( ! self.isApplicableField( ppwField.price_field ) ) {
return price;
}
return Math.ceil( price );
} );
};
self.init();
};
} )( jQuery );
</script>
<?php
}
public function add_init_script( $form ) {
if ( ! $this->is_applicable_form( $form ) || ! $this->has_pay_per_word_field( $form ) ) {
return;
}
$args = array(
'formId' => $this->_args['form_id'],
'fieldId' => $this->_args['field_id'],
);
$script = 'new GWPayPerWordRoundUpPrice( ' . json_encode( $args ) . ' );';
$slug = implode( '_', array( 'gw_pay_per_word_round_up_price', $this->_args['form_id'], $this->_args['field_id'] ) );
GFFormDisplay::add_init_script( $form['id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
}
public function is_applicable_form( $form ) {
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
return empty( $this->_args['form_id'] ) || (int) $form_id === (int) $this->_args['form_id'];
}
public function is_applicable_field( $field ) {
$field_id = isset( $field['id'] ) ? $field['id'] : ( isset( $field->id ) ? $field->id : $field );
return empty( $this->_args['field_id'] ) || (int) $field_id === (int) $this->_args['field_id'];
}
public function has_pay_per_word_field( $form ) {
foreach ( $form['fields'] as $field ) {
if ( rgar( $field, 'gwpayperword_enable' ) ) {
return true;
}
}
return false;
}
}
# Configuration
// Apply to all forms and fields
new GW_Pay_Per_Word_Round_Up_Price();
// Apply to specific form
// new GW_Pay_Per_Word_Round_Up_Price( array(
// 'form_id' => 1,
// ) );
// Apply to specific field on specific form
// new GW_Pay_Per_Word_Round_Up_Price( array(
// 'form_id' => 1,
// 'field_id' => 2,
// ) );