-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathgw-check-in.php
403 lines (319 loc) · 9.92 KB
/
gw-check-in.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?php
/**
* Gravity Wiz // Gravity Forms // Check-In
*
* "Check-in" for Gravity Forms products.
*
* @version 0.3
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/...
*
* Plugin Name: Gravity Forms Check-in
* Plugin URI: http://gravitywiz.com/
* Description: "Check-in" for Gravity Forms products.
* Author: Gravity Wiz
* Version: 0.3
* Author URI: http://gravitywiz.com
*/
class GW_Check_In {
private $_args = array();
public function __construct( $args = array() ) {
// set our default arguments, parse against the provided arguments, and store for use throughout the class
$this->_args = wp_parse_args( $args, array(
'form_id' => false,
'name_field_id' => false,
'product_field_ids' => array(),
'labels' => array(
'check_in_title' => __( 'Check-in' ),
),
) );
// do version check in the init to make sure if GF is going to be loaded, it is already loaded
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
if ( ! is_callable( 'gp_post_content_merge_tags' ) ) {
return;
}
// carry on
$this->maybe_process_check_in();
add_filter( 'gform_entry_post_save', array( $this, 'populate_check_in_url_field' ), 11 );
add_filter( 'gform_entry_meta', array( $this, 'custom_entry_meta' ), 10, 2 );
add_filter( 'gform_entries_field_value', array( $this, 'checked_in_products_entry_meta_value' ), 10, 4 );
}
public function maybe_process_check_in() {
if ( $this->is_check_in_request() ) {
$entry = gp_post_content_merge_tags()->get_entry();
if ( $entry && $this->is_applicable_form( $entry['form_id'] ) ) {
$this->output_check_in_markup( $entry );
die();
}
}
}
public function is_check_in_request() {
return rgget( 'gwci' );
}
public function output_check_in_markup( $entry ) {
if ( ! current_user_can( $this->_args['check_in_role'] ) ) {
die( 'You do not have permission to check-in.' );
}
if ( rgpost( 'nonce' ) ) {
if ( ! wp_verify_nonce( rgpost( 'nonce' ), 'gwci_check_in' ) ) {
die( 'Invalid nonce.' );
}
foreach ( rgpost( 'products' ) as $product_id ) {
$this->check_in_product( rgpost( 'entry_id' ), $product_id );
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><?php echo $this->_args['labels']['check_in_title']; ?></title>
<style>
body {
background-color: #f1f1f1;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
padding: 3rem;
font-size: 3rem;
}
.success {
border: 0.1rem solid #ddd;
background-color: #fff;
padding: 3rem;
border-radius: 0 0.4rem 0.4rem 0;
border-left: 0.4rem solid rgba(70, 180, 79, 1.000);
}
.title h1 {
margin: 0 0 4rem;
}
.title .descriptor {
font-size: 75%;
display: block;
font-weight: normal;
}
.products ul {
list-style: none;
padding-left: 0;
margin: 4rem 0;
}
.products li label {
border: 0.1rem solid #ddd;
padding: 3rem;
border-radius: 1rem;
margin-bottom: 2rem;
background-color: #fff;
display: block;
cursor: pointer;
}
.products input:disabled + span {
opacity: 0.5;
}
.products input:checked + span {
font-weight: bold;
}
.products input {
width: 3rem;
height: 3rem;
vertical-align: middle;
margin: 0 1rem 0 0;
}
input[type="submit"] {
background-color: rgba( 59, 153, 252, 1.0 );
color: #fff;
padding: 4rem;
border: 0;
border-radius: 0.4rem;
font-size: 3rem;
cursor: pointer;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
input[type="submit"]:disabled {
background-color: rgba( 0, 0, 0, 0.25 );
opacity: 0.5;
cursor: not-allowed;
}
input[type="submit"]:not(:disabled):hover {
opacity: 0.9;
}
</style>
</head>
<body>
<form action="" method="post">
<?php wp_nonce_field( 'gwci_check_in', 'nonce' ); ?>
<div class="title">
<h1>
<span class="descriptor">Check-in for</span>
<span class="name"><?php echo rgar( $entry, $this->_args['name_field_id'] . '.3' ); ?> <?php echo rgar( $entry, $this->_args['name_field_id'] . '.6' ); ?></span>
</h1>
</div>
<div class="notices">
<?php
if ( rgpost( 'products' ) ) :
foreach ( rgpost( 'products' ) as $product_id ) :
$text = $this->get_product_display_label( $product_id, $entry );
if ( ! $text ) {
continue;
}
?>
<div class="success">
<?php echo rgar( $entry, $this->_args['name_field_id'] . '.3' ); ?> was checked in for <em><?php echo $text; ?></em>.
</div>
<?php
endforeach;
endif;
?>
</div>
<div class="products">
<ul>
<?php
foreach ( $this->_args['product_field_ids'] as $product_field_id ) {
$text = $this->get_product_display_label( $product_field_id, $entry );
if ( ! $text ) {
continue;
}
$disabled = $this->is_product_checked_in( $product_field_id, $entry['id'] ) ? 'disabled="disabled"' : '';
printf( '
<li>
<label for="product_%1$d">
<input type="checkbox" name="products[]" id="product_%1$d" value="%1$d" %3$s>
<span>%2$s</span>
</label>
</li>',
$product_field_id, $text, $disabled
);
}
?>
</ul>
</div>
<div class="adminstrative">
<input type="hidden" name="entry_id" value="<?php echo $entry['id']; ?>" />
</div>
<div class="actions">
<input type="submit" value="Check-in for Selected Products" disabled="disabled">
</div>
</form>
<?php wp_print_scripts( array( 'jquery' ) ); ?>
<script type="text/javascript">
( function( $ ) {
$( '.products input[type="checkbox"]' ).click( function() {
var $submit = $( 'input[type="submit"]' );
$submit.prop( 'disabled', $( '.products input[type="checkbox"]:checked' ).length <= 0 );
} );
} )( jQuery );
</script>
</body>
</html>
<?php
}
public function get_product_display_label( $product_id, $entry ) {
$field = GFAPI::get_field( $entry['form_id'], $product_id );
$field_label = $field->get_field_label( false, '' );
if ( ! is_array( $field->choices ) ) {
$text = $field_label;
} else {
$choice = $this->get_product_choice( $product_id, $entry );
if ( ! $choice ) {
return false;
}
$text = sprintf( '%s (%s)', $choice['text'], $field_label );
}
return $text;
}
public function check_in_product( $entry_id, $product_id ) {
gform_add_meta( $entry_id, 'gwci_checked_in_product', $product_id );
}
public function is_product_checked_in( $product_id, $entry_id ) {
global $wpdb;
$result = $wpdb->get_var(
$wpdb->prepare( "SELECT count( id ) FROM {$wpdb->prefix}gf_entry_meta WHERE entry_id = %d AND meta_key = 'gwci_checked_in_product' AND meta_value = %d", $entry_id, $product_id )
);
return $result > 0;
}
public function is_applicable_form( $form ) {
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
return empty( $this->_args['form_id'] ) || $form_id == $this->_args['form_id'];
}
public function get_check_in_url( $entry_id ) {
$pretty_id = gform_get_meta( $entry_id, 'gppcmt_pretty_id' );
return add_query_arg( array(
'gwci' => 1,
'eid' => $pretty_id,
), get_site_url() );
}
public function get_product_choice( $product_id, $entry ) {
if ( ! $this->is_applicable_form( $entry['form_id'] ) ) {
return false;
}
$form = GFAPI::get_form( $entry['form_id'] );
$field = GFFormsModel::get_field( $form, $product_id );
if ( is_array( $field->choices ) ) {
$value = explode( '|', rgar( $entry, $product_id ) );
$value = array_shift( $value );
$choices = $field->choices;
foreach ( $choices as $index => $choice ) {
if ( $choice['value'] == $value ) {
return $choice;
}
}
}
return false;
}
public function custom_entry_meta( $entry_meta, $form_id ) {
$entry_meta['gwci_checked_in_products'] = array(
'label' => 'Checked-in Products',
'is_numeric' => false,
'update_entry_meta_callback' => null,
'is_default_column' => true,
);
return $entry_meta;
}
public function checked_in_products_entry_meta_value( $value, $form_id, $field_id, $entry ) {
if ( $field_id != 'gwci_checked_in_products' ) {
return $value;
}
$value = array();
foreach ( $this->_args['product_field_ids'] as $product_id ) {
$field = GFAPI::get_field( $form_id, $product_id );
$field_label = $field->get_field_label( false, '' );
if ( ! is_array( $field->choices ) ) {
$text = $field_label;
} else {
$choice = $this->get_product_choice( $product_id, $entry );
if ( ! $choice ) {
continue;
}
$text = sprintf( '%s (%s)', $choice['text'], $field_label );
}
$is_checked_in = $this->is_product_checked_in( $product_id, $entry['id'] );
$icon = $is_checked_in ? '✓' : '✗';
$class = $is_checked_in ? 'checked-in' : 'not-checked-in';
$value[] = "<li class=\"{$class}\">{$icon} {$text}</li>";
}
$value[] = '<li><a href="' . $this->get_check_in_url( $entry['id'] ) . '">Manage Check-ins</a>';
return '<ul>' . implode( "\n", $value ) . '</ul>';
}
public function populate_check_in_url_field( $entry ) {
if ( ! $this->is_applicable_form( $entry['form_id'] ) ) {
return $entry;
}
$field_id = $this->_args['url_field_id'];
$entry[ $field_id ] = $this->get_check_in_url( $entry['id'] );
GFAPI::update_entry_field( $entry['id'], $field_id, $entry[ $field_id ] );
return $entry;
}
}
# Configuration
new GW_Check_In( array(
'form_id' => 12,
'name_field_id' => 5,
'product_field_ids' => array( 1, 3 ),
'url_field_id' => 4,
'check_in_role' => 'administrator',
'labels' => array(
'check_in_title' => 'Product Check-in',
),
) );