Completed
Push — develop ( 4bedcb...2ebfd1 )
by Zack
18:10
created

GravityView_Plugin_Hooks_Gravity_Forms_Signature   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 73
rs 10
wmc 7
lcom 0
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add_hooks() 0 9 1
A after_edit_entry() 0 8 2
A edit_entry_field_input() 0 18 4
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 19 and the first side effect is on line 93.

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
 * 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 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $entry_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
52
53
		$signature_fields = GFAPI::get_fields_by_type( $form, 'signature' );
54
55
		foreach ( $signature_fields as $field ) {
56
			unset( $_POST["input_{$field->id}"] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
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 ) {
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...
74
75
		$context = function_exists('gravityview_get_context') ? gravityview_get_context() : '';
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
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
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
87
		$_POST["input_{$form_id}_{$field->id}_signature_filename"] = $entry_value; // Used when Edit Entry form *is not* submitted
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
88
89
		return ''; // Return empty string to force using $_POST values instead
90
	}
91
}
92
93
new GravityView_Plugin_Hooks_Gravity_Forms_Signature;