|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* Customization for the Gravity Forms Signature Addon |
|
4
|
|
|
* |
|
5
|
|
|
* @file class-gravityview-plugin-hooks-gravity-forms-signature.php |
|
6
|
|
|
* @package GravityView |
|
7
|
|
|
* @license GPL2+ |
|
8
|
|
|
* @author Katz Web Services, Inc. |
|
9
|
|
|
* @link http://gravityview.co |
|
10
|
|
|
* @copyright Copyright 2016, Katz Web Services, Inc. |
|
11
|
|
|
* |
|
12
|
|
|
* @since 1.17 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @inheritDoc |
|
17
|
|
|
* @since 1.17 |
|
18
|
|
|
*/ |
|
19
|
|
|
class GravityView_Plugin_Hooks_Gravity_Forms_Signature extends GravityView_Plugin_and_Theme_Hooks { |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @type string Class that should be exist in a plugin or theme. Used to check whether plugin is active. |
|
23
|
|
|
* @since 1.17 |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $class_name = 'GFSignature'; |
|
26
|
|
|
|
|
27
|
|
|
protected function add_hooks() { |
|
28
|
|
|
|
|
29
|
|
|
// Remove $_POST values so GF doesn't change output HTML laout |
|
30
|
|
|
add_action( 'gravityview/edit_entry/after_update', array( $this, 'after_edit_entry' ), 10, 2 ); |
|
31
|
|
|
|
|
32
|
|
|
// Set the priority to 5 so it processes before the Signature field input is generated |
|
33
|
|
|
// This way, we can modify the $_POST value before the Signature Addon uses it. |
|
34
|
|
|
add_filter( 'gform_field_input', array( $this, 'edit_entry_field_input' ), 5, 5 ); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* We need to remove the $value used by Gravity Forms so it instead checks for the $_POST field values |
|
39
|
|
|
* |
|
40
|
|
|
* In ~line 541, this code would be used if we didn't override in this method: |
|
41
|
|
|
* |
|
42
|
|
|
* `if (RG_CURRENT_VIEW == "entry" && $value){` |
|
43
|
|
|
* |
|
44
|
|
|
* We don't want that code (with the download/delete icons). So unsetting the $_POST here forces using the "sign again" code instead. |
|
45
|
|
|
* |
|
46
|
|
|
* @see GFSignature::signature_input |
|
47
|
|
|
* |
|
48
|
|
|
* @param array $form GF form array |
|
49
|
|
|
* @param int $entry_id Entry ID being edited |
|
50
|
|
|
*/ |
|
51
|
|
|
function after_edit_entry( $form, $entry_id ) { |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
$signature_fields = GFAPI::get_fields_by_type( $form, 'signature' ); |
|
54
|
|
|
|
|
55
|
|
|
foreach ( $signature_fields as $field ) { |
|
56
|
|
|
unset( $_POST["input_{$field->id}"] ); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* The Signature Addon only displays the output in the editable form if it thinks it's in the Admin or a form has been submitted |
|
62
|
|
|
* |
|
63
|
|
|
* @since 1.17 |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $field_content Always empty. Returning not-empty overrides the input. |
|
66
|
|
|
* @param GF_Field $field |
|
67
|
|
|
* @param string|array $value If array, it's a field with multiple inputs. If string, single input. |
|
68
|
|
|
* @param int $lead_id Lead ID. Always 0 for the `gform_field_input` filter. |
|
69
|
|
|
* @param int $form_id Form ID |
|
70
|
|
|
* |
|
71
|
|
|
* @return string Empty string forces Gravity Forms to use the $_POST values |
|
72
|
|
|
*/ |
|
73
|
|
|
function edit_entry_field_input( $field_content = '', $field, $value = '', $lead_id = 0, $form_id = 0 ) { |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
$context = function_exists('gravityview_get_context') ? gravityview_get_context() : ''; |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
if( 'signature' !== $field->type || 'edit' !== $context ) { |
|
78
|
|
|
return $field_content; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// We need to fetch a fresh version of the entry, since the saved entry hasn't refreshed in GV yet. |
|
82
|
|
|
$entry = GravityView_View::getInstance()->getCurrentEntry(); |
|
83
|
|
|
$entry = GFAPI::get_entry( $entry['id'] ); |
|
84
|
|
|
$entry_value = rgar( $entry, $field->id ); |
|
85
|
|
|
|
|
86
|
|
|
$_POST["input_{$field->id}"] = $entry_value; // Used when Edit Entry form *is* submitted |
|
|
|
|
|
|
87
|
|
|
$_POST["input_{$form_id}_{$field->id}_signature_filename"] = $entry_value; // Used when Edit Entry form *is not* submitted |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
return ''; // Return empty string to force using $_POST values instead |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
new GravityView_Plugin_Hooks_Gravity_Forms_Signature; |
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.